Skip to content

Pipeline parallel: weight the MoE router aux loss per accumulation step as the model forward does - #16708

Merged
danielhanchen merged 9 commits into
mainfrom
dh/cmp-pp-moe-aux-ga-weight
Nov 5, 2026
Merged

danielhanchen merged 9 commits into
mainfrom
dh/cmp-pp-moe-aux-ga-weight

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

Under UNSLOTH_PP_SIZE=2, a MoE model with the router load-balancing loss on trained a different objective than one GPU whenever gradient_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:

GA 1 GA 2 GA 4
step 1-3 loss, one process matches 8.0523, 7.9559, 7.9671 8.0086, 7.9533, 8.0074
step 1-3 loss, pp 2, before matches 9.1250, 9.0015, 8.9896 (13% off) 42% off
step 1-3 loss, pp 2, after matches 8.0523, 7.9559, 7.9671 (exact) exact loss, grad rel 5e-6

On one GPU, the CausalLM forward that Unsloth Zoo rewrites (fused_losses/aux_loss.py) wraps router_aux_loss_coef * aux_loss in unsloth_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 include unsloth_ga_scale_aux_loss.
  • _forward_aux_weighting passes the stage's coef x aux through that scaler, with this micro-batch's labels and attention mask and the window's token count, the same arguments the rewritten forward passes.
  • A forward without the rewrite keeps the term in full, which is also what one process trains then. There is no version check: the answer comes from the code that runs.

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_process in tests/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 no SFTConfig.router_aux_loss_coef.

corner (torch / transformers / TRL) base head
floor: 2.13 CPU / 4.57.6 / 0.22.2 GA 2 aux on FAIL, GA 4 aux on FAIL, 4 passed 6 passed
pin ceiling: 2.13 / 5.17.0 / 1.15.0 GA 2 aux on FAIL, GA 4 aux on FAIL, 4 passed 6 passed
latest: 2.15 CPU / 5.22.0 / 1.17.0 GA 2 aux on FAIL, GA 4 aux on FAIL, 4 passed 6 passed

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:

corner GA 2 aux on: loss / grad / adapter dev (lr) GA 4 aux on
floor 0 / 1.3e-5 / 5.1e-5 0 / 6.1e-5 / 1.9e-4
pin ceiling 0 / 0 / 0 0 / 5e-6 / 2.8e-5
latest 0 / 0 / 0 0 / 5e-6 / 2.8e-5

Suites on head, CPU gloo, after merging main:

suite result
tests/test_parallel_cp_compose_gloo.py, tests/test_pp_moe_aux_gloo.py 45 passed
tests/test_pp_env_gloo.py, tests/test_parallel_env.py, tests/test_pp_native_gloo.py, DDP aux test (before the merge) 185 passed

Not covered

  • TRL's own fused-head path (TRL >= 1.15 computes aux_loss / steps itself) was not exercised. Unsloth's trainer sends the normal loss path to the model forward, which is what this matches.
  • CPU gloo only; no NCCL run.

…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.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-05T01:20:17.061008Z b0089ed Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

Reviewed commit: 0689975f2a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Chef's kiss.

Reviewed commit: b0089edb87

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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".

@danielhanchen
danielhanchen merged commit 590119d into main Nov 5, 2026
@danielhanchen
danielhanchen deleted the dh/cmp-pp-moe-aux-ga-weight branch November 5, 2026 04:21
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.

1 participant