Skip to content

Add scaled_dot_product_attention with explicit head axis#719

Draft
CarloLucibello wants to merge 1 commit into
masterfrom
cl/scaled-dot-product-attention
Draft

Add scaled_dot_product_attention with explicit head axis#719
CarloLucibello wants to merge 1 commit into
masterfrom
cl/scaled-dot-product-attention

Conversation

@CarloLucibello

@CarloLucibello CarloLucibello commented Jun 10, 2026

Copy link
Copy Markdown
Member

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-head dot_product_attention.

The number of heads is inferred from the tensor shape (size(q, 2)), so there is no nheads keyword 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...).
  • Grouped-query attention (GQA): key/value may have fewer heads than the query (number of query heads must be divisible by the number of kv heads).
  • New scale (denominator, default √head_dim) and is_causal keywords.
  • make_causal_mask default changed to dims=3 to match the new layout's sequence axis.

Deprecation

The old dot_product_attention / dot_product_attention_scores keep working with a depwarn, 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 stochastic fdrop is not applied twice.

Notes / open questions

  • This is a API change with deprecations (new exported names, old ones deprecated).
  • The kernel is unchanged (batched_mul + softmax + batched_mul) — this PR is about the interface, not a fused/flash kernel. It sets up the nheads/is_causal surface a future NNkernels/cuDNN backend could dispatch on.
  • scale is kept as the denominator (matching the previous behavior) rather than PyTorch's multiplier convention — open to changing if preferred.

Tests

🤖 Generated with Claude Code

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