Skip to content

vulkan: size TurboQuant shared staging via specialization constants (fixes attn_prefill_256 >64KB smem) - #120

Closed
ybenyedder wants to merge 1 commit into
RustingStudio:mainfrom
ybenyedder:fix/vulkan-tq-smem
Closed

ybenyedder wants to merge 1 commit into
RustingStudio:mainfrom
ybenyedder:fix/vulkan-tq-smem

Conversation

@ybenyedder

Copy link
Copy Markdown
Contributor

Problem

After PR #84 (2d457b4), attn_prefill_256.spv declared shared float16_t tq_q[4][16][256] (32,768 B) and tq.glsl declared shared float tq_red[256] (1,024 B) unconditionally, pushing attn_prefill_256 to 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 every head_dim = 256 model (such as Qwen3.6-35B-A3B) to fail at prefill with:

vulkan: kernel attn_prefill_256 with specialization [[]] needs 80384 bytes of shared memory; this device allows 65536 (maxComputeSharedMemorySize)

Fix

  • Size tq_q in src/vulkan/shaders/attn_prefill.glsl using the TQ_K || TQ_V specialization constants ([4][16][HD] when TurboQuant is active, [1][1][1] otherwise).
  • Size tq_red in src/vulkan/shaders/tq.glsl to 256 when TQ_K || TQ_V, 1 otherwise.
  • Regenerate the 6 affected SPIR-V modules (attn_prefill_{64,128,256}.spv, ink_clip.spv, kv_store.spv, rms_norm.spv).
  • Add unit test spv_smem::tests::attn_prefill_non_turbo_fits_48kb ensuring attn_prefill_{64,128,256} stay <= 49152 bytes for all non-TurboQuant KV modes (0..=3).

Verification

  • cargo test --release --lib --locked -- spv_smem: 4/4 passed.
  • Live Qwen3.6-35B-A3B-UD-IQ4_XS (--kv q4 --ctx 262144) on AMD Radeon 890M Vulkan (maxComputeSharedMemorySize = 65536): HTTP 200 /v1/chat/completions verified.

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;

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.

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;

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.

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.

Comment thread src/vulkan/spv_smem.rs
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.

Comment thread src/vulkan/spv_smem.rs
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 · 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;

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.

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;

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.

@aletheore

aletheore Bot commented Nov 26, 2026

Copy link
Copy Markdown

Aletheore Flash Lite review

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

Merge confidence Review effort Merge risk
2 / 5 1 / 5 Medium
Area Files Lines
src/vulkan 3 +15 −2

7 finding(s) posted as inline review comment(s).

# Severity Category Where
1 🟠 High Correctness attn_prefill.glsl:27 Array sizes use TQ_K/TQ_V, which may be specialization constants that are not valid SPIR-V array lengths. If…
2 🟠 High Correctness attn_prefill.glsl:27 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…
3 🟠 High Correctness tq.glsl:100 tq_red shrinks to 1 element when TQ is off, but reductions may still run unguarded. Non-TurboQuant kernels…
4 🔵 Low Maintainability spv_smem.rs:378 Test named 'non_turbo' iterates kv modes 0..=3, which likely include turbo modes. If modes 1 to 3 enable…
5 🔵 Low Maintainability spv_smem.rs:378 Test passes &[] defines, so the TurboQuant sizing that this PR changes is never exercised. A regression that…
6 🔵 Low Maintainability attn_prefill.glsl:26 The condition (TQ_K || TQ_V) is repeated in four places instead of being defined once. A future change to the…
7 🔵 Low Maintainability attn_prefill.glsl:27 The fix shrinks one staging buffer by conditional sizing instead of reusing existing shared storage. The…
Review details

Reviewed 3 file(s); skipped 6. Dismissed 0; outside PR diff 0. Grounding: 7 of 7 proposed finding(s).

Fix all with your 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.

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.

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.

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.

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.

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.

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.

Note: 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: src/vulkan/spv/attn_prefill_128.spv, src/vulkan/spv/attn_prefill_256.spv, src/vulkan/spv/attn_prefill_64.spv, src/vulkan/spv/ink_clip.spv, src/vulkan/spv/kv_store.spv, src/vulkan/spv/rms_norm.spv.

@aletheore

aletheore Bot commented Nov 26, 2026

Copy link
Copy Markdown

🔍 Aletheore evidence diff

graph 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
Loading

What changed
Deterministic, computed from this commit's real scan and import graph - posted on every run, whether or not Flash Review found anything.

  • src/vulkan/shaders/attn_prefill.glsl · modified · +4/-1
  • src/vulkan/shaders/tq.glsl · modified · +1/-1
  • src/vulkan/spv/attn_prefill_128.spv · modified
  • src/vulkan/spv/attn_prefill_256.spv · modified
  • src/vulkan/spv/attn_prefill_64.spv · modified
  • src/vulkan/spv/ink_clip.spv · modified
  • src/vulkan/spv/kv_store.spv · modified
  • src/vulkan/spv/rms_norm.spv · modified
  • src/vulkan/spv_smem.rs · modified · +10/-0 · +1 function · 3 other dependents

Aggregate deltas

  • Modules: +0
  • Dependency graph edges: +0
  • Commits: 1

@Vasyl-Trefilov

Copy link
Copy Markdown
Member

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.

Vasyl-Trefilov pushed a commit that referenced this pull request Nov 26, 2026
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants