Skip to content

head_dim 512 attention: use the kernel for masks made under inference mode - #14202

Merged
danielhanchen merged 1 commit into
mainfrom
dh/hd512-inference-mask-cache
Oct 19, 2026
Merged

danielhanchen merged 1 commit into
mainfrom
dh/hd512-inference-mask-cache

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

#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.

@danielhanchen danielhanchen self-assigned this Oct 19, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 19, 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-19T05:58:26.193567Z ef6abf7 Manual request
🔒 Security Review ⚠️ Failed 2026-10-19T05:55:49.739193Z ef6abf7 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. 🎉

Reviewed commit: ef6abf7963

ℹ️ 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 9faa995 into main Oct 19, 2026
@danielhanchen
danielhanchen deleted the dh/hd512-inference-mask-cache branch October 19, 2026 10:30
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