From 32fa1a27eca88210f9070fb9b925e9f251de2c1d Mon Sep 17 00:00:00 2001 From: Malic Zilyas Date: Fri, 9 Oct 2026 12:30:15 +0200 Subject: [PATCH] libr4d: 5-/4-bit decode GEMM covers all row tiles in one block cfg_for() pins MB = 1 across the decode band (M <= 64): right for the bf16 and MXFP4 skinny kernels, where the grid, not the weight, is the cost. The w5a16 / w4a16_asym kernel is dequantise-bound, and one block per 16-row tile unpacks every weight fragment once per tile, so two or three concurrent streams (M = 16..48) paid the unpack two or three times: 104 us/call at one stream, 190 us at three (profile-ops, PARO int5, 1.3.0). Cover every row tile in one block (MB = ceil(M / 16), at most 4) unless a tuned MB is pinned. The split-K bound is unchanged, so every output element is still one wave's accumulation over the same K slices: output bits are identical. PARO int5, prod shape (175k / 3 seqs), BetterBench c3 aggregate t/s at 1 / 2 / 3 streams: 96 / 156 / 155 -> 92 / 169 / 170 (+9% / +10%); same-source A/B on the dev build: c3 127 -> 166. Single-stream decode is unchanged (that GEMM streams the weights at the memory roofline, ~620 GB/s). Quality unchanged: ppl wiki 6.797 / code 1.533, GSM8K 100%, genchat 17/17 cold-verified, dia-mini v2 23/27, v3 15/15. r4d_selftest gemm_w5a16_nt_m64: pass (incl. M = 40 with 3 parts, M = 64). --- libr4d/r4d_gemm_w5a16_nt_m64.hip | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/libr4d/r4d_gemm_w5a16_nt_m64.hip b/libr4d/r4d_gemm_w5a16_nt_m64.hip index 3a037dab..4309db4c 100644 --- a/libr4d/r4d_gemm_w5a16_nt_m64.hip +++ b/libr4d/r4d_gemm_w5a16_nt_m64.hip @@ -315,6 +315,13 @@ static int w5_dispatch(const RadArgs* a, RadStream s, bool hi5) { Cfg c; rc = cfg_for(a, g.K, mc, group, BR_FLAT, &c); if (rc != RAD_OK) return rc; + /* cfg_for pins MB = 1 across the decode band (the grid, not the weight, is the cost for the bf16 and + * mxfp4 skinny kernels). Here the dequantise is: one block per row tile unpacks each weight fragment + * once PER TILE, so three streams (M = 24, two tiles) pay it twice. Cover every row tile in one block + * unless the tuner pinned MB; same split, so the output bits are unchanged. */ + R4dGemmCfg tuned; + r4d_cfg_gemm(a, &tuned); + if (tuned.mb <= 0) c.mb = std::min(4, (mc + 15) / 16); const long long Ntot = g.N, K = g.K, lda = mp.lda ? mp.lda : g.K; const long long nsz = K / R4D_GEMM_W4_GROUP; for (int p = 0; p < mp.n; ++p) {