Fix attention_block_tkg d_head>128 decode Q/K layout corruption for batch>1 - #14
Open
jimburtoft wants to merge 1 commit into
Open
Conversation
…atch>1 The n_d_tiles>1 (d_head>128) token-gen decode path in _process_head_group wrote the tiled Q/K SBUF free dim head-major ([n_d_tiles][n_heads][B][S]), but the QK-matmul consumer in attention_tkg._compute_qk_matmul reads it batch-major ([n_d_tiles][B][q_heads][S]) -- matching the d_head<=128 dst_4d layout. The two coincide only at batch==1, so every existing d256 config (all batch=1) passed while batch>=2 silently permuted heads vs batches and corrupted every batch row's attention output (~8% cos error). Write the producer batch-major to match. Adds batch>=2 d256 regression configs (were all batch=1).
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.
PR: Fix attention_block_tkg d_head>128 (n_d_tiles>1) decode Q/K layout corruption for batch > 1
Summary
The token-generation decode path in
attention_block_tkgproduces numerically wrong output ford_head > 128(n_d_tiles > 1) whenever the decode batch size B >= 2. B=1 is correct, andd_head <= 128is correct at any batch. The bug is a layout-ordering mismatch between the d-tiledQ/K producer and the QK-matmul consumer that is masked at B=1 (singleton batch dim) and therefore
invisible to every existing d256 unit-test config (all
batch=1).Root cause
_process_head_group(inattention_block_tkg.py),n_d_tiles > 1branch, writes the tiled Q/KSBUF buffer's free dimension head-major:
But
attention_tkg._compute_qk_matmulreads the same buffer batch-major for the d>128 QK^Tmoving operand:
i.e.
[n_d_tiles][B][q_heads][S]. Thed_head <= 128path writesdst_4d = [d, B, n_heads, S](batch-major), matching the consumer — hence d128 is always correct. At B=1 head-major and batch-major
coincide (the batch dim is 1), so the mismatch is invisible; at B >= 2 the Q heads are permuted relative
to the batch dimension, corrupting every batch row's attention output by ~8% (cos ~0.92; both d-tiles
equally affected because QK^T sums over d).
Fix
Write the d-tiled producer batch-major so it matches both the consumer and the d128 path:
B * S <= pmaxon this path, so B and S are small and the extra per-batch copies are cheap.Validation (trn2, SDK 2.32, neuronx-cc 2.27, vllm-neuron 0.24)
Standalone
NF.attention_decoded256 (q=8, kv=4, update_cache=False, W_out=None), per-batch dense-fp32CPU golden:
End-to-end (Gemma3-4B / Sarvam-Translate, TP1): greedy-exact 0/12 → 9/12; TP4: 1/12 → 9/12.
Test coverage added
Three
batch >= 2d256 configs added toTestRangeAttnBlk.FAST_ATTN_BLK_CFGSintest/integration/nkilib/experimental/transformer/test_attention_block_tkg.py(batch=2 q=2/kv=1;batch=2 q=8/kv=4; batch=4 q=8/kv=4). They fail on the unpatched kernel and pass after the fix. The
pre-existing d256 configs were all
batch=1, which was the coverage gap.Note on the K producer
The same
_process_head_groupbranch also produces the tiled K (K_sb); the fix applies to ituniformly. The returned-K store (
K_tkg_hbmreassembly) reads whole per-d-tile slices, so thebatch-major reordering is consistent through the K path as well.