Repository navigation
Write TiledAttention outputs in token-major row order. - #1061
Merged
Merged
Conversation
copybara-service
Bot
force-pushed
the
test_999965978
branch
from
October 15, 2026 10:51
a367d25 to
87ee407
Compare
LocalAttentionForAllHeadsTokensAndBatch reads Q from row `token_idx * qbatch.Size() + qi`, and the caller adds `att_out` to `x` in that same token-major order. However, it wrote `att_out`, `softmax_d` and `softmax_max` to row `qi * num_tokens + token_idx`. These differ whenever there are several queries and several tokens per query. The dot-product (attention.cc) and flash (flash_attention.cc) paths already use token-major rows. No current caller is affected: prefill uses one query per QBatch and decode one token. Batched embeddings will be the first caller with both. Effect on Gemma 3 4B (sfp) when that case is exercised: 4 different 64-token prompts run through all layers as one QBatch vs. one at a time, comparing the next-token argmax at every position: attention batched == single cos(last-token logits) flash 61-64 / 64 >= 0.99993 flash_transposed_qs 5 / 64 0.70 - 0.87 (before) flash_transposed_qs 63-64 / 64 >= 0.99994 (after) With one query, both row orders coincide, hence single-prompt results are unchanged. The goldens of the multi-token tests (2 tokens, qbatch 2) had encoded the query-major order; their rows 1 and 2 are swapped accordingly. The test fails without the fix. PiperOrigin-RevId: 1000029819
copybara-service
Bot
force-pushed
the
test_999965978
branch
from
October 15, 2026 10:59
87ee407 to
e883fd8
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.
Write TiledAttention outputs in token-major row order.
LocalAttentionForAllHeadsTokensAndBatch reads Q from row
token_idx * qbatch.Size() + qi, and the caller addsatt_outtoxin thatsame token-major order. However, it wrote
att_out,softmax_dandsoftmax_maxto rowqi * num_tokens + token_idx. These differ whenever thereare several queries and several tokens per query. The dot-product
(attention.cc) and flash (flash_attention.cc) paths already use token-major
rows.
No current caller is affected: prefill uses one query per QBatch and decode
one token. Batched embeddings will be the first caller with both.
Effect on Gemma 3 4B (sfp) when that case is exercised: 4 different 64-token
prompts run through all layers as one QBatch vs. one at a time, comparing the
next-token argmax at every position:
attention batched == single cos(last-token logits)
flash 61-64 / 64 >= 0.99993
flash_transposed_qs 5 / 64 0.70 - 0.87 (before)
flash_transposed_qs 63-64 / 64 >= 0.99994 (after)
With one query, both row orders coincide, hence single-prompt results are
unchanged.
The goldens of the multi-token tests (2 tokens, qbatch 2) had encoded the
query-major order; their rows 1 and 2 are swapped accordingly. The test fails
without the fix.