diff --git a/crates/vetta-infer-hip/src/lib.rs b/crates/vetta-infer-hip/src/lib.rs index fcbb057..8c92d95 100644 --- a/crates/vetta-infer-hip/src/lib.rs +++ b/crates/vetta-infer-hip/src/lib.rs @@ -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 = (0..c * 4).map(|i| ((i % 17) as f32 - 8.0) * 0.05).collect(); + let mut hist: Vec = (0..3 * c).map(|i| ((i % 11) as f32 - 5.0) * 0.1).collect(); + let xs: Vec> = (0..steps).map(|t| (0..c).map(|i| ((i + t * 7) % 23) as f32 * 0.03 - 0.3).collect()).collect(); + let dyns: Vec> = (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() { diff --git a/kernels/hip/vetta_kernels.h b/kernels/hip/vetta_kernels.h index 48659c3..40b19a0 100644 --- a/kernels/hip/vetta_kernels.h +++ b/kernels/hip/vetta_kernels.h @@ -93,6 +93,11 @@ int vetta_shortconv4_decode(const float* cur, const float* static_w, float* hist float* out, const int32_t* slot_ids, int M, int C, vetta_stream_t stream); +// k_shortconv4_dyn_decode: full-attn ShortConv4 with dynamic taps [M,n_heads,4]. +int vetta_shortconv4_dyn_decode(const float* cur, const float* static_w, const float* dyn_taps, + float* hist, float* out, const int32_t* slot_ids, + int M, int C, int n_heads, vetta_stream_t stream); + // k_attn_res_mix: Inkling AttnRes deep-residual block-mix. blocks [n_blocks,M,D] // snapshots + cur [M,D] -> out [M,n_queries,D] (RMS-normalized depth mix). int vetta_attn_res_mix(const float* blocks, const float* cur, const float* queries, diff --git a/kernels/hip/vetta_kernels.hip b/kernels/hip/vetta_kernels.hip index 91ac3f2..543d294 100644 --- a/kernels/hip/vetta_kernels.hip +++ b/kernels/hip/vetta_kernels.hip @@ -613,6 +613,51 @@ extern "C" int vetta_shortconv4_decode(const float* cur, const float* static_w, return 0; } +// ---- k_shortconv4_dyn_decode: full-attn ShortConv4 with dynamic taps -------- +// Exact strata k2_shortconv4 (dyn): y = x0 + sw0*h0+sw1*h1+sw2*h2+sw3*x0; +// y += dt0*x0 + dt1*h2 + dt2*h1 + dt3*h0; y *= 1/3. +// dyn_taps [M, n_heads, 4]; head = c/(C/n_heads). static_w [C,4]. History shift. + +__global__ void k_shortconv4_dyn_decode(const float* __restrict__ cur, + const float* __restrict__ static_w, + const float* __restrict__ dyn_taps, + float* __restrict__ hist, + float* __restrict__ out, + const int32_t* __restrict__ slot_ids, + int M, int C, int n_heads) { + const int c = blockIdx.x * blockDim.x + threadIdx.x; + const int m = blockIdx.y; + if (c >= C || m >= M) return; + const int slot = slot_ids[m]; + float* h = hist + (size_t)slot * 3 * C; + const float h0 = h[0 * C + c]; + const float h1 = h[1 * C + c]; + const float h2 = h[2 * C + c]; + const float x0 = cur[(size_t)m * C + c]; + const float* sw = static_w + (size_t)c * 4; + float y = x0 + sw[0] * h0 + sw[1] * h1 + sw[2] * h2 + sw[3] * x0; + const int head = c / (C / n_heads); + const float* dt = dyn_taps + ((size_t)m * n_heads + head) * 4; + y += dt[0] * x0 + dt[1] * h2 + dt[2] * h1 + dt[3] * h0; + y *= (1.0f / 3.0f); + h[0 * C + c] = h1; + h[1 * C + c] = h2; + h[2 * C + c] = x0; + out[(size_t)m * C + c] = y; +} + +extern "C" int vetta_shortconv4_dyn_decode(const float* cur, const float* static_w, + const float* dyn_taps, float* hist, float* out, + const int32_t* slot_ids, int M, int C, int n_heads, + vetta_stream_t stream) { + if (M <= 0 || C <= 0 || n_heads <= 0 || (C % n_heads) != 0) return 1; + dim3 grid((C + 255) / 256, M); + hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0; + k_shortconv4_dyn_decode<<>>(cur, static_w, dyn_taps, hist, out, slot_ids, M, C, n_heads); + HIP_CHECK(hipGetLastError()); + return 0; +} + // ---- k_attn_res_mix: Inkling AttnRes deep-residual block-mix (parity) ------ // Exact strata k1_attn_res_mix: for each query q and token m, mix n_states = // n_blocks+1 depth states (block snapshots + current). Per state s: