diff --git a/crates/vetta-infer-hip/src/lib.rs b/crates/vetta-infer-hip/src/lib.rs index a2d28e8..91e216b 100644 --- a/crates/vetta-infer-hip/src/lib.rs +++ b/crates/vetta-infer-hip/src/lib.rs @@ -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()) }; diff --git a/crates/vetta-infer-ld/src/dspark.rs b/crates/vetta-infer-ld/src/dspark.rs index d1820f1..9d62d28 100644 --- a/crates/vetta-infer-ld/src/dspark.rs +++ b/crates/vetta-infer-ld/src/dspark.rs @@ -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 = (0..n).map(|i| (i % 200000) as i32).collect(); + let mut states: Vec> = (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> = (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); + } } \ No newline at end of file