[HIP] eliminate per-visit sync h2d: rel_bias in-kernel zero + async memset + grouped identity

Peer review (strata-ctl coder) flagged ~13.3 GB of blocking host zero-fill copies
per prompt: rel_bias [B,n_head,rel_extent] (25 MB at B=256) was h2d-zeroed every
layer visit, plus moe_batched dcnt/didx sync h2d drained the GPU queue each layer.
- k_relpos_bias / k_relpos_bias_batched now write 0.0f for dist>=lim (full
  rel_extent in-kernel); callers drop the host zero vec + sync h2d (M=1 too).
- vetta_buf_zero_async (hipMemsetAsync) + DeviceBuf::zero_async for dcnt.
- grouped gemv bucket_tok nullable: w2 passes None (tok=beg+t0+m identity in
  kernel), removing the didx identity h2d. moe_launch 2054ms -> 8ms per chunk.
Prefill 2048: 56 -> 52.3 s; parity EXACT (ld 23, hip 33, gw 4, Lean 6/6). Enables
host/device pipelining + is a graph-capture prerequisite. Big perf gap remains the
scalar GEMM/flash (WMMA next).
This commit is contained in:
adm committed 2026-12-02 01:40:20 +01:00
1 parent ac31924ebb
commit f25cd27cf0
4 files changed
+59 -35

No files matched your search

+19 -7
View File
@@ -33,6 +33,7 @@ mod ffi {
pub fn vetta_buf_copy2d(dst: *mut c_void, dpitch: usize, src: *const c_void, spitch: usize, width: usize, height: usize) -> c_int;
pub fn vetta_synchronize() -> c_int;
pub fn vetta_vram_used(out: *mut usize) -> c_int;
pub fn vetta_buf_zero_async(dst: *mut c_void, bytes: usize, stream: *mut c_void) -> c_int;
pub fn vetta_rms_norm_f32(
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,
@@ -316,6 +317,14 @@ impl DeviceBuf {
if rc != 0 { return Err("d2d copy2d failed".into()); }
Ok(())
}
/// Async zero-fill of the whole buffer on the default stream (hipMemsetAsync):
/// ordered before later kernels with NO blocking host round-trip. Replaces a
/// sync `h2d(&vec![0; n])` that would drain the GPU queue every layer visit.
pub fn zero_async(&self) -> Result<(), String> {
let rc = unsafe { ffi::vetta_buf_zero_async(self.ptr, self.bytes, std::ptr::null_mut()) };
if rc != 0 { return Err("memset async failed".into()); }
Ok(())
}
}
#[cfg(feature = "hip")]
@@ -631,9 +640,10 @@ pub fn situ_f16_batched(gu: &DeviceBuf, act: &DeviceBuf, inter: i32, beta: f32,
/// X [B,K] f16 -> Y [total_pairs,N] f32 bucket order.
#[cfg(feature = "hip")]
#[allow(clippy::too_many_arguments)]
pub fn mxfp8_grouped_gemv_f16(arena_weights: &DeviceBuf, arena_scales: &DeviceBuf, exp_off: &DeviceBuf, bucket_tok: &DeviceBuf, x: &DeviceBuf, y: &DeviceBuf, n: i32, k: i32, n_experts: i32, w_stride: i64, sc_stride: i64) -> Result<(), String> {
pub fn mxfp8_grouped_gemv_f16(arena_weights: &DeviceBuf, arena_scales: &DeviceBuf, exp_off: &DeviceBuf, bucket_tok: Option<&DeviceBuf>, x: &DeviceBuf, y: &DeviceBuf, n: i32, k: i32, n_experts: i32, w_stride: i64, sc_stride: i64) -> Result<(), String> {
let bt = bucket_tok.map(|b| b.ptr as *const i32).unwrap_or(std::ptr::null());
let rc = unsafe {
ffi::vetta_mxfp8_grouped_gemv_f16(arena_weights.ptr as *const u8, arena_scales.ptr as *const u8, exp_off.ptr as *const i32, bucket_tok.ptr as *const i32, x.ptr as *const u16, y.ptr as *mut f32, n, k, n_experts, w_stride, sc_stride, std::ptr::null_mut())
ffi::vetta_mxfp8_grouped_gemv_f16(arena_weights.ptr as *const u8, arena_scales.ptr as *const u8, exp_off.ptr as *const i32, bt, x.ptr as *const u16, y.ptr as *mut f32, n, k, n_experts, w_stride, sc_stride, std::ptr::null_mut())
};
if rc != 0 { return Err("mxfp8_grouped_gemv_f16 launch failed".into()); }
Ok(())
@@ -677,9 +687,10 @@ pub fn moe_scatter_buckets(topk_ids: &DeviceBuf, exp_cur: &DeviceBuf, bucket_tok
/// amortized MT-fold. X [B,K] f16, Y [total_pairs,N] f32 in bucket order.
#[cfg(feature = "hip")]
#[allow(clippy::too_many_arguments)]
pub fn nvfp4_grouped_gemv_f16(arena_packed: &DeviceBuf, arena_scales: &DeviceBuf, outer_arena: &DeviceBuf, exp_off: &DeviceBuf, bucket_tok: &DeviceBuf, x: &DeviceBuf, y: &DeviceBuf, n: i32, k: i32, n_experts: i32, w_stride: i64, sc_stride: i64) -> Result<(), String> {
pub fn nvfp4_grouped_gemv_f16(arena_packed: &DeviceBuf, arena_scales: &DeviceBuf, outer_arena: &DeviceBuf, exp_off: &DeviceBuf, bucket_tok: Option<&DeviceBuf>, x: &DeviceBuf, y: &DeviceBuf, n: i32, k: i32, n_experts: i32, w_stride: i64, sc_stride: i64) -> Result<(), String> {
let bt = bucket_tok.map(|b| b.ptr as *const i32).unwrap_or(std::ptr::null());
let rc = unsafe {
ffi::vetta_nvfp4_grouped_gemv_f16(arena_packed.ptr as *const u8, arena_scales.ptr as *const u8, outer_arena.ptr as *const f32, exp_off.ptr as *const i32, bucket_tok.ptr as *const i32, x.ptr as *const u16, y.ptr as *mut f32, n, k, n_experts, w_stride, sc_stride, std::ptr::null_mut())
ffi::vetta_nvfp4_grouped_gemv_f16(arena_packed.ptr as *const u8, arena_scales.ptr as *const u8, outer_arena.ptr as *const f32, exp_off.ptr as *const i32, bt, x.ptr as *const u16, y.ptr as *mut f32, n, k, n_experts, w_stride, sc_stride, std::ptr::null_mut())
};
if rc != 0 { return Err("nvfp4_grouped_gemv_f16 launch failed".into()); }
Ok(())
@@ -774,7 +785,8 @@ pub fn relpos_bias(r: &DeviceBuf, rel_proj: &DeviceBuf, rel_bias: &DeviceBuf, n_
}
/// Batched relpos bias (M=B): r [M,n_head,d_rel] -> rel_bias [M,n_head,rel_extent],
/// lim[m]=min(pos[m]+1,rel_extent). Caller zero-fills rel_bias.
/// lim[m]=min(pos[m]+1,rel_extent). Kernel writes the full rel_extent (0.0f beyond
/// lim), so the caller need NOT host zero-fill (no sync h2d).
#[cfg(feature = "hip")]
#[allow(clippy::too_many_arguments)]
pub fn relpos_bias_batched(r: &DeviceBuf, rel_proj: &DeviceBuf, rel_bias: &DeviceBuf, pos: &DeviceBuf, n_head: i32, d_rel: i32, rel_extent: i32, m: i32) -> Result<(), String> {
@@ -1395,7 +1407,7 @@ mod gpu_tests {
moe_prefix_buckets(&dcnt, &doff, &dcur).unwrap();
moe_scatter_buckets(&dids, &dcur, &dbtk, &ds2b, total as i32, top_k as i32).unwrap();
let dy = DeviceBuf::alloc(total * n * 4).unwrap();
nvfp4_grouped_gemv_f16(&ap, &asc, &ao, &doff, &dbtk, &dxh, &dy, n as i32, k as i32, n_exp as i32, w_stride as i64, sc_stride as i64).unwrap();
nvfp4_grouped_gemv_f16(&ap, &asc, &ao, &doff, Some(&dbtk), &dxh, &dy, n as i32, k as i32, n_exp as i32, w_stride as i64, sc_stride as i64).unwrap();
let mut got = vec![0f32; total * n]; dy.d2h(&mut got).unwrap();
let mut s2b_b = vec![0u8; total * 4]; ds2b.d2h_bytes(&mut s2b_b).unwrap();
let s2b: Vec<i32> = s2b_b.chunks_exact(4).map(|c| i32::from_le_bytes([c[0], c[1], c[2], c[3]])).collect();
@@ -1645,7 +1657,7 @@ mod gpu_tests {
moe_prefix_buckets(&dcnt, &doff, &dcur).unwrap();
moe_scatter_buckets(&dids, &dcur, &dbtk, &ds2b, total as i32, top_k as i32).unwrap();
let dy = DeviceBuf::alloc(total * n * 4).unwrap();
mxfp8_grouped_gemv_f16(&aw, &asc, &doff, &dbtk, &dxh, &dy, n as i32, k as i32, n_exp as i32, w_stride as i64, sc_stride as i64).unwrap();
mxfp8_grouped_gemv_f16(&aw, &asc, &doff, Some(&dbtk), &dxh, &dy, n as i32, k as i32, n_exp as i32, w_stride as i64, sc_stride as i64).unwrap();
let mut got = vec![0f32; total * n]; dy.d2h(&mut got).unwrap();
let mut s2b_b = vec![0u8; total * 4]; ds2b.d2h_bytes(&mut s2b_b).unwrap();
let s2b: Vec<i32> = s2b_b.chunks_exact(4).map(|c| i32::from_le_bytes([c[0], c[1], c[2], c[3]])).collect();
+9 -15
View File
@@ -342,8 +342,7 @@ pub fn layer_forward_swa(
let slot0 = DeviceBuf::alloc(4)?; slot0.h2d_bytes(&0i32.to_le_bytes())?;
let k_cv = DeviceBuf::alloc(kv_dim * 4)?;
let v_cv = DeviceBuf::alloc(kv_dim * 4)?;
let rel_bias = DeviceBuf::alloc(d.n_head * d.rel_extent * 4)?;
rel_bias.h2d(&vec![0f32; d.n_head * d.rel_extent])?;
let rel_bias = DeviceBuf::alloc(d.n_head * d.rel_extent * 4)?; // kernel writes all rel_extent (0.0f beyond lim): no sync host zero-fill
let aout = DeviceBuf::alloc(q_dim * 4)?;
let aout_bf = DeviceBuf::alloc(q_dim * 2)?;
let oproj = DeviceBuf::alloc(h * 4)?;
@@ -497,8 +496,7 @@ fn layer_forward_swa_batched(
let bu = b as usize;
let k_cv = DeviceBuf::alloc(bu * kv_dim * 4)?;
let v_cv = DeviceBuf::alloc(bu * kv_dim * 4)?;
let rel_bias = DeviceBuf::alloc(bu * d.n_head * d.rel_extent * 4)?;
rel_bias.h2d(&vec![0f32; bu * d.n_head * d.rel_extent])?;
let rel_bias = DeviceBuf::alloc(bu * d.n_head * d.rel_extent * 4)?; // kernel writes all rel_extent (0.0f beyond lim): no sync host zero-fill
let aout = DeviceBuf::alloc(bu * q_dim * 4)?;
let aout_bf = DeviceBuf::alloc(bu * q_dim * 2)?;
let oproj = DeviceBuf::alloc(bu * h * 4)?;
@@ -773,7 +771,7 @@ fn moe_batched(
let mlpin16 = DeviceBuf::alloc((b as usize) * h * 2)?;
vetta_infer_hip::cast_f32_f16(mlpin, &mlpin16, (b as usize) * h)?;
// Bucket CSR (device-side).
let dcnt = DeviceBuf::alloc(129 * 4)?; dcnt.h2d_bytes(&vec![0u8; 129 * 4])?;
let dcnt = DeviceBuf::alloc(129 * 4)?; dcnt.zero_async()?;
let doff = DeviceBuf::alloc(129 * 4)?;
let dcur = DeviceBuf::alloc(128 * 4)?;
let dbtk = DeviceBuf::alloc(total * 4)?;
@@ -783,15 +781,13 @@ fn moe_batched(
vetta_infer_hip::moe_scatter_buckets(ids, &dcur, &dbtk, &ds2b, total as i32, top_k as i32)?;
// w13 grouped GEMM: gather input tokens via bucket_tok -> gu [total, 2*inter].
let gu = DeviceBuf::alloc(total * 2 * inter * 4)?;
expert_arena_grouped_gemv_f16(&w13, &doff, &dbtk, &mlpin16, &gu)?;
expert_arena_grouped_gemv_f16(&w13, &doff, Some(&dbtk), &mlpin16, &gu)?;
let act16 = DeviceBuf::alloc(total * inter * 2)?;
vetta_infer_hip::situ_f16_batched(&gu, &act16, inter as i32, d.beta, d.lb, total as i32)?;
// w2 grouped GEMM: SiTU output is already bucket-ordered -> identity index.
let idx: Vec<i32> = (0..total as i32).collect();
let idx_bytes: Vec<u8> = idx.iter().flat_map(|x| x.to_le_bytes()).collect();
let didx = DeviceBuf::alloc(total * 4)?; didx.h2d_bytes(&idx_bytes)?;
// w2 grouped GEMM: SiTU output is already bucket-ordered -> identity index
// (bucket_tok=None reads tok = beg+t0+m in-kernel; no host identity h2d sync).
let y_part = DeviceBuf::alloc(total * h * 4)?;
expert_arena_grouped_gemv_f16(&w2, &doff, &didx, &act16, &y_part)?;
expert_arena_grouped_gemv_f16(&w2, &doff, None, &act16, &y_part)?;
// Scatter-back + weighted reduce per token.
let moeout = DeviceBuf::alloc((b as usize) * h * 4)?;
vetta_infer_hip::moe_reduce_buckets_masked(&y_part, &ds2b, weights, &moeout, b, top_k as i32, h as i32)?;
@@ -1151,8 +1147,7 @@ pub fn layer_forward_full(
vetta_infer_hip::l2_clip(&q, Some(&w.q_sc), d.n_head as i32, d.hd as i32, d.qk_clip, true, pos)?;
vetta_infer_hip::l2_clip(&r_raw, Some(&w.r_sc), d.n_head as i32, d.d_rel as i32, d.rel_clip, true, pos)?;
vetta_infer_hip::l2_clip(&k_cv, None, d.n_kv as i32, d.hd as i32, d.qk_clip, false, pos)?;
let rel_bias = DeviceBuf::alloc(d.n_head * d.rel_extent * 4)?;
rel_bias.h2d(&vec![0f32; d.n_head * d.rel_extent])?;
let rel_bias = DeviceBuf::alloc(d.n_head * d.rel_extent * 4)?; // kernel writes all rel_extent (0.0f beyond lim): no sync host zero-fill
let lim = (pos + 1).min(d.rel_extent as i32);
vetta_infer_hip::relpos_bias(&r_raw, &w.rel_proj, &rel_bias, d.n_head as i32, d.d_rel as i32, d.rel_extent as i32, lim)?;
if f16_kv() {
@@ -1278,8 +1273,7 @@ fn layer_forward_full_batched(
vetta_infer_hip::l2_clip_batched(&q, Some(&w.q_sc), (b as usize * d.n_head) as i32, d.hd as i32, d.qk_clip, true, 0, d.n_head as i32, Some(pos))?;
vetta_infer_hip::l2_clip_batched(&r_raw, Some(&w.r_sc), (b as usize * d.n_head) as i32, d.d_rel as i32, d.rel_clip, true, 0, d.n_head as i32, Some(pos))?;
vetta_infer_hip::l2_clip_batched(&k_cv, None, (b as usize * d.n_kv) as i32, d.hd as i32, d.qk_clip, false, 0, d.n_kv as i32, None)?;
let rel_bias = DeviceBuf::alloc(bu * d.n_head * d.rel_extent * 4)?;
rel_bias.h2d(&vec![0f32; bu * d.n_head * d.rel_extent])?;
let rel_bias = DeviceBuf::alloc(bu * d.n_head * d.rel_extent * 4)?; // kernel writes all rel_extent (0.0f beyond lim): no sync host zero-fill
vetta_infer_hip::relpos_bias_batched(&r_raw, &w.rel_proj, &rel_bias, pos, d.n_head as i32, d.d_rel as i32, d.rel_extent as i32, b)?;
vetta_infer_hip::kv_store_f16_batched(&k_cv, &v_cv, &st.k_cache[pass], &st.v_cache[pass], pos, d.n_kv as i32, d.hd as i32, st.max_seq as i32, d.eps, b)?;
let aout = DeviceBuf::alloc(bu * q_dim * 4)?;
+1 -1
View File
@@ -202,7 +202,7 @@ pub fn expert_arena_gemv_f16(a: &ExpertArena, ids: &DeviceBuf, slot: i32, x: &De
/// expert eid processes its bucket tokens [exp_off[eid],exp_off[eid+1]) indexed
/// by `bucket_tok`. X [B,K] f16 (gathered) or [total,K] f16 (bucket order, pass an
/// identity index). Y [total_pairs,N] f32 in bucket order. Dispatches NVFP4/MXFP8.
pub fn expert_arena_grouped_gemv_f16(a: &ExpertArena, exp_off: &DeviceBuf, bucket_tok: &DeviceBuf, x: &DeviceBuf, y: &DeviceBuf) -> Result<(), String> {
pub fn expert_arena_grouped_gemv_f16(a: &ExpertArena, exp_off: &DeviceBuf, bucket_tok: Option<&DeviceBuf>, x: &DeviceBuf, y: &DeviceBuf) -> Result<(), String> {
if a.mxfp8 {
vetta_infer_hip::mxfp8_grouped_gemv_f16(&a.packed, &a.scales, exp_off, bucket_tok, x, y, a.n, a.k, a.n_exp as i32, a.w_stride, a.sc_stride)
} else {