From 1bf13cd02f56d8c121dfb9da2dc2cd874123b751 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Tue, 10 Nov 2026 11:36:53 +0100 Subject: [PATCH] prefill: Q8_0 down behind a K-quant gate/up on the CUDA fused prompt kernels (opt-in, STRATA_PF_FUSED_KQ=1) Phase 4, NVIDIA item 4, step A2, on top of A1b (18027b91c). UD-Q4_K_XL's remaining five layers have a Q8_0 down (4 Q4_K / Q8_0, 1 Q5_K / Q8_0); with STRATA_PF_FUSED_KQ=1 they now take native_kernel too, so the whole set reads its prompt experts without MMQ. The decode is the HIP kernels' (load_unit / convert: int8 codes, one scale per 32 values, no minimum); the products are the IQ4_NL / Q5_0 down's (int8 mma.sync m16n8k32, FP32 scales and sums). A Q8_0 down is taken only behind a K-quant gate/up (the i-quant sets' Q8_0-down layers keep their path); opt-in unchanged, default off. Every edit is on a CUDA-only path (line numbers of this commit's src/prefill/moe_fused_iq.cu): - native_kernel, body inside __CUDA_ARCH__ >= 750: 637 (static_assert, no NT = 256 Q8_0 form), 690 (Q8_0 blocks addressed like IQ4_NL / Q5_0 / Q5_1: a constant per instance, the existing instances' value unchanged). - DevInfo kq8 / occ_kq8: 1799-1800, inside #if !defined(__HIPCC__). - dev_info(): 1976-1978, the CUDA (#else) branch, inside the STRATA_PF_FUSED_KQ=1 setup; its own occupancy; a failed setup leaves those layers on MMQ. - d_covered_on(): 2095, the CUDA (#else) branch. - native_supported(): 2194-2197, inside #if !defined(__HIPCC__) (the K-quant rule: a Q8_0 down only behind Q4_K / Q5_K). - experts_native(Batch): 2363, 2369-2372, inside #if !defined(__HIPCC__). experts_native(WideBatch): 2405, CUDA-only. The shared decode helpers are not edited. cmake/backends/cuda.cmake: prefill_fused_iq_test_kq takes every K-quant pair (--only="K / Q"). RTX A6000 (sm_86, CUDA 13.1), prefill_fused_iq_test: Q4_K / Q8_0 and Q5_K / Q8_0 against the int8-rounded model rel RMS 2.8e-7 to 1.9e-4 at the 6 reference shapes (lane OWN-4A's gfx12 kernel: 1.1e-4), 2.9e-7 to 2.7e-5 on the real-shape samples; against FP32 0.99x to 1.015x MMQ's own distance; fused against MMQ 3.0e-3 to 4.3e-3 (MMQ's activation-sum arithmetic, information under the K-quant rule of 8b0988d72). One layer, MMQ path / fused (uniform random routing; not the end-to-end measure): Q4_K / Q8_0 2K 12.91 / 9.91 ms (1.30x), 3.5K 14.98 / 13.35 (1.12x), 8K 23.78 / 29.06 (0.82x), 16K 38.24 / 50.77 (0.75x), 32K 66.25 / 99.60 (0.67x). The Q8_0 down instances use 124-126 registers, no stack. The default, q2n, q50 and kq entries pass. --- cmake/backends/cuda.cmake | 4 ++-- src/prefill/moe_fused_iq.cu | 24 +++++++++++++++++------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/cmake/backends/cuda.cmake b/cmake/backends/cuda.cmake index 0c53b93d64..9241d984ed 100644 --- a/cmake/backends/cuda.cmake +++ b/cmake/backends/cuda.cmake @@ -288,9 +288,9 @@ macro(strata_cuda_mmq) # (this build has no Q5_0 MMQ), with the tails and tile boundaries the test adds for every Q5_0 pair (77 below sm_80) add_test(NAME prefill_fused_iq_test_q50 COMMAND prefill_fused_iq_test "--only=Q5_0" --no-timing) set_tests_properties(prefill_fused_iq_test_q50 PROPERTIES ENVIRONMENT "STRATA_PF_FUSED_Q50=1" SKIP_RETURN_CODE 77 TIMEOUT 600) - # the opt-in STRATA_PF_FUSED_KQ=1 (phase 4): UD-Q4_K_XL's Q4_K / Q5_K gate/up with Q5_1 down on the native kernels, against the + # the opt-in STRATA_PF_FUSED_KQ=1 (phase 4): UD-Q4_K_XL's Q4_K / Q5_K gate/up with Q5_1 / Q8_0 down on the native kernels, against the # reference and the int8 model at every shape (--edge), MMQ's K-quant library alongside (STRATA_MMQ_KQ=1; 77 below sm_80) - add_test(NAME prefill_fused_iq_test_kq COMMAND prefill_fused_iq_test "--only=K / Q5_1" --edge --no-timing) + add_test(NAME prefill_fused_iq_test_kq COMMAND prefill_fused_iq_test "--only=K / Q" --edge --no-timing) set_tests_properties(prefill_fused_iq_test_kq PROPERTIES ENVIRONMENT "STRATA_PF_FUSED_KQ=1;STRATA_MMQ_KQ=1" SKIP_RETURN_CODE 77 TIMEOUT 600) # the MoE input quantized per token and scattered to its rows (mmq::quantize_scatter) against the per-row # gather: the same q8_1 bytes for every MMQ layout (--bench: both at 2K, 8K and 32K tokens) diff --git a/src/prefill/moe_fused_iq.cu b/src/prefill/moe_fused_iq.cu index 179c5959ca..baa8b0696e 100644 --- a/src/prefill/moe_fused_iq.cu +++ b/src/prefill/moe_fused_iq.cu @@ -634,7 +634,7 @@ native_kernel(const BatchOf b, const NativeGeom geo, const Tables tb, const constexpr int WT_BYTES = WR * WLDX, WS_FLOATS = F16 ? 0 : WR * 4, ACT_STAGE = TR * AB; constexpr bool TWO_PASS = NT != THREADS; // the two-pass loads (load_unit_l) only in the opt-in NT = 256 kernels static_assert(!(TWO_PASS && WT == T_Q5_0), "the NT = 256 kernels have no Q5_0 form"); - static_assert(!(TWO_PASS && has_min(WT)), "the NT = 256 kernels have no K-quant / Q5_1 form"); + static_assert(!(TWO_PASS && (has_min(WT) || WT == T_Q8_0)), "the NT = 256 kernels have no K-quant / Q5_1 / Q8_0 form"); constexpr bool KLEAN = GU && (WT == T_Q4_K || WT == T_Q5_K); // (A1b) the K-quant gate/up's lean loads (kq_lean_load) constexpr int RAW = KLEAN ? kq_lean_words() : raw_words(WT) > 5 ? raw_words(WT) : 5; // the raw words of a sub-block: 5, Q5_0's 6 constexpr int NS = (GU ? GU_ROWS_K : D_ROWS_K) / 64; // 64-value stages along K @@ -687,7 +687,7 @@ native_kernel(const BatchOf b, const NativeGeom geo, const Tables tb, const constexpr bool GU256 = GU && WT != T_Q2_0; auto unit = [&](int s) -> const uint8_t* { // the block of stage s's sub-block if (GU256) return wrow + (s >> 2) * BS; - return (WT == T_IQ4_NL || WT == T_Q5_0 || WT == T_Q5_1) ? wrow + (2 * s + uj) * BS : wrow + s * BS; // 32-value blocks: two a stage + return (WT == T_IQ4_NL || WT == T_Q5_0 || WT == T_Q5_1 || WT == T_Q8_0) ? wrow + (2 * s + uj) * BS : wrow + s * BS; // 32-value blocks: two a stage }; auto sub = [&](int s) { return GU256 ? 2 * (s & 3) + uj : uj; }; // the weight bytes into L2 ahead of the register loads (one stage ahead only - less than a DRAM round trip): @@ -1796,6 +1796,8 @@ struct DevInfo { int occ_q50 = 1; // their blocks per multiprocessor (kept apart, so the other formats' grids do not change) bool kq = false; // sm_80+ with STRATA_PF_FUSED_KQ=1: the K-quant kernels (Q4_K / Q5_K gate/up, Q5_1 down) are set up (opt-in, phase 4) int occ_kq = 1; // their blocks per multiprocessor (their own, as occ_q50) + bool kq8 = false; // with kq: the Q8_0 down kernels are set up too (A2; UD-Q4_K_XL's five Q8_0-down layers) + int occ_kq8 = 1; #endif }; std::mutex g_mu; @@ -1971,6 +1973,9 @@ const DevInfo& dev_info() { if (int o4 = 1 << 20; d.ok && kq_on() && setup_one(o4) && setup_one(o4) && setup_one(o4)) { d.kq = true; d.occ_kq = o4; + // (A2) the Q8_0 down behind a K-quant gate/up: int8 codes and one scale per 32 values, no minimum (load_unit / convert); + // its own occupancy, and a failed setup leaves those layers on MMQ + if (int o5 = 1 << 20; setup_one(o5)) { d.kq8 = true; d.occ_kq8 = o5; } } if (d.ok && nt256_requested()) { int o2 = 1 << 20; @@ -2087,6 +2092,7 @@ bool d_covered_on(int t, const DevInfo& d) { #else if (t == T_Q5_0) return d.q50; // STRATA_PF_FUSED_Q50=1 and its kernels set up (dev_info) if (t == T_Q5_1) return d.kq; // STRATA_PF_FUSED_KQ=1 (behind a K-quant gate/up only: native_supported) + if (t == T_Q8_0) return d.kq8; // STRATA_PF_FUSED_KQ=1, A2 (behind a K-quant gate/up only: native_supported) #endif (void) d; return d_covered(t); @@ -2185,9 +2191,10 @@ bool native_supported(int gu_type, int d_type) { // kernels on any sm_80+ card whose K-quant kernels were set up, whatever the 80-SM rule says; other downs keep MMQ for now. A Q5_1 // down needs the code sums only the K-quant gate/up epilogue writes, so it is never taken behind another gate/up. Not with the // NT = 256 / warp-specialized opt-ins (no K-quant form): MMQ. - if (gu_type == T_Q4_K || gu_type == T_Q5_K || d_type == T_Q5_1) { + if (gu_type == T_Q4_K || gu_type == T_Q5_K || d_type == T_Q5_1 || d_type == T_Q8_0) { const DevInfo& d = dev_info(); - return (gu_type == T_Q4_K || gu_type == T_Q5_K) && d_type == T_Q5_1 && d.ok && d.kq && !d.t75 && d.occ_nt <= 0 && !d.ws; + return (gu_type == T_Q4_K || gu_type == T_Q5_K) && (d_type == T_Q5_1 || (d_type == T_Q8_0 && d.kq8)) && d.ok && d.kq && !d.t75 && + d.occ_nt <= 0 && !d.ws; } // CUDA, a Q5_0 down (phase 4 step 1, opt-in): with STRATA_PF_FUSED_Q50=1 such a layer takes the fused kernels on any sm_80+ card // whose Q5_0 kernels were set up, whatever the 80-SM rule below says (STRATA_PF_FUSED=0 still turns every fused kernel off, above). @@ -2353,13 +2360,16 @@ void experts_native(const Batch& b, const NativeGeom& g, int n_expert, int64_t n if (g.gu_type == T_Q4_K || g.gu_type == T_Q5_K) { // STRATA_PF_FUSED_KQ=1 (gu_covered / d_covered_on checked d.kq above); their own occupancy const unsigned k_gu = (unsigned) std::min(tiles * (GU_ROWS_N / weight_rows(ww)), (int64_t) d.sms * d.occ_kq); const unsigned k_d = (unsigned) std::min(tiles * (D_ROWS_N / weight_rows(ww)), (int64_t) d.sms * d.occ_kq); - if (g.d_type != T_Q5_1) { + if (g.d_type != T_Q5_1 && !(g.d_type == T_Q8_0 && d.kq8)) { std::fprintf(stderr, "prefill fused experts (native): K-quant gate/up with down type %d is not covered here\n", g.d_type); std::exit(1); } if (g.gu_type == T_Q4_K) launch(ww, k_gu, b, g, tb, xa, src, ha, nullptr, s); else launch(ww, k_gu, b, g, tb, xa, src, ha, nullptr, s); - launch(ww, k_d, b, g, tb, ha, src, nullptr, dm, s); + if (g.d_type == T_Q8_0) { // (A2) its own occupancy + const unsigned k8 = (unsigned) std::min(tiles * (D_ROWS_N / weight_rows(ww)), (int64_t) d.sms * d.occ_kq8); + launch(ww, k8, b, g, tb, ha, src, nullptr, dm, s); + } else launch(ww, k_d, b, g, tb, ha, src, nullptr, dm, s); ck(cudaGetLastError(), "experts_native"); return; } @@ -2392,7 +2402,7 @@ void experts_native(const WideBatch& b, const NativeGeom& g, int n_expert, int64 const DevInfo& d = dev_info(); #if !defined(__HIPCC__) // (no Q5_0 / K-quant / Q5_1 form of the NT = 256 kernels: native_supported keeps those layers off this path) - const bool q50 = g.d_type == T_Q5_0 || g.gu_type == T_Q4_K || g.gu_type == T_Q5_K || g.d_type == T_Q5_1; + const bool q50 = g.d_type == T_Q5_0 || g.gu_type == T_Q4_K || g.gu_type == T_Q5_K || g.d_type == T_Q5_1 || g.d_type == T_Q8_0; #else constexpr bool q50 = false; #endif