Repository navigation
vulkan: size TurboQuant shared staging via specialization constants (fixes attn_prefill_256 >64KB smem) - #120
ybenyedder wants to merge 1 commit into
Conversation
…fixes attn_prefill_256 >64KB smem)
| shared float16_t tq_q[4][16][256]; // Q staging, reused for the inverse WHT output (warp, row, dim) | ||
| const uint TQ_Q_W = (TQ_K || TQ_V) ? 4u : 1u; | ||
| const uint TQ_Q_R = (TQ_K || TQ_V) ? 16u : 1u; | ||
| const uint TQ_Q_D = (TQ_K || TQ_V) ? HD : 1u; |
There was a problem hiding this comment.
🟠 High · Correctness · 1 of 7
Array sizes use TQ_K/TQ_V, which may be specialization constants that are not valid SPIR-V array lengths. If TQ_K or TQ_V is a layout(constant_id) specialization constant, the ternary yields a non-constant expression. SPIR-V requires OpTypeArray lengths to be OpConstant, so shader compilation or pipeline creation fails for every kv mode, not just TurboQuant.
Prompt for your AI agent
File: src/vulkan/shaders/attn_prefill.glsl
Line: 27
Issue: Array sizes use TQ_K/TQ_V, which may be specialization constants that are not valid SPIR-V array lengths. If TQ_K or TQ_V is a layout(constant_id) specialization constant, the ternary yields a non-constant expression. SPIR-V requires OpTypeArray lengths to be OpConstant, so shader compilation or pipeline creation fails for every kv mode, not just TurboQuant.
Make the smallest change that fixes this, add a test, and change nothing unrelated.
Reply /dismiss (optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.
| shared float16_t tq_q[4][16][256]; // Q staging, reused for the inverse WHT output (warp, row, dim) | ||
| const uint TQ_Q_W = (TQ_K || TQ_V) ? 4u : 1u; | ||
| const uint TQ_Q_R = (TQ_K || TQ_V) ? 16u : 1u; | ||
| const uint TQ_Q_D = (TQ_K || TQ_V) ? HD : 1u; |
There was a problem hiding this comment.
🟠 High · Correctness · 2 of 7
Non-TQ builds size tq_q to 1 element, but any unguarded access to it goes out of bounds. When TQ_K and TQ_V are both off, tq_q is declared [1][1][1]. If the shared Q-staging or WHT-output code runs outside a TQ guard, as the old 4x16x256 buffer allowed, it writes past the array and corrupts other shared variables or reads garbage.
Prompt for your AI agent
File: src/vulkan/shaders/attn_prefill.glsl
Line: 27
Issue: Non-TQ builds size tq_q to 1 element, but any unguarded access to it goes out of bounds. When TQ_K and TQ_V are both off, tq_q is declared [1][1][1]. If the shared Q-staging or WHT-output code runs outside a TQ guard, as the old 4x16x256 buffer allowed, it writes past the array and corrupts other shared variables or reads garbage.
Make the smallest change that fixes this, add a test, and change nothing unrelated.
Reply /dismiss (optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.
| shared float tq_red[256]; | ||
| shared float tq_red[(TQ_K || TQ_V) ? 256u : 1u]; | ||
| // Per-128-group sum, in the same butterfly order as CUDA tq_group_sum. `slot` must be unique per (row, group) | ||
| // within the workgroup; every invocation of that group calls this with the same slot. |
There was a problem hiding this comment.
🟠 High · Correctness · 3 of 7
tq_red shrinks to 1 element when TQ is off, but reductions may still run unguarded. Non-TurboQuant kernels that call the group-sum helper with a slot index above 0 index past the single-element tq_red. The helper's butterfly reduction then reads and writes out of bounds on shared memory.
Prompt for your AI agent
File: src/vulkan/shaders/tq.glsl
Line: 100
Issue: tq_red shrinks to 1 element when TQ is off, but reductions may still run unguarded. Non-TurboQuant kernels that call the group-sum helper with a slot index above 0 index past the single-element tq_red. The helper's butterfly reduction then reads and writes out of bounds on shared memory.
Make the smallest change that fixes this, add a test, and change nothing unrelated.
Reply /dismiss (optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.
| fn attn_prefill_non_turbo_fits_48kb() { | ||
| for kernel in ["attn_prefill_64", "attn_prefill_128", "attn_prefill_256"] { | ||
| for mode in 0..=3u32 { | ||
| let bytes = shared_bytes_split(kernel, &[], mode, mode).unwrap(); |
There was a problem hiding this comment.
🔵 Low · Maintainability · 4 of 7
Test named 'non_turbo' iterates kv modes 0..=3, which likely include turbo modes. If modes 1 to 3 enable TurboQuant, the test asserts the non-turbo budget on turbo configurations and either passes for the wrong reason or fails on a valid layout. The test does not verify what its name claims.
Prompt for your AI agent
File: src/vulkan/spv_smem.rs
Line: 378
Issue: Test named 'non_turbo' iterates kv modes 0..=3, which likely include turbo modes. If modes 1 to 3 enable TurboQuant, the test asserts the non-turbo budget on turbo configurations and either passes for the wrong reason or fails on a valid layout. The test does not verify what its name claims.
Make the smallest change that fixes this, add a test, and change nothing unrelated.
Reply /dismiss (optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.
| fn attn_prefill_non_turbo_fits_48kb() { | ||
| for kernel in ["attn_prefill_64", "attn_prefill_128", "attn_prefill_256"] { | ||
| for mode in 0..=3u32 { | ||
| let bytes = shared_bytes_split(kernel, &[], mode, mode).unwrap(); |
There was a problem hiding this comment.
🔵 Low · Maintainability · 5 of 7
Test passes &[] defines, so the TurboQuant sizing that this PR changes is never exercised. A regression that re-inflates the TQ staging buffer above 64KB for attn_prefill_256 under TQ_K or TQ_V passes CI, because the only size check runs with TQ defines disabled. The reported bug can come back unnoticed.
Prompt for your AI agent
File: src/vulkan/spv_smem.rs
Line: 378
Issue: Test passes &[] defines, so the TurboQuant sizing that this PR changes is never exercised. A regression that re-inflates the TQ staging buffer above 64KB for attn_prefill_256 under TQ_K or TQ_V passes CI, because the only size check runs with TQ defines disabled. The reported bug can come back unnoticed.
Make the smallest change that fixes this, add a test, and change nothing unrelated.
Reply /dismiss (optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.
| shared float rm[4 * 16]; // per row: the final max | ||
| shared float16_t tq_q[4][16][256]; // Q staging, reused for the inverse WHT output (warp, row, dim) | ||
| const uint TQ_Q_W = (TQ_K || TQ_V) ? 4u : 1u; | ||
| const uint TQ_Q_R = (TQ_K || TQ_V) ? 16u : 1u; |
There was a problem hiding this comment.
🔵 Low · Maintainability · 6 of 7
The condition (TQ_K || TQ_V) is repeated in four places instead of being defined once. A future change to the enable condition, such as adding a third TQ mode, must be edited in several places. Missing one leaves an array sized for one mode and indexed for another, which causes silent out-of-bounds access.
Prompt for your AI agent
File: src/vulkan/shaders/attn_prefill.glsl
Line: 26
Issue: The condition (TQ_K || TQ_V) is repeated in four places instead of being defined once. A future change to the enable condition, such as adding a third TQ mode, must be edited in several places. Missing one leaves an array sized for one mode and indexed for another, which causes silent out-of-bounds access.
Make the smallest change that fixes this, add a test, and change nothing unrelated.
Reply /dismiss (optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.
| shared float16_t tq_q[4][16][256]; // Q staging, reused for the inverse WHT output (warp, row, dim) | ||
| const uint TQ_Q_W = (TQ_K || TQ_V) ? 4u : 1u; | ||
| const uint TQ_Q_R = (TQ_K || TQ_V) ? 16u : 1u; | ||
| const uint TQ_Q_D = (TQ_K || TQ_V) ? HD : 1u; |
There was a problem hiding this comment.
🔵 Low · Maintainability · 7 of 7
The fix shrinks one staging buffer by conditional sizing instead of reusing existing shared storage. The turbo path still needs a 4x16xHD float16 staging area in addition to ss and rm. Adding more turbo features will push it past 64KB again, because the duplication was kept rather than aliased with an existing shared array.
Prompt for your AI agent
File: src/vulkan/shaders/attn_prefill.glsl
Line: 27
Issue: The fix shrinks one staging buffer by conditional sizing instead of reusing existing shared storage. The turbo path still needs a 4x16xHD float16 staging area in addition to ss and rm. Adding more turbo features will push it past 64KB again, because the duplication was kept rather than aliased with an existing shared array.
Make the smallest change that fixes this, add a test, and change nothing unrelated.
Reply /dismiss (optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.
Aletheore Flash Lite reviewWhat this PR does. This pull request changes the Vulkan attention prefill shader so that the TurboQuant Q-staging and reduction shared arrays are sized to zero-ish (1 element) unless TurboQuant K or V mode is enabled, via specialization constants, to stop the attn_prefill_256 kernel from exceeding 64KB of shared memory when TurboQuant is off. It also adds a Rust test asserting that non-TurboQuant prefill kernels fit within 48KB of shared memory across all KV modes.
7 finding(s) posted as inline review comment(s).
Review detailsReviewed 3 file(s); skipped 6. Dismissed 0; outside PR diff 0. Grounding: 7 of 7 proposed finding(s). Fix all with your agentNote: 6 of 9 changed file(s) were not included in this review (the review reads at most 30 files, and skips any file over 100KB). Issues in them would not be found, and citations pointing into them could not be checked against the real file: |
🔍 Aletheore evidence diffgraph LR
n0["spv_smem.rs"]
n1["mod.rs"]
n2["preflight.rs"]
n3["backend.rs"]
n0 --> n1
n0 --> n2
n0 --> n3
classDef changed fill:#d9772c,color:#fff,stroke:#b85f1c;
classDef changedFinding fill:#c0392b,color:#fff,stroke:#922b21;
classDef dependent fill:#f2f0ec,color:#18140f,stroke:#d6d0c5;
class n0 changed
class n1 dependent
class n2 dependent
class n3 dependent
What changed
Aggregate deltas
|
|
Hi, ty! The tq_red part is in main now (c4f13a8) with you as the author, and your test too. The tq_q part was already fixed the same way in 1.4.0, so I close this one. Thanks a lot for finding it on the 890M. |
tq.glsl comes into every shader through common.glsl, and its 1 KB reduction array was declared in all of them. It is now sized by the TQ_K/TQ_V specialization constants, as tq_q already is. Test attn_prefill_non_turbo_fits_48kb checks that attn_prefill_64/128/256 fit 48 KB for f16, q8, q4 and q2.
Problem
After PR #84 (
2d457b4),attn_prefill_256.spvdeclaredshared float16_t tq_q[4][16][256](32,768 B) andtq.glsldeclaredshared float tq_red[256](1,024 B) unconditionally, pushingattn_prefill_256to 80,384 bytes of Workgroup shared memory even when TurboQuant is not used (f16/q8/q4/q2).On Vulkan devices with
maxComputeSharedMemorySize = 65536(e.g. AMD Radeon 890M RDNA 3.5), this caused everyhead_dim = 256model (such asQwen3.6-35B-A3B) to fail at prefill with:Fix
tq_qinsrc/vulkan/shaders/attn_prefill.glslusing theTQ_K || TQ_Vspecialization constants ([4][16][HD]when TurboQuant is active,[1][1][1]otherwise).tq_redinsrc/vulkan/shaders/tq.glslto256whenTQ_K || TQ_V,1otherwise.attn_prefill_{64,128,256}.spv,ink_clip.spv,kv_store.spv,rms_norm.spv).spv_smem::tests::attn_prefill_non_turbo_fits_48kbensuringattn_prefill_{64,128,256}stay<= 49152bytes for all non-TurboQuant KV modes (0..=3).Verification
cargo test --release --lib --locked -- spv_smem: 4/4 passed.Qwen3.6-35B-A3B-UD-IQ4_XS(--kv q4 --ctx 262144) on AMD Radeon 890M Vulkan (maxComputeSharedMemorySize = 65536): HTTP 200/v1/chat/completionsverified.