diff --git a/libr4d/r4d_args.h b/libr4d/r4d_args.h index de4ca076..2bc7d1fb 100644 --- a/libr4d/r4d_args.h +++ b/libr4d/r4d_args.h @@ -316,6 +316,10 @@ struct R4dTileCfg { int variant; int bm; int bn; }; * default, decides at issue; a fixed value is for a tuner. */ struct R4dMxTileCfg { int tn; }; +/* The int4/int5 x int8 tiled GEMM's tile arm (r4d_gemm_wpa8i8_tiled.hip, WPT_ARMS). Arm 0 is the + * prefill tile as it always ran; the others serve the decode band above the skinny kernel's 64 rows. */ +struct R4dWpaCfg { int arm; }; + /* The MXFP4 decode GEMM's split-K width, K slab and weight-load hint. 0 on each is the rule in * r4d_gemm_mxfp4a8_decode.hip (it needs M and the shape, which exist only at issue); `nt` 1 streams * the weight past L2/MALL, 2 loads it plainly. */ @@ -357,6 +361,7 @@ void r4d_cfg_gemm (const RadArgs*, struct R4dGemmCfg*); void r4d_cfg_tiled (const RadArgs*, struct R4dTileCfg*); void r4d_cfg_prefill(const RadArgs*, struct R4dTileCfg*); void r4d_cfg_mxfp4_tiled(const RadArgs*, struct R4dMxTileCfg*); +void r4d_cfg_wpa8i8 (const RadArgs*, struct R4dWpaCfg*); void r4d_cfg_mxfp4_decode(const RadArgs*, struct R4dMxDecCfg*); void r4d_cfg_split (const RadArgs*, struct R4dSplitCfg*); void r4d_cfg_ar (const RadArgs*, struct R4dArCfg*); diff --git a/libr4d/r4d_gemm_wpa8i8_tiled.hip b/libr4d/r4d_gemm_wpa8i8_tiled.hip index 581d86e7..0bb50bbe 100644 --- a/libr4d/r4d_gemm_wpa8i8_tiled.hip +++ b/libr4d/r4d_gemm_wpa8i8_tiled.hip @@ -47,15 +47,19 @@ #include #include -#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 -__global__ __launch_bounds__(R4D_WPT_NTHR) void r4d_gemm_wpa8i8_tiled_kernel( +template +__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 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), 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), 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; } diff --git a/libr4d/r4d_rows.cpp b/libr4d/r4d_rows.cpp index cbe9c273..ea01abac 100644 --- a/libr4d/r4d_rows.cpp +++ b/libr4d/r4d_rows.cpp @@ -5766,6 +5766,15 @@ static const RadTunable tGemmPrefill[] = { "compiled tile arm; 0 is BM 128 x BN 256, libr4d's own default" }, }; +/* The int4 / int5 x int8 tiled GEMM's one axis (r4d_gemm_wpa8i8_tiled.hip): its tile arm. Arm 0 is + * the 256x64 prefill tile as it always ran. */ +static const int64_t kWpaArm[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 }; +static const RadTunable tGemmWpa8i8[] = { + { "arm", kWpaArm, NELEM(kWpaArm), 0, + "tile arm (r4d_gemm_wpa8i8_tiled.hip WPT_ARMS): 0 = 256x64 prefill, 1 = 128x128, 2 = 128x64, " + "3 = 128x32, 4 = 64x128, 5 = 64x64, 6 = 64x32, 7 = 32x128, 8 = 32x32" }, +}; + /* The MXFP4 tiled GEMM's one axis: compiled arms of one template. 0 is the M crossover (TN=4 from * M >= 2048), which needs M. */ static const int64_t kMxTn[] = { 0, 2, 4 }; @@ -8661,7 +8670,7 @@ R4D_WIN_TREE_PAIR(512, 4) R4D_WIN_TREE_PAIR(512, 8) "I8 A (parts side by side) with an f32 scale per (row, part, 128), 4-bit W with an f16 scale and " "integer zero per 128 K, bf16 C, int32 accumulate per 128 of K", RAD_DOMAIN_DEVICE, R4D_PRIO_GEMM_SKINNY, - ROW(cGemmNtW4A8I8), NOTUNE, + ROW(cGemmNtW4A8I8), ROW(tGemmWpa8i8), nullptr, nullptr, nullptr, r4d_rad_gemm_w4a8i8_tiled, r4d_rad_gemm_mxfp4a8_tiled_scratch, r4d_rad_layout_w4, r4d_rad_relayout_w4, nullptr, r4d_rad_unrelayout_w4 }, @@ -8673,7 +8682,7 @@ R4D_WIN_TREE_PAIR(512, 4) R4D_WIN_TREE_PAIR(512, 8) "I8 A (parts side by side) with an f32 scale per (row, part, 128), 5-bit W with an f16 scale and " "integer zero per 128 K, bf16 C, int32 accumulate per 128 of K", RAD_DOMAIN_DEVICE, R4D_PRIO_GEMM_SKINNY, - ROW(cGemmNtW5A8I8), NOTUNE, + ROW(cGemmNtW5A8I8), ROW(tGemmWpa8i8), nullptr, nullptr, nullptr, r4d_rad_gemm_w5a8i8_tiled, r4d_rad_gemm_mxfp4a8_tiled_scratch, r4d_rad_layout_w5, r4d_rad_relayout_w5, nullptr, r4d_rad_unrelayout_w5 }, @@ -11333,6 +11342,10 @@ static int64_t tune_i(const RadArgs* a, const RadTunable* tab, int n, const char } #define TUNE(a, tab, key) ((int)tune_i((a), (tab), NELEM(tab), (key))) +extern "C" void r4d_cfg_wpa8i8(const RadArgs* a, R4dWpaCfg* out) { + out->arm = TUNE(a, tGemmWpa8i8, "arm"); +} + extern "C" void r4d_cfg_gemm(const RadArgs* a, R4dGemmCfg* out) { out->wv = TUNE(a, tGemmSkinny, "wv"); out->sk = TUNE(a, tGemmSkinny, "sk"); diff --git a/libr4d/r4d_selftest.cpp b/libr4d/r4d_selftest.cpp index 65f9b809..adc36df0 100644 --- a/libr4d/r4d_selftest.cpp +++ b/libr4d/r4d_selftest.cpp @@ -6077,14 +6077,14 @@ static void w5a16_perf(const Plugin& pl, int bits, int64_t M, int64_t N, int64_t * scale and a u8 zero per 128 of K, an int8 activation with an f32 scale per (row, 128), parts * side by side. */ static void wpa8i8_numeric(const Plugin& pl, int bits, int64_t M, int64_t N, int64_t K, int64_t pb1 = 0, - int64_t pb2 = 0, uint32_t zdt = RAD_U8) { + int64_t pb2 = 0, uint32_t zdt = RAD_U8, int64_t arm = 0) { const char* row = bits == 4 ? "gemm_w4a8i8_tiled" : "gemm_w5a8i8_tiled"; if (!sel(row)) return; const PartCut cut(N, pb1, pb2); const int parts = cut.parts; char nm[160]; - std::snprintf(nm, sizeof nm, "%s numeric M=%lld N=%lld K=%lld parts=%d zero %s", row, - (long long)M, (long long)N, (long long)K, parts, rad_dtype_name(zdt)); + std::snprintf(nm, sizeof nm, "%s numeric M=%lld N=%lld K=%lld parts=%d zero %s arm %lld", row, + (long long)M, (long long)N, (long long)K, parts, rad_dtype_name(zdt), (long long)arm); const RadKernelInfo* r = find(pl, row); if (!r || !r->layout || !r->relayout) { ok(nm, false, "row or hooks absent"); return; } const int64_t G = K / 128; @@ -6132,6 +6132,7 @@ static void wpa8i8_numeric(const Plugin& pl, int bits, int64_t M, int64_t N, int PS("dtype", bits == 4 ? "w4a8i8_asym" : "w5a8i8") }; if (pb1) ps.push_back(PI("pb1", pb1)); if (pb2) ps.push_back(PI("pb2", pb2)); + if (arm) ps.push_back(PI("arm", arm)); RadArgs a; std::memset(&a, 0, sizeof(a)); a.t = ts.data(); a.n_t = (int)ts.size(); @@ -6230,6 +6231,9 @@ static void wpa_perf(const Plugin& pl, int64_t N, int64_t K, int64_t M, int iter std::vector ps = { PI("M", M), PI("N", N), PI("K", K), PI("group", 128), PS("dtype", bits == 4 ? "w4a8i8_asym" : "w5a8i8") }; if (pb1) ps.push_back(PI("pb1", pb1)); + /* R4D_PERF_ARM pins the row's tile arm, for a sweep by hand. */ + int64_t perf_arm = 0; + if (const char* e = std::getenv("R4D_PERF_ARM")) { perf_arm = std::atoll(e); ps.push_back(PI("arm", perf_arm)); } RadArgs a; std::memset(&a, 0, sizeof(a)); a.t = ts.data(); a.n_t = (int)ts.size(); @@ -6239,7 +6243,8 @@ static void wpa_perf(const Plugin& pl, int64_t N, int64_t K, int64_t M, int iter const int64_t sb = r->scratch(&a); if (sb > 0) { a.scratch = dalloc((size_t)sb); a.scratch_bytes = sb; } } - for (int i = 0; i < 3; ++i) r->launch(&a, nullptr); + if (r->launch(&a, nullptr) != RAD_OK) { std::printf("--perfwpa: arm %lld refused\n", (long long)perf_arm); return; } + for (int i = 0; i < 2; ++i) r->launch(&a, nullptr); st_stream_sync(nullptr); st_memcpy(hy.data(), d_y, hy.size() * 2, hipMemcpyDeviceToHost); uint64_t hash = 1469598103934665603ull; @@ -33799,6 +33804,13 @@ static void device_phase(const Plugin& pl) { wpa8i8_numeric(pl, bits, 2100, 34816, 5120, 17408); wpa8i8_numeric(pl, bits, 2048, 17408, 5120); wpa8i8_numeric(pl, bits, 2048, 5120, 17408); + /* The decode arms: a whole block, ragged M (a partial 16-row tile and a partial block) + * and a partitioned weight, on every arm. */ + for (int arm = 1; arm <= 8; ++arm) { + wpa8i8_numeric(pl, bits, 128, 1280, 1024, 0, 0, RAD_U8, arm); + wpa8i8_numeric(pl, bits, 40, 1280, 1024, 0, 0, RAD_U8, arm); + wpa8i8_numeric(pl, bits, 100, 2560, 1280, 1536, 0, RAD_U8, arm); + } if (bits == 4) { /* An int4 W4A16 release imported with a u4 zero plane and no rotation: five groups * a row, so each zero row ends in a pad nibble; then the delta net's qkv|z, the MLP's diff --git a/tools/opshapes.cpp b/tools/opshapes.cpp index e807a0db..699a0b40 100644 --- a/tools/opshapes.cpp +++ b/tools/opshapes.cpp @@ -193,6 +193,27 @@ int rad_operand_shapes(const std::string& op, const Geometry& g, int n_opd, int6 add((uint32_t)RAD_BF16, {M, N}); /* y */ add((uint32_t)RAD_F32, {K / group, (M + 15) / 16 * 16}); /* a_sum, group-major */ skip_opd(); /* b_ref */ + } else if (q && (std::strcmp(q, "w5a8i8") == 0 || std::strcmp(q, "w4a8i8_asym") == 0)) { + /* The same grids against an int8 activation (libr4d/r4d_gemm_wpa8i8_tiled.hip): + * givens_dense_quant's int8 codes, parts side by side, with an f32 scale per (row, + * part, 128 of K). The weight is libr4d/r4d_layout.cpp's int4 / int5 form: a nibble + * plane of N*K/2 bytes, then (int5 only) a fifth-bit plane of N*K/8, and one dword per + * row and 128 of K holding the f16 scale and zero. Random bytes suffice for timing. */ + if (N <= 0 || K <= 0 || group != 128 || (K % 128) || (N % 16)) + return RAD_E_UNSUPPORTED; + const int64_t pb1 = gi("pb1", 0), pb2 = gi("pb2", 0); + const int64_t e1 = pb1 > 0 && pb1 < N ? pb1 : N; + int64_t e2 = pb2 > 0 && pb2 < N ? pb2 : N; + if (e2 < e1) e2 = e1; + const int64_t parts = e2 < N ? 3 : (e1 < N ? 2 : 1); + const bool five = std::strcmp(q, "w5a8i8") == 0; + add((uint32_t)RAD_I8, {M, parts * K}); /* a */ + add((uint32_t)RAD_F32, {M, parts * (K / 128)}); /* a_scale */ + add((uint32_t)RAD_U8, {N, five ? K * 5 / 8 : K / 2}); /* b */ + add((uint32_t)RAD_U32, {N, K / group}); /* b_scale */ + add((uint32_t)RAD_BF16, {M, N}); /* y */ + skip_opd(); /* a_sum */ + skip_opd(); /* b_ref */ } else { const bool a8 = q && std::strcmp(q, "fp8a8") == 0; const bool a16 = q && std::strcmp(q, "fp8a16") == 0;