diff --git a/include/strata/prefill/moe_fused_q36.hpp b/include/strata/prefill/moe_fused_q36.hpp index f812f8a9ad..ba860e09ff 100644 --- a/include/strata/prefill/moe_fused_q36.hpp +++ b/include/strata/prefill/moe_fused_q36.hpp @@ -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 diff --git a/src/prefill/moe_fused_iq.cu b/src/prefill/moe_fused_iq.cu index 1d56025c9c..12cf5384f0 100644 --- a/src/prefill/moe_fused_iq.cu +++ b/src/prefill/moe_fused_iq.cu @@ -869,7 +869,7 @@ native_kernel(const BatchOf 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; diff --git a/src/prefill/moe_fused_iq_q36.cu b/src/prefill/moe_fused_iq_q36.cu index c7892ec2de..fc424a1034 100644 --- a/src/prefill/moe_fused_iq_q36.cu +++ b/src/prefill/moe_fused_iq_q36.cu @@ -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 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 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(o5) && setup_one(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(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(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 * (g.d_type == T_Q5_K ? kd.occ5 : kd.occ6)); + 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); + if (g.d_type == T_Q5_K) launch(ww, k_d, 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_ext"); +} } // namespace strata::prefill::fused_q36 #endif // !__HIPCC__ diff --git a/src/qwen35/q36_engine.cu b/src/qwen35/q36_engine.cu index e1a93d4729..b3682e94da 100644 --- a/src/qwen35/q36_engine.cu +++ b/src/qwen35/q36_engine.cu @@ -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 diff --git a/tests/cuda/prefill_fused_q36_test.cpp b/tests/cuda/prefill_fused_q36_test.cpp index 86d9f68fe0..8fee2ee0d4 100644 --- a/tests/cuda/prefill_fused_q36_test.cpp +++ b/tests/cuda/prefill_fused_q36_test.cpp @@ -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(), b.ha.p, b.dm.as(), s); + fq::experts_native_ext(bt, ng, E, rows, b.scratch.p, b.xa.p, b.src.as(), b.ha.p, b.dm.as(), 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"); }