Skip to content

Vulkan: enable TurboQuant and fix prefill/smem issues - #118

Open
sayampy wants to merge 1 commit into
RustingStudio:mainfrom
sayampy:fix/vulkan-turbo
Open

sayampy wants to merge 1 commit into
RustingStudio:mainfrom
sayampy:fix/vulkan-turbo

Conversation

@sayampy

@sayampy sayampy commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Re-enable TurboQuant on Vulkan after the three issues found after the first merge.

Fixes

attn_prefill_tile.comp rotated Q before loading it, so the non-coop tiled fallback used uninitialized values and never rotated the loaded Q. The rotation now runs after the load.

kv_store.comp declared 128-element shared rows but 256 threads wrote into them, and each 128-value group rotated using an out-of-range base. The rows are now [2][128] and each half rotates its own row. This matters for head_dim 256, where one workgroup handles two groups.

attn_prefill.glsl staged Q in a fixed [4][16][256] array. Sizing it by HD cuts head_dim 128 prefill from 64000 B of workgroup memory to 47616 B, under the 49152 B limit of the smaller Vulkan devices. The gate on STRATA_VK_TURBO is removed and the previously ignored head_dim 128 test is active again.

Verification

cargo test --release --lib --test backend --test vulkan_split_kv is green: 159 lib tests, 10 backend tests and 3 Vulkan split-KV tests. New coverage checks the non-coop tiled prefill at head_dim 128 and 256, and asserts the committed head_dim 128 prefill SPIR-V stays at 47616 B. Prefill versus decode relative L2 is 1.88e-7 tiled at head_dim 128, 1.56e-7 tiled at head_dim 256, 4.32e-4 cooperative at head_dim 128, 3.17e-4 for K=turbo4 V=f16, 3.04e-4 for K=f16 V=turbo4, and 2.43e-7 for the Inkling 2 head_dim 64 path. Legacy formats remain byte-identical to CUDA.

The original quality numbers still stand: on Tiel Coder 35B A3B over 511 scored positions, turbo4 has PPL 5.8122, top-1 agreement 90.80% and mean KLD 0.0361, against q4 at 5.8526, 89.04% and 0.0429 with the same cache size.

Rotate Q after loading it in the non-coop tiled prefill kernel. Give the two 128-value groups of kv_store their own shared-memory rows. Size the cooperative prefill Q staging by HD instead of a fixed 256, cutting hd 128 prefill from 64000 B to 47616 B. Remove the STRATA_VK_TURBO gate, unignore the hd 128 test, and add tiled hd 128/256 and smem-limit coverage.
Comment thread src/vulkan/mod.rs
@@ -369,10 +369,6 @@ impl Vk {
/// The usable GPU with index `device` (`backend::device_order`). Fails with "Vulkan init" (and the reasons) when there is none.
pub fn new(device: usize) -> anyhow::Result<Vk> {
let (kv_k_mode, kv_v_mode) = (crate::cuda::kv_k_mode(), crate::cuda::kv_v_mode());

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 6

Turbo KV is now enabled on Vulkan unconditionally, though the removed comment recorded wrong output on Qwen3-4B. Users running --kv turbo* on Vulkan with a model like Qwen3-4B get garbled text, because the only evidence of the fix is a non-coop test that is opt-in. The default coop prefill path has no test, so the removed guard now lets the known bad output reach users.

Prompt for your AI agent
File: src/vulkan/mod.rs
Line: 371
Symbol: new
Issue: Turbo KV is now enabled on Vulkan unconditionally, though the removed comment recorded wrong output on Qwen3-4B. Users running --kv turbo* on Vulkan with a model like Qwen3-4B get garbled text, because the only evidence of the fix is a non-coop test that is opt-in. The default coop prefill path has no test, so the removed guard now lets the known bad output reach users.
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 tests/vulkan_split_kv.rs
assert!(rel_l2(&got, &want) < 5e-4, "vulkan turbo hd64 prefill vs decode");
}

/// Exercises the non-coop tiled prefill path, where Q used to be rotated before it was loaded.

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 · 2 of 6

The new tiled-prefill test covers only the non-coop path, so default prefill correctness is unverified. The default coop prefill (attn_prefill.glsl) still uses the Q-rotation ordering the tile shader had, or is wrong for turbo. The tests pass with STRATA_VK_NO_COOP=1 while real turbo prefill on the default path still produces wrong output.

Prompt for your AI agent
File: tests/vulkan_split_kv.rs
Line: 171
Symbol: vulkan_inkling_turbo_hd64
Issue: The new tiled-prefill test covers only the non-coop path, so default prefill correctness is unverified. The default coop prefill (attn_prefill.glsl) still uses the Q-rotation ordering the tile shader had, or is wrong for turbo. The tests pass with STRATA_VK_NO_COOP=1 while real turbo prefill on the default path still produces wrong output.
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.

acc[j] = 0.0;
}
// Rotate after loading: rotating first would scramble uninitialized values and leave Q unrotated.
if (TQ_K) rotate_q(qv, lane);

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 6

Q is rotated right after shared-memory loads, which may need a barrier before rotate_q reads the staged values. If rotate_q reads Q values written to shared memory by other lanes during the load, a lane can read a stale value because no barrier separates the writes from the rotation. The result is nondeterministic, wrong Q rotation on some GPUs.

Prompt for your AI agent
File: src/vulkan/shaders/attn_prefill_tile.comp
Line: 54
Issue: Q is rotated right after shared-memory loads, which may need a barrier before rotate_q reads the staged values. If rotate_q reads Q values written to shared memory by other lanes during the load, a lane can read a stale value because no barrier separates the writes from the rotation. The result is nondeterministic, wrong Q rotation on some GPUs.
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
}

/// The turbo hd128 prefill staging must fit the smallest supported workgroup limit (49152 B).
#[test]

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 6

The shared-memory test checks only hd128; the hd256 turbo prefill is not checked against the 49152 B limit. With the Vulkan turbo gate removed, hd256 turbo prefill can exceed 49152 B on an RTX 3060-class device. The hd128 test still passes, so the regression reaches users as a pipeline creation failure.

Prompt for your AI agent
File: src/vulkan/spv_smem.rs
Line: 325
Symbol: shared_memory_refusal_names_the_kernel_spec_and_both_sizes
Issue: The shared-memory test checks only hd128; the hd256 turbo prefill is not checked against the 49152 B limit. With the Vulkan turbo gate removed, hd256 turbo prefill can exceed 49152 B on an RTX 3060-class device. The hd128 test still passes, so the regression reaches users as a pipeline creation failure.
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 tests/vulkan_split_kv.rs
#[ignore = "turbo attn_prefill_128 needs 64000 B of shared memory, more than the 49152 of an RTX 3060 under Vulkan"]
fn vulkan_split_legacy_and_turbo() {
let _lock = common::gpu_lock();
let shape: Shape = (4, 2, 128, 16, 3);

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 · 5 of 6

Removing #[ignore] runs the heavy turbo test by default. The env vars set in the new test are not restored on panic. The new test panics inside the loop after set_var(STRATA_VK_NO_COOP). The env var is never removed, so later tests in the same process take the non-coop path and fail or pass for the wrong reason.

Prompt for your AI agent
File: tests/vulkan_split_kv.rs
Line: 90
Symbol: vulkan_split_legacy_and_turbo
Issue: Removing #[ignore] runs the heavy turbo test by default. The env vars set in the new test are not restored on panic. The new test panics inside the loop after set_var(STRATA_VK_NO_COOP). The env var is never removed, so later tests in the same process take the non-coop path and fail or pass for the wrong reason.
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
}

/// The turbo hd128 prefill staging must fit the smallest supported workgroup limit (49152 B).
#[test]

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 6

The test hard-codes the exact byte count 47616, so any unrelated shader change breaks it. Adding a small shared array to the prefill shader changes the byte count and fails this test, even though the 49152 limit is still met. Maintainers then update the constant without checking the limit.

Prompt for your AI agent
File: src/vulkan/spv_smem.rs
Line: 325
Symbol: shared_memory_refusal_names_the_kernel_spec_and_both_sizes
Issue: The test hard-codes the exact byte count 47616, so any unrelated shader change breaks it. Adding a small shared array to the prefill shader changes the byte count and fails this test, even though the 49152 limit is still met. Maintainers then update the constant without checking the limit.
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 PR enables TurboQuant KV-cache modes (turbo*) on the Vulkan backend by removing the guard that refused them, and fixes several related bugs: Q is now rotated after loading in the tiled prefill shader, the KV store shader's WHT staging is split per workgroup group, and the prefill Q staging array is sized by HD.

Merge confidence Review effort Merge risk
3 / 5 2 / 5 Medium
Area Files Lines
src/vulkan 5 +23 −17
tests 1 +18 −2

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

# Severity Category Where
1 🟠 High Correctness mod.rs:371 Turbo KV is now enabled on Vulkan unconditionally, though the removed comment recorded wrong output on…
2 🔵 Low Maintainability vulkan_split_kv.rs:171 The new tiled-prefill test covers only the non-coop path, so default prefill correctness is unverified. The…
3 🟠 High Correctness attn_prefill_tile.comp:54 Q is rotated right after shared-memory loads, which may need a barrier before rotate_q reads the staged…
4 🔵 Low Maintainability spv_smem.rs:325 The shared-memory test checks only hd128; the hd256 turbo prefill is not checked against the 49152 B limit…
5 🔵 Low - vulkan_split_kv.rs:90 Removing #[ignore] runs the heavy turbo test by default. The env vars set in the new test are not restored on…
6 🔵 Low Maintainability spv_smem.rs:325 The test hard-codes the exact byte count 47616, so any unrelated shader change breaks it. Adding a small…
Review details

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

Fix all with your agent
File: src/vulkan/mod.rs
Line: 371
Symbol: new
Issue: Turbo KV is now enabled on Vulkan unconditionally, though the removed comment recorded wrong output on Qwen3-4B. Users running --kv turbo* on Vulkan with a model like Qwen3-4B get garbled text, because the only evidence of the fix is a non-coop test that is opt-in. The default coop prefill path has no test, so the removed guard now lets the known bad output reach users.
Make the smallest change that fixes this, add a test, and change nothing unrelated.

File: tests/vulkan_split_kv.rs
Line: 171
Symbol: vulkan_inkling_turbo_hd64
Issue: The new tiled-prefill test covers only the non-coop path, so default prefill correctness is unverified. The default coop prefill (attn_prefill.glsl) still uses the Q-rotation ordering the tile shader had, or is wrong for turbo. The tests pass with STRATA_VK_NO_COOP=1 while real turbo prefill on the default path still produces wrong output.
Make the smallest change that fixes this, add a test, and change nothing unrelated.

File: src/vulkan/shaders/attn_prefill_tile.comp
Line: 54
Issue: Q is rotated right after shared-memory loads, which may need a barrier before rotate_q reads the staged values. If rotate_q reads Q values written to shared memory by other lanes during the load, a lane can read a stale value because no barrier separates the writes from the rotation. The result is nondeterministic, wrong Q rotation on some GPUs.
Make the smallest change that fixes this, add a test, and change nothing unrelated.

File: src/vulkan/spv_smem.rs
Line: 325
Symbol: shared_memory_refusal_names_the_kernel_spec_and_both_sizes
Issue: The shared-memory test checks only hd128; the hd256 turbo prefill is not checked against the 49152 B limit. With the Vulkan turbo gate removed, hd256 turbo prefill can exceed 49152 B on an RTX 3060-class device. The hd128 test still passes, so the regression reaches users as a pipeline creation failure.
Make the smallest change that fixes this, add a test, and change nothing unrelated.

File: tests/vulkan_split_kv.rs
Line: 90
Symbol: vulkan_split_legacy_and_turbo
Issue: Removing #[ignore] runs the heavy turbo test by default. The env vars set in the new test are not restored on panic. The new test panics inside the loop after set_var(STRATA_VK_NO_COOP). The env var is never removed, so later tests in the same process take the non-coop path and fail or pass for the wrong reason.
Make the smallest change that fixes this, add a test, and change nothing unrelated.

File: src/vulkan/spv_smem.rs
Line: 325
Symbol: shared_memory_refusal_names_the_kernel_spec_and_both_sizes
Issue: The test hard-codes the exact byte count 47616, so any unrelated shader change breaks it. Adding a small shared array to the prefill shader changes the byte count and fails this test, even though the 49152 limit is still met. Maintainers then update the constant without checking the limit.
Make the smallest change that fixes this, add a test, and change nothing unrelated.

Note: 4 of 10 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_64.spv, src/vulkan/spv/attn_prefill_tile.spv, src/vulkan/spv/kv_store.spv.

@aletheore

aletheore Bot commented Nov 26, 2026

Copy link
Copy Markdown

🔍 Aletheore evidence diff

graph LR
    n0["src/vulkan/mod.rs"]
    n1["spv_smem.rs"]
    n2["src/backend.rs"]
    n3["lib.rs"]
    n4["preflight.rs"]
    n5["tests/backend.rs"]
    n6["tests/common/mod.rs"]
    n7["gpu_model.rs"]
    n8["kernels.rs"]
    n9["kv_q8.rs"]
    n10["llama_numerics.rs"]
    n0 --> n2
    n0 --> n3
    n0 --> n4
    n0 --> n5
    n0 --> n6
    n0 --> n7
    n0 --> n8
    n0 --> n9
    n0 --> n10
    n1 --> n4
    n1 --> n5
    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 changed
    class n2 dependent
    class n3 dependent
    class n4 dependent
    class n5 dependent
    class n6 dependent
    class n7 dependent
    class n8 dependent
    class n9 dependent
    class n10 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/mod.rs · modified · +0/-4 · 9 other dependents
  • src/vulkan/shaders/attn_prefill.glsl · modified · +1/-1
  • src/vulkan/shaders/attn_prefill_tile.comp · modified · +2/-1
  • src/vulkan/shaders/kv_store.comp · modified · +12/-11
  • src/vulkan/spv/attn_prefill_128.spv · modified
  • src/vulkan/spv/attn_prefill_64.spv · modified
  • src/vulkan/spv/attn_prefill_tile.spv · modified
  • src/vulkan/spv/kv_store.spv · modified
  • src/vulkan/spv_smem.rs · modified · +8/-0 · +1 function · 2 other dependents
  • tests/vulkan_split_kv.rs · modified · +18/-2 · +1 function

Aggregate deltas

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

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.

1 participant