fix MHA KV-cache allocation and singleton mask broadcast across backends - #6869
Open
futz12 wants to merge 3 commits into
Open
fix MHA KV-cache allocation and singleton mask broadcast across backends#6869futz12 wants to merge 3 commits into
futz12 wants to merge 3 commits into
Conversation
Keep persistent x86 KV caches on the blob allocator, broadcast single-channel attention masks across heads on CPU and Vulkan paths, and add long multi-token cache regressions.
Use the blob allocator for persistent concatenated K/V caches and broadcast singleton 3D attention masks from channel zero on all architecture-specific MHA paths.
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.
Summary
This PR fixes persistent KV-cache allocation and singleton 3D attention-mask broadcasting across the generic, x86, ARM, MIPS,
LoongArch, and Vulkan attention implementations.
It also fixes the equivalent singleton-mask issue in Vulkan SDPA.
Problems
Persistent MHA KV caches used the wrong allocator
The architecture-specific MHA implementations concatenated cached and current K/V tensors with:
These tensors are returned through top_blobs[1] and top_blobs[2] and persist across subsequent decoding steps. They must
therefore use the caller-provided blob allocator.
Affected optimized implementations:
The concatenated caches are now created with:
k_affine.create(dst_seqlen, embed_dim, elemsize, opt.blob_allocator);
v_affine.create(dst_seqlen, embed_dim, elemsize, opt.blob_allocator);
Singleton 3D masks were treated as per-head masks
An attention mask with shape:
(dst_seqlen, src_seqlen, 1)
is intended to be broadcast across every attention head.
Several implementations treated every 3D mask as a per-head mask and selected:
attn_mask_blob.channel(head)
For head > 0, this accesses a channel that does not exist.
Affected paths included:
Singleton masks now always select channel zero, while masks with multiple channels retain their per-head behavior.
Vulkan mask indexing
The Vulkan SDPA shaders use the following addressing logic:
mask_head = attn_mask_dims == 3 ? head : 0;
mask_index = mask_head * mask_cstep + row * dst_seqlen + column;
Previously, a dims=3, c=1 mask was still passed to the shader as a 3D per-head mask. Every head greater than zero therefore
read beyond the only allocated mask channel.
This was reproduced on an NVIDIA RTX 4060:
past_seqlen=2560
num_heads=4
mask_channels=1
value not match at c:1 h:0 w:1
expect 0.016805 but got 0.015332
The old implementation failed with exit code 1. After treating a singleton 3D mask as a 2D broadcast mask, the same test passes
with exit code 0.
Vulkan MHA additionally keeps 3D masks unpacked so that head packing cannot be confused with query-position packing.
Regression coverage
The MHA and SDPA tests now cover:
Verification
The following tests pass:
test_multiheadattention_kvcache CPU: PASS
test_sdpa_kvcache CPU: PASS
test_multiheadattention_block_quant CPU: PASS
test_multiheadattention_kvcache Vulkan: PASS
test_sdpa_kvcache Vulkan: PASS