File size: 45,226 Bytes
56d4bda f812229 56d4bda |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 |
# coding=utf-8
# Copyright 2022 Mesh TensorFlow authors, Manta Authors and HuggingFace Inc. team.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
""" PyTorch Manta model."""
import math
from dataclasses import dataclass
import warnings
from typing import Optional, Tuple, Union
import torch
from torch import nn
from torch.nn import CrossEntropyLoss
from transformers.modeling_outputs import BaseModelOutput, Seq2SeqLMOutput, Seq2SeqModelOutput
from transformers.modeling_utils import PreTrainedModel
from transformers.models.longformer import LongformerConfig, LongformerModel
from transformers.models.t5.configuration_t5 import T5Config
from transformers.models.t5.modeling_t5 import (
__HEAD_MASK_WARNING_MSG,
T5Attention,
T5Stack,
)
from transformers.utils import (
DUMMY_INPUTS,
DUMMY_MASK,
add_start_docstrings,
add_end_docstrings,
is_torch_fx_proxy,
logging,
replace_return_docstrings,
)
from .configuration_manta import MantaConfig
logger = logging.get_logger(__name__)
_CONFIG_FOR_DOC = "MantaConfig"
_TOKENIZER_FOR_DOC = "ByT5Tokenizer"
MANTA_PRETRAINED_MODEL_ARCHIVE_LIST = []
def gaussian_pdf(x):
return torch.exp(-x * x / 2.0)
def pad_block_embeddings(block_embeddings, pad_length):
if not pad_length:
return block_embeddings
padding_tensor_len = max(pad_length - block_embeddings.size(1), 0)
padding_tensor = torch.zeros(
(block_embeddings.size(0), padding_tensor_len, block_embeddings.size(2)),
device=block_embeddings.device,
dtype=block_embeddings.dtype,
)
return torch.cat([block_embeddings[:, :pad_length, :], padding_tensor], dim=1)
@add_end_docstrings()
@dataclass
class MantaSeq2SeqLMOutput(Seq2SeqLMOutput):
"""
Base class for Manta encoder's outputs that also contains : pre-computed hidden states that can speed up sequential
decoding.
Args:
last_hidden_state (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`):
Sequence of hidden-states at the output of the last layer of the decoder of the model.
If `past_key_values` is used only the last hidden-state of the sequences of shape `(batch_size, 1,
hidden_size)` is output.
past_key_values (`tuple(tuple(torch.FloatTensor))`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`):
Tuple of `tuple(torch.FloatTensor)` of length `config.n_layers`, with each tuple having 2 tensors of shape
`(batch_size, num_heads, sequence_length, embed_size_per_head)`) and 2 additional tensors of shape
`(batch_size, num_heads, encoder_sequence_length, embed_size_per_head)`.
Contains pre-computed hidden-states (key and values in the self-attention blocks and in the cross-attention
blocks) that can be used (see `past_key_values` input) to speed up sequential decoding.
decoder_hidden_states (`tuple(torch.FloatTensor)`, *optional*, returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`):
Tuple of `torch.FloatTensor` (one for the output of the embeddings, if the model has an embedding layer, +
one for the output of each layer) of shape `(batch_size, sequence_length, hidden_size)`.
Hidden-states of the decoder at the output of each layer plus the optional initial embedding outputs.
decoder_attentions (`tuple(torch.FloatTensor)`, *optional*, returned when `output_attentions=True` is passed or when `config.output_attentions=True`):
Tuple of `torch.FloatTensor` (one for each layer) of shape `(batch_size, num_heads, sequence_length,
sequence_length)`.
Attentions weights of the decoder, after the attention softmax, used to compute the weighted average in the
self-attention heads.
cross_attentions (`tuple(torch.FloatTensor)`, *optional*, returned when `output_attentions=True` is passed or when `config.output_attentions=True`):
Tuple of `torch.FloatTensor` (one for each layer) of shape `(batch_size, num_heads, sequence_length,
sequence_length)`.
Attentions weights of the decoder's cross-attention layer, after the attention softmax, used to compute the
weighted average in the cross-attention heads.
encoder_last_hidden_state (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*):
Sequence of hidden-states at the output of the last layer of the encoder of the model.
encoder_hidden_states (`tuple(torch.FloatTensor)`, *optional*, returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`):
Tuple of `torch.FloatTensor` (one for the output of the embeddings, if the model has an embedding layer, +
one for the output of each layer) of shape `(batch_size, sequence_length, hidden_size)`.
Hidden-states of the encoder at the output of each layer plus the optional initial embedding outputs.
encoder_attentions (`tuple(torch.FloatTensor)`, *optional*, returned when `output_attentions=True` is passed or when `config.output_attentions=True`):
Tuple of `torch.FloatTensor` (one for each layer) of shape `(batch_size, num_heads, sequence_length,
sequence_length)`.
Attentions weights of the encoder, after the attention softmax, used to compute the weighted average in the
self-attention heads.
frontier_predictions: (`torch.FloatTensor`, *optional*, of shape `(batch_size, sequence_length, 1)`):
Probability scores of being a frontier as predicted by the FrontierPredictor module.
"""
frontier_predictions: Optional[torch.FloatTensor] = None
@dataclass
class MantaBaseModelOutput(BaseModelOutput):
"""
Base class for Manta's outputs, with potential hidden states, attentions and Manta's frontier predictions.
Args:
last_hidden_state (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`):
Sequence of hidden-states at the output of the last layer of the model.
hidden_states (`tuple(torch.FloatTensor)`, *optional*, returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`):
Tuple of `torch.FloatTensor` (one for the output of the embeddings, if the model has an embedding layer, +
one for the output of each layer) of shape `(batch_size, sequence_length, hidden_size)`.
Hidden-states of the model at the output of each layer plus the optional initial embedding outputs.
attentions (`tuple(torch.FloatTensor)`, *optional*, returned when `output_attentions=True` is passed or when `config.output_attentions=True`):
Tuple of `torch.FloatTensor` (one for each layer) of shape `(batch_size, num_heads, sequence_length,
sequence_length)`.
Attentions weights after the attention softmax, used to compute the weighted average in the self-attention
heads.
frontier_predictions: (`torch.FloatTensor`, *optional*, of shape `(batch_size, sequence_length, 1)`):
Probability scores of being a frontier as predicted by the FrontierPredictor module.
"""
frontier_predictions: Optional[torch.FloatTensor] = None
class MantaFrontierPredictor(nn.Module):
def __init__(
self,
hidden_size,
num_layers,
num_attention_heads,
dropout_rate,
attention_window,
max_length,
):
super().__init__()
# First, find out what the maximum position will be after tensors are padded to a multiple of local_transformer_attention_window.
# Then, add 1 because LongFormer position embeddings are bugged when passed inputs_embeds.
max_position_embeddings = (max_length // attention_window + 1) * attention_window + 1
self.hidden_size = hidden_size
self.config = LongformerConfig(
attention_probs_dropout_prob=dropout_rate,
attention_window=attention_window,
hidden_act="gelu",
hidden_dropout_prob=dropout_rate,
hidden_size=hidden_size,
intermediate_size=hidden_size * 4,
max_position_embeddings=max_position_embeddings,
num_attention_heads=num_attention_heads,
num_hidden_layers=num_layers,
position_embedding_type="absolute", # Actually cannot be changed
vocab_size=1, # Remove almost entirely the embeddings
pad_token_id=0,
)
self.local_transformer = LongformerModel(self.config)
self.output_projection = nn.Linear(hidden_size, 1)
def forward(self, embeddings, attention_mask):
longformer_output = self.local_transformer(inputs_embeds=embeddings, attention_mask=attention_mask)
projection_outputs = self.output_projection(longformer_output.last_hidden_state)
frontier_predictions = torch.sigmoid(projection_outputs.squeeze(-1))
return frontier_predictions
class MantaConvFeatures(nn.Module):
def __init__(
self,
in_channels,
out_channels,
kernel_size,
groups,
padding,
):
"""
This nn.Module "decomposes" the convolution in order to extract and cache feature maps. This amounts to
computing an element-wise multiplication between weights of size (hidden_dim, kernel_size) and the input.
"""
super().__init__()
self.in_channels = in_channels
self.out_channels = out_channels
self.kernel_size = kernel_size
self.groups = groups
self.padding = padding
if groups == in_channels:
assert (
in_channels == out_channels
), "When using `groups = in_channels`, make sure to have `in_channels == out_channels`"
self.weight = nn.Parameter(torch.Tensor(1, 1, kernel_size, out_channels))
elif self.groups == 1:
self.weight = nn.Parameter(torch.Tensor(in_channels, out_channels, kernel_size))
else:
raise ValueError("MantaConvFeatures only supports `groups = 1` or `groups = in_channels`")
left_pad = (kernel_size - 1) // 2
self.pad = (left_pad, kernel_size - 1 - left_pad)
self.reset_parameters()
def reset_parameters(self):
"""
See https://pytorch.org/docs/stable/_modules/torch/nn/modules/conv.html#Conv1d, in the `_ConvNd` class :
> Setting a=sqrt(5) in kaiming_uniform is the same as initializing with
> uniform(-1/sqrt(k), 1/sqrt(k)), where k = weight.size(1) * prod(*kernel_size)
> For more details see: https://github.com/pytorch/pytorch/issues/15314#issuecomment-477448573"
The reason we permute the weights before init is because `kaiming_uniform_` uses the number of in and out
features for initialization, which are computed as tensor.size(0) and tensor.size(1). However, these
dimensions do not correspond for my weights.
"""
if self.groups == self.out_channels:
nn.init.kaiming_uniform_(self.weight.permute(3, 0, 1, 2), a=math.sqrt(5))
else:
nn.init.kaiming_uniform_(self.weight, a=math.sqrt(5))
def forward(self, x: torch.Tensor):
if self.groups == 1:
return self.forward_matmul(x)
else:
return self.forward_elementwise(x)
def forward_matmul(self, x: torch.Tensor):
if self.padding == "same":
padded_x = self._pad_pre_conv(x)
else:
padded_x = x
bs, _, seq_len = padded_x.size()
padded_x = padded_x.transpose(-1, -2)
# Size: (bs, seq_len+pad, hidden)
out = padded_x.matmul(self.weight.view(self.weight.size(0), -1)).view(bs, seq_len, self.out_channels, -1)
# Size: (bs, seq_len+pad, hidden, kernel_size)
return out.permute(0, 2, 3, 1)
def forward_elementwise(self, x: torch.Tensor):
assert len(x.size()) == 3
assert x.size(1) == self.out_channels
# Size: (bs, hidden, seq_len)
if self.padding == "same":
padded_x = self._pad_pre_conv(x)
else:
padded_x = x
# Unsqueeze for broadcasting with the kernel_size dim of the filters
padded_x = padded_x.transpose(-1, -2).unsqueeze(2)
# Size: (bs, seq_len, 1, hidden)
out = padded_x * self.weight
# Size: (bs, seq_len, kernel_size, hidden)
return out.transpose(1, 3)
def _pad_pre_conv(self, inp: torch.Tensor):
"""
Pad with zeros at the beginning and end just like `nn.Conv1d`.
"""
return nn.functional.pad(inp, self.pad, "constant", 0.0)
def extra_repr(self):
return "in_features={}, out_features={}, kernel_size={}, groups={}".format(
self.in_channels, self.out_channels, self.kernel_size, self.groups
)
class MantaCachedConvolutionPooling(nn.Module):
def __init__(
self,
padding_length,
output_dim,
kernel_size,
hidden_dim,
depthwise_convolution,
variance_regularization,
mean_pool,
):
super().__init__()
self.padding_length = padding_length
self.output_dim = output_dim
self.kernel_size = kernel_size
self.hidden_dim = hidden_dim
self.depthwise_convolution = depthwise_convolution
self.variance_regularization = variance_regularization
self.mean_pool = mean_pool
if isinstance(self.kernel_size, int):
self.kernel_size = [[self.kernel_size, hidden_dim]]
self.conv_output_dim = sum([k_dim[1] for k_dim in self.kernel_size])
# Since the sum of the hidden dimensions of all the filters might not match the language model hidden size, we
# specify it here
self.out_projection = nn.Linear(self.conv_output_dim, self.output_dim, bias=True)
self.conv_layers = nn.Sequential(
*[
MantaConvFeatures(self.hidden_dim, h, k, groups=h if self.depthwise_convolution else 1, padding="same")
for (k, h) in self.kernel_size
]
)
self.eps = None
self.conv_layer = None
def forward(self, unconstrained_separation_probs: torch.Tensor, byte_embeddings: torch.Tensor):
device = unconstrained_separation_probs.device
if self.eps is None:
self.eps = 5 * torch.finfo(unconstrained_separation_probs.dtype).resolution
self.variance_regularization = max(self.eps, self.variance_regularization)
if self.conv_layer is not None:
self.conv_layer = self.conv_layer.to(device)
batch_size, seq_len = byte_embeddings.shape[:2]
# We set the probability of the first token to be 0 therwise the cumsum will not work
separation_probs = unconstrained_separation_probs.clone()
separation_probs[:, 0] = 0
assert separation_probs.shape == (batch_size, seq_len)
# Compute the moments of the block_id random variable
block_id_expectation = separation_probs.cumsum(axis=-1)
block_id_std = torch.sqrt(
(separation_probs * (1.0 - separation_probs)).cumsum(axis=-1) + self.variance_regularization
)
# Get the maximum number of blocks
max_nb_blocks = min(seq_len, (block_id_expectation + 3 * block_id_std).max().int().item() + 1)
possible_blocks_id = torch.arange(max_nb_blocks).to(device)
# Get the block/byte proba using the Gaussian PDF
log_scale = block_id_std[:, None, :].log()
log_proba = (
-((block_id_expectation[:, None, :] - possible_blocks_id[None, :, None]) ** 2)
/ (2 * block_id_std[:, None, :])
- log_scale
- math.log((2 * math.pi) ** 0.5)
)
block_byte_proba = log_proba.softmax(-2)
token_size = block_byte_proba.sum(-1, keepdim=True)
regularized_token_size = torch.maximum(token_size, torch.ones_like(token_size))
if self.mean_pool:
block_byte_proba_normalized = block_byte_proba / regularized_token_size
else:
# Makes no sense to regularize using sequence length in the max_pooling case.
block_byte_proba_normalized = block_byte_proba
block_embeddings = self.pooling(byte_embeddings, block_byte_proba_normalized)
pad_length = min(self.padding_length, max_nb_blocks)
block_embeddings = pad_block_embeddings(block_embeddings, pad_length)
block_embeddings = self.out_projection(block_embeddings)
return block_embeddings
def pooling(self, embeddings: torch.Tensor, block_byte_proba: torch.Tensor):
block_embeddings = []
for conv_layer in self.conv_layers:
# First, compute the convolution maps SEPARATELY, i.e. without summing them together, only the element wise multiplication
# This is similar to a cache that we'll reuse for each block probabilities.
features = conv_layer(embeddings.transpose(1, 2)).permute(0, 3, 1, 2)
# Size : (batch_size, seq_len + padding, hidden_dim, kernel_size)
pad = conv_layer.pad
for i in range(0, conv_layer.kernel_size):
# We shift like that to match the padding done inside `conv_layer`
features[..., i] = features[..., i].roll(pad[0] - i, 1)
# Cut out the padded vector to obtain the right sequence length at the end
features = features[:, pad[1] : features.size(1) - pad[0]]
# Size : (batch_size, seq_len, hidden_dim, kernel_size)
# Then, artificially sum the convolution features by shifting the input bytes
padded_block_byte_proba = nn.functional.pad(block_byte_proba, pad, "constant", 0.0)
expanded_block_byte_proba = []
for i in range(0, conv_layer.kernel_size):
rolled_proba = padded_block_byte_proba.clone().roll(pad[0] - i, -1)
expanded_block_byte_proba.append(rolled_proba)
expanded_block_byte_proba = torch.stack(expanded_block_byte_proba, -1)
# We use :tensor.size(2) - pad instead of just :-pad because if pad = 0, we have an undesired behaviour where the whole sequence is removed
expanded_block_byte_proba = expanded_block_byte_proba[
:, :, pad[1] : expanded_block_byte_proba.size(2) - pad[0], :
]
# Size : (batch_size, block_size, seq_len, kernel_size)
if self.mean_pool:
convolved = torch.einsum("b s h k, b B s k -> b B h", features, expanded_block_byte_proba)
else:
convolved = torch.einsum("b s h k, b B s k -> b B s h", features, expanded_block_byte_proba)
convolved = convolved.max(dim=-2).values
block_embeddings.append(convolved)
block_embeddings = torch.cat(block_embeddings, dim=-1)
return block_embeddings
class MantaPreTrainedModel(PreTrainedModel):
"""
An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained
models.
"""
config_class = MantaConfig
base_model_prefix = "transformer"
supports_gradient_checkpointing = True
def _init_weights(self, module):
"""Initialize the weights"""
pass
def _set_gradient_checkpointing(self, module, value=False):
if isinstance(module, (T5Attention, T5Stack)):
module.gradient_checkpointing = value
def _shift_right(self, input_ids):
decoder_start_token_id = self.config.decoder_start_token_id
pad_token_id = self.config.pad_token_id
assert decoder_start_token_id is not None, (
"self.model.config.decoder_start_token_id has to be defined. In T5 it is usually set to the pad_token_id."
" See T5 docs for more information"
)
# shift inputs to the right
if is_torch_fx_proxy(input_ids):
# Item assignment is not supported natively for proxies.
shifted_input_ids = torch.full(input_ids.shape[:-1] + (1,), decoder_start_token_id)
shifted_input_ids = torch.cat([shifted_input_ids, input_ids[..., :-1]], dim=-1)
else:
shifted_input_ids = input_ids.new_zeros(input_ids.shape)
shifted_input_ids[..., 1:] = input_ids[..., :-1].clone()
shifted_input_ids[..., 0] = decoder_start_token_id
assert pad_token_id is not None, "self.model.config.pad_token_id has to be defined."
# replace possible -100 values in labels by `pad_token_id`
shifted_input_ids.masked_fill_(shifted_input_ids == -100, pad_token_id)
return shifted_input_ids
@add_start_docstrings(
"The bare Manta Model transformer outputting encoder's raw hidden-states without any specific head on top."
)
class MantaEncoderModel(MantaPreTrainedModel):
authorized_missing_keys = [
r"encoder.embed_tokens.weight",
]
def __init__(self, config: MantaConfig):
super().__init__(config)
self.byte_embeddings = nn.Embedding(config.vocab_size, config.byte_embedding_dim)
self.frontier_predictor = MantaFrontierPredictor(
hidden_size=config.byte_embedding_dim,
num_layers=config.frontier_predictor_num_layers,
num_attention_heads=config.frontier_predictor_num_attention_heads,
dropout_rate=config.dropout_rate,
attention_window=config.frontier_predictor_attention_window,
max_length=config.max_length_inputs,
)
self.pooler = MantaCachedConvolutionPooling(
padding_length=config.max_length_encoder_decoder,
output_dim=config.d_model,
kernel_size=config.pooling_kernel_size,
hidden_dim=config.byte_embedding_dim,
depthwise_convolution=config.pooling_depthwise_convolution,
variance_regularization=config.pooling_variance_regularization,
mean_pool=config.pooling_mean_pool,
)
self.t5_encoder = T5Stack(
T5Config(
d_model=config.d_model,
d_kv=config.d_kv,
d_ff=config.d_ff,
num_layers=config.num_layers,
num_heads=config.num_heads,
relative_attention_num_buckets=config.relative_attention_num_buckets,
relative_attention_max_distance=config.relative_attention_max_distance,
dropout_rate=config.dropout_rate,
layer_norm_epsilon=config.layer_norm_epsilon,
initializer_factor=config.initializer_factor,
feed_forward_proj=config.feed_forward_proj,
pad_token_id=config.pad_token_id,
eos_token_id=config.eos_token_id,
is_decoder=False,
use_cache=False,
)
)
# Initialize weights and apply final processing
self.post_init()
def get_input_embeddings(self):
return self.byte_embeddings
def set_input_embeddings(self, new_embeddings):
self.byte_embeddings = new_embeddings
def _prune_heads(self, heads_to_prune):
"""
Prunes heads of the model. heads_to_prune: dict of {layer_num: list of heads to prune in this layer} See base
class PreTrainedModel
"""
for layer, heads in heads_to_prune.items():
self.t5_encoder.block[layer].layer[0].SelfAttention.prune_heads(heads)
def _compute_pooled_representations(
self,
input_ids: Optional[torch.LongTensor] = None,
attention_mask: Optional[torch.FloatTensor] = None,
inputs_embeds: Optional[torch.FloatTensor] = None,
):
if inputs_embeds is None and input_ids is None:
return None
byte_embeddings = inputs_embeds if inputs_embeds is not None else self.byte_embeddings(input_ids)
frontier_predictions = self.frontier_predictor(byte_embeddings, attention_mask)
pooled_representations = self.pooler(frontier_predictions, byte_embeddings)
return pooled_representations, frontier_predictions
@replace_return_docstrings(output_type=MantaBaseModelOutput, config_class=_CONFIG_FOR_DOC)
def forward(
self,
input_ids: Optional[torch.LongTensor] = None,
attention_mask: Optional[torch.FloatTensor] = None,
head_mask: Optional[torch.FloatTensor] = None,
inputs_embeds: Optional[torch.FloatTensor] = None,
output_attentions: Optional[bool] = None,
output_hidden_states: Optional[bool] = None,
return_dict: Optional[bool] = None,
) -> Union[Tuple[torch.FloatTensor], MantaBaseModelOutput]:
r"""
Returns:
Example:
```python
>>> from transformers import ByT5Tokenizer, MantaEncoderModel
>>> tokenizer = ByT5Tokenizer.from_pretrained("google/byt5-small")
>>> model = MantaEncoderModel.from_pretrained("nthngdy/manta-small")
>>> input_ids = tokenizer(
... "Studies have been shown that owning a dog is good for you", return_tensors="pt"
... ).input_ids # Batch size 1
>>> outputs = model(input_ids=input_ids)
>>> last_hidden_states = outputs.last_hidden_state
```"""
output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
output_hidden_states = (
output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
)
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
pooled_representations, frontier_predictions = self._compute_pooled_representations(
input_ids, attention_mask, inputs_embeds
)
encoder_outputs = self.t5_encoder(
inputs_embeds=pooled_representations,
head_mask=head_mask,
output_attentions=output_attentions,
output_hidden_states=output_hidden_states,
return_dict=return_dict,
)
if not return_dict:
return encoder_outputs + (frontier_predictions,)
return MantaBaseModelOutput(frontier_predictions=frontier_predictions, **encoder_outputs)
class MantaModel(MantaPreTrainedModel):
_keys_to_ignore_on_load_missing = [
r"encoder_decoder.encoder.embed_tokens.weight",
r"encoder_decoder.decoder.embed_tokens.weight",
]
_keys_to_ignore_on_load_unexpected = [
r"encoder_decoder.decoder.block.0.layer.1.EncDecAttention.relative_attention_bias.weight",
]
def __init__(self, config: MantaConfig):
super().__init__(config)
self.encoder = MantaEncoderModel(config)
self.decoder_embeddings = nn.Embedding(config.vocab_size, config.d_model)
self.decoder = T5Stack(
T5Config(
vocab_size=config.vocab_size,
d_model=config.d_model,
d_kv=config.d_kv,
d_ff=config.d_ff,
num_layers=config.num_decoder_layers,
num_heads=config.num_heads,
relative_attention_num_buckets=config.relative_attention_num_buckets,
relative_attention_max_distance=config.relative_attention_max_distance,
dropout_rate=config.dropout_rate,
layer_norm_epsilon=config.layer_norm_epsilon,
initializer_factor=config.initializer_factor,
feed_forward_proj=config.feed_forward_proj,
use_cache=config.use_cache,
pad_token_id=config.pad_token_id,
eos_token_id=config.eos_token_id,
is_decoder=True,
is_encoder_decoder=False,
),
self.decoder_embeddings,
)
# Initialize weights and apply final processing
self.post_init()
def get_input_embeddings(self):
return self.encoder.get_input_embeddings()
def set_input_embeddings(self, new_embeddings):
self.encoder.set_input_embeddings(new_embeddings)
def get_encoder(self):
return self.encoder
def get_decoder(self):
return self.decoder
def _prune_heads(self, heads_to_prune):
"""
Prunes heads of the model. heads_to_prune: dict of {layer_num: list of heads to prune in this layer} See base
class PreTrainedModel
"""
for layer, heads in heads_to_prune.items():
self.encoder.layer[layer].attention.prune_heads(heads)
@replace_return_docstrings(output_type=MantaSeq2SeqLMOutput, config_class=_CONFIG_FOR_DOC)
def forward(
self,
input_ids: Optional[torch.LongTensor] = None,
attention_mask: Optional[torch.FloatTensor] = None,
decoder_input_ids: Optional[torch.LongTensor] = None,
decoder_attention_mask: Optional[torch.BoolTensor] = None,
head_mask: Optional[torch.FloatTensor] = None,
decoder_head_mask: Optional[torch.FloatTensor] = None,
cross_attn_head_mask: Optional[torch.Tensor] = None,
encoder_outputs: Optional[Tuple[Tuple[torch.FloatTensor]]] = None,
past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None,
inputs_embeds: Optional[torch.Tensor] = None,
decoder_inputs_embeds: Optional[torch.Tensor] = None,
use_cache: Optional[bool] = None,
output_attentions: Optional[bool] = None,
output_hidden_states: Optional[bool] = None,
return_dict: Optional[bool] = None,
) -> Union[Tuple[torch.FloatTensor], MantaSeq2SeqLMOutput]:
r"""
Returns:
Example:
```python
>>> from transformers import ByT5Tokenizer, MantaModel
>>> tokenizer = ByT5Tokenizer.from_pretrained("google/byt5-small")
>>> model = MantaModel.from_pretrained("nthngdy/manta-small")
>>> input_ids = tokenizer(
... "Studies have been shown that owning a dog is good for you", return_tensors="pt"
... ).input_ids # Batch size 1
>>> decoder_input_ids = tokenizer("Studies show that", return_tensors="pt").input_ids # Batch size 1
>>> # preprocess: Prepend decoder_input_ids with start token which is pad token for MantaModel.
>>> # This is not needed for torch's MantaForConditionalGeneration as it does this internally using labels arg.
>>> decoder_input_ids = model._shift_right(decoder_input_ids)
>>> # forward pass
>>> outputs = model(input_ids=input_ids, decoder_input_ids=decoder_input_ids)
>>> last_hidden_states = outputs.last_hidden_state
```"""
use_cache = use_cache if use_cache is not None else self.config.use_cache
output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
output_hidden_states = (
output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
)
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
if encoder_outputs is None:
encoder_outputs = self.encoder(
input_ids=input_ids,
attention_mask=attention_mask,
inputs_embeds=inputs_embeds,
head_mask=head_mask,
output_attentions=output_attentions,
output_hidden_states=output_hidden_states,
return_dict=return_dict,
)
elif return_dict and not isinstance(encoder_outputs, MantaBaseModelOutput):
encoder_outputs = MantaBaseModelOutput(
last_hidden_state=encoder_outputs[0],
hidden_states=encoder_outputs[1] if len(encoder_outputs) > 1 else None,
attentions=encoder_outputs[2] if len(encoder_outputs) > 2 else None,
frontier_predictions=encoder_outputs[3] if len(encoder_outputs) > 3 else None,
)
hidden_states = encoder_outputs[0]
decoder_outputs = self.decoder(
input_ids=decoder_input_ids,
attention_mask=decoder_attention_mask,
encoder_hidden_states=hidden_states,
encoder_attention_mask=attention_mask,
inputs_embeds=decoder_inputs_embeds,
head_mask=decoder_head_mask,
cross_attn_head_mask=cross_attn_head_mask,
past_key_values=past_key_values,
use_cache=use_cache,
output_attentions=output_attentions,
output_hidden_states=output_hidden_states,
return_dict=return_dict,
)
if not return_dict:
return decoder_outputs + encoder_outputs
return MantaSeq2SeqLMOutput(
last_hidden_state=decoder_outputs.last_hidden_state,
past_key_values=decoder_outputs.past_key_values,
decoder_hidden_states=decoder_outputs.hidden_states,
decoder_attentions=decoder_outputs.attentions,
cross_attentions=decoder_outputs.cross_attentions,
encoder_last_hidden_state=encoder_outputs.last_hidden_state,
encoder_hidden_states=encoder_outputs.hidden_states,
encoder_attentions=encoder_outputs.attentions,
frontier_predictions=encoder_outputs.frontier_predictions,
)
@add_start_docstrings("""Manta Model with a `language modeling` head on top.""")
class MantaForConditionalGeneration(MantaPreTrainedModel):
_keys_to_ignore_on_load_missing = [
r"encoder.embed_tokens.weight",
r"decoder.embed_tokens.weight",
r"lm_head.weight",
]
_keys_to_ignore_on_load_unexpected = [
r"decoder.block.0.layer.1.EncDecAttention.relative_attention_bias.weight",
]
def __init__(self, config: MantaConfig):
super().__init__(config)
self.model_dim = config.d_model
self.encoder = MantaEncoderModel(config)
self.decoder_embeddings = nn.Embedding(config.vocab_size, config.d_model)
self.decoder = T5Stack(
T5Config(
vocab_size=config.vocab_size,
d_model=config.d_model,
d_kv=config.d_kv,
d_ff=config.d_ff,
num_layers=config.num_decoder_layers,
num_heads=config.num_heads,
relative_attention_num_buckets=config.relative_attention_num_buckets,
relative_attention_max_distance=config.relative_attention_max_distance,
dropout_rate=config.dropout_rate,
layer_norm_epsilon=config.layer_norm_epsilon,
initializer_factor=config.initializer_factor,
feed_forward_proj=config.feed_forward_proj,
use_cache=config.use_cache,
pad_token_id=config.pad_token_id,
eos_token_id=config.eos_token_id,
is_decoder=True,
is_encoder_decoder=False,
),
self.decoder_embeddings,
)
self.lm_head = nn.Linear(config.d_model, config.vocab_size, bias=False)
# Initialize weights and apply final processing
self.post_init()
def get_input_embeddings(self):
return self.encoder.get_input_embeddings()
def set_input_embeddings(self, new_embeddings):
self.encoder.set_input_embeddings(new_embeddings)
def set_output_embeddings(self, new_embeddings):
self.lm_head = new_embeddings
def get_output_embeddings(self):
return self.lm_head
def get_encoder(self):
return self.encoder
def get_decoder(self):
return self.decoder
@replace_return_docstrings(output_type=MantaSeq2SeqLMOutput, config_class=_CONFIG_FOR_DOC)
def forward(
self,
input_ids: Optional[torch.LongTensor] = None,
attention_mask: Optional[torch.FloatTensor] = None,
decoder_input_ids: Optional[torch.LongTensor] = None,
decoder_attention_mask: Optional[torch.BoolTensor] = None,
head_mask: Optional[torch.FloatTensor] = None,
decoder_head_mask: Optional[torch.FloatTensor] = None,
cross_attn_head_mask: Optional[torch.Tensor] = None,
encoder_outputs: Optional[Tuple[Tuple[torch.Tensor]]] = None,
past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None,
inputs_embeds: Optional[torch.FloatTensor] = None,
decoder_inputs_embeds: Optional[torch.FloatTensor] = None,
labels: Optional[torch.LongTensor] = None,
use_cache: Optional[bool] = None,
output_attentions: Optional[bool] = None,
output_hidden_states: Optional[bool] = None,
return_dict: Optional[bool] = None,
) -> Union[Tuple[torch.FloatTensor], MantaSeq2SeqLMOutput]:
r"""
labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
Labels for computing the sequence classification/regression loss. Indices should be in `[-100, 0, ...,
config.vocab_size - 1]`. All labels set to `-100` are ignored (masked), the loss is only computed for
labels in `[0, ..., config.vocab_size]`
Returns:
Examples:
```python
>>> from transformers import ByT5Tokenizer, MantaForConditionalGeneration
>>> tokenizer = ByT5Tokenizer.from_pretrained("google/byt5-small")
>>> model = MantaForConditionalGeneration.from_pretrained("nthngdy/manta-small")
>>> # training
>>> input_ids = tokenizer("The <extra_id_0> walks in <extra_id_1> park", return_tensors="pt").input_ids
>>> labels = tokenizer("<extra_id_0> cute dog <extra_id_1> the <extra_id_2>", return_tensors="pt").input_ids
>>> outputs = model(input_ids=input_ids, labels=labels)
>>> loss = outputs.loss
>>> logits = outputs.logits
>>> # inference
>>> input_ids = tokenizer(
... "summarize: studies have shown that owning a dog is good for you", return_tensors="pt"
... ).input_ids # Batch size 1
>>> outputs = model.generate(input_ids)
>>> print(tokenizer.decode(outputs[0], skip_special_tokens=True))
>>> # studies have shown that owning a dog is good for you.
```"""
use_cache = use_cache if use_cache is not None else self.config.use_cache
output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
output_hidden_states = (
output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
)
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
# FutureWarning: head_mask was separated into two input args - head_mask, decoder_head_mask
if head_mask is not None and decoder_head_mask is None:
if self.config.num_layers == self.config.num_decoder_layers:
warnings.warn(__HEAD_MASK_WARNING_MSG, FutureWarning)
decoder_head_mask = head_mask
# Encode if needed (training, first prediction pass)
if encoder_outputs is None:
encoder_outputs = self.encoder(
input_ids=input_ids,
attention_mask=attention_mask,
inputs_embeds=inputs_embeds,
head_mask=head_mask,
output_attentions=output_attentions,
output_hidden_states=output_hidden_states,
return_dict=return_dict,
)
elif return_dict and not isinstance(encoder_outputs, MantaBaseModelOutput):
encoder_outputs = BaseModelOutput(
last_hidden_state=encoder_outputs[0],
hidden_states=encoder_outputs[1] if len(encoder_outputs) > 1 else None,
attentions=encoder_outputs[2] if len(encoder_outputs) > 2 else None,
frontier_predictions=encoder_outputs[3] if len(encoder_outputs) > 3 else None,
)
hidden_states = encoder_outputs[0]
if labels is not None and decoder_input_ids is None and decoder_inputs_embeds is None:
# get decoder inputs from shifting lm labels to the right
decoder_input_ids = self._shift_right(labels)
# Decode
decoder_outputs = self.decoder(
input_ids=decoder_input_ids,
attention_mask=decoder_attention_mask,
inputs_embeds=decoder_inputs_embeds,
past_key_values=past_key_values,
encoder_hidden_states=hidden_states,
head_mask=decoder_head_mask,
cross_attn_head_mask=cross_attn_head_mask,
use_cache=use_cache,
output_attentions=output_attentions,
output_hidden_states=output_hidden_states,
return_dict=return_dict,
)
sequence_output = decoder_outputs[0]
if self.config.tie_word_embeddings:
# Rescale output before projecting on vocab
# See https://github.com/tensorflow/mesh/blob/fa19d69eafc9a482aff0b59ddd96b025c0cb207d/mesh_tensorflow/transformer/transformer.py#L586
sequence_output = sequence_output * (self.model_dim**-0.5)
lm_logits = self.lm_head(sequence_output)
loss = None
if labels is not None:
loss_fct = CrossEntropyLoss(ignore_index=-100)
loss = loss_fct(lm_logits.view(-1, lm_logits.size(-1)), labels.view(-1))
# TODO(thom): Add z_loss https://github.com/tensorflow/mesh/blob/fa19d69eafc9a482aff0b59ddd96b025c0cb207d/mesh_tensorflow/layers.py#L666
if not return_dict:
output = (lm_logits,) + decoder_outputs[1:] + encoder_outputs
return ((loss,) + output) if loss is not None else output
return MantaSeq2SeqLMOutput(
loss=loss,
logits=lm_logits,
past_key_values=decoder_outputs.past_key_values,
decoder_hidden_states=decoder_outputs.hidden_states,
decoder_attentions=decoder_outputs.attentions,
cross_attentions=decoder_outputs.cross_attentions,
encoder_last_hidden_state=encoder_outputs.last_hidden_state,
encoder_hidden_states=encoder_outputs.hidden_states,
encoder_attentions=encoder_outputs.attentions,
frontier_predictions=encoder_outputs.frontier_predictions,
)
def prepare_inputs_for_generation(
self,
input_ids,
past=None,
attention_mask=None,
head_mask=None,
decoder_head_mask=None,
cross_attn_head_mask=None,
use_cache=None,
encoder_outputs=None,
**kwargs
):
# cut decoder_input_ids if past is used
if past is not None:
input_ids = input_ids[:, -1:]
return {
"decoder_input_ids": input_ids,
"past_key_values": past,
"encoder_outputs": encoder_outputs,
"attention_mask": attention_mask,
"head_mask": head_mask,
"decoder_head_mask": decoder_head_mask,
"cross_attn_head_mask": cross_attn_head_mask,
"use_cache": use_cache,
}
def prepare_decoder_input_ids_from_labels(self, labels: torch.Tensor):
return self._shift_right(labels)
def _reorder_cache(self, past, beam_idx):
# if decoder past is not included in output
# speedy decoding is disabled and no need to reorder
if past is None:
logger.warning("You might want to consider setting `use_cache=True` to speed up decoding")
return past
reordered_decoder_past = ()
for layer_past_states in past:
# get the correct batch idx from layer past batch dim
# batch dim of `past` is at 2nd position
reordered_layer_past_states = ()
for layer_past_state in layer_past_states:
# need to set correct `past` for each of the four key / value states
reordered_layer_past_states = reordered_layer_past_states + (
layer_past_state.index_select(0, beam_idx.to(layer_past_state.device)),
)
assert reordered_layer_past_states[0].shape == layer_past_states[0].shape
assert len(reordered_layer_past_states) == len(layer_past_states)
reordered_decoder_past = reordered_decoder_past + (reordered_layer_past_states,)
return reordered_decoder_past |