diff --git a/libr4d/r4d_moe_mxfp4.hip b/libr4d/r4d_moe_mxfp4.hip index 4900e3a2..9bba888f 100644 --- a/libr4d/r4d_moe_mxfp4.hip +++ b/libr4d/r4d_moe_mxfp4.hip @@ -1161,14 +1161,19 @@ int body(const RadArgs* a, RadStream stream) { return bm == 64 ? MX_P64(64, 64, 256, 1, false) : MX_P64(32, 64, 256, 1, false); #undef MX_P64 } - /* The small-M form at GLM's TP8 slices, ahead of the expert-major band: gate_up up to 384 - * routed rows, down up to 32, where it stops gaining (at 5 to 8 tokens it reads 3 to 5 % - * slower than the staged form). At those slices on gfx1201 (perfmx, cold) it is 6 to 16 % - * faster than the expert-major form for gate_up from 1 to 48 tokens, and 4 to 23 % for down - * from 1 to 4. */ + /* The small-M form at GLM's slices, ahead of the expert-major band (perfmx, cold, its output + * byte-identical to the expert-major form's). At the TP8 slices: gate_up up to 384 routed rows, + * down up to 32, where it stops gaining (at 5 to 8 tokens it reads 3 to 5 % slower than the + * staged form); on gfx1201 it is 6 to 16 % faster than the expert-major form for gate_up from + * 1 to 48 tokens, and 4 to 23 % for down from 1 to 4. At the TP4 slices (--perfmx-tp 4): + * gate_up from 2 to 48 tokens, 3 to 18 % faster (at one token it reads 5 % slower), and down + * up to 48 tokens at the K256 x4 tiles, 37 % faster at one token and 4 % at 48. */ + const bool tp8_small = tp8 && m.T <= (m.sorted_a ? 32 : 384); + const bool tp4 = glm_prefill && !tp8; + const bool tp4_small = tp4 && m.T <= 384 && (m.sorted_a || m.T > m.topk); if (g_mx_small_pin == 1 || g_mx_small_pin == 3 ? glm_prefill && m.T <= 384 - : g_mx_small_pin != 2 && tp8 && m.T <= (m.K <= 256 ? 32 : 384)) { + : g_mx_small_pin != 2 && (tp8_small || tp4_small)) { const long rows = std::min(m.experts, m.T) + 1; #define MX_SMALL(SK, CT) \ R4D_EMIT(stream, (moe_mxfp4_small_kernel), \ @@ -1176,7 +1181,9 @@ int body(const RadArgs* a, RadStream stream) { (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) - return m.K <= 256 || g_mx_small_pin == 3 ? MX_SMALL(256, 4) : MX_SMALL(512, 1); + return m.K <= 256 || g_mx_small_pin == 3 || (g_mx_small_pin == 0 && tp4 && m.sorted_a) + ? MX_SMALL(256, 4) + : MX_SMALL(512, 1); #undef MX_SMALL } /* The expert-major form up to a routed-row bound per projection (token rows: gate_up, sorted diff --git a/libr4d/r4d_selftest.cpp b/libr4d/r4d_selftest.cpp index 60fe1349..65f9b809 100644 --- a/libr4d/r4d_selftest.cpp +++ b/libr4d/r4d_selftest.cpp @@ -20125,8 +20125,10 @@ 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 = t8 && Tr >= (P.srt ? 5760 : 769) && (Tr + pbm - 1) / pbm + E <= 65535; + const bool t4_small = glm && !t8 && Tr <= 384 && (P.srt || Tr > top_k); const bool small = - (form == 1 || form == 3 ? Tr <= 384 : form != 2 && t8 && small_fits) || + (form == 1 || form == 3 ? Tr <= 384 + : form != 2 && ((t8 && small_fits) || t4_small)) || (f8 && E == 288 && top_k == 8 && !P.srt && Tr <= 384); const bool expert = !p64 && !small && (f8 || Tr <= (t8 ? (P.srt ? 6143 : 768) : 511)); const bool pipe = !p64 && !small && !expert && glm; @@ -20138,7 +20140,7 @@ static void mx_perf(const Plugin& pl, int64_t Mtok, int iters, int64_t E, int64_ else for (int64_t i = 0; i < Tr; ++i) streams += i % 16 == 0 || owner[(size_t)i] != owner[(size_t)(i - 1)]; - const bool tile4 = K <= 256 || form == 3; + const bool tile4 = K <= 256 || form == 3 || (form == 0 && !t8 && P.srt); const std::string label = p64 ? fmt("P64 BM%lld BN%d K%d", (long long)pbm, pwide ? 128 : 64, K > 256 && !pwide ? 256 : 128) +