- k_flash_swa_gqa_decode / vetta_flash_swa_gqa_decode: exact strata
k3_flash_swa_gqa semantics on a contiguous f32 KV cache:
* SWA window: k_start = (window>0 && cur_pos>=window) ? cur_pos-window+1 : 0;
* relpos bias rel_bias[h,dist] added to the score (dist=cur_pos-s, <max_rel);
* attention sink_logit[h]: virtual sink token (value 0) contributing
exp(sink-m) to the softmax denominator only;
* sigmoid gate_attn[h,d] applied per output dim.
Two-pass softmax over the windowed range, f32 throughout. rel_bias /
sink_logit / gate are optional (NULL disables each).
- Parity test vs a CPU oracle across 4 configs (window on/off x relpos x sink
x gate): max_rel_err 9.4e-7 .. 3.6e-6. The Inkling-specific attention math
(local window, relative position, sink, gate) is verified.
With ShortConv4, this covers the defining Inkling attention mechanisms; the
AttnRes block-mix deep residual is the remaining parity piece.
Verified: RUSTFLAGS="-D warnings" cargo test --workspace = 97/97 PASS;
lean proofs (4 files, 42 theorems) green; GPU 12/12 (--features hip).
614 lines
25 KiB
Plaintext
614 lines
25 KiB
Plaintext
// kernels/hip/vetta_kernels.hip
|
|
// First RDNA 3.5 (gfx1150) HIP microkernels + minimal runtime helpers.
|
|
// Built by vetta-infer-hip/build.rs via hipcc --offload-arch=gfx1150.
|
|
// Parity discipline: every kernel is checked against a CPU oracle (opt-in
|
|
// GPU test) before it is wired into the executor.
|
|
|
|
#include <hip/hip_runtime.h>
|
|
#include <cstdio>
|
|
#include <cstring>
|
|
#include "vetta_kernels.h"
|
|
|
|
#define HIP_CHECK(call) \
|
|
do { \
|
|
hipError_t _e = (call); \
|
|
if (_e != hipSuccess) { \
|
|
std::fprintf(stderr, "[vetta-hip] %s:%d %s -> %s\n", __FILE__, \
|
|
__LINE__, #call, hipGetErrorString(_e)); \
|
|
return 1; \
|
|
} \
|
|
} while (0)
|
|
|
|
// ---- device query ----------------------------------------------------------
|
|
|
|
extern "C" int vetta_hip_query_device(int dev_id, vetta_device_info_t* out) {
|
|
if (!out) return 1;
|
|
int n = 0;
|
|
HIP_CHECK(hipGetDeviceCount(&n));
|
|
if (dev_id < 0 || dev_id >= n) return 1;
|
|
hipDeviceProp_t p;
|
|
HIP_CHECK(hipGetDeviceProperties(&p, dev_id));
|
|
std::memset(out, 0, sizeof(*out));
|
|
std::snprintf(out->name, sizeof(out->name), "%s", p.name);
|
|
std::snprintf(out->gcn_arch, sizeof(out->gcn_arch), "%s", p.gcnArchName);
|
|
out->multi_processor_count = p.multiProcessorCount;
|
|
out->wave_size = p.warpSize;
|
|
out->total_vram_bytes = (uint64_t)p.totalGlobalMem;
|
|
int uma = 0;
|
|
hipDeviceGetAttribute(&uma, hipDeviceAttributeIntegrated, dev_id);
|
|
out->is_uma = uma;
|
|
return 0;
|
|
}
|
|
|
|
// ---- minimal device buffer lifecycle (UMA-aware) ---------------------------
|
|
|
|
extern "C" int vetta_buf_alloc(void** out, size_t bytes) {
|
|
if (!out) return 1;
|
|
HIP_CHECK(hipMalloc(out, bytes));
|
|
return 0;
|
|
}
|
|
extern "C" int vetta_buf_free(void* p) {
|
|
if (!p) return 0;
|
|
HIP_CHECK(hipFree(p));
|
|
return 0;
|
|
}
|
|
extern "C" int vetta_buf_h2d(void* dst, const void* src, size_t bytes) {
|
|
HIP_CHECK(hipMemcpy(dst, src, bytes, hipMemcpyHostToDevice));
|
|
return 0;
|
|
}
|
|
extern "C" int vetta_buf_d2h(void* dst, const void* src, size_t bytes) {
|
|
HIP_CHECK(hipMemcpy(dst, src, bytes, hipMemcpyDeviceToHost));
|
|
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.
|
|
|
|
__global__ void k_rms_norm_f32(const float* __restrict__ x,
|
|
const float* __restrict__ w,
|
|
float* __restrict__ y, int D, float eps) {
|
|
const int row = blockIdx.x;
|
|
const float* xr = x + (size_t)row * D;
|
|
float* yr = y + (size_t)row * D;
|
|
const int tid = threadIdx.x;
|
|
const int nthreads = blockDim.x;
|
|
|
|
float ss = 0.f;
|
|
for (int i = tid; i < D; i += nthreads) {
|
|
float v = xr[i];
|
|
ss += v * v;
|
|
}
|
|
// block reduction via shared memory
|
|
__shared__ float red[256];
|
|
red[tid] = ss;
|
|
__syncthreads();
|
|
for (int s = nthreads / 2; s > 0; s >>= 1) {
|
|
if (tid < s) red[tid] += red[tid + s];
|
|
__syncthreads();
|
|
}
|
|
const float inv = rsqrtf(red[0] / (float)D + eps);
|
|
for (int i = tid; i < D; i += nthreads) {
|
|
yr[i] = xr[i] * inv * (w ? w[i] : 1.f);
|
|
}
|
|
}
|
|
|
|
extern "C" int vetta_rms_norm_f32(const float* x, const float* w, float* y,
|
|
int rows, int D, float eps,
|
|
vetta_stream_t stream) {
|
|
if (rows <= 0 || D <= 0) return 1;
|
|
int threads = D < 256 ? ((D + 31) / 32) * 32 : 256;
|
|
if (threads < 32) threads = 32;
|
|
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
|
|
k_rms_norm_f32<<<rows, threads, 0, s>>>(x, w, y, D, eps);
|
|
HIP_CHECK(hipGetLastError());
|
|
return 0;
|
|
}
|
|
|
|
// ---- BF16 helpers ----------------------------------------------------------
|
|
|
|
__device__ __forceinline__ float bf16_to_f32(uint16_t h) {
|
|
return __uint_as_float(((uint32_t)h) << 16);
|
|
}
|
|
|
|
// ---- k_embed_bf16 (kernel #1): gather token rows from a BF16 table ---------
|
|
// out[b, :] = bf16_to_f32(table[token[b], :]). Exact (no arithmetic).
|
|
|
|
__global__ void k_embed_bf16_gather(const uint16_t* __restrict__ table,
|
|
const int32_t* __restrict__ tokens,
|
|
float* __restrict__ out, int D, int vocab) {
|
|
const int b = blockIdx.x;
|
|
const int tok = tokens[b];
|
|
const int tid = threadIdx.x;
|
|
const int nthreads = blockDim.x;
|
|
if (tok < 0 || tok >= vocab) return;
|
|
const uint16_t* row = table + (size_t)tok * D;
|
|
float* orow = out + (size_t)b * D;
|
|
for (int i = tid; i < D; i += nthreads) orow[i] = bf16_to_f32(row[i]);
|
|
}
|
|
|
|
extern "C" int vetta_embed_bf16_gather(const uint16_t* table, const int32_t* tokens,
|
|
float* out, int B, int D, int vocab,
|
|
vetta_stream_t stream) {
|
|
if (B <= 0 || D <= 0) return 1;
|
|
int threads = D < 256 ? ((D + 31) / 32) * 32 : 256;
|
|
if (threads < 32) threads = 32;
|
|
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
|
|
k_embed_bf16_gather<<<B, threads, 0, s>>>(table, tokens, out, D, vocab);
|
|
HIP_CHECK(hipGetLastError());
|
|
return 0;
|
|
}
|
|
|
|
// ---- k_gemm_bf16: y[M,N] = x[M,K] @ w[N,K]^T, f32 accumulate ---------------
|
|
// x BF16 row-major [M,K], w BF16 row-major [N,K], y f32 [M,N]. One thread per
|
|
// output element (correctness-first; WMMA tiling is the performance pass).
|
|
|
|
__global__ void k_gemm_bf16(const uint16_t* __restrict__ x,
|
|
const uint16_t* __restrict__ w,
|
|
float* __restrict__ y, int M, int N, int K) {
|
|
const int n = blockIdx.x * blockDim.x + threadIdx.x;
|
|
const int m = blockIdx.y * blockDim.y + threadIdx.y;
|
|
if (m >= M || n >= N) return;
|
|
const uint16_t* xr = x + (size_t)m * K;
|
|
const uint16_t* wr = w + (size_t)n * K;
|
|
float acc = 0.f;
|
|
for (int kk = 0; kk < K; ++kk) acc += bf16_to_f32(xr[kk]) * bf16_to_f32(wr[kk]);
|
|
y[(size_t)m * N + n] = acc;
|
|
}
|
|
|
|
extern "C" int vetta_gemm_bf16(const uint16_t* x, const uint16_t* w, float* y,
|
|
int M, int N, int K, vetta_stream_t stream) {
|
|
if (M <= 0 || N <= 0 || K <= 0) return 1;
|
|
dim3 block(16, 16);
|
|
dim3 grid((N + 15) / 16, (M + 15) / 16);
|
|
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
|
|
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_flash_swa_gqa_decode: Inkling flash SWA GQA (parity) ----------------
|
|
// Exact strata k3_flash_swa_gqa semantics on a contiguous f32 KV cache:
|
|
// SWA window: k_start = (window>0 && cur_pos>=window) ? cur_pos-window+1 : 0;
|
|
// rel_bias[h, dist] added to score (dist = cur_pos - s, if dist < max_rel);
|
|
// sink_logit[h]: virtual sink token (logit=sink, value=0) -> adds exp(sink-m)
|
|
// to the softmax denominator only;
|
|
// gate_attn[h, d]: output *= sigmoid(gate) per dim.
|
|
// Two-pass softmax over the windowed range, f32 throughout.
|
|
|
|
__global__ void k_flash_swa_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, int swa_window, int cur_pos,
|
|
const float* __restrict__ rel_bias, int max_rel,
|
|
const float* __restrict__ sink_logit,
|
|
const float* __restrict__ gate) {
|
|
const int h = blockIdx.x;
|
|
const int kvh = h / (n_head / n_kv);
|
|
const int tid = threadIdx.x;
|
|
const int nt = blockDim.x;
|
|
const int k_start = (swa_window > 0 && cur_pos >= swa_window) ? (cur_pos - swa_window + 1) : 0;
|
|
const int n_win = cur_pos - k_start + 1;
|
|
extern __shared__ float smem[];
|
|
float* scores = smem; // [n_win]
|
|
float* red = smem + n_win; // [nt]
|
|
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;
|
|
const float* rb = rel_bias ? (rel_bias + (size_t)h * max_rel) : nullptr;
|
|
const float sink = sink_logit ? sink_logit[h] : -INFINITY;
|
|
|
|
float lmax = sink;
|
|
for (int p = tid; p < n_win; p += nt) {
|
|
const int s = k_start + p;
|
|
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;
|
|
const int dist = cur_pos - s;
|
|
if (rb && dist < max_rel) dot += rb[dist];
|
|
scores[p] = 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 p = tid; p < n_win; p += nt) {
|
|
float e = __expf(scores[p] - m);
|
|
scores[p] = 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 total = red[0] + (sink_logit ? __expf(sink - m) : 0.f);
|
|
const float inv = 1.f / fmaxf(total, 1e-30f);
|
|
__syncthreads();
|
|
|
|
float* oh = out + (size_t)h * hd;
|
|
for (int d = tid; d < hd; d += nt) {
|
|
float acc = 0.f;
|
|
for (int p = 0; p < n_win; ++p) {
|
|
const int s = k_start + p;
|
|
acc += scores[p] * inv * vh[(size_t)s * hd + d];
|
|
}
|
|
if (gate) acc *= 1.f / (1.f + __expf(-gate[(size_t)h * hd + d]));
|
|
oh[d] = acc;
|
|
}
|
|
}
|
|
|
|
extern "C" int vetta_flash_swa_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, int swa_window, int cur_pos,
|
|
const float* rel_bias, int max_rel,
|
|
const float* sink_logit, const float* gate,
|
|
vetta_stream_t stream) {
|
|
if (n_head <= 0 || n_kv <= 0 || seq <= 0 || hd <= 0 || (n_head % n_kv) != 0) return 1;
|
|
if (cur_pos < 0 || cur_pos >= seq) return 1;
|
|
const int k_start = (swa_window > 0 && cur_pos >= swa_window) ? (cur_pos - swa_window + 1) : 0;
|
|
const int n_win = cur_pos - k_start + 1;
|
|
const int nt = 256;
|
|
const size_t shmem = ((size_t)n_win + nt) * sizeof(float);
|
|
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
|
|
k_flash_swa_gqa_decode<<<n_head, nt, shmem, s>>>(q, k, v, out, n_head, n_kv, seq, hd,
|
|
scale, swa_window, cur_pos, rel_bias, max_rel,
|
|
sink_logit, gate);
|
|
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;
|
|
}
|
|
|
|
// ---- 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
|
|
// the bf16 weight storage; RNE rounding is a later precision refinement.
|
|
|
|
__global__ void k_cast_f32_bf16(const float* __restrict__ x, uint16_t* __restrict__ y,
|
|
size_t n) {
|
|
size_t i = (size_t)blockIdx.x * blockDim.x + threadIdx.x;
|
|
if (i < n) y[i] = (uint16_t)(__float_as_uint(x[i]) >> 16);
|
|
}
|
|
|
|
extern "C" int vetta_cast_f32_bf16(const float* x, uint16_t* 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_cast_f32_bf16<<<grid, nt, 0, s>>>(x, y, n);
|
|
HIP_CHECK(hipGetLastError());
|
|
return 0;
|
|
}
|
|
|
|
// ---- NVFP4 (SF4) codecs — exact semantics from strata ink_hip_common -------
|
|
// E2M1 nibble LUT; F8_E4M3FN group scale (16 weights/group); f32 outer_scale.
|
|
// Layout: packed_e2m1 [N, K/2] (low nib = even col, high nib = odd col),
|
|
// scales_e4m3 [N, K/16], outer_scale = amax/denom.
|
|
|
|
__device__ __forceinline__ float sf4_e2m1_f32(uint32_t nib) {
|
|
static constexpr float kLut[16] = {
|
|
0.0f, 0.5f, 1.0f, 1.5f, 2.0f, 3.0f, 4.0f, 6.0f,
|
|
-0.0f, -0.5f, -1.0f, -1.5f, -2.0f, -3.0f, -4.0f, -6.0f
|
|
};
|
|
return kLut[nib & 15u];
|
|
}
|
|
|
|
__device__ __forceinline__ float e4m3fn_to_f32(uint8_t b) {
|
|
const uint32_t sign = (uint32_t)(b & 0x80u) << 24;
|
|
const uint32_t exp = (b >> 3) & 0x0Fu;
|
|
const uint32_t mant = b & 0x07u;
|
|
if (exp == 0) {
|
|
if (mant == 0) return __builtin_bit_cast(float, sign);
|
|
const float val = (float)mant * (1.0f / 512.0f);
|
|
return (b & 0x80u) ? -val : val;
|
|
} else if (exp == 15 && mant == 7) {
|
|
return 0.0f; // NaN -> 0 safely
|
|
}
|
|
uint32_t f = sign | ((exp + (127u - 7u)) << 23) | (mant << 20);
|
|
return __builtin_bit_cast(float, f);
|
|
}
|
|
|
|
// ---- k_nvfp4_gemv (M=1 decode): y[N] = dequant(W)[N,K] @ x[K] --------------
|
|
// One block per output row; threads stride over K/16 groups, f32 accumulate.
|
|
// Correctness-first; the Wave32 bandwidth-saturated path is the performance pass.
|
|
|
|
__global__ void k_nvfp4_gemv(const uint8_t* __restrict__ packed,
|
|
const uint8_t* __restrict__ scales,
|
|
float outer_scale, const float* __restrict__ x,
|
|
float* __restrict__ y, int N, int K) {
|
|
const int n = blockIdx.x;
|
|
if (n >= N) return;
|
|
const int tid = threadIdx.x;
|
|
const int nt = blockDim.x;
|
|
const uint8_t* wp = packed + (size_t)n * (K >> 1);
|
|
const uint8_t* ws = scales + (size_t)n * (K >> 4);
|
|
const int nblk = K >> 4;
|
|
float acc = 0.f;
|
|
for (int b = tid; b < nblk; b += nt) {
|
|
const float sc = e4m3fn_to_f32(ws[b]) * outer_scale;
|
|
const int k0 = b << 4;
|
|
const uint8_t* w8 = wp + b * 8;
|
|
for (int j = 0; j < 8; ++j) {
|
|
uint8_t byte = w8[j];
|
|
float w0 = sf4_e2m1_f32(byte & 15u) * sc;
|
|
float w1 = sf4_e2m1_f32(byte >> 4) * sc;
|
|
acc += x[k0 + j * 2] * w0 + x[k0 + j * 2 + 1] * w1;
|
|
}
|
|
}
|
|
__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[n] = red[0];
|
|
}
|
|
|
|
extern "C" int vetta_nvfp4_gemv(const uint8_t* packed, const uint8_t* scales,
|
|
float outer_scale, const float* x, float* y,
|
|
int N, int K, vetta_stream_t stream) {
|
|
if (N <= 0 || K <= 0 || (K % 16) != 0) return 1;
|
|
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
|
|
k_nvfp4_gemv<<<N, 256, 0, s>>>(packed, scales, outer_scale, x, y, N, K);
|
|
HIP_CHECK(hipGetLastError());
|
|
return 0;
|
|
}
|
|
|
|
// ---- SiTU activation (exact strata situ_f32, double-softcapped) ------------
|
|
// gu = [gate(inter); up(inter)] from w13. act[j] = g_act * u_act where
|
|
// g_act = beta*tanh(g/beta)*sigmoid(g), u_act = lb*tanh(u/lb).
|
|
|
|
__device__ __forceinline__ float situ_f32(float g, float u, float beta, float lb) {
|
|
const float sig = 1.f / (1.f + __expf(-g));
|
|
const float g_act = beta * tanhf(g / beta) * sig;
|
|
const float u_act = lb * tanhf(u / lb);
|
|
return g_act * u_act;
|
|
}
|
|
|
|
__global__ void k_situ(const float* __restrict__ gu, float* __restrict__ act,
|
|
int inter, float beta, float lb) {
|
|
int j = blockIdx.x * blockDim.x + threadIdx.x;
|
|
if (j < inter) act[j] = situ_f32(gu[j], gu[inter + j], beta, lb);
|
|
}
|
|
|
|
extern "C" int vetta_situ(const float* gu, float* act, int inter, float beta,
|
|
float lb, vetta_stream_t stream) {
|
|
if (inter <= 0) return 1;
|
|
const int nt = 256;
|
|
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
|
|
k_situ<<<(inter + nt - 1) / nt, nt, 0, s>>>(gu, act, inter, beta, lb);
|
|
HIP_CHECK(hipGetLastError());
|
|
return 0;
|
|
}
|
|
|
|
// ---- k_moe_reduce: out[d] = sum_e tk_w[e] * y_part[e, d] -------------------
|
|
|
|
__global__ void k_moe_reduce(const float* __restrict__ y_part,
|
|
const float* __restrict__ tk_w, float* __restrict__ out,
|
|
int top_k, int hidden) {
|
|
int d = blockIdx.x * blockDim.x + threadIdx.x;
|
|
if (d < hidden) {
|
|
float acc = 0.f;
|
|
for (int e = 0; e < top_k; ++e) acc += tk_w[e] * y_part[(size_t)e * hidden + d];
|
|
out[d] = acc;
|
|
}
|
|
}
|
|
|
|
extern "C" int vetta_moe_reduce(const float* y_part, const float* tk_w, float* out,
|
|
int top_k, int hidden, vetta_stream_t stream) {
|
|
if (top_k <= 0 || hidden <= 0) return 1;
|
|
const int nt = 256;
|
|
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
|
|
k_moe_reduce<<<(hidden + nt - 1) / nt, nt, 0, s>>>(y_part, tk_w, out, top_k, hidden);
|
|
HIP_CHECK(hipGetLastError());
|
|
return 0;
|
|
}
|
|
|
|
// ---- k_shortconv4_decode: Inkling causal depthwise ShortConv (kernel 4) ----
|
|
// Exact strata k2_shortconv4_batched (static, no dyn): per channel c, token t,
|
|
// y = x0 + sw0*h0 + sw1*h1 + sw2*h2 + sw3*x0; y *= 1/sqrt(5)
|
|
// history ring (t-3,t-2,t-1) shifts h0<-h1<-h2<-x0. f32 history here; the
|
|
// reference uses a BF16 ring for bit-exact CPU match (a parity refinement).
|
|
|
|
__global__ void k_shortconv4_decode(const float* __restrict__ cur,
|
|
const float* __restrict__ static_w,
|
|
float* __restrict__ hist,
|
|
float* __restrict__ out,
|
|
const int32_t* __restrict__ slot_ids,
|
|
int M, int C) {
|
|
const int c = blockIdx.x * blockDim.x + threadIdx.x;
|
|
const int m = blockIdx.y;
|
|
if (c >= C || m >= M) return;
|
|
const int slot = slot_ids[m];
|
|
float* h = hist + (size_t)slot * 3 * C;
|
|
const float h0 = h[0 * C + c];
|
|
const float h1 = h[1 * C + c];
|
|
const float h2 = h[2 * C + c];
|
|
const float x0 = cur[(size_t)m * C + c];
|
|
const float* sw = static_w + (size_t)c * 4;
|
|
float y = x0 + sw[0] * h0 + sw[1] * h1 + sw[2] * h2 + sw[3] * x0;
|
|
y *= 0.44721359549995793f; // 1/sqrt(5)
|
|
h[0 * C + c] = h1;
|
|
h[1 * C + c] = h2;
|
|
h[2 * C + c] = x0;
|
|
out[(size_t)m * C + c] = y;
|
|
}
|
|
|
|
extern "C" int vetta_shortconv4_decode(const float* cur, const float* static_w,
|
|
float* hist, float* out, const int32_t* slot_ids,
|
|
int M, int C, vetta_stream_t stream) {
|
|
if (M <= 0 || C <= 0) return 1;
|
|
dim3 grid((C + 255) / 256, M);
|
|
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
|
|
k_shortconv4_decode<<<grid, 256, 0, s>>>(cur, static_w, hist, out, slot_ids, M, C);
|
|
HIP_CHECK(hipGetLastError());
|
|
return 0;
|
|
} |