davda54 commited on
Commit
f2e3a27
1 Parent(s): 15c97e0

Upload 7 files

Browse files
config.json ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "NorbertForMaskedLM"
4
+ ],
5
+ "attention_probs_dropout_prob": 0.1,
6
+ "hidden_dropout_prob": 0.1,
7
+ "hidden_size": 768,
8
+ "intermediate_size": 2048,
9
+ "layer_norm_eps": 1e-07,
10
+ "max_position_embeddings": 512,
11
+ "num_attention_heads": 12,
12
+ "num_hidden_layers": 12,
13
+ "position_bucket_size": 32,
14
+ "torch_dtype": "float32",
15
+ "transformers_version": "4.23.1",
16
+ "vocab_size": 50000
17
+ }
configuration_norbert.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers.configuration_utils import PretrainedConfig
2
+
3
+
4
+ class NorbertConfig(PretrainedConfig):
5
+ """Configuration class to store the configuration of a `NorbertModel`.
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
+ **kwargs,
21
+ ):
22
+ super().__init__(**kwargs)
23
+
24
+ self.vocab_size = vocab_size
25
+ self.hidden_size = hidden_size
26
+ self.num_hidden_layers = num_hidden_layers
27
+ self.num_attention_heads = num_attention_heads
28
+ self.intermediate_size = intermediate_size
29
+ self.hidden_dropout_prob = hidden_dropout_prob
30
+ self.attention_probs_dropout_prob = attention_probs_dropout_prob
31
+ self.max_position_embeddings = max_position_embeddings
32
+ self.output_all_encoded_layers = output_all_encoded_layers
33
+ self.position_bucket_size = position_bucket_size
34
+ self.layer_norm_eps = layer_norm_eps
modeling_norbert.py ADDED
@@ -0,0 +1,629 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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_norbert import NorbertConfig
13
+ from transformers.modeling_utils import PreTrainedModel
14
+ from transformers.activations import gelu_new
15
+ from transformers.modeling_outputs import (
16
+ MaskedLMOutput,
17
+ MultipleChoiceModelOutput,
18
+ QuestionAnsweringModelOutput,
19
+ SequenceClassifierOutput,
20
+ TokenClassifierOutput,
21
+ BaseModelOutput
22
+ )
23
+
24
+
25
+ class Encoder(nn.Module):
26
+ def __init__(self, config, activation_checkpointing=False):
27
+ super().__init__()
28
+ self.layers = nn.ModuleList([EncoderLayer(config) for _ in range(config.num_hidden_layers)])
29
+
30
+ for i, layer in enumerate(self.layers):
31
+ layer.mlp.mlp[1].weight.data *= math.sqrt(1.0 / (2.0 * (1 + i)))
32
+ layer.mlp.mlp[-2].weight.data *= math.sqrt(1.0 / (2.0 * (1 + i)))
33
+
34
+ self.activation_checkpointing = activation_checkpointing
35
+
36
+ def forward(self, hidden_states, attention_mask, relative_embedding):
37
+ hidden_states, attention_probs = [hidden_states], []
38
+
39
+ for layer in self.layers:
40
+ if self.activation_checkpointing:
41
+ hidden_state, attention_p = checkpoint.checkpoint(layer, hidden_states[-1], attention_mask, relative_embedding)
42
+ else:
43
+ hidden_state, attention_p = layer(hidden_states[-1], attention_mask, relative_embedding)
44
+
45
+ hidden_states.append(hidden_state)
46
+ attention_probs.append(attention_p)
47
+
48
+ return hidden_states, attention_probs
49
+
50
+
51
+ class MaskClassifier(nn.Module):
52
+ def __init__(self, config, subword_embedding):
53
+ super().__init__()
54
+ self.nonlinearity = nn.Sequential(
55
+ nn.LayerNorm(config.hidden_size, config.layer_norm_eps, elementwise_affine=False),
56
+ nn.Linear(config.hidden_size, config.hidden_size),
57
+ nn.GELU(),
58
+ nn.LayerNorm(config.hidden_size, config.layer_norm_eps, elementwise_affine=False),
59
+ nn.Dropout(config.hidden_dropout_prob),
60
+ nn.Linear(subword_embedding.size(1), subword_embedding.size(0))
61
+ )
62
+ self.initialize(config.hidden_size, subword_embedding)
63
+
64
+ def initialize(self, hidden_size, embedding):
65
+ std = math.sqrt(2.0 / (5.0 * hidden_size))
66
+ nn.init.trunc_normal_(self.nonlinearity[1].weight, mean=0.0, std=std, a=-2*std, b=2*std)
67
+ self.nonlinearity[-1].weight = embedding
68
+ self.nonlinearity[1].bias.data.zero_()
69
+ self.nonlinearity[-1].bias.data.zero_()
70
+
71
+ def forward(self, x, masked_lm_labels=None):
72
+ if masked_lm_labels is not None:
73
+ x = torch.index_select(x.flatten(0, 1), 0, torch.nonzero(masked_lm_labels.flatten() != -100).squeeze())
74
+ x = self.nonlinearity(x)
75
+ return x
76
+
77
+
78
+ class EncoderLayer(nn.Module):
79
+ def __init__(self, config):
80
+ super().__init__()
81
+ self.attention = Attention(config)
82
+ self.mlp = FeedForward(config)
83
+
84
+ def forward(self, x, padding_mask, relative_embedding):
85
+ attention_output, attention_probs = self.attention(x, padding_mask, relative_embedding)
86
+ x = x + attention_output
87
+ x = x + self.mlp(x)
88
+ return x, attention_probs
89
+
90
+
91
+ class GeGLU(nn.Module):
92
+ def forward(self, x):
93
+ x, gate = x.chunk(2, dim=-1)
94
+ x = x * gelu_new(gate)
95
+ return x
96
+
97
+
98
+ class FeedForward(nn.Module):
99
+ def __init__(self, config):
100
+ super().__init__()
101
+ self.mlp = nn.Sequential(
102
+ nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps, elementwise_affine=False),
103
+ nn.Linear(config.hidden_size, 2*config.intermediate_size, bias=False),
104
+ GeGLU(),
105
+ nn.LayerNorm(config.intermediate_size, eps=config.layer_norm_eps, elementwise_affine=False),
106
+ nn.Linear(config.intermediate_size, config.hidden_size, bias=False),
107
+ nn.Dropout(config.hidden_dropout_prob)
108
+ )
109
+ self.initialize(config.hidden_size)
110
+
111
+ def initialize(self, hidden_size):
112
+ std = math.sqrt(2.0 / (5.0 * hidden_size))
113
+ nn.init.trunc_normal_(self.mlp[1].weight, mean=0.0, std=std, a=-2*std, b=2*std)
114
+ nn.init.trunc_normal_(self.mlp[-2].weight, mean=0.0, std=std, a=-2*std, b=2*std)
115
+
116
+ def forward(self, x):
117
+ return self.mlp(x)
118
+
119
+
120
+ class MaskedSoftmax(torch.autograd.Function):
121
+ @staticmethod
122
+ def forward(self, x, mask, dim):
123
+ self.dim = dim
124
+ x.masked_fill_(mask, float('-inf'))
125
+ x = torch.softmax(x, self.dim)
126
+ x.masked_fill_(mask, 0.0)
127
+ self.save_for_backward(x)
128
+ return x
129
+
130
+ @staticmethod
131
+ def backward(self, grad_output):
132
+ output, = self.saved_tensors
133
+ inputGrad = _softmax_backward_data(grad_output, output, self.dim, output.dtype)
134
+ return inputGrad, None, None
135
+
136
+
137
+ class Attention(nn.Module):
138
+ def __init__(self, config):
139
+ super().__init__()
140
+
141
+ self.config = config
142
+
143
+ if config.hidden_size % config.num_attention_heads != 0:
144
+ raise ValueError(f"The hidden size {config.hidden_size} is not a multiple of the number of attention heads {config.num_attention_heads}")
145
+
146
+ self.hidden_size = config.hidden_size
147
+ self.num_heads = config.num_attention_heads
148
+ self.head_size = config.hidden_size // config.num_attention_heads
149
+
150
+ self.in_proj_qk = nn.Linear(config.hidden_size, 2*config.hidden_size, bias=True)
151
+ self.in_proj_v = nn.Linear(config.hidden_size, config.hidden_size, bias=True)
152
+ self.out_proj = nn.Linear(config.hidden_size, config.hidden_size, bias=True)
153
+
154
+ self.pre_layer_norm = nn.LayerNorm(config.hidden_size, config.layer_norm_eps, elementwise_affine=False)
155
+ self.post_layer_norm = nn.LayerNorm(config.hidden_size, config.layer_norm_eps, elementwise_affine=True)
156
+
157
+ position_indices = torch.arange(config.max_position_embeddings, dtype=torch.long).unsqueeze(1) \
158
+ - torch.arange(config.max_position_embeddings, dtype=torch.long).unsqueeze(0)
159
+ position_indices = self.make_log_bucket_position(position_indices, config.position_bucket_size, config.max_position_embeddings)
160
+ position_indices = config.position_bucket_size - 1 + position_indices
161
+ self.register_buffer("position_indices", position_indices, persistent=True)
162
+
163
+ self.dropout = nn.Dropout(config.attention_probs_dropout_prob)
164
+ self.scale = 1.0 / math.sqrt(3 * self.head_size)
165
+ self.initialize()
166
+
167
+ def make_log_bucket_position(self, relative_pos, bucket_size, max_position):
168
+ sign = torch.sign(relative_pos)
169
+ mid = bucket_size // 2
170
+ abs_pos = torch.where((relative_pos < mid) & (relative_pos > -mid), mid - 1, torch.abs(relative_pos).clamp(max=max_position - 1))
171
+ log_pos = torch.ceil(torch.log(abs_pos / mid) / math.log((max_position-1) / mid) * (mid - 1)).int() + mid
172
+ bucket_pos = torch.where(abs_pos <= mid, relative_pos, log_pos * sign).long()
173
+ return bucket_pos
174
+
175
+ def initialize(self):
176
+ std = math.sqrt(2.0 / (5.0 * self.hidden_size))
177
+ nn.init.trunc_normal_(self.in_proj_qk.weight, mean=0.0, std=std, a=-2*std, b=2*std)
178
+ nn.init.trunc_normal_(self.in_proj_v.weight, mean=0.0, std=std, a=-2*std, b=2*std)
179
+ nn.init.trunc_normal_(self.out_proj.weight, mean=0.0, std=std, a=-2*std, b=2*std)
180
+ self.in_proj_qk.bias.data.zero_()
181
+ self.in_proj_v.bias.data.zero_()
182
+ self.out_proj.bias.data.zero_()
183
+
184
+ def compute_attention_scores(self, hidden_states, relative_embedding):
185
+ key_len, batch_size, _ = hidden_states.size()
186
+ query_len = key_len
187
+
188
+ if self.position_indices.size(0) < query_len:
189
+ position_indices = torch.arange(query_len, dtype=torch.long).unsqueeze(1) \
190
+ - torch.arange(query_len, dtype=torch.long).unsqueeze(0)
191
+ position_indices = self.make_log_bucket_position(position_indices, self.config.position_bucket_size, 512)
192
+ position_indices = self.config.position_bucket_size - 1 + position_indices
193
+ self.register_buffer("position_indices", position_indices.to(hidden_states.device), persistent=True)
194
+
195
+ hidden_states = self.pre_layer_norm(hidden_states)
196
+
197
+ query, key = self.in_proj_qk(hidden_states).chunk(2, dim=2) # shape: [T, B, D]
198
+ value = self.in_proj_v(hidden_states) # shape: [T, B, D]
199
+
200
+ pos = self.in_proj_qk(self.dropout(relative_embedding)) # shape: [2T-1, 2D]
201
+ pos = F.embedding(self.position_indices[:query_len, :key_len], pos) # shape: [T, T, 2D]
202
+ pos = pos.view(query_len, key_len, self.num_heads, 2*self.head_size)
203
+ query_pos, key_pos = pos.chunk(2, dim=3)
204
+
205
+ query = query.reshape(query_len, batch_size * self.num_heads, self.head_size).transpose(0, 1)
206
+ key = key.reshape(key_len, batch_size * self.num_heads, self.head_size).transpose(0, 1)
207
+ value = value.view(key_len, batch_size * self.num_heads, self.head_size).transpose(0, 1)
208
+
209
+ attention_scores = torch.bmm(query, key.transpose(1, 2) * self.scale)
210
+
211
+ query = query.view(batch_size, self.num_heads, query_len, self.head_size)
212
+ key = key.view(batch_size, self.num_heads, query_len, self.head_size)
213
+ attention_scores = attention_scores.view(batch_size, self.num_heads, query_len, key_len)
214
+ attention_scores.add_(torch.einsum("bhqd,qkhd->bhqk", query, key_pos * self.scale))
215
+ attention_scores.add_(torch.einsum("bhkd,qkhd->bhqk", key * self.scale, query_pos))
216
+
217
+ return attention_scores, value
218
+
219
+ def compute_output(self, attention_probs, value):
220
+ attention_probs = self.dropout(attention_probs)
221
+ context = torch.bmm(attention_probs.flatten(0, 1), value) # shape: [B*H, Q, D]
222
+ context = context.transpose(0, 1).reshape(context.size(1), -1, self.hidden_size) # shape: [Q, B, H*D]
223
+ context = self.out_proj(context)
224
+ context = self.post_layer_norm(context)
225
+ context = self.dropout(context)
226
+ return context
227
+
228
+ def forward(self, hidden_states, attention_mask, relative_embedding):
229
+ attention_scores, value = self.compute_attention_scores(hidden_states, relative_embedding)
230
+ attention_probs = MaskedSoftmax.apply(attention_scores, attention_mask, -1)
231
+ return self.compute_output(attention_probs, value), attention_probs.detach()
232
+
233
+
234
+ class Embedding(nn.Module):
235
+ def __init__(self, config):
236
+ super().__init__()
237
+ self.hidden_size = config.hidden_size
238
+
239
+ self.word_embedding = nn.Embedding(config.vocab_size, config.hidden_size)
240
+ self.word_layer_norm = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps, elementwise_affine=False)
241
+ self.dropout = nn.Dropout(config.hidden_dropout_prob)
242
+
243
+ self.relative_embedding = nn.Parameter(torch.empty(2 * config.position_bucket_size - 1, config.hidden_size))
244
+ self.relative_layer_norm = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps)
245
+
246
+ self.initialize()
247
+
248
+ def initialize(self):
249
+ std = math.sqrt(2.0 / (5.0 * self.hidden_size))
250
+ nn.init.trunc_normal_(self.relative_embedding, mean=0.0, std=std, a=-2*std, b=2*std)
251
+ nn.init.trunc_normal_(self.word_embedding.weight, mean=0.0, std=std, a=-2*std, b=2*std)
252
+
253
+ def forward(self, input_ids):
254
+ word_embedding = self.dropout(self.word_layer_norm(self.word_embedding(input_ids)))
255
+ relative_embeddings = self.relative_layer_norm(self.relative_embedding)
256
+ return word_embedding, relative_embeddings
257
+
258
+
259
+ #
260
+ # HuggingFace wrappers
261
+ #
262
+
263
+ class NorbertPreTrainedModel(PreTrainedModel):
264
+ config_class = NorbertConfig
265
+ base_model_prefix = "norbert3"
266
+ supports_gradient_checkpointing = True
267
+
268
+ def _set_gradient_checkpointing(self, module, value=False):
269
+ if isinstance(module, Encoder):
270
+ module.activation_checkpointing = value
271
+
272
+ def _init_weights(self, module):
273
+ pass # everything is already initialized
274
+
275
+
276
+ class NorbertModel(NorbertPreTrainedModel):
277
+ def __init__(self, config, add_mlm_layer=False):
278
+ super().__init__(config)
279
+ self.config = config
280
+
281
+ self.embedding = Embedding(config)
282
+ self.transformer = Encoder(config, activation_checkpointing=False)
283
+ self.classifier = MaskClassifier(config, self.embedding.word_embedding.weight) if add_mlm_layer else None
284
+
285
+ def get_input_embeddings(self):
286
+ return self.embedding.word_embedding
287
+
288
+ def set_input_embeddings(self, value):
289
+ self.embedding.word_embedding = value
290
+
291
+ def get_contextualized_embeddings(
292
+ self,
293
+ input_ids: Optional[torch.Tensor] = None,
294
+ attention_mask: Optional[torch.Tensor] = None
295
+ ) -> List[torch.Tensor]:
296
+ if input_ids is not None:
297
+ input_shape = input_ids.size()
298
+ else:
299
+ raise ValueError("You have to specify input_ids")
300
+
301
+ batch_size, seq_length = input_shape
302
+ device = input_ids.device
303
+
304
+ if attention_mask is None:
305
+ attention_mask = torch.zeros(batch_size, seq_length, dtype=torch.bool, device=device)
306
+ else:
307
+ attention_mask = ~attention_mask.bool()
308
+ attention_mask = attention_mask.unsqueeze(1).unsqueeze(2)
309
+
310
+ static_embeddings, relative_embedding = self.embedding(input_ids.t())
311
+ contextualized_embeddings, attention_probs = self.transformer(static_embeddings, attention_mask, relative_embedding)
312
+ contextualized_embeddings = [e.transpose(0, 1) for e in contextualized_embeddings]
313
+ last_layer = contextualized_embeddings[-1]
314
+ contextualized_embeddings = [contextualized_embeddings[0]] + [
315
+ contextualized_embeddings[i] - contextualized_embeddings[i - 1]
316
+ for i in range(1, len(contextualized_embeddings))
317
+ ]
318
+ return last_layer, contextualized_embeddings, attention_probs
319
+
320
+ def forward(
321
+ self,
322
+ input_ids: Optional[torch.Tensor] = None,
323
+ attention_mask: Optional[torch.Tensor] = None,
324
+ token_type_ids: Optional[torch.Tensor] = None,
325
+ position_ids: Optional[torch.Tensor] = None,
326
+ output_hidden_states: Optional[bool] = None,
327
+ output_attentions: Optional[bool] = None,
328
+ return_dict: Optional[bool] = None,
329
+ ) -> Union[Tuple[torch.Tensor], BaseModelOutput]:
330
+ return_dict = return_dict if return_dict is not None else self.config.use_return_dict
331
+
332
+ sequence_output, contextualized_embeddings, attention_probs = self.get_contextualized_embeddings(input_ids, attention_mask)
333
+
334
+ if not return_dict:
335
+ return sequence_output, contextualized_embeddings, attention_probs
336
+
337
+ return BaseModelOutput(
338
+ last_hidden_state=sequence_output,
339
+ hidden_states=contextualized_embeddings,
340
+ attentions=attention_probs
341
+ )
342
+
343
+
344
+ class NorbertForMaskedLM(NorbertModel):
345
+ _keys_to_ignore_on_load_unexpected = ["head"]
346
+
347
+ def __init__(self, config):
348
+ super().__init__(config, add_mlm_layer=True)
349
+
350
+ def get_output_embeddings(self):
351
+ return self.classifier.nonlinearity[-1].weight
352
+
353
+ def set_output_embeddings(self, new_embeddings):
354
+ self.classifier.nonlinearity[-1].weight = new_embeddings
355
+
356
+ def forward(
357
+ self,
358
+ input_ids: Optional[torch.Tensor] = None,
359
+ attention_mask: Optional[torch.Tensor] = None,
360
+ token_type_ids: Optional[torch.Tensor] = None,
361
+ position_ids: Optional[torch.Tensor] = None,
362
+ output_hidden_states: Optional[bool] = None,
363
+ output_attentions: Optional[bool] = None,
364
+ return_dict: Optional[bool] = None,
365
+ labels: Optional[torch.LongTensor] = None,
366
+ ) -> Union[Tuple[torch.Tensor], MaskedLMOutput]:
367
+ return_dict = return_dict if return_dict is not None else self.config.use_return_dict
368
+
369
+ sequence_output, contextualized_embeddings, attention_probs = self.get_contextualized_embeddings(input_ids, attention_mask)
370
+ subword_prediction = self.classifier(sequence_output)
371
+ subword_prediction[:, :, :106+1] = float("-inf")
372
+
373
+ masked_lm_loss = None
374
+ if labels is not None:
375
+ masked_lm_loss = F.cross_entropy(subword_prediction.flatten(0, 1), labels.flatten())
376
+
377
+ if not return_dict:
378
+ output = (subword_prediction, contextualized_embeddings, attention_probs)
379
+ return ((masked_lm_loss,) + output) if masked_lm_loss is not None else output
380
+
381
+ return MaskedLMOutput(
382
+ loss=masked_lm_loss,
383
+ logits=subword_prediction,
384
+ hidden_states=contextualized_embeddings,
385
+ attentions=attention_probs
386
+ )
387
+
388
+
389
+ class Classifier(nn.Module):
390
+ def __init__(self, config, num_labels: int):
391
+ super().__init__()
392
+
393
+ drop_out = getattr(config, "cls_dropout", None)
394
+ drop_out = config.hidden_dropout_prob if drop_out is None else drop_out
395
+
396
+ self.nonlinearity = nn.Sequential(
397
+ nn.LayerNorm(config.hidden_size, config.layer_norm_eps, elementwise_affine=False),
398
+ nn.Linear(config.hidden_size, config.hidden_size),
399
+ nn.GELU(),
400
+ nn.LayerNorm(config.hidden_size, config.layer_norm_eps, elementwise_affine=False),
401
+ nn.Dropout(drop_out),
402
+ nn.Linear(config.hidden_size, num_labels)
403
+ )
404
+ self.initialize(config.hidden_size)
405
+
406
+ def initialize(self, hidden_size):
407
+ std = math.sqrt(2.0 / (5.0 * hidden_size))
408
+ nn.init.trunc_normal_(self.nonlinearity[1].weight, mean=0.0, std=std, a=-2*std, b=2*std)
409
+ nn.init.trunc_normal_(self.nonlinearity[-1].weight, mean=0.0, std=std, a=-2*std, b=2*std)
410
+ self.nonlinearity[1].bias.data.zero_()
411
+ self.nonlinearity[-1].bias.data.zero_()
412
+
413
+ def forward(self, x):
414
+ x = self.nonlinearity(x)
415
+ return x
416
+
417
+
418
+ class NorbertForSequenceClassification(NorbertModel):
419
+ _keys_to_ignore_on_load_unexpected = ["classifier"]
420
+ _keys_to_ignore_on_load_missing = ["head"]
421
+
422
+ def __init__(self, config):
423
+ super().__init__(config, add_mlm_layer=False)
424
+
425
+ self.num_labels = config.num_labels
426
+ self.head = Classifier(config, self.num_labels)
427
+
428
+ def forward(
429
+ self,
430
+ input_ids: Optional[torch.Tensor] = None,
431
+ attention_mask: Optional[torch.Tensor] = None,
432
+ token_type_ids: Optional[torch.Tensor] = None,
433
+ position_ids: Optional[torch.Tensor] = None,
434
+ output_attentions: Optional[bool] = None,
435
+ output_hidden_states: Optional[bool] = None,
436
+ return_dict: Optional[bool] = None,
437
+ labels: Optional[torch.LongTensor] = None,
438
+ ) -> Union[Tuple[torch.Tensor], SequenceClassifierOutput]:
439
+ return_dict = return_dict if return_dict is not None else self.config.use_return_dict
440
+
441
+ sequence_output, contextualized_embeddings, attention_probs = self.get_contextualized_embeddings(input_ids, attention_mask)
442
+ logits = self.head(sequence_output[:, 0, :])
443
+
444
+ loss = None
445
+ if labels is not None:
446
+ if self.config.problem_type is None:
447
+ if self.num_labels == 1:
448
+ self.config.problem_type = "regression"
449
+ elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
450
+ self.config.problem_type = "single_label_classification"
451
+ else:
452
+ self.config.problem_type = "multi_label_classification"
453
+
454
+ if self.config.problem_type == "regression":
455
+ loss_fct = nn.MSELoss()
456
+ if self.num_labels == 1:
457
+ loss = loss_fct(logits.squeeze(), labels.squeeze())
458
+ else:
459
+ loss = loss_fct(logits, labels)
460
+ elif self.config.problem_type == "single_label_classification":
461
+ loss_fct = nn.CrossEntropyLoss()
462
+ loss = loss_fct(logits.view(-1, self.num_labels), labels.view(-1))
463
+ elif self.config.problem_type == "multi_label_classification":
464
+ loss_fct = nn.BCEWithLogitsLoss()
465
+ loss = loss_fct(logits, labels)
466
+
467
+ if not return_dict:
468
+ output = (logits, contextualized_embeddings, attention_probs)
469
+ return ((loss,) + output) if loss is not None else output
470
+
471
+ return SequenceClassifierOutput(
472
+ loss=loss,
473
+ logits=logits,
474
+ hidden_states=contextualized_embeddings,
475
+ attentions=attention_probs
476
+ )
477
+
478
+
479
+ class NorbertForTokenClassification(NorbertModel):
480
+ _keys_to_ignore_on_load_unexpected = ["classifier"]
481
+ _keys_to_ignore_on_load_missing = ["head"]
482
+
483
+ def __init__(self, config):
484
+ super().__init__(config, add_mlm_layer=False)
485
+
486
+ self.num_labels = config.num_labels
487
+ self.head = Classifier(config, self.num_labels)
488
+
489
+ def forward(
490
+ self,
491
+ input_ids: Optional[torch.Tensor] = None,
492
+ attention_mask: Optional[torch.Tensor] = None,
493
+ token_type_ids: Optional[torch.Tensor] = None,
494
+ position_ids: Optional[torch.Tensor] = None,
495
+ output_attentions: Optional[bool] = None,
496
+ output_hidden_states: Optional[bool] = None,
497
+ return_dict: Optional[bool] = None,
498
+ labels: Optional[torch.LongTensor] = None,
499
+ ) -> Union[Tuple[torch.Tensor], TokenClassifierOutput]:
500
+ return_dict = return_dict if return_dict is not None else self.config.use_return_dict
501
+
502
+ sequence_output, contextualized_embeddings, attention_probs = self.get_contextualized_embeddings(input_ids, attention_mask)
503
+ logits = self.head(sequence_output)
504
+
505
+ loss = None
506
+ if labels is not None:
507
+ loss_fct = nn.CrossEntropyLoss()
508
+ loss = loss_fct(logits.view(-1, self.num_labels), labels.view(-1))
509
+
510
+ if not return_dict:
511
+ output = (logits, contextualized_embeddings, attention_probs)
512
+ return ((loss,) + output) if loss is not None else output
513
+
514
+ return TokenClassifierOutput(
515
+ loss=loss,
516
+ logits=logits,
517
+ hidden_states=contextualized_embeddings,
518
+ attentions=attention_probs
519
+ )
520
+
521
+
522
+ class NorbertForQuestionAnswering(NorbertModel):
523
+ _keys_to_ignore_on_load_unexpected = ["classifier"]
524
+ _keys_to_ignore_on_load_missing = ["head"]
525
+
526
+ def __init__(self, config):
527
+ super().__init__(config, add_mlm_layer=False)
528
+
529
+ self.num_labels = config.num_labels
530
+ self.head = Classifier(config, self.num_labels)
531
+
532
+ def forward(
533
+ self,
534
+ input_ids: Optional[torch.Tensor] = None,
535
+ attention_mask: Optional[torch.Tensor] = None,
536
+ token_type_ids: Optional[torch.Tensor] = None,
537
+ position_ids: Optional[torch.Tensor] = None,
538
+ output_attentions: Optional[bool] = None,
539
+ output_hidden_states: Optional[bool] = None,
540
+ return_dict: Optional[bool] = None,
541
+ start_positions: Optional[torch.Tensor] = None,
542
+ end_positions: Optional[torch.Tensor] = None
543
+ ) -> Union[Tuple[torch.Tensor], QuestionAnsweringModelOutput]:
544
+ return_dict = return_dict if return_dict is not None else self.config.use_return_dict
545
+
546
+ sequence_output, contextualized_embeddings, attention_probs = self.get_contextualized_embeddings(input_ids, attention_mask)
547
+ logits = self.head(sequence_output)
548
+
549
+ start_logits, end_logits = logits.split(1, dim=-1)
550
+ start_logits = start_logits.squeeze(-1).contiguous()
551
+ end_logits = end_logits.squeeze(-1).contiguous()
552
+
553
+ total_loss = None
554
+ if start_positions is not None and end_positions is not None:
555
+ # If we are on multi-GPU, split add a dimension
556
+ if len(start_positions.size()) > 1:
557
+ start_positions = start_positions.squeeze(-1)
558
+ if len(end_positions.size()) > 1:
559
+ end_positions = end_positions.squeeze(-1)
560
+
561
+ # sometimes the start/end positions are outside our model inputs, we ignore these terms
562
+ ignored_index = start_logits.size(1)
563
+ start_positions = start_positions.clamp(0, ignored_index)
564
+ end_positions = end_positions.clamp(0, ignored_index)
565
+
566
+ loss_fct = nn.CrossEntropyLoss(ignore_index=ignored_index)
567
+ start_loss = loss_fct(start_logits, start_positions)
568
+ end_loss = loss_fct(end_logits, end_positions)
569
+ total_loss = (start_loss + end_loss) / 2
570
+
571
+ if not return_dict:
572
+ output = start_logits, end_logits, contextualized_embeddings, attention_probs
573
+ return ((total_loss,) + output) if total_loss is not None else output
574
+
575
+ return QuestionAnsweringModelOutput(
576
+ loss=total_loss,
577
+ start_logits=start_logits,
578
+ end_logits=end_logits,
579
+ hidden_states=contextualized_embeddings,
580
+ attentions=attention_probs,
581
+ )
582
+
583
+
584
+ class NorbertForMultipleChoice(NorbertModel):
585
+ _keys_to_ignore_on_load_unexpected = ["classifier"]
586
+ _keys_to_ignore_on_load_missing = ["head"]
587
+
588
+ def __init__(self, config):
589
+ super().__init__(config, add_mlm_layer=False)
590
+
591
+ self.num_labels = getattr(config, "num_labels", 2)
592
+ self.head = Classifier(config, self.num_labels)
593
+
594
+ def forward(
595
+ self,
596
+ input_ids: Optional[torch.Tensor] = None,
597
+ attention_mask: Optional[torch.Tensor] = None,
598
+ token_type_ids: Optional[torch.Tensor] = None,
599
+ position_ids: Optional[torch.Tensor] = None,
600
+ labels: Optional[torch.Tensor] = None,
601
+ return_dict: Optional[bool] = None,
602
+ start_positions: Optional[torch.Tensor] = None,
603
+ end_positions: Optional[torch.Tensor] = None
604
+ ) -> Union[Tuple[torch.Tensor], MultipleChoiceModelOutput]:
605
+ return_dict = return_dict if return_dict is not None else self.config.use_return_dict
606
+ num_choices = input_ids.shape[1]
607
+
608
+ flat_input_ids = input_ids.view(-1, input_ids.size(-1))
609
+ flat_attention_mask = attention_mask.view(-1, attention_mask.size(-1)) if attention_mask is not None else None
610
+
611
+ sequence_output, contextualized_embeddings, attention_probs = self.get_contextualized_embeddings(flat_input_ids, flat_attention_mask)
612
+ logits = self.head(sequence_output)
613
+ reshaped_logits = logits.view(-1, num_choices)
614
+
615
+ loss = None
616
+ if labels is not None:
617
+ loss_fct = nn.CrossEntropyLoss()
618
+ loss = loss_fct(reshaped_logits, labels)
619
+
620
+ if not return_dict:
621
+ output = (reshaped_logits, contextualized_embeddings, attention_probs)
622
+ return ((loss,) + output) if loss is not None else output
623
+
624
+ return MultipleChoiceModelOutput(
625
+ loss=loss,
626
+ logits=reshaped_logits,
627
+ hidden_states=contextualized_embeddings,
628
+ attentions=attention_probs,
629
+ )
pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:6efc91a5cf70b4dcb742e2ddb04ed151a48564dbac8c67f4c285009f90dcc312
3
+ size 521531691
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
+ }