[HIP] FP16 expert-path kernels (strata B=1 parity), opt-in; refactor run_experts

Add the exact strata B=1 FP16 expert path as verified kernels: k_cast_f32_f16 (RNE), k_nvfp4_gemv_f16 / k_mxfp8_gemv_f16 (FP16 activations, group partial dot then scale-fma, fdot2 order), k_situ_f16 (SiTU f32 -> stored FP16). Rust wrappers + expert_gemv_f16 dispatch.

Refactor the duplicated SWA/full expert loops into ld::layer::run_experts. Default stays all-f32 (verified parity baseline); VETTA_F16_EXPERT=1 selects the FP16 path.

MEASURED A/B (tok100): f32 argmax 220 (23.074) / 68014 22.643; fp16 argmax 220 (23.069) / 68014 22.632. FP16 shifts logits ~0.01 but does NOT flip the near-tie: the residual gap vs reference is the core-loop pass 1/2, NOT expert precision. FP16 kept opt-in for once the loop is fixed.

New real_expert_nvfp4_gemv_f16_parity (GPU fp16 vs CPU oracle over same fp16-rounded x) green. Verified: workspace -D warnings, 18 ld GPU + 17 hip GPU green.
This commit is contained in:
adm committed 2026-12-01 16:53:18 +01:00
1 parent 26bff57645
commit e4dcc5e015
5 files changed
+316 -31

No files matched your search

+137
View File
@@ -5,6 +5,7 @@
// GPU test) before it is wired into the executor.
#include <hip/hip_runtime.h>
#include <hip/hip_fp16.h>
#include <cstdio>
#include <cstring>
#include "vetta_kernels.h"
@@ -490,6 +491,31 @@ extern "C" int vetta_cast_f32_bf16(const float* x, uint16_t* y, size_t n,
return 0;
}
// ---- k_cast_f32_f16: f32 activation -> FP16 (RNE) ---------------------------
// Exact strata k_f32_to_f16: the B=1 expert path reads FP16 activations
// (mlpf16), and SiTU stores its output as FP16 before the w2 GEMV.
__global__ void k_cast_f32_f16(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] = __half_as_ushort(__float2half_rn(x[i]));
}
extern "C" int vetta_cast_f32_f16(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_f16<<<grid, nt, 0, s>>>(x, y, n);
HIP_CHECK(hipGetLastError());
return 0;
}
__device__ __forceinline__ float f16_to_f32(uint16_t h) {
return __half2float(__ushort_as_half(h));
}
// ---- 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),
@@ -565,6 +591,55 @@ extern "C" int vetta_nvfp4_gemv(const uint8_t* packed, const uint8_t* scales,
return 0;
}
// ---- k_nvfp4_gemv_f16: FP16-activation variant (exact strata B=1 path) ------
// x is FP16 (mlpf16); E2M1 nibbles are exact in FP16, so each product is
// f32(fp16 x) * e2m1. Group-of-16 partial sums are scaled AFTER the dot
// (acc = fmaf(d, sc, acc), exact k_moe_direct_w13_situ / w2_par fdot2 order).
__global__ void k_nvfp4_gemv_f16(const uint8_t* __restrict__ packed,
const uint8_t* __restrict__ scales,
float outer_scale, const uint16_t* __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;
float d = 0.f;
for (int j = 0; j < 8; ++j) {
uint8_t byte = w8[j];
d += f16_to_f32(x[k0 + j * 2]) * sf4_e2m1_f32(byte & 15u)
+ f16_to_f32(x[k0 + j * 2 + 1]) * sf4_e2m1_f32(byte >> 4);
}
acc = fmaf(d, sc, acc);
}
__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_f16(const uint8_t* packed, const uint8_t* scales,
float outer_scale, const uint16_t* 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_f16<<<N, 256, 0, s>>>(packed, scales, outer_scale, x, y, N, K);
HIP_CHECK(hipGetLastError());
return 0;
}
// ---- k_mxfp8_gemv (M=1 decode): MXFP8 experts (layers 20..23) --------------
// Exact strata MX8: y[N] = sum_k e4m3fn(W[n,k]) * ue8m0(scale[n,k/32]) * x[k].
// W [N,K] F8_E4M3 bytes (full K), scale [N,K/32] U8 UE8M0, NO outer scale.
@@ -613,6 +688,49 @@ extern "C" int vetta_mxfp8_gemv(const uint8_t* weights, const uint8_t* scales,
return 0;
}
// ---- k_mxfp8_gemv_f16: FP16-activation variant (exact strata MX8 path) ------
// E4M3 weights are exact in FP16; group-of-32 partial dot then scale-fma.
__global__ void k_mxfp8_gemv_f16(const uint8_t* __restrict__ weights,
const uint8_t* __restrict__ scales,
const uint16_t* __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 = weights + (size_t)n * K;
const uint8_t* ws = scales + (size_t)n * (K >> 5);
const int nblk = K >> 5;
float acc = 0.f;
for (int b = tid; b < nblk; b += nt) {
const float sc = ue8m0_to_f32(ws[b]);
const int k0 = b << 5;
const uint8_t* w8 = wp + k0;
float d = 0.f;
for (int j = 0; j < 32; ++j) d += f16_to_f32(x[k0 + j]) * e4m3fn_to_f32(w8[j]);
acc = fmaf(d, sc, acc);
}
__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_mxfp8_gemv_f16(const uint8_t* weights, const uint8_t* scales,
const uint16_t* x, float* y, int N, int K,
vetta_stream_t stream) {
if (N <= 0 || K <= 0 || (K % 32) != 0) return 1;
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
k_mxfp8_gemv_f16<<<N, 256, 0, s>>>(weights, scales, x, y, N, K);
HIP_CHECK(hipGetLastError());
return 0;
}
// ---- SiTU activation (exact strata situ_f32, double-softcapped) ------------
// gu rows are INTERLEAVED gate/up (exact strata k_moe_direct_w13_situ:
// row 2j=gate[j], row 2j+1=up[j]). act[j] = g_act * u_act where
@@ -641,6 +759,25 @@ extern "C" int vetta_situ(const float* gu, float* act, int inter, float beta,
return 0;
}
// ---- k_situ_f16: SiTU computed in f32, stored FP16 (exact strata act_out) ---
// The reference stores static_cast<half_t>(situ_f32(g, u)) before the w2 GEMV.
__global__ void k_situ_f16(const float* __restrict__ gu, uint16_t* __restrict__ act,
int inter, float beta, float lb) {
int j = blockIdx.x * blockDim.x + threadIdx.x;
if (j < inter) act[j] = __half_as_ushort(__float2half_rn(situ_f32(gu[2 * j], gu[2 * j + 1], beta, lb)));
}
extern "C" int vetta_situ_f16(const float* gu, uint16_t* 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_f16<<<(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,