[HIP] attention + MoE router + vec_add kernels (GPU-verified, parity vs reference semantics)
- k_flash_gqa_decode: single-token GQA attention over a f32 KV cache, two-pass softmax, correctness-first base. Parity vs CPU oracle ~3e-6 on the real Inkling shape (24 heads / 8 KV, hd 128). SWA window / relpos bias / sinks / split-KV / WMMA are the later parity+performance refinements. - k_moe_router_topk: Inkling EXACT sigmoid router, semantics verified against strata k4_moe_router_topk — p=sigmoid(logit), score=p+bias, top-K by score (smallest eid wins ties), weight=p[chosen], renormalized to route_scale. Parity: top-k ids exact, weights ~1e-5 (128 experts, top-8, route_scale 8). - k_vec_add: y=a+b elementwise (residual stream), exact. - C ABI + safe Rust wrappers + DeviceBuf byte/i32 transfers + CPU-oracle tests. Six compute kernels now verified on gfx1150 (embed, rms_norm, gemm_bf16, flash_gqa, moe_router, vec_add); the base decode graph is covered except the NVFP4 expert GEMMs (w13/w2) + reduce. Approach: standard correctness-first math, Inkling-specific features ported from the measured reference (no invented attention variants); performance pass (WMMA/split-KV) comes after. Verified: RUSTFLAGS="-D warnings" cargo test --workspace = 97/97 PASS; lean proofs (4 files, 42 theorems) green; GPU parity 7/7 (--features hip).
This commit is contained in:
1 parent
356f9c458e
commit
b99d3a7a6a
3 files changed
+327
No files matched your search
@@ -45,6 +45,21 @@ int vetta_embed_bf16_gather(const uint16_t* table, const int32_t* tokens,
|
||||
int vetta_gemm_bf16(const uint16_t* x, const uint16_t* w, float* y,
|
||||
int M, int N, int K, vetta_stream_t stream);
|
||||
|
||||
// k_flash_gqa_decode (base): single-token GQA attention over a f32 KV cache.
|
||||
int vetta_flash_gqa_decode(const float* q, const float* k, const float* v,
|
||||
float* out, int n_head, int n_kv, int seq,
|
||||
int hd, float scale, vetta_stream_t stream);
|
||||
|
||||
// k_moe_router_topk: Inkling exact sigmoid router (top-K, route_scale norm).
|
||||
int vetta_moe_router_topk(const float* logits, const float* bias,
|
||||
int32_t* topk_ids, float* topk_weights,
|
||||
int M, int n_experts, int K_top,
|
||||
float route_scale, vetta_stream_t stream);
|
||||
|
||||
// k_vec_add: y = a + b elementwise (residual stream).
|
||||
int vetta_vec_add(const float* a, const float* b, float* y, size_t n,
|
||||
vetta_stream_t stream);
|
||||
|
||||
// Core <15 microkernel entry points (implemented progressively in M1 & M2):
|
||||
// 1. k_embed_bf16
|
||||
// 2. k_rms_norm
|
||||
|
||||
@@ -164,4 +164,161 @@ extern "C" int vetta_gemm_bf16(const uint16_t* x, const uint16_t* w, float* y,
|
||||
k_gemm_bf16<<<grid, block, 0, s>>>(x, w, y, M, N, K);
|
||||
HIP_CHECK(hipGetLastError());
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ---- k_flash_gqa_decode (kernel #8 base): single-token GQA attention -------
|
||||
// q f32 [n_head, hd]; k/v f32 cache [n_kv, seq, hd]; out f32 [n_head, hd].
|
||||
// Head h attends to KV head h/(n_head/n_kv) over all `seq` cached positions
|
||||
// (decode is causal over the full cache). Two-pass softmax, f32 throughout.
|
||||
// Correctness-first; SWA window / relpos bias / split-KV / WMMA are the
|
||||
// performance + parity refinements layered on later.
|
||||
|
||||
__global__ void k_flash_gqa_decode(const float* __restrict__ q,
|
||||
const float* __restrict__ k,
|
||||
const float* __restrict__ v,
|
||||
float* __restrict__ out,
|
||||
int n_head, int n_kv, int seq, int hd,
|
||||
float scale) {
|
||||
const int h = blockIdx.x;
|
||||
const int kvh = h / (n_head / n_kv);
|
||||
const int tid = threadIdx.x;
|
||||
const int nt = blockDim.x;
|
||||
extern __shared__ float smem[];
|
||||
float* scores = smem; // [seq]
|
||||
float* red = smem + seq; // [nt] reduction scratch
|
||||
const float* qh = q + (size_t)h * hd;
|
||||
const float* kh = k + (size_t)kvh * seq * hd;
|
||||
const float* vh = v + (size_t)kvh * seq * hd;
|
||||
|
||||
float lmax = -INFINITY;
|
||||
for (int s = tid; s < seq; s += nt) {
|
||||
const float* ks = kh + (size_t)s * hd;
|
||||
float dot = 0.f;
|
||||
for (int d = 0; d < hd; ++d) dot += qh[d] * ks[d];
|
||||
dot *= scale;
|
||||
scores[s] = dot;
|
||||
lmax = fmaxf(lmax, dot);
|
||||
}
|
||||
red[tid] = lmax;
|
||||
__syncthreads();
|
||||
for (int s = nt / 2; s > 0; s >>= 1) {
|
||||
if (tid < s) red[tid] = fmaxf(red[tid], red[tid + s]);
|
||||
__syncthreads();
|
||||
}
|
||||
const float m = red[0];
|
||||
__syncthreads();
|
||||
|
||||
float lsum = 0.f;
|
||||
for (int s = tid; s < seq; s += nt) {
|
||||
float e = __expf(scores[s] - m);
|
||||
scores[s] = e;
|
||||
lsum += e;
|
||||
}
|
||||
red[tid] = lsum;
|
||||
__syncthreads();
|
||||
for (int s = nt / 2; s > 0; s >>= 1) {
|
||||
if (tid < s) red[tid] += red[tid + s];
|
||||
__syncthreads();
|
||||
}
|
||||
const float inv = 1.f / red[0];
|
||||
__syncthreads();
|
||||
|
||||
float* oh = out + (size_t)h * hd;
|
||||
for (int d = tid; d < hd; d += nt) {
|
||||
float acc = 0.f;
|
||||
for (int s = 0; s < seq; ++s) acc += scores[s] * inv * vh[(size_t)s * hd + d];
|
||||
oh[d] = acc;
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" int vetta_flash_gqa_decode(const float* q, const float* k, const float* v,
|
||||
float* out, int n_head, int n_kv, int seq,
|
||||
int hd, float scale, vetta_stream_t stream) {
|
||||
if (n_head <= 0 || n_kv <= 0 || seq <= 0 || hd <= 0 || (n_head % n_kv) != 0) return 1;
|
||||
const int nt = 256;
|
||||
const size_t shmem = ((size_t)seq + nt) * sizeof(float);
|
||||
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
|
||||
k_flash_gqa_decode<<<n_head, nt, shmem, s>>>(q, k, v, out, n_head, n_kv, seq, hd, scale);
|
||||
HIP_CHECK(hipGetLastError());
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ---- k_moe_router_topk (kernel #11): Inkling exact sigmoid router ----------
|
||||
// Semantics verified against strata k4_moe_router_topk:
|
||||
// p[e] = sigmoid(logits[m,e]) (NaN->0); score[e] = p[e] + bias[e] (NaN->-1e29);
|
||||
// top-K by score (strictly greater, smallest eid wins ties);
|
||||
// weight[k] = p[chosen] (the sigmoid prob, NOT the score);
|
||||
// weights *= route_scale / (sum(weights) + 1e-20).
|
||||
|
||||
__global__ void k_moe_router_topk(const float* __restrict__ logits,
|
||||
const float* __restrict__ bias,
|
||||
int32_t* __restrict__ topk_ids,
|
||||
float* __restrict__ topk_weights,
|
||||
int M, int n_experts, int K_top,
|
||||
float route_scale) {
|
||||
const int m = blockIdx.x;
|
||||
if (m >= M) return;
|
||||
extern __shared__ float smem[];
|
||||
float* prob = smem; // [n_experts]
|
||||
float* score = smem + n_experts; // [n_experts]
|
||||
const int tid = threadIdx.x;
|
||||
const int nt = blockDim.x;
|
||||
for (int e = tid; e < n_experts; e += nt) {
|
||||
float lg = logits[(size_t)m * n_experts + e];
|
||||
float p = 1.f / (1.f + __expf(-lg));
|
||||
if (!(p == p)) p = 0.f;
|
||||
prob[e] = p;
|
||||
float sc = p + (bias ? bias[e] : 0.f);
|
||||
if (!(sc == sc)) sc = -1e29f;
|
||||
score[e] = sc;
|
||||
}
|
||||
__syncthreads();
|
||||
if (tid == 0) {
|
||||
float wsum = 0.f;
|
||||
for (int k = 0; k < K_top; ++k) {
|
||||
float bv = -INFINITY;
|
||||
int bi = -1;
|
||||
for (int e = 0; e < n_experts; ++e) {
|
||||
if (score[e] > bv) { bv = score[e]; bi = e; }
|
||||
}
|
||||
topk_ids[(size_t)m * K_top + k] = bi;
|
||||
float w = prob[bi];
|
||||
topk_weights[(size_t)m * K_top + k] = w;
|
||||
wsum += w;
|
||||
score[bi] = -1e30f;
|
||||
}
|
||||
float inv = route_scale / (wsum + 1e-20f);
|
||||
for (int k = 0; k < K_top; ++k) topk_weights[(size_t)m * K_top + k] *= inv;
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" int vetta_moe_router_topk(const float* logits, const float* bias,
|
||||
int32_t* topk_ids, float* topk_weights,
|
||||
int M, int n_experts, int K_top,
|
||||
float route_scale, vetta_stream_t stream) {
|
||||
if (M <= 0 || n_experts <= 0 || K_top <= 0 || K_top > n_experts) return 1;
|
||||
const size_t shmem = (size_t)2 * n_experts * sizeof(float);
|
||||
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
|
||||
k_moe_router_topk<<<M, 256, shmem, s>>>(logits, bias, topk_ids, topk_weights, M, n_experts, K_top, route_scale);
|
||||
HIP_CHECK(hipGetLastError());
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ---- k_vec_add: y = a + b elementwise (residual stream) --------------------
|
||||
|
||||
__global__ void k_vec_add(const float* __restrict__ a, const float* __restrict__ b,
|
||||
float* __restrict__ y, size_t n) {
|
||||
size_t i = (size_t)blockIdx.x * blockDim.x + threadIdx.x;
|
||||
if (i < n) y[i] = a[i] + b[i];
|
||||
}
|
||||
|
||||
extern "C" int vetta_vec_add(const float* a, const float* b, float* y, size_t n,
|
||||
vetta_stream_t stream) {
|
||||
if (n == 0) return 0;
|
||||
const int nt = 256;
|
||||
const size_t grid = (n + nt - 1) / nt;
|
||||
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
|
||||
k_vec_add<<<grid, nt, 0, s>>>(a, b, y, n);
|
||||
HIP_CHECK(hipGetLastError());
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user