Repository navigation
head_dim 512 attention: sm80+ only, no 256 MB autotune buffer, OOM falls back to SDPA, no per-call mask sync - #13977
Conversation
…ck to SDPA, no per-call mask sync Triton's default autotune benchmarker allocates a 256 MB L2 flush buffer, so the load-time self check and the first call of each new GQA group size could OOM a model that fit on the previous SDPA path. Time configs in place instead (each config only rewrites its own outputs), treat an OOM in the self check or in the forward kernel as a fallback to SDPA, and only scan a mask when the verdict can be used: not for inference masks (no version counter, so every layer would pay a host sync) and not after a failed self check.
…en no self check passes The kernel was only timed faster than SDPA on sm80+, and T4 lacks the shared memory for the larger tiles and has no bf16. On pre-Ampere GPUs the self check now returns False without compiling or autotuning anything, and the router is not installed at all when every device / dtype the model uses failed its self check, so those loads keep transformers' sdpa function untouched.
|
@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. Keep them coming! 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. Breezy! 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". |
… mode (unslothai#14202) ## Summary unslothai#13977 made the head_dim 512 attention router send every attention mask created under `torch.inference_mode()` to SDPA. An inference tensor has no version counter, so the mask verdict cannot be cached across layers, and the change avoided rescanning it per layer. But `generate()` and evaluation build their masks under inference mode, so every masked Gemma-4 prefill lost the kernel. SDPA then repeats the KV heads and keeps the dense mask: at `[1, 8, 4096, 512]` with 2 KV heads, about 128 to 160 MiB more temporary memory, and a reproducible OOM at 8192 tokens under an allocator cap where the kernel fits. The bool mask case was also about 10% slower. Inference masks now go through the same scan as any other mask. They are rescanned on every call, as `intervals_from_mask` already does for them, which costs one host sync per global attention layer. That only happens at prefill, because decode (one query token) never reaches the kernel. Masks made outside inference mode, compiled graphs and the failed self check path are unchanged. ## Tests `tests/test_large_head_attention.py` (GPU): - New `test_router_uses_the_kernel_for_masks_made_under_inference_mode`: a causal mask made under inference mode now runs the kernel, with output equal to the maskless causal call. Fails on main (falls back to SDPA). - `test_router_skips_the_mask_scan_when_it_cannot_use_the_result` keeps the failed self check case and drops the inference mask one. All 50 tests in the file pass. ## Not covered here - No end-to-end Gemma-4 `generate` timing; the memory and OOM numbers above come from a kernel-level repro on B200.
Follow-up to #13869 for three problems on the default-on path.
T4 step time. On a Colab T4, fp16 Gemma-4 E2B vision SFT (12 steps, ABBA) ran 1.8% to 3.5% slower per step with #13869 than with its parent or with
UNSLOTH_HD512_ATTENTION=0. Yet the kernel never ran there: its self check fails on sm75, and fp16 Gemma-4 text attention reaches SDPA directly through the float32 path. The kernel was only timed faster on sm80+. Pre-Ampere GPUs now skip the self check entirely (no compile, no autotune), and the router is not installed when every device and dtype of the model failed its self check. On the same T4 job, this change is level with the kernel switched off (4300 / 4307 ms against 4403 / 4379 ms), with identical peak memory.Autotune memory. Triton's default autotune benchmarker allocates a 256 MB L2 flush buffer. With head_dim 512 this happens in the load-time self check, and again on the first forward and first backward of every new GQA group size, because the autotune key is
(HEAD_DIM, G)and the self check only covers G = 2. A model that fit on the previous SDPA path could therefore OOM at load or on its first training step. Under a 128 MB process cap, SDPA succeeds on a G = 8 call, while the kernel failed allocating 256 MB. The self check also added 258 MB of peak allocation.do_benchattribute, which works on Triton 3.2 (whereautotune()does not forwarddo_bench=) through 3.7.OutOfResourceshandling for configs that do not fit is unchanged.Mask scan sync. The router scanned the mask (one host sync) before looking at the self-check verdict, and inference-mode masks, which cannot cache the verdict, were rescanned on every layer. Left-padded generation prefill is then rejected anyway. Masks are now scanned only when the result can be used: not for inference-mode masks (they go straight to the previous SDPA, unchanged) and not after a failed self check. Training masks keep the one-scan-per-step cache.
Tests: 7 new tests in
tests/test_large_head_attention.py: benchmarker installed, self-check OOM, fresh autotune adds under 32 MB, router OOM fallback, no scan for inference masks or after a failed check, no compile before sm80, and no install when every self check fails. The first 5 fail on the current main. Router and self-check GPU tests now need sm80+. The full file plus the formatter and encoding tests give 79 passed, 2 skipped.Not covered: a Triton 3.2 run (checked against its source only), sm80+ step time, AMD (the route stays off on ROCm), and a speed comparison of the configs picked by in-place timing versus flush-based timing.