[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:
1 parent
3a2ad4da49
commit
51b230cdd2
3 files changed
+88
-10
No files matched your search
@@ -77,6 +77,7 @@ mod ffi {
|
|||||||
) -> c_int;
|
) -> 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_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_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_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_cast_f32_f16(x: *const c_float, y: *mut u16, n: usize, stream: *mut c_void) -> c_int;
|
||||||
pub fn vetta_nvfp4_gemv(
|
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_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_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_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(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_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;
|
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(())
|
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).
|
/// Launch k_cast_f32_bf16: f32 activation -> BF16 (round-to-nearest-even).
|
||||||
#[cfg(feature = "hip")]
|
#[cfg(feature = "hip")]
|
||||||
pub fn cast_f32_bf16(x: &DeviceBuf, y: &DeviceBuf, n: usize) -> Result<(), String> {
|
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")]
|
#[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> {
|
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 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()); }
|
if rc != 0 { return Err("l2_clip launch failed".into()); }
|
||||||
Ok(())
|
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].
|
/// Launch k_relpos_bias: rel_bias[h,dist] = sum_rc R[h,rc]*rel_proj[rc,dist].
|
||||||
#[cfg(feature = "hip")]
|
#[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> {
|
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]
|
#[test]
|
||||||
#[ignore]
|
#[ignore]
|
||||||
fn flash_swa_gqa_prefill_f16_parity() {
|
fn flash_swa_gqa_prefill_f16_parity() {
|
||||||
let (n_head, n_kv, hd, seq, b) = (8usize, 2usize, 128usize, 64usize, 20usize);
|
let (n_head, n_kv, hd, seq, b) = (24usize, 8usize, 128usize, 15usize, 8usize);
|
||||||
let (base, win, max_rel) = (10i32, 16i32, 16usize);
|
let (base, win, max_rel) = (5i32, 1024i32, 1024usize);
|
||||||
let scale = 1.0f32 / (hd as f32).sqrt();
|
let scale = 1.0f32 / (hd as f32).sqrt();
|
||||||
let max_pos = base + (b as i32) - 1;
|
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();
|
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}");
|
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.
|
/// Batched relpos bias (M=B, lim per token) vs M sequential relpos_bias.
|
||||||
#[test]
|
#[test]
|
||||||
#[ignore]
|
#[ignore]
|
||||||
|
|||||||
@@ -149,7 +149,7 @@ int vetta_attn_res_mix(const float* blocks, const float* cur, const float* queri
|
|||||||
|
|
||||||
// k_l2_clip: per-head L2 clip + optional per-dim scale + optional tau (in place).
|
// k_l2_clip: per-head L2 clip + optional per-dim scale + optional tau (in place).
|
||||||
int vetta_l2_clip(float* x, const float* scale, int rows, int hd, float clip,
|
int vetta_l2_clip(float* x, const float* scale, int rows, int hd, float clip,
|
||||||
int apply_tau, int pos, vetta_stream_t stream);
|
int apply_tau, int pos, int scale_rows, vetta_stream_t stream);
|
||||||
|
|
||||||
// k_relpos_bias: rel_bias[h,dist] = sum_rc R[h,rc]*rel_proj[rc,dist], dist<lim.
|
// k_relpos_bias: rel_bias[h,dist] = sum_rc R[h,rc]*rel_proj[rc,dist], dist<lim.
|
||||||
int vetta_relpos_bias(const float* r, const float* rel_proj, float* rel_bias,
|
int vetta_relpos_bias(const float* r, const float* rel_proj, float* rel_bias,
|
||||||
|
|||||||
@@ -681,6 +681,26 @@ extern "C" int vetta_copy_f32(const float* src, float* dst, size_t n, vetta_stre
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ---- k_gather_channel: extract controller message channel ch as [B,h] -------
|
||||||
|
// msgs is [B*nq, h] (attn_res_mix out[(m*nq+q)*h]); dst[m,h] = msgs[(m*nq+ch)*h].
|
||||||
|
// Strided gather so apply_controller_batched reads a contiguous [B,h] channel.
|
||||||
|
__global__ void k_gather_channel(const float* __restrict__ msgs, float* __restrict__ dst,
|
||||||
|
int ch, int B, int h, int nq) {
|
||||||
|
const int d = blockIdx.x * blockDim.x + threadIdx.x;
|
||||||
|
const int m = blockIdx.y;
|
||||||
|
if (d >= h || m >= B) return;
|
||||||
|
dst[(size_t)m * h + d] = msgs[((size_t)m * nq + ch) * h + d];
|
||||||
|
}
|
||||||
|
|
||||||
|
extern "C" int vetta_gather_channel(const float* msgs, float* dst, int ch, int B, int h, int nq, vetta_stream_t stream) {
|
||||||
|
if (B <= 0 || h <= 0 || nq <= 0 || ch < 0 || ch >= nq) return 1;
|
||||||
|
dim3 grid((h + 255) / 256, B);
|
||||||
|
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
|
||||||
|
k_gather_channel<<<grid, 256, 0, s>>>(msgs, dst, ch, B, h, nq);
|
||||||
|
HIP_CHECK(hipGetLastError());
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
// ---- k_router_gemv: y[N] = x_f32[K] @ w_bf16[N,K]^T, f32 accumulate --------
|
// ---- k_router_gemv: y[N] = x_f32[K] @ w_bf16[N,K]^T, f32 accumulate --------
|
||||||
// Exact strata B=1 router (launch_f32_linear): the router reads the F32 mlpin
|
// Exact strata B=1 router (launch_f32_linear): the router reads the F32 mlpin
|
||||||
// (NOT bf16-rounded) against the bf16 gate weight promoted to f32. Rounding the
|
// (NOT bf16-rounded) against the bf16 gate weight promoted to f32. Rounding the
|
||||||
@@ -1607,13 +1627,14 @@ extern "C" int vetta_attn_res_mix(const float* blocks, const float* cur, const f
|
|||||||
// (pos+1)/8192)); x = v*fac. One block per row (head), in place.
|
// (pos+1)/8192)); x = v*fac. One block per row (head), in place.
|
||||||
|
|
||||||
__global__ void k_l2_clip(float* __restrict__ x, const float* __restrict__ scale,
|
__global__ void k_l2_clip(float* __restrict__ x, const float* __restrict__ scale,
|
||||||
int rows, int hd, float clip, int apply_tau, int pos) {
|
int rows, int hd, float clip, int apply_tau, int pos, int scale_rows) {
|
||||||
const int r = blockIdx.x;
|
const int r = blockIdx.x;
|
||||||
if (r >= rows) return;
|
if (r >= rows) return;
|
||||||
const int tid = threadIdx.x;
|
const int tid = threadIdx.x;
|
||||||
const int nt = blockDim.x;
|
const int nt = blockDim.x;
|
||||||
float* xv = x + (size_t)r * hd;
|
float* xv = x + (size_t)r * hd;
|
||||||
const float* sc = scale ? (scale + (size_t)r * hd) : nullptr;
|
// Per-head scale repeats every scale_rows rows (batched rows = M*n_head).
|
||||||
|
const float* sc = scale ? (scale + (size_t)(r % scale_rows) * hd) : nullptr;
|
||||||
__shared__ float red[256];
|
__shared__ float red[256];
|
||||||
__shared__ float fac_s;
|
__shared__ float fac_s;
|
||||||
const float v = (tid < hd) ? (xv[tid] * (sc ? sc[tid] : 1.f)) : 0.f;
|
const float v = (tid < hd) ? (xv[tid] * (sc ? sc[tid] : 1.f)) : 0.f;
|
||||||
@@ -1633,10 +1654,10 @@ __global__ void k_l2_clip(float* __restrict__ x, const float* __restrict__ scale
|
|||||||
}
|
}
|
||||||
|
|
||||||
extern "C" int vetta_l2_clip(float* x, const float* scale, int rows, int hd,
|
extern "C" int vetta_l2_clip(float* x, const float* scale, int rows, int hd,
|
||||||
float clip, int apply_tau, int pos, vetta_stream_t stream) {
|
float clip, int apply_tau, int pos, int scale_rows, vetta_stream_t stream) {
|
||||||
if (rows <= 0 || hd <= 0 || hd > 256) return 1;
|
if (rows <= 0 || hd <= 0 || hd > 256 || scale_rows <= 0) return 1;
|
||||||
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
|
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
|
||||||
k_l2_clip<<<rows, 256, 0, s>>>(x, scale, rows, hd, clip, apply_tau, pos);
|
k_l2_clip<<<rows, 256, 0, s>>>(x, scale, rows, hd, clip, apply_tau, pos, scale_rows);
|
||||||
HIP_CHECK(hipGetLastError());
|
HIP_CHECK(hipGetLastError());
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user