[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

+30 -12
View File
@@ -95,6 +95,14 @@ extern "C" int vetta_vram_used(size_t* out) {
if (out) *out = total_b - free_b;
return 0;
}
// Async device zero-fill (hipMemsetAsync on the stream): ordered before subsequent
// kernels WITHOUT a blocking host round-trip (replaces sync h2d of a host zero vec).
extern "C" int vetta_buf_zero_async(void* dst, size_t bytes, vetta_stream_t stream) {
if (!dst || bytes == 0) return 0;
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
HIP_CHECK(hipMemsetAsync(dst, 0, bytes, s));
return 0;
}
// ---- k_rms_norm (kernel #2): y = x * rsqrt(mean(x^2)+eps) * w --------------
// One block per row; block-wide reduction over D. f32 in/out.
@@ -1333,7 +1341,7 @@ __global__ void __launch_bounds__(256) k_mxfp8_grouped_gemv_f16(
#pragma unroll
for (int m = 0; m < MT; ++m) {
if (t0 + m < m_e) {
const int tok = bucket_tok[beg + t0 + m];
const int tok = bucket_tok ? bucket_tok[beg + t0 + m] : (beg + t0 + m);
const uint16_t* xr = X + (size_t)tok * K + k0;
float d = 0.f;
#pragma unroll
@@ -1681,7 +1689,8 @@ extern "C" int vetta_l2_clip(float* x, const float* scale, int rows, int hd,
// ---- k_relpos_bias: rel_bias[h,dist] = sum_rc R[h,rc]*rel_proj[rc,dist] ----
// R already L2-clipped [n_head,d_rel]; rel_proj [d_rel,rel_extent]; output
// rel_bias [n_head,rel_extent] for dist < lim = min(pos+1, rel_extent).
// rel_bias [n_head,rel_extent] for dist < lim = min(pos+1, rel_extent); entries
// dist >= lim are written 0.0f in-kernel (no caller host zero-fill / sync h2d).
__global__ void k_relpos_bias(const float* __restrict__ r, const float* __restrict__ rel_proj,
float* __restrict__ rel_bias, int n_head, int d_rel,
@@ -1692,10 +1701,14 @@ __global__ void k_relpos_bias(const float* __restrict__ r, const float* __restri
const int nt = blockDim.x;
const float* rh = r + (size_t)h * d_rel;
float* dst = rel_bias + (size_t)h * rel_extent;
for (int dist = tid; dist < lim; dist += nt) {
float acc = 0.f;
for (int rc = 0; rc < d_rel; ++rc) acc += rh[rc] * rel_proj[(size_t)rc * rel_extent + dist];
dst[dist] = acc;
for (int dist = tid; dist < rel_extent; dist += nt) {
if (dist < lim) {
float acc = 0.f;
for (int rc = 0; rc < d_rel; ++rc) acc += rh[rc] * rel_proj[(size_t)rc * rel_extent + dist];
dst[dist] = acc;
} else {
dst[dist] = 0.f;
}
}
}
@@ -1711,7 +1724,8 @@ extern "C" int vetta_relpos_bias(const float* r, const float* rel_proj, float* r
// ---- k_relpos_bias_batched: relpos bias for M tokens (lim per token) --------
// r [M,n_head,d_rel], rel_bias [M,n_head,rel_extent]; grid (n_head, M). Each token
// m writes dist < lim[m] = min(pos[m]+1, rel_extent). Caller zero-fills rel_bias.
// m writes dist < lim[m] = min(pos[m]+1, rel_extent) and 0.0f for dist >= lim[m]
// (full rel_extent written in-kernel; no caller host zero-fill / sync h2d).
__global__ void k_relpos_bias_batched(const float* __restrict__ r, const float* __restrict__ rel_proj,
float* __restrict__ rel_bias, const int32_t* __restrict__ pos,
int n_head, int d_rel, int rel_extent, int M) {
@@ -1723,10 +1737,14 @@ __global__ void k_relpos_bias_batched(const float* __restrict__ r, const float*
const float* rh = r + ((size_t)m * n_head + h) * d_rel;
float* dst = rel_bias + ((size_t)m * n_head + h) * rel_extent;
const int lim = (pos[m] + 1 < rel_extent) ? (pos[m] + 1) : rel_extent;
for (int dist = tid; dist < lim; dist += nt) {
float acc = 0.f;
for (int rc = 0; rc < d_rel; ++rc) acc += rh[rc] * rel_proj[(size_t)rc * rel_extent + dist];
dst[dist] = acc;
for (int dist = tid; dist < rel_extent; dist += nt) {
if (dist < lim) {
float acc = 0.f;
for (int rc = 0; rc < d_rel; ++rc) acc += rh[rc] * rel_proj[(size_t)rc * rel_extent + dist];
dst[dist] = acc;
} else {
dst[dist] = 0.f;
}
}
}
@@ -2104,7 +2122,7 @@ __global__ void __launch_bounds__(256) k_nvfp4_grouped_gemv_f16(
#pragma unroll
for (int m = 0; m < MT; ++m) {
if (t0 + m < m_e) {
const int tok = bucket_tok[beg + t0 + m];
const int tok = bucket_tok ? bucket_tok[beg + t0 + m] : (beg + t0 + m);
const size_t xo = (size_t)tok * K + k0;
const uint4 x0 = *reinterpret_cast<const uint4*>(xh + xo);
const uint4 x1 = *reinterpret_cast<const uint4*>(xh + xo + 8);