Archived
libr4d: decode tile arms for the int4/int5 x int8 GEMM
gemm_w4a8i8_tiled / gemm_w5a8i8_tiled ran one 256 x 64 tile, built for 2048-row prefill chunks. At decode row counts its time is the weight's trip through the block and is flat in M (N=5120 K=17408: 272 us at M=32, 284 at M=256): a 128-row step computes half padding, and N/64 blocks (80 at N=5120) leave most of an R9700 idle. The kernel is now templated on its tile (TM x TN fragments a wave, WM x WN waves a block), with the body unchanged, and the rows gain one tunable axis, `arm`: 0 is the prefill tile (the old constants, so an untuned install runs the same kernel), 1-8 are 128-, 64- and 32-row tiles. tools/opshapes.cpp describes the int8-activation operands so rad-tune can walk the axis, and r4d_selftest checks every arm against the host reference for int4 and int5, with ragged M and a partitioned weight; --perfwpa takes R4D_PERF_ARM. Tuned on an R9700 at the trunk shapes of Qwen3.8-27B ParoQuant int5 (rad-tune, us, arm 0 -> the tuned arm): shape M=32 M=64 M=128 N=16384 K=5120 258 -> 72 229 -> 97 233 -> 151 N=5120 K=6144 101 -> 38 101 -> 42 101 -> 62 N=34816 K=5120 497 -> 217 494 -> 234 504 -> 309 N=5120 K=17408 273 -> 89 275 -> 103 277 -> 168 N=12288 K=5120 167 -> 52 167 -> 72 168 -> 112 N=1024 K=5120 36 -> 21 36 -> 22 37 -> 24 The prefill band (M=2048) keeps arm 0. Which rows reach this kernel is unchanged: the qwen35 plugin still hands the rotated int4/int5 linears to it above 64 rows, so the M=32/64 columns only bound what a lower handover could gain. The int4 rows share the kernel and gain alike (int4 x int8 at M=128, arm 0 -> best: N=5120 K=17408 267 -> 150 us, N=34816 K=5120 444 -> 248, N=16384 K=5120 211 -> 118, N=12288 K=5120 160 -> 91). End to end, Qwen3.8-27B ParoQuant int5 + DFlash2 on one R9700 (k6_v4 cache, f16 state, depth 3, 32 sequences of ~3k shared + ~2.5k unique prompt, 3000 tokens out, so 128-row verify steps), against origin/main on the same boot and tune cache: mean decode step 111.0 -> 93.8 ms, decode 467 -> 614 tok/s, wall 345 -> 418, no preemptions. Decode at up to 64 rows (one to eight sequences at depth 7) and prefill (arm 0) run the same kernels as before. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019BxxGmKGSAyx2h88G3sEGp
This commit is contained in:
1 parent
7da9ccf38b
commit
60bcc30e5e
5 files changed
+96
-22
No files matched your search
@@ -47,15 +47,19 @@
|
||||
#include <cstdint>
|
||||
#include <stdexcept>
|
||||
|
||||
#define R4D_WPT_TM 4 // 16-row fragments per wave
|
||||
#define R4D_WPT_TN 2 // 16-column fragments per wave
|
||||
#define R4D_WPT_WM 4 // waves along M
|
||||
#define R4D_WPT_WN 2 // waves along N
|
||||
#define R4D_WPT_PAD 8 // LDS row pad: 16 lanes reading rows 136 B apart
|
||||
#define R4D_WPT_NTHR (R4D_WPT_WM * R4D_WPT_WN * 32)
|
||||
#define R4D_WPT_BMF (R4D_WPT_WM * R4D_WPT_TM * 16)
|
||||
#define R4D_WPT_LBK 128 // one scale group a slab
|
||||
|
||||
// Tile arms (the row's `arm` axis): TM x TN fragments a wave, WM x WN waves a block. Arm 0 is the
|
||||
// prefill tile (256 x 64, eight waves). A prefill chunk fills it; a decode step of 32..256 rows does
|
||||
// not: its time is the weight's trip through the block, flat in M, and a 256-row block over 128 rows
|
||||
// computes half padding. The smaller arms cut BM to the batch and BN with it, multiplying the blocks a
|
||||
// narrow N gets (N / 64 = 80 at N = 5120 is a third of the card). Each arm needs 2 * TN % WM == 0
|
||||
// (the weight slab splits evenly over the block's threads).
|
||||
#define WPT_ARMS(X) \
|
||||
X(0, 4, 2, 4, 2) X(1, 4, 2, 2, 4) X(2, 4, 2, 2, 2) X(3, 4, 1, 2, 2) \
|
||||
X(4, 2, 2, 2, 4) X(5, 2, 2, 2, 2) X(6, 2, 1, 2, 2) X(7, 2, 2, 1, 4) X(8, 2, 1, 1, 2)
|
||||
|
||||
typedef int wpt_v2i __attribute__((ext_vector_type(2)));
|
||||
typedef int wpt_v8i __attribute__((ext_vector_type(8)));
|
||||
typedef unsigned wpt_u4 __attribute__((ext_vector_type(4)));
|
||||
@@ -70,12 +74,12 @@ static __device__ __forceinline__ unsigned wpt_sub_zero(unsigned codes4, unsigne
|
||||
return ((codes4 | 0x80808080u) - zz) ^ 0x80808080u;
|
||||
}
|
||||
|
||||
template <int BITS>
|
||||
__global__ __launch_bounds__(R4D_WPT_NTHR) void r4d_gemm_wpa8i8_tiled_kernel(
|
||||
template <int BITS, int TM, int TN, int WM, int WN>
|
||||
__global__ __launch_bounds__(WM * WN * 32) void r4d_gemm_wpa8i8_tiled_kernel(
|
||||
const unsigned char* __restrict__ AT, const float* __restrict__ As,
|
||||
const unsigned* __restrict__ Wq, const unsigned* __restrict__ Whi, const unsigned* __restrict__ Wsz,
|
||||
__hip_bfloat16* __restrict__ C, int M, int K, int N, int as_rs, int ldc) {
|
||||
constexpr int TM = R4D_WPT_TM, TN = R4D_WPT_TN, WN = R4D_WPT_WN, NTHR = R4D_WPT_NTHR, BMF = R4D_WPT_BMF;
|
||||
constexpr int NTHR = WM * WN * 32, BMF = WM * TM * 16;
|
||||
constexpr int LBK = R4D_WPT_LBK, NS = LBK / 16, STR = LBK + R4D_WPT_PAD, BNF = WN * TN * 16;
|
||||
constexpr int NTILES_T = BNF / 16, TOT_SLOTS = NTILES_T * NS * 32;
|
||||
// A thread stages one lane's 16 bytes of a 64-wide k block (four k steps of one weight row, as the
|
||||
@@ -243,17 +247,33 @@ __global__ __launch_bounds__(R4D_WPT_NTHR) void r4d_gemm_wpa8i8_tiled_kernel(
|
||||
}
|
||||
}
|
||||
|
||||
static bool wpa8i8_arm_ok(int arm) {
|
||||
#define X(id, tm, tn, wm, wn) if (arm == id) return true;
|
||||
WPT_ARMS(X)
|
||||
#undef X
|
||||
return false;
|
||||
}
|
||||
|
||||
template <int BITS>
|
||||
static void wpa8i8_launch(long at, long ascale, long wq, long whi, long wsz, long c, int M, int K, int N,
|
||||
int as_rs, long stream, int ldc) {
|
||||
int as_rs, long stream, int ldc, int arm) {
|
||||
if (M < 1) throw std::runtime_error("r4d gemm_wpa8i8_tiled: M must be >= 1");
|
||||
if (N < 16 || N % 16) throw std::runtime_error("r4d gemm_wpa8i8_tiled: N must be a multiple of 16");
|
||||
if (K % R4D_WPT_LBK) throw std::runtime_error("r4d gemm_wpa8i8_tiled: K must be a multiple of 128");
|
||||
const int bnf = R4D_WPT_WN * R4D_WPT_TN * 16;
|
||||
dim3 grid((N + bnf - 1) / bnf, (M + R4D_WPT_BMF - 1) / R4D_WPT_BMF), block(R4D_WPT_NTHR);
|
||||
hipLaunchKernelGGL((r4d_gemm_wpa8i8_tiled_kernel<BITS>), grid, block, 0, (hipStream_t)stream,
|
||||
(const unsigned char*)at, (const float*)ascale, (const unsigned*)wq,
|
||||
(const unsigned*)whi, (const unsigned*)wsz, (__hip_bfloat16*)c, M, K, N, as_rs, ldc);
|
||||
if (!wpa8i8_arm_ok(arm)) throw std::runtime_error("r4d gemm_wpa8i8_tiled: no such tile arm");
|
||||
hipStream_t st = (hipStream_t)stream;
|
||||
#define X(id, tm, tn, wm, wn) \
|
||||
if (arm == id) { \
|
||||
static_assert((2 * tn) % wm == 0, "arm " #id ": the weight slab must split over the block"); \
|
||||
const int bnf = wn * tn * 16, bmf = wm * tm * 16; \
|
||||
dim3 grid((N + bnf - 1) / bnf, (M + bmf - 1) / bmf), block(wm * wn * 32); \
|
||||
hipLaunchKernelGGL((r4d_gemm_wpa8i8_tiled_kernel<BITS, tm, tn, wm, wn>), grid, block, 0, st, \
|
||||
(const unsigned char*)at, (const float*)ascale, (const unsigned*)wq, \
|
||||
(const unsigned*)whi, (const unsigned*)wsz, (__hip_bfloat16*)c, M, K, N, as_rs, \
|
||||
ldc); \
|
||||
}
|
||||
WPT_ARMS(X)
|
||||
#undef X
|
||||
}
|
||||
|
||||
// ============================================================== the plugin ABI entry points
|
||||
@@ -274,6 +294,9 @@ static int wpa8i8_dispatch(const RadArgs* a, RadStream s) {
|
||||
rc = mx_ascale_grid(g, &as_group, &as_rs, mp.n);
|
||||
if (rc != RAD_OK) return rc;
|
||||
if (as_group != 128) return R4D_NO(RAD_E_SHAPE); // one scale a (row, 128): the slab
|
||||
R4dWpaCfg cfg;
|
||||
r4d_cfg_wpa8i8(a, &cfg);
|
||||
if (!wpa8i8_arm_ok(cfg.arm)) return R4D_NO(RAD_E_INVAL);
|
||||
const long long slab = r4d_mxfp4a8_tile_a_bytes((int)g.M, (int)g.K);
|
||||
if (!a->scratch || a->scratch_bytes < slab) return R4D_NO(RAD_E_INVAL);
|
||||
const long long kb = g.K / 128;
|
||||
@@ -288,7 +311,7 @@ static int wpa8i8_dispatch(const RadArgs* a, RadStream s) {
|
||||
BITS == 5 ? r4d_addr(g.b) + g.N * g.K / 2 + n0 * g.K / 8 : 0L,
|
||||
r4d_addr(g.bscale) + (n0 / 16) * kb * 16 * 4,
|
||||
r4d_addr(g.y) + n0 * 2, (int)g.M, (int)g.K, (int)np, as_rs,
|
||||
(long)(uintptr_t)s, (int)g.N));
|
||||
(long)(uintptr_t)s, (int)g.N, cfg.arm));
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user