[HIP] Device-routed NVFP4 f16 GEMV (indirect): foundation for graph-capturable MoE

Refactor k_nvfp4_gemv_f16 into a shared device body + direct wrapper + indirect
wrapper. The indirect variant reads the expert id from a DEVICE buffer (ids[slot])
and indexes a contiguous per-layer weight arena (packed/scales/outer), so launch
parameters stay static — no host ids round-trip. This is the prerequisite for
HIP graph capture of the decode step (current MoE dispatch is host-mediated).

Math identical to the direct kernel (same body): parity test builds a synthetic
4-expert arena and routes 4 slots, asserting BIT-EXACT equality indirect vs direct.
Additive only — default forward path unchanged.

Verified: hip GPU 19 green (incl. new indirect parity), workspace CPU -D warnings
green, 6 Lean proofs 0 sorryAx.
This commit is contained in:
adm committed 2026-12-01 22:35:31 +01:00
1 parent 8d886bd56f
commit 4c82040610
2 files changed
+99 -2

No files matched your search

+41 -1
View File
@@ -744,7 +744,7 @@ extern "C" int vetta_nvfp4_gemv(const uint8_t* packed, const uint8_t* scales,
// 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 __launch_bounds__(256) k_nvfp4_gemv_f16(const uint8_t* __restrict__ packed,
__device__ __forceinline__ void nvfp4_gemv_f16_body(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) {
@@ -788,6 +788,46 @@ __global__ void __launch_bounds__(256) k_nvfp4_gemv_f16(const uint8_t* __restric
if (lane == 0) y[row] = sum;
}
__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) {
nvfp4_gemv_f16_body(packed, scales, outer_scale, x, y, N, K);
}
// Indirect (device-routed) variant for graph-capturable MoE: the expert id is
// read from a DEVICE buffer (ids[slot]) and indexes a contiguous per-layer
// weight arena, so launch parameters stay static (no host round-trip). Strides
// are the per-expert byte spans of the packed-weight / scale arenas; outer_arena
// holds one precomputed outer_scale per expert. Math identical to the direct
// kernel (same body) — only the weight base address is selected on device.
__global__ void __launch_bounds__(256) k_nvfp4_gemv_f16_indirect(
const uint8_t* __restrict__ arena_packed,
const uint8_t* __restrict__ arena_scales,
const float* __restrict__ outer_arena,
const int* __restrict__ ids, int slot,
const uint16_t* __restrict__ x,
float* __restrict__ y, int N, int K,
long w_stride, long sc_stride) {
const int id = ids[slot];
nvfp4_gemv_f16_body(arena_packed + (long)id * w_stride,
arena_scales + (long)id * sc_stride,
outer_arena[id], x, y, N, K);
}
extern "C" int vetta_nvfp4_gemv_f16_indirect(const uint8_t* arena_packed,
const uint8_t* arena_scales, const float* outer_arena,
const int* ids, int slot, const uint16_t* x, float* y,
int N, int K, long w_stride, long sc_stride,
vetta_stream_t stream) {
if (N <= 0 || K <= 0 || (K % 16) != 0 || slot < 0) return 1;
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
k_nvfp4_gemv_f16_indirect<<<(N + 7) / 8, 256, 0, s>>>(
arena_packed, arena_scales, outer_arena, ids, slot, x, y, N, K, w_stride, sc_stride);
HIP_CHECK(hipGetLastError());
return 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) {