[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:
adm committed 2026-12-02 00:20:55 +01:00
1 parent 3a2ad4da49
commit 51b230cdd2
3 files changed
+88 -10

No files matched your search

+26 -5
View File
@@ -681,6 +681,26 @@ extern "C" int vetta_copy_f32(const float* src, float* dst, size_t n, vetta_stre
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 --------
// 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
@@ -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.
__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;
if (r >= rows) return;
const int tid = threadIdx.x;
const int nt = blockDim.x;
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 fac_s;
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,
float clip, int apply_tau, int pos, vetta_stream_t stream) {
if (rows <= 0 || hd <= 0 || hd > 256) return 1;
float clip, int apply_tau, int pos, int scale_rows, vetta_stream_t stream) {
if (rows <= 0 || hd <= 0 || hd > 256 || scale_rows <= 0) return 1;
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());
return 0;
}