Repository navigation
vulkan: size TurboQuant shared staging via specialization constants (fixes attn_prefill_256 >64KB smem) #120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,7 +22,10 @@ shared float ss[4 * 16 * BK]; // scores of each subgroup; at the start the | |
| shared float16_t ps[4 * 16 * PP]; // softmax weights of each subgroup | ||
| shared float rs[4 * 16]; // per row: the rescale factor, at the end the sum l | ||
| 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; | ||
| const uint TQ_Q_D = (TQ_K || TQ_V) ? HD : 1u; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟠 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 agentReply There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟠 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 agentReply There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔵 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 agentReply |
||
| shared float16_t tq_q[TQ_Q_W][TQ_Q_R][TQ_Q_D]; // Q staging, reused for the inverse WHT output (warp, row, dim) | ||
| float tq_kget(uint row, uint col) { | ||
| uvec4 u = kvs[row * PQ + col / 8u]; | ||
| return unpackFloat2x16(u[(col % 8u) / 2u])[col % 2u]; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -95,7 +95,7 @@ float tq_c2(int q) { bool p = q >= 2; float m = p ? 0.133462 : 0.039994; return | |
| int tq_nearest(uint bits, float v) { return bits == 3u ? tq_q3(v) : bits == 2u ? tq_q2(v) : tq_q4(v); } | ||
| float tq_centroid(uint bits, int q) { return bits == 3u ? tq_c3(q) : bits == 2u ? tq_c2(q) : tq_c4(q); } | ||
|
|
||
| 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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟠 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 agentReply |
||
| float tq_group_reduce(float cs, uint slot) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -370,4 +370,14 @@ mod tests { | |
| assert!(worst_v100.0 <= 49152, "{} needs {} bytes of Workgroup memory with cooperative matrices off", | ||
| worst_v100.1, worst_v100.0); | ||
| } | ||
|
|
||
| #[test] | ||
| 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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔵 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 agentReply There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔵 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 agentReply |
||
| assert!(bytes <= 49152, "{kernel} with kv_mode={mode} needs {bytes} bytes (> 49152)"); | ||
| } | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔵 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
Reply
/dismiss(optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.