Repository navigation
Skip the attention kernel for single-key SDPA - #1145
Open
guerrantif wants to merge 1 commit into
Open
guerrantif wants to merge 1 commit into
guerrantif wants to merge 1 commit into
Conversation
With a single key, softmax assigns it weight one, so attention returns its value for every query. Return the value directly when no mask is given instead of launching the attention kernel, which is slow for many length-one sequences (e.g. variate attention over one variate).
guerrantif
marked this pull request as ready for review
October 15, 2026 16:15
guerrantif
requested review from
RBendias,
akihironitta,
aw471 and
rusty1s
as code owners
October 15, 2026 16:15
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.
With a single key, softmax assigns it weight one, so attention returns its value for every query.
SDPAnow returns the value directly in that case, repeated across query heads for GQA/MQA and broadcast over the batch, instead of callingF.scaled_dot_product_attention. The shortcut applies only when neitherattn_masknorseqused_key_valueis given, so a mask that blocks the key still takes the regular path.This case comes up in variate attention over a single variate, e.g. TimesFM3 univariate forecasting, which runs B × N attention problems of length one.
TimesFM3, univariate target without covariates, fp32 on an L4:
Forecasts match to fp32 rounding (max. relative difference 1.2e-5), and peak memory is unchanged.