Repository navigation
Block compile: test that checkpoint early-stop does not demote the layer - #14065
danielhanchen merged 6 commits into
Conversation
Non-reentrant gradient checkpoint raises _StopRecomputationError to end recomputation early. The block-compile trial loop treated that as a compile failure, demoted the layer mid-recompute, and tripped CheckpointError on distributed runs (unslothai#14063). Re-raise it instead.
…nt checkpoint (unslothai#14195) Addresses the GatedDeltaNet failure in unslothai#14063. ## Problem On 2026.10.6, LoRA training of Qwen3.5 (a GatedDeltaNet hybrid) with `use_gradient_checkpointing="unsloth"` under DDP or FSDP2 dies in the backward of step 1. Reproduced here on Qwen3.5-0.8B with 2 GPUs; the issue reports the same on the 27B model with 8 GPUs: ``` torch.utils.checkpoint.CheckpointError: torch.utils.checkpoint: A different number of tensors was saved during the original forward and recomputation. Number of tensors saved during forward: 55 Number of tensors saved during recomputation: 45. ``` Single-GPU runs are not affected. `UNSLOTH_BLOCK_COMPILE=0` avoids it. ## Root cause Distributed runs use torch's non-reentrant checkpoint (`use_reentrant = not is_distributed()` in `vision.py`), whose saved-tensor hooks are active for the whole layer forward. On the first GatedDeltaNet layer, the block-compile wrapper calls `_prepare_gdn`, which runs `_gdn_op._fla_ops`. Its `_self_check` does a forward and `torch.autograd.grad` against fla's own Function. Because this happens inside the checkpointed layer forward: 1. The self-check's saved tensors are packed into the layer's checkpoint frame. 2. Its `autograd.grad` unpacks them, which starts a recompute of the whole layer during forward0. 3. That nested recompute enters the block-compile wrapper before the layer is marked checked, so the trial loop catches torch's `_StopRecomputationError` and demotes the layer kind `_FULL -> _SPLIT -> _OFF`. The self-check then fails as well, so the fla opaque op is disabled. 4. The original forward finishes compiled and saves the self-check's tensors plus the compiled set (55). The real recompute in backward runs the stock forward for that layer (45), and `check_recomputed_tensors_match` raises. The fla self-check runs for every user because unsloth_zoo vendors fla. ## Fix Run the self-check in `_fla_ops` under its own `saved_tensors_hooks`, so an enclosing checkpoint never sees its tensors. The pack hook detaches, as torch's own recompute hook does, so a check that fails before `autograd.grad` does not leave a reference cycle holding its tensors. unslothai#14065 alone does not fix the reproduction. Re-raising `_StopRecomputationError` removes the demotion, but the self-check's tensors are still in the frame, so the run still fails with 55 vs 46. With this change the nested recompute never happens. ## Verification Qwen3.5-0.8B, LoRA r=16, `use_gradient_checkpointing="unsloth"`, 3 steps, 2 GPUs, torch 2.12.1+cu130: | Launch | main | This PR | | --- | --- | --- | | `torchrun`, DDP wrapper | `CheckpointError` 55 vs 45, 18 layers OFF | trains, 24 layers FULL | | `accelerate` FSDP2, SFTTrainer | `CheckpointError` 55 vs 45, 18 layers OFF | trains, 24 layers FULL | Single GPU (reentrant) is unchanged: same step-0 loss, all layers FULL. Tests: - New `test_gdn_self_check_stays_out_of_a_non_reentrant_checkpoint` runs `_fla_ops` inside `checkpoint(..., use_reentrant=False)` with a stand-in self-check that calls `autograd.grad`. It fails on main with `CheckpointError` (10 vs 5) and passes here. It runs on CPU without fla. - `tests/test_block_compile.py`: 22 passed, 1 failed. `test_qwen3_5_gdn_block_has_no_break` fails the same way on main. ## Not covered The plain-attention case in the issue (`MuseGlimmerTextDecoderLayer`, 65 vs 64) and the reported slowdown.
|
@codex review |
Codex Review SummaryThis 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. |
|
Codex Review: Didn't find any major issues. Another round soon, please! 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". |
|
@codex review |
|
Codex Review: Didn't find any major issues. 🚀 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". |
After merging main, Backend CI failed on two jobs for unrelated regressions, not the block-compile test: - Repo tests (CPU, studio): gpt-oss skipifs used torch.cuda.is_available(), which the accelerator skip-guard lint rejects (spoofable). Gate on has_real_cuda() instead. - Repo tests (CPU, rest): open() without utf-8 in the gpt-oss Hub stub, and DDP BF16/FP16 comm-hook cases that raise on CPU gloo and sink the shared ranks fixture. Encode utf-8; only run hook cases on real CUDA.
|
@codex review |
1 similar comment
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 4edd22a10b
ℹ️ 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".
| ("bf16_hook", "full", {"UNSLOTH_DDP_COMM_HOOK": "bf16"}, {}), | ||
| ("fp16_hook", "peft", {"UNSLOTH_DDP_COMM_HOOK": "fp16"}, {}), | ||
| ] | ||
| CASES = _BASE_CASES + (_HOOK_CASES if has_real_cuda() else []) |
There was a problem hiding this comment.
Keep compression-hook cases out of the CPU worker
On a CUDA-equipped test host, this condition adds _HOOK_CASES to the module-wide CASES, but run_rank in tests/_ddp_comm_defaults_worker.py unconditionally sets ACCELERATE_USE_CPU=true and constructs Accelerator(cpu=True), so these cases still run through CPU/gloo and raise the same BF16/FP16 TypeError described above. The skipif on test_comm_hook_opt_in cannot help because the three unskipped DDP tests all instantiate the shared ranks fixture with this full list, causing the whole module to fail on such hosts; omit these cases from the CPU worker or add a genuinely NCCL-backed worker.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
The premise does not hold: torch's guard (DistributedDataParallel._check_comm_hook) checks the torch build (CUDA/HIP plus NCCL 2.10+), not the backend or a GPU, so on a CUDA build both hook cases pass through the gloo CPU worker (verified: 29 passed with CUDA_VISIBLE_DEVICES empty). has_real_cuda() was the wrong predicate the other way, though, so c3f41ca gates only the bf16 case on that same build check and keeps the fp16 case on every host.
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. Another round soon, please! 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". |
Part of #14063.
Summary
The fix this PR originally made (re-raise torch's
_StopRecomputationErrorfrom the block-compile trial loop instead of demoting the layer) landed on main separately in #14231, so the code change here is dropped after merging main. What main does not have is a test for it, so this PR now adds only that:test_stop_recomputation_does_not_demotemakes the compiled block raisetorch.utils.checkpoint._StopRecomputationErrorand asserts it propagates with the layer still on its first mode and not marked checked. Demoting on it is what made the recompute run the stock forward and failcheck_recomputed_tensors_matchon distributed runs.Verification
if False:), the new test fails; with main as is, it passes.tests/test_block_compile.pyon CPU: the new test passes; the one failure there (test_sm_count_is_read_outside_the_trace) needs a CUDA device and fails the same way on main without one.The remaining Muse Glimmer route in #14063 (65 vs 64 with no demotion logged, plus the slowdown) is fixed in unslothai/unsloth-zoo#1895.
CI fixes pulled in with main
Merging main brought in CPU CI failures unrelated to block compile; they are fixed here (tests only, merge base to head touches only
tests/):tests/test_gpt_oss_*: skip guards readhas_real_cuda()instead of the spoofabletorch.cuda.is_available(), and the Hub stub writes withencoding="utf-8".tests/test_ddp_comm_defaults.py: torch refusesbf16_compress_hookon builds without CUDA/HIP and NCCL 2.10+ (DistributedDataParallel._check_comm_hook), which sank the shared ranks fixture on CPU wheels. The bf16 case now runs only where that same build check passes; the fp16 case still runs on every host.