[HIP] NVFP4 f16 GEMV performance pass: hardware fdot2 + register half2 dequant + wave-per-row
Port the exact strata k_sf4_gemv path (the 'performance pass' the original correctness-first kernel deferred): one wave per output row (8 rows/block), E2M1->FP16 register LUT dequant, hardware v_dot2_f32_f16 (fdot2_f32) over vectorized uint4 activation loads, DPP wave32 reduction. Adds sf4_e2m1_f16, fdot2_f32, wave32_sum helpers (gfx1150 builtins). Parity verified: real expert w13 f16 max_rel_err 8.4e-5 (tol 2e-3); hip 18 + ld 18 GPU green. MEASURED decode (64 tok greedy, eager-resident): f32-scalar 4.63 tok/s vs f16-fdot2 4.86 tok/s (1.05x). Profiler shows why it is modest: experts are only ~45ms of ~185ms/token; decode is dominated by attention + ~1300 kernel launches (host submission), not expert compute. This kernel is the verified foundation for the WMMA M>1 prefill GEMM (next brick); the decode lever is HIP graph capture.
This commit is contained in:
1 parent
58940840d6
commit
8d886bd56f
1 file changed
+66
-29
@@ -665,6 +665,30 @@ __device__ __forceinline__ float e4m3fn_to_f32(uint8_t b) {
|
||||
return __builtin_bit_cast(float, f);
|
||||
}
|
||||
|
||||
// ---- RDNA3.5 hardware dot/dequant helpers (exact strata k_sf4_gemv path) ----
|
||||
// E2M1 nibble -> exact FP16 bit pattern (register dequant, zero conversion cost).
|
||||
__device__ __forceinline__ __half sf4_e2m1_f16(uint32_t nib) {
|
||||
static constexpr uint16_t kLut[16] = {
|
||||
0x0000u, 0x3800u, 0x3C00u, 0x3E00u, 0x4000u, 0x4200u, 0x4400u, 0x4600u,
|
||||
0x8000u, 0xB800u, 0xBC00u, 0xBE00u, 0xC000u, 0xC200u, 0xC400u, 0xC600u
|
||||
};
|
||||
return __ushort_as_half(kLut[nib & 15u]);
|
||||
}
|
||||
// v_dot2_f32_f16: two f16 products accumulated into f32 in ONE instruction.
|
||||
__device__ __forceinline__ float fdot2_f32(uint32_t a, uint32_t b, float c) {
|
||||
return __builtin_amdgcn_fdot2(__builtin_bit_cast(__half2, a),
|
||||
__builtin_bit_cast(__half2, b), c, false);
|
||||
}
|
||||
// DPP-accelerated wave32 sum (gfx11): 2 DPP moves + 3 shfl instead of 5 shfl.
|
||||
__device__ __forceinline__ float wave32_sum(float v) {
|
||||
v += __builtin_bit_cast(float, __builtin_amdgcn_mov_dpp(__builtin_bit_cast(int, v), 0xB1, 0xf, 0xf, true));
|
||||
v += __builtin_bit_cast(float, __builtin_amdgcn_mov_dpp(__builtin_bit_cast(int, v), 0x4E, 0xf, 0xf, true));
|
||||
v += __shfl_xor(v, 4, 32);
|
||||
v += __shfl_xor(v, 8, 32);
|
||||
v += __shfl_xor(v, 16, 32);
|
||||
return v;
|
||||
}
|
||||
|
||||
// ---- 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.
|
||||
@@ -712,43 +736,56 @@ 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).
|
||||
// ---- k_nvfp4_gemv_f16: FP16-activation variant (exact strata k_sf4_gemv) ----
|
||||
// Performance pass: one WAVE per output row (8 rows/block), register half2
|
||||
// dequant via the exact E2M1 FP16 LUT, hardware fdot2_f32 (v_dot2_f32_f16) over
|
||||
// vectorized uint4 activation loads, DPP wave32 reduction. Group-of-16 partial
|
||||
// dot is scaled AFTER (acc = fmaf(d, sc, acc)) — identical math to the scalar
|
||||
// oracle (E2M1 exact in FP16, f16*f16 product exact in f32), only the f32
|
||||
// accumulation order differs (within the 2e-3 parity tolerance).
|
||||
|
||||
__global__ void k_nvfp4_gemv_f16(const uint8_t* __restrict__ packed,
|
||||
__global__ void __launch_bounds__(256) 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;
|
||||
const int wave = threadIdx.x >> 5;
|
||||
const int lane = threadIdx.x & 31;
|
||||
const int row = blockIdx.x * 8 + wave;
|
||||
if (row >= N) return;
|
||||
const uint8_t* wp = packed + (size_t)row * (K >> 1);
|
||||
const uint8_t* ws = scales + (size_t)row * (K >> 4);
|
||||
const __half* xh = reinterpret_cast<const __half*>(x);
|
||||
const int nblk16 = K >> 4;
|
||||
float acc = 0.f;
|
||||
for (int b = tid; b < nblk; b += nt) {
|
||||
for (int b = lane; b < nblk16; b += 32) {
|
||||
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);
|
||||
const uint2 raw = *reinterpret_cast<const uint2*>(wp + b * 8);
|
||||
uint32_t w2[8];
|
||||
#pragma unroll
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
const uint32_t byte0 = (raw.x >> (i * 8)) & 0xFFu;
|
||||
const uint32_t byte1 = (raw.y >> (i * 8)) & 0xFFu;
|
||||
const __half2 h0 = __halves2half2(sf4_e2m1_f16(byte0 & 15u), sf4_e2m1_f16(byte0 >> 4));
|
||||
const __half2 h1 = __halves2half2(sf4_e2m1_f16(byte1 & 15u), sf4_e2m1_f16(byte1 >> 4));
|
||||
w2[i] = __builtin_bit_cast(uint32_t, h0);
|
||||
w2[4 + i] = __builtin_bit_cast(uint32_t, h1);
|
||||
}
|
||||
const int k0 = b << 4;
|
||||
const uint4 x0 = *reinterpret_cast<const uint4*>(xh + k0);
|
||||
const uint4 x1 = *reinterpret_cast<const uint4*>(xh + k0 + 8);
|
||||
float d = 0.f;
|
||||
d = fdot2_f32(w2[0], x0.x, d);
|
||||
d = fdot2_f32(w2[1], x0.y, d);
|
||||
d = fdot2_f32(w2[2], x0.z, d);
|
||||
d = fdot2_f32(w2[3], x0.w, d);
|
||||
d = fdot2_f32(w2[4], x1.x, d);
|
||||
d = fdot2_f32(w2[5], x1.y, d);
|
||||
d = fdot2_f32(w2[6], x1.z, d);
|
||||
d = fdot2_f32(w2[7], x1.w, d);
|
||||
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];
|
||||
const float sum = wave32_sum(acc);
|
||||
if (lane == 0) y[row] = sum;
|
||||
}
|
||||
|
||||
extern "C" int vetta_nvfp4_gemv_f16(const uint8_t* packed, const uint8_t* scales,
|
||||
@@ -756,7 +793,7 @@ extern "C" int vetta_nvfp4_gemv_f16(const uint8_t* packed, const uint8_t* scales
|
||||
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);
|
||||
k_nvfp4_gemv_f16<<<(N + 7) / 8, 256, 0, s>>>(packed, scales, outer_scale, x, y, N, K);
|
||||
HIP_CHECK(hipGetLastError());
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user