From 7e363dbebbbe145901c73df8e4a5a3df6581d886 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Sat, 3 Oct 2026 00:33:19 +0000 Subject: [PATCH] prefill: the gfx11 fused prompt-expert grids at 2 blocks per WGP HIP counts a gfx11 WGP (two CUs, which the WMMA prompt-expert kernels use as one in the default WGP mode) as one multiprocessor, and the occupancy query answers 1 block for these kernels where 2 run side by side - so every fused launch (native IQ and Q2_0) ran a 20-block grid on gfx1151, 2 waves per SIMD. wgp_blocks() doubles the query's answer (STRATA_PF_OCC=N sets it, an experiment knob). The same work items and arithmetic: parity passes, fused-vs-MMQ rel RMS unchanged, and the engine's outputs are the same tokens. gfx1151, one layer, 8192 tokens x top 10, IQ3_S / IQ4_NL: blocks per WGP 1: 60.7 ms, 2: 44.7, 3: 49.9, 4: 45.2. Q2_0 (2048 tokens): 13.3 -> 11.4 ms. Engine, UD-IQ4_XS (MTP + vision + speed projection): 64K prompt 819.8 / 823.9 -> 892.4 / 878.8 tok/s, 8K 825.5 / 820.8 -> 874.5 (the other 8K run overlapped a download: 758); outputs identical (drafts 292/516 at 64K, 343/468 at 8K); ledger {32,17,130}, 8K retrieval 3/3. --- src/prefill/moe_fused.cu | 18 ++++++++++++++++-- src/prefill/moe_fused_iq.cu | 18 ++++++++++++++++-- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/prefill/moe_fused.cu b/src/prefill/moe_fused.cu index 6b9a68cb..0e21cd3d 100644 --- a/src/prefill/moe_fused.cu +++ b/src/prefill/moe_fused.cu @@ -577,6 +577,20 @@ struct DevInfo { std::mutex g_mu; DevInfo g_dev[32]; +#if defined(__HIPCC__) +// Resident blocks per multiprocessor for the gfx11 WMMA prompt-expert kernels' persistent grids. HIP counts a gfx11 +// WGP (two CUs, which these kernels use as one in the default WGP mode) as one multiprocessor, and the occupancy query +// answers 1 block for these kernels where 2 run side by side - so the grid was half the GPU's room. gfx1151 (20 +// WGPs), 8192 tokens x top 10, IQ3_S / IQ4_NL, one layer: 1 block per WGP 60.7 ms, 2 44.7, 3 49.9, 4 45.2 (the same +// work items and arithmetic: the results do not change). STRATA_PF_OCC=N sets the blocks per WGP (an experiment knob). +static int wgp_blocks(int occ) { + static const int env = [] { + const char* v = std::getenv("STRATA_PF_OCC"); + return v != nullptr ? std::atoi(v) : 0; + }(); + return env > 0 ? env : 2 * std::max(occ, 1); +} +#endif const DevInfo& dev_info() { int dev = 0; cudaGetDevice(&dev); @@ -692,8 +706,8 @@ void experts(const Batch& b, int n_expert, int64_t n, const void* scratch, const // the most tiles the batch can have: every row in it, plus a partial tile per expert const int64_t tiles = (n + kTileRows - 1) / kTileRows + (b.e1 - b.e0); #if defined(__HIPCC__) - const unsigned g_gu = (unsigned) std::min(tiles * (1280 / W_ROWS), (int64_t) d.sms * d.occ_gu); - const unsigned g_d = (unsigned) std::min(tiles * (2560 / W_ROWS), (int64_t) d.sms * d.occ_d); + const unsigned g_gu = (unsigned) std::min(tiles * (1280 / W_ROWS), (int64_t) d.sms * wgp_blocks(d.occ_gu)); + const unsigned g_d = (unsigned) std::min(tiles * (2560 / W_ROWS), (int64_t) d.sms * wgp_blocks(d.occ_d)); expert_w11_kernel<<>>(b, tb, (const uint8_t*) xa, src, (uint8_t*) ha, nullptr); expert_w11_kernel<<>>(b, tb, (const uint8_t*) ha, src, nullptr, dm); #else diff --git a/src/prefill/moe_fused_iq.cu b/src/prefill/moe_fused_iq.cu index 14b02ec8..72c278d7 100644 --- a/src/prefill/moe_fused_iq.cu +++ b/src/prefill/moe_fused_iq.cu @@ -761,6 +761,20 @@ template bool setup_ww(int& occ) { template bool setup_one(int& occ) { return setup_ww(occ) && setup_ww(occ); } #endif +#if defined(__HIPCC__) +// Resident blocks per multiprocessor for the gfx11 WMMA prompt-expert kernels' persistent grids. HIP counts a gfx11 +// WGP (two CUs, which these kernels use as one in the default WGP mode) as one multiprocessor, and the occupancy query +// answers 1 block for these kernels where 2 run side by side - so the grid was half the GPU's room. gfx1151 (20 +// WGPs), 8192 tokens x top 10, IQ3_S / IQ4_NL, one layer: 1 block per WGP 60.7 ms, 2 44.7, 3 49.9, 4 45.2 (the same +// work items and arithmetic: the results do not change). STRATA_PF_OCC=N sets the blocks per WGP (an experiment knob). +static int wgp_blocks(int occ) { + static const int env = [] { + const char* v = std::getenv("STRATA_PF_OCC"); + return v != nullptr ? std::atoi(v) : 0; + }(); + return env > 0 ? env : 2 * std::max(occ, 1); +} +#endif const DevInfo& dev_info() { int dev = 0; cudaGetDevice(&dev); @@ -861,8 +875,8 @@ void experts_native(const Batch& b, const NativeGeom& g, int n_expert, int64_t n #if defined(__HIPCC__) { const int64_t tiles = (n + kTileRows - 1) / kTileRows + (b.e1 - b.e0); - const unsigned g_gu = (unsigned) std::min(tiles * (1280 / NW_ROWS), (int64_t) d.sms * d.occ); - const unsigned g_d = (unsigned) std::min(tiles * (2560 / NW_ROWS), (int64_t) d.sms * d.occ); + const unsigned g_gu = (unsigned) std::min(tiles * (1280 / NW_ROWS), (int64_t) d.sms * wgp_blocks(d.occ)); + const unsigned g_d = (unsigned) std::min(tiles * (2560 / NW_ROWS), (int64_t) d.sms * wgp_blocks(d.occ)); const uint8_t* xa8 = (const uint8_t*) xa; uint8_t* ha8 = (uint8_t*) ha; #define STRATA_NW_GU(T) native_w11_kernel<<>>(b, g, tb, xa8, src, ha8, nullptr)