Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/vulkan/shaders/attn_prefill.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Copy link
Copy Markdown

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
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.

const uint TQ_Q_D = (TQ_K || TQ_V) ? HD : 1u;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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 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.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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 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.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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 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.

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];
Expand Down
2 changes: 1 addition & 1 deletion src/vulkan/shaders/tq.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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 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.

float tq_group_reduce(float cs, uint slot) {
Expand Down
Binary file modified src/vulkan/spv/attn_prefill_128.spv
Binary file not shown.
Binary file modified src/vulkan/spv/attn_prefill_256.spv
Binary file not shown.
Binary file modified src/vulkan/spv/attn_prefill_64.spv
Binary file not shown.
Binary file modified src/vulkan/spv/ink_clip.spv
Binary file not shown.
Binary file modified src/vulkan/spv/kv_store.spv
Binary file not shown.
Binary file modified src/vulkan/spv/rms_norm.spv
Binary file not shown.
10 changes: 10 additions & 0 deletions src/vulkan/spv_smem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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 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.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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 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.

assert!(bytes <= 49152, "{kernel} with kv_mode={mode} needs {bytes} bytes (> 49152)");
}
}
}
}
Loading