prefill: Q5_K and Q6_K down projections on the fused kernels for Qwen3.6 (UD-Q4_K_XL's 41 layers), behind the same opt-in

UD-Q4_K_XL's expert downs are Q5_K (38 layers) and Q6_K (3 layers); the fused decoders covered Q5_1 / Q8_0 downs only, so with STRATA_PF_FUSED=1 the file stayed on
MMQ layer by layer. With K = n_ff = 512 a K-quant down row is exactly two 256-value super-blocks, which native_kernel's gate/up already walks (unit(s) = block s / 4,
sub(s) = 2 (s & 3) + uj); the decode (load_unit / convert for Q5_K and Q6_K, golden-covered) and the minimum term from the H code sums the Q4_K / Q5_K gate/up
epilogue writes (has_min) are the gate/up's too.
 - src/prefill/moe_fused_iq.cu: ONE line, GU256 also true for a Q4_K / Q5_K / Q6_K down whose K (D_ROWS_K) is a multiple of 256. False for every existing instance:
   no K-quant down instance existed, and Flash-Next's n_ff 640 is not a multiple of 256.
 - src/prefill/moe_fused_iq_q36.cu (the second copy only): the two down instances' own set-up (setup_one<T_Q5_K, false> / <T_Q6_K, false>), native_supported_ext
   (the pair rule of the K-quant gate/up, native_supported(gu, T_Q5_1), plus that set-up) and experts_native_ext (the K-quant launch of experts_native with the new
   down; every other pair forwards to the copy's own functions). Declared in moe_fused_q36.hpp; the Qwen3.6 engine and the one-layer test call the _ext names.
 No new setting: it comes with STRATA_PF_FUSED=1 (the K-quant switches the engine already sets). With the opt-in off nothing changes.

RTX A4000 (sm_86, CUDA 12.9), z440-20261111-2357-e3b0, the one-layer test with the pairs Q4_K / Q5_K, Q4_K / Q6_K, Q5_K / Q6_K (five shapes each, MMQ beside them,
the K-quant rule of the file): passed. Fused against the int8 model rel RMS <= 2.6e-6, worst row <= 5.7e-3 (limits 2e-3 / 3e-2); fused against FP32 equal to MMQ's
distance (Q4_K / Q5_K 9.004e-3 against 8.990e-3); fused against MMQ rel RMS 4.3e-4 to 7.5e-4 (Q4_K / Q5_K), 3.1e-3 to 4.3e-3 (the Q6_K downs). The output hash is the
same at tile 64, tile 128 and the default for every pair at 2048 and 8192 tokens. compute-sanitizer memcheck over all five pairs (--no-ref, 2048): 0 errors. One
layer, MMQ / fused ms, default tile: Q4_K / Q5_K 2048 tokens 7.647 / 8.088, 8192 tokens 18.544 / 26.318; Q4_K / Q6_K 8.132 / 8.167 and 18.992 / 31.883; Q5_K /
Q6_K 8.682 / 9.462 and 19.581 / 38.300 (tile 128 at 8192 tokens: 21.249, 22.415, 24.875). The gap at 8K is the staging / tile question of the other kernels, not
settled here.

Device code, strata-q36 / strata / libstrata_kq.so, base = the tree without this commit, built twice (A/A floor) beside the head:
 - sm_120 (RTX PRO 5000, CUDA 13.2, l620-20261112-0055-be25): floor 0 differing everywhere; base vs head 0 differing, 4 only in the head (strata-q36 2,843 -> 2,847,
   strata 3,666 -> 3,670; libstrata_kq.so 256, 0 / 0): the new down instances.
 - sm_86 (z440-20261112-0018-ebae, strata-q36 2,843 -> 2,847 functions, 4 only in the head, named by z440-20261112-0056-9297: native_kernel<13, false, 2 / 4, 512>
   and <14, false, 2 / 4, 512> of strata::prefill::fused_q36): no existing native_kernel instance differs. The differing functions are native_mmvq_il_pf_kernel
   instances (native_mmvq.cu) and mul_mat_q<ggml_type 149, 16, true>, which also differ between the two base builds (the known sm_86 build-to-build variation, 7
   in the floor, 8 base vs head; native_mmvq.cu includes none of the files of this commit).
 - HIP: strata and strata-q36 compile for gfx1201 (p620-20261112-0048-93b9, compile only, not run).
Not run: UD-Q4_K_XL end to end (the file is not on a box with the commit; z440 would be hybrid and streaming-bound), the clean merge against e14d4e370 / f-p4-sm's tip
beyond a textual one (git merge-tree: no conflicts, not compiled).
This commit is contained in:
Niko1221 committed 2026-11-12 01:30:50 +01:00
1 parent a041fd038c
commit 7bc09fadf0
5 files changed
+77 -5

No files matched your search

+5
View File
@@ -29,6 +29,11 @@ void quantize_act_native(const float* x, int64_t rows, int64_t cols, void* xa, v
/// One layer's experts [b.e0, b.e1): see moe_fused_iq.hpp's experts_native.
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);
/// native_supported / experts_native plus the K-quant downs of UD-Q4_K_XL (a Q4_K or Q5_K gate/up with a Q5_K or Q6_K down: K = 512 is two 256-value
/// super-blocks), which the copy of moe_fused_iq.cu has instances for but whose dispatch is not part of it. Every other pair goes to the two above.
bool native_supported_ext(int gu_type, int d_type);
void experts_native_ext(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);
#endif
} // namespace strata::prefill::fused_q36
+1 -1
View File
@@ -869,7 +869,7 @@ native_kernel(const BatchOf<NT> b, const NativeGeom geo, const Tables tb, const
: blob + geo.down_off + (size_t) (rbase + ur) * geo.d_row;
// gate/up in Q2_0 (64-value blocks): a block per stage, as the down rows read theirs; in Q8_0 (32-value blocks, CUDA
// STRATA_PF_FUSED_Q8GU=1): two blocks a stage, as the Q8_0 down rows read theirs
constexpr bool GU256 = GU && WT != T_Q2_0 && WT != T_Q8_0 && !is_tq(WT);
constexpr bool GU256 = (GU || ((WT == T_Q4_K || WT == T_Q5_K || WT == T_Q6_K) && D_ROWS_K % 256 == 0)) && WT != T_Q2_0 && WT != T_Q8_0 && !is_tq(WT); // (a K-quant DOWN walks its 256-value super-blocks too, when n_ff is a multiple of 256: Qwen3.6's 512)
constexpr bool B128 = is_tq(WT); // (CUDA, STRATA_PF_FUSED_TQ=1) 128-value blocks, gate/up and down: a block per two stages, sub-blocks 0-3
auto unit = [&](int s) -> const uint8_t* { // the block of stage s's sub-block
if (B128) return wrow + (s >> 1) * BS;
+67
View File
@@ -57,6 +57,73 @@ bool requested();
namespace strata::prefill::fused_q36 {
// the one function of moe_fused.cu (outside the renamed copy) the kernels' rules call: the same answer for both copies
bool requested() { return strata::prefill::fused::requested(); }
// ---- the K-quant downs of UD-Q4_K_XL (Q5_K on 38 layers, Q6_K on 3), item C --------------------------------------------------------------------------
// native_kernel<T, false, ...> walks a K-quant down row's 256-value super-blocks (GU256's `D_ROWS_K % 256 == 0` arm: K = 512 here, two blocks a row) and reads
// the minimum's code sums the Q4_K / Q5_K gate/up epilogue writes into H's spare bytes (has_min); the dispatch is not in the copy's native_supported /
// experts_native (and moe_fused_iq.cu is not edited for it), so it is here: the pair rule of the K-quant gate/up (native_supported(gu, T_Q5_1): the same
// prerequisites - STRATA_PF_FUSED, the K-quant instances set up, no NT = 256 / warp-specialized kernels) plus this file's own set-up of the two down instances,
// then the K-quant launch of experts_native with the new down. (After lane SMALL-SM's pick_ww(n, n_expert, staged, wide) lands this call takes its arguments.)
namespace {
struct KqDown {
bool ok = false;
int occ5 = 1, occ6 = 1; // blocks per multiprocessor of the Q5_K / Q6_K down instances
};
const KqDown& kqdown_info() {
static std::mutex mu;
static KqDown per[32];
static bool done[32] = {};
int dev = 0;
cudaGetDevice(&dev);
std::lock_guard<std::mutex> lk(mu);
KqDown& k = per[dev & 31];
if (done[dev & 31]) return k;
done[dev & 31] = true;
const DevInfo& d = dev_info();
int o5 = 1 << 20, o6 = 1 << 20;
if (d.ok && d.kq && setup_one<T_Q5_K, false>(o5) && setup_one<T_Q6_K, false>(o6)) {
k.ok = true;
k.occ5 = o5;
k.occ6 = o6;
}
cudaGetLastError();
return k;
}
bool is_kq_down_pair(int gu_type, int d_type) {
return (gu_type == T_Q4_K || gu_type == T_Q5_K) && (d_type == T_Q5_K || d_type == T_Q6_K);
}
} // namespace
bool native_supported_ext(int gu_type, int d_type) {
if (is_kq_down_pair(gu_type, d_type)) return native_supported(gu_type, T_Q5_1) && kqdown_info().ok;
return native_supported(gu_type, d_type);
}
void experts_native_ext(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) {
if (!is_kq_down_pair(g.gu_type, g.d_type)) {
experts_native(b, g, n_expert, n, scratch, xa, src, ha, dm, stream);
return;
}
if (b.e1 <= b.e0 || n <= 0) return;
const DevInfo& d = dev_info();
const KqDown& kd = kqdown_info();
if (!d.ok || !d.kq || !kd.ok) {
std::fprintf(stderr, "prefill fused experts (Qwen3.6): types %d / %d are not covered here\n", g.gu_type, g.d_type);
std::exit(1);
}
const cudaStream_t s = (cudaStream_t) stream;
const Tables tb = tables(const_cast<void*>(scratch), n_expert);
const int ww = pick_ww(n, n_expert);
const int64_t tiles = (n + kTileRows - 1) / kTileRows + (b.e1 - b.e0);
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 * (g.d_type == T_Q5_K ? kd.occ5 : kd.occ6));
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);
if (g.d_type == T_Q5_K) launch<T_Q5_K, false>(ww, k_d, b, g, tb, ha, src, nullptr, dm, s);
else launch<T_Q6_K, false>(ww, k_d, b, g, tb, ha, src, nullptr, dm, s);
ck(cudaGetLastError(), "experts_native_ext");
}
} // namespace strata::prefill::fused_q36
#endif // !__HIPCC__
+2 -2
View File
@@ -1398,7 +1398,7 @@ struct Engine {
g.n_ff == FQ::kQ36FF)
for (size_t i = 0; i < n_state; ++i) {
const Layer& L = layer_at((int) i);
fused_on[i] = FQ::native_supported(L.L.gu_type, L.L.d_type) ? 1 : 0;
fused_on[i] = FQ::native_supported_ext(L.L.gu_type, L.L.d_type) ? 1 : 0;
}
#endif
use_pf = true;
@@ -2119,7 +2119,7 @@ struct Engine {
b.e0 = e;
b.e1 = std::min(ne, e + FQ::kQ36MaxBatch);
for (int x = b.e0; x < b.e1; ++x) b.blob[x - b.e0] = wbase + (size_t) x * L.stride;
FQ::experts_native(b, ng, ne, n_ent, f_scr, f_xq, f_src, f_ha, eparts, st);
FQ::experts_native_ext(b, ng, ne, n_ent, f_scr, f_xq, f_src, f_ha, eparts, st);
}
prow = f_slot;
#endif
+2 -2
View File
@@ -245,7 +245,7 @@ void run_fused(const Pair& p, const Geo& geo, FusedBufs& b, const float* x_dev,
bt.e0 = e0;
bt.e1 = std::min(cuts[c + 1], e0 + fq::kQ36MaxBatch);
for (int e = bt.e0; e < bt.e1; ++e) bt.blob[e - bt.e0] = blob[(size_t) e];
fq::experts_native(bt, ng, E, rows, b.scratch.p, b.xa.p, b.src.as<int32_t>(), b.ha.p, b.dm.as<float>(), s);
fq::experts_native_ext(bt, ng, E, rows, b.scratch.p, b.xa.p, b.src.as<int32_t>(), b.ha.p, b.dm.as<float>(), s);
}
}
}
@@ -614,7 +614,7 @@ int main(int argc, char** argv) {
ck(cudaStreamCreateWithFlags(&s, cudaStreamNonBlocking), "stream");
for (const Pair& p : pairs) {
if (only && std::string(p.name).find(only) == std::string::npos) continue;
if (!fq::native_supported((int) p.gu, (int) p.d)) {
if (!fq::native_supported_ext((int) p.gu, (int) p.d)) {
if (p.optional) { std::printf("%s: not covered by this build of the Qwen3.6 kernels (skipped)\n", p.name); continue; }
throw std::runtime_error(std::string(p.name) + ": not covered by the Qwen3.6 copy of the kernels");
}