[HIP] ShortConv prefill parallel (tile 16) + update_hist: batched attention brick

k_shortconv4_prefill_par (exact strata k2_shortconv4_prefill_par): one thread per
channel processes a tile of 16 contiguous tokens; 3-token history seeded from hist
[3,C] (bf16-rounded f32, same ring as decode) for t0<3 else in_x bf16 round-trip.
Static 1/sqrt(5) or dynamic taps [B,n_heads,4] 1/3. Replaces B sequential decode
calls with one parallel launch (grid C-tiles x ceil(B/16)).
k_shortconv4_update_hist: roll history <- last 3 in_x (bf16), shift for B<3.

Parity vs B sequential decode steps: B=37 (partial last tile) C=64, output AND
history BIT-EXACT (0.0). Note: k_attn_res_mix already supports M>1 (grid nq x M).

Additive — decode default unchanged. Remaining batched attention: causal flash
M=B + kv_store B positions + batched depth-blocks/controllers/bank, then
forward_batch orchestration + serve wiring + EXACT parity.

Verified: hip GPU 24 green, workspace CPU -D warnings green, 6 Lean 0 sorryAx.
This commit is contained in:
adm committed 2026-12-01 23:38:20 +01:00
1 parent b6113b549a
commit ec2ad3fdc5
2 files changed
+140

No files matched your search

+60
View File
@@ -107,6 +107,8 @@ mod ffi {
pub fn vetta_moe_reduce_buckets_masked(bucket_y: *const c_float, slotk_to_bidx: *const c_int, tk_w: *const c_float, out: *mut c_float, b: c_int, top_k: c_int, d: c_int, stream: *mut c_void) -> c_int;
pub fn vetta_shortconv4_decode(cur: *const c_float, static_w: *const c_float, hist: *mut c_float, out: *mut c_float, slot_ids: *const i32, m: c_int, c: c_int, stream: *mut c_void) -> c_int;
pub fn vetta_shortconv4_dyn_decode(cur: *const c_float, static_w: *const c_float, dyn_taps: *const c_float, hist: *mut c_float, out: *mut c_float, slot_ids: *const i32, m: c_int, c: c_int, n_heads: c_int, stream: *mut c_void) -> c_int;
pub fn vetta_shortconv4_prefill_par(in_x: *const c_float, static_w: *const c_float, dyn_taps: *const c_float, hist: *const c_float, out_y: *mut c_float, b: c_int, c: c_int, n_heads: c_int, stream: *mut c_void) -> c_int;
pub fn vetta_shortconv4_update_hist(in_x: *const c_float, hist: *mut c_float, b: c_int, c: c_int, stream: *mut c_void) -> c_int;
pub fn vetta_attn_res_mix(blocks: *const c_float, cur: *const c_float, queries: *const c_float, out: *mut c_float, n_blocks: c_int, m: c_int, n_queries: c_int, d: c_int, eps: c_float, stream: *mut c_void) -> c_int;
pub fn vetta_l2_clip(x: *mut c_float, scale: *const c_float, rows: c_int, hd: c_int, clip: c_float, apply_tau: c_int, pos: c_int, stream: *mut c_void) -> c_int;
pub fn vetta_relpos_bias(r: *const c_float, rel_proj: *const c_float, rel_bias: *mut c_float, n_head: c_int, d_rel: c_int, rel_extent: c_int, lim: c_int, stream: *mut c_void) -> c_int;
@@ -592,6 +594,25 @@ pub fn shortconv4_dyn_decode(cur: &DeviceBuf, static_w: &DeviceBuf, dyn_taps: &D
Ok(())
}
/// Parallel causal ShortConv over B contiguous prefill tokens (tile 16). hist
/// [3,C] bf16-rounded f32 seed; dyn_taps nullable (static 1/sqrt5 vs dyn 1/3).
#[cfg(feature = "hip")]
#[allow(clippy::too_many_arguments)]
pub fn shortconv4_prefill_par(in_x: &DeviceBuf, static_w: &DeviceBuf, dyn_taps: Option<&DeviceBuf>, hist: &DeviceBuf, out_y: &DeviceBuf, b: i32, c: i32, n_heads: i32) -> Result<(), String> {
let dt = dyn_taps.map(|t| t.ptr as *const f32).unwrap_or(std::ptr::null());
let rc = unsafe { ffi::vetta_shortconv4_prefill_par(in_x.ptr as *const f32, static_w.ptr as *const f32, dt, hist.ptr as *const f32, out_y.ptr as *mut f32, b, c, n_heads, std::ptr::null_mut()) };
if rc != 0 { return Err("shortconv4_prefill_par launch failed".into()); }
Ok(())
}
/// Roll the 3-token ShortConv history after a prefill chunk (last 3 in_x bf16).
#[cfg(feature = "hip")]
pub fn shortconv4_update_hist(in_x: &DeviceBuf, hist: &DeviceBuf, b: i32, c: i32) -> Result<(), String> {
let rc = unsafe { ffi::vetta_shortconv4_update_hist(in_x.ptr as *const f32, hist.ptr as *mut f32, b, c, std::ptr::null_mut()) };
if rc != 0 { return Err("shortconv4_update_hist launch failed".into()); }
Ok(())
}
/// Launch k_attn_res_mix: Inkling AttnRes deep-residual block-mix.
#[cfg(feature = "hip")]
#[allow(clippy::too_many_arguments)]
@@ -1284,6 +1305,45 @@ mod gpu_tests {
assert!(max_rel < 1e-4, "reduce_buckets_masked parity failed: {max_rel}");
}
/// prefill_par (parallel causal ShortConv, tile 16) must equal B sequential
/// decode steps bit-for-bit (same bf16 history semantics), and update_hist must
/// equal the decode's final history. B=37 exercises a partial last tile.
#[test]
#[ignore]
fn shortconv4_prefill_par_parity() {
let (b, c) = (37usize, 64usize);
let inx: Vec<f32> = (0..b * c).map(|i| ((i % 53) as f32 - 26.0) * 0.03).collect();
let sw: Vec<f32> = (0..c * 4).map(|i| 0.1 + (i % 7) as f32 * 0.05).collect();
let hist0: Vec<f32> = (0..3 * c).map(|i| bf16_to_f32(f32_to_bf16(((i % 17) as f32 - 8.0) * 0.1))).collect();
let din = DeviceBuf::alloc(b * c * 4).unwrap(); din.h2d(&inx).unwrap();
let dsw = DeviceBuf::alloc(c * 4 * 4).unwrap(); dsw.h2d(&sw).unwrap();
let dhist_p = DeviceBuf::alloc(3 * c * 4).unwrap(); dhist_p.h2d(&hist0).unwrap();
let dout_p = DeviceBuf::alloc(b * c * 4).unwrap();
shortconv4_prefill_par(&din, &dsw, None, &dhist_p, &dout_p, b as i32, c as i32, 0).unwrap();
let mut got_p = vec![0f32; b * c]; dout_p.d2h(&mut got_p).unwrap();
let dhist_s = DeviceBuf::alloc(3 * c * 4).unwrap(); dhist_s.h2d(&hist0).unwrap();
let dslot = DeviceBuf::alloc(4).unwrap(); dslot.h2d_bytes(&0i32.to_le_bytes()).unwrap();
let dcur = DeviceBuf::alloc(c * 4).unwrap();
let dout1 = DeviceBuf::alloc(c * 4).unwrap();
let mut got_s = vec![0f32; b * c];
for t in 0..b {
dcur.h2d(&inx[t * c..(t + 1) * c]).unwrap();
shortconv4_decode(&dcur, &dsw, &dhist_s, &dout1, &dslot, 1, c as i32).unwrap();
let mut o = vec![0f32; c]; dout1.d2h(&mut o).unwrap();
got_s[t * c..(t + 1) * c].copy_from_slice(&o);
}
let mut max_rel = 0f32;
for (g, w) in got_p.iter().zip(got_s.iter()) { max_rel = max_rel.max((g - w).abs() / w.abs().max(1e-3)); }
shortconv4_update_hist(&din, &dhist_p, b as i32, c as i32).unwrap();
let mut hp = vec![0f32; 3 * c]; dhist_p.d2h(&mut hp).unwrap();
let mut hs = vec![0f32; 3 * c]; dhist_s.d2h(&mut hs).unwrap();
let mut hmax = 0f32;
for (a, bb) in hp.iter().zip(hs.iter()) { hmax = hmax.max((a - bb).abs()); }
eprintln!("shortconv4_prefill_par B={b} C={c} out_max_rel={max_rel:.3e} hist_max_abs={hmax:.3e}");
assert!(max_rel < 1e-5, "prefill_par output parity failed: {max_rel}");
assert!(hmax < 1e-6, "update_hist parity failed: {hmax}");
}
#[test]
#[ignore]
fn shortconv4_multistep_parity() {