diff --git a/src/qwen35/q36_engine.cu b/src/qwen35/q36_engine.cu index c6f426ebb1..d2927272c6 100644 --- a/src/qwen35/q36_engine.cu +++ b/src/qwen35/q36_engine.cu @@ -26,6 +26,7 @@ #include "strata/prefill/gemm.hpp" #include "strata/prefill/moe_fused.hpp" #include "strata/prefill/moe_fused_q36.hpp" +#include "strata/prefill/mmq_ids_margin.hpp" #include "strata/prefill/moe_mmq.hpp" #include "strata/qwen35/context_fit.hpp" #include "strata/qwen35/engine.hpp" @@ -1392,8 +1393,9 @@ struct Engine { const size_t n_state = m->layers.size() + (m->has_mtp ? 1 : 0); fused_on.assign(n_state, 0); #if !defined(__HIPCC__) - // Strata's fused kernels where they cover the layer's pair on this card (STRATA_PF_FUSED=0: none, as for Flash-Next) - if (const char* pf = std::getenv("STRATA_PF_FUSED"); !(pf != nullptr && pf[0] == '0') && g.n_embd == FQ::kQ36Embd && + // Strata's fused kernels where they cover the layer's pair on this card: opt-in (STRATA_PF_FUSED=1), as for Flash-Next's packs + // below 80 SMs; unset or 0 keeps every layer on MMQ (the default is not changed until the gate for it is passed) + if (const char* pf = std::getenv("STRATA_PF_FUSED"); pf != nullptr && pf[0] == '1' && g.n_embd == FQ::kQ36Embd && g.n_ff == FQ::kQ36FF) for (size_t i = 0; i < n_state; ++i) { const Layer& L = layer_at((int) i); @@ -1435,9 +1437,17 @@ struct Engine { if (use_mmq) { const int64_t ne = (int64_t) T * Kp; mctx = std::make_unique(); - ident = dalloc_t((size_t) ne); + // The row table (ids) is read past its end by llama.cpp's MMQ: mul_mat_q copies ids_dst[col_low + jt*J + j] for a whole J-column tile before it + // compares the tile with the expert's rows, and its stream-k fixup does the same for every tile up to roundup(max_rows, J) from an expert's first row, + // which can be the table's last (mmq.cuh; J goes to 128). mmq_ids_table_rows adds roundup(max_rows, 128) + 128 entries, max_rows <= T (an expert takes at + // most one row per token): the margin the Flash-Next prompt path already uses (prefill.cpp mmq_ids_rows, PR #5108). An exact table of ne ints (64 KB at the + // default chunk) was read past its end: compute-sanitizer on the same size in the Qwen3.6 one-layer test (z440-20261111-2224-d478), 311 invalid reads of size 4 + // in mul_mat_q, 29 to 57+ bytes after a 65,536-byte allocation. The identity is written over the whole table, as there. + const int64_t ids_rows = (int64_t) MMQ::mmq_ids_table_rows((uint64_t) ne, (uint64_t) T); + if (ids_rows <= 0) { std::fprintf(stderr, "strata-q36: the MMQ row table does not fit\n"); std::exit(1); } + ident = dalloc_t((size_t) ids_rows); CK(cudaDeviceSynchronize()); // ident's cudaMemset (legacy stream) before the iota on the non-blocking stream - MMQ::iota(ident, ne, st); + MMQ::iota(ident, ids_rows, st); CK(cudaMalloc(&mq_x, MMQ::q8_bytes(ne, g.n_embd))); CK(cudaMalloc(&mq_hq, MMQ::q8_bytes(ne, g.n_ff))); mq_gu = dalloc((size_t) ne * 2 * FF); diff --git a/tests/cuda/prefill_fused_q36_test.cpp b/tests/cuda/prefill_fused_q36_test.cpp index 5d3ace5bd2..86d9f68fe0 100644 --- a/tests/cuda/prefill_fused_q36_test.cpp +++ b/tests/cuda/prefill_fused_q36_test.cpp @@ -14,6 +14,7 @@ // Exit 77 without a CUDA device of sm_80 or newer. #include "strata/prefill/moe_fused.hpp" #include "strata/prefill/moe_fused_q36.hpp" +#include "strata/prefill/mmq_ids_margin.hpp" #include "strata/prefill/moe_mmq.hpp" #include "ggml.h" @@ -56,6 +57,7 @@ struct Dev { struct Pair { ggml_type gu, d; const char* name; + bool optional = false; // a pair this build may not cover yet: skipped with a printed line instead of failing (UD-Q4_K_XL's K-quant downs) }; bool mmq_ok(const Pair& p) { return mmq::supported((int) p.gu) && mmq::supported((int) p.d); } size_t row_size(ggml_type t, int64_t n) { return (size_t) (n / ggml_blck_size(t)) * ggml_type_size(t); } @@ -160,13 +162,19 @@ Routing sort_rows(const std::vector& ids, int E) { return r; } +// MMQ's row table (the identity ids) is read past its end by llama.cpp's mul_mat_q and its stream-k fixup (a whole J-column tile from an expert's first row, J up +// to 128): the table takes mmq_ids_table_rows(rows, max_rows) entries, max_rows <= the tokens of the layer (an expert takes at most one row per token). An exact +// table of rows * 4 = 64 KB ended a cudaMalloc chunk and faulted in compute-sanitizer's and the plain run (z440-20261111-2224-d478: 311 invalid reads of size 4 in +// mul_mat_q, 29 to 57+ bytes after the 65,536-byte allocation). +size_t ids_table_rows(int64_t rows) { return (size_t) mmq::mmq_ids_table_rows((uint64_t) rows, (uint64_t) (rows / K)); } + // MMQ, as prefill.cpp runs a native layer: per group of 16 routed experts a gather, gate/up, SwiGLU (gate rows, then up rows), q8_1 of H, // down. Returns Dm [rows][N] in sorted-row order. struct MmqBufs { Dev xq, gu, h, hq, dm, ident, bounds, grp_gu, grp_d; MmqBufs(const Pair& p, int64_t rows, int E) : xq(mmq::q8_bytes(rows, N)), gu((size_t) rows * FF2 * 4), h((size_t) rows * FF * 4), - hq(mmq::q8_bytes(rows, FF)), dm((size_t) rows * N * 4), ident((size_t) rows * 4), + hq(mmq::q8_bytes(rows, FF)), dm((size_t) rows * N * 4), ident(ids_table_rows(rows) * 4), // (the margin llama.cpp's kernels read past the table: see ids_table_rows) bounds((size_t) (2 * (E + E / GROUP + 2)) * 4), grp_gu(GROUP * mmq::matrix_bytes(p.gu, FF2, N) + 4096), grp_d(GROUP * mmq::matrix_bytes(p.d, N, FF) + 4096) {} }; @@ -184,7 +192,7 @@ void run_mmq(const Pair& p, const Geo& geo, mmq::Context& ctx, MmqBufs& b, const for (size_t i = 0; i <= GROUP; ++i) bh[n + 1 + g * (GROUP + 1) + i] = bh[std::min(n, g * GROUP + i)] - bh[g * GROUP]; ck(cudaMemcpyAsync(b.bounds.p, bh.data(), bh.size() * 4, cudaMemcpyHostToDevice, s), "bounds"); - mmq::iota(b.ident.as(), rows, s); + mmq::iota(b.ident.as(), (int64_t) ids_table_rows(rows), s); mmq::quantize(x_dev, src_dev, b.xq.p, (int) p.gu, N, N, rows, s); for (size_t j = 0; j < n; ++j) { const size_t q = j % GROUP; @@ -581,7 +589,11 @@ int main(int argc, char** argv) { const std::vector pairs = { // UD-Q6_K_XL: 40 layers Q6_K / Q8_0 and one Q8_0 / Q8_0; the Q8_0 file: Q8_0 / Q8_0 on all 41 {GGML_TYPE_Q6_K, GGML_TYPE_Q8_0, "Q6_K + Q8_0"}, - {GGML_TYPE_Q8_0, GGML_TYPE_Q8_0, "Q8_0 / Q8_0"}}; + {GGML_TYPE_Q8_0, GGML_TYPE_Q8_0, "Q8_0 / Q8_0"}, + // UD-Q4_K_XL: Q4_K gate/up with a Q5_K down on 38 layers, Q4_K / Q6_K on 2, Q5_K / Q6_K on 1 (K-quant downs: K = 512 is two 256-value super-blocks) + {GGML_TYPE_Q4_K, GGML_TYPE_Q5_K, "Q4_K / Q5_K", true}, + {GGML_TYPE_Q4_K, GGML_TYPE_Q6_K, "Q4_K / Q6_K", true}, + {GGML_TYPE_Q5_K, GGML_TYPE_Q6_K, "Q5_K / Q6_K", true}}; const char* only = nullptr; // --only=NAME: the pairs whose name contains NAME bool timing = true, ref = true; std::vector chunks = {2048, 8192}; // --chunks=A,B: the timing part's chunk sizes @@ -602,7 +614,10 @@ 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)) throw std::runtime_error(std::string(p.name) + ": not covered by the Qwen3.6 copy of the kernels"); + if (!fq::native_supported((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"); + } if (ref) { reference_part(p, s); // the tails and tile edges: a handful of tokens, one token, 130 and 517 tokens (the counts follow the routing)