[HIP] Flash dispatch by M: WMMA prefill (perf) + deterministic scalar decode/verify

Supersedes the global scalar-flash default (6e854a9) which regressed prefill ~2x.
Root cause confirmed empirically: the FA2 WMMA flash leaks a masked extra KV key's
real V through the PV (~1 ulp) when q_max_pos > a query's pos in a mixed-position
tile (zeroing K13/V13 makes M=2==M=1 exact). That ~1 ulp is within the relaxed
prefill parity (greedy-vs-reference gate) but breaks EXACT speculative losslessness
(decode M=1 vs verify M=nd+1 must agree bit-for-bit; the drift bf16-rounds into the
ShortConv hist and accumulates until a greedy near-tie flips).
Fix: dispatch flash by M -> WMMA for M>=16 (prefill chunks, preserves the measured
win), per-query-deterministic scalar f16 flash for M<16 (decode B=1 + spec verify
M<=8). VETTA_FLASH_WMMA=1 forces WMMA, VETTA_NO_FLASH_WMMA=1 forces scalar.
MEASURED prefill 1024 tok chunk 256: WMMA 5.786 ms/tok vs scalar 10.047 ms/tok
(1.74x) -> no prefill regression. flash_batch_determinism M=2 vs M=1 maxdiff=0.
spec_lossless_gate 200 tokens EXACT. CPU -D warnings clean, hip 45/45, ld GPU 65/65.
This commit is contained in:
adm committed 2026-12-02 21:49:21 +01:00
1 parent 7b617bc333
commit 11404c4486
2 files changed
+45 -6

No files matched your search

+13 -6
View File
@@ -652,12 +652,19 @@ pub fn flash_swa_gqa_prefill_f16(q: &DeviceBuf, k: &DeviceBuf, v: &DeviceBuf, ou
let rb = rel_bias.map(|b| b.ptr as *const f32).unwrap_or(std::ptr::null());
let sk = sink_logit.map(|b| b.ptr as *const f32).unwrap_or(std::ptr::null());
let gt = gate.map(|b| b.ptr as *const f32).unwrap_or(std::ptr::null());
// FA2 WMMA prefill (hd==128) is OPT-IN (VETTA_FLASH_WMMA=1): its online-softmax
// rescale over a mixed-position query tile makes a query's output depend on its
// batch neighbours by ~1 ulp (the masked extra KV block still rescales o_acc).
// That breaks speculative losslessness AND chunked-prefill determinism, so the
// default is the per-query-deterministic scalar f16 flash (verified baseline).
if hd == 128 && std::env::var("VETTA_FLASH_WMMA").is_ok() {
// FA2 WMMA prefill (hd==128) keeps P in LDS -> no global scores_scratch (~-53%
// prefill, measured). Its online-softmax over a mixed-position query tile makes
// a query's output depend on batch neighbours by ~1 ulp (a masked extra KV key
// still leaks through the WMMA PV). That is within the relaxed prefill parity
// (greedy-vs-reference is the gate) but BREAKS exact speculative losslessness,
// which needs decode (M=1) and verify (M=nd+1<=8) to agree bit-for-bit. So: WMMA
// for large M (prefill chunks, perf), per-query-deterministic scalar f16 flash
// for small M (decode + spec verify). VETTA_FLASH_WMMA=1 forces WMMA (testing);
// VETTA_NO_FLASH_WMMA=1 forces scalar everywhere.
let force_wmma = std::env::var("VETTA_FLASH_WMMA").is_ok();
let force_scalar = std::env::var("VETTA_NO_FLASH_WMMA").is_ok();
let use_wmma = hd == 128 && (force_wmma || (!force_scalar && m >= 16));
if use_wmma {
let rc = unsafe {
ffi::vetta_flash_swa_gqa_prefill_wmma(q.ptr as *const f32, k.ptr as *const u16, v.ptr as *const u16, out.ptr as *mut f32, n_head, n_kv, seq, hd, scale, swa_window, pos.ptr as *const i32, rb, max_rel, sk, gt, m, std::ptr::null_mut())
};