libr4d: moe_gemm_mxfp4a8's small-M form at GLM's TP8 slices -- a one-stream verify pass is a chain of short expert launches

At 1 to 48 tokens a chunk (8 to 384 routed rows) the expert-major forms spent their time around
the loads rather than on them. Taking the loads out left gate_up at 72 of its 115 us at six
tokens; what remained was the skeleton of each block: 289 grid rows for the few touched experts,
activation scales read row by row from LDS behind a divergent branch, weight rows loaded through
generic pointers (flat loads, which every LDS wait also waits for), loads behind branches so each
store waited for the whole prefetch, and __syncthreads' workgroup fence. With 160 blocks resident
at 21.8 KiB of LDS each, the last round of an expert-major launch ran nearly empty, at the latency
of one block's chain.

moe_mxfp4_small_kernel keeps the expert-major per-element sequence, and so its bytes:

- block row y takes the y-th touched expert, counted by ballot over the offsets, so the grid has
  min(experts, rows) + 1 rows;
- weight codes stay packed in LDS and are decoded at the fragment read, halving the weight tile;
  gate_up stages 512 of K, reading 256 bytes of each weight row at once, down its 128-byte rows
  whole over four column tiles;
- loads are unconditional from clamped addresses and masked at the store, weight rows load
  through global pointers, the barriers order LDS only, and a 128 of K reads its activation scales
  and eight fragment pairs before its WMMA chains.

The rule takes it at GLM's TP8 slices for gate_up to 384 routed rows and down to 32; from five
tokens down reads 3 to 5 % slower than the staged form and keeps it. Other shapes are unchanged.
A deeper register prefetch, a barrier-free form with B straight from global memory and A tiles
sized to the chunk's tokens measured no faster.

Measured at the TP8 slices (r4d_selftest --cold 256 --perfmx), expert-major against small-M:

  tokens          1      3      6      8     16     48
  gate_up, us  36.8   79.5  120.5  145.0  230.3  416.7
               31.4   69.5  101.4  122.4  206.8  390.0

  tokens          1      2      3      4
  down, us     21.2   26.0   34.3   39.5
               16.4   22.5   33.2   37.9

Byte for byte against the expert-major forms over 1 to 48 tokens at every form tried (perfmx with
the old form kept beside it during the work), and in r4d_emu against the host restatement: three
GLM TP8 cases, with runs longer than one tile, dropped slots and E8M0 codes 0 and 255.
r4d_selftest checks the new band against libref at 1 to 48 tokens, and --perfmx names the form.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
NunzeandClaude Opus 5.5 committed 2026-10-21 15:26:57 +02:00
1 parent 332b5fa986
commit 961a9b3f13
3 files changed
+293 -6

No files matched your search

+13 -5
View File
@@ -15007,8 +15007,8 @@ static void mx_experts_check(const Plugin& pl, int M, int parts, bool pooled, co
*
* The form is the launcher's rule, mirrored here only to name it and count the expert slices it
* streams: a (job, expert) pair reads the whole slice across the column blocks, so row groups
* re-read an expert once per group its run spans, the expert-major decode form once per 16 of its
* rows, and the P64 and GLM prefill forms once per job. */
* re-read an expert once per group its run spans, the expert-major and small-M forms once per 16
* of its rows, and the P64 and GLM prefill forms once per job. */
static void mx_perf(const Plugin& pl, int64_t Mtok, int iters, int64_t E, int64_t top_k,
bool f8 = false, const char* skew = nullptr) {
if (iters <= 0) iters = 100;
@@ -15235,11 +15235,12 @@ static void mx_perf(const Plugin& pl, int64_t Mtok, int iters, int64_t E, int64_
const int64_t pbm = pwide || (K > 256 && Tr >= 12288) ? 64 : 32;
const bool p64 =
glm && Tr >= (P.srt ? 5760 : 769) && (Tr + pbm - 1) / pbm + E <= 65535;
const bool expert = !p64 && (f8 || Tr <= (glm ? (P.srt ? 6143 : 768) : 511));
const bool pipe = !p64 && !expert && glm;
const bool small = glm && Tr <= (P.srt ? 32 : 384);
const bool expert = !p64 && !small && (f8 || Tr <= (glm ? (P.srt ? 6143 : 768) : 511));
const bool pipe = !p64 && !small && !expert && glm;
const int64_t jr = p64 ? pbm : pipe ? 32 : 16;
int64_t streams = 0;
if (p64 || pipe || expert)
if (p64 || pipe || expert || small)
for (int64_t e = 0; e < E; ++e)
streams += (off[(size_t)(e + 1)] - off[(size_t)e] + jr - 1) / jr;
else
@@ -15249,6 +15250,7 @@ static void mx_perf(const Plugin& pl, int64_t Mtok, int iters, int64_t E, int64_
p64 ? fmt("P64 BM%lld BN%d K%d", (long long)pbm, pwide ? 128 : 64,
K > 256 && !pwide ? 256 : 128) +
(K <= 256 ? " x4, A resident" : "") + (pwide ? ", LDS out" : "")
: small ? fmt("small-M K%d x%d tiles", K <= 256 ? 256 : 512, K <= 256 ? 4 : 1)
: expert ? fmt("expert-major K%d x%d tiles", K <= 256 ? 128 : 256,
K <= 256 || (f8 && Tr < 128) ? 4 : 1)
: pipe ? std::string("GLM prefill form, jobs of 32 rows")
@@ -26495,6 +26497,12 @@ static void device_phase(const Plugin& pl) {
mx_experts_check(pl, m, shape % 2 ? 2 : 1, false, shape == 2 ? "sorted" : "token",
shape, true, 288, 8);
}
// At GLM's TP8 slices the small-M form for gate_up to 384 routed rows and down to 32,
// the expert-major form for down above, with dropped slots.
for (int m : { 1, 3, 4, 6, 8, 12, 48 }) {
mx_experts_check(pl, m, 2, m % 2 == 0, "token", 3, false, 288, 8, true);
mx_experts_check(pl, m, 1, m % 2 != 0, "sorted", 4, m == 3, 288, 8, true);
}
// At GLM's TP8 slices the expert-major form to its bound (gate_up 768 rows, down below
// P64's 5760), then P64 by its rule: gate_up BM32 BN64 K256 from 769 rows, BM64 BN64 K256
// from 12288 and BM64 BN128 K128 with the LDS output from 24576; down BM32 BN64 K128 x4