[HIP] dynamic-tap ShortConv4 kernel (full-attn layers) verified

- k_shortconv4_dyn_decode / vetta_shortconv4_dyn_decode: exact strata
  k2_shortconv4 dyn semantics for the full-attention layers — y = x0 +
  sw0*h0+sw1*h1+sw2*h2+sw3*x0, then y += dt0*x0+dt1*h2+dt2*h1+dt3*h0, scaled
  by 1/3 (vs 1/sqrt(5) for the static SWA variant). dyn_taps [M,n_heads,4],
  head = c/(C/n_heads); static_w [C,4]; history ring shifted in place.
- Multi-step parity test: 5-token recurrence vs CPU oracle, per-step match
  (<1e-5) AND device history ring == host history ring (exact). The dynamic
  short-conv (driven by k/v_sconv.weight_proj taps) evolves correctly.

This is the last shortconv variant needed for the full-attn layers {5,11,17,23}
(3-query mix split + separate q/k/v norms + dyn shortconv + tau + global attn).

Verified: RUSTFLAGS="-D warnings" cargo test --workspace = 97/97 PASS;
lean proofs (5 files incl. inkling_loop) green; GPU 16/16 (--features hip).
This commit is contained in:
adm committed 2026-12-01 15:00:00 +01:00
1 parent 04aae6db9d
commit 38a983ce80
3 files changed
+97

No files matched your search

+47
View File
@@ -63,6 +63,7 @@ mod ffi {
pub fn vetta_situ(gu: *const c_float, act: *mut c_float, inter: c_int, beta: c_float, lb: c_float, stream: *mut c_void) -> c_int;
pub fn vetta_moe_reduce(y_part: *const c_float, tk_w: *const c_float, out: *mut c_float, top_k: c_int, hidden: 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_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;
@@ -275,6 +276,15 @@ pub fn shortconv4_decode(cur: &DeviceBuf, static_w: &DeviceBuf, hist: &DeviceBuf
Ok(())
}
/// Launch k_shortconv4_dyn_decode: full-attn ShortConv4 with dynamic taps.
#[cfg(feature = "hip")]
#[allow(clippy::too_many_arguments)]
pub fn shortconv4_dyn_decode(cur: &DeviceBuf, static_w: &DeviceBuf, dyn_taps: &DeviceBuf, hist: &DeviceBuf, out: &DeviceBuf, slot_ids: &DeviceBuf, m: i32, c: i32, n_heads: i32) -> Result<(), String> {
let rc = unsafe { ffi::vetta_shortconv4_dyn_decode(cur.ptr as *const f32, static_w.ptr as *const f32, dyn_taps.ptr as *const f32, hist.ptr as *mut f32, out.ptr as *mut f32, slot_ids.ptr as *const i32, m, c, n_heads, std::ptr::null_mut()) };
if rc != 0 { return Err("shortconv4_dyn launch failed".into()); }
Ok(())
}
/// Launch k_attn_res_mix: Inkling AttnRes deep-residual block-mix.
#[cfg(feature = "hip")]
#[allow(clippy::too_many_arguments)]
@@ -753,6 +763,43 @@ mod gpu_tests {
eprintln!("shortconv4 {steps}-step recurrence + history ring: exact");
}
#[test]
#[ignore]
fn shortconv4_dyn_multistep_parity() {
let c = 64usize; let n_heads = 4usize; let steps = 5usize;
let sw: Vec<f32> = (0..c * 4).map(|i| ((i % 17) as f32 - 8.0) * 0.05).collect();
let mut hist: Vec<f32> = (0..3 * c).map(|i| ((i % 11) as f32 - 5.0) * 0.1).collect();
let xs: Vec<Vec<f32>> = (0..steps).map(|t| (0..c).map(|i| ((i + t * 7) % 23) as f32 * 0.03 - 0.3).collect()).collect();
let dyns: Vec<Vec<f32>> = (0..steps).map(|t| (0..n_heads * 4).map(|i| ((i + t * 5) % 13) as f32 * 0.02 - 0.1).collect()).collect();
let dsw = DeviceBuf::alloc(sw.len() * 4).unwrap(); dsw.h2d(&sw).unwrap();
let dh = DeviceBuf::alloc(hist.len() * 4).unwrap(); dh.h2d(&hist).unwrap();
let dcur = DeviceBuf::alloc(c * 4).unwrap();
let dout = DeviceBuf::alloc(c * 4).unwrap();
let ddyn = DeviceBuf::alloc(n_heads * 4 * 4).unwrap();
let dslot = DeviceBuf::alloc(4).unwrap(); dslot.h2d_bytes(&0i32.to_le_bytes()).unwrap();
for t in 0..steps {
let mut want = vec![0f32; c];
for ch in 0..c {
let h0 = hist[ch]; let h1 = hist[c + ch]; let h2 = hist[2 * c + ch];
let x0 = xs[t][ch];
let w = &sw[ch * 4..ch * 4 + 4];
let head = ch / (c / n_heads);
let dt = &dyns[t][head * 4..head * 4 + 4];
let y = x0 + w[0] * h0 + w[1] * h1 + w[2] * h2 + w[3] * x0 + dt[0] * x0 + dt[1] * h2 + dt[2] * h1 + dt[3] * h0;
want[ch] = y / 3.0;
}
for ch in 0..c { hist[ch] = hist[c + ch]; hist[c + ch] = hist[2 * c + ch]; hist[2 * c + ch] = xs[t][ch]; }
dcur.h2d(&xs[t]).unwrap(); ddyn.h2d(&dyns[t]).unwrap();
shortconv4_dyn_decode(&dcur, &dsw, &ddyn, &dh, &dout, &dslot, 1, c as i32, n_heads as i32).unwrap();
let mut got = vec![0f32; c]; dout.d2h(&mut got).unwrap();
let mut mr = 0f32; for (g, w) in got.iter().zip(want.iter()) { mr = mr.max((g - w).abs() / w.abs().max(1e-3)); }
assert!(mr < 1e-5, "shortconv4_dyn step {t} parity failed: {mr}");
}
let mut dh_host = vec![0f32; 3 * c]; dh.d2h(&mut dh_host).unwrap();
assert_eq!(dh_host, hist, "shortconv4_dyn history ring must match");
eprintln!("shortconv4_dyn {steps}-step recurrence + history ring: exact");
}
#[test]
#[ignore]
fn attn_res_mix_parity_vs_cpu() {