Text Generation
Transformers
PyTorch
code
gpt2
custom_code
Eval Results
text-generation-inference
Inference Endpoints
lvwerra HF staff commited on
Commit
5fb20b0
1 Parent(s): ab5ccc2

Upload 2 files

Browse files
Files changed (2) hide show
  1. configuration_gpt2_mq.py +201 -0
  2. modeling_gpt2_mq.py +346 -0
configuration_gpt2_mq.py ADDED
@@ -0,0 +1,201 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2018 The OpenAI Team Authors and HuggingFace Inc. team.
3
+ # Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ """ Custom GPT-2 configuration"""
17
+ from collections import OrderedDict
18
+ from typing import Any, List, Mapping, Optional
19
+ from enum import Enum
20
+
21
+ from transformers import PreTrainedTokenizer, TensorType, is_torch_available
22
+
23
+ from transformers.configuration_utils import PretrainedConfig
24
+ from transformers.onnx import OnnxConfigWithPast, PatchingSpec
25
+ from transformers.utils import logging
26
+
27
+
28
+ logger = logging.get_logger(__name__)
29
+
30
+ GPT2_PRETRAINED_CONFIG_ARCHIVE_MAP = {
31
+ "gpt2": "https://huggingface.co/gpt2/resolve/main/config.json",
32
+ "gpt2-medium": "https://huggingface.co/gpt2-medium/resolve/main/config.json",
33
+ "gpt2-large": "https://huggingface.co/gpt2-large/resolve/main/config.json",
34
+ "gpt2-xl": "https://huggingface.co/gpt2-xl/resolve/main/config.json",
35
+ "distilgpt2": "https://huggingface.co/distilgpt2/resolve/main/config.json",
36
+ }
37
+
38
+ MULTI_HEAD = "multihead"
39
+ MULTI_QUERY = "multiquery"
40
+
41
+
42
+ class GPT2CustomConfig(PretrainedConfig):
43
+ """
44
+ This is the configuration class to store the configuration of a [`GPT2Model`] or a [`TFGPT2Model`]. It is used to
45
+ instantiate a GPT-2 model according to the specified arguments, defining the model architecture. Instantiating a
46
+ configuration with the defaults will yield a similar configuration to that of the GPT-2
47
+ [gpt2](https://huggingface.co/gpt2) architecture.
48
+
49
+ Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
50
+ documentation from [`PretrainedConfig`] for more information.
51
+
52
+
53
+ Args:
54
+ vocab_size (`int`, *optional*, defaults to 50257):
55
+ Vocabulary size of the GPT-2 model. Defines the number of different tokens that can be represented by the
56
+ `inputs_ids` passed when calling [`GPT2Model`] or [`TFGPT2Model`].
57
+ n_positions (`int`, *optional*, defaults to 1024):
58
+ The maximum sequence length that this model might ever be used with. Typically set this to something large
59
+ just in case (e.g., 512 or 1024 or 2048).
60
+ n_embd (`int`, *optional*, defaults to 768):
61
+ Dimensionality of the embeddings and hidden states.
62
+ n_layer (`int`, *optional*, defaults to 12):
63
+ Number of hidden layers in the Transformer encoder.
64
+ n_head (`int`, *optional*, defaults to 12):
65
+ Number of attention heads for each attention layer in the Transformer encoder.
66
+ n_inner (`int`, *optional*, defaults to None):
67
+ Dimensionality of the inner feed-forward layers. `None` will set it to 4 times n_embd
68
+ activation_function (`str`, *optional*, defaults to `"gelu"`):
69
+ Activation function, to be selected in the list `["relu", "silu", "gelu", "tanh", "gelu_new"]`.
70
+ resid_pdrop (`float`, *optional*, defaults to 0.1):
71
+ The dropout probability for all fully connected layers in the embeddings, encoder, and pooler.
72
+ embd_pdrop (`int`, *optional*, defaults to 0.1):
73
+ The dropout ratio for the embeddings.
74
+ attn_pdrop (`float`, *optional*, defaults to 0.1):
75
+ The dropout ratio for the attention.
76
+ layer_norm_epsilon (`float`, *optional*, defaults to 1e-5):
77
+ The epsilon to use in the layer normalization layers.
78
+ initializer_range (`float`, *optional*, defaults to 0.02):
79
+ The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
80
+ summary_type (`string`, *optional*, defaults to `"cls_index"`):
81
+ Argument used when doing sequence summary, used in the models [`GPT2DoubleHeadsModel`] and
82
+ [`TFGPT2DoubleHeadsModel`].
83
+
84
+ Has to be one of the following options:
85
+
86
+ - `"last"`: Take the last token hidden state (like XLNet).
87
+ - `"first"`: Take the first token hidden state (like BERT).
88
+ - `"mean"`: Take the mean of all tokens hidden states.
89
+ - `"cls_index"`: Supply a Tensor of classification token position (like GPT/GPT-2).
90
+ - `"attn"`: Not implemented now, use multi-head attention.
91
+ summary_use_proj (`bool`, *optional*, defaults to `True`):
92
+ Argument used when doing sequence summary, used in the models [`GPT2DoubleHeadsModel`] and
93
+ [`TFGPT2DoubleHeadsModel`].
94
+
95
+ Whether or not to add a projection after the vector extraction.
96
+ summary_activation (`str`, *optional*):
97
+ Argument used when doing sequence summary. Used in for the multiple choice head in
98
+ [`GPT2DoubleHeadsModel`].
99
+
100
+ Pass `"tanh"` for a tanh activation to the output, any other value will result in no activation.
101
+ summary_proj_to_labels (`bool`, *optional*, defaults to `True`):
102
+ Argument used when doing sequence summary, used in the models [`GPT2DoubleHeadsModel`] and
103
+ [`TFGPT2DoubleHeadsModel`].
104
+
105
+ Whether the projection outputs should have `config.num_labels` or `config.hidden_size` classes.
106
+ summary_first_dropout (`float`, *optional*, defaults to 0.1):
107
+ Argument used when doing sequence summary, used in the models [`GPT2DoubleHeadsModel`] and
108
+ [`TFGPT2DoubleHeadsModel`].
109
+
110
+ The dropout ratio to be used after the projection and activation.
111
+ scale_attn_weights (`bool`, *optional*, defaults to `True`):
112
+ Scale attention weights by dividing by sqrt(head_dim)..
113
+ use_cache (`bool`, *optional*, defaults to `True`):
114
+ Whether or not the model should return the last key/values attentions (not used by all models).
115
+ scale_attn_by_inverse_layer_idx (`bool`, *optional*, defaults to `False`):
116
+ Whether to additionally scale attention weights by `1 / layer_idx + 1`.
117
+ reorder_and_upcast_attn (`bool`, *optional*, defaults to `False`):
118
+ Whether to scale keys (K) prior to computing attention (dot-product) and upcast attention
119
+ dot-product/softmax to float() when training with mixed precision.
120
+
121
+ Example:
122
+
123
+ ```python
124
+ >>> from transformers import GPT2Config, GPT2Model
125
+
126
+ >>> # Initializing a GPT2 configuration
127
+ >>> configuration = GPT2Config()
128
+
129
+ >>> # Initializing a model (with random weights) from the configuration
130
+ >>> model = GPT2Model(configuration)
131
+
132
+ >>> # Accessing the model configuration
133
+ >>> configuration = model.config
134
+ ```"""
135
+
136
+ model_type = "gpt2"
137
+ keys_to_ignore_at_inference = ["past_key_values"]
138
+ attribute_map = {
139
+ "hidden_size": "n_embd",
140
+ "max_position_embeddings": "n_positions",
141
+ "num_attention_heads": "n_head",
142
+ "num_hidden_layers": "n_layer",
143
+ }
144
+
145
+ def __init__(
146
+ self,
147
+ vocab_size=50257,
148
+ n_positions=1024,
149
+ n_embd=768,
150
+ n_layer=12,
151
+ n_head=12,
152
+ n_inner=None,
153
+ activation_function="gelu_new",
154
+ resid_pdrop=0.1,
155
+ embd_pdrop=0.1,
156
+ attn_pdrop=0.1,
157
+ layer_norm_epsilon=1e-5,
158
+ initializer_range=0.02,
159
+ summary_type="cls_index",
160
+ summary_use_proj=True,
161
+ summary_activation=None,
162
+ summary_proj_to_labels=True,
163
+ summary_first_dropout=0.1,
164
+ scale_attn_weights=True,
165
+ use_cache=True,
166
+ bos_token_id=50256,
167
+ eos_token_id=50256,
168
+ scale_attn_by_inverse_layer_idx=False,
169
+ reorder_and_upcast_attn=False,
170
+ attention_head_type=MULTI_HEAD,
171
+ **kwargs,
172
+ ):
173
+ self.vocab_size = vocab_size
174
+ self.n_positions = n_positions
175
+ self.n_embd = n_embd
176
+ self.n_layer = n_layer
177
+ self.n_head = n_head
178
+ self.n_inner = n_inner
179
+ self.activation_function = activation_function
180
+ self.resid_pdrop = resid_pdrop
181
+ self.embd_pdrop = embd_pdrop
182
+ self.attn_pdrop = attn_pdrop
183
+ self.layer_norm_epsilon = layer_norm_epsilon
184
+ self.initializer_range = initializer_range
185
+ self.summary_type = summary_type
186
+ self.summary_use_proj = summary_use_proj
187
+ self.summary_activation = summary_activation
188
+ self.summary_first_dropout = summary_first_dropout
189
+ self.summary_proj_to_labels = summary_proj_to_labels
190
+ self.scale_attn_weights = scale_attn_weights
191
+ self.use_cache = use_cache
192
+ self.scale_attn_by_inverse_layer_idx = scale_attn_by_inverse_layer_idx
193
+ self.reorder_and_upcast_attn = reorder_and_upcast_attn
194
+ self.attention_head_type = attention_head_type
195
+ # assert attention_head_type in [AttentionType.MULTI_HEAD, AttentionType.MULTI_QUERY]
196
+ assert attention_head_type in [MULTI_HEAD, MULTI_QUERY]
197
+
198
+ self.bos_token_id = bos_token_id
199
+ self.eos_token_id = eos_token_id
200
+
201
+ super().__init__(bos_token_id=bos_token_id, eos_token_id=eos_token_id, **kwargs)
modeling_gpt2_mq.py ADDED
@@ -0,0 +1,346 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """PyTorch OpenAI GPT-2 model modified with MultiQuery attention"""
2
+
3
+
4
+ import math
5
+ import os
6
+ from dataclasses import dataclass
7
+ from typing import Optional, Tuple, Union
8
+
9
+ import torch
10
+ import torch.utils.checkpoint
11
+ from torch import nn
12
+ from torch.cuda.amp import autocast
13
+ from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss
14
+
15
+ from transformers.activations import ACT2FN
16
+ from transformers.modeling_outputs import (
17
+ BaseModelOutputWithPastAndCrossAttentions,
18
+ CausalLMOutputWithCrossAttentions,
19
+ SequenceClassifierOutputWithPast,
20
+ TokenClassifierOutput,
21
+ )
22
+ from transformers.modeling_utils import PreTrainedModel, SequenceSummary
23
+ from transformers.pytorch_utils import Conv1D, find_pruneable_heads_and_indices, prune_conv1d_layer
24
+
25
+ from transformers.utils import (
26
+ ModelOutput,
27
+ add_code_sample_docstrings,
28
+ add_start_docstrings,
29
+ add_start_docstrings_to_model_forward,
30
+ logging,
31
+ replace_return_docstrings,
32
+ )
33
+ from transformers.utils.model_parallel_utils import assert_device_map, get_device_map
34
+ from transformers.models.gpt2.modeling_gpt2 import GPT2Model, GPT2Block, GPT2PreTrainedModel, GPT2LMHeadModel
35
+ from tools.hf_transformers.configuration_gpt2_mq import GPT2CustomConfig, MULTI_QUERY, MULTI_HEAD
36
+
37
+
38
+
39
+ class GPT2MQAttention(nn.Module):
40
+ def __init__(self, config, is_cross_attention=False, layer_idx=None):
41
+ super().__init__()
42
+ assert config.attention_head_type == MULTI_QUERY
43
+
44
+ max_positions = config.max_position_embeddings
45
+ self.register_buffer(
46
+ "bias",
47
+ torch.tril(torch.ones((max_positions, max_positions), dtype=torch.uint8)).view(
48
+ 1, 1, max_positions, max_positions
49
+ ),
50
+ )
51
+ self.register_buffer("masked_bias", torch.tensor(-1e4))
52
+
53
+ self.embed_dim = config.hidden_size
54
+ self.num_heads = config.num_attention_heads
55
+ self.head_dim = self.embed_dim // self.num_heads
56
+ self.split_size = self.embed_dim
57
+ if self.head_dim * self.num_heads != self.embed_dim:
58
+ raise ValueError(
59
+ f"`embed_dim` must be divisible by num_heads (got `embed_dim`: {self.embed_dim} and `num_heads`:"
60
+ f" {self.num_heads})."
61
+ )
62
+
63
+ self.scale_attn_weights = config.scale_attn_weights
64
+ if is_cross_attention:
65
+ raise NotImplementedError("Cross-attention not implemented for MQA")
66
+ self.is_cross_attention = is_cross_attention
67
+
68
+ # Layer-wise attention scaling, reordering, and upcasting
69
+ self.scale_attn_by_inverse_layer_idx = config.scale_attn_by_inverse_layer_idx
70
+ self.layer_idx = layer_idx
71
+ self.reorder_and_upcast_attn = config.reorder_and_upcast_attn
72
+
73
+ if self.is_cross_attention:
74
+ self.c_attn = Conv1D(2 * self.embed_dim, self.embed_dim)
75
+ self.q_attn = Conv1D(self.embed_dim, self.embed_dim)
76
+ else:
77
+ # self.c_attn = Conv1D(3 * self.embed_dim, self.embed_dim)
78
+ self.q_attn = Conv1D(self.embed_dim, self.embed_dim)
79
+ # Keys and values are shared across heads
80
+ self.kv_attn = Conv1D(2 * self.head_dim, self.embed_dim)
81
+ self.c_proj = Conv1D(self.embed_dim, self.embed_dim)
82
+
83
+ self.attn_dropout = nn.Dropout(config.attn_pdrop)
84
+ self.resid_dropout = nn.Dropout(config.resid_pdrop)
85
+
86
+ self.pruned_heads = set()
87
+
88
+ def prune_heads(self, heads):
89
+ if len(heads) == 0:
90
+ return
91
+ heads, index = find_pruneable_heads_and_indices(heads, self.num_heads, self.head_dim, self.pruned_heads)
92
+ index_attn = torch.cat([index, index + self.split_size, index + (2 * self.split_size)])
93
+
94
+ # Prune conv1d layers
95
+ self.c_attn = prune_conv1d_layer(self.c_attn, index_attn, dim=1)
96
+ self.c_proj = prune_conv1d_layer(self.c_proj, index, dim=0)
97
+
98
+ # Update hyper params
99
+ self.split_size = (self.split_size // self.num_heads) * (self.num_heads - len(heads))
100
+ self.num_heads = self.num_heads - len(heads)
101
+ self.pruned_heads = self.pruned_heads.union(heads)
102
+
103
+ def _attn(self, query, key, value, attention_mask=None, head_mask=None):
104
+ # query: (b, num_heads * sq, head_dim)
105
+ # key: (b, head_dim, sk)
106
+ # value: (b, sk, head_dim)
107
+ batch_size = query.size(0)
108
+ query_length = query.size(1) // self.num_heads
109
+ key_length = key.size(2)
110
+ # (b, num_heads * sq, head_dim) x (b, head_dim, sk) -> (b, num_heads * sq, sk)
111
+ attn_weights = torch.bmm(query, key)
112
+ # -> (b, num_heads, sq, sk)
113
+ attn_weights = attn_weights.view(batch_size, self.num_heads, query_length, key_length)
114
+
115
+ if self.scale_attn_weights:
116
+ attn_weights = attn_weights / torch.tensor(
117
+ value.size(-1) ** 0.5, dtype=attn_weights.dtype, device=attn_weights.device
118
+ )
119
+
120
+ # Layer-wise attention scaling
121
+ if self.scale_attn_by_inverse_layer_idx:
122
+ attn_weights = attn_weights / float(self.layer_idx + 1)
123
+
124
+ if not self.is_cross_attention:
125
+ # if only "normal" attention layer implements causal mask
126
+ causal_mask = self.bias[:, :, key_length - query_length : key_length, :key_length].to(torch.bool)
127
+ mask_value = torch.finfo(attn_weights.dtype).min
128
+ # Need to be a tensor, otherwise we get error: `RuntimeError: expected scalar type float but found double`.
129
+ # Need to be on the same device, otherwise `RuntimeError: ..., x and y to be on the same device`
130
+ mask_value = torch.tensor(mask_value, dtype=attn_weights.dtype).to(attn_weights.device)
131
+ attn_weights = torch.where(causal_mask, attn_weights, mask_value)
132
+
133
+ if attention_mask is not None:
134
+ # Apply the attention mask
135
+ attn_weights = attn_weights + attention_mask
136
+
137
+ attn_weights = nn.functional.softmax(attn_weights, dim=-1)
138
+
139
+ # Downcast (if necessary) back to V's dtype (if in mixed-precision) -- No-Op otherwise
140
+ attn_weights = attn_weights.type(value.dtype)
141
+ attn_weights = self.attn_dropout(attn_weights)
142
+
143
+ # Mask heads if we want to
144
+ if head_mask is not None:
145
+ attn_weights = attn_weights * head_mask
146
+
147
+ # (b, num_heads, sq, sk) -> (b, num_heads * sq, sk)
148
+ _attn_weights = attn_weights.view(batch_size, self.num_heads * query_length, key_length)
149
+ # (b, num_heads * sq, sk) x (b, sk, head_dim) -> (b, num_heads * sq, head_dim)
150
+ attn_output = torch.bmm(_attn_weights, value)
151
+ attn_output = attn_output.view(batch_size, self.num_heads, query_length, self.head_dim)
152
+
153
+ return attn_output, attn_weights
154
+
155
+ def _upcast_and_reordered_attn(self, query, key, value, attention_mask=None, head_mask=None):
156
+ # Use `torch.baddbmm` (a bit more efficient w/ alpha param for scaling -- from Megatron-LM)
157
+ bsz, num_heads, q_seq_len, dk = query.size()
158
+ _, _, k_seq_len, _ = key.size()
159
+
160
+ # Preallocate attn_weights for `baddbmm`
161
+ attn_weights = torch.empty(bsz * num_heads, q_seq_len, k_seq_len, dtype=torch.float32, device=query.device)
162
+
163
+ # Compute Scale Factor
164
+ scale_factor = 1.0
165
+ if self.scale_attn_weights:
166
+ scale_factor /= float(value.size(-1)) ** 0.5
167
+
168
+ if self.scale_attn_by_inverse_layer_idx:
169
+ scale_factor /= float(self.layer_idx + 1)
170
+
171
+ # Upcast (turn off autocast) and reorder (Scale K by 1 / root(dk))
172
+ with autocast(enabled=False):
173
+ q, k = query.reshape(-1, q_seq_len, dk), key.transpose(-1, -2).reshape(-1, dk, k_seq_len)
174
+ attn_weights = torch.baddbmm(attn_weights, q.float(), k.float(), beta=0, alpha=scale_factor)
175
+ attn_weights = attn_weights.reshape(bsz, num_heads, q_seq_len, k_seq_len)
176
+
177
+ if not self.is_cross_attention:
178
+ # if only "normal" attention layer implements causal mask
179
+ query_length, key_length = query.size(-2), key.size(-2)
180
+ causal_mask = self.bias[:, :, key_length - query_length : key_length, :key_length].bool()
181
+ mask_value = torch.finfo(attn_weights.dtype).min
182
+ # Need to be a tensor, otherwise we get error: `RuntimeError: expected scalar type float but found double`.
183
+ # Need to be on the same device, otherwise `RuntimeError: ..., x and y to be on the same device`
184
+ mask_value = torch.tensor(mask_value, dtype=attn_weights.dtype).to(attn_weights.device)
185
+ attn_weights = torch.where(causal_mask, attn_weights, mask_value)
186
+
187
+ if attention_mask is not None:
188
+ # Apply the attention mask
189
+ attn_weights = attn_weights + attention_mask
190
+
191
+ attn_weights = nn.functional.softmax(attn_weights, dim=-1)
192
+
193
+ # Downcast (if necessary) back to V's dtype (if in mixed-precision) -- No-Op if otherwise
194
+ if attn_weights.dtype != torch.float32:
195
+ raise RuntimeError("Error with upcasting, attn_weights does not have dtype torch.float32")
196
+ attn_weights = attn_weights.type(value.dtype)
197
+ attn_weights = self.attn_dropout(attn_weights)
198
+
199
+ # Mask heads if we want to
200
+ if head_mask is not None:
201
+ attn_weights = attn_weights * head_mask
202
+
203
+ attn_output = torch.matmul(attn_weights, value)
204
+
205
+ return attn_output, attn_weights
206
+
207
+ def _split_heads(self, tensor, num_heads, attn_head_size):
208
+ """
209
+ Splits hidden_size dim into attn_head_size and num_heads
210
+ """
211
+ new_shape = tensor.size()[:-1] + (num_heads, attn_head_size)
212
+ tensor = tensor.view(new_shape)
213
+ return tensor.permute(0, 2, 1, 3) # (batch, head, seq_length, head_features)
214
+
215
+ def _merge_heads(self, tensor, num_heads, attn_head_size):
216
+ """
217
+ Merges attn_head_size dim and num_attn_heads dim into hidden_size
218
+ """
219
+ tensor = tensor.permute(0, 2, 1, 3).contiguous()
220
+ new_shape = tensor.size()[:-2] + (num_heads * attn_head_size,)
221
+ return tensor.view(new_shape)
222
+
223
+ def forward(
224
+ self,
225
+ hidden_states: Optional[Tuple[torch.FloatTensor]],
226
+ layer_past: Optional[Tuple[torch.Tensor]] = None,
227
+ attention_mask: Optional[torch.FloatTensor] = None,
228
+ head_mask: Optional[torch.FloatTensor] = None,
229
+ encoder_hidden_states: Optional[torch.Tensor] = None,
230
+ encoder_attention_mask: Optional[torch.FloatTensor] = None,
231
+ use_cache: Optional[bool] = False,
232
+ output_attentions: Optional[bool] = False,
233
+ ) -> Tuple[Union[torch.Tensor, Tuple[torch.Tensor]], ...]:
234
+ if encoder_hidden_states is not None:
235
+ raise NotImplementedError("Cross-attention not implemented for MQA")
236
+ if not hasattr(self, "q_attn"):
237
+ raise ValueError(
238
+ "If class is used as cross attention, the weights `q_attn` have to be defined. "
239
+ "Please make sure to instantiate class with `GPT2Attention(..., is_cross_attention=True)`."
240
+ )
241
+
242
+ query = self.q_attn(hidden_states)
243
+ key, value = self.c_attn(encoder_hidden_states).split(self.split_size, dim=2)
244
+ attention_mask = encoder_attention_mask
245
+ else:
246
+ query = self.q_attn(hidden_states)
247
+ key, value = self.kv_attn(hidden_states).split(self.head_dim, dim=2)
248
+
249
+
250
+ batch_size, seq_length = query.shape[:2]
251
+ # (query_length, batch, num_heads, head_dim)
252
+ # (batch, num_heads * query_length, head_dim)\
253
+
254
+ # (batch, query_length, hidden_size) -> (batch, num_heads, query_length, head_dim)
255
+ query = query.view(batch_size, seq_length, self.num_heads, self.head_dim).permute([0, 2, 1, 3])
256
+ # -> (batch, num_heads * query_length, head_dim)
257
+ query = query.reshape(batch_size, self.num_heads * seq_length, self.head_dim)
258
+
259
+ # (batch, query_length, hidden_size) -> (batch, query_length * num_heads, head_dim)
260
+ # query = query.view(
261
+ # batch_size, seq_length, self.num_heads, self.head_dim,
262
+ # ).reshape(
263
+ # batch_size, seq_length * self.num_heads, self.head_dim
264
+ # )
265
+ key = key.permute(0, 2, 1) # (batch_size, head_dim, seq_length)
266
+ # value (batch_size, seq_length, head_dim)
267
+
268
+ if layer_past is not None:
269
+ past_key, past_value = layer_past
270
+ # Concatenate on sequence dimension
271
+ key = torch.cat((past_key, key), dim=-1)
272
+ value = torch.cat((past_value, value), dim=-2)
273
+
274
+ if use_cache is True:
275
+ present = (key, value)
276
+ else:
277
+ present = None
278
+
279
+ if self.reorder_and_upcast_attn:
280
+ raise NotImplementedError("Reorder and upcast attention not implemented for MQA")
281
+ attn_output, attn_weights = self._upcast_and_reordered_attn(query, key, value, attention_mask, head_mask)
282
+ else:
283
+ attn_output, attn_weights = self._attn(query, key, value, attention_mask, head_mask)
284
+
285
+ attn_output = self._merge_heads(attn_output, self.num_heads, self.head_dim)
286
+ attn_output = self.c_proj(attn_output)
287
+ attn_output = self.resid_dropout(attn_output)
288
+
289
+ outputs = (attn_output, present)
290
+ if output_attentions:
291
+ outputs += (attn_weights,)
292
+
293
+ return outputs # a, present, (attentions)
294
+
295
+
296
+ # inherit from gpt_modeling.py, and override `attn` module
297
+ class GPT2CustomBlock(GPT2Block):
298
+
299
+ def __init__(self, config: GPT2CustomConfig, layer_idx=None):
300
+ super().__init__(config, layer_idx)
301
+ # Override attention module if using multiquery
302
+ if config.attention_head_type == MULTI_QUERY:
303
+ self.attn = GPT2MQAttention(config, layer_idx=layer_idx)
304
+ if config.add_cross_attention:
305
+ raise NotImplementedError("Cross-attention not implemented for MQA")
306
+
307
+
308
+ # inherit from gpt_modeling.py and override `__init__` method
309
+ class GPT2CustomModel(GPT2Model):
310
+ config_class = GPT2CustomConfig
311
+
312
+ def __init__(self, config):
313
+ GPT2PreTrainedModel.__init__(self, config)
314
+
315
+ self.embed_dim = config.hidden_size
316
+
317
+ self.wte = nn.Embedding(config.vocab_size, self.embed_dim)
318
+ self.wpe = nn.Embedding(config.max_position_embeddings, self.embed_dim)
319
+
320
+ self.drop = nn.Dropout(config.embd_pdrop)
321
+ self.h = nn.ModuleList([GPT2CustomBlock(config, layer_idx=i) for i in range(config.num_hidden_layers)])
322
+ self.ln_f = nn.LayerNorm(self.embed_dim, eps=config.layer_norm_epsilon)
323
+
324
+ # Model parallel
325
+ self.model_parallel = False
326
+ self.device_map = None
327
+ self.gradient_checkpointing = False
328
+
329
+ # Initialize weights and apply final processing
330
+ self.post_init()
331
+
332
+
333
+ class GPT2LMHeadCustomModel(GPT2LMHeadModel):
334
+ config_class = GPT2CustomConfig
335
+
336
+ def __init__(self, config):
337
+ GPT2PreTrainedModel.__init__(self, config)
338
+ self.transformer = GPT2CustomModel(config)
339
+ self.lm_head = nn.Linear(config.n_embd, config.vocab_size, bias=False)
340
+
341
+ # Model parallel
342
+ self.model_parallel = False
343
+ self.device_map = None
344
+
345
+ # Initialize weights and apply final processing
346
+ self.post_init()