[HIP] Batched causal flash SWA GQA M=B (FP16 KV): the hardest prefill attention kernel
k_flash_swa_gqa_prefill_f16: same two-pass softmax semantics as the decode f16 kernel extended to M tokens (grid n_head x M). Each query token m uses cur_pos=pos[m] (causal: attends keys <= pos[m]); KV cache [n_kv,seq,hd] must hold all B positions first. q/out/gate [M,n_head,hd], rel_bias [M,n_head,max_rel], sink [M,n_head], scores_scratch [M,n_head,n_win_max] (alloc_unpooled, varies per batch). SWA window + rel_bias + sink + sigmoid gate all preserved. Parity vs B sequential decode flash f16 calls: B=20 n_head=8 win=16 base=10 (windowed + unwindowed tokens), BIT-EXACT (0.0). Additive — decode default unchanged. Batched attention kernels now landed: ShortConv prefill_par + causal flash M=B + attn_res_mix (already M>1). Remaining: kv_store B positions + batched depth-blocks/controllers/bank + SiTU/MXFP8 batched, then forward_batch orchestration + serve wiring + EXACT parity + measure turn-1. Verified: hip GPU 25 green, workspace CPU -D warnings green, 6 Lean 0 sorryAx.
This commit is contained in:
1 parent
ec2ad3fdc5
commit
a7a1ab11b0
2 files changed
+183
No files matched your search
@@ -63,6 +63,13 @@ mod ffi {
|
||||
swa_window: c_int, cur_pos: c_int, rel_bias: *const c_float, max_rel: c_int,
|
||||
sink_logit: *const c_float, gate: *const c_float, scores_scratch: *mut c_float, stream: *mut c_void,
|
||||
) -> c_int;
|
||||
pub fn vetta_flash_swa_gqa_prefill_f16(
|
||||
q: *const c_float, k: *const u16, v: *const u16, out: *mut c_float,
|
||||
n_head: c_int, n_kv: c_int, seq: c_int, hd: c_int, scale: c_float,
|
||||
swa_window: c_int, pos: *const c_int, rel_bias: *const c_float, max_rel: c_int,
|
||||
sink_logit: *const c_float, gate: *const c_float, scores_scratch: *mut c_float,
|
||||
n_win_max: c_int, m: c_int, stream: *mut c_void,
|
||||
) -> c_int;
|
||||
pub fn vetta_moe_router_topk(
|
||||
logits: *const c_float, bias: *const c_float, topk_ids: *mut i32, topk_weights: *mut c_float,
|
||||
m: c_int, n_experts: c_int, k_top: c_int, route_scale: c_float, stream: *mut c_void,
|
||||
@@ -393,6 +400,25 @@ pub fn flash_swa_gqa_decode_f16(q: &DeviceBuf, k: &DeviceBuf, v: &DeviceBuf, out
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Batched causal flash SWA GQA (M=B) on FP16 KV. `pos` [M] gives each query
|
||||
/// token's cur_pos (causal); KV cache must already hold all B positions.
|
||||
/// `max_pos` = largest pos in the batch (sizes the [M,n_head,n_win_max] scratch,
|
||||
/// allocated unpooled since it varies per batch). q/out/gate [M,n_head,hd].
|
||||
#[cfg(feature = "hip")]
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub fn flash_swa_gqa_prefill_f16(q: &DeviceBuf, k: &DeviceBuf, v: &DeviceBuf, out: &DeviceBuf, n_head: i32, n_kv: i32, seq: i32, hd: i32, scale: f32, swa_window: i32, pos: &DeviceBuf, max_pos: i32, rel_bias: Option<&DeviceBuf>, max_rel: i32, sink_logit: Option<&DeviceBuf>, gate: Option<&DeviceBuf>, m: i32) -> Result<(), String> {
|
||||
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());
|
||||
let n_win_max = if swa_window > 0 && max_pos >= swa_window { swa_window } else { max_pos + 1 };
|
||||
let scratch = DeviceBuf::alloc_unpooled((m as usize) * (n_head as usize) * (n_win_max as usize) * 4)?;
|
||||
let rc = unsafe {
|
||||
ffi::vetta_flash_swa_gqa_prefill_f16(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, scratch.ptr as *mut f32, n_win_max, m, std::ptr::null_mut())
|
||||
};
|
||||
if rc != 0 { return Err("flash_swa_gqa_prefill_f16 launch failed".into()); }
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Launch k_moe_router_topk (Inkling exact sigmoid router).
|
||||
#[cfg(feature = "hip")]
|
||||
pub fn moe_router_topk(logits: &DeviceBuf, bias: Option<&DeviceBuf>, ids: &DeviceBuf, weights: &DeviceBuf, m: i32, n_experts: i32, k_top: i32, route_scale: f32) -> Result<(), String> {
|
||||
@@ -1344,6 +1370,57 @@ mod gpu_tests {
|
||||
assert!(hmax < 1e-6, "update_hist parity failed: {hmax}");
|
||||
}
|
||||
|
||||
/// Batched causal flash prefill (M=B) must equal B sequential decode flash f16
|
||||
/// calls (each cur_pos=base+m), same FP16 KV + rel_bias + sink + gate. win=16
|
||||
/// exercises windowing; base=10 so early tokens are unwindowed, later windowed.
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn flash_swa_gqa_prefill_f16_parity() {
|
||||
let (n_head, n_kv, hd, seq, b) = (8usize, 2usize, 128usize, 64usize, 20usize);
|
||||
let (base, win, max_rel) = (10i32, 16i32, 16usize);
|
||||
let scale = 1.0f32 / (hd as f32).sqrt();
|
||||
let max_pos = base + (b as i32) - 1;
|
||||
let kf: Vec<f32> = (0..n_kv * seq * hd).map(|i| ((i % 41) as f32 - 20.0) * 0.02).collect();
|
||||
let vf: Vec<f32> = (0..n_kv * seq * hd).map(|i| ((i % 37) as f32 - 18.0) * 0.02).collect();
|
||||
let dkf = DeviceBuf::alloc(kf.len() * 4).unwrap(); dkf.h2d(&kf).unwrap();
|
||||
let dk = DeviceBuf::alloc(kf.len() * 2).unwrap(); cast_f32_f16(&dkf, &dk, kf.len()).unwrap();
|
||||
let dvf = DeviceBuf::alloc(vf.len() * 4).unwrap(); dvf.h2d(&vf).unwrap();
|
||||
let dv = DeviceBuf::alloc(vf.len() * 2).unwrap(); cast_f32_f16(&dvf, &dv, vf.len()).unwrap();
|
||||
let qf: Vec<f32> = (0..b * n_head * hd).map(|i| ((i % 29) as f32 - 14.0) * 0.03).collect();
|
||||
let rb: Vec<f32> = (0..b * n_head * max_rel).map(|i| ((i % 13) as f32 - 6.0) * 0.01).collect();
|
||||
let sk: Vec<f32> = (0..b * n_head).map(|i| -2.0 + (i % 5) as f32 * 0.1).collect();
|
||||
let gt: Vec<f32> = (0..b * n_head * hd).map(|i| ((i % 17) as f32 - 8.0) * 0.05).collect();
|
||||
let dq = DeviceBuf::alloc(qf.len() * 4).unwrap(); dq.h2d(&qf).unwrap();
|
||||
let drb = DeviceBuf::alloc(rb.len() * 4).unwrap(); drb.h2d(&rb).unwrap();
|
||||
let dsk = DeviceBuf::alloc(sk.len() * 4).unwrap(); dsk.h2d(&sk).unwrap();
|
||||
let dgt = DeviceBuf::alloc(gt.len() * 4).unwrap(); dgt.h2d(>).unwrap();
|
||||
let pos: Vec<i32> = (0..b).map(|m| base + m as i32).collect();
|
||||
let pos_bytes: Vec<u8> = pos.iter().flat_map(|x| x.to_le_bytes()).collect();
|
||||
let dpos = DeviceBuf::alloc(b * 4).unwrap(); dpos.h2d_bytes(&pos_bytes).unwrap();
|
||||
let dout_p = DeviceBuf::alloc(b * n_head * hd * 4).unwrap();
|
||||
flash_swa_gqa_prefill_f16(&dq, &dk, &dv, &dout_p, n_head as i32, n_kv as i32, seq as i32, hd as i32, scale, win, &dpos, max_pos, Some(&drb), max_rel as i32, Some(&dsk), Some(&dgt), b as i32).unwrap();
|
||||
let mut got_p = vec![0f32; b * n_head * hd]; dout_p.d2h(&mut got_p).unwrap();
|
||||
let dq1 = DeviceBuf::alloc(n_head * hd * 4).unwrap();
|
||||
let drb1 = DeviceBuf::alloc(n_head * max_rel * 4).unwrap();
|
||||
let dsk1 = DeviceBuf::alloc(n_head * 4).unwrap();
|
||||
let dgt1 = DeviceBuf::alloc(n_head * hd * 4).unwrap();
|
||||
let dout1 = DeviceBuf::alloc(n_head * hd * 4).unwrap();
|
||||
let mut got_s = vec![0f32; b * n_head * hd];
|
||||
for m in 0..b {
|
||||
dq1.h2d(&qf[m * n_head * hd..(m + 1) * n_head * hd]).unwrap();
|
||||
drb1.h2d(&rb[m * n_head * max_rel..(m + 1) * n_head * max_rel]).unwrap();
|
||||
dsk1.h2d(&sk[m * n_head..(m + 1) * n_head]).unwrap();
|
||||
dgt1.h2d(>[m * n_head * hd..(m + 1) * n_head * hd]).unwrap();
|
||||
flash_swa_gqa_decode_f16(&dq1, &dk, &dv, &dout1, n_head as i32, n_kv as i32, seq as i32, hd as i32, scale, win, pos[m], Some(&drb1), max_rel as i32, Some(&dsk1), Some(&dgt1)).unwrap();
|
||||
let mut o = vec![0f32; n_head * hd]; dout1.d2h(&mut o).unwrap();
|
||||
got_s[m * n_head * hd..(m + 1) * n_head * hd].copy_from_slice(&o);
|
||||
}
|
||||
let mut max_rel_err = 0f32;
|
||||
for (g, w) in got_p.iter().zip(got_s.iter()) { max_rel_err = max_rel_err.max((g - w).abs() / w.abs().max(1e-3)); }
|
||||
eprintln!("flash_swa_gqa_prefill_f16 B={b} n_head={n_head} win={win} max_rel_err={max_rel_err:.3e}");
|
||||
assert!(max_rel_err < 1e-5, "flash prefill parity failed: {max_rel_err}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn shortconv4_multistep_parity() {
|
||||
|
||||
Reference in new issue
Block a user