From 7969a6ec93c52bdd39f214ffadbe170784a012fa Mon Sep 17 00:00:00 2001 From: jkuepker <20614147+jkuepker@users.noreply.github.com> Date: Thu, 8 Oct 2026 09:40:28 -0400 Subject: [PATCH] HIP gfx12: native int8-WMMA prompt experts on gfx1200/gfx1201 (opt-in STRATA_PF_FUSED=1, #1277) The native IQ / K-quant prompt-expert kernels (moe_fused_iq.cu, native_w11_kernel) were gfx11-only; on RDNA4 a pack with STRATA_PF_FUSED=1 silently kept the MMQ path. They now compile and run on gfx1200 / gfx1201. Still opt-in: STRATA_PF_FUSED=1 stays off by default (it changes rounding, so it is not an arch_defaults switch), and STRATA_PF_FUSED_NATIVE=0 still turns the native kernels off. Compile gating - STRATA_NAT_W12 for __gfx1200__ / __gfx1201__, STRATA_NAT_WMMA for either family. The kernel calls the _gfx12 spelling of the iu8 WMMA builtin (the un-suffixed one does not compile there). Any other target keeps the trapping body. Kernel (same arithmetic, other lane layout) - gfx12 WMMA fragments are 8 int8 per lane and the two lane halves hold different k (8*hi .. 8*hi+7), not a replica as on gfx11: B and A are loaded as uint2 at the matching offsets, and every 16-value k step is still one WMMA, so the per-16 scales of the IQ2_S / IQ2_XS paths are untouched. - A lane's 8 accumulator rows are consecutive (8*hi + i), so a feature's gate and up are in the same lane: SwiGLU needs no __shfl_xor, and the down tile is stored as two float4 per lane. NW_DROW(i, hi) gives the row of element i for the per-row scales. Host side - gfx_arch_is_gfx12_wmma(): exact gfx1200 / gfx1201 match, never a prefix, like the gfx11 helper. - dev_info() takes the gfx11 list or gfx12, and also checks that the kernel the build loaded is the real one: the trap stub (a gfx12-generic or SPIR-V build on a listed card) declares no LDS and still passes hipFuncGetAttributes and the occupancy query. - native_supported() reads the STRATA_PF_FUSED=1 opt-in itself: fused::available() / requested() belong to the Q2_0 pack's expert_w11_kernel, which is not ported and stays false on gfx12. Off gfx12 the result is what it was. prefill.cpp fused_ring() follows: a native pack asks native_supported() alone (it implied enabled() before), so on gfx12 it gets the fused buffer layout and ring. - Grid factor per format: gfx12 holds 3 blocks per WGP for IQ2_S / IQ2_XS gate/up (LDS and VGPRs), 4 for the rest. The occupancy API cannot say (it models 64 KB of LDS per multiprocessor), so it is a small table; STRATA_PF_OCC_W=N overrides it. gfx11 keeps 4. - experts_native() refuses a dm that is not 16-byte aligned on gfx12 (the float4 stores); the prompt path's buffers always are. - moe_fused.cu compares the gfx11 list with the helper instead of the "gfx11" prefix (no change on any listed part). Tests (prefill_fused_iq_test) - Runs on gfx12 (the skip gate also asks native_supported()), covers the other four pairs of the Coder IQ1_M pack, S20_E sets the expert count, and the real-shape check of fused against MMQ is now rel RMS <= 1e-3 and worst row <= 2e-2 instead of rel RMS <= 0.05. Measured on a Radeon AI PRO R9700 (gfx1201), Coder IQ1_M pack: - parity: all 10 IQ pairs plus Q4_K / Q5_K pass, rel RMS <= 9.1e-5. - kernels vs MMQ, 48 layers: 1.48x / 1.20x / 1.14x at 4K / 16K / 32K. - prompt speed in the engine (server-reported, alternating starts per arm): 4K +12% to +19% depending on the start, 16K +8.3%, 32K +8.5%. - first-token KL against the FP16 prompt path is 1.10x MMQ's (48 prompts of 4K), top-1 agreement 48/48; 4/4 long-context retrieval checks. Not run on gfx11 or CUDA hardware here. --- include/strata/kernels/gfx_arch.hpp | 4 + include/strata/prefill/moe_fused.hpp | 4 +- include/strata/prefill/moe_fused_iq.hpp | 3 +- src/prefill/moe_fused.cu | 3 +- src/prefill/moe_fused_iq.cu | 126 +++++++++++++++++++++--- src/prefill/prefill.cpp | 5 +- tests/cuda/prefill_fused_iq_test.cpp | 17 +++- 7 files changed, 137 insertions(+), 25 deletions(-) diff --git a/include/strata/kernels/gfx_arch.hpp b/include/strata/kernels/gfx_arch.hpp index ad7ee627..ec57bdc1 100644 --- a/include/strata/kernels/gfx_arch.hpp +++ b/include/strata/kernels/gfx_arch.hpp @@ -24,6 +24,10 @@ inline bool gfx_arch_is_gfx11_wmma(const char* gcn) { gfx_arch_is(gcn, "gfx1150") || gfx_arch_is(gcn, "gfx1151"); } +/// The gfx12 (RDNA4) targets the gfx12 matrix-core kernels are compiled for (`#if defined(__gfx1200__) || +/// defined(__gfx1201__)`): the R9700 / 9070 (XT) are gfx1201, the 9060 (XT) gfx1200. +inline bool gfx_arch_is_gfx12_wmma(const char* gcn) { return gfx_arch_is(gcn, "gfx1200") || gfx_arch_is(gcn, "gfx1201"); } + /// Strix Halo (Ryzen AI Max, RDNA3.5): the part the gfx1151 defaults (docs/STRIX_HALO.md) are measured on. inline bool gfx_arch_is_gfx1151(const char* gcn) { return gfx_arch_is(gcn, "gfx1151"); } diff --git a/include/strata/prefill/moe_fused.hpp b/include/strata/prefill/moe_fused.hpp index a18590fe..eef9e2dc 100644 --- a/include/strata/prefill/moe_fused.hpp +++ b/include/strata/prefill/moe_fused.hpp @@ -31,7 +31,9 @@ bool built(); bool available(); /// available(), unless STRATA_PF_FUSED=0 (read once): the Q2_0 pack's fused experts, on by default since 0.1.36. bool enabled(); -/// STRATA_PF_FUSED=1 given explicitly, and available(): the native IQ packs' fused kernels (opt-in). +/// STRATA_PF_FUSED=1 given explicitly, and available(): the native IQ packs' fused kernels (opt-in). On gfx12 (RDNA4) +/// only the native kernels exist: available() / enabled() / requested() stay false there and +/// native_supported() (moe_fused_iq.hpp) reads the same opt-in itself. bool requested(); /// Bytes of `rows` activation rows of `cols` values (a multiple of 64) in the kernels' int8 form: per 64 values 64 diff --git a/include/strata/prefill/moe_fused_iq.hpp b/include/strata/prefill/moe_fused_iq.hpp index 7c9e3282..7e63df9b 100644 --- a/include/strata/prefill/moe_fused_iq.hpp +++ b/include/strata/prefill/moe_fused_iq.hpp @@ -34,7 +34,8 @@ void quantize_act_native(const float* x, int64_t rows, int64_t cols, void* xa, v /// experts() for native blobs: b.blob[e - e0] is the native blob of expert e (gate at 0, up at g.up_off, down at /// g.down_off; 2-byte aligned). `xa` from quantize_act_native; H in `ha` (act_bytes(n, 640)); down into `dm` at the -/// rows of `group`. +/// rows of `group`. `dm` 16-byte aligned (gfx12 stores float4; checked there - the prompt path's buffers are 256-aligned +/// through Alloc's 256-byte takes from a 256-aligned cache slot or cudaMalloc). void experts_native(const Batch& b, const NativeGeom& g, int n_expert, int64_t n, const void* scratch, const void* xa, const int32_t* src, void* ha, float* dm, void* stream); diff --git a/src/prefill/moe_fused.cu b/src/prefill/moe_fused.cu index e5c1e003..178c09cc 100644 --- a/src/prefill/moe_fused.cu +++ b/src/prefill/moe_fused.cu @@ -18,6 +18,7 @@ // for the k of a half-block: mma k index 16h + 4t + j stands for weight (and activation) 16h + t + 4j. The // activations are stored in it (perm32), and a lane's two B registers (h = 0 and 1) are adjacent: one 8-byte load. #include "strata/prefill/moe_fused.hpp" +#include "strata/kernels/gfx_arch.hpp" #include #include @@ -607,7 +608,7 @@ const DevInfo& dev_info() { cudaDeviceProp prop; cudaDeviceGetAttribute(&d.sms, cudaDevAttrMultiProcessorCount, dev); hipFuncAttributes fa{}; - if (cudaGetDeviceProperties(&prop, dev) != cudaSuccess || std::strncmp(prop.gcnArchName, "gfx11", 5) != 0 || + if (cudaGetDeviceProperties(&prop, dev) != cudaSuccess || !strata::kernels::gfx_arch_is_gfx11_wmma(prop.gcnArchName) || hipFuncGetAttributes(&fa, reinterpret_cast(expert_w11_kernel)) != hipSuccess) { cudaGetLastError(); return d; diff --git a/src/prefill/moe_fused_iq.cu b/src/prefill/moe_fused_iq.cu index 0196a768..e22b927c 100644 --- a/src/prefill/moe_fused_iq.cu +++ b/src/prefill/moe_fused_iq.cu @@ -20,6 +20,7 @@ // ldmatrix. The products are mma.sync m16n8k32 (m16n8k16 for the formats with a scale per 16 values); the epilogues // (SwiGLU, H to int8 per 32 features; down into the per-slot rows) are moe_fused.cu's. #include "strata/prefill/moe_fused_iq.hpp" +#include "strata/kernels/gfx_arch.hpp" #include #include @@ -655,11 +656,28 @@ native_kernel(const Batch b, const NativeGeom geo, const Tables tb, const uint8_ // A work item = NW_ROWS weight rows x a 64-row tile; 8 waves: 4 along the weight rows (32 each) x 2 along the tile // (32 each), each 2 x 2 WMMA tiles. Gate/up: local row r is feature r / 2, its gate (r even) or up (r odd) row, so a // lane pair l, l + 16 holds gate and up of one feature. +// gfx12 (RDNA4: gfx1200 / gfx1201, wave32) has the same instruction with another layout (checked on gfx1201, R9700): +// A lane l holds A[l % 16][8 (l / 16) + j], B lane l B[8 (l / 16) + j][l % 16] (j = 0..7: 8 int8, two VGPRs, no +// replication), C/D lane l holds D[8 (l / 16) + i][l % 16]. A WMMA still contracts 16 k; lane half hi supplies k +// 8 hi .. 8 hi + 7 of them, so a 16-value k-step is still one WMMA (the K16 scales stay per WMMA). NW_DROW is the +// D row of a lane's element i; on gfx12 a lane's 8 rows are consecutive, so a feature's gate (even local row) and up +// (odd) sit in one lane (elements 2j, 2j + 1) and the SwiGLU needs no cross-lane exchange. #if defined(__gfx1100__) || defined(__gfx1101__) || defined(__gfx1102__) || defined(__gfx1150__) || defined(__gfx1151__) #define STRATA_NAT_W11 1 #else #define STRATA_NAT_W11 0 #endif +#if defined(__gfx1200__) || defined(__gfx1201__) +#define STRATA_NAT_W12 1 +#else +#define STRATA_NAT_W12 0 +#endif +#define STRATA_NAT_WMMA (STRATA_NAT_W11 || STRATA_NAT_W12) +#if STRATA_NAT_W12 +#define NW_DROW(i, hi) (8 * (hi) + (i)) +#else +#define NW_DROW(i, hi) (2 * (i) + (hi)) +#endif // The VGPR cap of the occupancy variant (amdgpu_waves_per_eu). 8 was measured against ROCm 7's clang; clang 22 (ROCm 7.10) // computes wrong results with it (#1180: gfx1100, every run), so there the cap is off. -DSTRATA_W_LB=N sets it. #if defined(STRATA_W_LB) @@ -670,19 +688,31 @@ constexpr int NW_LB = 1; constexpr int NW_LB = 8; #endif typedef int nw_i4 __attribute__((ext_vector_type(4))); +typedef int nw_i2 __attribute__((ext_vector_type(2))); typedef int nw_i8 __attribute__((ext_vector_type(8))); constexpr int NW_ROWS = 128; constexpr int NW_THREADS = 256; -__device__ __forceinline__ nw_i8 nw_wmma(nw_i4 a, nw_i4 b, nw_i8 c) { +// one A / B fragment: gfx11 16 int8 per lane (uint4), gfx12 8 (uint2) +#if STRATA_NAT_W12 +typedef uint2 nw_frag; +typedef nw_i2 nw_ab; +__device__ __forceinline__ nw_ab nw_v(uint2 v) { return nw_ab{(int) v.x, (int) v.y}; } +#else +typedef uint4 nw_frag; +typedef nw_i4 nw_ab; +__device__ __forceinline__ nw_ab nw_v(uint4 v) { return nw_ab{(int) v.x, (int) v.y, (int) v.z, (int) v.w}; } +#endif +__device__ __forceinline__ nw_i8 nw_wmma(nw_ab a, nw_ab b, nw_i8 c) { #if STRATA_NAT_W11 return __builtin_amdgcn_wmma_i32_16x16x16_iu8_w32(true, a, true, b, c, false); +#elif STRATA_NAT_W12 + return __builtin_amdgcn_wmma_i32_16x16x16_iu8_w32_gfx12(true, a, true, b, c, false); #else __builtin_trap(); return c; #endif } -__device__ __forceinline__ nw_i4 nw_u4(uint4 v) { return nw_i4{(int) v.x, (int) v.y, (int) v.z, (int) v.w}; } // W (X1): ALIAS = the H tile lives on the weight buffers (one extra barrier); LB > 0 = amdgpu_waves_per_eu(LB) (VGPR cap). // Disable both with -DSTRATA_W_NO_OCC (the launch sites then use and grid factor wgp_blocks). @@ -690,7 +720,7 @@ template __global__ void __launch_bounds__(NW_THREADS) __attribute__((amdgpu_waves_per_eu(LB > 0 ? LB : 1))) native_w11_kernel(const Batch b, const NativeGeom geo, const Tables tb, const uint8_t* __restrict__ act, const int32_t* __restrict__ src, uint8_t* __restrict__ out, float* __restrict__ dm) { -#if STRATA_NAT_W11 +#if STRATA_NAT_WMMA constexpr int NS = (GU ? GU_ROWS_K : D_ROWS_K) / 64; // 64-value stages along K constexpr int NFB = (GU ? 1280 : 2560) / NW_ROWS; // work items per tile constexpr int ACT_LD = NS * AB; @@ -766,13 +796,18 @@ native_w11_kernel(const Batch b, const NativeGeom geo, const Tables tb, const ui for (int nt = 0; nt < 2; ++nt) #pragma unroll for (int i = 0; i < 8; ++i) acc[mt][nt][i] = 0.0f; - uint4 bq[2][4]; + nw_frag bq[2][4]; // B per 16-value k-step: bq[nt][2 h + kk] = k 32 h + 16 kk .. (gfx12: this lane's 8 of them) float2 bx[2], bsum[2]; // the activation scales, and (the formats with a minimum) the codes' sums per 32 values - auto fetch_b = [&](int s, uint4 (&q)[2][4], float2 (&x)[2], float2 (&sm)[2]) { + auto fetch_b = [&](int s, nw_frag (&q)[2][4], float2 (&x)[2], float2 (&sm)[2]) { #pragma unroll for (int nt = 0; nt < 2; ++nt) { +#if STRATA_NAT_W12 + const uint2* p = reinterpret_cast(brow[nt] + s * AB + 8 * hi); // k 16 j + 8 hi .. + 7 + q[nt][0] = p[0]; q[nt][1] = p[2]; q[nt][2] = p[4]; q[nt][3] = p[6]; +#else const uint4* p = reinterpret_cast(brow[nt] + s * AB); q[nt][0] = p[0]; q[nt][1] = p[1]; q[nt][2] = p[2]; q[nt][3] = p[3]; +#endif x[nt] = *reinterpret_cast(brow[nt] + s * AB + 64); if constexpr (has_min(WT)) sm[nt] = *reinterpret_cast(brow[nt] + s * AB + 72); else sm[nt] = make_float2(0.0f, 0.0f); @@ -781,7 +816,7 @@ native_w11_kernel(const Batch b, const NativeGeom geo, const Tables tb, const ui if (on) fetch_b(0, bq, bx, bsum); for (int s = 0; s < NS; ++s) { __syncthreads(); // stage s's weights are in buffer s & 1 - uint4 nbq[2][4]; + nw_frag nbq[2][4]; float2 nbx[2], nbsum[2]; if (on && s + 1 < NS) fetch_b(s + 1, nbq, nbx, nbsum); if (on) { @@ -791,18 +826,24 @@ native_w11_kernel(const Batch b, const NativeGeom geo, const Tables tb, const ui #pragma unroll for (int mt = 0; mt < 2; ++mt) { const int rb = 32 * wm + 16 * mt; +#if STRATA_NAT_W12 + // k 32 h + 8 hi .. + 7 (A0) and 32 h + 16 + 8 hi .. + 7 (A1) of row rb + l16 + const uint2* ap = reinterpret_cast(&wt[bf][rb + l16][32 * h + 8 * hi]); + const nw_ab A0 = nw_v(ap[0]), A1 = nw_v(ap[2]); +#else const uint4* ap = reinterpret_cast(&wt[bf][rb + l16][32 * h]); - const nw_i4 A0 = nw_u4(ap[0]), A1 = nw_u4(ap[1]); + const nw_ab A0 = nw_v(ap[0]), A1 = nw_v(ap[1]); +#endif float w0[8], w1[8]; #pragma unroll for (int i = 0; i < 8; ++i) { - const float2 sw = *reinterpret_cast(&ws[bf][rb + 2 * i + hi][2 * h]); + const float2 sw = *reinterpret_cast(&ws[bf][rb + NW_DROW(i, hi)][2 * h]); w0[i] = sw.x; w1[i] = sw.y; } #pragma unroll for (int nt = 0; nt < 2; ++nt) { const float dx = h ? bx[nt].y : bx[nt].x; - const nw_i4 B0 = nw_u4(bq[nt][2 * h]), B1 = nw_u4(bq[nt][2 * h + 1]); + const nw_ab B0 = nw_v(bq[nt][2 * h]), B1 = nw_v(bq[nt][2 * h + 1]); const nw_i8 m = nw_i8{MAGIC, MAGIC, MAGIC, MAGIC, MAGIC, MAGIC, MAGIC, MAGIC}; if constexpr (K16) { const nw_i8 d0 = nw_wmma(A0, B0, m), d1 = nw_wmma(A1, B1, m); @@ -844,13 +885,23 @@ native_w11_kernel(const Batch b, const NativeGeom geo, const Tables tb, const ui #pragma unroll for (int mt = 0; mt < 2; ++mt) #pragma unroll - for (int nt = 0; nt < 2; ++nt) + for (int nt = 0; nt < 2; ++nt) { +#if STRATA_NAT_W12 + // lane l: local rows rb + 8 hi + i = features 16 wm + 8 mt + 4 hi + j, gate (i = 2j) and up (2j + 1) +#pragma unroll + for (int j = 0; j < 4; ++j) { + const float gt = acc[mt][nt][2 * j], up = acc[mt][nt][2 * j + 1]; + hs[32 * wn + 16 * nt + l16][16 * wm + 8 * mt + 4 * hi + j] = gt / (1.0f + __expf(-gt)) * up; + } +#else #pragma unroll for (int i = 0; i < 8; ++i) { const float up = __shfl_xor_sync(0xffffffffu, acc[mt][nt][i], 16); const float gt = acc[mt][nt][i]; if (hi == 0) hs[32 * wn + 16 * nt + l16][16 * wm + 8 * mt + i] = gt / (1.0f + __expf(-gt)) * up; } +#endif + } } __syncthreads(); // H block fb (features 64 fb ..) to int8 per 32, natural order: a thread per (tile row, half) @@ -885,9 +936,16 @@ native_w11_kernel(const Batch b, const NativeGeom geo, const Tables tb, const ui for (int nt = 0; nt < 2; ++nt) { const int r = 32 * wn + 16 * nt + l16; if (r < nrows) { +#if STRATA_NAT_W12 + // columns rbase + 32 wm + 16 mt + 8 hi .. + 7: contiguous, 32-byte aligned (dm: 16, checked in experts_native) + float4* d = reinterpret_cast(dm + (size_t) (row0 + r) * 2560 + rbase + 32 * wm + 16 * mt + 8 * hi); + d[0] = make_float4(acc[mt][nt][0], acc[mt][nt][1], acc[mt][nt][2], acc[mt][nt][3]); + d[1] = make_float4(acc[mt][nt][4], acc[mt][nt][5], acc[mt][nt][6], acc[mt][nt][7]); +#else float* d = dm + (size_t) (row0 + r) * 2560 + rbase + 32 * wm + 16 * mt + hi; #pragma unroll for (int i = 0; i < 8; ++i) d[2 * i] = acc[mt][nt][i]; +#endif } } } @@ -904,6 +962,7 @@ unsigned blocks(int64_t n, int per) { return (unsigned) ((n + per - 1) / per); } // per device: whether every kernel here runs (sm_80+, device code in this build, fits), and their occupancy struct DevInfo { bool done = false, ok = false; + bool w12 = false; // gfx12 (RDNA4): the native kernels only - the Q2_0 pack's (moe_fused.cu) are not ported there int sms = 0, occ = 1; }; std::mutex g_mu; @@ -939,6 +998,20 @@ static int wgp_blocks(int occ) { }(); return env > 0 ? env : 2 * std::max(occ, 1); } +// The occupancy variants' (W, X1) blocks per WGP: as many as are resident at once, so the persistent grid has no second +// wave. gfx12 (a WGP: 128 KB of LDS, 1536 VGPRs per SIMD; the compiler's occupancy, -Rpass-analysis=kernel-resource-usage, +// clang 23): gate/up IQ2_S (37,120 B of LDS) and IQ2_XS (203 VGPRs) 3, every other one 4. R9700, IQ2_S / IQ4_NL, one +// layer, 4 runs each: 4K 8.80-8.93 ms at 4, 8.18-8.33 at 3; 16K -3%; IQ3_S / IQ4_NL 2-3% slower at 3. gfx11 keeps 4 +// (measured on gfx1151). Not from the occupancy API: it counts 64 KB of LDS per multiprocessor (37,120 B -> 1 block). +// STRATA_PF_OCC_W=N sets it for every occupancy variant (an experiment knob). +static int occ_blocks(bool w12, int type, bool gu) { + static const int env = [] { + const char* v = std::getenv("STRATA_PF_OCC_W"); + return v != nullptr ? std::atoi(v) : 0; + }(); + if (env > 0) return env; + return w12 && gu && (type == T_IQ2_S || type == T_IQ2_XS) ? 3 : 4; +} #endif const DevInfo& dev_info() { int dev = 0; @@ -952,12 +1025,19 @@ const DevInfo& dev_info() { // empty in a hipcc build: never available here (the prompt path keeps its own expert GEMMs) return d; #elif defined(__HIPCC__) - { // gfx11 with this build's code: the WMMA kernels (one occupancy for all: the same shape and LDS budget) + { // gfx11 / gfx12 with this build's code: the WMMA kernels (one occupancy for all: the same shape and LDS budget) cudaDeviceGetAttribute(&d.sms, cudaDevAttrMultiProcessorCount, dev); cudaDeviceProp prop; hipFuncAttributes fa{}; - if (cudaGetDeviceProperties(&prop, dev) != cudaSuccess || std::strncmp(prop.gcnArchName, "gfx11", 5) != 0 || - hipFuncGetAttributes(&fa, reinterpret_cast(native_w11_kernel)) != hipSuccess) { + const bool props = cudaGetDeviceProperties(&prop, dev) == cudaSuccess; + d.w12 = props && strata::kernels::gfx_arch_is_gfx12_wmma(prop.gcnArchName); + // the arch lists of STRATA_NAT_W11 / STRATA_NAT_W12 (gfx_arch.hpp: never a prefix - another gfx11 / gfx12 part + // has the trapping body, which hipFuncGetAttributes and the occupancy query still accept). And the body this + // build loaded for the device is the real one: the __builtin_trap() stub (a gfx11-generic / gfx12-generic or + // SPIR-V build on a listed card) declares no LDS, the real kernel at least its weight buffers. + if (!props || !(strata::kernels::gfx_arch_is_gfx11_wmma(prop.gcnArchName) || d.w12) || + hipFuncGetAttributes(&fa, reinterpret_cast(native_w11_kernel)) != hipSuccess || + fa.sharedSizeBytes < (size_t) 2 * NW_ROWS * WLD) { cudaGetLastError(); return d; } @@ -1040,7 +1120,15 @@ bool native_supported(int gu_type, int d_type) { const char* v = std::getenv("STRATA_PF_FUSED_NATIVE"); return v != nullptr && v[0] == '0'; }(); - return !off && requested() && gu_covered(gu_type) && d_covered(d_type) && dev_info().ok; // opt-in (=1) + // gfx12: fused::available() (the Q2_0 pack's expert_w11_kernel, not ported) stays false, so requested() does too; the + // same opt-in (STRATA_PF_FUSED=1) is read here. Every other device: requested() as before. + static const bool env = [] { + const char* v = std::getenv("STRATA_PF_FUSED"); + return v != nullptr && v[0] == '1'; + }(); + if (off || !env || !gu_covered(gu_type) || !d_covered(d_type)) return false; // opt-in (=1): no device query without it + const DevInfo& d = dev_info(); + return d.ok && (requested() || d.w12); } void quantize_act_native(const float* x, int64_t rows, int64_t cols, void* xa, void* stream) { @@ -1073,8 +1161,14 @@ void experts_native(const Batch& b, const NativeGeom& g, int n_expert, int64_t n g.gu_type == T_IQ3_XXS || g.gu_type == T_IQ3_S || g.gu_type == T_IQ4_XS; const bool occ_d = g.d_type == T_Q2_0 || g.d_type == T_Q8_0 || g.d_type == T_IQ4_NL; #endif - const unsigned g_gu = (unsigned) std::min(tiles * (1280 / NW_ROWS), (int64_t) d.sms * (occ_gu ? 4 : wgp_blocks(d.occ))); - const unsigned g_d = (unsigned) std::min(tiles * (2560 / NW_ROWS), (int64_t) d.sms * (occ_d ? 4 : wgp_blocks(d.occ))); + if (d.w12 && (reinterpret_cast(dm) & 15) != 0) { // gfx12's down store: two float4 per lane + std::fprintf(stderr, "prefill fused experts (native): the down rows (%p) must be 16-byte aligned\n", (void*) dm); + std::exit(1); + } + const unsigned g_gu = (unsigned) std::min(tiles * (1280 / NW_ROWS), + (int64_t) d.sms * (occ_gu ? occ_blocks(d.w12, g.gu_type, true) : wgp_blocks(d.occ))); + const unsigned g_d = (unsigned) std::min(tiles * (2560 / NW_ROWS), + (int64_t) d.sms * (occ_d ? occ_blocks(d.w12, g.d_type, false) : wgp_blocks(d.occ))); const uint8_t* xa8 = (const uint8_t*) xa; uint8_t* ha8 = (uint8_t*) ha; #define STRATA_NW_GU(T) do { if (occ_gu) native_w11_kernel<<>>(b, g, tb, xa8, src, ha8, nullptr); else native_w11_kernel<<>>(b, g, tb, xa8, src, ha8, nullptr); } while (0) diff --git a/src/prefill/prefill.cpp b/src/prefill/prefill.cpp index 4e03dc8b..43fd1b3d 100644 --- a/src/prefill/prefill.cpp +++ b/src/prefill/prefill.cpp @@ -217,10 +217,11 @@ int64_t g_ring_small_max = 0; // 8192 to 6144, but more experts stay resident). A native pack likewise when the native kernels (moe_fused_iq.hpp) // take any of its layers: IQ2_XS, 4K / 32K, their first version at 384 slots -3% / -6% against MMQ, at 512 +8% / 0%. inline bool fused_ring() { - if (!fused::enabled()) return false; if (core::peer_portable()) return false; // multi-GPU: --peer-device keeps the MMQ path and its buffer sizes const strata::kernels::cpu::ExpertLayout& lay = strata::kernels::cpu::expert_layout(); - if (!lay.native) return true; + // the Q2_0 pack: fused::enabled(). A native pack: native_supported() alone - it implies enabled() wherever the Q2_0 + // kernels exist, and on gfx12 (the native kernels only) enabled() is false while the native layers do run fused. + if (!lay.native) return fused::enabled(); // EVERY layer: fused_layout() shrinks the MoE buffers to the fused path's needs, so a layer the native kernels do // not cover (Unsloth UD-IQ4_XS's Q8_0 down projections) would run MMQ in them at the full chunk and overflow them // (garbage, an illegal memory access or a hung prompt on gfx1151). A pack with such a layer keeps MMQ's buffers; diff --git a/tests/cuda/prefill_fused_iq_test.cpp b/tests/cuda/prefill_fused_iq_test.cpp index 30242cff..ebab9b54 100644 --- a/tests/cuda/prefill_fused_iq_test.cpp +++ b/tests/cuda/prefill_fused_iq_test.cpp @@ -418,7 +418,7 @@ void reference_part(const Pair& p, cudaStream_t s) { // part 2: one layer at a real chunk, timed void timing_part(const Pair& p, int T, cudaStream_t s) { - constexpr int E = 512; + static const int E = [] { const char* v = std::getenv("S20_E"); return v != nullptr && std::atoi(v) > 0 ? std::atoi(v) : 512; }(); // S20_E=256: the Coder pack const Geo geo(p); std::mt19937 rng(1360); const int64_t rows = (int64_t) T * K; @@ -626,7 +626,9 @@ void timing_part(const Pair& p, int T, cudaStream_t s) { cudaEventDestroy(a); cudaEventDestroy(b); delete mbp; - if (efm.rms > 0.05) throw std::runtime_error(std::string(p.name) + ": the paths disagree at the real shape"); + // the two int8 paths at the real shape, every tile of the layer: R9700 rel RMS 1.3-1.9e-4, worst row 2.9e-3-8.3e-3 + if (mq && !no_mmq && (efm.rms > 1e-3 || efm.worst > 2e-2)) + throw std::runtime_error(std::string(p.name) + ": the paths disagree at the real shape"); } } // namespace @@ -640,7 +642,11 @@ int main(int argc, char** argv) { #endif int n = 0; if (cudaGetDeviceCount(&n) != cudaSuccess || n == 0) { std::printf("no CUDA device: skipped\n"); return 77; } - if (!fused::available()) { std::printf("the fused kernels need sm_80 or newer: skipped\n"); return 77; } + // gfx12: only the native kernels exist (fused::available() is the Q2_0 pack's), so they decide there + if (!fused::available() && !fused::native_supported(GGML_TYPE_IQ3_XXS, GGML_TYPE_IQ4_NL)) { + std::printf("the fused kernels need sm_80 or newer (or gfx11 / gfx12 matrix cores): skipped\n"); + return 77; + } if (!mmq::built()) { std::printf("no MMQ in this build\n"); return 1; } const std::vector pairs = { {GGML_TYPE_IQ2_S, GGML_TYPE_Q2_0, "IQ2_S / Q2_0"}, {GGML_TYPE_IQ2_XXS, GGML_TYPE_Q2_0, "IQ2_XXS / Q2_0"}, @@ -648,7 +654,10 @@ int main(int argc, char** argv) { {GGML_TYPE_IQ3_S, GGML_TYPE_IQ4_NL, "IQ3_S / IQ4_NL"}, {GGML_TYPE_IQ4_XS, GGML_TYPE_Q2_0, "IQ4_XS / Q2_0"}, // (HIP only) UD-Q4_K_XL's: 47 layers Q4_K / Q5_1, 4 Q4_K / Q8_0, and the one Q5_K layer {GGML_TYPE_Q4_K, GGML_TYPE_Q5_1, "Q4_K / Q5_1"}, {GGML_TYPE_Q4_K, GGML_TYPE_Q8_0, "Q4_K / Q8_0"}, - {GGML_TYPE_Q5_K, GGML_TYPE_Q5_1, "Q5_K / Q5_1"}, {GGML_TYPE_Q5_K, GGML_TYPE_Q8_0, "Q5_K / Q8_0"}}; + {GGML_TYPE_Q5_K, GGML_TYPE_Q5_1, "Q5_K / Q5_1"}, {GGML_TYPE_Q5_K, GGML_TYPE_Q8_0, "Q5_K / Q8_0"}, + // the Coder IQ1_M pack's other pairs (appended: the default timing set takes pairs 0, 1, 4, 3 by index) + {GGML_TYPE_IQ2_S, GGML_TYPE_IQ4_NL, "IQ2_S / IQ4_NL"}, {GGML_TYPE_IQ3_XXS, GGML_TYPE_Q2_0, "IQ3_XXS / Q2_0"}, + {GGML_TYPE_IQ3_S, GGML_TYPE_Q2_0, "IQ3_S / Q2_0"}, {GGML_TYPE_IQ4_XS, GGML_TYPE_IQ4_NL, "IQ4_XS / IQ4_NL"}}; const char* only = nullptr; // --only=NAME: the pairs whose name contains NAME for (int i = 1; i < argc; ++i) if (std::strncmp(argv[i], "--only=", 7) == 0) only = argv[i] + 7;