Skip to content

Attention dispatch: saved attention routing scope, torch 2.4 enable_gqa floor, block SDPA cache tag - #18631

Merged
danielhanchen merged 5 commits into
mainfrom
kr4-attention-dispatch
Nov 20, 2026
Merged

danielhanchen merged 5 commits into
mainfrom
kr4-attention-dispatch

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Follow-up fixes for #18488, #18453, #18462, found while re-checking the PRs merged on 2026-11-19. One commit per fix; every fix has a test that fails on main and passes here.

Fix Follows up Severity
Attention dispatch: automatic saved attention only inside Unsloth's checkpoint #18488 P2
ROCm SDPA self-check: no enable_gqa on torch without it #18453 P2
Block SDPA align guard: cache tag also under the override #18462 P2

Tests (this branch vs main 54af93780b, CPU)

  • New and changed tests: main 8 failed / 16 passed, this branch 24 passed. Each test loads the code of its own tree.
  • Neighbouring suites on this branch: attention dispatch, ROCm SDPA check and block SDPA guard suites: 61 passed, 5 skipped.
  • prepush_gate.py passes.

Attention dispatch: automatic saved attention only inside Unsloth's checkpoint (follows up #18488)

Summary

Follow-up to #18488. With UNSLOTH_GC_SAVE_ATTENTION unset, automatic saved attention now routes a long training call only where a saved-attention frame can exist. That means under Unsloth's smart checkpoint (use_gradient_checkpointing = "unsloth") and, when the call is traced, only inside Unsloth's own compiled block.

What broke

#18488 routes every unpacked SDPA training call at or above the threshold through unsloth_zoo.saved_attention.saved_attention_call. The zoo creates frames only inside UnslothCheckpointFunction (zoo gradient_checkpointing.py:2074). With use_gradient_checkpointing = True or False, apply_unsloth_gradient_checkpointing unpatches it, so no frame ever exists. saved_attention_call is @torch.compiler.disabled and with no frame it just runs the plain call. Inside a compiled decoder block (per-block compile is on by default for FastLanguageModel on torch >= 2.11), each long attention call therefore becomes a graph break that saves nothing. Under a user's torch.compile(..., fullgraph=True) it raises Unsupported: Skip calling torch.compiler.disable()'d function.

This only happens where zoo turns on automatic mode: T4 and A100 from 8K tokens, L4 from 16K.

Repro (CPU, automatic mode simulated with a threshold of 16, 32-token call, no smart checkpoint)

arm default compile fullgraph=True
automatic mode off (before #18488) 0 graph breaks compiles
main 3 graph break records (Skip calling torch.compiler.disable()'d function), no frame active Unsupported
this branch 0 graph breaks compiles

Fix

_auto_saves_attention now also requires _saved_attention_frames_possible():

  • torch.utils.checkpoint.CheckpointFunction is UnslothCheckpointFunction. Dynamo cannot read a Function class's name, so this read goes through _constant_probe. The patch is installed at get_peft_model / load time, before any trace.

  • When traced, unsloth.models._block_compile._IN_COMPILED_REGION[0] must be set. That flag marks Unsloth's own block (fullgraph=False, where a break is legal) and not a user's compile. zoo's sequence tiling reads the same flag.

  • In automatic mode the setting must still be auto when the call is made. UNSLOTH_GC_SAVE_ATTENTION=0 set after import unsloth stops zoo's frames (new_saved_attention_frame), but before this change it did not stop the routing, so it still produced a graph break per layer inside Unsloth's own block.

The explicit =1 / offload setting (min_seq None) is unchanged.

Tests

New tests/test_attention_dispatch_saved_attention_scope.py (CPU):

  • automatic mode routes only under the smart checkpoint;
  • a user's compile has 0 graph breaks with and without the smart checkpoint, and fullgraph=True compiles;
  • inside Unsloth's block under the smart checkpoint the call is still routed;
  • the explicit setting still routes;
  • UNSLOTH_GC_SAVE_ATTENTION=0 / false set after import stops routing and leaves 0 graph breaks inside Unsloth's block.

tests/test_attention_dispatch_saved_attention.py: the two automatic-mode routing tests now install UnslothCheckpointFunction, as a real "unsloth" run does.

Command (per worktree, unsloth_zoo main on PYTHONPATH): python -m pytest -o addopts= -m "not server and not native_mxc" -p no:cacheprovider -q tests/test_attention_dispatch_saved_attention_scope.py tests/test_attention_dispatch_saved_attention.py. Each arm imports its own tree.

arm result
main (635532f) 6 failed, 14 passed
this branch 20 passed

Wider suites (tests/*attention*.py, tests/test_sdpa*.py, tests/utils/test_rocm_sdpa_check.py): main 2 failed / 255 passed / 302 skipped. This branch: 2 failed / 261 passed / 302 skipped. The same two failures in test_flash_attention_sub_config_fallback.py show up on main.

Not proven here

ROCm SDPA self-check: no enable_gqa on torch without it (follows up #18453)

Summary

Follow-up to #18453. On torch 2.4, the declared floor, the ROCm SDPA self-check refused AOTriton flash for every Unsloth attention call on an RDNA GPU. The kernel was never the cause: the check sent an argument that torch 2.4 does not have.

What broke

The flash child in unsloth/utils/rocm_sdpa_check.py runs a segments_gqa case that calls F.scaled_dot_product_attention(..., enable_gqa=True). torch added enable_gqa in 2.5. On 2.4 the call raises TypeError: scaled_dot_product_attention() got an unexpected keyword argument 'enable_gqa'. The child records any exception other than "No available kernel" or out of memory as a failed kernel case, so the report says ok: false and rocm_sdpa_verdict refuses flash for that device. Unsloth itself never passes enable_gqa when SDPA lacks it (SDPA_HAS_GQA in attention_dispatch.py), so no real call can reach that layout there.

Repro (CPU)

The real child script runs in a subprocess. CUDA calls are mapped to CPU (torch's CPU flash kernel, which is correct), and F.scaled_dot_product_attention is replaced by one that rejects enable_gqa, as torch 2.4's operator does.

arm flash child report
main (635532f) ok: false, ran 8, every failure is segments_gqa ...: unexpected keyword argument 'enable_gqa'
this branch ok: true, ran 4 (expanded K / V segments, fp16 and bf16, head dims 64 and 128)

Fix

When enable_gqa is not in F.scaled_dot_product_attention.__doc__, the child drops the segments_gqa case. That is the same test attention_dispatch uses. The expanded K / V and masked cases still run, so a broken kernel is still refused.

Tests

New tests/utils/test_rocm_sdpa_check_no_gqa.py (CPU). Command (per worktree): python -m pytest -o addopts= -m "not server and not native_mxc" -p no:cacheprovider -q tests/utils/test_rocm_sdpa_check_no_gqa.py tests/utils/test_rocm_sdpa_check.py. Each arm imports its own tree.

arm result
main 1 failed, 24 passed, 2 skipped
this branch 25 passed, 2 skipped

Not proven here

  • No ROCm torch 2.4 hardware run, and no claim about which RDNA archs torch 2.4 offers AOTriton flash on. Where it does not offer flash, this change makes no difference.
  • torch >= 2.5 keeps the segments_gqa case unchanged.

Block SDPA align guard: cache tag also under the override (follows up #18462)

What broke

#18462 adds the cache tag unsloth/efficient-backward-guard so that graphs compiled without the efficient-backward guard are not served again. The patched SDPA lowering is not part of the FX / AOT cache key. UNSLOTH_BLOCK_SDPA_ALIGN_GUARD=1 returned before adding that tag.

On CUDA torch < 2.14, a run with the override therefore has the same cache key as a run of the same Unsloth version from before the tag existed. The version string is 2026.11.7 both before and after #18462. That older run compiled the efficient backward without the guard. A user who set the override as the workaround for the attn_bias is not correctly aligned fault, and kept it after updating, keeps being served the faulting backward from a warm cache. The PR's own evidence notes the override was not a reliable workaround once a failing run had warmed the cache.

Fix

unsloth/models/_block_compile.py, _sdpa_alignment_without_guard: under the override on an affected torch, add the same cache tag before returning. The lowering stays stock as before. Torch 2.14+ and ROCm are unchanged.

Tests

New tests/test_block_sdpa_align_guard_override_cache_tag.py (CPU, no compile): with the override set, the lowering must stay untouched. The tag must be added exactly once on an affected torch and not on 2.14+. The test loads _block_compile.py from its own tree and asserts it.

Arm Result
main 1 failed (torch_lt_2_14), 2 passed
this branch 3 passed on torch 2.14.1; on torch 2.4.1 (no cache_key_tag) the tag tests skip with that reason

tests/test_block_sdpa_efficient_backward_guard.py passes on both arms.

Not covered

No warm-cache GPU run of the fault itself.

Not proven here

Step time and memory of long-sequence SFT with and without automatic saved attention; ROCm hardware (gfx1151 / gfx942) for the self-check; torch 2.14 on GPU for the align guard.

Automatic saved attention (UNSLOTH_GC_SAVE_ATTENTION unset, zoo #2619) routed every long unpacked
SDPA training call through saved_attention_call. Frames only exist inside Unsloth's smart checkpoint,
so with use_gradient_checkpointing True or False the routed call ran the plain attention behind
torch.compiler.disable: a graph break per layer inside a compiled block for nothing, and Unsupported
under a user's fullgraph=True compile.

Route automatic mode only when UnslothCheckpointFunction is installed and, when traced, only inside
Unsloth's own compiled block (_IN_COMPILED_REGION). The explicit =1 / offload setting is unchanged.
The automatic threshold is read when unsloth is imported, but zoo decides per checkpoint whether to make
a frame. UNSLOTH_GC_SAVE_ATTENTION=0 set after import therefore stopped the frames while run_attention
kept routing long calls into saved_attention_call: a graph break per layer for nothing inside Unsloth's
own block. Automatic routing now also requires the setting to still be auto.
…ble_gqa

On torch 2.4 (the declared floor) F.scaled_dot_product_attention has no enable_gqa argument. The flash
child sent it in its segments_gqa case, recorded the TypeError as a failed kernel case and refused
AOTriton flash for every Unsloth attention call on that GPU, although Unsloth never passes enable_gqa
where SDPA lacks it. The child now drops that case there, the same docstring test attention_dispatch
uses (SDPA_HAS_GQA); the expanded K / V cases still run.
…UARD=1 too

The override keeps the stock SDPA lowering and returned before adding the
efficient-backward cache tag. The lowering patch is not part of the cache
key, so on CUDA torch < 2.14 the override shares its key with a run of the
same version from before the tag existed, which compiled the efficient
backward without the guard: a warm cache keeps serving that faulting
backward. The override now adds the tag as well.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 20, 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-20T13:56:18.543063Z f4f8509 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. Can't wait for the next one!

Reviewed commit: f4f8509c6a

ℹ️ 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 3cd18d9 into main Nov 20, 2026
@danielhanchen
danielhanchen deleted the kr4-attention-dispatch branch November 20, 2026 16:58
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