[HIP] ShortConv4 decode kernel (Inkling causal depthwise conv + history ring) verified

- k_shortconv4_decode / vetta_shortconv4_decode: exact strata
  k2_shortconv4_batched static (no-dyn) semantics — per channel
  y = x0 + sw0*h0 + sw1*h1 + sw2*h2 + sw3*x0, scaled by 1/sqrt(5); the 3-tap
  history ring (t-3,t-2,t-1) shifts h0<-h1<-h2<-x0 in place. This is a defining
  Inkling mechanism (applied to k/v/attn/mlp). f32 history here; the BF16 ring
  for bit-exact CPU match is a later parity refinement.
- Multi-step parity test: 6-token recurrence vs a CPU oracle, per-step output
  match (<1e-5) AND device history ring == host history ring after all steps
  (exact). The stateful short-conv evolves correctly across tokens.

Verified: RUSTFLAGS="-D warnings" cargo test --workspace = 97/97 PASS;
lean proofs (4 files, 42 theorems) green; GPU 11/11 (--features hip).
This commit is contained in:
adm committed 2026-12-01 14:26:14 +01:00
1 parent 00a6993c50
commit e060862660
3 files changed
+96

No files matched your search

+49
View File
@@ -56,6 +56,7 @@ mod ffi {
) -> c_int;
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;
}
}
@@ -240,6 +241,14 @@ pub fn moe_reduce(y_part: &DeviceBuf, tk_w: &DeviceBuf, out: &DeviceBuf, top_k:
Ok(())
}
/// Launch k_shortconv4_decode (static): updates `hist` ring in place.
#[cfg(feature = "hip")]
pub fn shortconv4_decode(cur: &DeviceBuf, static_w: &DeviceBuf, hist: &DeviceBuf, out: &DeviceBuf, slot_ids: &DeviceBuf, m: i32, c: i32) -> Result<(), String> {
let rc = unsafe { ffi::vetta_shortconv4_decode(cur.ptr as *const f32, static_w.ptr as *const f32, hist.ptr as *mut f32, out.ptr as *mut f32, slot_ids.ptr as *const i32, m, c, std::ptr::null_mut()) };
if rc != 0 { return Err("shortconv4 launch failed".into()); }
Ok(())
}
#[cfg(all(test, feature = "hip"))]
mod gpu_tests {
use super::*;
@@ -566,6 +575,46 @@ mod gpu_tests {
assert!(max_rel < 1e-5, "moe_reduce parity failed: {max_rel}");
}
#[test]
#[ignore]
fn shortconv4_multistep_parity() {
let c = 64usize;
let steps = 6usize;
let inv_sqrt5 = 0.44721359549995793f32;
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 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 dslot = DeviceBuf::alloc(4).unwrap(); dslot.h2d_bytes(&0i32.to_le_bytes()).unwrap();
for t in 0..steps {
// CPU oracle step (hist evolves on host).
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];
want[ch] = (x0 + w[0] * h0 + w[1] * h1 + w[2] * h2 + w[3] * x0) * inv_sqrt5;
}
for ch in 0..c { hist[ch] = hist[c + ch]; hist[c + ch] = hist[2 * c + ch]; hist[2 * c + ch] = xs[t][ch]; }
// GPU step (hist evolves on device).
dcur.h2d(&xs[t]).unwrap();
shortconv4_decode(&dcur, &dsw, &dh, &dout, &dslot, 1, c as i32).unwrap();
let mut got = vec![0f32; c]; dout.d2h(&mut got).unwrap();
let mut max_rel = 0f32;
for (g, w) in got.iter().zip(want.iter()) { max_rel = max_rel.max((g - w).abs() / w.abs().max(1e-3)); }
assert!(max_rel < 1e-5, "shortconv4 step {t} parity failed: {max_rel}");
}
// device history must match host history after all steps.
let mut dh_host = vec![0f32; 3 * c]; dh.d2h(&mut dh_host).unwrap();
assert_eq!(dh_host, hist, "shortconv4 history ring must match");
eprintln!("shortconv4 {steps}-step recurrence + history ring: exact");
}
fn bytemuck_u16(v: &[u16]) -> &[u8] {
unsafe { std::slice::from_raw_parts(v.as_ptr() as *const u8, v.len() * 2) }
}