[HIP] NVFP4 expert GEMV kernel (exact E2M1+E4M3 dequant, GPU-verified ~4e-7)

- kernels/hip: NVFP4 (SF4) codecs ported exactly from strata ink_hip_common —
  sf4_e2m1_f32 (E2M1 nibble LUT {0,0.5,1,1.5,2,3,4,6,...}), e4m3fn_to_f32
  (F8_E4M3FN: subnormal mant*2^-9, NaN->0, bias 7). Layout: packed_e2m1
  [N,K/2] (low nib even col), scales_e4m3 [N,K/16], f32 outer_scale.
- k_nvfp4_gemv (M=1 decode): y[N] = dequant(W)[N,K] @ x[K], one block/row,
  threads stride K/16 groups, f32 accumulate. Correctness-first; the Wave32
  bandwidth-saturated path is the performance pass. This is the core of the
  MoE expert compute (w13/w2); the full direct path (SiTU activation +
  weighted reduce) builds on it.
- C ABI + safe wrapper nvfp4_gemv + CPU-oracle parity test (Rust e2m1/e4m3
  matching the kernel).

Verified LIVE on gfx1150: nvfp4_gemv max_rel_err ~4e-7 across (8,64)/(16,256)/
(32,128). Eight compute kernels now verified. Default suite 97/97, lean 4
files green; GPU parity 8/8 (--features hip).
This commit is contained in:
adm committed 2026-12-01 14:07:14 +01:00
1 parent 87a66aee37
commit bd49da5636
3 files changed
+149

No files matched your search

+75
View File
@@ -343,4 +343,79 @@ extern "C" int vetta_cast_f32_bf16(const float* x, uint16_t* y, size_t n,
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;
}