maidacundo commited on
Commit
3edee97
·
verified ·
1 Parent(s): 8fed5c0

fix: shift logits/labels for proper causal LM loss (was predicting current token instead of next token)

Browse files
Files changed (1) hide show
  1. open_mythos_hf/modeling.py +5 -3
open_mythos_hf/modeling.py CHANGED
@@ -671,12 +671,14 @@ class OpenMythosForCausalLM(OpenMythosPreTrainedModel, GenerationMixin):
671
  # 6. LM Head
672
  logits = self.lm_head(x).float()
673
 
674
- # 7. Loss
675
  loss = None
676
  if labels is not None:
 
 
677
  loss = F.cross_entropy(
678
- logits.view(-1, logits.shape[-1]),
679
- labels.view(-1),
680
  ignore_index=-100,
681
  )
682
 
 
671
  # 6. LM Head
672
  logits = self.lm_head(x).float()
673
 
674
+ # 7. Loss (shifted for causal LM: logits[i] predicts token[i+1])
675
  loss = None
676
  if labels is not None:
677
+ shift_logits = logits[..., :-1, :].contiguous()
678
+ shift_labels = labels[..., 1:].contiguous()
679
  loss = F.cross_entropy(
680
+ shift_logits.view(-1, shift_logits.shape[-1]),
681
+ shift_labels.view(-1),
682
  ignore_index=-100,
683
  )
684