Repository navigation
Eager flex_attention: build the score_mod backward graph outside checkpoint saved-tensor hooks - #18219
Conversation
…kpoint saved-tensor hooks Eager flex_attention traces its score_mod backward with torch.autograd.grad in create_fw_bw_graph during the forward. Inside a non-reentrant checkpoint (the DDP default) the unpack restarts the layer's recompute mid forward: CheckpointError (tiny Gemma-2 with block compile under DDP) or silently wrong input gradients. Trace under identity hooks while saved-tensor hooks are active.
|
@codex review |
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. |
|
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". |
…ch without the hook query (2.6) [skip ci]
|
@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". |
Summary
Eager
flex_attentioninside a non-reentrant gradient checkpoint (the default under DDP) restarts the checkpointed layer's recompute in the middle of its own forward. Depending on what the layer saved, training either stops withor keeps going with wrong input gradients and no error.
This PR builds flex's score_mod backward graph under identity saved-tensor hooks whenever some saved-tensor hooks are active. Nothing changes when no hooks are active.
Root cause
When flex_attention runs eagerly (Inductor could not compile the call,
suppress_errorsfalls back, or Dynamo has marked flex as skipped), the HOP's autograd implementation callscreate_fw_bw_graph(torch/_higher_order_ops/flex_attention.py). That function traces the score_mod joint graph withmake_fx, and the trace callstorch.autograd.grad. Inside a non-reentrant checkpoint the checkpoint's pack / unpack hooks are the active default hooks. The trace packs through them and then unpacks, and the unpack is what tells the checkpoint frame to recompute. So the layer reruns while its first forward is still in flight, and the checkpoint compares the tensors saved so far against a full rerun.The traced joint graph holds no activations, so running the trace under identity hooks (the innermost hooks win) is the correct behaviour.
The same happens without block compile. On origin/main, the tiny Gemma-2 test model with
UNSLOTH_BLOCK_COMPILE=0and non-reentrant checkpointing does not raise, but its LoRA gradient norm is 146.57 on the first step and 373.30 on the second (the correct value is 0.026149). Nothing in the run flags it.Pure torch reproduces it with no Unsloth code. A Linear followed by eager
flex_attentionwith a softcap score_mod, on 1 x 2 x 32 x 8 inputs:In Unsloth, the tiny Gemma-2 test model (head_dim 8) hits it through per-layer block compile. The forced main flex kernel cannot lower at head_dim below 16, so the full-attention layer keeps flex outside the block, and that flex call runs eagerly. With reentrant checkpointing (single GPU) the forward runs under no_grad and never builds the joint graph, which is why only DDP failed. Real Gemma-2 (head_dim 256) compiles flex in this configuration, so this exact trigger is specific to the tiny model. A tiny Gemma-2 copy with head_dim 64 compiles flex and never reaches the eager path. Any training run where flex falls back to eager while non-reentrant checkpointing is on is exposed, though.
Changes
unsloth/import_fixes.py:fix_flex_attention_eager_saved_tensor_hookswrapscreate_fw_bw_graph. Under active saved-tensor hooks it pushes identity hooks for the trace. Otherwise, or if hooks are disabled, it calls torch's function unchanged. torch 2.6 has no way to query the active hooks (_top_saved_tensors_default_hooks), so there the trace always runs under identity hooks. The fix is idempotent and does nothing on a torch without the function.unsloth/_gpu_init.py: applies the fix at import, next to the other torch fixes.tests/test_flex_eager_checkpoint_hooks.py: a CPU test for the wrapper contract (wraps once, nothing pushed without hooks, identity hooks pushed under active hooks) and a GPU test checking that eager flex in a non-reentrant checkpoint matches the plain run, with exactly one layer call during the forward.Testing
the checkpoint recomputed the layer inside its own forward, wrapper missing), this branch 2 passed, about 1 s of test time each.Tiny Gemma-2, non-reentrant checkpointing forced on one GPU, fp32, 2 LoRA steps (one-step loss and LoRA grad norm):
Block on vs off on this branch: loss delta 0, grad norm relative delta 7.1e-08, inside the #10744 tolerances (loss 1.4e-4, grad norm 1.5e-3).
No change where nothing was broken: a tiny Gemma-2 with head_dim 64 (flex compiles, both layers fully block compiled) gives bit-identical loss and grad norm on origin/main and this branch, in bf16 and fp32. A counting wrapper shows that
create_fw_bw_graphruns 2 times there and never under active hooks, so the fix stays inert. On the head_dim 8 model it engages 8 times. Reentrant checkpointing results are identical on both trees.torch versions (CPU wheels): wrapper applied, CPU test, and the pure-torch repro (Linear + eager flex with a softcap score_mod in a non-reentrant checkpoint, input grad error vs no checkpoint)
sdpa_dense_backwardcall site)AMD gfx1151 (ROCm, torch 2.11), base vs this PR
Not covered