Skip to content

Block compile: keep the non-reentrant trial forward out of transformers output capture - #17872

Merged
danielhanchen merged 2 commits into
mainfrom
block-trial-no-output-capture
Nov 15, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
block-trial-no-output-capture

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

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_logits entry. 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):

  • transformers 5.x: output_capturing._active_collector is set to None for the trial.
  • transformers 4.x: each make_capture_wrapper forward is temporarily swapped back to its __wrapped__ original.

Results

Check main This PR
CPU test_trial_forward_is_not_recorded_as_a_model_output[hook|forward] (transformers 4.57.6 / 5.22) fails passes
tests/test_block_compile.py, 4.57.6 56 passed, 15 skipped
tests/test_block_compile.py, 5.22 64 passed, 7 skipped
B200, tiny Qwen3-MoE, aux coefficient 0.9, LoRA grad norm with vs without non-reentrant checkpointing 1.0439 vs 1.2138 (4.57.6), 1.0874 vs 1.3469 (5.23) equal

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

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

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-15T19:51:24.883840Z a53874d 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. You're on a roll.

Reviewed commit: a53874d917

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

[skip ci]
@danielhanchen
danielhanchen merged commit fdfd812 into main Nov 15, 2026
@danielhanchen
danielhanchen deleted the block-trial-no-output-capture branch November 15, 2026 22:34
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.
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