[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:
1 parent
7b617bc333
commit
11404c4486
2 files changed
+45
-6
No files matched your search
@@ -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())
|
||||
};
|
||||
|
||||
@@ -1061,4 +1061,36 @@ mod tests {
|
||||
}
|
||||
eprintln!("spec_lockstep_diag: 200 rounds hist EXACT");
|
||||
}
|
||||
|
||||
/// Prefill perf probe: chunked forward_batch over 1024 synthetic tokens
|
||||
/// (chunk 256, M>=16 -> WMMA flash by default). Run with VETTA_NO_FLASH_WMMA=1
|
||||
/// to compare the scalar path. Confirms the M-based dispatch preserves the
|
||||
/// WMMA prefill win while decode/verify (M<16) stay deterministic.
|
||||
#[test]
|
||||
#[cfg(feature = "hip")]
|
||||
#[ignore]
|
||||
fn prefill_perf_probe() {
|
||||
use crate::layer::{Dims, WeightCache, forward_batch};
|
||||
let ar = std::sync::Arc::new(SafetensorsArchive::open("/mnt/models/Inkling/safetensors").unwrap());
|
||||
let cache = WeightCache::new(ar.clone());
|
||||
let d = Dims { hidden: 2304, n_head: 24, n_kv: 8, hd: 128, d_rel: 16, rel_extent: 1024, inter: 1152, n_experts: 128, top_k: 8, block_size: 6, qk_clip: 22.627416997969522, rel_clip: 8.0, eps: 1e-6, route_scale: 8.0, beta: 4.0, lb: 25.0, swa_win: 1024 };
|
||||
let vocab = 200058usize; let n = 1024usize; let chunk = 256usize; let max_seq = n + 8;
|
||||
let toks: Vec<i32> = (0..n).map(|i| (i % 200000) as i32).collect();
|
||||
let mut states: Vec<Option<crate::layer::LayerState>> = (0..24).map(|_| None).collect();
|
||||
// warmup one chunk (weight residency)
|
||||
let _ = forward_batch(&cache, &d, &toks[..chunk], vocab, max_seq, 0, 0.0, &mut states).unwrap();
|
||||
vetta_infer_hip::synchronize().unwrap();
|
||||
let mut states: Vec<Option<crate::layer::LayerState>> = (0..24).map(|_| None).collect();
|
||||
let t0 = std::time::Instant::now();
|
||||
let mut done = 0usize;
|
||||
while done < n {
|
||||
let take = chunk.min(n - done);
|
||||
let _ = forward_batch(&cache, &d, &toks[done..done + take], vocab, max_seq, done as i32, 0.0, &mut states).unwrap();
|
||||
done += take;
|
||||
}
|
||||
vetta_infer_hip::synchronize().unwrap();
|
||||
let ms = t0.elapsed().as_secs_f64() * 1000.0;
|
||||
let wmma = std::env::var("VETTA_NO_FLASH_WMMA").is_err();
|
||||
eprintln!("prefill_perf_probe: {n} tok chunk={chunk} flash={} total={ms:.1}ms per_tok={:.3}ms", if wmma {"WMMA(m>=16)"} else {"scalar"}, ms / n as f64);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user