Repository navigation
Honor shift_labels in decoder-only LLM/VLM losses - #48493
Conversation
There was a problem hiding this comment.
Model linter — first pass
transformers-mlinter found 1 item(s) in the model files this PR touches. These are structural conventions a maintainer would otherwise flag by hand.
This is automated and advisory — it does not block merging.
| rule | count | what it checks |
|---|---|---|
TRF053 |
1 | No manual label shifting in modeling code; self.loss_function owns it. |
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
`modular_qwen4_exp.py` inherits from `qwen3_5` and `qwen3_5_moe`, so passing `**kwargs` to the loss there left its generated file stale. blip trips TRF053 because this PR renames the manual shift's target from `labels` to `shift_labels`, which is the pattern the rule matches. The shift itself is unchanged and the model keeps its own loss: `CrossEntropyLoss` with `reduction` and `label_smoothing`, plus a `reduction="none"` branch that sums per sample. Moving it to `self.loss_function` is the per-model work the rule's own comment defers.
zucchini-nlp
left a comment
There was a problem hiding this comment.
Nice PR, these should have been clean-up at some point
Overall lgtm, my only question is re if labels is not None or shift_labels is not None which makes sense on itself. Though it diverges from how most models compute loss only if labels are present
So do we need to support passing only shift_labels for TRL and if yes, let's split this out for its own PR that touches all models?
Every producer of `shift_labels` wants the model gated on `labels`. The Trainer's context-parallel path keeps `labels` in the inputs next to `shift_labels`, so the model gets both and the loss picks `shift_labels`. The DeepSpeed Ulysses adapter pops `labels` so that the model computes no loss, since the Trainer computes it from the gathered logits. blip and git were the only two models testing `shift_labels` as well, and on the DeepSpeed path that computed a loss the adapter had removed `labels` to prevent. They now match every other model.
zucchini-nlp
left a comment
There was a problem hiding this comment.
Thanks for iterating, lets' fix a failing test for Cohere and we can merge
The model was built without seeding, so its init followed whatever RNG state the shard's test order left behind, and every target position held the same id, which averages the per-position loss differences out. For heads that damp their logits the reported loss then came back bit-identical when shift_labels changed: Cohere failed on run 34166425798 and StableLm on run 34254878785, both spuriously. Seed the init, draw the target id per position, and compare the losses exactly. Over 30 init draws the loss delta stays above 2.4e-5 for Cohere and 7.5e-5 for StableLm, against allclose's 4.6e-5 tolerance at a loss near log(vocab_size).
|
[For maintainers] Suggested jobs to run (before merge) run-slow: blip, blip_2, bloom, cosmos3_omni, ernie4_5_vl_moe, falcon_mamba, gemma3, git, glm46v, glm4v, glm4v_moe, glm_image, glm_ocr, llama4, mamba, minicpmv4_6 |
CI recapDashboard: View test results in Grafana |
* Honor `shift_labels` in decoder-only LLM/VLM losses * Regenerate qwen4_exp and allowlist blip for TRF053 `modular_qwen4_exp.py` inherits from `qwen3_5` and `qwen3_5_moe`, so passing `**kwargs` to the loss there left its generated file stale. blip trips TRF053 because this PR renames the manual shift's target from `labels` to `shift_labels`, which is the pattern the rule matches. The shift itself is unchanged and the model keeps its own loss: `CrossEntropyLoss` with `reduction` and `label_smoothing`, plus a `reduction="none"` branch that sums per sample. Moving it to `self.loss_function` is the per-model work the rule's own comment defers. * Gate the blip and git losses on labels alone Every producer of `shift_labels` wants the model gated on `labels`. The Trainer's context-parallel path keeps `labels` in the inputs next to `shift_labels`, so the model gets both and the loss picks `shift_labels`. The DeepSpeed Ulysses adapter pops `labels` so that the model computes no loss, since the Trainer computes it from the gathered logits. blip and git were the only two models testing `shift_labels` as well, and on the DeepSpeed path that computed a loss the adapter had removed `labels` to prevent. They now match every other model. * Keep the shift_labels loss test off the init draw The model was built without seeding, so its init followed whatever RNG state the shard's test order left behind, and every target position held the same id, which averages the per-position loss differences out. For heads that damp their logits the reported loss then came back bit-identical when shift_labels changed: Cohere failed on run 34166425798 and StableLm on run 34254878785, both spuriously. Seed the init, draw the target id per position, and compare the losses exactly. Over 30 init draws the loss delta stays above 2.4e-5 for Cohere and 7.5e-5 for StableLm, against allclose's 4.6e-5 tolerance at a loss near log(vocab_size).
Fixes #48491. Three ways a head was dropping the pre-shifted targets:
self.loss_function(...)called without**kwargs, soshift_labelsnever reachedForCausalLMLoss(Qwen3-VL(+MoE), Qwen3.5(+MoE), GLM-4V(+MoE)/4.6V/OCR/Image, VipLlava, Ernie4.5-VL-MoE, MiniMax-M3-VL, MiniCPM-V4.6, Cosmos3-Omni, Qwen2.5-Omni and Qwen3-Omni thinkers, Phi4-multimodal, Bloom, XGLM)labelsshifted by hand instead of going throughself.loss_function(Gemma3, Llama4, Mamba, FalconMamba, xLSTM, ModernBERT-decoder, BLIP-2, BLIP)ForCausalLMLoss, GIT raisedTypeError: got multiple values for keyword argument 'shift_labels'This breaks sequence and context parallel training (accelerate CP, Ulysses SP, DeepSpeed ALST), where the shift has to happen on the full sequence before it is sharded: each shard's last token silently gets no target.
Adds
test_causal_lm_loss_honors_shift_labelstoModelTesterMixin. The oracle is differential, so it assumes nothing about how a head builds its loss: withshift_labelspassed, the loss must move whenshift_labelschanges and stay put when onlylabelschanges.Heads that score each position against its own target (standalone encoder-decoder decoders, permutation LM heads, per-codebook audio LMs) are listed in
UNSHIFTED_LM_HEADSand skipped.Also drops
accepts_loss_kwargs = FalseonGemma3ForConditionalGeneration: it no longer filters logits/labels, sonum_items_in_batchnormalization now applies (root cause of #40564).🤖 mlinter review state
27ea4c31cbef0855shift_labelsin decoder-only LLM/VLM losses #48493 (review)