From 9f9045159936f6b8fe92f97da7d0f56ed3bdc5ff Mon Sep 17 00:00:00 2001 From: Constantin Date: Sun, 25 Oct 2026 07:07:02 -0400 Subject: [PATCH] Prune padded-row work from single-row asymmetric W4/W5 GEMMs Keep WMMA, group and split arithmetic unchanged, but stage and reduce only the live row. Add byte parity, guard and canonical-arithmetic cases over legal launch geometries. --- libr4d/r4d_gemm_w5a16_nt_m64.hip | 50 +++++++++++++++++++++++++--- libr4d/r4d_selftest.cpp | 57 ++++++++++++++++++++++++++++++-- 2 files changed, 99 insertions(+), 8 deletions(-) diff --git a/libr4d/r4d_gemm_w5a16_nt_m64.hip b/libr4d/r4d_gemm_w5a16_nt_m64.hip index a7819625..814c44ce 100644 --- a/libr4d/r4d_gemm_w5a16_nt_m64.hip +++ b/libr4d/r4d_gemm_w5a16_nt_m64.hip @@ -83,7 +83,7 @@ __device__ __forceinline__ v8f mmah(v8h a, v8h b, v8f c) { // MT = row tiles per block, as in the bf16 kernel. HI false is the 4-bit asymmetric grid (z-lab's // plain int4), with no fifth-bit plane. -template +template __global__ __launch_bounds__(1024) void r4d_gemm_w5a16_nt_m64_kernel( const unsigned short* __restrict__ A, const unsigned* __restrict__ Wq, const unsigned* __restrict__ Whi, const unsigned* __restrict__ Wsz, @@ -181,16 +181,46 @@ __global__ __launch_bounds__(1024) void r4d_gemm_w5a16_nt_m64_kernel( #pragma unroll for (int i = 0; i < MT; i++) #pragma unroll - for (int e = 0; e < 8; e++) acc[j][i][e] = fmaf(g_acc[j][i][e], sc[j], acc[j][i][e]); + for (int e = 0; e < (ONE ? 1 : 8); e++) + acc[j][i][e] = fmaf(g_acc[j][i][e], sc[j], acc[j][i][e]); #pragma unroll for (int i = 0; i < MT; i++) pa[i] += (R4D_GEMM_W4_GROUP / 16) * 4; } } - // Split-K reduction and store as in r4d_gemm_bf16_nt_m64: one row tile at a time, fixed split - // order. const int ncols = WV * NPW; const int nthreads = WV * SK * WAVE32; + if constexpr (ONE) { + // M=1: only element 0 of lanes 0..15 belongs to the live row. Keep the same WMMA, + // per-group FMA and split order, but do not scale/reduce the other fifteen rows. + static_assert(MT == 1, "the one-row epilogue needs one row tile"); + if (SK == 1) { + if (lane < 16) { +#pragma unroll + for (int j = 0; j < NPW; j++) { + const int n = (nt0 + j) * 16 + lane; + if (n < N) C[n] = (__hip_bfloat16)(0.f + acc[j][0][0]); + } + } + return; + } + if (lane < 16) { +#pragma unroll + for (int j = 0; j < NPW; j++) + red[((col_local * NPW + j) * SK + ksplit) * 16 + lane] = acc[j][0][0]; + } + __syncthreads(); + for (int idx = tid; idx < ncols * 16; idx += nthreads) { + const int col = idx / 16, n = (blockIdx.x * ncols + col) * 16 + idx % 16; + float v = 0.f; + for (int s = 0; s < SK; s++) v += red[(col * SK + s) * 16 + idx % 16]; + if (n < N) C[n] = (__hip_bfloat16)v; + } + return; + } + + // Split-K reduction and store as in r4d_gemm_bf16_nt_m64: one row tile at a time, fixed split + // order. #pragma unroll for (int i = 0; i < MT; i++) { __syncthreads(); @@ -229,8 +259,12 @@ void r4d_gemm_w5a16_nt_m64(long a, long wq, long whi, long wsz, long c, int M, i const int ntiles = N / 16, mtiles = (M + 15) / 16; const int ncols = WV * NPW; dim3 grid((ntiles + ncols - 1) / ncols, (mtiles + MB - 1) / MB), block(WV * SK * WAVE32); + // Keep the same accepted tuning space as the multi-row form. The single-row specialization + // uses 1/16 of its reduction storage (none without a split). size_t smem = (size_t)ncols * SK * 256 * sizeof(float); if (smem > 64 * 1024) throw std::runtime_error("r4d gemm_w5a16_nt_m64: WV*NPW*SK too large"); + const bool one = M == 1 && MB == 1; + if (one) smem = SK == 1 ? 0 : smem / 16; auto Ap = (const unsigned short*)a; auto Wp = (const unsigned*)wq; auto Hp = (const unsigned*)whi; @@ -242,8 +276,13 @@ void r4d_gemm_w5a16_nt_m64(long a, long wq, long whi, long wsz, long c, int M, i st, Ap, Wp, Hp, Sp, Cp, M, K, N, WV, SK, lda, ldc); \ else hipLaunchKernelGGL((r4d_gemm_w5a16_nt_m64_kernel), grid, block, smem, \ st, Ap, Wp, Hp, Sp, Cp, M, K, N, WV, SK, lda, ldc); } while (0) +#define LAUNCH_ONE(NP, TT) do { \ + if (hi5) hipLaunchKernelGGL((r4d_gemm_w5a16_nt_m64_kernel<1, NP, TT, true, true>), grid, block, smem, \ + st, Ap, Wp, Hp, Sp, Cp, M, K, N, WV, SK, lda, ldc); \ + else hipLaunchKernelGGL((r4d_gemm_w5a16_nt_m64_kernel<1, NP, TT, false, true>), grid, block, smem, \ + st, Ap, Wp, Hp, Sp, Cp, M, K, N, WV, SK, lda, ldc); } while (0) #define LAUNCH_MB(NP, TT) switch (MB) { \ - case 1: LAUNCH(1, NP, TT); break; \ + case 1: if (one) { LAUNCH_ONE(NP, TT); } else { LAUNCH(1, NP, TT); } break; \ case 2: LAUNCH(2, NP, TT); break; \ case 3: LAUNCH(3, NP, TT); break; \ case 4: LAUNCH(4, NP, TT); break; \ @@ -254,6 +293,7 @@ void r4d_gemm_w5a16_nt_m64(long a, long wq, long whi, long wsz, long c, int M, i if (NT) { LAUNCH_MB(4, true); } else { LAUNCH_MB(4, false); } } #undef LAUNCH_MB +#undef LAUNCH_ONE #undef LAUNCH } diff --git a/libr4d/r4d_selftest.cpp b/libr4d/r4d_selftest.cpp index ab9e8fc7..e66a1968 100644 --- a/libr4d/r4d_selftest.cpp +++ b/libr4d/r4d_selftest.cpp @@ -5192,7 +5192,8 @@ static void givens_numeric(const Plugin& pl, int64_t M, int64_t K, int parts, in * dtype `zdt`), an f16 activation, the weight stored through the row's hooks. `pb1`/`pb2` give a * partitioned weight. M past 64 runs the dispatcher's 64-row chunks. */ static void w5a16_numeric(const Plugin& pl, int64_t M, int64_t N, int64_t K, int64_t pb1 = 0, - int64_t pb2 = 0, int bits = 5, uint32_t zdt = RAD_U8) { + int64_t pb2 = 0, int bits = 5, uint32_t zdt = RAD_U8, + bool one_row_exact = false) { const char* row = bits == 5 ? "gemm_w5a16_nt_m64" : "gemm_w4a16_asym_nt_m64"; const char* dtype = bits == 5 ? "w5a16" : "w4a16_asym"; if (!sel(row)) return; @@ -5229,7 +5230,7 @@ static void w5a16_numeric(const Plugin& pl, int64_t M, int64_t N, int64_t K, int void* d_a = dalloc(ha.size() * 2); void* d_w = dalloc(wq.size()); void* d_s = dalloc(ws.size()); - void* d_y = dalloc(hy.size() * 2); + void* d_y = dalloc((hy.size() + 16) * 2); if (!d_a || !d_w || !d_s || !d_y) { ok(nm, false, "hipMalloc failed"); return; } st_memcpy(d_a, ha.data(), ha.size() * 2, hipMemcpyHostToDevice); st_memcpy(d_w, wq.data(), wq.size(), hipMemcpyHostToDevice); @@ -5252,7 +5253,7 @@ static void w5a16_numeric(const Plugin& pl, int64_t M, int64_t N, int64_t K, int const int rc = r->launch(&a, (RadStream) nullptr); if (rc != RAD_OK || st_stream_sync(nullptr) != hipSuccess) { ok(nm, false, "rc=" + std::to_string(rc)); return; } st_memcpy(hy.data(), d_y, hy.size() * 2, hipMemcpyDeviceToHost); - const int64_t nstep = N > 96 ? N / 96 : 1; + const int64_t nstep = !one_row_exact && N > 96 ? N / 96 : 1; double num = 0, den = 0; for (int64_t i = 0; i < M; ++i) for (int64_t n = 0; n < N; n = (n + 1 == N) ? N : std::min(n + nstep, N - 1)) { @@ -5273,6 +5274,48 @@ static void w5a16_numeric(const Plugin& pl, int64_t M, int64_t N, int64_t K, int char d[64]; std::snprintf(d, sizeof d, "rel_l2 %.3e", rel); ok(nm, rel < 1e-2, d); + if (!one_row_exact) return; + if (M != 1) { ok(nm, false, "one-row comparison needs M=1"); return; } + + // MB=1 selects the compact one-row epilogue; MB=2 retains the original 16-row reduction. + // Compare every byte at each legal point, not only the default split, including an end guard. + std::vector compact(hy.size() + 16), full(compact.size()); + int checked = 0; + for (int wv : { 1, 2, 4 }) + for (int sk : { 1, 2, 4, 8, 16 }) + for (int npw : { 1, 4 }) + for (int nt : { 0, 1 }) { + if (K % (sk * 128) || wv * sk > 32 || wv * npw * sk > 64) continue; + std::vector cp = ps; + const size_t mb = cp.size() + 2; + cp.insert(cp.end(), { PI("wv", wv), PI("sk", sk), PI("mb", 1), + PI("npw", npw), PI("nt", nt) }); + a.p = cp.data(); a.n_p = (int)cp.size(); + bool ran = true; + for (int pass = 0; pass < 2; ++pass) { + cp[mb] = PI("mb", pass + 1); + auto& out = pass ? full : compact; + if (st_memset(d_y, 0xa5, out.size() * 2) != hipSuccess || + r->launch(&a, nullptr) != RAD_OK || st_stream_sync(nullptr) != hipSuccess || + st_memcpy(out.data(), d_y, out.size() * 2, hipMemcpyDeviceToHost) != hipSuccess) { + ran = false; + break; + } + } + const bool guard = std::all_of(compact.end() - 16, compact.end(), + [](uint16_t v) { return v == 0xa5a5; }) && + std::all_of(full.end() - 16, full.end(), + [](uint16_t v) { return v == 0xa5a5; }); + char point[256]; + std::snprintf(point, sizeof point, "%s one-row bytes N=%lld K=%lld parts=%d " + "wv=%d sk=%d npw=%d nt=%d", row, (long long)N, (long long)K, + parts, wv, sk, npw, nt); + ok(point, ran && guard && compact == full, + !ran ? "launch/copy failed" : !guard ? "output guard overwritten" : ""); + if (!ran) return; + ++checked; + } + ok(nm, checked > 0, "one-row tuning points checked: " + std::to_string(checked)); } @@ -30457,6 +30500,12 @@ static void device_phase(const Plugin& pl) { /* z-lab's ParoQuant int4 (u8 zero, parts) and an int4 W4A16 release imported with a u4 * zero plane: five groups a row (a pad nibble ends each zero row), then decode and verify * rows at the 27B's shapes. */ + // HYV3 decode geometries, plus column tails and two/three partitioned activation copies. + for (const auto& nk : { std::pair{4096, 2048}, {512, 2048}, + {2048, 4096}, {13824, 2048}, {2048, 6912}, {48, 640} }) + w5a16_numeric(pl, 1, nk.first, nk.second, 0, 0, 4, RAD_U8, true); + w5a16_numeric(pl, 1, 400, 640, 144, 0, 4, RAD_U8, true); + w5a16_numeric(pl, 1, 512, 2560, 128, 384, 4, RAD_U4, true); w5a16_numeric(pl, 8, 272, 640, 0, 0, 4); w5a16_numeric(pl, 5, 400, 640, 144, 0, 4); w5a16_numeric(pl, 40, 512, 2560, 128, 384, 4); @@ -30472,6 +30521,8 @@ static void device_phase(const Plugin& pl) { if (sel("gemm_w5a16_nt_m64")) { if (!g_case) std::printf("\nParoQuant's 5-bit GEMM, against the planes' meaning\n"); + w5a16_numeric(pl, 1, 272, 640, 0, 0, 5, RAD_U8, true); + w5a16_numeric(pl, 1, 400, 640, 144, 0, 5, RAD_U8, true); w5a16_numeric(pl, 8, 272, 640); w5a16_numeric(pl, 64, 400, 2560); w5a16_numeric(pl, 5, 400, 640, 144);