summerMC commited on
Commit
9d0d4bd
·
verified ·
1 Parent(s): dc244cf

Upload modeling_trm_text_ism.py

Browse files
Files changed (1) hide show
  1. modeling_trm_text_ism.py +19 -5
modeling_trm_text_ism.py CHANGED
@@ -1,4 +1,3 @@
1
-
2
  import torch
3
  import torch.nn as nn
4
  import torch.nn.functional as F
@@ -64,17 +63,32 @@ class TRMTextISMForCausalLM(PreTrainedModel, GenerationMixin):
64
  self.register_buffer("rope_cos", f.cos().view(1, 1, config.max_seq_len, -1))
65
  self.register_buffer("rope_sin", f.sin().view(1, 1, config.max_seq_len, -1))
66
  self.post_init()
67
- def get_input_embeddings(self): return self.token_emb
68
- def get_output_embeddings(self): return self.lm_head
 
 
 
 
 
 
 
 
 
 
 
69
  def tie_weights(self, *args, **kwargs):
70
- if hasattr(self, 'lm_head'): self.lm_head.weight = self.token_emb.weight
 
 
71
  def prepare_inputs_for_generation(self, input_ids, attention_mask=None, **kwargs):
72
  return {"input_ids": input_ids, "attention_mask": attention_mask, "use_cache": False}
 
73
  def forward(self, input_ids, attention_mask=None, **kwargs):
74
  B, S = input_ids.shape
75
  x = self.token_emb(input_ids)
76
  m = torch.tril(torch.ones(S, S, device=input_ids.device)).bool().unsqueeze(0).expand(B, -1, -1)
77
- if attention_mask is not None: m = m & attention_mask[:, None, :].bool()
 
78
  c, s = self.rope_cos, self.rope_sin
79
  for _ in range(self.config.recurrence_steps):
80
  x = self.block(x, m, c, s)
 
 
1
  import torch
2
  import torch.nn as nn
3
  import torch.nn.functional as F
 
63
  self.register_buffer("rope_cos", f.cos().view(1, 1, config.max_seq_len, -1))
64
  self.register_buffer("rope_sin", f.sin().view(1, 1, config.max_seq_len, -1))
65
  self.post_init()
66
+
67
+ def get_input_embeddings(self):
68
+ return self.token_emb
69
+
70
+ def set_input_embeddings(self, value):
71
+ self.token_emb = value
72
+
73
+ def get_output_embeddings(self):
74
+ return self.lm_head
75
+
76
+ def set_output_embeddings(self, value):
77
+ self.lm_head = value
78
+
79
  def tie_weights(self, *args, **kwargs):
80
+ if hasattr(self, 'lm_head'):
81
+ self.lm_head.weight = self.token_emb.weight
82
+
83
  def prepare_inputs_for_generation(self, input_ids, attention_mask=None, **kwargs):
84
  return {"input_ids": input_ids, "attention_mask": attention_mask, "use_cache": False}
85
+
86
  def forward(self, input_ids, attention_mask=None, **kwargs):
87
  B, S = input_ids.shape
88
  x = self.token_emb(input_ids)
89
  m = torch.tril(torch.ones(S, S, device=input_ids.device)).bool().unsqueeze(0).expand(B, -1, -1)
90
+ if attention_mask is not None:
91
+ m = m & attention_mask[:, None, :].bool()
92
  c, s = self.rope_cos, self.rope_sin
93
  for _ in range(self.config.recurrence_steps):
94
  x = self.block(x, m, c, s)