Skip to content

head_dim 512 attention: sm80+ only, no 256 MB autotune buffer, OOM falls back to SDPA, no per-call mask sync - #13977

Merged
danielhanchen merged 2 commits into
mainfrom
dh/hd512-autotune-oom
Oct 16, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
dh/hd512-autotune-oom

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 16, 2026 •

Copy link
Copy Markdown
Member

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.

  • The three autotuned kernels now time configs in place with CUDA events (warmup, estimate, about 10 ms of reps) and allocate nothing extra. Every config only rewrites its own outputs, so rerunning is safe. This is set as the autotuner's do_bench attribute, which works on Triton 3.2 (where autotune() does not forward do_bench=) through 3.7. OutOfResources handling for configs that do not fit is unchanged.
  • An OOM in the self check now disables the kernel for that device and dtype (SDPA, as before) instead of failing the load. An OOM in the forward kernel falls back to SDPA for that call.
  • Self check peak: 270,408,704 to 8,917,504 bytes. The G = 8 call under the 128 MB cap now runs forward and backward on the kernel.

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.

…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.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 16, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-16T08:07:59.312549Z 9543f98 Manual request
🔒 Security Review ⚠️ Failed 2026-10-16T07:23:26.126634Z 9543f98 PR opened
ℹ️ 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.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

Reviewed commit: 9543f9896d

ℹ️ 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 changed the title head_dim 512 attention: no 256 MB autotune buffer, OOM falls back to SDPA, no per-call mask sync head_dim 512 attention: sm80+ only, no 256 MB autotune buffer, OOM falls back to SDPA, no per-call mask sync Oct 16, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Breezy!

Reviewed commit: 9543f9896d

ℹ️ 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 1574001 into main Oct 16, 2026
@danielhanchen
danielhanchen deleted the dh/hd512-autotune-oom branch October 16, 2026 11:17
danielhanchen added a commit to Etherll/unsloth that referenced this pull request Oct 19, 2026
… 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.
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