[HIP] Batched router_gemv (f32) + relpos_bias + ctrl_pack (M=B): last attention/MoE prep kernels
k_router_gemv_batched: f32 router logits x[M,K]·w[N,K]bf16 -> y[M,N], grid(N,M) (router reads F32 mlpin, exact strata launch_f32_linear). k_relpos_bias_batched: r[M,n_head,d_rel] -> rel_bias[M,n_head,rel_extent], grid (n_head,M), lim[m]=min(pos[m]+1,rel_extent) per token. k_ctrl_pack_batched: cur/msg[M,D] -> packed[M,2D]bf16, grid M (controller input). Parity vs M sequential: router 0.0, relpos 0.0, ctrl_pack BYTE-IDENTICAL (M=20). Additive — decode default unchanged. With these, ALL per-layer kernels needed by a batched forward now support M=B (gemm_bf16/rms_norm/attn_res_mix/router_topk already did). Remaining: device-resident depth-block arena + batched layer orchestration (SWA+full) + forward_batch + serve wiring + EXACT parity. Verified: hip GPU 31 green, workspace CPU -D warnings green, 6 Lean 0 sorryAx.
This commit is contained in:
1 parent
708a40d91b
commit
388e9108e5
2 files changed
+219
No files matched your search
@@ -695,6 +695,38 @@ extern "C" int vetta_router_gemv(const float* x, const uint16_t* w, float* y,
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ---- k_router_gemv_batched: f32 router logits for M tokens ------------------
|
||||
// x [M,K] f32 (router reads F32 mlpin, exact strata launch_f32_linear), w [N,K]
|
||||
// bf16, y [M,N]. grid (N, M); block (n,m) reduces x[m,:]·w[n,:].
|
||||
__global__ void k_router_gemv_batched(const float* __restrict__ x,
|
||||
const uint16_t* __restrict__ w,
|
||||
float* __restrict__ y, int N, int K, int M) {
|
||||
const int n = blockIdx.x;
|
||||
const int m = blockIdx.y;
|
||||
if (n >= N || m >= M) return;
|
||||
const int tid = threadIdx.x;
|
||||
const int nt = blockDim.x;
|
||||
const uint16_t* wr = w + (size_t)n * K;
|
||||
const float* xr = x + (size_t)m * K;
|
||||
float acc = 0.f;
|
||||
for (int kk = tid; kk < K; kk += nt) acc += xr[kk] * bf16_to_f32(wr[kk]);
|
||||
__shared__ float red[256];
|
||||
red[tid] = acc;
|
||||
__syncthreads();
|
||||
for (int s = nt / 2; s > 0; s >>= 1) { if (tid < s) red[tid] += red[tid + s]; __syncthreads(); }
|
||||
if (tid == 0) y[(size_t)m * N + n] = red[0];
|
||||
}
|
||||
|
||||
extern "C" int vetta_router_gemv_batched(const float* x, const uint16_t* w, float* y,
|
||||
int N, int K, int M, vetta_stream_t stream) {
|
||||
if (N <= 0 || K <= 0 || M <= 0) return 1;
|
||||
dim3 grid(N, M);
|
||||
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
|
||||
k_router_gemv_batched<<<grid, 256, 0, s>>>(x, w, y, N, K, M);
|
||||
HIP_CHECK(hipGetLastError());
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ---- k_cast_f32_bf16: f32 activation -> BF16 (truncate) before GEMM --------
|
||||
// The residual stream is f32; trunk/expert GEMMs read BF16 activations (the
|
||||
// reference inserts k_f32_to_bf16/f16 before the matmul). Truncation matches
|
||||
@@ -1622,6 +1654,38 @@ extern "C" int vetta_relpos_bias(const float* r, const float* rel_proj, float* r
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ---- 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.
|
||||
__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) {
|
||||
const int h = blockIdx.x;
|
||||
const int m = blockIdx.y;
|
||||
if (h >= n_head || m >= M) return;
|
||||
const int tid = threadIdx.x;
|
||||
const int nt = blockDim.x;
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" int vetta_relpos_bias_batched(const float* r, const float* rel_proj, float* rel_bias,
|
||||
const int32_t* pos, int n_head, int d_rel, int rel_extent, int M,
|
||||
vetta_stream_t stream) {
|
||||
if (n_head <= 0 || d_rel <= 0 || rel_extent <= 0 || M <= 0) return 1;
|
||||
dim3 grid(n_head, M);
|
||||
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
|
||||
k_relpos_bias_batched<<<grid, 256, 0, s>>>(r, rel_proj, rel_bias, pos, n_head, d_rel, rel_extent, M);
|
||||
HIP_CHECK(hipGetLastError());
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ---- k_kv_store: K (pre-clipped) stored as-is; V RMS-normalized; contiguous
|
||||
// f32 cache [n_kv, seq, hd] at position pos. Exact strata K-clip/V-rmsnorm.
|
||||
|
||||
@@ -1793,6 +1857,48 @@ extern "C" int vetta_ctrl_pack(const float* cur, const float* msg, uint16_t* pac
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ---- k_ctrl_pack_batched: controller input pack for M tokens ----------------
|
||||
// cur [M,D], msg [M,D], packed [M,2D] bf16; grid M, one block per token.
|
||||
__global__ void k_ctrl_pack_batched(const float* __restrict__ cur, const float* __restrict__ msg,
|
||||
uint16_t* __restrict__ packed, int D, float eps, int M) {
|
||||
const int m = blockIdx.y;
|
||||
if (m >= M) return;
|
||||
const int tid = threadIdx.x;
|
||||
const int nt = blockDim.x;
|
||||
const float* cv = cur + (size_t)m * D;
|
||||
const float* mv = msg + (size_t)m * D;
|
||||
uint16_t* pk = packed + (size_t)m * 2 * D;
|
||||
__shared__ float red[256];
|
||||
__shared__ float inv_c_s, inv_m_s;
|
||||
float ssc = 0.f, ssm = 0.f;
|
||||
for (int d = tid; d < D; d += nt) { ssc += cv[d] * cv[d]; ssm += mv[d] * mv[d]; }
|
||||
red[tid] = ssc; __syncthreads();
|
||||
for (int t = nt / 2; t > 0; t >>= 1) { if (tid < t) red[tid] += red[tid + t]; __syncthreads(); }
|
||||
if (tid == 0) inv_c_s = rsqrtf(red[0] / (float)D + eps);
|
||||
__syncthreads();
|
||||
red[tid] = ssm; __syncthreads();
|
||||
for (int t = nt / 2; t > 0; t >>= 1) { if (tid < t) red[tid] += red[tid + t]; __syncthreads(); }
|
||||
if (tid == 0) inv_m_s = rsqrtf(red[0] / (float)D + eps);
|
||||
__syncthreads();
|
||||
const float ic = inv_c_s, im = inv_m_s;
|
||||
for (int d = tid; d < D; d += nt) {
|
||||
const uint32_t uc = __float_as_uint(cv[d] * ic);
|
||||
const uint32_t um = __float_as_uint(mv[d] * im);
|
||||
pk[d] = (uint16_t)((uc + 0x7FFFu + ((uc >> 16) & 1u)) >> 16);
|
||||
pk[D + d] = (uint16_t)((um + 0x7FFFu + ((um >> 16) & 1u)) >> 16);
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" int vetta_ctrl_pack_batched(const float* cur, const float* msg, uint16_t* packed,
|
||||
int D, float eps, int M, vetta_stream_t stream) {
|
||||
if (D <= 0 || M <= 0) return 1;
|
||||
dim3 grid(1, M);
|
||||
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
|
||||
k_ctrl_pack_batched<<<grid, 256, 0, s>>>(cur, msg, packed, D, eps, M);
|
||||
HIP_CHECK(hipGetLastError());
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ---- k_ctrl_apply: out = (1 - tanh(forget))*cur + tanh(gain)*msg -----------
|
||||
// Exact strata k1_loop_ctrl_apply_out (gain/forget gating of the loop message).
|
||||
|
||||
|
||||
Reference in new issue
Block a user