[HIP] DSpark brick 5b-1: per-prefix ShortConv history snapshots kernel

k_shortconv4_hist_snaps: snaps[t] (t=0..B) = [3,C] history after t tokens of
in_x from hist0, same bf16-round/shift semantics as update_hist. Enables exact
DSpark partial-accept rollback (hist <- snaps[accepted]); the KV ring truncates
with no data restore (proofs/speculative_draft.lean kept_rejected_disjoint).
Parity: snaps[t] == verified update_hist on prefix t, all 9 prefixes EXACT.
This commit is contained in:
adm committed 2026-12-02 20:56:49 +01:00
1 parent 3812845f1a
commit ca12801b40
2 files changed
+80

No files matched your search

+36
View File
@@ -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<f32> = (0..3 * c).map(|i| ((i % 23) as f32 - 11.0) * 0.1).collect();
let in_x: Vec<f32> = (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) }