Conversation
Adds `Axon.Layers.scaled_dot_product_attention/4` and the graph-level
`Axon.scaled_dot_product_attention/4`.
The functional kernel computes `softmax(Q · Kᵀ * scale + mask) · V` over
rank-4 inputs, and supports:
* `:axes` - `:bshd` (default) or `:bhsd` input layouts
* `:scale` - defaults to `1 / sqrt(head_dim)`
* `:mask` - `:causal`, `{:causal, offset}`, `{:sliding_window, w}`,
`{:sliding_window, w, offset}`, or a runtime boolean/float tensor
* `:dropout_rate` - attention-probability dropout in `:train` mode
* `:return_attention_weights`
* grouped-query and multi-query attention, by repeating key/value
heads to match the query
The graph layer wires a tensor mask in as an extra input and, when
dropout is enabled, adds a PRNG key state parameter so the key is
threaded across steps. Mask tensors and dropout keys are passed
positionally, which is why the kernel has 4-, 5- and 6-arity heads.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
polvalente
reviewed
Aug 16, 2026
| maybe_attention_dropout(weights, dropout_key, opts[:dropout_rate], opts[:mode]) | ||
|
|
||
| output = Nx.dot(weights, [3], [0, 1], value, [2], [0, 1]) | ||
| output = from_bhsd(output, opts[:axes]) |
Member
There was a problem hiding this comment.
Can't we avoid transposes by using the correct reduce axes in dot directly?
This branch has not been deployed
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.
No description provided.