davda54 commited on
Commit
a425998
1 Parent(s): 5daf3f3

Upload 8 files

Browse files
config.json ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "NorT5ForConditionalGeneration"
4
+ ],
5
+ "attention_probs_dropout_prob": 0.0,
6
+ "bos_token_id": 5,
7
+ "cls_token_id": 1,
8
+ "eos_token_id": 6,
9
+ "hidden_dropout_prob": 0.0,
10
+ "hidden_size": 192,
11
+ "initializer_range": 0.02,
12
+ "intermediate_size": 512,
13
+ "layer_norm_eps": 1e-07,
14
+ "max_position_embeddings": 512,
15
+ "num_attention_heads": 3,
16
+ "num_hidden_layers": 12,
17
+ "output_all_encoded_layers": true,
18
+ "pad_token_id": 3,
19
+ "position_bucket_size": 32,
20
+ "sep_token_id": 2,
21
+ "torch_dtype": "float32",
22
+ "transformers_version": "4.24.0",
23
+ "vocab_size": 50000,
24
+ "max_length": 512,
25
+ "max_new_tokens": 256,
26
+ "is_encoder_decoder": true
27
+ }
configuration_nort5.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers.configuration_utils import PretrainedConfig
2
+
3
+
4
+ class NorT5Config(PretrainedConfig):
5
+ """Configuration class to store the configuration of a `NorT5`.
6
+ """
7
+ def __init__(
8
+ self,
9
+ vocab_size=50000,
10
+ attention_probs_dropout_prob=0.1,
11
+ hidden_dropout_prob=0.1,
12
+ hidden_size=768,
13
+ intermediate_size=2048,
14
+ max_position_embeddings=512,
15
+ position_bucket_size=32,
16
+ num_attention_heads=12,
17
+ num_hidden_layers=12,
18
+ layer_norm_eps=1.0e-7,
19
+ output_all_encoded_layers=True,
20
+ pad_token_id=3,
21
+ cls_token_id=1,
22
+ sep_token_id=2,
23
+ bos_token_id=5,
24
+ eos_token_id=6,
25
+ **kwargs,
26
+ ):
27
+ super().__init__(**kwargs)
28
+
29
+ self.vocab_size = vocab_size
30
+ self.hidden_size = hidden_size
31
+ self.num_hidden_layers = num_hidden_layers
32
+ self.num_attention_heads = num_attention_heads
33
+ self.intermediate_size = intermediate_size
34
+ self.hidden_dropout_prob = hidden_dropout_prob
35
+ self.attention_probs_dropout_prob = attention_probs_dropout_prob
36
+ self.max_position_embeddings = max_position_embeddings
37
+ self.output_all_encoded_layers = output_all_encoded_layers
38
+ self.position_bucket_size = position_bucket_size
39
+ self.layer_norm_eps = layer_norm_eps
40
+ self.pad_token_id = pad_token_id
41
+ self.cls_token_id = cls_token_id
42
+ self.sep_token_id = sep_token_id
43
+ self.bos_token_id = bos_token_id
44
+ self.eos_token_id = eos_token_id
generation_config.json ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_from_model_config": true,
3
+ "decoder_start_token_id": 5,
4
+ "eos_token_id": 6,
5
+ "pad_token_id": 3,
6
+ "transformers_version": "4.27.0.dev0"
7
+ }
8
+
modeling_nort5.py ADDED
@@ -0,0 +1,581 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import absolute_import, division, print_function, unicode_literals
2
+
3
+ import math
4
+ from typing import List, Optional, Tuple, Union
5
+
6
+ import torch
7
+ import torch.nn as nn
8
+ import torch.nn.functional as F
9
+ from torch import _softmax_backward_data as _softmax_backward_data
10
+ from torch.utils import checkpoint
11
+
12
+ from configuration_nort5 import NorT5Config
13
+ from transformers.modeling_utils import PreTrainedModel
14
+ from transformers.activations import gelu_new
15
+ from transformers.modeling_outputs import (
16
+ Seq2SeqModelOutput, Seq2SeqLMOutput, BaseModelOutput
17
+ )
18
+
19
+
20
+ class Encoder(nn.Module):
21
+ def __init__(self, config, activation_checkpointing=False):
22
+ super().__init__()
23
+ self.main_input_name = "input_ids"
24
+
25
+ self.relative_embedding = RelativeEmbedding(config)
26
+ self.layers = nn.ModuleList([EncoderLayer(config) for _ in range(config.num_hidden_layers)])
27
+
28
+ for i, layer in enumerate(self.layers):
29
+ layer.mlp.mlp[1].weight.data *= math.sqrt(1.0 / (2.0 * (1 + i)))
30
+ layer.mlp.mlp[-2].weight.data *= math.sqrt(1.0 / (2.0 * (1 + i)))
31
+
32
+ self.activation_checkpointing = activation_checkpointing
33
+
34
+ def forward(self, hidden_states, attention_mask):
35
+ relative_embedding = self.relative_embedding()
36
+ hidden_states, attention_probs = [hidden_states], []
37
+
38
+ for layer in self.layers:
39
+ if self.activation_checkpointing:
40
+ hidden_state, attention_p = checkpoint.checkpoint(layer, hidden_states[-1], attention_mask, relative_embedding)
41
+ else:
42
+ hidden_state, attention_p = layer(hidden_states[-1], attention_mask, relative_embedding)
43
+
44
+ hidden_states.append(hidden_state)
45
+ attention_probs.append(attention_p)
46
+
47
+ return hidden_states, attention_probs
48
+
49
+
50
+ class Decoder(nn.Module):
51
+ def __init__(self, config, activation_checkpointing=False):
52
+ super().__init__()
53
+ self.self_relative_embedding = RelativeEmbedding(config)
54
+ self.cross_relative_embedding = RelativeEmbedding(config)
55
+ self.layers = nn.ModuleList([DecoderLayer(config) for _ in range(config.num_hidden_layers)])
56
+
57
+ for i, layer in enumerate(self.layers):
58
+ layer.mlp.mlp[1].weight.data *= math.sqrt(1.0 / (2.0 * (1 + i)))
59
+ layer.mlp.mlp[-2].weight.data *= math.sqrt(1.0 / (2.0 * (1 + i)))
60
+
61
+ def forward(self, x, encoder_output, encoder_padding_mask):
62
+ self_relative_embedding = self.self_relative_embedding()
63
+ cross_relative_embedding = self.cross_relative_embedding()
64
+
65
+ autoreg_mask = torch.triu(
66
+ torch.full((x.size(0), x.size(0)), True, device=x.device),
67
+ diagonal=1
68
+ )
69
+
70
+ for layer in self.layers:
71
+ x = layer(x, autoreg_mask, encoder_output, encoder_padding_mask, self_relative_embedding, cross_relative_embedding)
72
+ return x
73
+
74
+
75
+ class MaskClassifier(nn.Module):
76
+ def __init__(self, config):
77
+ super().__init__()
78
+ self.nonlinearity = nn.Sequential(
79
+ nn.LayerNorm(config.hidden_size, config.layer_norm_eps, elementwise_affine=False),
80
+ nn.Dropout(config.hidden_dropout_prob),
81
+ nn.Linear(config.hidden_size, config.vocab_size)
82
+ )
83
+ self.initialize(config.hidden_size)
84
+
85
+ def initialize(self, hidden_size):
86
+ std = math.sqrt(2.0 / (5.0 * hidden_size))
87
+ nn.init.trunc_normal_(self.nonlinearity[-1].weight, mean=0.0, std=std, a=-2*std, b=2*std)
88
+ self.nonlinearity[-1].bias.data.zero_()
89
+
90
+ def forward(self, x):
91
+ x = self.nonlinearity(x)
92
+ return x
93
+
94
+
95
+ class EncoderLayer(nn.Module):
96
+ def __init__(self, config):
97
+ super().__init__()
98
+ self.attention = Attention(config)
99
+ self.mlp = FeedForward(config)
100
+
101
+ def forward(self, x, padding_mask, relative_embedding):
102
+ attention_output, attention_probs = self.attention(x, x, padding_mask, relative_embedding)
103
+ x = x + attention_output
104
+ x = x + self.mlp(x)
105
+ return x, attention_probs
106
+
107
+
108
+ class DecoderLayer(nn.Module):
109
+ def __init__(self, config):
110
+ super().__init__()
111
+ self.self_attention = Attention(config)
112
+ self.cross_attention = Attention(config)
113
+ self.mlp = FeedForward(config)
114
+
115
+ def forward(self, x, autoreg_mask, encoder_output, encoder_padding_mask, self_relative_embedding, cross_relative_embedding):
116
+ x = x + self.self_attention(x, x, autoreg_mask, self_relative_embedding)[0]
117
+ x = x + self.cross_attention(x, encoder_output, encoder_padding_mask, cross_relative_embedding)[0]
118
+ x = x + self.mlp(x)
119
+ return x
120
+
121
+
122
+ class GeGLU(nn.Module):
123
+ def forward(self, x):
124
+ x, gate = x.chunk(2, dim=-1)
125
+ x = x * gelu_new(gate)
126
+ return x
127
+
128
+
129
+ class FeedForward(nn.Module):
130
+ def __init__(self, config):
131
+ super().__init__()
132
+ self.mlp = nn.Sequential(
133
+ nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps, elementwise_affine=False),
134
+ nn.Linear(config.hidden_size, 2*config.intermediate_size, bias=False),
135
+ GeGLU(),
136
+ nn.LayerNorm(config.intermediate_size, eps=config.layer_norm_eps, elementwise_affine=False),
137
+ nn.Linear(config.intermediate_size, config.hidden_size, bias=False),
138
+ nn.Dropout(config.hidden_dropout_prob)
139
+ )
140
+ self.initialize(config.hidden_size)
141
+
142
+ def initialize(self, hidden_size):
143
+ std = math.sqrt(2.0 / (5.0 * hidden_size))
144
+ nn.init.trunc_normal_(self.mlp[1].weight, mean=0.0, std=std, a=-2*std, b=2*std)
145
+ nn.init.trunc_normal_(self.mlp[-2].weight, mean=0.0, std=std, a=-2*std, b=2*std)
146
+
147
+ def forward(self, x):
148
+ return self.mlp(x)
149
+
150
+
151
+ class MaskedSoftmax(torch.autograd.Function):
152
+ @staticmethod
153
+ def forward(self, x, mask, dim):
154
+ self.dim = dim
155
+ x.masked_fill_(mask, float('-inf'))
156
+ x = torch.softmax(x, self.dim)
157
+ x.masked_fill_(mask, 0.0)
158
+ self.save_for_backward(x)
159
+ return x
160
+
161
+ @staticmethod
162
+ def backward(self, grad_output):
163
+ output, = self.saved_tensors
164
+ inputGrad = _softmax_backward_data(grad_output, output, self.dim, output.dtype)
165
+ return inputGrad, None, None
166
+
167
+
168
+ class Attention(nn.Module):
169
+ def __init__(self, config):
170
+ super().__init__()
171
+
172
+ self.config = config
173
+
174
+ if config.hidden_size % config.num_attention_heads != 0:
175
+ raise ValueError(f"The hidden size {config.hidden_size} is not a multiple of the number of attention heads {config.num_attention_heads}")
176
+
177
+ self.hidden_size = config.hidden_size
178
+ self.num_heads = config.num_attention_heads
179
+ self.head_size = config.hidden_size // config.num_attention_heads
180
+
181
+ self.in_proj_q = nn.Linear(config.hidden_size, config.hidden_size, bias=True)
182
+ self.in_proj_k = nn.Linear(config.hidden_size, config.hidden_size, bias=True)
183
+ self.in_proj_v = nn.Linear(config.hidden_size, config.hidden_size, bias=True)
184
+ self.out_proj = nn.Linear(config.hidden_size, config.hidden_size, bias=True)
185
+
186
+ self.pre_layer_norm = nn.LayerNorm(config.hidden_size, config.layer_norm_eps, elementwise_affine=False)
187
+ self.post_layer_norm = nn.LayerNorm(config.hidden_size, config.layer_norm_eps, elementwise_affine=True)
188
+
189
+ position_indices = torch.arange(config.max_position_embeddings, dtype=torch.long).unsqueeze(1) \
190
+ - torch.arange(config.max_position_embeddings, dtype=torch.long).unsqueeze(0)
191
+ position_indices = self.make_log_bucket_position(position_indices, config.position_bucket_size, config.max_position_embeddings)
192
+ position_indices = config.position_bucket_size - 1 + position_indices
193
+ self.register_buffer("position_indices", position_indices, persistent=True)
194
+
195
+ self.dropout = nn.Dropout(config.attention_probs_dropout_prob)
196
+ self.scale = 1.0 / math.sqrt(3 * self.head_size)
197
+ self.initialize()
198
+
199
+ def make_log_bucket_position(self, relative_pos, bucket_size, max_position):
200
+ sign = torch.sign(relative_pos)
201
+ mid = bucket_size // 2
202
+ abs_pos = torch.where((relative_pos < mid) & (relative_pos > -mid), mid - 1, torch.abs(relative_pos).clamp(max=max_position - 1))
203
+ log_pos = torch.ceil(torch.log(abs_pos / mid) / math.log((max_position-1) / mid) * (mid - 1)).int() + mid
204
+ bucket_pos = torch.where(abs_pos <= mid, relative_pos, log_pos * sign).long()
205
+ return bucket_pos
206
+
207
+ def initialize(self):
208
+ std = math.sqrt(2.0 / (5.0 * self.hidden_size))
209
+ nn.init.trunc_normal_(self.in_proj_q.weight, mean=0.0, std=std, a=-2*std, b=2*std)
210
+ nn.init.trunc_normal_(self.in_proj_k.weight, mean=0.0, std=std, a=-2*std, b=2*std)
211
+ nn.init.trunc_normal_(self.in_proj_v.weight, mean=0.0, std=std, a=-2*std, b=2*std)
212
+ nn.init.trunc_normal_(self.out_proj.weight, mean=0.0, std=std, a=-2*std, b=2*std)
213
+ self.in_proj_q.bias.data.zero_()
214
+ self.in_proj_k.bias.data.zero_()
215
+ self.in_proj_v.bias.data.zero_()
216
+ self.out_proj.bias.data.zero_()
217
+
218
+ def compute_attention_scores(self, q, kv, relative_embedding):
219
+ key_len, batch_size, _ = kv.size()
220
+ query_len, _, _ = q.size()
221
+
222
+ if self.position_indices.size(0) < query_len:
223
+ position_indices = torch.arange(query_len, dtype=torch.long).unsqueeze(1) \
224
+ - torch.arange(query_len, dtype=torch.long).unsqueeze(0)
225
+ position_indices = self.make_log_bucket_position(position_indices, self.config.position_bucket_size, 512)
226
+ position_indices = self.config.position_bucket_size - 1 + position_indices
227
+ self.register_buffer("position_indices", position_indices.to(q.device), persistent=True)
228
+
229
+ kv = self.pre_layer_norm(kv)
230
+ q = self.pre_layer_norm(q)
231
+
232
+ query = self.in_proj_q(q) # shape: [T, B, D]
233
+ key = self.in_proj_k(kv) # shape: [T, B, D]
234
+ value = self.in_proj_v(kv) # shape: [T, B, D]
235
+
236
+ query_pos = self.in_proj_q(self.dropout(relative_embedding)) # shape: [2T-1, 2D]
237
+ query_pos = F.embedding(self.position_indices[:query_len, :key_len], query_pos) # shape: [T, T, 2D]
238
+ query_pos = query_pos.view(query_len, key_len, self.num_heads, self.head_size)
239
+
240
+ key_pos = self.in_proj_k(self.dropout(relative_embedding)) # shape: [2T-1, 2D]
241
+ key_pos = F.embedding(self.position_indices[:query_len, :key_len], key_pos) # shape: [T, T, 2D]
242
+ key_pos = key_pos.view(query_len, key_len, self.num_heads, self.head_size)
243
+
244
+ query = query.reshape(query_len, batch_size * self.num_heads, self.head_size).transpose(0, 1)
245
+ key = key.reshape(key_len, batch_size * self.num_heads, self.head_size).transpose(0, 1)
246
+ value = value.view(key_len, batch_size * self.num_heads, self.head_size).transpose(0, 1)
247
+
248
+ attention_scores = torch.bmm(query, key.transpose(1, 2) * self.scale)
249
+
250
+ query = query.view(batch_size, self.num_heads, query_len, self.head_size)
251
+ key = key.view(batch_size, self.num_heads, key_len, self.head_size)
252
+ attention_scores = attention_scores.view(batch_size, self.num_heads, query_len, key_len)
253
+ attention_scores.add_(torch.einsum("bhqd,qkhd->bhqk", query, key_pos * self.scale))
254
+ attention_scores.add_(torch.einsum("bhkd,qkhd->bhqk", key * self.scale, query_pos))
255
+
256
+ return attention_scores, value
257
+
258
+ def compute_output(self, attention_probs, value):
259
+ attention_probs = self.dropout(attention_probs)
260
+ context = torch.bmm(attention_probs.flatten(0, 1), value) # shape: [B*H, Q, D]
261
+ context = context.transpose(0, 1).reshape(context.size(1), -1, self.hidden_size) # shape: [Q, B, H*D]
262
+ context = self.out_proj(context)
263
+ context = self.post_layer_norm(context)
264
+ context = self.dropout(context)
265
+ return context
266
+
267
+ def forward(self, q, kv, attention_mask, relative_embedding):
268
+ attention_scores, value = self.compute_attention_scores(q, kv, relative_embedding)
269
+ attention_probs = MaskedSoftmax.apply(attention_scores, attention_mask, -1)
270
+ return self.compute_output(attention_probs, value), attention_probs.detach()
271
+
272
+
273
+ class WordEmbedding(nn.Module):
274
+ def __init__(self, config):
275
+ super().__init__()
276
+ self.hidden_size = config.hidden_size
277
+
278
+ self.word_embedding = nn.Embedding(config.vocab_size, config.hidden_size)
279
+ self.word_layer_norm = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps, elementwise_affine=False)
280
+ self.dropout = nn.Dropout(config.hidden_dropout_prob)
281
+
282
+ self.initialize()
283
+
284
+ def initialize(self):
285
+ std = math.sqrt(2.0 / (5.0 * self.hidden_size))
286
+ nn.init.trunc_normal_(self.word_embedding.weight, mean=0.0, std=std, a=-2*std, b=2*std)
287
+
288
+ def forward(self, input_ids):
289
+ return self.dropout(self.word_layer_norm(self.word_embedding(input_ids)))
290
+
291
+
292
+ class RelativeEmbedding(nn.Module):
293
+ def __init__(self, config):
294
+ super().__init__()
295
+ self.relative_embedding = nn.Parameter(torch.empty(2 * config.position_bucket_size - 1, config.hidden_size))
296
+ self.relative_layer_norm = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps)
297
+
298
+ self.initialize(config.hidden_size)
299
+
300
+ def initialize(self, hidden_size):
301
+ std = math.sqrt(2.0 / (5.0 * hidden_size))
302
+ nn.init.trunc_normal_(self.relative_embedding, mean=0.0, std=std, a=-2*std, b=2*std)
303
+
304
+ def forward(self):
305
+ return self.relative_layer_norm(self.relative_embedding)
306
+
307
+
308
+ #
309
+ # HuggingFace wrappers
310
+ #
311
+
312
+ class NorT5PreTrainedModel(PreTrainedModel):
313
+ config_class = NorT5Config
314
+ base_model_prefix = "norT5"
315
+ supports_gradient_checkpointing = True
316
+
317
+ def _set_gradient_checkpointing(self, module, value=False):
318
+ if isinstance(module, Encoder):
319
+ module.activation_checkpointing = value
320
+
321
+ def _init_weights(self, module):
322
+ pass # everything is already initialized
323
+
324
+
325
+ class NorT5Model(NorT5PreTrainedModel):
326
+ def __init__(self, config, add_lm_layer=False, add_decoder=True):
327
+ super().__init__(config)
328
+ self.config = config
329
+
330
+ self.cls_token_id = config.cls_token_id
331
+ self.sep_token_id = config.sep_token_id
332
+ self.bos_token_id = config.bos_token_id
333
+ self.eos_token_id = config.eos_token_id
334
+ self.pad_token_id = config.pad_token_id
335
+
336
+ self.embedding = WordEmbedding(config)
337
+ self.encoder = Encoder(config, activation_checkpointing=False)
338
+ self.decoder = Decoder(config, activation_checkpointing=False) if add_decoder else None
339
+ self.classifier = MaskClassifier(config) if add_lm_layer else None
340
+
341
+ def get_input_embeddings(self):
342
+ return self.embedding.word_embedding
343
+
344
+ def set_input_embeddings(self, value):
345
+ self.embedding.word_embedding = value
346
+
347
+ def get_encoder(self):
348
+ return self.get_encoder_output
349
+
350
+ def get_decoder(self):
351
+ return self.decoder
352
+
353
+ def set_decoder_special_tokens(self, target_id):
354
+ target_id.masked_fill_(target_id == self.cls_token_id, self.bos_token_id)
355
+ target_id.masked_fill_(target_id == self.sep_token_id, self.eos_token_id)
356
+ return target_id
357
+
358
+ def _shift_right(self, input_ids):
359
+ shifted_input_ids = input_ids.new_zeros(input_ids.shape)
360
+ shifted_input_ids[..., 1:] = input_ids[..., :-1].clone()
361
+ shifted_input_ids[..., 0] = self.bos_token_id
362
+
363
+ return shifted_input_ids
364
+
365
+ def get_encoder_output(
366
+ self,
367
+ input_ids: Optional[torch.Tensor] = None,
368
+ attention_mask: Optional[torch.Tensor] = None,
369
+ output_hidden_states: Optional[bool] = None,
370
+ output_attentions: Optional[bool] = None,
371
+ return_dict = False
372
+ ):
373
+ if input_ids is not None:
374
+ input_shape = input_ids.size()
375
+ else:
376
+ raise ValueError("You have to specify input_ids")
377
+
378
+ batch_size, seq_length = input_shape
379
+ device = input_ids.device
380
+
381
+ if attention_mask is None:
382
+ attention_mask = torch.zeros(batch_size, seq_length, dtype=torch.bool, device=device)
383
+ else:
384
+ attention_mask = ~attention_mask.bool()
385
+ attention_mask = attention_mask.unsqueeze(1).unsqueeze(2)
386
+
387
+ static_embeddings = self.embedding(input_ids.t())
388
+ contextualized_embeddings, attention_probs = self.encoder(static_embeddings, attention_mask)
389
+ contextualized_embeddings = [e.transpose(0, 1) for e in contextualized_embeddings]
390
+ last_layer = contextualized_embeddings[-1]
391
+ contextualized_embeddings = [contextualized_embeddings[0]] + [
392
+ contextualized_embeddings[i] - contextualized_embeddings[i - 1]
393
+ for i in range(1, len(contextualized_embeddings))
394
+ ]
395
+
396
+ if not return_dict:
397
+ return last_layer, contextualized_embeddings, attention_probs
398
+
399
+ return BaseModelOutput(
400
+ last_hidden_state=last_layer,
401
+ hidden_states=contextualized_embeddings,
402
+ attentions=attention_probs
403
+ )
404
+
405
+ def get_decoder_output(
406
+ self, target_ids, encoder_output, attention_mask
407
+ ):
408
+ batch_size, seq_length = target_ids.shape
409
+ device = target_ids.device
410
+
411
+ if attention_mask is None:
412
+ attention_mask = torch.zeros(batch_size, seq_length, dtype=torch.bool, device=device)
413
+ else:
414
+ attention_mask = ~attention_mask.bool()
415
+ attention_mask = attention_mask.unsqueeze(1).unsqueeze(2)
416
+
417
+ return self.decoder(
418
+ self.embedding(target_ids.t()),
419
+ encoder_output.transpose(0, 1),
420
+ attention_mask
421
+ ).transpose(0, 1)
422
+
423
+ def forward(
424
+ self,
425
+ input_ids: Optional[torch.LongTensor] = None,
426
+ attention_mask: Optional[torch.FloatTensor] = None,
427
+ decoder_input_ids: Optional[torch.LongTensor] = None,
428
+ decoder_attention_mask: Optional[torch.BoolTensor] = None,
429
+ return_dict: Optional[bool] = None,
430
+ ):
431
+
432
+ return_dict = return_dict if return_dict is not None else self.config.use_return_dict
433
+
434
+ decoder_input_ids = self.set_decoder_special_tokens(decoder_input_ids)
435
+
436
+ encoder_outputs, encoder_contextualized_embeddings, encoder_attention_probs = self.get_encoder_output(input_ids, attention_mask)
437
+ decoder_outputs = self.get_decoder_output(decoder_input_ids, encoder_outputs, attention_mask)
438
+
439
+ if not return_dict:
440
+ return (decoder_outputs, encoder_outputs)
441
+
442
+ return Seq2SeqModelOutput(
443
+ last_hidden_state=decoder_outputs,
444
+ past_key_values=None,
445
+ decoder_hidden_states=None,
446
+ decoder_attentions=None,
447
+ cross_attentions=None,
448
+ encoder_last_hidden_state=encoder_outputs,
449
+ encoder_hidden_states=encoder_contextualized_embeddings,
450
+ encoder_attentions=encoder_attention_probs,
451
+ )
452
+
453
+
454
+ class NorT5ForConditionalGeneration(NorT5Model):
455
+
456
+ def __init__(self, config):
457
+ super().__init__(config, add_lm_layer=True)
458
+
459
+ def forward(
460
+ self,
461
+ input_ids: Optional[torch.LongTensor] = None,
462
+ attention_mask: Optional[torch.FloatTensor] = None,
463
+ decoder_input_ids: Optional[torch.LongTensor] = None,
464
+ decoder_attention_mask: Optional[torch.BoolTensor] = None,
465
+ head_mask: Optional[torch.FloatTensor] = None,
466
+ decoder_head_mask: Optional[torch.FloatTensor] = None,
467
+ cross_attn_head_mask: Optional[torch.Tensor] = None,
468
+ encoder_outputs: Optional[Tuple[Tuple[torch.Tensor]]] = None,
469
+ past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None,
470
+ inputs_embeds: Optional[torch.FloatTensor] = None,
471
+ decoder_inputs_embeds: Optional[torch.FloatTensor] = None,
472
+ labels: Optional[torch.LongTensor] = None,
473
+ use_cache: Optional[bool] = None,
474
+ output_attentions: Optional[bool] = None,
475
+ output_hidden_states: Optional[bool] = None,
476
+ return_dict: Optional[bool] = None,
477
+ ):
478
+
479
+ use_cache = False
480
+ return_dict = return_dict if return_dict is not None else self.config.use_return_dict
481
+
482
+ if encoder_outputs is None:
483
+ encoder_outputs = self.get_encoder_output(input_ids, attention_mask, return_dict=True)
484
+
485
+ if labels is not None:
486
+ labels = self.set_decoder_special_tokens(labels)
487
+
488
+ if labels is not None and decoder_input_ids is None and decoder_inputs_embeds is None:
489
+ decoder_input_ids = self._shift_right(labels)
490
+ elif decoder_input_ids is not None:
491
+ decoder_input_ids = self.set_decoder_special_tokens(decoder_input_ids)
492
+
493
+ decoder_outputs = self.get_decoder_output(decoder_input_ids, encoder_outputs.last_hidden_state, attention_mask)
494
+ lm_logits = self.classifier(decoder_outputs)
495
+
496
+ loss = None
497
+ if labels is not None:
498
+ loss_fct = nn.CrossEntropyLoss(ignore_index=self.pad_token_id)
499
+ loss = loss_fct(lm_logits.flatten(0, 1), labels.flatten())
500
+
501
+ if not return_dict:
502
+ output = (lm_logits,) + encoder_outputs
503
+ return ((loss,) + output) if loss is not None else output
504
+
505
+ return Seq2SeqLMOutput(
506
+ loss=loss,
507
+ logits=lm_logits,
508
+ decoder_hidden_states=decoder_outputs,
509
+ decoder_attentions=None,
510
+ cross_attentions=None,
511
+ encoder_last_hidden_state=encoder_outputs.last_hidden_state,
512
+ encoder_hidden_states=encoder_outputs.hidden_states,
513
+ encoder_attentions=encoder_outputs.attentions,
514
+ )
515
+
516
+ def prepare_inputs_for_generation(
517
+ self,
518
+ input_ids,
519
+ past_key_values=None,
520
+ attention_mask=None,
521
+ head_mask=None,
522
+ decoder_head_mask=None,
523
+ cross_attn_head_mask=None,
524
+ use_cache=None,
525
+ encoder_outputs=None,
526
+ **kwargs,
527
+ ):
528
+ return {
529
+ "decoder_input_ids": input_ids,
530
+ "past_key_values": past_key_values,
531
+ "encoder_outputs": encoder_outputs,
532
+ "attention_mask": attention_mask,
533
+ "head_mask": head_mask,
534
+ "decoder_head_mask": decoder_head_mask,
535
+ "cross_attn_head_mask": cross_attn_head_mask,
536
+ "use_cache": use_cache,
537
+ }
538
+
539
+ def prepare_decoder_input_ids_from_labels(self, labels: torch.Tensor):
540
+ return self._shift_right(labels)
541
+
542
+ def _reorder_cache(self, past_key_values, beam_idx):
543
+ # if decoder past is not included in output
544
+ # speedy decoding is disabled and no need to reorder
545
+ if past_key_values is None:
546
+ print("You might want to consider setting `use_cache=True` to speed up decoding")
547
+ return past_key_values
548
+
549
+ reordered_decoder_past = ()
550
+ for layer_past_states in past_key_values:
551
+ # get the correct batch idx from layer past batch dim
552
+ # batch dim of `past` is at 2nd position
553
+ reordered_layer_past_states = ()
554
+ for layer_past_state in layer_past_states:
555
+ # need to set correct `past` for each of the four key / value states
556
+ reordered_layer_past_states = reordered_layer_past_states + (
557
+ layer_past_state.index_select(0, beam_idx.to(layer_past_state.device)),
558
+ )
559
+
560
+ assert reordered_layer_past_states[0].shape == layer_past_states[0].shape
561
+ assert len(reordered_layer_past_states) == len(layer_past_states)
562
+
563
+ reordered_decoder_past = reordered_decoder_past + (reordered_layer_past_states,)
564
+ return reordered_decoder_past
565
+
566
+
567
+ class NorT5Encoder(NorT5Model):
568
+ def __init__(self, config):
569
+ super().__init__(config, add_lm_layer=False, add_decoder=True)
570
+
571
+ def forward(
572
+ self,
573
+ input_ids: Optional[torch.Tensor] = None,
574
+ attention_mask: Optional[torch.Tensor] = None,
575
+ output_hidden_states: Optional[bool] = None,
576
+ output_attentions: Optional[bool] = None,
577
+ return_dict: Optional[bool] = None,
578
+ ):
579
+ return_dict = return_dict if return_dict is not None else self.config.use_return_dict
580
+
581
+ return self.get_encoder_output(input_ids, attention_mask, return_dict=return_dict)
pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3adfa636d0d180c94537abeb1c0fc9eda7a3dacf7793e081f67a461b194fed57
3
+ size 202506925
special_tokens_map.json ADDED
@@ -0,0 +1 @@
 
 
1
+ {"bos_token": "[BOS]", "eos_token": "[EOS]", "unk_token": "[UNK]", "sep_token": "[SEP]", "pad_token": "[PAD]", "cls_token": "[CLS]", "mask_token": "[MASK]"}
tokenizer.json ADDED
The diff for this file is too large to render. See raw diff
 
tokenizer_config.json ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ {
2
+ "tokenizer_class": "PreTrainedTokenizerFast"
3
+ }