Skip to content
Open
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
4 changes: 0 additions & 4 deletions src/vulkan/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.

// ponytail: Vulkan turbo gave wrong text on Qwen3-4B (RTX 3060) and its hd 128 prefill needs 64000 B of shared
// memory; refused until fixed, `STRATA_VK_TURBO=1` opens it for tests.
ensure!((kv_k_mode < 4 && kv_v_mode < 4) || std::env::var_os("STRATA_VK_TURBO").is_some(),
"--kv turbo* is not ready on Vulkan yet; use the CUDA backend or --kv q8/q4");
ensure!(!(crate::cuda::kv_mode() > 0 && crate::cuda::llama_numerics()), "--kv q8/q4/turbo* does not combine with --llama-numerics (its attention reads f16 KV)");
// RADV hides VK_AMD_device_coherent_memory on RDNA4 (RX 9000) unless this driconf option is set, and without
// it the GPU caches the CPU expert mailbox: MoE combines miss the worker's answer and wait until they give up.
Expand Down
2 changes: 1 addition & 1 deletion src/vulkan/shaders/attn_prefill.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ 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)
shared float16_t tq_q[4][16][HD]; // 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
3 changes: 2 additions & 1 deletion src/vulkan/shaders/attn_prefill_tile.comp
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,13 @@ void main() {
uint pos0 = uint(p.step.v[0]), last_max = pos0 + p.q_off + min(q0 + p.bq, p.t) - 1, last_q = pos0 + p.q_off + q;
uint kvd = p.nkv * HD, kvo = kvh * HD;
float qv[8], acc[8], m = 0.0, l = 0.0;
if (TQ_K) rotate_q(qv, lane);
bool first = true;
[[unroll]] for (uint j = 0; j < PER; j++) {
qv[j] = live ? p.q.v[(qr * p.nh + hr) * HD + tq_idx(j, lane, KV_MODE)] * p.scale : 0.0;
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.

for (uint k0 = 0; k0 <= last_max; k0 += BK) {
barrier();
// All threads load the tile (inactive tasks too, they still reach the barrier). Rows past the last key are
Expand Down
23 changes: 12 additions & 11 deletions src/vulkan/shaders/kv_store.comp
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,32 @@ layout(local_size_x = 256) in;
#include "common.glsl"
// Store `total` values (consecutive tokens of n each, heads of hd) of k and v into the caches from position step[0].
layout(push_constant) uniform P { F32 k; F32 v; U16 kc; U16 vc; I32 step; uint n; uint total; uint hd; } p;
shared float kq[128], vq[128];
shared float kq[2][128], vq[2][128];
// A 128-value WHT group is gathered as 4*lane+j so all five butterfly stages stay inside one warp.
void rotate(inout float s[128], uint gbase, uint lane, bool inv) {
vec4 a = vec4(s[gbase + 4u * lane], s[gbase + 4u * lane + 1u], s[gbase + 4u * lane + 2u], s[gbase + 4u * lane + 3u]);
void rotate(inout float s[128], uint lane, bool inv) {
vec4 a = vec4(s[4u * lane], s[4u * lane + 1u], s[4u * lane + 2u], s[4u * lane + 3u]);
a = tq_wht4(a, lane, inv);
s[gbase + 4u * lane] = a.x; s[gbase + 4u * lane + 1u] = a.y;
s[gbase + 4u * lane + 2u] = a.z; s[gbase + 4u * lane + 3u] = a.w;
s[4u * lane] = a.x; s[4u * lane + 1u] = a.y;
s[4u * lane + 2u] = a.z; s[4u * lane + 3u] = a.w;
}
void main() {
uint tid = gl_LocalInvocationID.x, i = gl_WorkGroupID.x * 256u + tid;
uint e = uint(p.step.v[0]) * p.n + i;
bool valid = i < p.total;
float kv = valid ? p.k.v[i] : 0.0, vv = valid ? p.v.v[i] : 0.0;
kq[tid] = kv; vq[tid] = vv;
uint g = tid >> 7u, t = tid & 127u;
kq[g][t] = kv; vq[g][t] = vv;
barrier();
if (TQ_K || TQ_V) {
uint lane = tid & 31u, gbase = (tid >> 7u) * 128u;
if ((tid & 127u) < 32u) {
if (TQ_K) rotate(kq, gbase, lane, false);
if (TQ_V) rotate(vq, gbase, lane, false);
uint lane = tid & 31u;
if (t < 32u) {
if (TQ_K) rotate(kq[g], lane, false);
if (TQ_V) rotate(vq[g], lane, false);
}
barrier();
}
uint slot = ((e / p.hd) & 7u) * 8u + ((e % p.hd) >> 7u);
float krot = TQ_K ? kq[tid] : kv, vrot = TQ_V ? vq[tid] : vv;
float krot = TQ_K ? kq[g][t] : kv, vrot = TQ_V ? vq[g][t] : vv;
float sk = 1.0, sv = 1.0;
if (TQ_K) sk = sqrt_(tq_group_reduce(krot * krot, slot));
if (TQ_V) sv = sqrt_(tq_group_reduce(vrot * vrot, 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_64.spv
Binary file not shown.
Binary file modified src/vulkan/spv/attn_prefill_tile.spv
Binary file not shown.
Binary file modified src/vulkan/spv/kv_store.spv
Binary file not shown.
8 changes: 8 additions & 0 deletions src/vulkan/spv_smem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,14 @@ mod tests {
assert_eq!(shared_bytes("gemmi_s_16", &[0, 10, 84, 0, 0], 0).unwrap(), 20992);
}

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

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.

fn turbo_prefill_128_fits_49152() {
let bytes = shared_bytes_split("attn_prefill_128", &[], 4, 4).unwrap();
assert_eq!(bytes, 47616, "turbo attn_prefill_128 shared memory changed");
assert!(bytes <= 49152, "turbo attn_prefill_128 needs {bytes} B");
}

#[test]
fn shared_memory_refusal_names_the_kernel_spec_and_both_sizes() {
let e = check_shared_memory("gemmi_dp", &[0, 10, 84, 0, 0], 0, 0, 40960).unwrap_err().to_string();
Expand Down
20 changes: 18 additions & 2 deletions tests/vulkan_split_kv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ fn set(k: &str, v: &str) {
unsafe {
std::env::set_var("STRATA_KV_K", k);
std::env::set_var("STRATA_KV_V", v);
std::env::set_var("STRATA_VK_TURBO", "1");
std::env::remove_var("STRATA_KV");
std::env::remove_var("STRATA_KV_Q8");
}
Expand Down Expand Up @@ -86,7 +85,6 @@ fn cu_caches(k: &str, v: &str, s: Shape) -> (Vec<u16>, Vec<u16>) {
}

#[test]
#[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.

Expand Down Expand Up @@ -169,3 +167,21 @@ fn vulkan_inkling_turbo_hd64() {
eprintln!("vulkan turbo hd64 prefill vs decode {:.2e}", rel_l2(&got, &want));
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.

#[test]
fn vulkan_turbo_tiled_prefill_matches_decode() {
let _lock = common::gpu_lock();
set("turbo4", "turbo4");
unsafe { std::env::set_var("STRATA_VK_NO_COOP", "1") };
let Some(vk) = open_vk() else {
unsafe { std::env::remove_var("STRATA_VK_NO_COOP") };
return;
};
for hd in [128, 256] {
let (_, _, pf, dec) = run(&vk, (4, 2, hd, 16, 3));
eprintln!("vulkan turbo4 tiled hd {hd} prefill vs decode {:.2e}", rel_l2(&pf, &dec));
assert!(rel_l2(&pf, &dec) < 5e-4, "vulkan turbo4 tiled hd {hd} prefill vs decode");
}
unsafe { std::env::remove_var("STRATA_VK_NO_COOP") };
}
Loading