Repository navigation
Block compile: keep the non-reentrant trial forward out of transformers output capture - #17872
Merged
Merged
Conversation
The trial forward that #17857 runs inside a non-reentrant checkpoint was recorded by transformers' output capture as an extra MoE router_logits entry, which changed the load-balancing loss and its gradient (tiny Qwen3-MoE, coefficient 0.9: LoRA grad norm 1.044 with checkpointing vs 1.214 without). Suppress capture for the trial: the collector context variable on transformers 5, the make_capture_wrapper forwards on 4.x.
Codex Review Summary: ✅ CompletedThis 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. |
Member
Author
|
@codex review |
|
Codex Review: Didn't find any major issues. You're on a roll. 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". |
[skip ci]
LeoBorcherding
pushed a commit
to LeoBorcherding/unsloth
that referenced
this pull request
Nov 15, 2026
…inting (unslothai#17873) With the MoE router load-balancing loss on, Unsloth's default `"unsloth"` gradient checkpointing kept the aux loss in the logged value but dropped its gradient. On gpt-oss the step-1 grad norm was 0.0261 with checkpointing vs 0.0486 without. ## Root cause Reentrant checkpointing runs the layer forward under `no_grad` first. The router logits that transformers captures there carry no graph. The loss therefore gains the aux value, but the router and expert LoRA weights get no aux gradient. - **Affected:** one GPU and FSDP2. - **Not affected:** DDP, which already checkpoints non-reentrantly. - **Already refused:** `pp_native` refused this pairing for pipeline parallel only. ## Fix A model whose config has `output_router_logits`, with the loss on, now takes the non-reentrant checkpoint path that DeepSeek-V4.1 and DDP already use. "Loss on" means `output_router_logits` is set, or `router_aux_loss_coef > 0`, which TRL's SFTTrainer turns on. Activations are still offloaded through saved-tensor hooks. Gemma 3n / 4 keep reentrant checkpointing. This PR includes unslothai#17872's commit, which keeps block compile's non-reentrant trial forward out of output capture. Without it, Qwen3-MoE records an extra `router_logits` entry. Merge unslothai#17872 first. ## Results **GPU test** `tests/test_moe_aux_loss_checkpointing.py` (B200, correctness only): LoRA grad norm with `"unsloth"` checkpointing equals the norm without checkpointing, at aux coefficient 0.9. | Model, transformers | main (with / without checkpointing) | This PR | |---|---|---| | gpt-oss tiny, 4.57.6 | 0.0170 / 0.0283 | pass | | gpt-oss tiny, 5.23 | 0.0176 / 0.0701 | pass | | Qwen3-MoE tiny, 4.57.6 | 0.502 / 1.214 | pass | | Qwen3-MoE tiny, 5.23 | | pass | **Colab T4** (fp16, gpt-oss, one GPU): grad norms with checkpointing now equal those without, `[0.0486, 0.048, 0.0625, 0.0266]`. 8 activation tensors were offloaded. Qwen3-MoE also matches. **Cost** (Colab L4, medium random Qwen3-MoE, hidden 1024, 8 layers, 32 experts top-4, LoRA r16, bf16, batch 2 x 1024, median of steps 8-30; base run 3 times, head 3 times): | Arm | Median step | Peak allocated | |---|---|---| | main (reentrant) | 0.1571 s (A/A 0.155-0.162) | 2.248 GiB | | This PR (non-reentrant offload) | 0.1650 s | 1.736 GiB | Step time is about 5% slower, and peak memory about 23% lower. **Not covered:** FSDP2 after the change, larger models and longer sequences.
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 join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Follow-up to #17857. The grad-mode trial forward it added before a block's first non-reentrant checkpoint call was recorded by transformers' output capture.
Problem
transformers' output capture collects every router (gate) output during a forward. It also captured the trial, so MoE models got an extra
router_logitsentry. That changed the load-balancing loss and its gradient on the first step.This hits Qwen3-MoE and other MoE families under non-reentrant checkpointing, the default from transformers 5.22. gpt-oss was unaffected only because its decoder layer is in block compile's skip list, so its trial never runs.
Fix
The trial runs inside
_no_output_capture(layer):output_capturing._active_collectoris set toNonefor the trial.make_capture_wrapperforward is temporarily swapped back to its__wrapped__original.Results
test_trial_forward_is_not_recorded_as_a_model_output[hook|forward](transformers 4.57.6 / 5.22)tests/test_block_compile.py, 4.57.6tests/test_block_compile.py, 5.22