ereniko commited on
Commit
030a3c5
·
verified ·
1 Parent(s): 230fc1a

Upload Ivme-Coder-v1 (Otter 1): safetensors + custom modeling code

Browse files
Files changed (2) hide show
  1. model/model.py +17 -5
  2. modeling_ivme_coder.py +17 -5
model/model.py CHANGED
@@ -148,10 +148,17 @@ class IvmeCoderV1ForCausalLM(PreTrainedModel, GenerationMixin):
148
  self.head = nn.Linear(config.d_model, config.vocab_size, bias=False)
149
  self.head.weight = self.tok_emb.weight # tied embeddings
150
 
151
- d_head = config.d_model // config.n_head
152
- cos, sin = precompute_rope(d_head, config.context_len, theta=config.rope_theta)
153
- self.register_buffer("rope_cos", cos, persistent=False)
154
- self.register_buffer("rope_sin", sin, persistent=False)
 
 
 
 
 
 
 
155
 
156
  self.post_init()
157
 
@@ -179,9 +186,14 @@ class IvmeCoderV1ForCausalLM(PreTrainedModel, GenerationMixin):
179
  # generate()'s kwarg validation, but intentionally unused: this model only
180
  # supports left-padding for batched generation (see model card), and single-
181
  # sequence causal generation (the common case) needs no mask at all.
 
 
 
 
 
182
  x = self.tok_emb(input_ids)
183
  for block in self.blocks:
184
- x = block(x, self.rope_cos, self.rope_sin)
185
  x = self.ln_f(x)
186
  logits = self.head(x)
187
  loss = None
 
148
  self.head = nn.Linear(config.d_model, config.vocab_size, bias=False)
149
  self.head.weight = self.tok_emb.weight # tied embeddings
150
 
151
+ # NOTE: RoPE cos/sin tables are intentionally NOT stored as a persistent=False
152
+ # buffer here. transformers v5's from_pretrained() has a known bug where
153
+ # persistent=False buffers get overwritten with uninitialized/garbage memory
154
+ # after loading (https://github.com/huggingface/transformers/issues/44534),
155
+ # even though they're computed correctly at __init__ time. That garbage then
156
+ # silently produces huge (but finite) values through the RoPE rotation, which
157
+ # overflow to NaN inside scaled_dot_product_attention. Recomputing the tables
158
+ # fresh on every forward() call sidesteps this entirely - it's cheap (a cos/sin
159
+ # over d_head/2 * context_len elements) relative to the rest of the forward pass.
160
+ self.d_head = config.d_model // config.n_head
161
+ self.rope_theta = config.rope_theta
162
 
163
  self.post_init()
164
 
 
186
  # generate()'s kwarg validation, but intentionally unused: this model only
187
  # supports left-padding for batched generation (see model card), and single-
188
  # sequence causal generation (the common case) needs no mask at all.
189
+ # Recompute RoPE tables fresh each call - see note in __init__ for why this
190
+ # isn't cached in a buffer.
191
+ rope_cos, rope_sin = precompute_rope(
192
+ self.d_head, self.context_len, theta=self.rope_theta, device=input_ids.device
193
+ )
194
  x = self.tok_emb(input_ids)
195
  for block in self.blocks:
196
+ x = block(x, rope_cos, rope_sin)
197
  x = self.ln_f(x)
198
  logits = self.head(x)
199
  loss = None
modeling_ivme_coder.py CHANGED
@@ -148,10 +148,17 @@ class IvmeCoderV1ForCausalLM(PreTrainedModel, GenerationMixin):
148
  self.head = nn.Linear(config.d_model, config.vocab_size, bias=False)
149
  self.head.weight = self.tok_emb.weight # tied embeddings
150
 
151
- d_head = config.d_model // config.n_head
152
- cos, sin = precompute_rope(d_head, config.context_len, theta=config.rope_theta)
153
- self.register_buffer("rope_cos", cos, persistent=False)
154
- self.register_buffer("rope_sin", sin, persistent=False)
 
 
 
 
 
 
 
155
 
156
  self.post_init()
157
 
@@ -179,9 +186,14 @@ class IvmeCoderV1ForCausalLM(PreTrainedModel, GenerationMixin):
179
  # generate()'s kwarg validation, but intentionally unused: this model only
180
  # supports left-padding for batched generation (see model card), and single-
181
  # sequence causal generation (the common case) needs no mask at all.
 
 
 
 
 
182
  x = self.tok_emb(input_ids)
183
  for block in self.blocks:
184
- x = block(x, self.rope_cos, self.rope_sin)
185
  x = self.ln_f(x)
186
  logits = self.head(x)
187
  loss = None
 
148
  self.head = nn.Linear(config.d_model, config.vocab_size, bias=False)
149
  self.head.weight = self.tok_emb.weight # tied embeddings
150
 
151
+ # NOTE: RoPE cos/sin tables are intentionally NOT stored as a persistent=False
152
+ # buffer here. transformers v5's from_pretrained() has a known bug where
153
+ # persistent=False buffers get overwritten with uninitialized/garbage memory
154
+ # after loading (https://github.com/huggingface/transformers/issues/44534),
155
+ # even though they're computed correctly at __init__ time. That garbage then
156
+ # silently produces huge (but finite) values through the RoPE rotation, which
157
+ # overflow to NaN inside scaled_dot_product_attention. Recomputing the tables
158
+ # fresh on every forward() call sidesteps this entirely - it's cheap (a cos/sin
159
+ # over d_head/2 * context_len elements) relative to the rest of the forward pass.
160
+ self.d_head = config.d_model // config.n_head
161
+ self.rope_theta = config.rope_theta
162
 
163
  self.post_init()
164
 
 
186
  # generate()'s kwarg validation, but intentionally unused: this model only
187
  # supports left-padding for batched generation (see model card), and single-
188
  # sequence causal generation (the common case) needs no mask at all.
189
+ # Recompute RoPE tables fresh each call - see note in __init__ for why this
190
+ # isn't cached in a buffer.
191
+ rope_cos, rope_sin = precompute_rope(
192
+ self.d_head, self.context_len, theta=self.rope_theta, device=input_ids.device
193
+ )
194
  x = self.tok_emb(input_ids)
195
  for block in self.blocks:
196
+ x = block(x, rope_cos, rope_sin)
197
  x = self.ln_f(x)
198
  logits = self.head(x)
199
  loss = None