Repository navigation
Attention dispatch: saved attention routing scope, torch 2.4 enable_gqa floor, block SDPA cache tag - #18631
Merged
Conversation
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.
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. Can't wait for the next one! 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". |
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.
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.
Tests (this branch vs main
54af93780b, CPU)prepush_gate.pypasses.Attention dispatch: automatic saved attention only inside Unsloth's checkpoint (follows up #18488)
Summary
Follow-up to #18488. With
UNSLOTH_GC_SAVE_ATTENTIONunset, 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 insideUnslothCheckpointFunction(zoogradient_checkpointing.py:2074). Withuse_gradient_checkpointing = TrueorFalse,apply_unsloth_gradient_checkpointingunpatches it, so no frame ever exists.saved_attention_callis@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'storch.compile(..., fullgraph=True)it raisesUnsupported: 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)
fullgraph=TrueSkip calling torch.compiler.disable()'d function), no frame activeUnsupportedFix
_auto_saves_attentionnow also requires_saved_attention_frames_possible():torch.utils.checkpoint.CheckpointFunctionisUnslothCheckpointFunction. Dynamo cannot read a Function class's name, so this read goes through_constant_probe. The patch is installed atget_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
autowhen the call is made.UNSLOTH_GC_SAVE_ATTENTION=0set afterimport unslothstops 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/offloadsetting (min_seq None) is unchanged.Tests
New
tests/test_attention_dispatch_saved_attention_scope.py(CPU):fullgraph=Truecompiles;UNSLOTH_GC_SAVE_ATTENTION=0/falseset 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 installUnslothCheckpointFunction, 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.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 intest_flash_attention_sub_config_fallback.pyshow up on main.Not proven here
use_gradient_checkpointing = Trueat 8K on an A100 or T4 (main vs this branch) needs a real card. The smart-checkpoint path is unchanged, so Attention dispatch: route long SDPA training calls through automatic saved-attention GC replay #18488's measured gains still apply.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.pyruns asegments_gqacase that callsF.scaled_dot_product_attention(..., enable_gqa=True). torch addedenable_gqain 2.5. On 2.4 the call raisesTypeError: 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 saysok: falseandrocm_sdpa_verdictrefuses flash for that device. Unsloth itself never passesenable_gqawhen SDPA lacks it (SDPA_HAS_GQAinattention_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_attentionis replaced by one that rejectsenable_gqa, as torch 2.4's operator does.ok: false, ran 8, every failure issegments_gqa ...: unexpected keyword argument 'enable_gqa'ok: true, ran 4 (expanded K / V segments, fp16 and bf16, head dims 64 and 128)Fix
When
enable_gqais not inF.scaled_dot_product_attention.__doc__, the child drops thesegments_gqacase. That is the same testattention_dispatchuses. 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.Not proven here
segments_gqacase unchanged.Block SDPA align guard: cache tag also under the override (follows up #18462)
What broke
#18462 adds the cache tag
unsloth/efficient-backward-guardso 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=1returned 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 alignedfault, 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.pyfrom its own tree and asserts it.torch_lt_2_14), 2 passedcache_key_tag) the tag tests skip with that reasontests/test_block_sdpa_efficient_backward_guard.pypasses 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.