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<Q8_0, 96, false>, 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.
This commit is contained in:
Niko1221 committed 2026-11-11 22:56:04 +01:00
1 parent 5f0ae5b55a
commit d7f7d9677b
1 file changed
+11 -2
+11 -2
View File
@@ -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<int32_t>((size_t) g.n_expert);
rbounds = dalloc_t<int32_t>((size_t) g.n_expert + 1);
rmax = dalloc_t<int32_t>(1);
ident = dalloc_t<int32_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<Q8_0, 96>, 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<int32_t>((size_t) ids_rows);
rinv = dalloc_t<int32_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);