Add scaled_dot_product_attention with explicit head axis#719
Draft
CarloLucibello wants to merge 1 commit into
Draft
Add scaled_dot_product_attention with explicit head axis#719CarloLucibello wants to merge 1 commit into
CarloLucibello wants to merge 1 commit into
Conversation
Introduce `scaled_dot_product_attention` and `scaled_dot_product_attention_scores` taking inputs shaped `(head_dim, nheads, seq_len, batch...)`, PyTorch-style, with the number of heads inferred from the tensor shape rather than a `nheads` keyword. - `scaled_dot_product_attention` returns only the attention output; the scores are available separately via `scaled_dot_product_attention_scores`. - Grouped-query attention (GQA): key/value may have fewer heads than the query. - New `scale` and `is_causal` keywords; `make_causal_mask` now defaults to `dims=3`. - Deprecate the old packed-head `dot_product_attention` / `dot_product_attention_scores`, forwarding through the new implementation. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
CarloLucibello
force-pushed
the
cl/scaled-dot-product-attention
branch
from
June 10, 2026 16:12
dbbf00a to
ea8ff69
Compare
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.
Summary
Introduces a new attention interface,
scaled_dot_product_attention, taking inputs with an explicit head axis(head_dim, nheads, seq_len, batch...)as in PyTorch, and deprecates the old packed-headdot_product_attention.The number of heads is inferred from the tensor shape (
size(q, 2)), so there is nonheadskeyword anymore.What's new
scaled_dot_product_attention(q, k, v, [bias]; fdrop, mask, scale, is_causal)— returns only the attention output(v_head_dim, nheads, q_len, batch...).scaled_dot_product_attention_scores(q, k, [bias]; ...)— returns the attention weights(kv_len, q_len, nheads, batch...).scale(denominator, default√head_dim) andis_causalkeywords.make_causal_maskdefault changed todims=3to match the new layout's sequence axis.Deprecation
The old
dot_product_attention/dot_product_attention_scoreskeep working with adepwarn, forwarding through the new implementation (heads split out of the feature dim, then joined back on output). Output and scores come from a single pass, so a stochasticfdropis not applied twice.Notes / open questions
batched_mul+softmax+batched_mul) — this PR is about the interface, not a fused/flash kernel. It sets up thenheads/is_causalsurface a future NNkernels/cuDNN backend could dispatch on.scaleis kept as the denominator (matching the previous behavior) rather than PyTorch's multiplier convention — open to changing if preferred.Tests
is_causal,scale, scores↔output consistency, gradients through the GQA path.🤖 Generated with Claude Code