This repository has been archived on 2026-11-24. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
radiance-sycl/libr4d/r4d_moe_mxfp4.hip
T
NunzeandJohn McDonough 78c58841bf libr4d: GLM-5.3's kernels -- mHC, KDA, the DSA indexer, MLA, f32-scaled FP8, MXFP4 experts
The device rows for GLM-5.3-Flash's ops, at its geometry and at TP4 and TP8 slices:

- mhc_pre / mhc_post / mhc_contract, and mhc_pre_partial / mhc_pre_finish, which run mhc_pre's
  own projection and finishing kernels with its chunk-sum order, so the pair is byte-identical
  to mhc_pre at n 4096, hc 4, chunk 2048.
- kda_update_k128_v128_bf16, one, two or four value rows a wave, both state forms.
- dsa_pool_fp8, dsa_query_fp8 and dsa_score_fp8 (fp8 WMMA, 32 heads of 128).
- mla_attn: a matrix row whose WMMA tile holds a query's heads, or pairs eight heads of two
  neighbouring queries over the union of their page tables, above the wave row; mla_project
  with up to eight rows a thread or wave.
- gemm_nt_f32 and router_sigmoid_topk in tiled and one-wave-a-row forms; gemm_fp8a8_f32scale
  and moe_gemm_fp8a8_f32scale read f32 block scales in place; moe_gemm_mxfp4a8 decodes e2m1 into
  exact E4M3 bytes for fp8 WMMA, with an expert-major decode form and a wide prefill form.
- swiglu_clamped, and the device-only fusions of GLM's MoE block: swiglu_clamped_quant_fp8,
  router_sigmoid_topk_scatter, and moe_scatter_inv / moe_gather_inv, which pass the gather's slot
  table from the sort instead of rebuilding it.

moe_gather takes f32 routing weights in its column-per-thread kernel. The sort and the gather
kernels move from r4d_moe.hip into r4d_moe_sort.h and r4d_moe_gather.h, so the fused router and
gather instantiate the same code.

Every form that replaces another gives its bytes: tests/r4d_emu runs the kernels on the host
(test_f32, test_fp8f32, test_kda, test_mhc, test_mla, test_moefuse, test_moemx), r4d_selftest
compares them on a card against libref and against each other, and plugin_test pins the
mla_attn band. docs/OPS.md states what libr4d serves.

Co-Authored-By: John McDonough <18016200+JohnCMcDonough@users.noreply.github.com>
2026-10-19 20:13:47 +02:00

1157 lines
59 KiB
Plaintext

/* Canonical MXFP4 experts: packed E2M1 codes and E8M0 bytes remain in their
* source layout, including mapped host expert tables. A wave decodes packed
* words into exact E4M3 staging bytes for FP8 WMMA. Activation then weight
* scales fold in F32 per 32 values, matching the reference grouping. */
#include <hip/hip_runtime.h>
#include "r4d_args.h"
#include "r4d_common.h"
#include "r4d_emit.h"
#include "r4d_fp8_wmma.h"
#include "r4d_mxfp4.h"
#include <climits>
#include <algorithm>
#include <cstdlib>
namespace {
struct MxMeta {
long wstep[2], sstep[2];
int wrow[2], srow[2], n[2], k[2];
long ar, asr, yr;
int T, Arows, N, K, experts, topk, sorted_a, tile, parts, aligned_jobs;
int stage_out, out16;
};
__device__ const uint8_t* entry(const void* base, long step, int e) {
return step ? (const uint8_t*)base + (long)e * step : ((const uint8_t* const*)base)[e];
}
/* E2M1 values are exactly representable in E4M3. Stage those codes directly,
* use FP8 WMMA, and fold each 32-wide E8M0 scale into the F32 accumulator.
* No persistent expansion or checkpoint relayout is needed. */
// Four independent bytes: magnitudes >=2 map to (m+12)*4; 0/1 map to 0/48.
// Expand each magnitude-test bit into a byte mask without cross-byte carries.
__device__ __forceinline__ uint32_t fp4x4(uint32_t n) {
const uint32_t mag = n & 0x07070707u, flag = (mag + 0x06060606u) & 0x08080808u;
const uint32_t mask = (flag << 5) - (flag >> 3);
return ((((mag + 0x0c0c0c0cu) << 2) & mask) | (((mag & 0x01010101u) * 0x30u) & ~mask) |
((n & 0x08080808u) << 4));
}
/* Eight codes of a packed word to their E4M3 bytes in k order. The table form (r4d_mxfp4.h, v_perm
* on the magnitude) gives the same bytes in about a third of the instructions; the arithmetic form
* stays reachable (RADIANCE_MXFP4_LUT=0) as the bytewise reference. */
template <bool Lut>
__device__ __forceinline__ uint2 mx_codes8(uint32_t packed) {
if constexpr (Lut) {
uint32_t lo, hi;
r4d_mxfp4_unpack8_exact(packed, &lo, &hi);
return uint2{ lo, hi };
} else {
const uint32_t low = fp4x4(packed & 0x0f0f0f0fu), high = fp4x4((packed >> 4) & 0x0f0f0f0fu);
return uint2{ __byte_perm(low, high, 0x5140), __byte_perm(low, high, 0x7362) };
}
}
template <int ColTiles, int RowTiles, bool CachedRows, bool Lut>
__global__ void moe_mxfp4_fp8_kernel(const uint8_t* a, const float* as, const void* w0,
const void* s0, const void* w1, const void* s1,
const int* sorted, const int* offset, uint16_t* y, MxMeta m) {
__shared__ __align__(16) uint8_t aa[16 * RowTiles * 136], bb[16 * ColTiles * 136];
const int tid = threadIdx.x, lane = tid & 31, wave = tid >> 5,
col0 = blockIdx.x * (16 * ColTiles);
constexpr int JobRows = 16 * RowTiles, Threads = 32 * ColTiles * RowTiles;
const int wave_col = ColTiles == 1 ? 0 : wave % ColTiles,
wave_row = RowTiles == 1 ? 0 : wave / ColTiles;
int first = blockIdx.y * m.tile;
int group_end = min((int)(blockIdx.y + 1) * m.tile, m.T), job_expert = 0;
if (m.aligned_jobs) {
// Expert e begins at job floor(offset[e]/JobRows)+e. The extra job per expert
// bounds padding without a prefix-sum pass; every live job owns one row tile
// of one expert, so hot experts still spread across independent CTAs.
const int job = blockIdx.y, tail = offset[m.experts] / JobRows + m.experts;
if (job >= tail) {
const int r0 = offset[m.experts] + (job - tail) * JobRows;
for (int r = r0; r < min(r0 + JobRows, m.T); ++r)
if (tid < 16 * ColTiles && col0 + tid < m.N) y[(long)r * m.yr + col0 + tid] = 0;
return;
}
int lo = 0, hi = m.experts;
while (lo + 1 < hi) {
const int mid = (lo + hi) / 2;
if (offset[mid] / JobRows + mid <= job)
lo = mid;
else
hi = mid;
}
job_expert = lo;
first = offset[lo] + (job - (offset[lo] / JobRows + lo)) * JobRows;
group_end = min(first + JobRows, offset[lo + 1]);
}
for (int start = first; start < group_end;) {
if (sorted[start] < 0 || offset[m.experts] <= start) {
if (tid < 16 * ColTiles && col0 + tid < m.N) y[(long)start * m.yr + col0 + tid] = 0;
++start;
continue;
}
int lo = m.aligned_jobs ? job_expert : 0, hi = m.experts;
if (!m.aligned_jobs)
while (lo + 1 < hi) {
const int mid = (lo + hi) / 2;
if (offset[mid] <= start)
lo = mid;
else
hi = mid;
}
const int cls = lo & 1, end = min(group_end, offset[lo + 1]);
if (end <= start) return;
const auto* w = entry(cls ? w1 : w0, m.wstep[cls], lo / 2);
const auto* ws = entry(cls ? s1 : s0, m.sstep[cls], lo / 2);
auto total = r4d_fp8::acc_zero();
// Routing is invariant across K; each activation scale covers all four groups.
int sources[8];
if constexpr (CachedRows) {
#pragma unroll
for (int row = 0; row < 8; ++row) {
const int r = start + wave_row * 16 + (lane >> 4) * 8 + row;
const int flat = r < end ? sorted[r] : -1;
const int ar = flat < 0 ? -1 : m.sorted_a ? r : flat / m.topk;
sources[row] = ar >= 0 && ar < m.Arows ? ar : -1;
}
}
for (int kb = 0; kb < m.k[cls]; kb += 128) {
for (int j = tid; j < 16 * RowTiles * 16; j += Threads) {
const int r = j / 16, d = (j % 16) * 8;
const int ar = start + r < end && sorted[start + r] >= 0
? (m.sorted_a ? start + r : sorted[start + r] / m.topk)
: -1;
uint2 av{};
if (ar >= 0 && ar < m.Arows) __builtin_memcpy(&av, a + (long)ar * m.ar + kb + d, 8);
*reinterpret_cast<uint2*>(aa + r * 136 + d) = av;
}
for (int j = tid; j < 16 * ColTiles * 16; j += Threads) {
const int r = j / 16, d = (j % 16) * 8;
const int n = col0 + r, part = n / (m.N / m.parts), within = n % (m.N / m.parts);
const int wn = part * (m.n[cls] / m.parts) + within;
uint32_t packed = 0;
if (n < m.N && within < m.n[cls] / m.parts)
__builtin_memcpy(&packed, w + (long)wn * m.wrow[cls] + (kb + d) / 2, 4);
*reinterpret_cast<uint2*>(bb + r * 136 + d) = mx_codes8<Lut>(packed);
}
__syncthreads();
if (start + wave_row * 16 < end) {
const int n = col0 + wave_col * 16 + (lane & 15), part = n / (m.N / m.parts),
within = n % (m.N / m.parts);
const int wn = part * (m.n[cls] / m.parts) + within;
const bool valid = n < m.N && within < m.n[cls] / m.parts;
float activation[8];
if constexpr (CachedRows) {
#pragma unroll
for (int row = 0; row < 8; ++row)
activation[row] =
sources[row] >= 0 ? as[(long)sources[row] * m.asr + kb / 128] : 0;
}
#pragma unroll
for (int group = 0; group < 4; ++group) {
const int exp = valid ? ws[(long)wn * m.srow[cls] + kb / 32 + group] : 127;
auto raw = r4d_fp8::acc_zero();
#pragma unroll
for (int k = group * 32; k < (group + 1) * 32; k += 16)
raw = r4d_fp8::mma(r4d_fp8::frag<128>(aa, wave_row * 16, k, lane),
r4d_fp8::frag<128>(bb, wave_col * 16, k, lane), raw);
// E8M0 maps directly to the F32 exponent field; code zero is 2^-127,
// a representable subnormal, and code 255 is NaN.
const float scale = __uint_as_float(exp == 255 ? 0x7fc00000u
: exp ? ((uint32_t)exp << 23)
: 0x00400000u);
#pragma unroll
for (int row = 0; row < 8; ++row) {
if constexpr (CachedRows) {
if (sources[row] >= 0) total[row] += raw[row] * activation[row] * scale;
} else {
const int r = start + wave_row * 16 + (lane >> 4) * 8 + row;
if (r < end) {
const int ar = sorted[r] < 0 ? -1
: m.sorted_a ? r
: sorted[r] / m.topk;
if (ar >= 0 && ar < m.Arows)
total[row] +=
raw[row] * as[(long)ar * m.asr + kb / 128] * scale;
}
}
}
}
}
__syncthreads();
}
#pragma unroll
for (int row = 0; row < 8; ++row) {
const int r = start + wave_row * 16 + (lane >> 4) * 8 + row,
n = col0 + wave_col * 16 + (lane & 15);
if (r < end && n < m.N) y[(long)r * m.yr + n] = f32_to_bf16(total[row]);
}
start = end;
}
}
/* An expert's weight and exponent rows reached through entry() are generic pointers, so their loads
* compile to flat loads, which every LDS wait (s_wait_dscnt) also waits for: a barrier then stalls
* on the prefetch. The tables hold device or device-mapped host addresses, so a global load reads
* the same bytes. Byte-aligned, as a stacked plane's row pitch may be odd. */
typedef r4d_u32x4 mx_g16 __attribute__((aligned(1)));
typedef uint32_t mx_g4 __attribute__((aligned(1)));
__device__ __forceinline__ r4d_u32x4 mx_ldg16(const uint8_t* p) {
return *(const __attribute__((address_space(1))) mx_g16*)p;
}
__device__ __forceinline__ uint32_t mx_ldg4(const uint8_t* p) {
return *(const __attribute__((address_space(1))) mx_g4*)p;
}
/* The TP8 prefill form (M32N64, aligned jobs, cached rows) with its loads a stage ahead. A job's
* row table resolves into LDS once; the next 128 of K (activation bytes, weight codes, an exponent
* dword a column, the activation scales) is loaded into registers while the current stage computes
* from LDS. The form above waits on three dependent latencies a stage: the routing entry, the
* activation row it names, and the exponent bytes it reads after the barrier. A job begins at its
* first row even when that slot is dropped; rows are independent, so the bytes are the same.
* The exponents are decoded once, at staging; the fold carries no row predicate, the epilogue
* writes a dropped row's zero instead.
* One stage ahead only: a deeper prefetch measured slower on the expert-major forms. The barriers
* order LDS only (lds_barrier), so the prefetch stays in flight across them and L0 keeps the
* activation rows the next column block reads. */
template <bool Lut>
__global__ __launch_bounds__(256)
R4D_WAVES(16) void moe_mxfp4_prefill_kernel(const uint8_t* a, const float* as, const void* w0,
const void* s0, const void* w1, const void* s1,
const int* sorted, const int* offset, uint16_t* y,
MxMeta m) {
constexpr int BN = 64, BM = 32, Threads = 256, P = 136;
static_assert(BM * 128 / 16 == Threads && BN * 64 / 16 == Threads,
"one 16-byte chunk a thread and operand");
__shared__ __align__(16) uint8_t aa[BM * P], bb[BN * P];
__shared__ __align__(16) float ss[4 * BN];
__shared__ float sa[BM];
__shared__ int src[BM];
const int tid = threadIdx.x, lane = tid & 31, wave = tid >> 5, col0 = blockIdx.x * BN;
const int wave_col = wave & 3, wave_row = wave >> 2;
const int job = blockIdx.y, tail = offset[m.experts] / BM + m.experts;
if (job >= tail) {
const int r0 = offset[m.experts] + (job - tail) * BM;
for (int r = r0; r < min(r0 + BM, m.T); ++r)
if (tid < BN && col0 + tid < m.N) y[(long)r * m.yr + col0 + tid] = 0;
return;
}
int lo = 0, hi = m.experts;
while (lo + 1 < hi) {
const int mid = (lo + hi) / 2;
if (offset[mid] / BM + mid <= job)
lo = mid;
else
hi = mid;
}
const int first = offset[lo] + (job - (offset[lo] / BM + lo)) * BM,
end = min(first + BM, offset[lo + 1]);
if (first >= end) return;
const int cls = lo & 1, kc = m.k[cls], pw = m.N / m.parts, cw = m.n[cls] / m.parts;
const auto* w = entry(cls ? w1 : w0, m.wstep[cls], lo / 2);
const auto* ws = entry(cls ? s1 : s0, m.sstep[cls], lo / 2);
if (tid < BM) {
const int r = first + tid, flat = r < end ? sorted[r] : -1;
const int ar = flat < 0 ? -1 : m.sorted_a ? r : flat / m.topk;
src[tid] = ar >= 0 && ar < m.Arows ? ar : -1;
}
// The block's columns are fixed, so each thread's weight row and exponent row are too.
const int wcol = tid >> 2, wd = (tid & 3) * 16, wn_c = col0 + wcol, wn_w = wn_c % pw;
const bool wvalid = wn_c < m.N && wn_w < cw;
const uint8_t* wp = w + (long)((wn_c / pw) * cw + wn_w) * m.wrow[cls] + wd;
const int en_c = col0 + tid, en_w = en_c % pw;
const bool evalid = tid < BN && en_c < m.N && en_w < cw;
const uint8_t* ep = ws + (long)((en_c / pw) * cw + en_w) * m.srow[cls];
lds_barrier();
const int arow = tid >> 3, ad = (tid & 7) * 16, a_src = src[arow];
const uint8_t* ap = a + (long)max(a_src, 0) * m.ar + ad;
const int sa_row = tid - BN, sa_src = sa_row >= 0 && sa_row < BM ? src[sa_row] : -1;
const float* asp = as + (long)max(sa_src, 0) * m.asr;
const int rowbase = wave_row * 16 + (lane >> 4) * 8, mycol = wave_col * 16 + (lane & 15);
const bool live = first + wave_row * 16 < end;
bool rv[8];
#pragma unroll
for (int j = 0; j < 8; ++j) rv[j] = src[rowbase + j] >= 0;
r4d_u32x4 ra, rw;
uint32_t rs = 0x7f7f7f7fu;
float rsa = 0;
auto load = [&](int k0) {
ra = r4d_u32x4{ 0, 0, 0, 0 };
if (a_src >= 0) __builtin_memcpy(&ra, ap + k0, 16);
rw = r4d_u32x4{ 0, 0, 0, 0 };
if (wvalid) rw = mx_ldg16(wp + k0 / 2);
if (evalid) rs = mx_ldg4(ep + k0 / 32);
if (sa_src >= 0) rsa = asp[k0 / 128];
};
auto store = [&]() {
*reinterpret_cast<r4d_u32x4*>(aa + arow * P + ad) = ra;
#pragma unroll
for (int t = 0; t < 4; ++t)
*reinterpret_cast<uint2*>(bb + wcol * P + 2 * wd + 8 * t) = mx_codes8<Lut>(rw[t]);
if (tid < BN) {
// E8M0 maps directly to the F32 exponent field; code zero is 2^-127, a representable
// subnormal, and code 255 is NaN.
float sc[4];
#pragma unroll
for (int g = 0; g < 4; ++g) {
const uint32_t exp = (rs >> (8 * g)) & 0xffu;
sc[g] = __uint_as_float(exp == 255 ? 0x7fc00000u : exp ? (exp << 23) : 0x00400000u);
}
*reinterpret_cast<float4*>(ss + 4 * tid) = make_float4(sc[0], sc[1], sc[2], sc[3]);
}
if (sa_row >= 0 && sa_row < BM) sa[sa_row] = rsa;
};
auto total = r4d_fp8::acc_zero();
load(0);
for (int k0 = 0; k0 < kc; k0 += 128) {
store();
lds_barrier();
if (k0 + 128 < kc) load(k0 + 128);
if (live) {
const float4 sc4 = *reinterpret_cast<const float4*>(ss + 4 * mycol);
const float scales[4] = { sc4.x, sc4.y, sc4.z, sc4.w };
float activation[8];
#pragma unroll
for (int row = 0; row < 8; ++row) activation[row] = sa[rowbase + row];
#pragma unroll
for (int group = 0; group < 4; ++group) {
auto raw = r4d_fp8::acc_zero();
#pragma unroll
for (int k = group * 32; k < (group + 1) * 32; k += 16)
raw = r4d_fp8::mma(r4d_fp8::frag<128>(aa, wave_row * 16, k, lane),
r4d_fp8::frag<128>(bb, wave_col * 16, k, lane), raw);
// A dropped row folds zeros (or NaN under a code-255 scale) and is written as 0.
#pragma unroll
for (int row = 0; row < 8; ++row)
total[row] += raw[row] * activation[row] * scales[group];
// Two groups' WMMA chains at a time: with all four in flight the kernel takes 104
// VGPRs, 12 waves a SIMD at gfx1201's 24-register granule instead of 16.
if (group == 1) sched_barrier();
}
}
lds_barrier();
}
#pragma unroll
for (int row = 0; row < 8; ++row) {
const int r = first + rowbase + row, n = col0 + mycol;
if (r < end && n < m.N) y[(long)r * m.yr + n] = rv[row] ? f32_to_bf16(total[row]) : 0;
}
}
/* The wide prefill form (P64): jobs of BM rows of one expert, blocks of BN columns, eight waves
* as BM/16 row waves times 8/(BM/16) column waves, so a wave holds BN/(16*column waves) tiles of
* 16x16 and reuses each A fragment across them. M32N64 reads every expert slice once per 32 rows
* and a fragment pair per WMMA pair; at 57 rows an expert (2048 tokens) BM 64 halves the slice
* reads, and a row wave past the run's end skips its compute, so the taller job costs no WMMA.
*
* Resident (K <= 256, down): the job's whole A tile sits in LDS for the block's CT column tiles,
* walked as one stage sequence whose next stage (weight codes, exponents) is loaded while the
* current one computes, across tile boundaries; A is read once per CT tiles instead of once per
* column block. Otherwise (gate_up) A streams with the weights, a stage ahead.
*
* Each element keeps the row-group form's sequence: per 128 of K in order, per 32-wide group a
* fresh WMMA pair from zero, folded as (raw * activation scale) * E8M0 scale and added, two
* roundings and an add (the unit is built -ffp-contract=off). The tiles of a wave are separate
* accumulators, so no element sees another order. `stage_out` writes the output through LDS in
* 16-byte rows instead of a lane's 2-byte column stores.
*
* DB (streamed A only): two LDS stages, one barrier a stage. The next stage is stored into the
* other buffer after the current one computes, which every wave last read before the previous
* barrier; the single-buffer order needs a second barrier before it may overwrite the stage it
* computes from. Same bytes in LDS, same per-element sequence. */
template <int BM, int BN, int StageK, int CT, bool Resident, bool DB = false>
struct MxP64 {
static constexpr int Threads = 256, WR = BM / 16, WC = 8 / WR, TC = BN / (16 * WC),
KB = StageK / 128;
static constexpr int KR = 256, AK = Resident ? KR : StageK, PA = AK + 8, PB = StageK + 8,
PO = 2 * BN + 16;
static constexpr int TPA = Threads / BM, ACh = StageK / 16 / TPA, RCh = KR / 16 / TPA;
static constexpr int TPW = Threads / BN, WCh = StageK / 32 / TPW,
SAK = Resident ? KR / 128 : KB;
static constexpr int NB = DB ? 2 : 1, BB = NB * BN * PB > BM *PO ? NB * BN * PB : BM * PO;
static constexpr int Lds = NB * (BM * PA + 16 * KB * BN + 4 * SAK * BM) + BB + 4 * BM;
// The waves a SIMD the LDS admits (128 KiB a WGP, a block's eight waves over four SIMDs):
// stating it keeps the compiler from trading the stage registers for waves it cannot get.
static constexpr int Waves = 2 * (131072 / Lds > 8 ? 8 : 131072 / Lds);
};
template <int BM, int BN, int StageK, int CT, bool Resident, bool DB = false>
__global__ __launch_bounds__(256)
R4D_WAVES((MxP64<BM, BN, StageK, CT, Resident, DB>::Waves)) void moe_mxfp4_p64_kernel(
const uint8_t* a, const float* as, const void* w0, const void* s0, const void* w1,
const void* s1, const int* sorted, const int* offset, uint16_t* y, MxMeta m) {
using G = MxP64<BM, BN, StageK, CT, Resident, DB>;
constexpr int Threads = G::Threads, WC = G::WC, TC = G::TC, KB = G::KB, AK = G::AK, PA = G::PA,
PB = G::PB;
constexpr int PO = G::PO, TPA = G::TPA, TPW = G::TPW, SAK = G::SAK;
static_assert(BM % 16 == 0 && 8 % G::WR == 0 && TC >= 1 && TC * 16 * WC == BN &&
(Resident || CT == 1) && G::ACh >= 1 && G::WCh >= 1 && BN * KB <= Threads &&
BM * SAK <= Threads && !(DB && Resident) && G::Lds <= 65536,
"stage chunks must divide evenly over the block, in 64 KiB of LDS");
constexpr int NB = G::NB;
__shared__ __align__(16) uint8_t aa[NB * BM * PA], bb[G::BB];
__shared__ __align__(16) float ss[NB * 4 * KB * BN], sa[NB * SAK * BM];
__shared__ int src[BM];
const int tid = threadIdx.x, lane = tid & 31, wave = tid >> 5, wave_row = wave / WC,
wave_col = wave % WC;
const int colb = blockIdx.x * (BN * CT), job = blockIdx.y,
tail = offset[m.experts] / BM + m.experts;
if (job >= tail) {
const int r0 = offset[m.experts] + (job - tail) * BM, rows = min(r0 + BM, m.T) - r0;
for (int i = tid; i < rows * BN * CT; i += Threads) {
const int n = colb + i % (BN * CT);
if (n < m.N) y[(long)(r0 + i / (BN * CT)) * m.yr + n] = 0;
}
return;
}
int lo = 0, hi = m.experts;
while (lo + 1 < hi) {
const int mid = (lo + hi) / 2;
if (offset[mid] / BM + mid <= job)
lo = mid;
else
hi = mid;
}
const int first = offset[lo] + (job - (offset[lo] / BM + lo)) * BM,
end = min(first + BM, offset[lo + 1]);
if (first >= end) return;
const int cls = lo & 1, kc = m.k[cls], pw = m.N / m.parts, cw = m.n[cls] / m.parts;
const auto* w = entry(cls ? w1 : w0, m.wstep[cls], lo / 2);
const auto* ws = entry(cls ? s1 : s0, m.sstep[cls], lo / 2);
if (tid < BM) {
const int r = first + tid, flat = r < end ? sorted[r] : -1;
const int ar = flat < 0 ? -1 : m.sorted_a ? r : flat / m.topk;
src[tid] = ar >= 0 && ar < m.Arows ? ar : -1;
}
lds_barrier();
// A thread's A chunks share one row and its weight chunks one column, so each keeps a single
// row pointer; neighbouring threads cover neighbouring 16 bytes, as one coalesced row.
const int arow = tid / TPA, a0 = tid % TPA, a_src = src[arow];
const uint8_t* ap = a + (long)max(a_src, 0) * m.ar + a0 * 16;
const int wcol = tid / TPW, w0c = tid % TPW, ecol = tid / KB, ekb = tid % KB, skb = tid / BM;
const int sa_src = tid < BM * SAK ? src[tid % BM] : -1;
const float* asp = as + (long)max(sa_src, 0) * m.asr;
const int rowbase = wave_row * 16 + (lane >> 4) * 8, colw = wave_col * 16 * TC;
const bool live = first + wave_row * 16 < end;
const uint8_t *wp = w, *ep = ws;
bool wvalid = false, evalid = false;
auto columns = [&](int col0) {
const int wn = col0 + wcol, wi = wn % pw, en = col0 + ecol, ei = en % pw;
wvalid = wn < m.N && wi < cw;
wp = w + (long)((wn / pw) * cw + wi) * m.wrow[cls] + w0c * 16;
evalid = tid < BN * KB && en < m.N && ei < cw;
ep = ws + (long)((en / pw) * cw + ei) * m.srow[cls];
};
if constexpr (Resident) {
#pragma unroll
for (int i = 0; i < G::RCh; ++i) {
const int d = (a0 + i * TPA) * 16;
r4d_u32x4 v{ 0, 0, 0, 0 };
if (a_src >= 0 && d < kc) __builtin_memcpy(&v, ap + i * TPA * 16, 16);
*reinterpret_cast<r4d_u32x4*>(aa + arow * PA + d) = v;
}
if (tid < BM * SAK) sa[tid] = sa_src >= 0 && skb * 128 < kc ? asp[skb] : 0;
}
const int ksn = (kc + StageK - 1) / StageK,
tiles = Resident ? min(CT, (m.N - colb + BN - 1) / BN) : 1, S = tiles * ksn;
r4d_u32x4 ra[Resident ? 1 : G::ACh], rw[G::WCh];
uint32_t re = 0x7f7f7f7fu;
float rsa = 0;
int cur = -1;
auto load = [&](int s) {
const int tile = s / ksn, k0 = (s - tile * ksn) * StageK;
if (tile != cur) {
cur = tile;
columns(colb + tile * BN);
}
if constexpr (!Resident) {
#pragma unroll
for (int i = 0; i < G::ACh; ++i) {
ra[i] = r4d_u32x4{ 0, 0, 0, 0 };
if (a_src >= 0 && k0 + (a0 + i * TPA) * 16 < kc)
__builtin_memcpy(&ra[i], ap + k0 + i * TPA * 16, 16);
}
if (tid < BM * KB) rsa = sa_src >= 0 && k0 + skb * 128 < kc ? asp[k0 / 128 + skb] : 0;
}
#pragma unroll
for (int i = 0; i < G::WCh; ++i) {
rw[i] = r4d_u32x4{ 0, 0, 0, 0 };
if (wvalid && k0 + (w0c + i * TPW) * 32 < kc)
rw[i] = mx_ldg16(wp + k0 / 2 + i * TPW * 16);
}
re = 0x7f7f7f7fu;
if (evalid && k0 + ekb * 128 < kc) re = mx_ldg4(ep + (k0 + ekb * 128) / 32);
};
auto store = [&](int b) {
if constexpr (!Resident) {
#pragma unroll
for (int i = 0; i < G::ACh; ++i)
*reinterpret_cast<r4d_u32x4*>(aa + b * BM * PA + arow * PA + (a0 + i * TPA) * 16) =
ra[i];
if (tid < BM * KB) sa[b * SAK * BM + tid] = rsa;
}
#pragma unroll
for (int i = 0; i < G::WCh; ++i) {
#pragma unroll
for (int t = 0; t < 4; ++t)
*reinterpret_cast<uint2*>(bb + b * BN * PB + wcol * PB + (w0c + i * TPW) * 32 +
8 * t) = mx_codes8<true>(rw[i][t]);
}
if (tid < BN * KB) {
// E8M0 maps directly to the F32 exponent field; code zero is 2^-127, a representable
// subnormal, and code 255 is NaN.
#pragma unroll
for (int g = 0; g < 4; ++g) {
const uint32_t exp = (re >> (8 * g)) & 0xffu;
ss[b * 4 * KB * BN + (ekb * 4 + g) * BN + ecol] =
__uint_as_float(exp == 255 ? 0x7fc00000u
: exp ? (exp << 23)
: 0x00400000u);
}
}
};
r4d_fp8::f8v total[TC];
#pragma unroll
for (int t = 0; t < TC; ++t) total[t] = r4d_fp8::acc_zero();
auto compute = [&](int s, int b) {
const int k0 = (s % ksn) * StageK;
const uint8_t *ab = aa + b * BM * PA, *bp = bb + b * BN * PB;
const float* sp = ss + b * 4 * KB * BN;
#pragma unroll
for (int kb = 0; kb < KB; ++kb) {
if (k0 + kb * 128 >= kc) break;
const float* sar = sa + b * SAK * BM + (Resident ? k0 / 128 + kb : kb) * BM + rowbase;
float activation[8];
#pragma unroll
for (int row = 0; row < 8; ++row) activation[row] = sar[row];
const int ka = (Resident ? k0 : 0) + kb * 128;
#pragma unroll
for (int group = 0; group < 4; ++group) {
const auto fa0 = r4d_fp8::frag<AK>(ab, wave_row * 16, ka + group * 32, lane);
const auto fa1 = r4d_fp8::frag<AK>(ab, wave_row * 16, ka + group * 32 + 16, lane);
#pragma unroll
for (int t = 0; t < TC; ++t) {
auto raw = r4d_fp8::mma(
fa0, r4d_fp8::frag<StageK>(bp, colw + t * 16, kb * 128 + group * 32, lane),
r4d_fp8::acc_zero());
raw = r4d_fp8::mma(
fa1,
r4d_fp8::frag<StageK>(bp, colw + t * 16, kb * 128 + group * 32 + 16, lane),
raw);
const float scale = sp[(kb * 4 + group) * BN + colw + t * 16 + (lane & 15)];
// A dropped row folds zeros (or NaN under a code-255 scale) and is written as 0.
#pragma unroll
for (int row = 0; row < 8; ++row)
total[t][row] += raw[row] * activation[row] * scale;
}
// One group's TC chains at a time: they already give the WMMA pipe independent
// work, and a second group in flight would double the raw registers.
sched_barrier();
}
}
};
auto epilogue = [&](int col0) {
if (m.stage_out) {
if (live) {
#pragma unroll
for (int t = 0; t < TC; ++t) {
#pragma unroll
for (int row = 0; row < 8; ++row)
*reinterpret_cast<uint16_t*>(bb + (rowbase + row) * PO +
2 * (colw + t * 16 + (lane & 15))) =
src[rowbase + row] >= 0 ? f32_to_bf16(total[t][row]) : 0;
}
}
lds_barrier();
for (int j = tid; j < BM * BN / 8; j += Threads) {
const int r = j / (BN / 8), c = (j % (BN / 8)) * 8, n = col0 + c;
if (first + r >= end || n >= m.N) continue;
const r4d_u32x4 v = *reinterpret_cast<const r4d_u32x4*>(bb + r * PO + 2 * c);
uint16_t* dst = y + (long)(first + r) * m.yr + n;
if (m.out16 && n + 8 <= m.N)
*reinterpret_cast<r4d_u32x4*>(dst) = v;
else
for (int q = 0; q < 8 && n + q < m.N; ++q)
dst[q] = (uint16_t)(v[q / 2] >> (16 * (q & 1)));
}
lds_barrier();
} else if (live) {
#pragma unroll
for (int t = 0; t < TC; ++t) {
#pragma unroll
for (int row = 0; row < 8; ++row) {
const int r = first + rowbase + row, n = col0 + colw + t * 16 + (lane & 15);
if (r < end && n < m.N)
y[(long)r * m.yr + n] =
src[rowbase + row] >= 0 ? f32_to_bf16(total[t][row]) : 0;
}
}
}
};
load(0);
if constexpr (DB) {
store(0);
lds_barrier();
}
for (int s = 0; s < S; ++s) {
if constexpr (DB) {
if (s + 1 < S) load(s + 1);
if (live) compute(s, s & 1);
if (s + 1 < S) store((s + 1) & 1);
} else {
store(0);
lds_barrier();
if (s + 1 < S) load(s + 1);
if (live) compute(s, 0);
}
lds_barrier();
if (s % ksn == ksn - 1) {
epilogue(colb + (s / ksn) * BN);
#pragma unroll
for (int t = 0; t < TC; ++t) total[t] = r4d_fp8::acc_zero();
}
}
}
/* The decode form, expert-major: a block per (column block, expert) takes every row of its expert
* in 16-row tiles, so an expert slice is read from memory once however its rows fall in the
* sorted order (row groups re-read it once per group its run spans). Parallelism is column
* blocks times touched experts. Split-K would sum partials in another order than the row-group
* form and lose its bits, so each element keeps that form's sequence: per 128 of K in order, per
* 32-wide group a fresh WMMA pair, folded as raw * activation scale * E8M0 scale.
*
* A stage of StageK of K is loaded into registers while the previous one computes from LDS; at
* gate_up's K of 4096 the 256 stage measured fastest. Block row `experts` zeroes the dropped rows
* past offset[experts]. */
template <int Waves, int StageK, bool Lut>
__global__ __launch_bounds__(32 * Waves) void moe_mxfp4_expert_kernel(
const uint8_t* a, const float* as, const void* w0, const void* s0, const void* w1,
const void* s1, const int* sorted, const int* offset, uint16_t* y, MxMeta m) {
constexpr int BN = 16 * Waves, Threads = 32 * Waves, KB = StageK / 128, P = StageK + 8;
constexpr int AChunks = StageK / Threads, WChunks = BN * StageK / 32 / Threads;
constexpr int SChunks = (BN * KB + Threads - 1) / Threads;
static_assert(StageK % Threads == 0 && (BN * StageK / 32) % Threads == 0 && 16 * KB <= Threads,
"stage chunks must divide evenly over the block");
__shared__ __align__(16) uint8_t aa[16 * P], bb[BN * P];
__shared__ uint32_t ss[BN * KB];
__shared__ float sa[16 * KB];
__shared__ int src[16];
const int tid = threadIdx.x, lane = tid & 31, wave = tid >> 5, col0 = blockIdx.x * BN,
e = blockIdx.y;
if (e == m.experts) {
const int r0 = offset[m.experts];
for (long i = tid; i < (long)(m.T - r0) * BN; i += Threads) {
const int n = col0 + (int)(i % BN);
if (n < m.N) y[(long)(r0 + i / BN) * m.yr + n] = 0;
}
return;
}
const int lo = offset[e], hi = offset[e + 1];
if (lo >= hi) return;
const int cls = e & 1, kc = m.k[cls], pw = m.N / m.parts, cw = m.n[cls] / m.parts;
const auto* w = entry(cls ? w1 : w0, m.wstep[cls], e / 2);
const auto* ws = entry(cls ? s1 : s0, m.sstep[cls], e / 2);
const int rowbase = (lane >> 4) * 8, mycol = wave * 16 + (lane & 15);
auto wrow = [&](int c, bool& valid) {
const int n = col0 + c, within = n % pw;
valid = n < m.N && within < cw;
return (n / pw) * cw + within;
};
for (int r0 = lo; r0 < hi; r0 += 16) {
__syncthreads();
if (tid < 16) {
const int r = r0 + tid, flat = r < hi ? sorted[r] : -1;
const int ar = flat < 0 ? -1 : m.sorted_a ? r : flat / m.topk;
src[tid] = ar >= 0 && ar < m.Arows ? ar : -1;
}
__syncthreads();
bool rv[8];
#pragma unroll
for (int j = 0; j < 8; ++j) rv[j] = src[rowbase + j] >= 0;
uint4 ra[AChunks], rw[WChunks];
uint32_t rs[SChunks];
float rsa = 0;
auto load = [&](int k0) {
#pragma unroll
for (int i = 0; i < AChunks; ++i) {
const int j = tid + i * Threads, r = j / (StageK / 16),
d = (j % (StageK / 16)) * 16, ar = src[r];
ra[i] = uint4{ 0, 0, 0, 0 };
if (ar >= 0 && k0 + d < kc)
__builtin_memcpy(&ra[i], a + (long)ar * m.ar + k0 + d, 16);
}
#pragma unroll
for (int i = 0; i < WChunks; ++i) {
const int j = tid + i * Threads, c = j / (StageK / 32),
d = (j % (StageK / 32)) * 16;
bool valid;
const int wn = wrow(c, valid);
rw[i] = uint4{ 0, 0, 0, 0 };
if (valid && k0 + 2 * d < kc)
__builtin_memcpy(&rw[i], w + (long)wn * m.wrow[cls] + k0 / 2 + d, 16);
}
#pragma unroll
for (int i = 0; i < SChunks; ++i) {
const int j = tid + i * Threads, c = j / KB, kb = k0 + (j % KB) * 128;
bool valid;
const int wn = wrow(c, valid);
rs[i] = 0x7f7f7f7fu;
if (j < BN * KB && valid && kb < kc)
__builtin_memcpy(&rs[i], ws + (long)wn * m.srow[cls] + kb / 32, 4);
}
if (tid < 16 * KB) {
const int ar = src[tid / KB], kb = k0 + (tid % KB) * 128;
rsa = ar >= 0 && kb < kc ? as[(long)ar * m.asr + kb / 128] : 0;
}
};
auto store = [&]() {
#pragma unroll
for (int i = 0; i < AChunks; ++i) {
const int j = tid + i * Threads, r = j / (StageK / 16),
d = (j % (StageK / 16)) * 16;
*reinterpret_cast<uint4*>(aa + r * P + d) = ra[i];
}
#pragma unroll
for (int i = 0; i < WChunks; ++i) {
const int j = tid + i * Threads, c = j / (StageK / 32),
d = (j % (StageK / 32)) * 16;
const uint32_t q[4] = { rw[i].x, rw[i].y, rw[i].z, rw[i].w };
#pragma unroll
for (int t = 0; t < 4; ++t)
*reinterpret_cast<uint2*>(bb + c * P + 2 * d + 8 * t) = mx_codes8<Lut>(q[t]);
}
#pragma unroll
for (int i = 0; i < SChunks; ++i) {
const int j = tid + i * Threads;
if (j < BN * KB) ss[j] = rs[i];
}
if (tid < 16 * KB) sa[tid] = rsa;
};
auto total = r4d_fp8::acc_zero();
load(0);
for (int k0 = 0; k0 < kc; k0 += StageK) {
store();
__syncthreads();
if (k0 + StageK < kc) load(k0 + StageK);
#pragma unroll
for (int kb = 0; kb < KB; ++kb) {
if (k0 + kb * 128 >= kc) break;
const uint32_t exps = ss[mycol * KB + kb];
#pragma unroll
for (int group = 0; group < 4; ++group) {
auto raw = r4d_fp8::acc_zero();
#pragma unroll
for (int k = kb * 128 + group * 32; k < kb * 128 + (group + 1) * 32; k += 16)
raw = r4d_fp8::mma(r4d_fp8::frag<StageK>(aa, 0, k, lane),
r4d_fp8::frag<StageK>(bb, wave * 16, k, lane), raw);
const uint32_t exp = (exps >> (8 * group)) & 0xffu;
const float scale = __uint_as_float(exp == 255 ? 0x7fc00000u
: exp ? (exp << 23)
: 0x00400000u);
#pragma unroll
for (int row = 0; row < 8; ++row)
if (rv[row]) total[row] += raw[row] * sa[(rowbase + row) * KB + kb] * scale;
}
}
__syncthreads();
}
#pragma unroll
for (int row = 0; row < 8; ++row) {
const int r = r0 + rowbase + row, n = col0 + mycol;
if (r < hi && n < m.N) y[(long)r * m.yr + n] = f32_to_bf16(total[row]);
}
}
}
/* The expert-major form for a short K, same per-element sequence as the one above: a block walks
* ColTiles column tiles of 64, each over K in stages of StageK, as one stage sequence with the
* next stage loaded into registers while the current one computes. At down's K of 256 a single
* stage leaves a block nothing to overlap; 128 stages over four tiles give it eight, and halve
* LDS and registers a block, so twice the blocks are resident. Measured fastest for down at
* every routed-row count up to 16384, prefill forms included. A deeper prefetch, two-wave
* blocks and non-temporal weight loads measured slower. */
typedef unsigned mx_u4 __attribute__((ext_vector_type(4)));
template <int StageK>
struct MxStage {
static constexpr int Threads = 128, BN = 64, KB = StageK / 128;
static constexpr int AChunks = StageK / Threads, WChunks = BN * StageK / 32 / Threads;
static constexpr int SChunks = (BN * KB + Threads - 1) / Threads;
mx_u4 a[AChunks], w[WChunks];
uint32_t s[SChunks];
float sa;
};
template <int StageK, int ColTiles, bool Lut>
__global__ __launch_bounds__(128) void moe_mxfp4_expert_staged_kernel(
const uint8_t* a, const float* as, const void* w0, const void* s0, const void* w1,
const void* s1, const int* sorted, const int* offset, uint16_t* y, MxMeta m) {
using St = MxStage<StageK>;
constexpr int BN = St::BN, Threads = St::Threads, KB = St::KB, P = StageK + 8;
static_assert(StageK % Threads == 0 && (BN * StageK / 32) % Threads == 0 && 16 * KB <= Threads,
"stage chunks must divide evenly over the block");
__shared__ __align__(16) uint8_t aa[16 * P], bb[BN * P];
__shared__ uint32_t ss[BN * KB];
__shared__ float sa[16 * KB];
__shared__ int src[16];
const int tid = threadIdx.x, lane = tid & 31, wave = tid >> 5, e = blockIdx.y;
const int colb = blockIdx.x * (BN * ColTiles);
if (e == m.experts) {
const int r0 = offset[m.experts];
for (long i = tid; i < (long)(m.T - r0) * BN * ColTiles; i += Threads) {
const int n = colb + (int)(i % (BN * ColTiles));
if (n < m.N) y[(long)(r0 + i / (BN * ColTiles)) * m.yr + n] = 0;
}
return;
}
const int lo = offset[e], hi = offset[e + 1];
if (lo >= hi) return;
const int cls = e & 1, kc = m.k[cls], pw = m.N / m.parts, cw = m.n[cls] / m.parts;
const auto* w = entry(cls ? w1 : w0, m.wstep[cls], e / 2);
const auto* ws = entry(cls ? s1 : s0, m.sstep[cls], e / 2);
const int ksn = (kc + StageK - 1) / StageK, tiles = min(ColTiles, (m.N - colb + BN - 1) / BN),
S = tiles * ksn;
const int rowbase = (lane >> 4) * 8, mycol = wave * 16 + (lane & 15);
auto wrow = [&](int n, bool& valid) {
const int within = n % pw;
valid = n < m.N && within < cw;
return (n / pw) * cw + within;
};
for (int r0 = lo; r0 < hi; r0 += 16) {
__syncthreads();
if (tid < 16) {
const int r = r0 + tid, flat = r < hi ? sorted[r] : -1;
const int ar = flat < 0 ? -1 : m.sorted_a ? r : flat / m.topk;
src[tid] = ar >= 0 && ar < m.Arows ? ar : -1;
}
__syncthreads();
bool rv[8];
#pragma unroll
for (int j = 0; j < 8; ++j) rv[j] = src[rowbase + j] >= 0;
auto load = [&](int s, St& R) {
const int col0 = colb + (s / ksn) * BN, k0 = (s % ksn) * StageK;
#pragma unroll
for (int i = 0; i < St::AChunks; ++i) {
const int j = tid + i * Threads, r = j / (StageK / 16),
d = (j % (StageK / 16)) * 16, ar = src[r];
R.a[i] = mx_u4{ 0, 0, 0, 0 };
if (ar >= 0 && k0 + d < kc)
__builtin_memcpy(&R.a[i], a + (long)ar * m.ar + k0 + d, 16);
}
#pragma unroll
for (int i = 0; i < St::WChunks; ++i) {
const int j = tid + i * Threads, c = j / (StageK / 32),
d = (j % (StageK / 32)) * 16;
bool valid;
const int wn = wrow(col0 + c, valid);
R.w[i] = mx_u4{ 0, 0, 0, 0 };
if (valid && k0 + 2 * d < kc)
__builtin_memcpy(&R.w[i], w + (long)wn * m.wrow[cls] + k0 / 2 + d, 16);
}
#pragma unroll
for (int i = 0; i < St::SChunks; ++i) {
const int j = tid + i * Threads, c = j / KB, kb = k0 + (j % KB) * 128;
bool valid;
const int wn = wrow(col0 + c, valid);
R.s[i] = 0x7f7f7f7fu;
if (j < BN * KB && valid && kb < kc)
__builtin_memcpy(&R.s[i], ws + (long)wn * m.srow[cls] + kb / 32, 4);
}
if (tid < 16 * KB) {
const int ar = src[tid / KB], kb = k0 + (tid % KB) * 128;
R.sa = ar >= 0 && kb < kc ? as[(long)ar * m.asr + kb / 128] : 0;
}
};
auto store = [&](const St& R) {
#pragma unroll
for (int i = 0; i < St::AChunks; ++i) {
const int j = tid + i * Threads, r = j / (StageK / 16),
d = (j % (StageK / 16)) * 16;
*reinterpret_cast<mx_u4*>(aa + r * P + d) = R.a[i];
}
#pragma unroll
for (int i = 0; i < St::WChunks; ++i) {
const int j = tid + i * Threads, c = j / (StageK / 32),
d = (j % (StageK / 32)) * 16;
#pragma unroll
for (int t = 0; t < 4; ++t)
*reinterpret_cast<uint2*>(bb + c * P + 2 * d + 8 * t) =
mx_codes8<Lut>(R.w[i][t]);
}
#pragma unroll
for (int i = 0; i < St::SChunks; ++i) {
const int j = tid + i * Threads;
if (j < BN * KB) ss[j] = R.s[i];
}
if (tid < 16 * KB) sa[tid] = R.sa;
};
auto total = r4d_fp8::acc_zero();
auto compute = [&](int s) {
const int k0 = (s % ksn) * StageK;
#pragma unroll
for (int kb = 0; kb < KB; ++kb) {
if (k0 + kb * 128 >= kc) break;
const uint32_t exps = ss[mycol * KB + kb];
#pragma unroll
for (int group = 0; group < 4; ++group) {
auto raw = r4d_fp8::acc_zero();
#pragma unroll
for (int k = kb * 128 + group * 32; k < kb * 128 + (group + 1) * 32; k += 16)
raw = r4d_fp8::mma(r4d_fp8::frag<StageK>(aa, 0, k, lane),
r4d_fp8::frag<StageK>(bb, wave * 16, k, lane), raw);
const uint32_t exp = (exps >> (8 * group)) & 0xffu;
const float scale = __uint_as_float(exp == 255 ? 0x7fc00000u
: exp ? (exp << 23)
: 0x00400000u);
#pragma unroll
for (int row = 0; row < 8; ++row)
if (rv[row]) total[row] += raw[row] * sa[(rowbase + row) * KB + kb] * scale;
}
}
if (s % ksn == ksn - 1) {
const int n = colb + (s / ksn) * BN + mycol;
#pragma unroll
for (int row = 0; row < 8; ++row) {
const int r = r0 + rowbase + row;
if (r < hi && n < m.N) y[(long)r * m.yr + n] = f32_to_bf16(total[row]);
}
total = r4d_fp8::acc_zero();
}
};
St R;
load(0, R);
for (int s = 0; s < S; ++s) {
store(R);
__syncthreads();
if (s + 1 < S) load(s + 1, R);
compute(s);
__syncthreads();
}
}
}
int table(const RadTensor* t, uint32_t dtype, int bits, long experts, long n, long k, long* step,
int* row) {
if (!t || !t->data || t->rank != 3 || t->dtype != dtype || t->shape[0] < experts ||
t->shape[1] != n || t->shape[2] != k || t->stride[2] != 1 || t->stride[1] < k ||
(t->stride[1] * bits) % 8 || t->stride[1] * bits / 8 > INT_MAX)
return RAD_E_SHAPE;
if (t->stride[0] && (t->stride[0] < n * t->stride[1] || (t->stride[0] * bits) % 8))
return RAD_E_STRIDE;
*step = t->stride[0] * bits / 8;
*row = (int)(t->stride[1] * bits / 8);
return RAD_OK;
}
int body(const RadArgs* a, RadStream stream) {
if (!a || a->n_t < 9) return RAD_E_INVAL;
const auto *aq = rad_arg_in(a, 0), *as = rad_arg_in(a, 1), *sorted = rad_arg_in(a, 4),
*off = rad_arg_in(a, 5);
const auto* y = rad_arg_in(a, 8);
if (!aq || !as || !sorted || !off || !y) return RAD_E_INVAL;
MxMeta m{};
const long n0 = r4d_geti_or(a, "N", 0), k0 = r4d_geti_or(a, "K", 0);
const long nn[2] = { n0, r4d_geti_or(a, "N_odd", n0) },
kk[2] = { k0, r4d_geti_or(a, "K_odd", k0) };
const long ne = r4d_geti_or(a, "n_expert", 0), top = r4d_geti_or(a, "top_k", 0),
parts = r4d_geti_or(a, "parts", 1);
if (ne < 2 || ne > 512 || top < 1 || top > ne || parts < 1 || parts > INT_MAX ||
r4d_geti_or(a, "group", 0) != 128)
return RAD_E_SHAPE;
const RadTensor* w[2] = { rad_arg_in(a, 2), rad_arg_in(a, 6) };
const RadTensor* s[2] = { rad_arg_in(a, 3), rad_arg_in(a, 7) };
for (int q = 0; q < 2; ++q) {
if (nn[q] < 1 || nn[q] > INT_MAX - 16 || kk[q] < 128 || kk[q] > INT_MAX || kk[q] % 128 ||
nn[q] % parts)
return RAD_E_SHAPE;
m.n[q] = (int)nn[q];
m.k[q] = (int)kk[q];
int rc =
table(w[q], RAD_FP4E2M1, 4, (ne + 1 - q) / 2, nn[q], kk[q], &m.wstep[q], &m.wrow[q]);
if (rc < 0) return rc;
const uint32_t dt = s[q] ? s[q]->dtype : 0;
if (dt != RAD_U8 && dt != RAD_E8M0) return RAD_E_DTYPE;
rc = table(s[q], dt, 8, (ne + 1 - q) / 2, nn[q], kk[q] / 32, &m.sstep[q], &m.srow[q]);
if (rc < 0) return rc;
}
m.N = (int)std::max(nn[0], nn[1]);
m.K = (int)std::max(kk[0], kk[1]);
if (aq->dtype != RAD_F8E4M3 || as->dtype != RAD_F32 || y->dtype != RAD_BF16 ||
sorted->dtype != RAD_I32 || off->dtype != RAD_I32)
return RAD_E_DTYPE;
if (aq->rank != 2 || aq->shape[0] > INT_MAX || aq->shape[1] != m.K || aq->stride[1] != 1 ||
aq->stride[0] < m.K || as->rank != 2 || as->shape[0] != aq->shape[0] ||
as->shape[1] != m.K / 128 || as->stride[1] != 1 || as->stride[0] < m.K / 128 ||
y->rank != 2 || y->shape[0] < 1 || y->shape[0] > INT_MAX || y->shape[1] != m.N ||
y->stride[1] != 1 || y->stride[0] < m.N || sorted->rank != 1 || sorted->stride[0] != 1 ||
sorted->shape[0] < y->shape[0] || off->rank != 1 || off->stride[0] != 1 ||
off->shape[0] < ne + 1)
return RAD_E_SHAPE;
const char* ord = r4d_gets(a, "a_order");
if (!ord || (std::strcmp(ord, "token") && std::strcmp(ord, "sorted"))) return RAD_E_INVAL;
m.sorted_a = !std::strcmp(ord, "sorted");
m.T = (int)y->shape[0];
m.Arows = (int)aq->shape[0];
if (m.T % top || m.Arows < (m.sorted_a ? m.T : m.T / top)) return RAD_E_SHAPE;
m.experts = (int)ne;
m.topk = (int)top;
m.parts = (int)parts;
m.ar = aq->stride[0];
m.asr = as->stride[0];
m.yr = y->stride[0];
m.tile = m.T < 32 ? 1 : m.T < 128 ? 4 : 16;
// Validated GLM projections at TP4 and TP8: retain the small/decode path and other models.
const bool glm_prefill = ne == 288 && top == 8 && nn[0] == nn[1] && kk[0] == kk[1] &&
((parts == 2 && (nn[0] == 1024 || nn[0] == 512) && kk[0] == 4096) ||
(parts == 1 && nn[0] == 4096 && (kk[0] == 512 || kk[0] == 256)));
auto enabled = [&](const char* name) {
const char* v = std::getenv(name);
return v ? v[0] == '1' : glm_prefill;
};
/* The expert-major form up to a routed-row bound per projection (token rows: gate_up, sorted
* rows: down), measured at GLM's TP8 slices: gate_up up to 768 rows, where the prefill form
* overtakes it, down up to 6143 rows (at 8192 the pipelined prefill form reads the slice
* 2.2 times less often and wins by a quarter). Other shapes keep the decode band. The environment
* moves the bounds, forces either side (RADIANCE_MXFP4_DECODE_EXPERTS 0 or 1) and pins stage
* and tiles, for the microbenchmark. */
const char* lut_env = std::getenv("RADIANCE_MXFP4_LUT");
const bool lut = lut_env ? lut_env[0] == '1' : true;
auto env_long = [](const char* name, long dflt) {
const char* v = std::getenv(name);
return v && *v ? std::strtol(v, nullptr, 10) : dflt;
};
const long max_t =
env_long(m.sorted_a ? "RADIANCE_MXFP4_DECODE_EXPERTS_MAX_T_DOWN"
: "RADIANCE_MXFP4_DECODE_EXPERTS_MAX_T_GATE_UP",
glm_prefill && (nn[0] == 512 || kk[0] == 256) ? (m.sorted_a ? 6143 : 768) : 511);
const char* experts_env = std::getenv("RADIANCE_MXFP4_DECODE_EXPERTS");
/* The P64 form from a routed-row bound per projection, ahead of the expert-major band (its
* down bound overlaps it): gate_up from 769 rows, where the prefill forms begin, down from
* 20 rows an expert at GLM's 288. Default at GLM's TP8 slices only, the measured ones
* (RADIANCE_MXFP4_PREFILL_P64=0 keeps the TP8 prefill form of stage 1, which also takes a grid
* P64 cannot address). The
* tile follows the rows an expert, measured at the TP8 slices (perfmx, cold): down BM32 BN64
* K128 over four column tiles up to 3072 tokens (2048: 647 against 831 us), above BM64 BN128
* K128 x4 with the LDS output (4096: 1101 against 1607); gate_up BM32 BN64 K256 up to 1536
* tokens (768: 563 against 660), BM64 BN64 K256 up to 3072 (2048: 941 against 995), above
* BM64 BN128 K128 with the LDS output (4096: 1715 against 1915). BM, BN, stage, column tiles,
* the LDS output, the double buffer and the three row bounds move with the environment, and a
* forced expert-major form (DECODE_EXPERTS=1) keeps precedence. */
const char* p64_env = std::getenv("RADIANCE_MXFP4_PREFILL_P64");
const bool resident = m.K <= 256;
long dbm = 32, dbn = 64, dsk = 128, dout = 0;
if (resident) {
if (m.T >= env_long("RADIANCE_MXFP4_PREFILL_P64_T_DOWN_WIDE", 24576)) {
dbm = 64;
dbn = 128;
dout = 1;
}
} else if (m.T < env_long("RADIANCE_MXFP4_PREFILL_P64_T_BM64", 12288))
dsk = 256;
else if (m.T < env_long("RADIANCE_MXFP4_PREFILL_P64_T_BN128", 24576)) {
dbm = 64;
dsk = 256;
} else {
dbm = 64;
dbn = 128;
dout = 1;
}
const long bm = env_long("RADIANCE_MXFP4_PREFILL_P64_BM", dbm) == 32 ? 32 : 64;
const bool tp8 = glm_prefill && (nn[0] == 512 || kk[0] == 256);
if ((p64_env ? p64_env[0] == '1' : tp8) && !(experts_env && experts_env[0] == '1') &&
m.T >= env_long(m.sorted_a ? "RADIANCE_MXFP4_PREFILL_P64_MIN_T_DOWN"
: "RADIANCE_MXFP4_PREFILL_P64_MIN_T_GATE_UP",
m.sorted_a ? 5760 : 769) &&
(m.T + bm - 1) / bm + m.experts <= 65535) {
const long bn = env_long("RADIANCE_MXFP4_PREFILL_P64_BN", dbn) == 64 ? 64 : 128;
const long sk = env_long("RADIANCE_MXFP4_PREFILL_P64_STAGE_K", dsk) == 256 ? 256 : 128;
const long ct = resident && env_long("RADIANCE_MXFP4_PREFILL_P64_COLTILES", 4) != 1 ? 4 : 1;
m.stage_out = env_long("RADIANCE_MXFP4_PREFILL_P64_STAGE_OUT", dout) == 1;
// The double buffer exists where two stages fit in 64 KiB of LDS: every 128 stage, and
// the 256 stage at BM32 BN64.
const bool db = !resident && env_long("RADIANCE_MXFP4_PREFILL_P64_DB", 0) == 1 &&
(sk == 128 || (bm == 32 && bn == 64));
m.out16 = (reinterpret_cast<uintptr_t>(y->data) & 15) == 0 && m.yr % 8 == 0;
const long jobs = (m.T + bm - 1) / bm + m.experts;
#define MX_P64(BM, BN, SK, CT, R) \
R4D_EMIT(stream, (moe_mxfp4_p64_kernel<BM, BN, SK, CT, R>), \
dim3(((long)m.N + BN * CT - 1) / (BN * CT), jobs), dim3(256), 0, \
(const uint8_t*)aq->data, (const float*)as->data, w[0]->data, s[0]->data, w[1]->data, \
s[1]->data, (const int*)sorted->data, (const int*)off->data, (uint16_t*)y->data, m)
#define MX_P64D(BM, BN, SK) \
R4D_EMIT(stream, (moe_mxfp4_p64_kernel<BM, BN, SK, 1, false, true>), \
dim3(((long)m.N + BN - 1) / BN, jobs), dim3(256), 0, (const uint8_t*)aq->data, \
(const float*)as->data, w[0]->data, s[0]->data, w[1]->data, s[1]->data, \
(const int*)sorted->data, (const int*)off->data, (uint16_t*)y->data, m)
if (db) {
if (sk == 256) return MX_P64D(32, 64, 256);
if (bm == 32) return bn == 64 ? MX_P64D(32, 64, 128) : MX_P64D(32, 128, 128);
return bn == 64 ? MX_P64D(64, 64, 128) : MX_P64D(64, 128, 128);
}
#undef MX_P64D
#define MX_P64_SK(BM, BN, CT, R) \
return sk == 256 ? MX_P64(BM, BN, 256, CT, R) : MX_P64(BM, BN, 128, CT, R);
#define MX_P64_BN(BM, CT, R) \
if (bn == 64) { MX_P64_SK(BM, 64, CT, R) } \
MX_P64_SK(BM, 128, CT, R)
#define MX_P64_BM(CT, R) \
if (bm == 32) { MX_P64_BN(32, CT, R) } \
MX_P64_BN(64, CT, R)
if (!resident) { MX_P64_BM(1, false) }
if (ct == 1) { MX_P64_BM(1, true) }
MX_P64_BM(4, true)
#undef MX_P64_BM
#undef MX_P64_BN
#undef MX_P64_SK
#undef MX_P64
}
if (experts_env ? experts_env[0] == '1' : max_t < 0 || m.T <= max_t) {
const long stage = env_long("RADIANCE_MXFP4_EXPERTS_STAGE_K", m.K <= 256 ? 128 : 256);
const long tiles = env_long("RADIANCE_MXFP4_EXPERTS_COLTILES", m.K <= 256 ? 4 : 1);
#define MX_EXPERTS(KERNEL, C) \
R4D_EMIT(stream, KERNEL, dim3(((long)m.N + 64 * C - 1) / (64 * C), m.experts + 1), dim3(128), \
0, (const uint8_t*)aq->data, (const float*)as->data, w[0]->data, s[0]->data, \
w[1]->data, s[1]->data, (const int*)sorted->data, (const int*)off->data, \
(uint16_t*)y->data, m)
#define MX_EXPERTS_LUT(L) \
if (stage == 128) \
return tiles == 4 ? MX_EXPERTS((moe_mxfp4_expert_staged_kernel<128, 4, L>), 4) \
: MX_EXPERTS((moe_mxfp4_expert_staged_kernel<128, 1, L>), 1); \
return tiles == 4 ? MX_EXPERTS((moe_mxfp4_expert_staged_kernel<256, 4, L>), 4) \
: MX_EXPERTS((moe_mxfp4_expert_kernel<4, 256, L>), 1);
if (lut) { MX_EXPERTS_LUT(true) }
MX_EXPERTS_LUT(false)
#undef MX_EXPERTS_LUT
#undef MX_EXPERTS
}
m.aligned_jobs = enabled("RADIANCE_MXFP4_ALIGNED_JOBS") && m.T >= 512;
const bool wide = enabled("RADIANCE_MXFP4_PREFILL_N32") && m.T >= 512;
const bool tall = wide && m.aligned_jobs && enabled("RADIANCE_MXFP4_PREFILL_M32");
const int job_rows = tall ? 32 : 16;
const int jobs =
m.aligned_jobs ? (m.T + job_rows - 1) / job_rows + m.experts : (m.T + m.tile - 1) / m.tile;
if (jobs > 65535) return RAD_E_SHAPE;
#define MX_EMIT(CT, RT, CACHE, L) \
R4D_EMIT(stream, (moe_mxfp4_fp8_kernel<CT, RT, CACHE, L>), \
dim3(((long)m.N + 16 * CT - 1) / (16 * CT), jobs), dim3(32 * CT * RT), 0, \
(const uint8_t*)aq->data, (const float*)as->data, w[0]->data, s[0]->data, w[1]->data, \
s[1]->data, (const int*)sorted->data, (const int*)off->data, (uint16_t*)y->data, m)
#define MX_PIPE(L) \
R4D_EMIT(stream, (moe_mxfp4_prefill_kernel<L>), dim3(((long)m.N + 63) / 64, jobs), dim3(256), \
0, (const uint8_t*)aq->data, (const float*)as->data, w[0]->data, s[0]->data, \
w[1]->data, s[1]->data, (const int*)sorted->data, (const int*)off->data, \
(uint16_t*)y->data, m)
const char* cached = std::getenv("RADIANCE_MXFP4_PREFILL_CACHE_ROWS");
const bool pipe = enabled("RADIANCE_MXFP4_PREFILL_PIPE");
#define MX_EMIT_LUT(L) \
if (tall && enabled("RADIANCE_MXFP4_PREFILL_N64")) { \
if (cached ? cached[0] == '1' : glm_prefill) \
return pipe ? MX_PIPE(L) : MX_EMIT(4, 2, true, L); \
return MX_EMIT(4, 2, false, L); \
} \
if (tall) return MX_EMIT(2, 2, false, L); \
if (wide) return MX_EMIT(2, 1, false, L); \
return MX_EMIT(1, 1, false, L);
if (lut) { MX_EMIT_LUT(true) }
MX_EMIT_LUT(false)
#undef MX_EMIT_LUT
#undef MX_PIPE
#undef MX_EMIT
}
}
extern "C" int r4d_rad_moe_mxfp4(const RadArgs* a, RadStream stream) { return body(a, stream); }