[HIP] DSpark brick 3a: mix_taps + per-head RMSNorm/NeoX-RoPE kernels + parity

Exact port of strata k_dspark_mix_taps (softmax(fuse_w[row]) weighted sum of 4
target taps + RMSNorm) and k_dspark_rmsnorm_rope (per-head RMSNorm + NeoX
half-split RoPE pairs (d,d+hd/2)). f32 activations; bf16 store is the caller's.
RoPE freq/trig computed in f64 internally (f32 angle) for tight parity at large
pos; the downstream bf16 ctx store makes the extra precision moot.
Host wrappers dspark_mix_taps / dspark_rmsnorm_rope + CPU-oracle parity tests
(both < 1e-4). hip 39/39; CPU -D warnings green; speculative_draft.lean 0 err.
This commit is contained in:
adm committed 2026-12-02 20:21:54 +01:00
1 parent 3a9533afd0
commit 7e74db7ba5
2 files changed
+224

No files matched your search

+119
View File
@@ -38,6 +38,15 @@ mod ffi {
x: *const c_float, w: *const c_float, y: *mut c_float,
rows: c_int, d: c_int, eps: c_float, stream: *mut c_void,
) -> c_int;
pub fn vetta_dspark_mix_taps(
t0: *const c_float, t1: *const c_float, t2: *const c_float, t3: *const c_float,
fuse_w: *const c_float, row: c_int, norm_w: *const c_float, out: *mut c_float,
m: c_int, d: c_int, eps: c_float, stream: *mut c_void,
) -> c_int;
pub fn vetta_dspark_rmsnorm_rope(
x: *mut c_float, nw: *const c_float, pos: *const c_int, theta: c_float,
m: c_int, h: c_int, hd: c_int, eps: c_float, stream: *mut c_void,
) -> c_int;
pub fn vetta_embed_bf16_gather(
table: *const u16, tokens: *const i32, out: *mut c_float,
b: c_int, d: c_int, vocab: c_int, stream: *mut c_void,
@@ -411,6 +420,33 @@ pub fn rms_norm_f32(x: &DeviceBuf, w: Option<&DeviceBuf>, y: &DeviceBuf, rows: i
Ok(())
}
/// DSpark: mix 4 tap buffers [M,D] with softmax(fuse_w[row]) + RMSNorm(norm_w).
#[cfg(feature = "hip")]
#[allow(clippy::too_many_arguments)]
pub fn dspark_mix_taps(taps: [&DeviceBuf; 4], fuse_w: &DeviceBuf, row: i32, norm_w: &DeviceBuf, out: &DeviceBuf, m: i32, d: i32, eps: f32) -> Result<(), String> {
let rc = unsafe {
ffi::vetta_dspark_mix_taps(
taps[0].ptr as *const f32, taps[1].ptr as *const f32,
taps[2].ptr as *const f32, taps[3].ptr as *const f32,
fuse_w.ptr as *const f32, row, norm_w.ptr as *const f32,
out.ptr as *mut f32, m, d, eps, std::ptr::null_mut())
};
if rc != 0 { return Err("dspark_mix_taps launch failed".into()); }
Ok(())
}
/// DSpark: in-place per-head RMSNorm(nw) + NeoX half-split RoPE(theta) on x
/// [M,H,hd]; pos[M] gives each row's absolute position.
#[cfg(feature = "hip")]
#[allow(clippy::too_many_arguments)]
pub fn dspark_rmsnorm_rope(x: &DeviceBuf, nw: &DeviceBuf, pos: &DeviceBuf, theta: f32, m: i32, h: i32, hd: i32, eps: f32) -> Result<(), String> {
let rc = unsafe {
ffi::vetta_dspark_rmsnorm_rope(x.ptr as *mut f32, nw.ptr as *const f32, pos.ptr as *const i32, theta, m, h, hd, eps, std::ptr::null_mut())
};
if rc != 0 { return Err("dspark_rmsnorm_rope launch failed".into()); }
Ok(())
}
/// Launch k_embed_bf16_gather: `b` token rows from BF16 table `[vocab, d]` → f32.
#[cfg(feature = "hip")]
pub fn embed_bf16_gather(table: &DeviceBuf, tokens: &DeviceBuf, out: &DeviceBuf, b: i32, d: i32, vocab: i32) -> Result<(), String> {
@@ -1009,6 +1045,89 @@ mod gpu_tests {
}
}
/// DSpark mix_taps: softmax(fuse_w[row]) weighted sum of 4 taps + RMSNorm.
#[test]
#[ignore]
fn dspark_mix_taps_parity() {
let (m, d) = (3usize, 2304usize);
let eps = 1e-6f32;
let row = 1i32;
let taps: Vec<Vec<f32>> = (0..4)
.map(|t| (0..m * d).map(|i| ((i % 41) as f32 - 20.0) * 0.05 + t as f32 * 0.1).collect())
.collect();
let fuse: Vec<f32> = (0..16).map(|i| ((i % 7) as f32 - 3.0) * 0.3).collect();
let nw: Vec<f32> = (0..d).map(|i| 0.5 + (i % 13) as f32 * 0.05).collect();
// oracle
let fw = &fuse[row as usize * 4..row as usize * 4 + 4];
let mx = fw.iter().cloned().fold(f32::NEG_INFINITY, f32::max);
let e: Vec<f32> = fw.iter().map(|v| (v - mx).exp()).collect();
let es: f32 = e.iter().sum();
let sw: Vec<f32> = e.iter().map(|v| v / es).collect();
let mut want = vec![0f32; m * d];
for mi in 0..m {
let mut ss = 0f32;
for i in 0..d {
let mut v = 0f32;
for t in 0..4 { v += sw[t] * taps[t][mi * d + i]; }
want[mi * d + i] = v;
ss += v * v;
}
let inv = 1.0f32 / (ss / d as f32 + eps).sqrt();
for i in 0..d { want[mi * d + i] *= inv * nw[i]; }
}
let dt: Vec<DeviceBuf> = taps.iter().map(|t| { let b = DeviceBuf::alloc(t.len() * 4).unwrap(); b.h2d(t).unwrap(); b }).collect();
let df = DeviceBuf::alloc(fuse.len() * 4).unwrap(); df.h2d(&fuse).unwrap();
let dn = DeviceBuf::alloc(nw.len() * 4).unwrap(); dn.h2d(&nw).unwrap();
let dy = DeviceBuf::alloc(want.len() * 4).unwrap();
dspark_mix_taps([&dt[0], &dt[1], &dt[2], &dt[3]], &df, row, &dn, &dy, m as i32, d as i32, eps).unwrap();
let mut got = vec![0f32; want.len()];
dy.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)); }
eprintln!("dspark_mix_taps max_rel_err={max_rel:.3e}");
assert!(max_rel < 1e-4, "dspark_mix_taps parity failed: {max_rel}");
}
/// DSpark per-head RMSNorm + NeoX half-split RoPE (theta=1e6), in-place.
#[test]
#[ignore]
fn dspark_rmsnorm_rope_parity() {
let (m, h, hd) = (2usize, 8usize, 128usize);
let eps = 1e-6f32;
let theta = 1e6f32;
let pos: Vec<i32> = vec![5, 1024];
let nw: Vec<f32> = (0..hd).map(|i| 0.5 + (i % 11) as f32 * 0.06).collect();
let x: Vec<f32> = (0..m * h * hd).map(|i| ((i % 37) as f32 - 18.0) * 0.08).collect();
let mut want = x.clone();
for mi in 0..m {
for hi in 0..h {
let base = (mi * h + hi) * hd;
let ss: f32 = (0..hd).map(|i| x[base + i] * x[base + i]).sum();
let inv = 1.0f32 / (ss / hd as f32 + eps).sqrt();
let t: Vec<f32> = (0..hd).map(|i| x[base + i] * inv * nw[i]).collect();
for i in 0..hd / 2 {
let freq = (1.0f64 / (theta as f64).powf((2 * i) as f64 / hd as f64)) as f32;
let ang = pos[mi] as f32 * freq;
let s = (ang as f64).sin() as f32;
let c = (ang as f64).cos() as f32;
want[base + i] = t[i] * c - t[i + hd / 2] * s;
want[base + i + hd / 2] = t[i] * s + t[i + hd / 2] * c;
}
}
}
let dx = DeviceBuf::alloc(x.len() * 4).unwrap(); dx.h2d(&x).unwrap();
let dn = DeviceBuf::alloc(nw.len() * 4).unwrap(); dn.h2d(&nw).unwrap();
let dp = DeviceBuf::alloc(pos.len() * 4).unwrap();
dp.h2d_bytes(&pos.iter().flat_map(|v| v.to_le_bytes()).collect::<Vec<u8>>()).unwrap();
dspark_rmsnorm_rope(&dx, &dn, &dp, theta, m as i32, h as i32, hd as i32, eps).unwrap();
let mut got = vec![0f32; x.len()];
dx.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)); }
eprintln!("dspark_rmsnorm_rope max_rel_err={max_rel:.3e}");
assert!(max_rel < 1e-4, "dspark_rmsnorm_rope parity failed: {max_rel}");
}
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) }