ereniko commited on
Commit
7438e98
·
verified ·
1 Parent(s): d7d2340

Upload modeling_ivme_conversate.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. modeling_ivme_conversate.py +23 -0
modeling_ivme_conversate.py CHANGED
@@ -180,6 +180,17 @@ class IvmeConversateModel(PreTrainedModel):
180
  nn.init.normal_(module.weight, mean=0.0, std=0.02)
181
 
182
  def forward(self, input_ids, **kwargs):
 
 
 
 
 
 
 
 
 
 
 
183
  x = self.tok_embed(input_ids)
184
  cos, sin = precompute_rope(self._head_dim, self._context_len,
185
  theta=self._rope_theta, device=x.device)
@@ -231,6 +242,18 @@ class IvmeConversateForCausalLM(PreTrainedModel, GenerationMixin):
231
  # attention_mask accepted for interface compatibility but not
232
  # used - this model is trained/used causal-only with no padding
233
  # within a sequence (see ShardedTokenDataset / infer scripts).
 
 
 
 
 
 
 
 
 
 
 
 
234
  hidden = self.model(input_ids)
235
  logits = self.lm_head(hidden)
236
 
 
180
  nn.init.normal_(module.weight, mean=0.0, std=0.02)
181
 
182
  def forward(self, input_ids, **kwargs):
183
+ # Defensive truncation: keep only the most recent context_len
184
+ # tokens. This is a second safety layer beyond exposing
185
+ # max_position_embeddings in the config (see
186
+ # IvmeConversateConfig) - if any caller still manages to pass a
187
+ # longer sequence, truncate cleanly to the trailing window
188
+ # rather than crash inside RoPE with a cryptic tensor-shape
189
+ # mismatch (confirmed reproducible: a 2048-token input against
190
+ # this model's 1024 context previously crashed deep inside
191
+ # apply_rope with no indication of the real cause).
192
+ if input_ids.shape[1] > self._context_len:
193
+ input_ids = input_ids[:, -self._context_len:]
194
  x = self.tok_embed(input_ids)
195
  cos, sin = precompute_rope(self._head_dim, self._context_len,
196
  theta=self._rope_theta, device=x.device)
 
242
  # attention_mask accepted for interface compatibility but not
243
  # used - this model is trained/used causal-only with no padding
244
  # within a sequence (see ShardedTokenDataset / infer scripts).
245
+ #
246
+ # If input_ids exceeds context_len, self.model() truncates it
247
+ # internally (see IvmeConversateModel.forward). labels must be
248
+ # truncated identically here, BEFORE the loss is computed -
249
+ # otherwise a caller passing both an over-length input_ids and
250
+ # matching labels (e.g. a perplexity/loglikelihood task) would
251
+ # hit a shape mismatch between logits and labels, or worse, a
252
+ # silent misalignment if the shapes happened to coincide.
253
+ context_len = self.config.max_position_embeddings
254
+ if input_ids.shape[1] > context_len and labels is not None:
255
+ labels = labels[:, -context_len:]
256
+
257
  hidden = self.model(input_ids)
258
  logits = self.lm_head(hidden)
259