[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
@@ -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)?;
|
||||
|
||||
Reference in new issue
Block a user