[HIP] l2_clip scale_rows fix (batched per-head scale) + gather_channel + copy_f32_at + l2_clip_batched

k_l2_clip indexed the per-head scale as scale[r*hd] with r=row. For M=1
rows=n_head so r=head (correct); for batched rows=M*n_head the rows >= n_head
read the [n_head,hd] scale OUT OF BOUNDS, corrupting q/r (r_raw clipped to 0).
Add scale_rows param: sc = scale[(r % scale_rows)*hd]. l2_clip passes rows
(M=1 unchanged); new l2_clip_batched passes n_head. Regression test
l2_clip_batched_parity (M=8,n_head=24,hd=128) bit-exact vs sequential.

Also: k_gather_channel (strided msgs channel [B*nq,h]->[B,h] for batched
controllers), copy_f32_at (offset copy into depth-block arena). Flash prefill
parity test now exercises the real config (n_head=24,n_kv=8,hd=128,win=1024).
This commit is contained in:
adm committed 2026-12-02 00:20:55 +01:00
1 parent 3a2ad4da49
commit 51b230cdd2
3 files changed
+88 -10

No files matched your search

+61 -4
View File
@@ -77,6 +77,7 @@ mod ffi {
) -> c_int;
pub fn vetta_vec_add(a: *const c_float, b: *const c_float, y: *mut c_float, n: usize, stream: *mut c_void) -> c_int;
pub fn vetta_copy_f32(src: *const c_float, dst: *mut c_float, n: usize, stream: *mut c_void) -> c_int;
pub fn vetta_gather_channel(msgs: *const c_float, dst: *mut c_float, ch: c_int, b: c_int, h: c_int, nq: c_int, stream: *mut c_void) -> c_int;
pub fn vetta_cast_f32_bf16(x: *const c_float, y: *mut u16, n: usize, stream: *mut c_void) -> c_int;
pub fn vetta_cast_f32_f16(x: *const c_float, y: *mut u16, n: usize, stream: *mut c_void) -> c_int;
pub fn vetta_nvfp4_gemv(
@@ -121,7 +122,7 @@ mod ffi {
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_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_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, 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;
pub fn vetta_relpos_bias_batched(r: *const c_float, rel_proj: *const c_float, rel_bias: *mut c_float, pos: *const c_int, n_head: c_int, d_rel: c_int, rel_extent: c_int, m: c_int, stream: *mut c_void) -> c_int;
pub fn vetta_kv_store(k_in: *const c_float, v_in: *const c_float, k_cache: *mut c_float, v_cache: *mut c_float, pos: c_int, n_kv: c_int, hd: c_int, seq: c_int, rms_eps: c_float, stream: *mut c_void) -> c_int;
@@ -461,6 +462,24 @@ pub fn copy_f32(src: &DeviceBuf, dst: &DeviceBuf, n: usize) -> Result<(), String
Ok(())
}
/// Device-side f32 copy into `dst` at element offset `dst_offset` (arena snapshot).
#[cfg(feature = "hip")]
pub fn copy_f32_at(src: &DeviceBuf, dst: &DeviceBuf, dst_offset: usize, n: usize) -> Result<(), String> {
if dst_offset * 4 + n * 4 > dst.bytes { return Err("copy_f32_at overflow".into()); }
let dst_ptr = unsafe { (dst.ptr as *mut f32).add(dst_offset) };
let rc = unsafe { ffi::vetta_copy_f32(src.ptr as *const f32, dst_ptr, n, std::ptr::null_mut()) };
if rc != 0 { return Err("copy_f32_at launch failed".into()); }
Ok(())
}
/// Gather controller message channel `ch` from msgs [B*nq,h] -> dst [B,h] (strided).
#[cfg(feature = "hip")]
pub fn gather_channel(msgs: &DeviceBuf, dst: &DeviceBuf, ch: i32, b: i32, h: i32, nq: i32) -> Result<(), String> {
let rc = unsafe { ffi::vetta_gather_channel(msgs.ptr as *const f32, dst.ptr as *mut f32, ch, b, h, nq, std::ptr::null_mut()) };
if rc != 0 { return Err("gather_channel launch failed".into()); }
Ok(())
}
/// Launch k_cast_f32_bf16: f32 activation -> BF16 (round-to-nearest-even).
#[cfg(feature = "hip")]
pub fn cast_f32_bf16(x: &DeviceBuf, y: &DeviceBuf, n: usize) -> Result<(), String> {
@@ -696,11 +715,23 @@ pub fn attn_res_mix(blocks: &DeviceBuf, cur: &DeviceBuf, queries: &DeviceBuf, ou
#[cfg(feature = "hip")]
pub fn l2_clip(x: &DeviceBuf, scale: Option<&DeviceBuf>, rows: i32, hd: i32, clip: f32, apply_tau: bool, pos: i32) -> Result<(), String> {
let sc = scale.map(|b| b.ptr as *const f32).unwrap_or(std::ptr::null());
let rc = unsafe { ffi::vetta_l2_clip(x.ptr as *mut f32, sc, rows, hd, clip, apply_tau as i32, pos, std::ptr::null_mut()) };
let rc = unsafe { ffi::vetta_l2_clip(x.ptr as *mut f32, sc, rows, hd, clip, apply_tau as i32, pos, rows, std::ptr::null_mut()) };
if rc != 0 { return Err("l2_clip launch failed".into()); }
Ok(())
}
/// Batched L2 clip (rows = M*n_head): the per-head scale repeats every
/// `scale_rows` (=n_head) rows, so row r uses scale[r % scale_rows]. Without this
/// the batched rows >= n_head would index the [n_head,hd] scale out of bounds.
#[cfg(feature = "hip")]
#[allow(clippy::too_many_arguments)]
pub fn l2_clip_batched(x: &DeviceBuf, scale: Option<&DeviceBuf>, rows: i32, hd: i32, clip: f32, apply_tau: bool, pos: i32, scale_rows: i32) -> Result<(), String> {
let sc = scale.map(|b| b.ptr as *const f32).unwrap_or(std::ptr::null());
let rc = unsafe { ffi::vetta_l2_clip(x.ptr as *mut f32, sc, rows, hd, clip, apply_tau as i32, pos, scale_rows, std::ptr::null_mut()) };
if rc != 0 { return Err("l2_clip_batched launch failed".into()); }
Ok(())
}
/// Launch k_relpos_bias: rel_bias[h,dist] = sum_rc R[h,rc]*rel_proj[rc,dist].
#[cfg(feature = "hip")]
pub fn relpos_bias(r: &DeviceBuf, rel_proj: &DeviceBuf, rel_bias: &DeviceBuf, n_head: i32, d_rel: i32, rel_extent: i32, lim: i32) -> Result<(), String> {
@@ -1448,8 +1479,8 @@ mod gpu_tests {
#[test]
#[ignore]
fn flash_swa_gqa_prefill_f16_parity() {
let (n_head, n_kv, hd, seq, b) = (8usize, 2usize, 128usize, 64usize, 20usize);
let (base, win, max_rel) = (10i32, 16i32, 16usize);
let (n_head, n_kv, hd, seq, b) = (24usize, 8usize, 128usize, 15usize, 8usize);
let (base, win, max_rel) = (5i32, 1024i32, 1024usize);
let scale = 1.0f32 / (hd as f32).sqrt();
let max_pos = base + (b as i32) - 1;
let kf: Vec<f32> = (0..n_kv * seq * hd).map(|i| ((i % 41) as f32 - 20.0) * 0.02).collect();
@@ -1633,6 +1664,32 @@ mod gpu_tests {
assert!(max_rel < 1e-6, "router batched parity failed: {max_rel}");
}
/// Batched l2_clip (rows=M*n_head) must match M sequential per-head l2_clip:
/// the per-head scale repeats every n_head rows (r % scale_rows). Regression
/// guard for the out-of-bounds scale indexing that corrupted batched q/r.
#[test]
#[ignore]
fn l2_clip_batched_parity() {
let (m, n_head, hd) = (8usize, 24usize, 128usize);
let clip = 22.627416997969522f32;
let x: Vec<f32> = (0..m * n_head * hd).map(|i| ((i % 41) as f32 - 20.0) * 0.3).collect();
let sc: Vec<f32> = (0..n_head * hd).map(|i| 0.5 + (i % 13) as f32 * 0.05).collect();
let dx = DeviceBuf::alloc(x.len() * 4).unwrap(); dx.h2d(&x).unwrap();
let dsc = DeviceBuf::alloc(sc.len() * 4).unwrap(); dsc.h2d(&sc).unwrap();
l2_clip_batched(&dx, Some(&dsc), (m * n_head) as i32, hd as i32, clip, false, 0, n_head as i32).unwrap();
let mut got = vec![0f32; x.len()]; dx.d2h(&mut got).unwrap();
let dx1 = DeviceBuf::alloc(n_head * hd * 4).unwrap();
let mut max_rel = 0f32;
for mi in 0..m {
dx1.h2d(&x[mi * n_head * hd..(mi + 1) * n_head * hd]).unwrap();
l2_clip(&dx1, Some(&dsc), n_head as i32, hd as i32, clip, false, 0).unwrap();
let mut o = vec![0f32; n_head * hd]; dx1.d2h(&mut o).unwrap();
for i in 0..n_head * hd { max_rel = max_rel.max((got[mi * n_head * hd + i] - o[i]).abs() / o[i].abs().max(1e-3)); }
}
eprintln!("l2_clip_batched M={m} n_head={n_head} hd={hd} max_rel_err={max_rel:.3e}");
assert!(max_rel < 1e-6, "batched l2_clip parity failed: {max_rel}");
}
/// Batched relpos bias (M=B, lim per token) vs M sequential relpos_bias.
#[test]
#[ignore]