Skip to content

Block compile: keep a failed compile attempt out of a non-reentrant checkpoint - #17857

Merged
danielhanchen merged 1 commit into
mainfrom
gc2-nonreentrant-ckpt
Nov 15, 2026
Merged

danielhanchen merged 1 commit into
mainfrom
gc2-nonreentrant-ckpt

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Under non-reentrant gradient checkpointing, a block compile attempt that fails on the first call leaves its packed tensors inside the checkpoint, and backward then raises CheckpointError.

Root cause

On a block's first grad-mode call, _block_compile's _wrap.forward tries FULL, then SPLIT, then OFF, all inside the current call. Under non-reentrant checkpointing that call is the checkpointed forward.

When SPLIT runs its first compiled graph and then fails on a later one (tiny Gemma3: flex lowering fails at head_dim 8), the stock forward runs as well. The forward then saves 94 tensors (70 + 24), while the recompute saves 70, and torch raises CheckpointError. The reentrant path already ran a grad-mode trial first; the non-reentrant path had no equivalent.

Users reach this path through:

  • transformers 5.22.1 / 5.23 gradient_checkpointing_enable(), which defaults to use_reentrant=False (plain HF Trainer or a custom loop);
  • FastModel under DDP;
  • DeepSeek-V4.1.

Fix

When a block's first grad-mode call is inside a non-reentrant checkpoint, the same trial now runs first. It uses forked RNG and identity saved-tensor hooks, so nothing from the trial reaches the checkpoint. This is the pattern zoo's MoE SwiGLU warm-up already uses. The cost is one extra forward per layer, once, on the first step.

Results

Check main this PR
CPU flip test_failed_attempt_stays_out_of_a_non_reentrant_checkpoint CheckpointError pass
Gemma3 FastLanguageModel, non-reentrant, block compile on (transformers 5.22.1 and 5.23) CheckpointError (94 vs 70) pass
Gemma3 non-reentrant vs block compile off n/a bitwise equal
Gemma2 / Qwen3 FLM, Qwen2.5-VL FastModel: this PR vs main loss and grads bitwise equal
Qwen3 / Gemma2 compile stats 1 / 2 graphs, 0 breaks unchanged

tests/test_block_cudagraphs_clone.py now expects the one extra trial call.

…heckpoint

A block whose first grad-mode call runs inside torch's non-reentrant checkpoint demoted
FULL -> SPLIT -> OFF inside the checkpointed forward. A SPLIT attempt that ran its first
graph and then failed had already packed tensors into the checkpoint frame, so the
recompute (stock forward) saved fewer: CheckpointError (94 vs 70 on tiny Gemma3).

Run the grad-mode trial first, as the reentrant no-grad path already does, under identity
saved-tensor hooks so its tensors never reach the checkpoint.
@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-15T17:33:00.355173Z e2ff338 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. Chef's kiss.

Reviewed commit: e2ff338f59

ℹ️ 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 ee6d304 into main Nov 15, 2026
@danielhanchen
danielhanchen deleted the gc2-nonreentrant-ckpt branch November 15, 2026 18:57
Stanley00 pushed a commit to stanley-fork/unsloth that referenced this pull request Nov 15, 2026
…rs output capture (unslothai#17872)

Follow-up to unslothai#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 |
danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Nov 16, 2026
Final comment pass for recently merged PRs.

Trims comments added by unslothai#17660, unslothai#17806, unslothai#17807, unslothai#17808, unslothai#17812, unslothai#17830,
unslothai#17854, unslothai#17857 and unslothai#17870 that are still on main.
Multi-line explanations are collapsed to one line, and comments that
restate the code are removed.
Comments that explain a version quirk or a correctness trap are kept,
along with license headers and pragmas.
Changed comment lines: unslothai#17660 5 -> 2, unslothai#17806 4 -> 2, unslothai#17808 1 -> 0,
unslothai#17830 3 -> 1, unslothai#17857 1 -> 0. unslothai#17807, unslothai#17812, unslothai#17854 and unslothai#17870 are
unchanged.
Overall: 6 files, 14 lines removed, 5 added. Only comments change: the
AST check (comment_tools.py check) and the pre-push gate both pass.
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