Repository navigation
Block compile: keep a failed compile attempt out of a non-reentrant checkpoint - #17857
Merged
Merged
Conversation
…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.
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. 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". |
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.
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.
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.forwardtries 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:
gradient_checkpointing_enable(), which defaults touse_reentrant=False(plain HF Trainer or a custom loop);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
test_failed_attempt_stays_out_of_a_non_reentrant_checkpointtests/test_block_cudagraphs_clone.pynow expects the one extra trial call.