From 73691a1189ae64f9a748e2dd6bf6d83c0ce03c06 Mon Sep 17 00:00:00 2001 From: keptn42 Date: Mon, 5 Oct 2026 21:49:10 +0000 Subject: [PATCH] r4d_selftest: the R4D_MOE_PF2 aligned-form pass at every class's own geometry, fp8a8 included moe_q4_gemm holds the run-aligned form under parts 16 and 64 to the form without them as well, so the block-fp8 instance of the early weight issue and the narrow classes' pad tiles are compared byte for byte too. Past 512 experts the launcher builds no run map and there is no aligned launch, so the pass is skipped there rather than failed. --- libr4d/r4d_selftest.cpp | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/libr4d/r4d_selftest.cpp b/libr4d/r4d_selftest.cpp index 862cc90c..f35c144b 100644 --- a/libr4d/r4d_selftest.cpp +++ b/libr4d/r4d_selftest.cpp @@ -6772,12 +6772,21 @@ static void moe_dec_variants(const Plugin& pl, const RadKernelInfo* row, RadArgs * Described first where the row can be: the map's kernel must differ from the default's exactly * when 16 is on, and the GEMM's exactly when 64 is or the wide tile applies, so the bytes below are * never one kernel against itself; and the launch must report the parts it took. `base` is the - * form's own bytes, already held to the host decode by the form loop. */ + * form's own bytes, already held to the host decode by the form loop. + * + * PAST 512 EXPERTS THERE IS NO ALIGNED LAUNCH: the map's builder holds kMaxExpert + 1 offsets, so + * the launcher hands a wider expert count the uniform grid whatever scratch it is given, and none + * of these parts has a launch to take. Such a shape is skipped, not failed. */ static void moe_pf2_aligned(const Plugin& pl, const RadKernelInfo* row, RadArgs& a, void* d_y, void* d_map, int64_t map_bytes, size_t elems, - const std::vector& base, bool dn_wide, + const std::vector& base, bool dn_wide, int64_t ne, const std::string& what) { if (!pl.pin_pf2 || !pl.pf2_used || !pl.pin_moe || base.empty()) return; + if (ne > 512) { + skip((what + " aligned prefill form, R4D_MOE_PF2").c_str(), + "no run map past 512 experts, so no aligned launch"); + return; + } void* const sc0 = a.scratch; const int64_t sb0 = a.scratch_bytes; struct Var { int pin; uint32_t mask; const char* name; }; @@ -7105,7 +7114,8 @@ static void moe_w4_gemm(const Plugin& pl, int64_t Tr, const MoeW4Shape& sh, int /* The aligned form under R4D_MOE_PF2: the parallel map prefix always, the 256-column down * tile where the sorted operand's every class is whole tiles of it. */ const bool dn_wide = sh.sorted_a && sh.parts == 1 && sh.N0 % 256 == 0 && sh.N1 % 256 == 0; - moe_pf2_aligned(pl, r, a, d_y, d_map, map_bytes, (size_t)(Tr * N), two_cls[1], dn_wide, what); + moe_pf2_aligned(pl, r, a, d_y, d_map, map_bytes, (size_t)(Tr * N), two_cls[1], dn_wide, ne, + what); /* The decode form's staged K both ways, where this shape can take two blocks a pass -- * never on the sorted operand, which the rule keeps at one. */ const bool fk2 = !sh.sorted_a && g64 && sh.K0 % 256 == 0 && sh.K1 % 256 == 0; @@ -7173,7 +7183,7 @@ static void moe_w4_gemm(const Plugin& pl, int64_t Tr, const MoeW4Shape& sh, int ok(label.c_str(), diff == 0, std::to_string(diff) + " of " + std::to_string(Tr * N) + " elements differ from the two-class op's"); } - moe_pf2_aligned(pl, r4, a4, d_y, d_map, map_bytes, (size_t)(Tr * N), two_cls[1], dn_wide, + moe_pf2_aligned(pl, r4, a4, d_y, d_map, map_bytes, (size_t)(Tr * N), two_cls[1], dn_wide, ne, what4); moe_dec_variants(pl, r4, a4, d_y, (size_t)(Tr * N), fk2, what4); } @@ -7400,6 +7410,7 @@ static void moe_q4_gemm(const Plugin& pl, int64_t Tr, const MoeQ4Shape& sh, cons } const int f_lo = pl.pin_moe ? 1 : 0, f_hi = pl.pin_moe ? 3 : 0; + std::vector aligned; /* the aligned form's bytes, for the R4D_MOE_PF2 pass */ for (int form = f_lo; form <= f_hi; ++form) { const std::string label = what + " " + kForm[form]; if (pl.pin_moe) pl.pin_moe(form); @@ -7436,7 +7447,11 @@ static void moe_q4_gemm(const Plugin& pl, int64_t Tr, const MoeQ4Shape& sh, cons } ok(label.c_str(), bad == 0, std::to_string(bad) + " of " + std::to_string(Tr * N) + " elements off, worst " + std::to_string(worst) + first); + if (form == 1) aligned = got; } + /* The aligned form under R4D_MOE_PF2, each class at its own geometry; fp8a8 runs W4 0 here, + * the token operand, so no 256-column down tile. */ + moe_pf2_aligned(pl, r, a, d_y, d_map, map_bytes, (size_t)(Tr * N), aligned, false, ne, what); /* The decode form's variants, each class at its own geometry; fp8a8 runs W4 0 here. */ bool fk2 = g64; for (int q = 0; q < 4; ++q) fk2 = fk2 && sh.K[q] % 256 == 0;