diff --git a/src/vulkan/mod.rs b/src/vulkan/mod.rs index fa5c80c..2757980 100644 --- a/src/vulkan/mod.rs +++ b/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 { let (kv_k_mode, kv_v_mode) = (crate::cuda::kv_k_mode(), crate::cuda::kv_v_mode()); - // 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. diff --git a/src/vulkan/shaders/attn_prefill.glsl b/src/vulkan/shaders/attn_prefill.glsl index d1c6a9f..814423f 100644 --- a/src/vulkan/shaders/attn_prefill.glsl +++ b/src/vulkan/shaders/attn_prefill.glsl @@ -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]; diff --git a/src/vulkan/shaders/attn_prefill_tile.comp b/src/vulkan/shaders/attn_prefill_tile.comp index c1331a0..ec63e46 100644 --- a/src/vulkan/shaders/attn_prefill_tile.comp +++ b/src/vulkan/shaders/attn_prefill_tile.comp @@ -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); 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 diff --git a/src/vulkan/shaders/kv_store.comp b/src/vulkan/shaders/kv_store.comp index c592c2a..9ea9cf4 100644 --- a/src/vulkan/shaders/kv_store.comp +++ b/src/vulkan/shaders/kv_store.comp @@ -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)); diff --git a/src/vulkan/spv/attn_prefill_128.spv b/src/vulkan/spv/attn_prefill_128.spv index 36213fb..5ab657d 100644 Binary files a/src/vulkan/spv/attn_prefill_128.spv and b/src/vulkan/spv/attn_prefill_128.spv differ diff --git a/src/vulkan/spv/attn_prefill_64.spv b/src/vulkan/spv/attn_prefill_64.spv index 7a73af8..4c59f61 100644 Binary files a/src/vulkan/spv/attn_prefill_64.spv and b/src/vulkan/spv/attn_prefill_64.spv differ diff --git a/src/vulkan/spv/attn_prefill_tile.spv b/src/vulkan/spv/attn_prefill_tile.spv index 586402b..410772d 100644 Binary files a/src/vulkan/spv/attn_prefill_tile.spv and b/src/vulkan/spv/attn_prefill_tile.spv differ diff --git a/src/vulkan/spv/kv_store.spv b/src/vulkan/spv/kv_store.spv index a9a16c9..1c00bd6 100644 Binary files a/src/vulkan/spv/kv_store.spv and b/src/vulkan/spv/kv_store.spv differ diff --git a/src/vulkan/spv_smem.rs b/src/vulkan/spv_smem.rs index 79e1fce..ef409f3 100644 --- a/src/vulkan/spv_smem.rs +++ b/src/vulkan/spv_smem.rs @@ -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] + 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(); diff --git a/tests/vulkan_split_kv.rs b/tests/vulkan_split_kv.rs index 979b590..7e6783b 100644 --- a/tests/vulkan_split_kv.rs +++ b/tests/vulkan_split_kv.rs @@ -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"); } @@ -86,7 +85,6 @@ fn cu_caches(k: &str, v: &str, s: Shape) -> (Vec, Vec) { } #[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); @@ -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. +#[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") }; +}