Skip to content

Honor shift_labels in decoder-only LLM/VLM losses - #48493

Merged
qgallouedec merged 9 commits into
mainfrom
fix-shift-labels-decoder-only
Sep 8, 2026
Merged

qgallouedec merged 9 commits into
mainfrom
fix-shift-labels-decoder-only

Conversation

@qgallouedec

@qgallouedec qgallouedec commented Sep 2, 2026 •

Copy link
Copy Markdown
Member

CPU CI GPU run-slow

Fixes #48491. Three ways a head was dropping the pre-shifted targets:

  • self.loss_function(...) called without **kwargs, so shift_labels never reached ForCausalLMLoss (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)
  • labels shifted by hand instead of going through self.loss_function (Gemma3, Llama4, Mamba, FalconMamba, xLSTM, ModernBERT-decoder, BLIP-2, BLIP)
  • Moshi shifted and then re-shifted inside ForCausalLMLoss, GIT raised TypeError: 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_labels to ModelTesterMixin. The oracle is differential, so it assumes nothing about how a head builds its loss: with shift_labels passed, the loss must move when shift_labels changes and stay put when only labels changes.
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_HEADS and skipped.

Also drops accepts_loss_kwargs = False on Gemma3ForConditionalGeneration: it no longer filters logits/labels, so num_items_in_batch normalization now applies (root cause of #40564).

🤖 mlinter review state

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@HuggingFaceDocBuilderDev

Copy link
Copy Markdown

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.

qgallouedec and others added 3 commits September 3, 2026 01:00
`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 zucchini-nlp left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Comment thread tests/test_modeling_common.py Resolved
qgallouedec and others added 3 commits September 7, 2026 16:42
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 zucchini-nlp left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).
@github-actions

github-actions Bot commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

[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

@qgallouedec
qgallouedec added this pull request to the merge queue Sep 8, 2026
@github-actions

github-actions Bot commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

CI recap

Dashboard: View test results in Grafana
Latest run: 34254878785:2
Result: success | Jobs: 1 | Tests: 16,209 | Failures: 0 | Duration: 23m 14s

Merged via the queue into main with commit bd05a4b Sep 8, 2026
113 checks passed
@qgallouedec
qgallouedec deleted the fix-shift-labels-decoder-only branch September 8, 2026 18:09
sbucaille pushed a commit to sbucaille/transformers that referenced this pull request Sep 16, 2026
* 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).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Qwen3-VL and Gemma3 (VLM) ignore shift_labels

3 participants