diff --git a/docs/AMD_HIP.md b/docs/AMD_HIP.md index 661152f6..11448a3d 100644 --- a/docs/AMD_HIP.md +++ b/docs/AMD_HIP.md @@ -419,7 +419,7 @@ an RX 9070 XT 16 GB and a Radeon AI PRO R9700 32 GB (both gfx1201), a Ryzen 9 39 nightly); a user's repeated A/B/A/B with setup's TheRock 7.10 wheels and an i5-12600K measured +1-4%, inside a 12-20% run-to-run spread (#432) - how much it gains depends on how much of decode the router is on that setup. `STRATA_HIP_ROUTER_OLD=1` runs the portable kernel. - - `STRATA_HIP_WMMA=1` (opt-in, gfx12 only, int8 KV): the prompt path's QSA attention on RDNA4 matrix cores (the engine prints a one-line hint about it at start on gfx12 with `--kv int8`; it never enables it, because the output bits change; setting the variable to anything, 0 included, silences the hint) + - `STRATA_HIP_WMMA` (on by default on gfx12 with an int8 KV since 148-amd, `STRATA_HIP_WMMA=0` turns it off; gfx11: opt-in with `=1`): the prompt path's QSA attention on RDNA4 matrix cores (the engine says so once, at the first prompt) (`v_wmma_f32_16x16x16_f16`, FP16 hi + lo halves like the CUDA tensor-core kernel; `hip_prompt_attn_wmma` bounds it against the FP32 kernel and FP64). 7.2-7.5x the portable kernel; R9700 prompts 4K 1,784 -> 2,427 tok/s, 16K 1,797 -> 2,700 (a user with TheRock 7.10: +29-34%, #432). Not bitwise: greedy text differs from token ~50 on, as with the CUDA tensor-core attention. With it @@ -434,6 +434,12 @@ an RX 9070 XT 16 GB and a Radeon AI PRO R9700 32 GB (both gfx1201), a Ryzen 9 39 1,990 at 4,210 (+17.1%), medians 1,571 -> 2,064 and a 1.27x geometric mean over those three. decode 89.8 -> 86.9 tok/s, unchanged. `STRATA_SELECT_WMMA=1` on top changed nothing at those lengths (2,026 tok/s); its +1.5% is a 16K/262K-ctx number. `hip_prompt_attn_wmma` passes on that card. + **Default on gfx12 (148-amd).** Quality on an R9700 (Q2_0, int8 KV, fixed expert cache so that the default against itself is bit-identical): + teacher-forced KL against the old kernel through the real serve decode path (STRATA_FOLLOW, 28 prompts, 6,024 positions) 0.0003 (p99 0.006, top-1 + 99.8%, perplexity of the forced text x1.0003), and 0.0014 over 1,760 positions of eight 6K-token documents (top-1 98.9%, x1.0009); HumanEval 157/164 + (default 156 and 156), reasoning 25/30 (24 and 26), agent 6/6, needles at 32K and 125K x three depths 6/6, CJK 8/8, tool calls 5/5. Prompt speed, + interleaved pairs on a quiet R9700, Q2_0 32 GB: see the 148-amd table below. The first-token KL against the FP16 prompt path cannot rank it (0.0296 against 0.0280 for the old + kernel, a paired difference of +0.0016 +- 0.0026): on a 2-bit pack any change of the prompt numerics, even `--prefill 4096`, moves the first token by about 0.025. - The prompt path's QSA top-k picks its kernel by the blocks a query actually has, not the cache's capacity (#337, bsorensen110): the same ids, on by default with AMD (NVIDIA keeps its capacity rule: there the 64K prompts read 1-3% slower with it). `STRATA_SELECT_WMMA=1` (opt-in, gfx12) adds #337's @@ -452,6 +458,17 @@ an RX 9070 XT 16 GB and a Radeon AI PRO R9700 32 GB (both gfx1201), a Ryzen 9 39 prompt's top token was the same one, so that check says little); 4 of 4 long-context retrieval checks passed. 0.1.42 re-check on an R9700 (Linux, ROCm 7.14), 12 prompts of 1.5K-24K tokens, first-token KL against the FP16 prompt path (MMQ / fused): IQ3_S 0.00332 / 0.00284, top-1 12 of 12 for both; IQ3_XXS 0.00335 / 0.00452 on one sample and 0.00344 / 0.00323 on a second (24 prompts pooled: 0.00340 / 0.00388, top-1 24 of 24 for both); the Q2_0 pack does not take these kernels on gfx12 (output bit-identical to MMQ). Because the IQ3_XXS pool is not at or below MMQ's, the flag stays opt-in. Measured on Linux only. gfx1200 builds the same kernels and was not run; Windows and K-quant packs (`STRATA_PF_FUSED_KQ=1`) were not measured. + **Q2_0 gate/up (148-amd).** The native Q2_0 pack's gate/up (64-value blocks) take the same kernels and are in the gfx12 default set with IQ3_S and + IQ3_XXS (`STRATA_PF_FUSED=0` keeps MMQ). One layer, 2,048 tokens x top 10 over 512 experts: MMQ 24.5 ms, fused 4.4 ms; 8,192 tokens 31.7 vs 13.2 ms + (`hip_prefill_fused_iq`, fused against MMQ rel. RMS 1.6e-5). Engine, R9700 Q2_0 32 GB, 5 interleaved pairs: prompt 4K +16.7%, 20K +19.0% (noise arm 1.001 / 0.999). + Quality: teacher-forced KL against MMQ 0.0004 (6,022 positions, top-1 99.5%) and 0.0016 (1,760 long-document positions, top-1 98.2%), the same tasks as + above unchanged (156/164, 24/30, 6/6, 6/6, 8/8, 5/5). IQ2_XXS / IQ2_XS / IQ2_S gate/up stay opt-in on gfx12. + **Both defaults together (148-amd), R9700, interleaved starts of one engine, prompt tok/s** (base = the build before them; the new paths change + bits, so decode texts differ from the first differing token on and decode is compared with both switched off instead: 95.23 vs 95.28, same ids): + Q2_0 32 GB, 5 pairs: 4K 1,151 -> 2,286 (a slower start mode gives 1,988), 20K 1,384 -> 2,751 (2,481); with one lever each (3 pairs): WMMA 4K x1.35 / 20K x1.46, + Q2_0 fused experts x1.28 / x1.19, both x1.73 / x1.79. IQ3_XXS 32 GB, 5 pairs (only the attention is new): 4K 1,369 -> 1,436 (the base starts in a 1,170 or a + 1,370 mode), 20K 1,576 -> 1,997 (x1.27). The identity gate with both switched off (STRATA_HIP_WMMA=0, STRATA_PF_FUSED=0): 10 of 10 prompts and the + 4K / 20K residual dumps identical on both packs. - **Known:** rarely (about 1 start in 10) a HIP run's greedy output differs from another start's at some token, on one card or two and on engine 0.1.29 as well; not yet explained. - **Not validated:** images, long contexts beyond 16K, answer-quality benchmarks. diff --git a/include/strata/kernels/qsa_prompt_attn.hpp b/include/strata/kernels/qsa_prompt_attn.hpp index 046cd6fc..22710d91 100644 --- a/include/strata/kernels/qsa_prompt_attn.hpp +++ b/include/strata/kernels/qsa_prompt_attn.hpp @@ -29,6 +29,11 @@ namespace strata::kernels { bool qsa_prompt_attn_batch(const float* q, const QsaAttnPools& pools, const int32_t* ids, const int32_t* steps, int64_t cap, const QsaShapes& s, float* attn, int64_t n_q, void* stream); +/// HIP only (false in every other build): the prompt attention runs on the RDNA4 matrix cores on the current device - +/// the default on gfx12 (STRATA_HIP_WMMA=0 turns it off), STRATA_HIP_WMMA=1 on gfx11 as well. The prompt path sizes its +/// expert ring for it (prefill.cpp). +bool qsa_hip_wmma_active(); + /// STRATA_QSA_SPLIT_ATTN=1 (the idea of fork Eddoursul fd23d91): `qsa_decode_attn_batch`'s decode /// attention through the int8 tensor-core kernel above with each query's cells split among up to 20 blocks, merged with /// the log-sum-exp rescale, its partials in `scratch` (`scratch_floats` available). False (nothing diff --git a/src/kernels/cuda/qsa_prompt_attn.cu b/src/kernels/cuda/qsa_prompt_attn.cu index 1f18b9c6..f5397e65 100644 --- a/src/kernels/cuda/qsa_prompt_attn.cu +++ b/src/kernels/cuda/qsa_prompt_attn.cu @@ -2175,30 +2175,33 @@ __global__ void __launch_bounds__(THREADS) prompt_attn_wmma_kernel(const float* #endif } -// gfx12 (RDNA4) and gfx11 (RDNA3 / RDNA3.5), only on request: the output differs from the default kernel's in its -// last bits +// gfx12 (RDNA4): on by default (148-amd; STRATA_HIP_WMMA=0 turns it off). gfx11 (RDNA3 / RDNA3.5): only on request +// (STRATA_HIP_WMMA=1). The output differs from the old kernel's in its last bits (FP32-level accuracy, another summation +// order): teacher-forced KL against the old kernel 0.0003 (28 prompts) / 0.0014 (8 long documents) on an R9700. bool hip_wmma_usable() { - static const bool want = [] { + static const int env = [] { // -1 unset, 0 off, 1 on const char* e = std::getenv("STRATA_HIP_WMMA"); - return e != nullptr && e[0] == '1'; + return e == nullptr || e[0] == '\0' ? -1 : (e[0] == '1' ? 1 : 0); }(); - if (!want) return false; - static int arch[64] = {}; // 0 unknown, 1 gfx11 / gfx12, 2 other + if (env == 0) return false; + static int arch[64] = {}; // 0 unknown, 1 gfx12, 2 other, 3 gfx11 matrix cores int dev = 0; if (hipGetDevice(&dev) != hipSuccess || dev < 0 || dev >= 64) { (void) hipGetLastError(); return false; } if (arch[dev] == 0) { hipDeviceProp_t prop{}; if (hipGetDeviceProperties(&prop, dev) != hipSuccess) { (void) hipGetLastError(); return false; } - arch[dev] = std::strncmp(prop.gcnArchName, "gfx12", 5) == 0 || strata::kernels::gfx_arch_is_gfx11_wmma(prop.gcnArchName) - ? 1 : 2; + arch[dev] = std::strncmp(prop.gcnArchName, "gfx12", 5) == 0 ? 1 + : strata::kernels::gfx_arch_is_gfx11_wmma(prop.gcnArchName) ? 3 : 2; static bool told = false; - if (!told) { + if (!told && (env == 1 || arch[dev] == 1)) { told = true; - std::fprintf(stderr, "strata: STRATA_HIP_WMMA: the prompt attention on matrix cores %s (%s)\n", - arch[dev] == 1 ? "on" : "unavailable", prop.gcnArchName); + const bool on = arch[dev] == 1 || (arch[dev] == 3 && env == 1); + std::fprintf(stderr, "strata: %sthe prompt attention on matrix cores %s (%s)%s\n", env == 1 ? "STRATA_HIP_WMMA: " : "", + on ? "on" : "unavailable", prop.gcnArchName, + env == 1 ? "" : " by default; STRATA_HIP_WMMA=0 turns it off"); } } - return arch[dev] == 1; + return arch[dev] == 1 || (arch[dev] == 3 && env == 1); } bool launch_wmma(const float* q, const QsaAttnPools& pools, const int32_t* ids, const int32_t* steps, int64_t cap, @@ -2284,6 +2287,14 @@ bool qsa_decode_attn_split(const float* q, const QsaAttnPools& pools, const int3 } void qsa_prompt_attn_set_imma(int on) { g_pa_imma = on < 0 ? -1 : (on ? 1 : 0); } +bool qsa_hip_wmma_active() { +#if defined(__HIPCC__) + return hip_wmma_usable(); +#else + return false; +#endif +} + bool qsa_prompt_attn_batch(const float* q, const QsaAttnPools& pools, const int32_t* ids, const int32_t* steps, int64_t cap, const QsaShapes& s, float* attn, int64_t n_q, void* stream) { if (n_q <= 0) return true; diff --git a/src/prefill/moe_fused_iq.cu b/src/prefill/moe_fused_iq.cu index 4e3dc24e..6eeb21db 100644 --- a/src/prefill/moe_fused_iq.cu +++ b/src/prefill/moe_fused_iq.cu @@ -858,10 +858,10 @@ native_w11_kernel(const Batch b, const NativeGeom geo, const Tables tb, const ui const uint8_t* wrow = GU ? blob + ((ur & 1) ? geo.up_off : 0) + (size_t) (fb * (NW_ROWS / 2) + (ur >> 1)) * geo.gu_row : blob + geo.down_off + (size_t) (rbase + ur) * geo.d_row; auto unit = [&](int s) -> const uint8_t* { - if (GU) return wrow + (s >> 2) * BS; + if (GU && WT != T_Q2_0) return wrow + (s >> 2) * BS; // (Q2_0 gate/up: 64-value blocks, one a stage, like its down) return (WT == T_IQ4_NL || WT == T_Q5_1 || WT == T_Q8_0) ? wrow + (2 * s + uj) * BS : wrow + s * BS; }; - auto sub = [&](int s) { return GU ? 2 * (s & 3) + uj : uj; }; + auto sub = [&](int s) { return GU && WT != T_Q2_0 ? 2 * (s & 3) + uj : uj; }; auto put = [&](const uint32_t (&raw)[raw_words(WT)], int buf) { uint32_t q[8]; float s0, s1; @@ -1334,6 +1334,7 @@ bool gu_covered(int t) { || (t == T_Q2_0 && dev_info().t75) // the canonical Q2_0 pack's gate/up: Turing opt-in only (sm_80+ keeps MMQ for it) #endif #if defined(__HIPCC__) + || (t == T_Q2_0 && dev_info().w12) // gfx12: the native Q2_0 pack's gate/up on native_w11_kernel (64-value blocks) || ((t == T_Q4_K || t == T_Q5_K) && kq_on()) // the gfx11 kernels only #endif ; @@ -1393,8 +1394,9 @@ bool native_supported(int gu_type, int d_type) { }(); // 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. - // unset: on by default on gfx12 for IQ3_S and IQ3_XXS gate/up only (R9700: prompt +13..+20%, first-token KL against the FP16 prompt - // path equal to MMQ's within noise, 96 prompts each); STRATA_PF_FUSED=0 turns it off, =1 keeps the opt-in for every covered format + // unset: on by default on gfx12 for IQ3_S, IQ3_XXS and (148-amd) Q2_0 gate/up (R9700: prompt +13..+20%, first-token KL against the FP16 + // prompt path equal to MMQ's within noise, 96 prompts each; Q2_0: prompt 4K +16.7%, 20K +19.0%, teacher-forced KL 0.0004 / 0.0016, + // tasks equal to MMQ's); STRATA_PF_FUSED=0 turns it off, =1 keeps the opt-in for every covered format static const int env = [] { const char* v = std::getenv("STRATA_PF_FUSED"); return v == nullptr || v[0] == '\0' ? -1 : (v[0] == '1' ? 1 : 0); @@ -1415,7 +1417,7 @@ bool native_supported(int gu_type, int d_type) { return d.ok && d.s120 && d.sms >= 80 && !d.t75 && d_covered_on(d_type, d); } #endif - if (env < 0 && gu_type != T_IQ3_S && gu_type != T_IQ3_XXS) return false; // default: only these two + if (env < 0 && gu_type != T_IQ3_S && gu_type != T_IQ3_XXS && gu_type != T_Q2_0) return false; // default: only these three const DevInfo& d = dev_info(); if (!d.ok || !d_covered_on(d_type, d)) return false; if (d.t75 && (gu_type != T_Q2_0 || d_type != T_Q2_0)) return false; @@ -1452,7 +1454,7 @@ void experts_native(const Batch& b, const NativeGeom& g, int n_expert, int64_t n const bool occ_gu = false, occ_d = false; #else const bool occ_gu = g.gu_type == T_IQ2_XXS || g.gu_type == T_IQ2_XS || g.gu_type == T_IQ2_S || - g.gu_type == T_IQ3_XXS || g.gu_type == T_IQ3_S || g.gu_type == T_IQ4_XS; + g.gu_type == T_IQ3_XXS || g.gu_type == T_IQ3_S || g.gu_type == T_IQ4_XS || g.gu_type == T_Q2_0; const bool occ_d = g.d_type == T_Q2_0 || g.d_type == T_Q8_0 || g.d_type == T_IQ4_NL; #endif if (d.w12 && (reinterpret_cast(dm) & 15) != 0) { // gfx12's down store: two float4 per lane @@ -1494,6 +1496,7 @@ void experts_native(const Batch& b, const NativeGeom& g, int n_expert, int64_t n case T_IQ2_S: STRATA_NW_GU(T_IQ2_S); break; case T_IQ3_XXS: STRATA_NW_GU(T_IQ3_XXS); break; case T_IQ3_S: STRATA_NW_GU(T_IQ3_S); break; + case T_Q2_0: STRATA_NW_GU(T_Q2_0); break; case T_Q4_K: STRATA_NW_GU(T_Q4_K); break; case T_Q5_K: STRATA_NW_GU(T_Q5_K); break; default: STRATA_NW_GU(T_IQ4_XS); break; diff --git a/src/prefill/prefill.cpp b/src/prefill/prefill.cpp index 5fee9fe9..f145736f 100644 --- a/src/prefill/prefill.cpp +++ b/src/prefill/prefill.cpp @@ -308,13 +308,9 @@ inline int ring_budget_slots() { inline int ring_slots(size_t T) { const char* v = std::getenv("STRATA_PREFILL_RING"); #if defined(STRATA_USE_HIP) - // S6: with the opt-in RDNA4 matrix-core attention (STRATA_HIP_WMMA=1) a 96-slot ring: measured with it, 9070 XT - // 4K prompts 718 -> 1,211 tok/s (16K 1,949 -> 2,032), R9700 4K 2,426 -> 2,483 (16K the same) - static const bool wmma = [] { - const char* e = std::getenv("STRATA_HIP_WMMA"); - return e != nullptr && e[0] == '1'; - }(); - if (!v && g_ring_override <= 0 && wmma) return (int64_t) T >= stream_all_min() ? 96 : STAGE; + // S6: with the RDNA4 matrix-core attention (the gfx12 default since 148-amd; STRATA_HIP_WMMA=1 elsewhere) a 96-slot + // ring: measured with it, 9070 XT 4K prompts 718 -> 1,211 tok/s (16K 1,949 -> 2,032), R9700 4K 2,426 -> 2,483 (16K the same) + if (!v && g_ring_override <= 0 && strata::kernels::qsa_hip_wmma_active()) return (int64_t) T >= stream_all_min() ? 96 : STAGE; #endif // the byte budget as this pack's slots: 1024 fused / 384 not on Q2_0, fewer on a pack with bigger blobs. The // unpinned arm stays a slot count (96): it was measured where the host copies are the limit, and there the ring diff --git a/src/program/generate.cpp b/src/program/generate.cpp index 4af80ab3..b9d60162 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -5373,13 +5373,8 @@ int main(int argc, char** argv) { const char* name = named && p.name[0] ? p.name : "(an unnamed GPU)"; #if defined(STRATA_USE_HIP) std::fprintf(stderr, "strata generate: GPU %d: %s (%s)\n", dev, name, named ? p.gcnArchName : "?"); - // A hint only, never an action: on RDNA4 with an int8 KV the opt-in matrix-core prompt attention reads prompts - // about 30% faster (R9700, PR #1318), but its output differs from the default kernel's in the last bits, so it - // stays the user's choice. Silent when the variable is set to anything (STRATA_HIP_WMMA=0 included). - if (named && o.kv == "int8" && std::strncmp(p.gcnArchName, "gfx12", 5) == 0 && std::getenv("STRATA_HIP_WMMA") == nullptr) - std::fprintf(stderr, "strata generate: hint: STRATA_HIP_WMMA=1 reads prompts about 30%% faster on this card with " - "--kv int8 (prompt attention on the matrix cores); it is off by default because the output " - "bits change (last-place rounding). Nothing was enabled.\n"); + // (RDNA4's matrix-core prompt attention is the default since 148-amd: the engine says so once, at its first prompt; + // STRATA_HIP_WMMA=0 turns it off.) #if defined(_WIN32) // #468 #461: which HIP runtime was loaded - the bundled one beside the exe, or an AMD driver's System32 copy if (HMODULE h = GetModuleHandleA("amdhip64_7.dll")) {