haznitrama commited on
Commit
23b3b53
·
verified ·
1 Parent(s): 0efd819

Add main weights for eng

Browse files
README.md ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ library_name: transformers
3
+ pipeline_tag: text-generation
4
+ tags: [gpt-bert, babylm, remote-code]
5
+ license: other
6
+ ---
7
+ # haznitrama/babybabellm-multi_gpu-gpt_bert-eng-main-causal
8
+
9
+ GPT-BERT style BabyBabyLLM model for language **eng**.
10
+
11
+ This repository may include both *main* and *EMA* variants.
12
+
13
+ **Default variant exposed to generic loaders:** `main`
14
+
15
+ ## Variants Available
16
+ main
17
+
18
+ ## Files
19
+ - model.safetensors (alias of default variant)
20
+
21
+ ## Configuration
22
+ ```json
23
+ {
24
+ "attention_probs_dropout_prob": 0.1,
25
+ "hidden_dropout_prob": 0.1,
26
+ "hidden_size": 768,
27
+ "intermediate_size": 2560,
28
+ "max_position_embeddings": 512,
29
+ "position_bucket_size": 32,
30
+ "num_attention_heads": 12,
31
+ "num_hidden_layers": 12,
32
+ "vocab_size": 16384,
33
+ "layer_norm_eps": 1e-05
34
+ }
35
+ ```
36
+ Tokenizer file: `tokenizer_eng.json`
37
+
38
+ ## Quick Usage
39
+ ```python
40
+ from transformers import AutoTokenizer, AutoModelForMaskedLM
41
+ model_id = 'haznitrama/babybabellm-multi_gpu-gpt_bert-eng-main-causal'
42
+ tok = AutoTokenizer.from_pretrained(model_id)
43
+ model = AutoModelForMaskedLM.from_pretrained(model_id, trust_remote_code=True)
44
+ out = model(**tok('Hello world', return_tensors='pt'))
45
+ ```
46
+
47
+ ### Causal LM Wrapper
48
+ This repo includes a lightweight GPTBertForCausalLM wrapper.
49
+ Generation example:
50
+ ```python
51
+ from transformers import AutoTokenizer, AutoModelForCausalLM
52
+ mid='haznitrama/babybabellm-multi_gpu-gpt_bert-eng-main-causal'
53
+ tok=AutoTokenizer.from_pretrained(mid)
54
+ model=AutoModelForCausalLM.from_pretrained(mid, trust_remote_code=True)
55
+ print(tok.decode(model.generate(**tok('Hello', return_tensors='pt'), max_new_tokens=20)[0], skip_special_tokens=True))
56
+ ```
57
+
58
+ ## Notes
59
+ - Converted on 2025-09-27T15:21:53.977598+00:00
60
+ - Weights are the exact trained parameters; no new layers were initialized.
61
+ - Requires `trust_remote_code=True` due to custom architecture.
config.json ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "GPTBertForMaskedLM",
4
+ "GPTBertForCausalLM"
5
+ ],
6
+ "attention_probs_dropout_prob": 0.1,
7
+ "auto_map": {
8
+ "AutoConfig": "configuration_gpt_bert.GPTBertConfig",
9
+ "AutoModel": "modeling_gpt_bert.GPTBertForCausalLM",
10
+ "AutoModelForCausalLM": "modeling_gpt_bert.GPTBertForCausalLM",
11
+ "AutoModelForMaskedLM": "modeling_gpt_bert.GPTBertForMaskedLM"
12
+ },
13
+ "bos_token_id": 1,
14
+ "eos_token_id": 2,
15
+ "hidden_dropout_prob": 0.1,
16
+ "hidden_size": 768,
17
+ "intermediate_size": 2560,
18
+ "layer_norm_eps": 1e-05,
19
+ "mask_token_id": 4,
20
+ "max_position_embeddings": 512,
21
+ "model_type": "gpt_bert",
22
+ "num_attention_heads": 12,
23
+ "num_hidden_layers": 12,
24
+ "pad_token_id": 3,
25
+ "position_bucket_size": 32,
26
+ "vocab_size": 16384
27
+ }
configuration_gpt_bert.py ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import PretrainedConfig
2
+
3
+ class GPTBertConfig(PretrainedConfig):
4
+ model_type = 'gpt_bert'
5
+ def __init__(self, **kwargs):
6
+ self.attention_probs_dropout_prob = kwargs.pop('attention_probs_dropout_prob', 0.1)
7
+ self.hidden_dropout_prob = kwargs.pop('hidden_dropout_prob', 0.1)
8
+ self.hidden_size = kwargs.pop('hidden_size', 768)
9
+ self.intermediate_size = kwargs.pop('intermediate_size', 2560)
10
+ self.max_position_embeddings = kwargs.pop('max_position_embeddings', 512)
11
+ self.position_bucket_size = kwargs.pop('position_bucket_size', 32)
12
+ self.num_attention_heads = kwargs.pop('num_attention_heads', 12)
13
+ self.num_hidden_layers = kwargs.pop('num_hidden_layers', 12)
14
+ self.vocab_size = kwargs.pop('vocab_size', 16384)
15
+ self.layer_norm_eps = kwargs.pop('layer_norm_eps', 1e-5)
16
+ self.auto_map = {
17
+ 'AutoConfig': 'configuration_gpt_bert.GPTBertConfig',
18
+ 'AutoModel': 'modeling_gpt_bert.GPTBertForCausalLM',
19
+ 'AutoModelForCausalLM': 'modeling_gpt_bert.GPTBertForCausalLM',
20
+ 'AutoModelForMaskedLM': 'modeling_gpt_bert.GPTBertForMaskedLM',
21
+ }
22
+ super().__init__(**kwargs)
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:2e7ce858c260e48aac28f32d49a28061458f1974dea791677ae05d1be60aa184
3
+ size 553331552
modeling_gpt_bert.py ADDED
@@ -0,0 +1,448 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Original training architecture (verbatim)
2
+ import math
3
+
4
+ import torch
5
+ import torch.nn as nn
6
+ import torch.nn.functional as F
7
+ from torch import _softmax_backward_data as _softmax_backward_data
8
+
9
+
10
+ class Bert(nn.Module):
11
+ def __init__(self, config, activation_checkpointing=False):
12
+ super().__init__()
13
+ self.embedding = Embedding(config)
14
+ self.transformer = Encoder(config, activation_checkpointing)
15
+ self.classifier = MaskClassifier(config, self.embedding.word_embedding.weight)
16
+
17
+ def get_contextualized(self, input_ids, attention_mask):
18
+ static_embeddings, relative_embedding = self.embedding(input_ids)
19
+ contextualized_embeddings = self.transformer(static_embeddings, attention_mask.unsqueeze(1), relative_embedding)
20
+ return contextualized_embeddings
21
+
22
+ def forward(self, input_ids, attention_mask, masked_lm_labels, num_masked=None, ratio=None):
23
+ contextualized_embeddings = self.get_contextualized(input_ids, attention_mask)
24
+
25
+ if num_masked is None:
26
+ subword_prediction = self.classifier(contextualized_embeddings, masked_lm_labels, num_masked)
27
+
28
+ gold_labels = masked_lm_labels.flatten()
29
+ gold_labels = gold_labels[gold_labels != -100]
30
+
31
+ loss = F.cross_entropy(subword_prediction, gold_labels, reduction="none").mean()
32
+ z_loss = torch.logsumexp(subword_prediction, dim=-1).pow(2).mean()
33
+
34
+ with torch.no_grad():
35
+ accuracy = (subword_prediction.argmax(-1) == gold_labels).float().mean()
36
+
37
+ num_tokens = gold_labels.size(0)
38
+
39
+ return loss, accuracy, z_loss, num_tokens
40
+ else:
41
+ masked_subword_prediction, causal_subword_prediction = self.classifier(contextualized_embeddings, masked_lm_labels, num_masked)
42
+
43
+ if masked_subword_prediction is not None:
44
+ masked_gold_labels = masked_lm_labels[:, :num_masked].flatten()
45
+ masked_gold_labels = masked_gold_labels[masked_gold_labels != -100]
46
+
47
+ masked_loss = F.cross_entropy(masked_subword_prediction, masked_gold_labels)
48
+ masked_z_loss = torch.logsumexp(masked_subword_prediction, dim=-1).pow(2).mean()
49
+
50
+ with torch.no_grad():
51
+ masked_accuracy = (masked_subword_prediction.argmax(-1) == masked_gold_labels).float().mean()
52
+
53
+ num_masked_tokens = masked_gold_labels.size(0)
54
+ else:
55
+ masked_loss = 0.0
56
+ masked_z_loss = 0.0
57
+ masked_accuracy = 0.0
58
+ num_masked_tokens = 0
59
+
60
+ if causal_subword_prediction is not None:
61
+ causal_gold_labels = masked_lm_labels[:, num_masked:].flatten()
62
+ causal_gold_labels = causal_gold_labels[causal_gold_labels != -100]
63
+
64
+ causal_loss = F.cross_entropy(causal_subword_prediction, causal_gold_labels)
65
+ causal_z_loss = torch.logsumexp(causal_subword_prediction, dim=-1).pow(2).mean()
66
+
67
+ with torch.no_grad():
68
+ causal_accuracy = (causal_subword_prediction.argmax(-1) == causal_gold_labels).float().mean()
69
+
70
+ num_causal_tokens = causal_gold_labels.size(0)
71
+ else:
72
+ causal_loss = 0.0
73
+ causal_z_loss = 0.0
74
+ causal_accuracy = 0.0
75
+ num_causal_tokens = 0
76
+
77
+ loss = ratio * masked_loss + (1 - ratio) * causal_loss
78
+ z_loss = ratio * masked_z_loss + (1 - ratio) * causal_z_loss
79
+
80
+ with torch.no_grad():
81
+ accuracy = ratio * masked_accuracy + (1 - ratio) * causal_accuracy
82
+
83
+ num_tokens = num_masked_tokens + num_causal_tokens
84
+
85
+ return loss, masked_loss, causal_loss, accuracy, masked_accuracy, causal_accuracy, z_loss, num_tokens
86
+
87
+
88
+ # From https://github.com/epfml/DenseFormer
89
+ class InPlaceSetSlice(torch.autograd.Function):
90
+ @staticmethod
91
+ def forward(ctx, full_tensor, last_slice, x_idx, x_val):
92
+ full_tensor[x_idx] = x_val
93
+ ctx.x_idx = x_idx
94
+ ret = torch.Tensor().to(full_tensor.device)
95
+ ret.set_(full_tensor[:x_idx + 1])
96
+ return ret
97
+
98
+ @staticmethod
99
+ def backward(ctx, grad_out):
100
+ if ctx.x_idx == 0:
101
+ return None, None, None, grad_out[ctx.x_idx]
102
+ else:
103
+ return None, grad_out[:ctx.x_idx], None, grad_out[ctx.x_idx]
104
+
105
+
106
+ def apply_inplace_set(x_acc, x_idx, x_val):
107
+ full_tensor, last_slice = x_acc
108
+ new_slice = InPlaceSetSlice.apply(full_tensor, last_slice, x_idx, x_val)
109
+ return full_tensor, new_slice
110
+
111
+
112
+ class DWAModules(torch.nn.Module):
113
+ def __init__(self, hidden_size, n_blocks):
114
+ super().__init__()
115
+ self.n_blocks = n_blocks
116
+ self.alphas = nn.ParameterList([nn.Parameter(torch.zeros(i + 2)) for i in range(n_blocks)])
117
+ self.accumulator = None
118
+ self._init_weights()
119
+
120
+ def _init_weights(self):
121
+ for module in self.alphas:
122
+ module.data.zero_()
123
+ module.data[-1] = 1.0
124
+
125
+ def init_accumulator(self, x):
126
+ self.accumulator = (torch.zeros((self.n_blocks + 1, *x.shape), device=x.device, dtype=x.dtype), None)
127
+ self.accumulator = apply_inplace_set(self.accumulator, 0, x)
128
+
129
+ def forward(self, x, block_idx):
130
+ assert self.accumulator is not None, "`init_accumulator(x)` needs to be called first"
131
+ self.accumulator = apply_inplace_set(
132
+ self.accumulator,
133
+ block_idx + 1,
134
+ x
135
+ )
136
+ x = torch.tensordot(self.alphas[block_idx], self.accumulator[1], dims=1)
137
+ return x
138
+
139
+
140
+ class Encoder(nn.Module):
141
+ def __init__(self, config, activation_checkpointing=False):
142
+ super().__init__()
143
+ self.attention_layers = nn.ModuleList([Attention(config) for _ in range(config.num_hidden_layers)])
144
+ self.mlp_layers = nn.ModuleList([FeedForward(config) for _ in range(config.num_hidden_layers)])
145
+ self.dwa_modules = DWAModules(config.hidden_size, config.num_hidden_layers * 2)
146
+
147
+ for i, layer in enumerate(self.mlp_layers):
148
+ layer.mlp[1].weight.data *= math.sqrt(1.0 / (2.0 * (1 + i)))
149
+ layer.mlp[-2].weight.data *= math.sqrt(1.0 / (2.0 * (1 + i)))
150
+
151
+ self.activation_checkpointing = activation_checkpointing
152
+
153
+ def forward(self, x, attention_mask, relative_embedding):
154
+ self.dwa_modules.init_accumulator(x)
155
+ for i, (attention_layer, mlp_layer) in enumerate(zip(self.attention_layers, self.mlp_layers)):
156
+ x = x + attention_layer(x, attention_mask, relative_embedding)
157
+ x = self.dwa_modules(x, block_idx=i * 2)
158
+
159
+ x = x + mlp_layer(x)
160
+ x = self.dwa_modules(x, block_idx=i * 2 + 1)
161
+
162
+ return x
163
+
164
+
165
+ class MaskClassifier(nn.Module):
166
+ def __init__(self, config, subword_embedding):
167
+ super().__init__()
168
+ self.nonlinearity = nn.Sequential(
169
+ nn.LayerNorm(config.hidden_size, config.layer_norm_eps, elementwise_affine=False),
170
+ nn.Linear(config.hidden_size, config.hidden_size),
171
+ nn.GELU(),
172
+ nn.LayerNorm(config.hidden_size, config.layer_norm_eps, elementwise_affine=False),
173
+ nn.Dropout(config.hidden_dropout_prob),
174
+ nn.Linear(subword_embedding.size(1), subword_embedding.size(0))
175
+ )
176
+ self.initialize(config.hidden_size, subword_embedding)
177
+
178
+ def initialize(self, hidden_size, embedding):
179
+ std = math.sqrt(2.0 / (5.0 * hidden_size))
180
+ nn.init.trunc_normal_(self.nonlinearity[1].weight, mean=0.0, std=std, a=-2*std, b=2*std)
181
+ self.nonlinearity[-1].weight = embedding
182
+ self.nonlinearity[1].bias.data.zero_()
183
+ self.nonlinearity[-1].bias.data.zero_()
184
+
185
+ def forward(self, x, masked_lm_labels, num_masked=None):
186
+ if num_masked is None:
187
+ x = torch.index_select(x.flatten(0, 1), 0, torch.nonzero(masked_lm_labels.flatten() != -100).squeeze())
188
+ x = self.nonlinearity(x)
189
+ return x
190
+ else:
191
+ masked_x, causal_x = torch.tensor_split(x, (num_masked,), dim=1)
192
+ mntp_masked_lm_labels, causal_masked_lm_labels = torch.tensor_split(masked_lm_labels, (num_masked,), dim=1)
193
+
194
+ if masked_x.size(1) != 0:
195
+ masked_x = torch.index_select(masked_x.flatten(0, 1), 0, torch.nonzero(mntp_masked_lm_labels.flatten() != -100).squeeze())
196
+ masked_x = self.nonlinearity(masked_x)
197
+ else:
198
+ masked_x = None
199
+
200
+ if causal_x.size(1) != 0:
201
+ causal_x = torch.index_select(causal_x.flatten(0, 1), 0, torch.nonzero(causal_masked_lm_labels.flatten() != -100).squeeze())
202
+ causal_x = self.nonlinearity(causal_x)
203
+ else:
204
+ causal_x = None
205
+
206
+ return masked_x, causal_x
207
+
208
+
209
+ class GeGLU(nn.Module):
210
+ def forward(self, x):
211
+ x, gate = x.chunk(2, dim=-1)
212
+ x = x * F.gelu(gate, approximate='tanh')
213
+ return x
214
+
215
+
216
+ class FeedForward(nn.Module):
217
+ def __init__(self, config):
218
+ super().__init__()
219
+ self.mlp = nn.Sequential(
220
+ nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps, elementwise_affine=False),
221
+ nn.Linear(config.hidden_size, 2*config.intermediate_size, bias=False),
222
+ GeGLU(),
223
+ nn.LayerNorm(config.intermediate_size, eps=config.layer_norm_eps, elementwise_affine=False),
224
+ nn.Linear(config.intermediate_size, config.hidden_size, bias=False),
225
+ nn.Dropout(config.hidden_dropout_prob)
226
+ )
227
+ self.initialize(config.hidden_size)
228
+
229
+ def initialize(self, hidden_size):
230
+ std = math.sqrt(2.0 / (5.0 * hidden_size))
231
+ nn.init.trunc_normal_(self.mlp[1].weight, mean=0.0, std=std, a=-2*std, b=2*std)
232
+ nn.init.trunc_normal_(self.mlp[-2].weight, mean=0.0, std=std, a=-2*std, b=2*std)
233
+
234
+ def forward(self, x):
235
+ return self.mlp(x)
236
+
237
+
238
+ class MaskedSoftmax(torch.autograd.Function):
239
+ @staticmethod
240
+ def forward(self, x, mask, dim):
241
+ self.dim = dim
242
+ x.masked_fill_(mask, float('-inf'))
243
+ x = torch.softmax(x, self.dim)
244
+ x.masked_fill_(mask, 0.0)
245
+ self.save_for_backward(x)
246
+ return x
247
+
248
+ @staticmethod
249
+ def backward(self, grad_output):
250
+ output, = self.saved_tensors
251
+ inputGrad = _softmax_backward_data(grad_output, output, self.dim, output.dtype)
252
+ return inputGrad, None, None
253
+
254
+
255
+ class Attention(nn.Module):
256
+ def __init__(self, config):
257
+ super().__init__()
258
+
259
+ self.config = config
260
+
261
+ if config.hidden_size % config.num_attention_heads != 0:
262
+ raise ValueError(f"The hidden size {config.hidden_size} is not a multiple of the number of attention heads {config.num_attention_heads}")
263
+
264
+ self.hidden_size = config.hidden_size
265
+ self.num_heads = config.num_attention_heads
266
+ self.head_size = config.hidden_size // config.num_attention_heads
267
+
268
+ self.in_proj_qk = nn.Linear(config.hidden_size, 2*config.hidden_size, bias=True)
269
+ self.in_proj_vg = nn.Linear(config.hidden_size, 2*config.hidden_size, bias=True)
270
+ self.out_proj = nn.Linear(config.hidden_size, config.hidden_size, bias=True)
271
+
272
+ self.pre_layer_norm = nn.LayerNorm(config.hidden_size, config.layer_norm_eps, elementwise_affine=False)
273
+ self.post_layer_norm = nn.LayerNorm(config.hidden_size, config.layer_norm_eps, elementwise_affine=False)
274
+
275
+ position_indices = torch.arange(config.max_position_embeddings, dtype=torch.long).unsqueeze(1) \
276
+ - torch.arange(config.max_position_embeddings, dtype=torch.long).unsqueeze(0)
277
+ position_indices = self.make_log_bucket_position(position_indices, config.position_bucket_size, config.max_position_embeddings)
278
+ position_indices = config.position_bucket_size - 1 + position_indices
279
+ self.register_buffer("position_indices", position_indices, persistent=True)
280
+
281
+ self.dropout = nn.Dropout(config.attention_probs_dropout_prob)
282
+ self.scale = 1.0 / math.sqrt(3 * self.head_size)
283
+ self.initialize()
284
+
285
+ def make_log_bucket_position(self, relative_pos, bucket_size, max_position):
286
+ sign = torch.sign(relative_pos)
287
+ mid = bucket_size // 2
288
+ abs_pos = torch.where((relative_pos < mid) & (relative_pos > -mid), mid - 1, torch.abs(relative_pos).clamp(max=max_position - 1))
289
+ log_pos = torch.ceil(torch.log(abs_pos / mid) / math.log((max_position-1) / mid) * (mid - 1)).int() + mid
290
+ bucket_pos = torch.where(abs_pos <= mid, relative_pos, log_pos * sign).long()
291
+ return bucket_pos
292
+
293
+ def initialize(self):
294
+ std = math.sqrt(2.0 / (5.0 * self.hidden_size))
295
+ nn.init.trunc_normal_(self.in_proj_qk.weight, mean=0.0, std=std, a=-2*std, b=2*std)
296
+ nn.init.trunc_normal_(self.in_proj_vg.weight, mean=0.0, std=std, a=-2*std, b=2*std)
297
+ nn.init.trunc_normal_(self.out_proj.weight, mean=0.0, std=std, a=-2*std, b=2*std)
298
+ self.in_proj_qk.bias.data.zero_()
299
+ self.in_proj_vg.bias.data.zero_()
300
+ self.out_proj.bias.data.zero_()
301
+
302
+ def forward(self, hidden_states, attention_mask, relative_embedding):
303
+ key_len, batch_size, _ = hidden_states.size()
304
+ query_len = key_len
305
+
306
+ if self.position_indices.size(0) < query_len:
307
+ position_indices = torch.arange(query_len, dtype=torch.long).unsqueeze(1) \
308
+ - torch.arange(query_len, dtype=torch.long).unsqueeze(0)
309
+ position_indices = self.make_log_bucket_position(position_indices, self.config.position_bucket_size, 512)
310
+ position_indices = self.config.position_bucket_size - 1 + position_indices
311
+ self.register_buffer("position_indices", position_indices.to(hidden_states.device), persistent=True)
312
+
313
+ hidden_states = self.pre_layer_norm(hidden_states)
314
+ query, key = self.in_proj_qk(hidden_states).chunk(2, dim=2) # shape: [T, B, D]
315
+ value, gate = self.in_proj_vg(hidden_states).chunk(2, dim=2) # shape: [T, B, D]
316
+ gate = F.gelu(gate)
317
+
318
+ pos = self.in_proj_qk(self.dropout(relative_embedding)) # shape: [2T-1, 2D]
319
+ pos = F.embedding(self.position_indices[:query_len, :key_len], pos) # shape: [T, T, 2D]
320
+ query_pos, key_pos = pos.chunk(2, dim=-1)
321
+ query_pos = query_pos.view(query_len, key_len, self.num_heads, self.head_size)
322
+ key_pos = key_pos.view(query_len, key_len, self.num_heads, self.head_size)
323
+
324
+ query = query.reshape(query_len, batch_size * self.num_heads, self.head_size).transpose(0, 1)
325
+ key = key.reshape(key_len, batch_size * self.num_heads, self.head_size).transpose(0, 1)
326
+ value = value.reshape(key_len, batch_size * self.num_heads, self.head_size).transpose(0, 1)
327
+
328
+ attention_scores = torch.bmm(query, key.transpose(1, 2) * self.scale)
329
+
330
+ query = query.view(batch_size, self.num_heads, query_len, self.head_size)
331
+ key = key.view(batch_size, self.num_heads, query_len, self.head_size)
332
+ attention_scores = attention_scores.view(batch_size, self.num_heads, query_len, key_len)
333
+ attention_scores.add_(torch.einsum("bhqd,qkhd->bhqk", query, key_pos * self.scale))
334
+ attention_scores.add_(torch.einsum("bhkd,qkhd->bhqk", key * self.scale, query_pos))
335
+
336
+ attention_probs = MaskedSoftmax.apply(attention_scores, attention_mask, -1)
337
+
338
+ attention_probs = self.dropout(attention_probs)
339
+ context = torch.bmm(attention_probs.flatten(0, 1), value) # shape: [B*H, Q, D]
340
+ context = context.transpose(0, 1).reshape(context.size(1), -1, self.hidden_size) # shape: [Q, B, H*D]
341
+ context = context * gate
342
+ context = self.post_layer_norm(context)
343
+ context = self.out_proj(context)
344
+ context = self.dropout(context)
345
+
346
+ return context
347
+
348
+
349
+ class Embedding(nn.Module):
350
+ def __init__(self, config):
351
+ super().__init__()
352
+ self.hidden_size = config.hidden_size
353
+
354
+ self.word_embedding = nn.Embedding(config.vocab_size, config.hidden_size)
355
+ self.word_layer_norm = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps, elementwise_affine=False)
356
+ self.dropout = nn.Dropout(config.hidden_dropout_prob)
357
+
358
+ self.relative_embedding = nn.Parameter(torch.empty(2 * config.position_bucket_size - 1, config.hidden_size))
359
+ self.relative_layer_norm = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps)
360
+
361
+ self.initialize()
362
+
363
+ def initialize(self):
364
+ std = math.sqrt(2.0 / (5.0 * self.hidden_size))
365
+ nn.init.trunc_normal_(self.relative_embedding, mean=0.0, std=std, a=-2*std, b=2*std)
366
+ nn.init.trunc_normal_(self.word_embedding.weight, mean=0.0, std=std, a=-2*std, b=2*std)
367
+
368
+ def forward(self, input_ids):
369
+ word_embedding = self.dropout(self.word_layer_norm(self.word_embedding(input_ids)))
370
+ relative_embeddings = self.relative_layer_norm(self.relative_embedding)
371
+ return word_embedding, relative_embeddings
372
+
373
+
374
+ # HF wrappers that preserve state dict keys and behavior
375
+
376
+
377
+ from transformers import PreTrainedModel
378
+ from transformers.modeling_outputs import MaskedLMOutput, CausalLMOutputWithCrossAttentions
379
+ from .configuration_gpt_bert import GPTBertConfig
380
+ import torch
381
+ import torch.nn as nn
382
+
383
+ class GPTBertForMaskedLM(PreTrainedModel):
384
+ config_class = GPTBertConfig
385
+ base_model_prefix = 'gpt_bert'
386
+ def __init__(self, config: GPTBertConfig):
387
+ super().__init__(config)
388
+ self.model = Bert(config)
389
+
390
+ def tie_weights(self):
391
+ try:
392
+ self.model.classifier.nonlinearity[-1].weight = self.model.embedding.word_embedding.weight
393
+ except Exception:
394
+ pass
395
+ return super().tie_weights()
396
+
397
+ def forward(self, input_ids, attention_mask=None, labels=None):
398
+ if attention_mask is None:
399
+ attention_mask = torch.ones_like(input_ids)
400
+ mask_bool = (attention_mask == 0).unsqueeze(1).unsqueeze(1)
401
+ static_embeddings, relative_embedding = self.model.embedding(input_ids)
402
+ if static_embeddings.dim() == 3 and static_embeddings.shape[0] == input_ids.shape[0]:
403
+ static_embeddings = static_embeddings.transpose(0, 1)
404
+ contextualized = self.model.transformer(static_embeddings, mask_bool, relative_embedding)
405
+ hs = contextualized.transpose(0, 1)
406
+ B,S,H = hs.shape
407
+ flat = hs.reshape(B*S, H)
408
+ logits_flat = self.model.classifier.nonlinearity(flat)
409
+ vocab = logits_flat.size(-1)
410
+ logits = logits_flat.view(B, S, vocab)
411
+ loss = None
412
+ if labels is not None:
413
+ loss_fct = nn.CrossEntropyLoss(ignore_index=-100)
414
+ loss = loss_fct(logits.view(-1, vocab), labels.view(-1))
415
+ return MaskedLMOutput(loss=loss, logits=logits)
416
+
417
+
418
+ class GPTBertForCausalLM(PreTrainedModel):
419
+ config_class = GPTBertConfig
420
+ base_model_prefix = 'gpt_bert'
421
+ def __init__(self, config: GPTBertConfig):
422
+ super().__init__(config)
423
+ self.model = Bert(config)
424
+
425
+ def prepare_inputs_for_generation(self, input_ids, **kwargs):
426
+ return {'input_ids': input_ids, 'attention_mask': kwargs.get('attention_mask', None)}
427
+
428
+ def forward(self, input_ids, attention_mask=None, labels=None):
429
+ if attention_mask is None:
430
+ attention_mask = torch.ones_like(input_ids)
431
+ mask_bool = (attention_mask == 0).unsqueeze(1).unsqueeze(1)
432
+ static_embeddings, relative_embedding = self.model.embedding(input_ids)
433
+ if static_embeddings.dim() == 3 and static_embeddings.shape[0] == input_ids.shape[0]:
434
+ static_embeddings = static_embeddings.transpose(0, 1)
435
+ contextualized = self.model.transformer(static_embeddings, mask_bool, relative_embedding)
436
+ hs = contextualized.transpose(0, 1)
437
+ B,S,H = hs.shape
438
+ flat = hs.reshape(B*S, H)
439
+ logits_flat = self.model.classifier.nonlinearity(flat)
440
+ vocab = logits_flat.size(-1)
441
+ logits = logits_flat.view(B, S, vocab)
442
+ loss = None
443
+ if labels is not None:
444
+ shift_logits = logits[..., :-1, :].contiguous()
445
+ shift_labels = labels[..., 1:].contiguous()
446
+ loss_fct = nn.CrossEntropyLoss(ignore_index=-100)
447
+ loss = loss_fct(shift_logits.view(-1, shift_logits.size(-1)), shift_labels.view(-1))
448
+ return CausalLMOutputWithCrossAttentions(loss=loss, logits=logits)
original_project_config.json ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "attention_probs_dropout_prob": 0.1,
3
+ "hidden_dropout_prob": 0.1,
4
+ "hidden_size": 768,
5
+ "intermediate_size": 2560,
6
+ "max_position_embeddings": 512,
7
+ "position_bucket_size": 32,
8
+ "num_attention_heads": 12,
9
+ "num_hidden_layers": 12,
10
+ "vocab_size": 16384,
11
+ "layer_norm_eps": 1e-05
12
+ }
special_tokens_map.json ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ {
2
+ "bos_token": "<s>",
3
+ "eos_token": "</s>",
4
+ "mask_token": "<mask>",
5
+ "pad_token": "<pad>",
6
+ "unk_token": "<unk>"
7
+ }
tokenizer.json ADDED
The diff for this file is too large to render. See raw diff
 
tokenizer_config.json ADDED
@@ -0,0 +1,141 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "added_tokens_decoder": {
3
+ "0": {
4
+ "content": "<unk>",
5
+ "lstrip": false,
6
+ "normalized": false,
7
+ "rstrip": false,
8
+ "single_word": false,
9
+ "special": true
10
+ },
11
+ "1": {
12
+ "content": "<s>",
13
+ "lstrip": false,
14
+ "normalized": false,
15
+ "rstrip": false,
16
+ "single_word": false,
17
+ "special": true
18
+ },
19
+ "2": {
20
+ "content": "</s>",
21
+ "lstrip": false,
22
+ "normalized": false,
23
+ "rstrip": false,
24
+ "single_word": false,
25
+ "special": true
26
+ },
27
+ "3": {
28
+ "content": "<pad>",
29
+ "lstrip": false,
30
+ "normalized": false,
31
+ "rstrip": false,
32
+ "single_word": false,
33
+ "special": true
34
+ },
35
+ "4": {
36
+ "content": "<mask>",
37
+ "lstrip": false,
38
+ "normalized": false,
39
+ "rstrip": false,
40
+ "single_word": false,
41
+ "special": true
42
+ },
43
+ "5": {
44
+ "content": "<special_0>",
45
+ "lstrip": false,
46
+ "normalized": false,
47
+ "rstrip": false,
48
+ "single_word": false,
49
+ "special": true
50
+ },
51
+ "6": {
52
+ "content": "<special_1>",
53
+ "lstrip": false,
54
+ "normalized": false,
55
+ "rstrip": false,
56
+ "single_word": false,
57
+ "special": true
58
+ },
59
+ "7": {
60
+ "content": "<special_2>",
61
+ "lstrip": false,
62
+ "normalized": false,
63
+ "rstrip": false,
64
+ "single_word": false,
65
+ "special": true
66
+ },
67
+ "8": {
68
+ "content": "<special_3>",
69
+ "lstrip": false,
70
+ "normalized": false,
71
+ "rstrip": false,
72
+ "single_word": false,
73
+ "special": true
74
+ },
75
+ "9": {
76
+ "content": "<special_4>",
77
+ "lstrip": false,
78
+ "normalized": false,
79
+ "rstrip": false,
80
+ "single_word": false,
81
+ "special": true
82
+ },
83
+ "10": {
84
+ "content": "<special_5>",
85
+ "lstrip": false,
86
+ "normalized": false,
87
+ "rstrip": false,
88
+ "single_word": false,
89
+ "special": true
90
+ },
91
+ "11": {
92
+ "content": "<special_6>",
93
+ "lstrip": false,
94
+ "normalized": false,
95
+ "rstrip": false,
96
+ "single_word": false,
97
+ "special": true
98
+ },
99
+ "12": {
100
+ "content": "<special_7>",
101
+ "lstrip": false,
102
+ "normalized": false,
103
+ "rstrip": false,
104
+ "single_word": false,
105
+ "special": true
106
+ },
107
+ "13": {
108
+ "content": "<special_8>",
109
+ "lstrip": false,
110
+ "normalized": false,
111
+ "rstrip": false,
112
+ "single_word": false,
113
+ "special": true
114
+ },
115
+ "14": {
116
+ "content": "<special_9>",
117
+ "lstrip": false,
118
+ "normalized": false,
119
+ "rstrip": false,
120
+ "single_word": false,
121
+ "special": true
122
+ },
123
+ "15": {
124
+ "content": "<special_10>",
125
+ "lstrip": false,
126
+ "normalized": false,
127
+ "rstrip": false,
128
+ "single_word": false,
129
+ "special": true
130
+ }
131
+ },
132
+ "bos_token": "<s>",
133
+ "clean_up_tokenization_spaces": false,
134
+ "eos_token": "</s>",
135
+ "extra_special_tokens": {},
136
+ "mask_token": "<mask>",
137
+ "model_max_length": 1000000000000000019884624838656,
138
+ "pad_token": "<pad>",
139
+ "tokenizer_class": "PreTrainedTokenizerFast",
140
+ "unk_token": "<unk>"
141
+ }