summerMC commited on
Commit
2bb7fc4
·
verified ·
1 Parent(s): a412afb

v19: Fix tied weights and lm_head loading

Browse files
config.json CHANGED
@@ -1,8 +1,4 @@
1
  {
2
- "auto_map": {
3
- "AutoConfig": "configuration_trm_text_ism_v6.TRMTextISMConfig",
4
- "AutoModelForCausalLM": "modeling_trm_text_ism_v6.TRMTextISMForCausalLM"
5
- },
6
  "bos_token_id": null,
7
  "dim": 768,
8
  "dropout": 0.0,
 
1
  {
 
 
 
 
2
  "bos_token_id": null,
3
  "dim": 768,
4
  "dropout": 0.0,
configuration_trm_text_ism_v6.py CHANGED
@@ -2,9 +2,8 @@
2
  from transformers import PretrainedConfig
3
  class TRMTextISMConfig(PretrainedConfig):
4
  model_type = "trm_text_ism"
5
- def __init__(self, vocab_size=50257, max_seq_len=512, dim=768, n_heads=12, head_dim=64, recurrence_steps=4, mlp_ratio=2.6666666667, mlp_hidden_size=None, dropout=0.0, gate_style="stable", gate_init=-1.5, residual_scale=0.5, tie_word_embeddings=True, use_cache=False, **kwargs):
6
- super().__init__(tie_word_embeddings=tie_word_embeddings, use_cache=use_cache, **kwargs)
7
  self.vocab_size, self.max_seq_len, self.dim, self.n_heads, self.head_dim = vocab_size, max_seq_len, dim, n_heads, head_dim
8
  self.recurrence_steps, self.mlp_ratio, self.mlp_hidden_size, self.dropout = recurrence_steps, mlp_ratio, mlp_hidden_size, dropout
9
  self.gate_style, self.gate_init, self.residual_scale = gate_style, gate_init, residual_scale
10
- self.hidden_size, self.num_attention_heads, self.num_hidden_layers = dim, n_heads, 1
 
2
  from transformers import PretrainedConfig
3
  class TRMTextISMConfig(PretrainedConfig):
4
  model_type = "trm_text_ism"
5
+ def __init__(self, vocab_size=50257, max_seq_len=512, dim=768, n_heads=12, head_dim=64, recurrence_steps=4, mlp_ratio=2.6666666667, mlp_hidden_size=None, dropout=0.0, gate_style="stable", gate_init=-1.5, residual_scale=0.5, tie_word_embeddings=True, **kwargs):
6
+ super().__init__(tie_word_embeddings=tie_word_embeddings, **kwargs)
7
  self.vocab_size, self.max_seq_len, self.dim, self.n_heads, self.head_dim = vocab_size, max_seq_len, dim, n_heads, head_dim
8
  self.recurrence_steps, self.mlp_ratio, self.mlp_hidden_size, self.dropout = recurrence_steps, mlp_ratio, mlp_hidden_size, dropout
9
  self.gate_style, self.gate_init, self.residual_scale = gate_style, gate_init, residual_scale
 
modeling_trm_text_ism_v6.py CHANGED
@@ -10,8 +10,11 @@ from .configuration_trm_text_ism_v6 import TRMTextISMConfig
10
  def apply_rope(x, cos, sin):
11
  B, H, S, D = x.shape
12
  half = D // 2
 
 
 
13
  x1, x2 = x[..., :half], x[..., half:]
14
- return torch.cat([x1 * cos - x2 * sin, x2 * cos + x1 * sin], dim=-1)
15
 
16
  class SwiGLUMLP(nn.Module):
17
  def __init__(self, config):
@@ -72,12 +75,11 @@ class TRMTextISMForCausalLM(PreTrainedModel, GenerationMixin):
72
  def forward(self, input_ids, attention_mask=None, response_starts=None, **kwargs):
73
  B, S = input_ids.shape
74
  x = self.token_emb(input_ids)
75
- cos, sin = self.rope_cos[:, :, :S, :], self.rope_sin[:, :, :S, :]
76
  mask = torch.tril(torch.ones(S, S, device=x.device)).bool().unsqueeze(0).expand(B, -1, -1)
77
  if response_starts is not None:
78
  for b in range(B):
79
  rs = response_starts[b]
80
  mask[b, :rs, :rs] = True
81
  for _ in range(self.config.recurrence_steps):
82
- x = self.block(x, mask, cos, sin)
83
  return CausalLMOutputWithPast(logits=self.lm_head(self.norm(x)))
 
10
  def apply_rope(x, cos, sin):
11
  B, H, S, D = x.shape
12
  half = D // 2
13
+ # Slice to current seq length and ensure dtypes match x
14
+ c = cos[:, :, :S, :].to(x.dtype)
15
+ s = sin[:, :, :S, :].to(x.dtype)
16
  x1, x2 = x[..., :half], x[..., half:]
17
+ return torch.cat([x1 * c - x2 * s, x2 * c + x1 * s], dim=-1)
18
 
19
  class SwiGLUMLP(nn.Module):
20
  def __init__(self, config):
 
75
  def forward(self, input_ids, attention_mask=None, response_starts=None, **kwargs):
76
  B, S = input_ids.shape
77
  x = self.token_emb(input_ids)
 
78
  mask = torch.tril(torch.ones(S, S, device=x.device)).bool().unsqueeze(0).expand(B, -1, -1)
79
  if response_starts is not None:
80
  for b in range(B):
81
  rs = response_starts[b]
82
  mask[b, :rs, :rs] = True
83
  for _ in range(self.config.recurrence_steps):
84
+ x = self.block(x, mask, self.rope_cos, self.rope_sin)
85
  return CausalLMOutputWithPast(logits=self.lm_head(self.norm(x)))