Repository navigation
Rotate the decoded query at its cache position in RoPE attention - #5655
Open
vineethsaivs wants to merge 1 commit into
Open
vineethsaivs wants to merge 1 commit into
vineethsaivs wants to merge 1 commit into
Conversation
During MultiHeadAttention decode the query is one token, but dot_product_attention_with_rope rotated it with the default positions, so every step used position 0 while the cached keys held positions 0 to max_len - 1. Outputs drifted from the full causal pass from the second token on, and input_positions could not work around it because the vmap mapped the positions over their sequence axis. Map RoPE over heads only, apply input_positions to the key only when it has the query's length, and pass the cache index as the query position during decode when the attention_fn takes input_positions.
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.
What does this PR do?
nnx.MultiHeadAttentiondecoding withdot_product_attention_with_ropegives wrong outputs from the second token on. The query is one token per step but was rotated with the default positions, so it always sat at position 0 while the cached keys held positions 0 tomax_len - 1. Token-by-token decode differed from the full causal pass by[0, 0.73, 1.25, 0.33, 0.80, 0.51], andinput_positionscould not work around it (vmap got inconsistent sizes).Fix: map RoPE over heads only, apply
input_positionsto the key only when it has the query's length, and during decode pass the cache index as the query position when theattention_fntakesinput_positions. Non-decode outputs and default-attention_fndecode are bit-identical to main.The vmap
in_axeschange is the same one as #5537 (open since July, no review yet), which decode also needs. Same class of decode bug as #5592.The new
TestRoPEtest fails on main (max diff 1.25) and passes here;attention_test.py: 128 passed. Also checked undernnx.jit, GQA andnnx.view(max error 1.8e-7).Checklist