Repository navigation
Pipeline parallel: weight the MoE router aux loss per accumulation step as the model forward does - #16708
Conversation
…ep as the model forward does Under UNSLOTH_PP_SIZE the last stage added router_aux_loss_coef x the load-balancing loss of every Trainer step in full. On one GPU the CausalLM forward Unsloth Zoo rewrites (fused_losses/aux_loss.py) wraps that term in unsloth_ga_scale_aux_loss: the micro-batch's aux times its token share of the accumulation window. With gradient accumulation the pipeline trained a ~GA x larger aux term: tiny Qwen3-MoE, coef 0.5, step-1 loss 9.125 vs 8.052 at GA 2 (13%), 42% off at GA 4, on a cell the capability registry lists as supported. The last stage now passes its aux term through the same scaler, read from the forward that runs (its code names unsloth_ga_scale_aux_loss), with that micro-batch's labels / mask and the window's token count; a forward without the rewrite keeps the term in full, as one process does. Parity at GA 1 / 2 / 4, aux on and off, on transformers 4.57.6 + TRL 0.22.2, 5.17 + 1.15 and 5.22 + 1.17.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
@codex review |
|
Codex Review: Didn't find any major issues. Already looking forward to the next diff. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
…-batch, surface worker errors torch < 2.13 refuses fewer micro-batches than stages in every single-stage schedule, GPipe included (PipelineScheduleSingle.__init__, schedules.py:579 on 2.10.0; 2.13 keeps the check in Schedule1F1B only), and the pipeline takes the router aux loss over one micro-batch only, so on torch 2.10 the cell cannot run: skip when every rank returns that refusal. _rows_per_step / _same_rows now assert on a worker's error dict (error + trace) instead of raising KeyError: 'ga'.
…d forward FastModel's compiled module gives the CausalLM a forward that returns its module-level <Class>_forward(self, ...), and only that function calls unsloth_ga_scale_aux_loss. The detection read only the class forward and its __wrapped__ chain, so under that forward the last stage still added the aux term in full while one process weighted it by token share (GA 2: step-1 loss 14.07 vs 13.00). Follow one level of module-level *_forward callees, keep walking the __wrapped__ chain when a layer names the scaler without defining it, and test the compiled forward beside the import-time one.
|
@codex review |
|
Codex Review: Didn't find any major issues. Chef's kiss. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Summary
Under
UNSLOTH_PP_SIZE=2, a MoE model with the router load-balancing loss on trained a different objective than one GPU whenevergradient_accumulation_steps > 1. The capability registry lists this cell as supported (MoE qwen3_moe x lora+experts x aux on x pp), and nothing warned.Tiny Qwen3-MoE, LoRA on the experts, router aux coefficient 0.5, the same SFTTrainer script on one process vs 2 gloo ranks over the same rows:
On one GPU, the CausalLM forward that Unsloth Zoo rewrites (
fused_losses/aux_loss.py) wrapsrouter_aux_loss_coef * aux_lossinunsloth_ga_scale_aux_loss. Each micro-batch's aux term is weighted by that micro-batch's token share of the accumulation window (28/52 and 24/52 in the run above). The pipeline's last stage added the full term on every Trainer step, so with GA steps the aux term weighed roughly GA times as much as on one GPU.Change
unsloth/models/pp_native.py, last stage only:_forward_aux_scaler(owner)reads the scaler from the forward that actually runs. That is the instance forward, else the class forward, unwrapped; the code is accepted when its names includeunsloth_ga_scale_aux_loss._forward_aux_weightingpasses the stage'scoef x auxthrough that scaler, with this micro-batch's labels and attention mask and the window's token count, the same arguments the rewritten forward passes.GA 1, aux off, the statistics carried between stages and the refusals (more than one pipeline micro-batch with the aux loss, reentrant checkpointing) are unchanged.
Testing
New
test_pp_moe_router_aux_loss_with_accumulation_matches_one_processintests/test_parallel_cp_compose_gloo.py: GA 1 / 2 / 4 x aux on / off, pp 2 vs one process. It compares step losses, grad norms, gradients and adapters. The aux coefficient 0.5 is set on the config and on the CausalLM, because TRL < 1.6 has noSFTConfig.router_aux_loss_coef.The floor uses torch 2.13 because pipeline parallel needs torch >= 2.10, and the 2.10 gpipe schedule refuses one micro-batch over two stages. The test's tolerance is rel 5e-4 with atol 1e-5 on the adapters: transformers 4.x drifts about 1.4e-4 at GA 4 with the aux loss off as well, against a 13% to 43% gap from the bug.
The same cells through the composition harness, with the band max(3 x A/A, fp32 floor), on head:
Suites on head, CPU gloo, after merging main:
Not covered
aux_loss / stepsitself) was not exercised. Unsloth's trainer sends the normal loss path to the model forward, which is what this matches.