[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:
1 parent
ac31924ebb
commit
f25cd27cf0
4 files changed
+59
-35
No files matched your search
@@ -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();
|
||||
|
||||
Reference in new issue
Block a user