Repository navigation
[pull] main from huggingface:main - #7
Merged
Merged
Conversation
…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).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 : )