Skip to content

[pull] main from huggingface:main - #7

Merged
pull[bot] merged 5 commits into
ZikrieHR:mainfrom
huggingface:main
Sep 8, 2026
Merged

pull[bot] merged 5 commits into
ZikrieHR:mainfrom
huggingface:main

Conversation

@pull

@pull pull Bot commented Sep 8, 2026 •

Copy link
Copy Markdown

See Commits and Changes for more details.


Created by pull[bot] (v2.0.0-alpha.4)

Can you help keep this open source service alive? 💖 Please sponsor : )

qgallouedec and others added 5 commits September 8, 2026 17:25
…ributions (#48205)

* Fix NaN gradients in expert-parallel training: mask uninitialized grouped_mm rows

Under EP, sentinel token-expert slots sit beyond offsets[-1] and torch._grouped_mm
leaves those output rows (fwd output and bwd d_input) uninitialized. The forward
relied on a single post-mask plus a single pre-mask, letting NaN/Inf from
uninitialized memory transit the activation and down-projection backward. The
gate product's backward (act_fn(gate) * up) turns 0 x Inf into NaN
(torch.autograd anomaly mode names this exact Mul), and it escapes into finite
gradients: full fine-tuning of any EP-sharded MoE produced nan grad_norm on the
second step (the first step survives only because freshly-allocated CUDA memory
happens to be zeroed) and the loss collapsed to 0.

Mask the sentinel-tail rows after each grouped GEMM instead. Full fine-tuning of
OLMoE-1B-7B under ep=4 now matches the single-GPU loss trajectory.

* Fix wrong gradients for all non-expert parameters in expert-parallel training

Under EP the router hook zeroes the routing scores of non-local experts, so in
backward each rank's score gradient covers only the slots of its local experts,
and nothing sums the per-rank partial gradients: the gate weights and, through
the gate's input, every parameter upstream of each MoE block receive gradients
missing the contributions that flow through remote experts. The existing
_AllReduceBackward on the experts' hidden input covers the dispatch branch, and
the top_k_weights branch is explicitly skipped when is_expert_parallel -- but
under EP it is exactly as partial as under TP-MoE.

Measured against a single-GPU reference (OLMoE-1B-7B, one batch, fp32 so
rounding noise vanishes): before the fix, 3/179 parameters agree (relative
max-abs errors 0.3-2.5 on attention, norms, embeddings and router gates,
10-100x above the run-to-run noise floor; only the last layer's experts and the
final norm -- the parameters backward reaches before crossing an expert block --
are correct). After the fix: 179/179 agree, max relative error 2.7e-5.

Fix: allreduce-sum the score gradient in the EP router hook, before the
non-local mask (each slot has exactly one owning rank, so the sum is exact).

* Trim comments

* Gate the router-score backward all-reduce on grad mode; drop the post-mask superseded by the per-mm masks

* Fix expert parallelism through Trainer (#48208)
…ester (#48624)

* [KimiLinear] Fix test_cpu_offload: set num_local_experts=4 in model tester

The KimiLinearConfig defaults to num_local_experts=256, but the model tester
only set self.n_routed_experts=8 which is a wrong attribute name and never
mapped to the config. As a result, the test model used 256 experts, making
layers.1 (the MoE layer) ~94% of total model size. With _no_split_modules
constraining layers.1 to be atomic, infer_auto_device_map could never fit it
within any reasonable GPU budget fraction, placing everything on CPU (single
device). dispatch_model was then never called, so hf_device_map was never set,
causing AttributeError in test_cpu_offload.

Fix: set self.num_local_experts=4 so the config receives a small expert count,
making the MoE layer a proportionate fraction of the model and allowing
accelerate to split it across GPU and CPU as intended.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>

* [KimiLinear] Add comment explaining why num_local_experts=4

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>

* [KimiLinear] Remove dead n_routed_experts attribute from model tester

KimiLinearConfig has no n_routed_experts field — it uses num_local_experts.
The attribute was silently ignored and had no effect on the config.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>

---------

Co-authored-by: ydshieh <[email protected]>
* 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).
* fix

* oops

* simplify

* my own nits

* style
@pull pull Bot locked and limited conversation to collaborators Sep 8, 2026
@pull pull Bot added the ⤵️ pull label Sep 8, 2026
@pull
pull Bot merged commit cbc1651 into ZikrieHR:main Sep 8, 2026
1 of 2 checks passed
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants