From d7f7d9677b9eb0f2895a1bc2f23fe18651408ad6 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Wed, 11 Nov 2026 22:53:56 +0100 Subject: [PATCH 1/3] qwen35: give MMQ's row-id table the margin llama.cpp's kernels read past its end The prompt path's MMQ call takes `ident`, the identity row-id table, allocated with exactly ne = chunk * 8 ints (2048 * 8 * 4 = 64 KB at the default chunk). llama.cpp's mul_mat_q (third_party/strata-ggml/ggml/src/ggml-cuda/mmq.cuh) 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 mul_mat_q_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; J goes up to 128. The Flash-Next prompt path already sizes its tables for this (mmq_ids_margin.hpp: roundup(max_rows, 128) + 128 entries, prefill.cpp mmq_ids_rows, PR #5108, where an exact table ending a 2 MiB page faulted on a long prompt). The Qwen3.6 engine did not: the table now takes mmq_ids_table_rows(ne, T) entries (an expert takes at most one row per token, so max_rows <= T = the chunk) and the identity is written over all of them, as there. compute-sanitizer on a table of the same exact size in the one-layer test of the Qwen3.6 fused kernels (z440, RTX A4000, CUDA 12.9, z440-20261111-2224-d478): 311 invalid __global__ reads of size 4 in mul_mat_q, 29 to 57+ bytes after a 65,536-byte allocation, an illegal memory access in the test when that table ended a cudaMalloc chunk. The engine's MMQ path never faulted in the runs so far (the bytes after the table belonged to another allocation); this removes the possibility. No other change. --- src/qwen35/q36_engine.cu | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/qwen35/q36_engine.cu b/src/qwen35/q36_engine.cu index 4bec71bc77..a4d8653572 100644 --- a/src/qwen35/q36_engine.cu +++ b/src/qwen35/q36_engine.cu @@ -24,6 +24,7 @@ #include "strata/kernels/native_moe.hpp" #include "strata/kernels/native_router.hpp" #include "strata/prefill/gemm.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" @@ -1386,10 +1387,18 @@ struct Engine { rcur = dalloc_t((size_t) g.n_expert); rbounds = dalloc_t((size_t) g.n_expert + 1); rmax = dalloc_t(1); - 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); rinv = dalloc_t((size_t) ne); 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); From 14dd746f6ac1c799e91150a9506dc1e4e37236e8 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Wed, 11 Nov 2026 22:59:41 +0100 Subject: [PATCH 2/3] prefill: size the dense MMQ row-id table with the margin llama.cpp's kernels read past it (STRATA_DENSE_MMQ=1) Gemm::native_mmq (HIP builds, opt-in STRATA_DENSE_MMQ=1) passes MMQ an identity row-id table of exactly kRows = 1024 ints (4096 bytes), followed by 256 bytes of bounds and then the end of the allocation. llama.cpp's 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 mul_mat_q_stream_k_fixup reads up to roundup(max_rows, J) ids from the expert's first row; J goes up to 128. A full chunk (1024 rows) with a tile width that does not divide it (J = 112: the last tile starts at row 1008 and reads to index 1119) reads 384 bytes past the table, 128 past the allocation. The table now has kRows + roundup(kRows, 128) + 128 entries (mmq_ids_table_rows, the margin the Flash-Next prompt path uses, include/strata/prefill/mmq_ids_margin.hpp; max_rows <= kRows) and the identity is written over all of it; bounds follow the larger table. Not run: this code is compiled only for HIP (STRATA_PREFILL_MMQ with __HIPCC__) and only with the opt-in switch, so it has not been built or run for this commit. The change adds 4,608 bytes to one scratch allocation and writes more identity entries; the products' inputs are the same. --- src/prefill/gemm.cu | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/prefill/gemm.cu b/src/prefill/gemm.cu index 25910b2e17..595846a969 100644 --- a/src/prefill/gemm.cu +++ b/src/prefill/gemm.cu @@ -2,6 +2,7 @@ #include "strata/prefill/gemm.hpp" #include "strata/kernels/dequant_bf16.hpp" #if defined(STRATA_PREFILL_MMQ) && defined(__HIPCC__) +#include "strata/prefill/mmq_ids_margin.hpp" #include "strata/prefill/moe_mmq.hpp" #endif @@ -1294,8 +1295,12 @@ bool Gemm::native_mmq(const uint16_t* X, int type, const void* W, float* Y, int6 if (ldy <= 0) ldy = N; const auto up = [](size_t v) { return (v + 255) & ~(size_t) 255; }; + // The row table is read past its end by llama.cpp's MMQ (mul_mat_q: a whole J-column tile from the start of each tile, J up to 128; the stream-k fixup: + // up to roundup(max_rows, J) from the expert's first row): kRows + the margin of mmq_ids_margin.hpp (max_rows <= kRows), the identity written over all of it. + // Exact, with only the 256 bytes of the bounds after it, a tile of 112 columns on a full chunk read 384 bytes past the table, 128 past the allocation. + const size_t ids_rows = (size_t) mmq::mmq_ids_table_rows((uint64_t) kRows, (uint64_t) kRows); const size_t ident_off = up(mmq::q8_bytes(kRows, kMaxK)); - const size_t bounds_off = ident_off + up((size_t) kRows * 4); + const size_t bounds_off = ident_off + up(ids_rows * 4); if (!mmq_buf_) { if (cudaMalloc(&mmq_buf_, bounds_off + 256) != cudaSuccess) { cudaGetLastError(); @@ -1303,7 +1308,7 @@ bool Gemm::native_mmq(const uint16_t* X, int type, const void* W, float* Y, int6 return false; } mmq_ctx_ = new mmq::Context(); - mmq::iota((int32_t*) ((uint8_t*) mmq_buf_ + ident_off), kRows, stream_); + mmq::iota((int32_t*) ((uint8_t*) mmq_buf_ + ident_off), (int64_t) ids_rows, stream_); std::fprintf(stderr, "prefill gemm: dense MMQ on (STRATA_DENSE_MMQ=1)\n"); } void* xq = mmq_buf_; From 697b6d67a7d3cde6c78d0122841c6b241cdc0da3 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Wed, 11 Nov 2026 23:00:07 +0100 Subject: [PATCH 3/3] prefill: size the DeepSeek MTP draft step's MMQ row-id table with the margin llama.cpp's kernels read past it The drafter's batched pass (prefill.cpp, Q8_0 projections through MMQ) carves its row-id table `ident` with exactly B * hc ints, followed by 16 bytes of bounds and the rest of the scratch region. llama.cpp's 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 the stream-k fixup reads up to roundup(max_rows, J) ids from the expert's first row; J goes up to 128, so up to about 508 bytes were read past a table that ends 256 bytes before the bounds' own end. The table now has B * hc plus the margin of mmq_ids_margin.hpp (mmq_ids_table_rows; max_rows <= B * hc, one expert) and the identity is written over all of it; the per-row scratch estimate that sizes B takes the second copy of the row table. Not run: the model family is no longer supported and no model of it is on any box. A buffer enlargement only (and a batch size that can come out slightly smaller through the per-row estimate); the products' inputs are the same. --- src/prefill/prefill.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/prefill/prefill.cpp b/src/prefill/prefill.cpp index 09f6c5898a..e3f3ded6c2 100644 --- a/src/prefill/prefill.cpp +++ b/src/prefill/prefill.cpp @@ -2314,7 +2314,7 @@ bool Prefill::draft_kv(core::MtpDrafter& mtp, const float* R_rows, const int32_t static const bool f16_only = [] { const char* v = std::getenv("STRATA_MTP_BATCH_F16"); return v && v[0] == '1'; }(); const bool q8 = !f16_only && mmq::built() && mmq::fits(kQ8_0, Nn) && mmq::fits(kQ8_0, KV); // #420 const uint64_t per_row = 4 * (2 * Nn + 4 * HCN + LR + HC + Nn + 2 * KV + 1) + 2 * (Nn + 2 * HCN + LR + Nn) + 64 + - (q8 ? (uint64_t) mmq::q8_bytes(g.hc, Nn) + 4 * g.hc : 0); + (q8 ? (uint64_t) mmq::q8_bytes(g.hc, Nn) + 8 * g.hc : 0); // (8 * hc: the row table twice, see its margin below) int64_t B = std::min(n - r0, (int64_t) (m.region_bytes / per_row) & ~(int64_t) 63); if (st.kv_mode == 2) // a ring: one batch's cells must not share a slot (a batch can straddle one page more) B = std::min(B, ((st.n_slots - 1) * strata::kernels::qsa_real_shapes().page_size) & ~(int64_t) 63); @@ -2339,7 +2339,10 @@ bool Prefill::draft_kv(core::MtpDrafter& mtp, const float* R_rows, const int32_t float* Vc = (float*) carve((size_t) B * KV * 4); int32_t* tok = (int32_t*) carve((size_t) B * 4); void* xq = q8 ? carve(mmq::q8_bytes(B * g.hc, Nn)) : nullptr; - int32_t* ident = q8 ? (int32_t*) carve((size_t) B * g.hc * 4) : nullptr; + // The row table is read past its end by llama.cpp's MMQ (a whole J-column tile from the start of each tile; the stream-k fixup up to roundup(max_rows, J) + // from the expert's first row; J up to 128): rows + the margin of mmq_ids_margin.hpp, max_rows <= B * hc (one expert, p.n = 1), the identity over all of it. + const uint64_t ids_rows = q8 ? mmq::mmq_ids_table_rows((uint64_t) (B * g.hc), (uint64_t) (B * g.hc)) : 0; + int32_t* ident = q8 ? (int32_t*) carve((size_t) ids_rows * 4) : nullptr; int32_t* bnd = q8 ? (int32_t*) carve(16) : nullptr; if ((uint64_t) (q - m.region) > m.region_bytes) return decline("the batch buffers do not fit the scratch region"); static const bool timing = std::getenv("STRATA_DRAFT_TIMING") != nullptr; // debug: where this pass's time goes @@ -2352,7 +2355,7 @@ bool Prefill::draft_kv(core::MtpDrafter& mtp, const float* R_rows, const int32_t std::vector tk((size_t) B); if (q8) { if (!m.mmq_ctx) m.mmq_ctx = std::make_unique(); - mmq::iota(ident, B * g.hc, m.cs); + mmq::iota(ident, (int64_t) ids_rows, m.cs); } // y[rows, n_out] = x[rows, k] . w^T for a Q8_0 matrix: MMQ from the FP32 rows (bounds slot 0: rows, 1: rows*hc) auto proj = [&](const float* x, const uint16_t* x16, const void* w, float* y, int64_t rows, int64_t n_out,