williamconvertino commited on
Commit
f25c8e8
·
verified ·
1 Parent(s): fec730c

Delete model.py

Browse files
Files changed (1) hide show
  1. model.py +0 -541
model.py DELETED
@@ -1,541 +0,0 @@
1
- from typing import Tuple
2
-
3
- import torch
4
- from torch import Tensor
5
- import torch.nn as nn
6
- import torch.nn.functional as F
7
-
8
- from transformers import PreTrainedModel, GenerationMixin
9
- from transformers.modeling_outputs import BaseModelOutputWithPast, CausalLMOutputWithPast, MaskedLMOutput
10
- from transformers.cache_utils import Cache, DynamicCache
11
-
12
- from rotary_embedding_torch import RotaryEmbedding
13
- from .config import FSTConfig
14
-
15
- # === Util ===
16
-
17
- class Residual(nn.Module):
18
- def __init__(self):
19
- super().__init__()
20
-
21
- def forward(self, x: Tensor, delta: Tensor):
22
- return x + delta
23
-
24
- # === MLP ===
25
-
26
- class MLP(nn.Module):
27
- def __init__(
28
- self,
29
- hidden_size: int,
30
- intermediate_size: int
31
- ):
32
- super().__init__()
33
-
34
- self.fc_up = nn.Linear(hidden_size, intermediate_size)
35
- self.activation = nn.GELU()
36
- self.fc_down = nn.Linear(intermediate_size, hidden_size)
37
-
38
- def forward(self, x: Tensor):
39
- return self.fc_down(self.activation(self.fc_up(x)))
40
-
41
- # === Attention ===
42
-
43
- class MHAttention(nn.Module):
44
-
45
- def __init__(
46
- self,
47
- hidden_size: int,
48
- num_attention_heads: int,
49
- use_causal_attention: bool = True,
50
- layer_idx: int | None = None
51
- ):
52
- super().__init__()
53
-
54
- self.hidden_size = hidden_size
55
- self.num_attention_heads = num_attention_heads
56
- self.head_dim = hidden_size // num_attention_heads
57
-
58
- assert self.head_dim * self.num_attention_heads == self.hidden_size
59
-
60
- self.use_causal_attention = use_causal_attention
61
- self.layer_idx = layer_idx
62
-
63
- self.q_proj = nn.Linear(self.hidden_size, self.hidden_size, bias=False)
64
- self.k_proj = nn.Linear(self.hidden_size, self.hidden_size, bias=False)
65
- self.v_proj = nn.Linear(self.hidden_size, self.hidden_size, bias=True)
66
- self.o_proj = nn.Linear(self.hidden_size, self.hidden_size, bias=True)
67
-
68
- self.rotary_emb = RotaryEmbedding(dim=self.head_dim)
69
- self.scale = self.head_dim ** -0.5
70
-
71
- def forward(
72
- self,
73
- q: Tensor,
74
- k: Tensor | None = None,
75
- v: Tensor | None = None,
76
- attention_mask: Tensor | None = None,
77
- past_key_values: Cache | None = None
78
- ):
79
- B, T, _ = q.size()
80
-
81
- if k is None:
82
- k = q
83
- if v is None:
84
- v = q
85
-
86
- q = self.q_proj(q)
87
- k = self.k_proj(k)
88
- v = self.v_proj(v)
89
-
90
- q = q.view(B, T, self.num_attention_heads, self.head_dim).transpose(1, 2)
91
- k = k.view(B, T, self.num_attention_heads, self.head_dim).transpose(1, 2)
92
- v = v.view(B, T, self.num_attention_heads, self.head_dim).transpose(1, 2)
93
-
94
- if past_key_values is None:
95
-
96
- q = self.rotary_emb.rotate_queries_or_keys(q)
97
- k = self.rotary_emb.rotate_queries_or_keys(k)
98
-
99
- else:
100
-
101
- cache_position = past_key_values.get_seq_length(self.layer_idx)
102
-
103
- q = self.rotary_emb.rotate_queries_or_keys(q, offset=cache_position)
104
- k = self.rotary_emb.rotate_queries_or_keys(k, offset=cache_position)
105
-
106
- k, v = past_key_values.update(k, v, self.layer_idx)
107
-
108
- is_causal = self.use_causal_attention and attention_mask is None
109
- attn_output = F.scaled_dot_product_attention(q, k, v, attn_mask=attention_mask, scale=self.scale, is_causal=is_causal)
110
-
111
- attn_output = attn_output.transpose(1, 2).contiguous().view(B, T, self.hidden_size)
112
- out = self.o_proj(attn_output)
113
-
114
- return out
115
-
116
- # === Blocks ===
117
-
118
- class FeatureBlock(nn.Module):
119
-
120
- def __init__(
121
- self,
122
- config: FSTConfig,
123
- layer_idx: int = None
124
- ):
125
- super().__init__()
126
-
127
- self.attn = MHAttention(
128
- hidden_size=config.hidden_size,
129
- num_attention_heads=config.num_attention_heads,
130
- use_causal_attention=config.use_causal_attention,
131
- layer_idx=layer_idx,
132
- )
133
-
134
- self.mlp = MLP(
135
- config.hidden_size,
136
- config.intermediate_size
137
- )
138
-
139
- self.norm_attn = nn.LayerNorm(config.hidden_size)
140
- self.norm_mlp = nn.LayerNorm(config.hidden_size)
141
-
142
- self.resid_attn = Residual()
143
- self.resid_mlp = Residual()
144
-
145
- def forward(
146
- self,
147
- x: Tensor,
148
- attention_mask: Tensor | None = None,
149
- past_key_values: Cache | None = None
150
- ):
151
-
152
- attn_out = self.attn(self.norm_attn(x), attention_mask=attention_mask, past_key_values=past_key_values)
153
- x = self.resid_attn(x, attn_out)
154
-
155
- mlp_out = self.mlp(self.norm_mlp(x))
156
- x = self.resid_mlp(x, mlp_out)
157
-
158
- return x
159
-
160
- class PredictiveBlock(nn.Module):
161
-
162
- def __init__(
163
- self,
164
- config: FSTConfig,
165
- layer_idx: int = None
166
- ):
167
- super().__init__()
168
-
169
- self.attn = MHAttention(
170
- hidden_size=config.hidden_size,
171
- num_attention_heads=config.num_attention_heads,
172
- use_causal_attention=config.use_causal_attention,
173
- layer_idx=layer_idx,
174
- )
175
-
176
- self.mlp = MLP(
177
- config.hidden_size,
178
- config.intermediate_size
179
- )
180
-
181
- self.norm_attn_qk = nn.LayerNorm(config.hidden_size)
182
- self.norm_attn_v = nn.LayerNorm(config.hidden_size)
183
- self.norm_mlp = nn.LayerNorm(config.hidden_size)
184
-
185
- self.resid_attn = Residual()
186
- self.resid_mlp = Residual()
187
-
188
- def forward(
189
- self,
190
- phi: Tensor,
191
- f: Tensor,
192
- e: Tensor,
193
- attention_mask: Tensor | None = None,
194
- past_key_values: Cache | None = None
195
- ):
196
-
197
- qk = self.norm_attn_qk(phi)
198
- v = self.norm_attn_v(e)
199
-
200
- attn_out = self.attn(qk, qk, v, attention_mask=attention_mask, past_key_values=past_key_values)
201
- f = self.resid_attn(f, attn_out)
202
-
203
- mlp_out = self.mlp(self.norm_mlp(f))
204
- f = self.resid_mlp(f, mlp_out)
205
-
206
- return f
207
-
208
- # === Base Model ===
209
-
210
- class FSTPreTrainedModel(PreTrainedModel):
211
-
212
- config_class = FSTConfig
213
- base_model_prefix = "model"
214
- _no_split_modules = ["FSTBlock"]
215
- _skip_keys_device_placement = ["past_key_values"]
216
- _supports_flash_attn_2 = True
217
- _supports_cache_class = True
218
-
219
- # Initialization taken from Deepseek and Falcon
220
- def _init_weights(self, module):
221
- std = self.config.initializer_range
222
- if isinstance(module, nn.Linear):
223
- module.weight.data.normal_(mean=0.0, std=std)
224
- if module.bias is not None:
225
- module.bias.data.zero_()
226
- elif isinstance(module, nn.Embedding):
227
- module.weight.data.normal_(mean=0.0, std=std)
228
- if module.padding_idx is not None:
229
- module.weight.data[module.padding_idx].zero_()
230
-
231
- class FSTModel(FSTPreTrainedModel):
232
-
233
- def __init__(
234
- self,
235
- config: FSTConfig
236
- ):
237
- super().__init__(config)
238
-
239
- self.config = config
240
- self.embedding = nn.Embedding(config.vocab_size, config.hidden_size)
241
-
242
- self.feature_blocks = nn.ModuleList([FeatureBlock(config, layer_idx) for layer_idx in range(0, config.num_hidden_layers, 2)])
243
- self.predictive_blocks = nn.ModuleList([PredictiveBlock(config, layer_idx) for layer_idx in range(1, config.num_hidden_layers, 2)])
244
- self.norm_out = nn.LayerNorm(config.hidden_size)
245
-
246
- self.post_init()
247
-
248
- def _prepare_attention_mask(
249
- self,
250
- x: Tensor,
251
- attention_mask: Tensor | None = None,
252
- past_key_values: Cache | None = None,
253
- use_causal_attention: bool = True
254
- ):
255
-
256
- device = x.device
257
- B = x.shape[0]
258
- T = x.shape[1]
259
-
260
- T_past = past_key_values.get_seq_length() if past_key_values is not None else 0
261
- T_total = T + T_past
262
-
263
- if use_causal_attention:
264
- causal_mask = ~torch.triu(
265
- torch.ones((T, T_total), dtype=torch.bool, device=device),
266
- diagonal=(1 + T_past)
267
- ).unsqueeze(0).unsqueeze(0)
268
-
269
- if attention_mask is not None:
270
- attn_len = attention_mask.shape[-1]
271
-
272
- if attn_len < T_total:
273
- pad = torch.ones(B, T_past, device=device, dtype=attention_mask.dtype) # Fixed: ones instead of zeros
274
- attention_mask = torch.cat([pad, attention_mask], dim=-1)
275
- elif attn_len > T_total:
276
- attention_mask = attention_mask[:, -T_total:]
277
-
278
- expanded_mask = (attention_mask == 1).view(B, 1, 1, T_total)
279
-
280
- if use_causal_attention and attention_mask is not None:
281
- return causal_mask & expanded_mask
282
- elif use_causal_attention:
283
- return causal_mask
284
- elif attention_mask is not None: # Added: handle non-causal with custom mask
285
- return expanded_mask
286
- else:
287
- return torch.ones((1, 1, T, T_total), dtype=torch.bool, device=device)
288
-
289
- def forward(
290
- self,
291
- input_ids: Tensor | None = None,
292
- attention_mask: Tensor | None = None,
293
- inputs_embeds: Tensor | None = None,
294
- past_key_values = None,
295
- use_cache: bool | None = None,
296
- output_hidden_states: bool | None = None,
297
- return_dict: bool | None = None,
298
- **kwargs,
299
- ):
300
-
301
- use_cache = use_cache if use_cache is not None else self.config.use_cache
302
- output_hidden_states = output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
303
- return_dict = return_dict if return_dict is not None else self.config.use_return_dict
304
-
305
- assert not (input_ids is not None and inputs_embeds is not None), "You cannot specify both input_ids and inputs_embeds"
306
- assert not (input_ids is None and inputs_embeds is None), "You must specify either input_ids or inputs_embeds"
307
-
308
- e = self.embedding(input_ids) if input_ids is not None else inputs_embeds
309
-
310
- B, T, _ = e.shape
311
- device = e.device
312
- dtype = e.dtype
313
-
314
- if not use_cache:
315
- past_key_values=None
316
- elif past_key_values is None:
317
- past_key_values = DynamicCache()
318
-
319
- # Note that we must use an attention mask when caching- otherwise, SDPA uses is_casual and breaks
320
- if attention_mask is not None or past_key_values is not None:
321
- attention_mask = self._prepare_attention_mask(e, attention_mask=attention_mask, use_causal_attention=self.config.use_causal_attention, past_key_values=past_key_values)
322
-
323
- hidden_states = [] if output_hidden_states else None
324
-
325
- phi = e
326
- f = torch.zeros(B, T, self.config.hidden_size, dtype=dtype, device=device) # Initialize f as zero for purity, but f=e also works fine
327
-
328
- for feature_block, predictive_block in zip(self.feature_blocks, self.predictive_blocks):
329
-
330
- phi = feature_block(phi, attention_mask=attention_mask, past_key_values=past_key_values)
331
- f = predictive_block(phi, f, e, attention_mask=attention_mask, past_key_values=past_key_values)
332
-
333
- if output_hidden_states:
334
- hidden_states.append(phi)
335
- hidden_states.append(f)
336
-
337
- if hidden_states is not None:
338
- hidden_states = tuple(hidden_states)
339
-
340
- f = self.norm_out(f)
341
-
342
- if return_dict:
343
- return BaseModelOutputWithPast(
344
- last_hidden_state=f,
345
- past_key_values=past_key_values,
346
- hidden_states=hidden_states
347
- )
348
-
349
- return f, past_key_values, hidden_states
350
-
351
- # === Applied Models ===
352
-
353
- class FSTForCausalLM(GenerationMixin, FSTPreTrainedModel):
354
-
355
- accepts_loss_kwargs = False
356
-
357
- def __init__(
358
- self,
359
- config: FSTConfig
360
- ):
361
- super().__init__(config)
362
-
363
- self.model = FSTModel(config)
364
- self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
365
-
366
- if config.tie_word_embeddings:
367
- self.tie_weights()
368
- self._dynamic_tied_weights_keys = {"lm_head.weight": "model.embedding.weight"} # Avoids safetensor naming issues
369
-
370
- self.post_init()
371
-
372
- def get_input_embeddings(self):
373
- return self.model.embedding
374
-
375
- def set_input_embeddings(self, new_embeddings):
376
- self.model.embedding = new_embeddings
377
-
378
- def get_output_embeddings(self):
379
- return self.lm_head
380
-
381
- def set_output_embeddings(self, new_embeddings):
382
- self.lm_head = new_embeddings
383
-
384
- def tie_weights(self):
385
- self._tie_or_clone_weights(self.lm_head, self.get_input_embeddings())
386
-
387
- def forward(
388
- self,
389
- input_ids: Tensor | None = None,
390
- attention_mask: Tensor | None = None,
391
- past_key_values = None,
392
- inputs_embeds: Tensor | None = None,
393
- labels: Tensor | None = None,
394
- use_cache: bool | None = None,
395
- output_hidden_states: bool | None = None,
396
- return_dict: bool | None = None,
397
- **kwargs,
398
- ):
399
-
400
- if labels is not None:
401
- return_dict = True
402
- else:
403
- return_dict = return_dict if return_dict is not None else self.config.use_return_dict
404
-
405
- model_output = self.model(
406
- input_ids=input_ids,
407
- attention_mask=attention_mask,
408
- inputs_embeds=inputs_embeds,
409
- past_key_values=past_key_values,
410
- use_cache=use_cache,
411
- output_hidden_states=output_hidden_states
412
- )
413
-
414
- logits = self.lm_head(model_output[0])
415
-
416
- loss = None
417
- if labels is not None:
418
- shift_logits = logits[:, :-1, :].contiguous()
419
- shift_labels = labels[:, 1:].contiguous()
420
- loss = F.cross_entropy(
421
- shift_logits.view(-1, shift_logits.size(-1)),
422
- shift_labels.view(-1),
423
- ignore_index=self.config.pad_token_id if self.config.pad_token_id is not None else -100
424
- )
425
-
426
- if not return_dict:
427
- output = (logits,) + model_output[1:]
428
- return ((loss,) + output) if loss is not None else output
429
-
430
- return CausalLMOutputWithPast(
431
- loss=loss,
432
- logits=logits,
433
- past_key_values=model_output.past_key_values,
434
- hidden_states=model_output.hidden_states
435
- )
436
-
437
- def _prepare_inputs_for_generation(
438
- self,
439
- input_ids: Tensor,
440
- past_key_values: Cache | None = None,
441
- attention_mask: Tensor | None = None,
442
- **kwargs
443
- ):
444
- if past_key_values is not None:
445
- input_ids = input_ids[:, -1:]
446
-
447
- model_inputs = {"input_ids": input_ids, "past_key_values": past_key_values, "use_cache": True}
448
-
449
- if attention_mask is not None:
450
- model_inputs["attention_mask"] = attention_mask
451
-
452
- for key, value in kwargs.items():
453
- model_inputs[key] = value
454
-
455
- return model_inputs
456
-
457
- def _reorder_cache(self, past_key_values: Cache, beam_idx: Tensor):
458
- return past_key_values.reorder_cache(beam_idx)
459
-
460
- class FSTForMaskedLM(FSTPreTrainedModel):
461
-
462
- accepts_loss_kwargs = False
463
-
464
- def __init__(
465
- self,
466
- config: FSTConfig
467
- ):
468
- super().__init__(config)
469
-
470
- assert not config.use_causal_attention, "FSTForMaskedLM requires use_causal_attention=False"
471
- assert not config.use_cache, "FSTForMaskedLM requires use_cache=False (caching not supported for bidirectional models)"
472
-
473
- self.model = FSTModel(config)
474
- self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
475
-
476
- if config.tie_word_embeddings:
477
- self.tie_weights()
478
- self._dynamic_tied_weights_keys = {"lm_head.weight": "model.embedding.weight"} # Avoids safetensor naming issues
479
-
480
- self.post_init()
481
-
482
- def get_input_embeddings(self):
483
- return self.model.embedding
484
-
485
- def set_input_embeddings(self, new_embeddings):
486
- self.model.embedding = new_embeddings
487
-
488
- def get_output_embeddings(self):
489
- return self.lm_head
490
-
491
- def set_output_embeddings(self, new_embeddings):
492
- self.lm_head = new_embeddings
493
-
494
- def tie_weights(self):
495
- self._tie_or_clone_weights(self.lm_head, self.get_input_embeddings())
496
-
497
- def forward(
498
- self,
499
- input_ids: Tensor | None = None,
500
- attention_mask: Tensor | None = None,
501
- inputs_embeds: Tensor | None = None,
502
- labels: Tensor | None = None,
503
- output_hidden_states: bool | None = None,
504
- return_dict: bool | None = None,
505
- **kwargs,
506
- ):
507
-
508
- if labels is not None:
509
- return_dict = True
510
- else:
511
- return_dict = return_dict if return_dict is not None else self.config.use_return_dict
512
-
513
- model_output = self.model(
514
- input_ids=input_ids,
515
- attention_mask=attention_mask,
516
- inputs_embeds=inputs_embeds,
517
- past_key_values=None,
518
- use_cache=False,
519
- output_hidden_states=output_hidden_states
520
- )
521
-
522
- logits = self.lm_head(model_output[0])
523
-
524
- loss = None
525
- if labels is not None:
526
-
527
- loss = F.cross_entropy(
528
- logits.view(-1, logits.size(-1)),
529
- labels.view(-1),
530
- ignore_index=self.config.pad_token_id if self.config.pad_token_id is not None else -100
531
- )
532
-
533
- if not return_dict:
534
- output = (logits,) + model_output[1:]
535
- return ((loss,) + output) if loss is not None else output
536
-
537
- return MaskedLMOutput(
538
- loss=loss,
539
- logits=logits,
540
- hidden_states=model_output.hidden_states
541
- )