Skip to content

head_dim 512 attention: leave mask plus is_causal to SDPA, fall back on mask-scan OOM - #14691

Merged
danielhanchen merged 6 commits into
mainfrom
large-head-attn-mask-causal
Oct 25, 2026
Merged

danielhanchen merged 6 commits into
mainfrom
large-head-attn-mask-causal

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 22, 2026 •

Copy link
Copy Markdown
Member

Two edge cases in the head_dim 512 attention router from #13869 (unsloth/kernels/large_head_attention.py). The common paths (no mask, a causal / packed / padded mask without an explicit is_causal) take the same kernel route as before; the change adds one boolean check per call (about 0.04 us).

1. Explicit is_causal=True next to a mask

transformers 4.57.6 sdpa_attention_forward keeps an explicit is_causal beside the mask, and SDPA then applies both. The router built its intervals from the mask alone and dropped the causal part, so a call carrying a non-causal mask (all-allowed, or a 4D padding mask) plus is_causal=True let early tokens attend to later ones. transformers 5.x drops is_causal whenever a mask is present, so the two versions disagree on what that pairing means. The router now hands it to the wrapped function, which keeps each version's own meaning. No transformers model passes is_causal into the attention interface, and Unsloth's own callers that pass is_causal=True drop the mask first, so this only affects model code that passes both.

2. OOM in the mask scan

intervals_from_mask allocates int32 row chunks (512 MiB at mask batch 8, length 16384) before the kernel's own OOM guard, so an OOM there escaped instead of falling back to SDPA. It now falls back like an OOM in the kernel.

The strided-view int32 bound this PR first carried landed on main in #14790 (_row_span, which bounds only the int32 row offset since batch and head offsets are int64); main's version is kept and the duplicate helper and tests were dropped in the merge.

Tests (tests/test_large_head_attention.py, both fail on main and pass here):

  • test_router_leaves_an_explicit_is_causal_beside_a_mask_to_sdpa
  • test_router_falls_back_to_sdpa_on_mask_scan_oom

Verification (B200, torch 2.13, transformers 5.19):

  • Test file, main source vs this branch with the branch's tests: 2 FIXED (fail on main, pass here), 60 unchanged passes, 0 failures on the branch.
  • Mask plus is_causal=True probe with the 4.57.6 sdpa_attention_forward semantics (repeat_kv, then SDPA with both): main returns 6.81 for row 3 where SDPA gives 2.03 (it reads future tokens, max abs diff 10.19); this branch matches SDPA exactly (max abs diff 0.0).
  • Causal mask without is_causal: one kernel call on both main and this branch, max diff 0 vs SDPA.

…ound strided offsets to int32, fall back on mask-scan OOM
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 22, 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-25T05:42:51.672305Z 6445694 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.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🎉

Reviewed commit: c8c0224f42

ℹ️ 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 self-assigned this Oct 25, 2026
@danielhanchen danielhanchen changed the title head_dim 512 attention: leave mask plus is_causal to SDPA, bound strided offsets, fall back on mask-scan OOM head_dim 512 attention: leave mask plus is_causal to SDPA, fall back on mask-scan OOM Oct 25, 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. More of your lovely PRs please.

Reviewed commit: 644569487d

ℹ️ 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 36f8f79 into main Oct 25, 2026
29 of 37 checks passed
@danielhanchen
danielhanchen deleted the large-head-attn-mask-causal branch October 25, 2026 07:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant