prefill: Q4_K / Q5_K gate/up with Q5_1 down on the CUDA fused prompt kernels (opt-in, STRATA_PF_FUSED_KQ=1)

Phase 4, NVIDIA item 4, step A1. UD-Q4_K_XL's experts (Q4_K gate/up in 47 layers, Q5_K in one; Q5_1 down in 43) have no CUDA
fused kernel and run on MMQ (libstrata_kq, STRATA_MMQ_KQ=1 by default on cc >= 8.0). With STRATA_PF_FUSED_KQ=1 (the switch HIP
already has, same name, same meaning; on CUDA default off) the Q4_K / Q5_K gate/up + Q5_1 down layers run on native_kernel on any
sm_80+ card. The decode is the HIP kernels' (load_unit / convert: int8 codes, a scale and a minimum per 32 values); the products
are the i-quants' (int8 mma.sync m16n8k32, int32 sums, FP32 scales and accumulation), plus the minimum term s1 * d_x * (sum of the
activation codes) per 32 values, from the sums quant_act_nat_kernel already stores in the activation blocks' spare bytes 72-79,
as HIP's native_w11_kernel does. The K-quant gate/up epilogue writes the same sums for H (the Q5_1 down reads them). The Q8_0-down
layers (5 of 48) keep MMQ in this commit. No default changes.

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: 594 (static_assert, no NT = 256 K-quant form), 646 (Q5_1 blocks addressed
  like IQ4_NL / Q5_0: a constant per instance, the existing instances' value unchanged), 737-744 (the activation code sums,
  if constexpr has_min), 825-827 (the minimum term, if constexpr has_min), 871-890 (the gate/up epilogue writes the H code sums,
  if constexpr has_min; the existing instances take the unchanged else branch).
- DevInfo kq / occ_kq: 1743-1744, inside #if !defined(__HIPCC__).
- kq_on() forward declaration: 1769, in the #if !defined(__HIPCC__) block.
- dev_info(): 1912-1920, the CUDA (#else) branch; the K-quant kernels' own occupancy (occ_kq), so the other formats' grids do
  not change; a failed setup leaves those layers on MMQ.
- gu_covered(): 1973, inside #if !defined(__HIPCC__). d_covered_on(): 2035, the CUDA (#else) branch.
- native_supported(): 2130-2137, inside #if !defined(__HIPCC__), before the Q5_0 rule: a K-quant gate/up only with a Q5_1 down,
  a Q5_1 down only behind a K-quant gate/up; not with the NT = 256 / warp-specialized opt-ins.
- experts_native(Batch): 2298-2312, inside #if !defined(__HIPCC__) (a HIP build gains no K-quant instance of native_kernel).
  experts_native(WideBatch): 2340-2341, CUDA-only (refuses K-quant / Q5_1 like Q5_0).
The shared decode helpers (load_unit / convert / has_min / raw_words) are not edited.

cmake/backends/cuda.cmake: ctest prefill_fused_iq_test_kq (STRATA_PF_FUSED_KQ=1 STRATA_MMQ_KQ=1, --only="K / Q5_1" --edge).
data/settings-register.json: STRATA_PF_FUSED_KQ's note names the CUDA meaning.

RTX A6000 (sm_86, CUDA 13.1), prefill_fused_iq_test: Q4_K / Q5_1 and Q5_K / Q5_1 against the int8-rounded model rel RMS
1.5e-7 to 4.2e-5 at the 6 reference shapes, 1.2e-7 to 3.4e-6 on the real-shape samples; against FP32 at MMQ's distance (rel RMS
0.99x to 1.10x MMQ's). The default, q2n, q50 and kq entries pass. Registers: the K-quant instances sit at the 128-register cap
of the 512-thread kernel with 40-72 bytes of stack (Q4_K 64, Q5_K 72, Q5_1 40); speed is not yet MMQ's (opt-in only).
This commit is contained in:
Niko1221 committed 2026-11-10 10:48:47 +01:00
1 parent 8b0988d72f
commit 15a80a9ef6
3 files changed
+77 -3

No files matched your search

+4
View File
@@ -288,6 +288,10 @@ 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
# 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)
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)
# the Unsloth v3.1 sets' expert formats through MMQ (Q5_0 down only in a STRATA_ORCA_Q4KS_MMQ build), and --bench
+1 -1
View File
@@ -25564,7 +25564,7 @@
"last_date": "2026-11-05",
"status": "opt-in",
"owner": "prefill",
"note": "[reviewed] Documented: K-quant packs take fused experts only with this set. [docs/STRIX_HALO.md:228]",
"note": "[reviewed] Documented: K-quant packs take fused experts only with this set. [docs/STRIX_HALO.md:228] CUDA (phase 4, opt-in, default off): Q4_K / Q5_K gate/up with a Q5_1 down on the fused native kernels on any sm_80+ card instead of MMQ.",
"retire_after": "",
"retired_in": ""
},
+72 -2
View File
@@ -591,6 +591,7 @@ native_kernel(const BatchOf<NT> 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");
constexpr int RAW = 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
constexpr int NFB = (GU ? GU_ROWS_N : D_ROWS_N) / WR; // weight-row blocks per tile
@@ -642,7 +643,7 @@ native_kernel(const BatchOf<NT> 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) ? 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) ? 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):
@@ -733,6 +734,14 @@ native_kernel(const BatchOf<NT> b, const NativeGeom geo, const Tables tb, const
for (int n = 0; n < 2; ++n)
#pragma unroll
for (int cc = 0; cc < 2; ++cc) dx[n][cc] = F16 ? float2{} : *(const float2*) (sa + (nb0 + 8 * n + 2 * tig + cc) * AB + 64);
// (the formats with a minimum, CUDA K-quant opt-in) the activation codes' sums per 32 values, bytes 72-79
float2 sx[2][2];
if constexpr (has_min(WT)) {
#pragma unroll
for (int n = 0; n < 2; ++n)
#pragma unroll
for (int cc = 0; cc < 2; ++cc) sx[n][cc] = *(const float2*) (sa + (nb0 + 8 * n + 2 * tig + cc) * AB + 72);
}
if constexpr (F16) {
#pragma unroll
for (int h = 0; h < 2; ++h) {
@@ -813,6 +822,9 @@ native_kernel(const BatchOf<NT> b, const NativeGeom geo, const Tables tb, const
for (int q = 0; q < 4; ++q) {
const float p = (q < 2 ? wa0 : wb0) * (h ? dx[n][q & 1].y : dx[n][q & 1].x);
acc[i][n][q] = fmaf(p, dotf(d[q]), acc[i][n][q]);
if constexpr (has_min(WT)) // w = s0 q + s1: + s1 d_x (sum of the codes), as native_w11_kernel
acc[i][n][q] = fmaf((q < 2 ? wa1 : wb1) * (h ? dx[n][q & 1].y : dx[n][q & 1].x),
h ? sx[n][q & 1].y : sx[n][q & 1].x, acc[i][n][q]);
}
}
}
@@ -856,6 +868,26 @@ native_kernel(const BatchOf<NT> b, const NativeGeom geo, const Tables tb, const
for (int o = 4; o < 32; o <<= 1) am = fmaxf(am, __shfl_xor_sync(0xffffffffu, am, o));
const float inv = am > 0.0f ? 127.0f / am : 0.0f;
const int r = nb0 + 8 * n + 2 * tig + c;
if constexpr (has_min(WT)) {
// (K-quant gate/up, STRATA_PF_FUSED_KQ=1, CUDA) the down formats with a minimum (Q5_1) read the codes' sum
// per 32 values from the H block's spare bytes 72-79, as quant_act_nat_kernel writes them for the
// activations and HIP's native_w11_kernel for H: the same codes, summed over the warp's 32 features
int code[4], cs = 0;
#pragma unroll
for (int i = 0; i < 4; ++i) { code[i] = __float2int_rn(h[i] * inv); cs += code[i]; }
#pragma unroll
for (int o = 4; o < 32; o <<= 1) cs += __shfl_xor_sync(0xffffffffu, cs, o);
if (r < nrows) {
uint8_t* o = out + (size_t) (row0 + r) * H_ROW_BYTES + (blk >> 1) * AB;
const int hh = blk & 1;
#pragma unroll
for (int i = 0; i < 4; ++i) o[32 * hh + 8 * i + g] = (uint8_t) (int8_t) code[i];
if (g == 0) {
*(float*) (o + 64 + 4 * hh) = am / 127.0f;
*(float*) (o + 72 + 4 * hh) = (float) cs;
}
}
} else
if (r < nrows) {
uint8_t* o = out + (size_t) (row0 + r) * H_ROW_BYTES + (blk >> 1) * AB;
const int hh = blk & 1;
@@ -1708,6 +1740,8 @@ struct DevInfo {
#if !defined(__HIPCC__)
bool q50 = false; // sm_80+ with STRATA_PF_FUSED_Q50=1: the Q5_0 down kernels are set up (opt-in, phase 4 step 1)
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)
#endif
};
std::mutex g_mu;
@@ -1732,6 +1766,7 @@ bool q50_cuda_requested() {
static const bool on = [] { const char* v = std::getenv("STRATA_PF_FUSED_Q50"); return v != nullptr && v[0] == '1'; }();
return on;
}
bool kq_on(); // STRATA_PF_FUSED_KQ (below): on CUDA "1" sets the K-quant kernels up (dev_info)
template <int T, bool GU, int WW, int NT = THREADS, bool F16 = false> bool setup_ww(int& occ) {
cudaFuncAttributes fa{};
if (cudaFuncGetAttributes(&fa, native_kernel<T, GU, WW, NT, F16>) != cudaSuccess || fa.ptxVersion < 75) return false;
@@ -1874,6 +1909,15 @@ const DevInfo& dev_info() {
// FP32 scales and sums). Their occupancy is their own (occ_q50), so the other formats' launches do not change; a failed
// setup leaves those layers on the FP16 path.
if (int o3 = 1 << 20; d.ok && q50_cuda_requested() && setup_one<T_Q5_0, false>(o3)) { d.q50 = true; d.occ_q50 = o3; }
// STRATA_PF_FUSED_KQ=1 (opt-in on CUDA, off by default; the HIP switch's name and meaning): the K-quant experts of UD-Q4_K_XL -
// gate/up Q4_K / Q5_K, down Q5_1 - on native_kernel instead of MMQ (libstrata_kq). The decode is the HIP kernels' (load_unit /
// convert: int8 codes, a scale and a minimum per 32 values); the minimum enters as s1 d_x (sum of the activation codes), from the
// sums quant_act_nat_kernel and the K-quant gate/up epilogue store in the spare bytes. Their own occupancy (occ_kq); a failed
// setup leaves those layers on MMQ.
if (int o4 = 1 << 20; d.ok && kq_on() && setup_one<T_Q4_K, true>(o4) && setup_one<T_Q5_K, true>(o4) && setup_one<T_Q5_1, false>(o4)) {
d.kq = true;
d.occ_kq = o4;
}
if (d.ok && nt256_requested()) {
int o2 = 1 << 20;
const bool ok2 = setup_nt<T_IQ2_XXS, true>(o2) && setup_nt<T_IQ2_XS, true>(o2) && setup_nt<T_IQ2_S, true>(o2) &&
@@ -1926,6 +1970,7 @@ bool gu_covered(int t) {
return t == T_IQ2_XXS || t == T_IQ2_XS || t == T_IQ2_S || t == T_IQ3_XXS || t == T_IQ3_S || t == T_IQ4_XS
#if !defined(__HIPCC__)
|| (t == T_Q2_0 && (dev_info().t75 || dev_info().q2n)) // Q2_0 gate/up: the Turing opt-in, or STRATA_PF_FUSED_Q2N=1 on sm_80+
|| ((t == T_Q4_K || t == T_Q5_K) && dev_info().kq) // STRATA_PF_FUSED_KQ=1 and its kernels set up
#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)
@@ -1987,6 +2032,7 @@ bool d_covered_on(int t, const DevInfo& d) {
if (t == T_Q8_0 && !d.w12 && (f16::requested() || q8d_f16_default(d))) return true;
#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)
#endif
(void) d;
return d_covered(t);
@@ -2081,6 +2127,14 @@ bool native_supported(int gu_type, int d_type) {
if (off || env == 0 || !gu_covered(gu_type)) return false;
if (strata::prefill::pf_fused_override().load(std::memory_order_relaxed) == 0) return false; // the small-card prompt path (pf_fused_override.hpp)
#if !defined(__HIPCC__)
// CUDA, a K-quant gate/up (phase 4, opt-in): with STRATA_PF_FUSED_KQ=1 a Q4_K / Q5_K gate/up with a Q5_1 down takes the fused
// 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) {
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;
}
// 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).
// Not with the NT = 256 / warp-specialized opt-ins (their kernels have no Q5_0 form): those layers keep the FP16 path.
@@ -2241,6 +2295,21 @@ void experts_native(const Batch& b, const NativeGeom& g, int n_expert, int64_t n
const int64_t tiles = (n + kTileRows - 1) / kTileRows + (b.e1 - b.e0);
const unsigned g_gu = (unsigned) std::min<int64_t>(tiles * (GU_ROWS_N / weight_rows(ww)), (int64_t) d.sms * d.occ);
const unsigned g_d = (unsigned) std::min<int64_t>(tiles * (D_ROWS_N / weight_rows(ww)), (int64_t) d.sms * d.occ);
#if !defined(__HIPCC__) // (a HIP build returns above; no K-quant instance of native_kernel in its code objects)
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<int64_t>(tiles * (GU_ROWS_N / weight_rows(ww)), (int64_t) d.sms * d.occ_kq);
const unsigned k_d = (unsigned) std::min<int64_t>(tiles * (D_ROWS_N / weight_rows(ww)), (int64_t) d.sms * d.occ_kq);
if (g.d_type != T_Q5_1) {
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<T_Q4_K, true>(ww, k_gu, b, g, tb, xa, src, ha, nullptr, s);
else launch<T_Q5_K, true>(ww, k_gu, b, g, tb, xa, src, ha, nullptr, s);
launch<T_Q5_1, false>(ww, k_d, b, g, tb, ha, src, nullptr, dm, s);
ck(cudaGetLastError(), "experts_native");
return;
}
#endif
switch (g.gu_type) {
case T_IQ2_XXS: launch<T_IQ2_XXS, true>(ww, g_gu, b, g, tb, xa, src, ha, nullptr, s); break;
case T_IQ2_XS: launch<T_IQ2_XS, true>(ww, g_gu, b, g, tb, xa, src, ha, nullptr, s); break;
@@ -2268,7 +2337,8 @@ void experts_native(const WideBatch& b, const NativeGeom& g, int n_expert, int64
if (b.e1 <= b.e0 || n <= 0) return;
const DevInfo& d = dev_info();
#if !defined(__HIPCC__)
const bool q50 = g.d_type == T_Q5_0; // (no Q5_0 form of the NT = 256 kernels: native_supported keeps those layers off this path)
// (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;
#else
constexpr bool q50 = false;
#endif