diff --git a/crates/vetta-infer-hip/src/lib.rs b/crates/vetta-infer-hip/src/lib.rs index edf775e..5f04432 100644 --- a/crates/vetta-infer-hip/src/lib.rs +++ b/crates/vetta-infer-hip/src/lib.rs @@ -170,6 +170,7 @@ mod ffi { 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_shortconv4_hist_snaps(in_x: *const c_float, hist0: *const c_float, snaps: *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, scale_rows: c_int, pos_arr: *const 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; @@ -951,6 +952,16 @@ pub fn shortconv4_update_hist(in_x: &DeviceBuf, hist: &DeviceBuf, b: i32, c: i32 Ok(()) } +/// Per-prefix ShortConv history snapshots: snaps[t] (t=0..B) = [3,C] history +/// after t tokens of in_x from hist0 (same bf16-round/shift as update_hist). +/// snaps buffer must hold (B+1)*3*C f32. For DSpark partial-accept rollback. +#[cfg(feature = "hip")] +pub fn shortconv4_hist_snaps(in_x: &DeviceBuf, hist0: &DeviceBuf, snaps: &DeviceBuf, b: i32, c: i32) -> Result<(), String> { + let rc = unsafe { ffi::vetta_shortconv4_hist_snaps(in_x.ptr as *const f32, hist0.ptr as *const f32, snaps.ptr as *mut f32, b, c, std::ptr::null_mut()) }; + if rc != 0 { return Err("shortconv4_hist_snaps launch failed".into()); } + Ok(()) +} + /// Launch k_attn_res_mix: Inkling AttnRes deep-residual block-mix. #[cfg(feature = "hip")] #[allow(clippy::too_many_arguments)] @@ -1340,6 +1351,31 @@ mod gpu_tests { assert_eq!(got, want, "corr_combine must be exact"); } + /// hist_snaps[t] must equal the verified update_hist applied to the first t + /// tokens (from hist0), for every t=0..B. Exact (same bf16-round/shift). + #[test] + #[ignore] + fn shortconv4_hist_snaps_parity() { + let (b, c) = (8usize, 64usize); + let hist0: Vec = (0..3 * c).map(|i| ((i % 23) as f32 - 11.0) * 0.1).collect(); + let in_x: Vec = (0..b * c).map(|i| ((i % 41) as f32 - 20.0) * 0.07).collect(); + let dx = DeviceBuf::alloc(in_x.len() * 4).unwrap(); dx.h2d(&in_x).unwrap(); + let dh0 = DeviceBuf::alloc(hist0.len() * 4).unwrap(); dh0.h2d(&hist0).unwrap(); + let snaps = DeviceBuf::alloc((b + 1) * 3 * c * 4).unwrap(); + shortconv4_hist_snaps(&dx, &dh0, &snaps, b as i32, c as i32).unwrap(); + let mut gs = vec![0f32; (b + 1) * 3 * c]; + snaps.d2h(&mut gs).unwrap(); + // t=0 + assert_eq!(&gs[0..3 * c], &hist0[..], "snaps[0] must be hist0"); + for t in 1..=b { + let dh = DeviceBuf::alloc(hist0.len() * 4).unwrap(); dh.h2d(&hist0).unwrap(); + shortconv4_update_hist(&dx, &dh, t as i32, c as i32).unwrap(); + let mut want = vec![0f32; 3 * c]; dh.d2h(&mut want).unwrap(); + assert_eq!(&gs[t * 3 * c..(t + 1) * 3 * c], &want[..], "snaps[{t}] != update_hist prefix"); + } + eprintln!("shortconv4_hist_snaps: all {} prefixes exact", b + 1); + } + fn f32_to_bf16(v: f32) -> u16 { let u = v.to_bits(); ((u + 0x7FFFu32 + ((u >> 16) & 1)) >> 16) as u16 } fn bf16_to_f32(h: u16) -> f32 { f32::from_bits((h as u32) << 16) } diff --git a/kernels/hip/vetta_kernels.hip b/kernels/hip/vetta_kernels.hip index a8a9d70..c3bbe30 100644 --- a/kernels/hip/vetta_kernels.hip +++ b/kernels/hip/vetta_kernels.hip @@ -1980,6 +1980,50 @@ extern "C" int vetta_shortconv4_update_hist(const float* in_x, float* hist, int return 0; } +// ---- k_shortconv4_hist_snaps: per-prefix ShortConv history snapshots -------- +// snaps[t] (t=0..B) = the [3,C] history after processing t tokens of in_x from +// hist0, with the SAME bf16-round + shift semantics as update_hist. Used for +// DSpark partial-accept rollback: hist <- snaps[accepted] (KV ring truncates). +__global__ void k_shortconv4_hist_snaps(const float* __restrict__ in_x, + const float* __restrict__ hist0, + float* __restrict__ snaps, int B, + int C) { + const int c = blockIdx.x * blockDim.x + threadIdx.x; + if (c >= C) return; + snaps[(size_t)0 * 3 * C + 0 * C + c] = hist0[0 * C + c]; + snaps[(size_t)0 * 3 * C + 1 * C + c] = hist0[1 * C + c]; + snaps[(size_t)0 * 3 * C + 2 * C + c] = hist0[2 * C + c]; + for (int t = 1; t <= B; ++t) { + float h0, h1, h2; + if (t >= 3) { + h0 = bf16_round(in_x[(size_t)(t - 3) * C + c]); + h1 = bf16_round(in_x[(size_t)(t - 2) * C + c]); + h2 = bf16_round(in_x[(size_t)(t - 1) * C + c]); + } else if (t == 2) { + h0 = hist0[2 * C + c]; + h1 = bf16_round(in_x[0 * C + c]); + h2 = bf16_round(in_x[1 * C + c]); + } else { + h0 = hist0[1 * C + c]; + h1 = hist0[2 * C + c]; + h2 = bf16_round(in_x[0 * C + c]); + } + snaps[(size_t)t * 3 * C + 0 * C + c] = h0; + snaps[(size_t)t * 3 * C + 1 * C + c] = h1; + snaps[(size_t)t * 3 * C + 2 * C + c] = h2; + } +} + +extern "C" int vetta_shortconv4_hist_snaps(const float* in_x, const float* hist0, + float* snaps, int B, int C, + vetta_stream_t stream) { + if (B <= 0 || C <= 0) return 1; + hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0; + k_shortconv4_hist_snaps<<<(C + 255) / 256, 256, 0, s>>>(in_x, hist0, snaps, B, C); + 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: