tests: give the MMQ tests' scatter-permutation and identity row-id tables the margin too

The same defect as the previous commit in the tests whose row-id table is not the plain identity of the fused tests: llama.cpp's mul_mat_q and its stream-k
fixup read the table up to a J-column tile (J <= 128) past the last row, up to roundup(max_rows, J) ids from an expert's first row, and these tests
allocated exactly `rows` entries. Each table now takes mmq_ids_table_rows(rows, max_rows) entries (include/strata/prefill/mmq_ids_margin.hpp):
 - tests/cuda/prefill_mmq_kquant_test.cpp, tests/cuda/prefill_mmq_v31_test.cpp (the grouped product): the table is the destination permutation `dst`; the extra
   entries are row index 0 (a valid row; the kernel reads them into shared memory and never uses them: only the first col_diff entries of a tile are used)
 - tests/cuda/prefill_mmq_v31_test.cpp (the timing sweep): the identity over the whole table, as prefill.cpp writes it
 - tests/cuda/prefill_iq4_w4a8_test.cu: the identity over the whole table
 - tests/hip/prefill_mmq_parity.cpp: the destination permutation with row index 0 in the extra entries
Test code only; no engine code changes.
This commit is contained in:
Niko1221 committed 2026-11-11 23:03:23 +01:00
1 parent e4138c1c4d
commit 9f0056e412
4 files changed
+30 -9

No files matched your search

+4 -2
View File
@@ -15,6 +15,7 @@
// some products of both types. Exit 0: identical; 1: a difference; 77: no CUDA device, or a GPU the kernel does not
// run on (it covers sm_80..sm_88; elsewhere it declines every product and MMQ runs as before).
#include "strata/prefill/moe_iq4_w4a8.hpp"
#include "strata/prefill/mmq_ids_margin.hpp"
#include "strata/prefill/moe_mmq.hpp"
#include "ggml.h"
@@ -153,8 +154,9 @@ Result run_case(int64_t T, double sigma, bool tab, int wide, bool bench) {
int32_t *bounds_dev, *ids;
ck(cudaMalloc(&bounds_dev, bounds_abs.size() * 4), "bounds");
ck(cudaMemcpy(bounds_dev, bounds_abs.data(), bounds_abs.size() * 4, cudaMemcpyHostToDevice), "bounds");
ck(cudaMalloc(&ids, (size_t) TK * 4), "ids");
mmq::iota(ids, TK, nullptr);
const size_t ids_rows = (size_t) mmq::mmq_ids_table_rows((uint64_t) TK, (uint64_t) TK); // (mul_mat_q reads the row table past its last row: mmq_ids_margin.hpp)
ck(cudaMalloc(&ids, ids_rows * 4), "ids");
mmq::iota(ids, (int64_t) ids_rows, nullptr);
float *gu_a, *dm_a, *gu_b, *dm_b;
const size_t ngu = (size_t) TK * 2 * NFF, ndm = (size_t) TK * N;
ck(cudaMalloc(&gu_a, ngu * 4), "gu_a");
+8 -2
View File
@@ -5,6 +5,7 @@
// to q8_1 by design, so this is a screen for layout, stride, expert-bound and row-id errors (the bounds of
// tests/hip/prefill_mmq_parity.cpp), not a bit-exactness test. Several experts per product, permuted rows, an
// all-zero row. The MMQ path covers gate/up Q4_K / Q5_K / Q6_K and down Q5_1 / Q8_0 (UD-Q4_K_XL / UD-Q6_K_XL).
#include "strata/prefill/mmq_ids_margin.hpp"
#include "strata/prefill/moe_mmq.hpp"
#include "ggml.h"
@@ -87,11 +88,16 @@ void product(mmq::Context& ctx, cudaStream_t s, const char* name, ggml_type t, i
ws.push_back(make_matrix(t, out_rows, cols, e, trial));
std::copy(ws.back().begin(), ws.back().end(), w.begin() + (ptrdiff_t) ((size_t) e * eb));
}
Dev dx(x.size() * 4), dsrc((size_t) rows * 4), ddst((size_t) rows * 4), db(bounds.size() * 4), dw(w.size()),
// llama.cpp's mul_mat_q and its stream-k fixup read the row-id table up to a J-column tile (J <= 128) past the last row, up to roundup(max_rows, J)
// from an expert's first row (mmq_ids_margin.hpp): the table has the margin, its extra entries hold row index 0 (valid; they are read, never used)
const size_t ids_rows = (size_t) mmq::mmq_ids_table_rows((uint64_t) rows, (uint64_t) *std::max_element(counts.begin(), counts.end()));
std::vector<int32_t> dst_table(dst);
dst_table.resize(ids_rows, 0);
Dev dx(x.size() * 4), dsrc((size_t) rows * 4), ddst(ids_rows * 4), db(bounds.size() * 4), dw(w.size()),
dxq(mmq::q8_bytes(rows, cols)), dy((size_t) rows * (size_t) out_rows * 4);
ck(cudaMemcpy(dx.p, x.data(), x.size() * 4, cudaMemcpyHostToDevice), "x");
ck(cudaMemcpy(dsrc.p, src.data(), src.size() * 4, cudaMemcpyHostToDevice), "src");
ck(cudaMemcpy(ddst.p, dst.data(), dst.size() * 4, cudaMemcpyHostToDevice), "dst");
ck(cudaMemcpy(ddst.p, dst_table.data(), dst_table.size() * 4, cudaMemcpyHostToDevice), "dst");
ck(cudaMemcpy(db.p, bounds.data(), bounds.size() * 4, cudaMemcpyHostToDevice), "bounds");
ck(cudaMemcpy(dw.p, w.data(), w.size(), cudaMemcpyHostToDevice), "w");
ck(cudaMemset(dy.p, 0xff, (size_t) rows * (size_t) out_rows * 4), "sentinel");
+10 -3
View File
@@ -11,6 +11,7 @@
// prefill_mmq_v31_test --bench [--reps N] Q5_0 down: MMQ against the dequantize-to-FP16 + cuBLAS route
// (what a build without the instance runs), per expert-group size
#include "strata/kernels/dequant_bf16.hpp"
#include "strata/prefill/mmq_ids_margin.hpp"
#include "strata/prefill/moe_mmq.hpp"
#include "ggml.h"
@@ -94,11 +95,16 @@ void product(mmq::Context& ctx, cudaStream_t s, const char* name, ggml_type t, i
ws.push_back(make_matrix(t, out_rows, cols, e, trial));
std::copy(ws.back().begin(), ws.back().end(), w.begin() + (ptrdiff_t) ((size_t) e * eb));
}
Dev dx(x.size() * 4), dsrc((size_t) rows * 4), ddst((size_t) rows * 4), db(bounds.size() * 4), dw(w.size()),
// llama.cpp's mul_mat_q and its stream-k fixup read the row-id table up to a J-column tile (J <= 128) past the last row, up to roundup(max_rows, J)
// from an expert's first row (mmq_ids_margin.hpp): the table has the margin, its extra entries hold row index 0 (valid; they are read, never used)
const size_t ids_rows = (size_t) mmq::mmq_ids_table_rows((uint64_t) rows, (uint64_t) *std::max_element(counts.begin(), counts.end()));
std::vector<int32_t> dst_table(dst);
dst_table.resize(ids_rows, 0);
Dev dx(x.size() * 4), dsrc((size_t) rows * 4), ddst(ids_rows * 4), db(bounds.size() * 4), dw(w.size()),
dxq(mmq::q8_bytes(rows, cols)), dy((size_t) rows * (size_t) out_rows * 4);
ck(cudaMemcpy(dx.p, x.data(), x.size() * 4, cudaMemcpyHostToDevice), "x");
ck(cudaMemcpy(dsrc.p, src.data(), src.size() * 4, cudaMemcpyHostToDevice), "src");
ck(cudaMemcpy(ddst.p, dst.data(), dst.size() * 4, cudaMemcpyHostToDevice), "dst");
ck(cudaMemcpy(ddst.p, dst_table.data(), dst_table.size() * 4, cudaMemcpyHostToDevice), "dst");
ck(cudaMemcpy(db.p, bounds.data(), bounds.size() * 4, cudaMemcpyHostToDevice), "bounds");
ck(cudaMemcpy(dw.p, w.data(), w.size(), cudaMemcpyHostToDevice), "w");
ck(cudaMemset(dy.p, 0xff, (size_t) rows * (size_t) out_rows * 4), "sentinel");
@@ -198,7 +204,8 @@ void bench(ggml_type t, int reps) {
for (int per : {16, 64, 256, 1024}) {
const int rows = per * NE;
const std::vector<float> x = make_x(rows, FF, 3);
std::vector<int32_t> bounds((size_t) NE + 1), iota((size_t) rows);
const size_t ids_rows = (size_t) mmq::mmq_ids_table_rows((uint64_t) rows, (uint64_t) per); // (the identity over the whole table: mmq_ids_margin.hpp)
std::vector<int32_t> bounds((size_t) NE + 1), iota(ids_rows);
for (int e = 0; e <= NE; ++e) bounds[(size_t) e] = e * per;
std::iota(iota.begin(), iota.end(), 0);
Dev dx(x.size() * 4), db(bounds.size() * 4), di(iota.size() * 4), dxq(mmq::q8_bytes(rows, FF)),
+8 -2
View File
@@ -3,6 +3,7 @@
// It compares GGML's MMQ result (with its intended q8_1 activation rounding) to a CPU FP32 product using
// GGML's own dequantizer for the exact same weight blocks. The tolerances screen layout/stride/bounds errors;
// they do not assert bitwise or FP16 parity.
#include "strata/prefill/mmq_ids_margin.hpp"
#include "strata/prefill/moe_mmq.hpp"
#include "ggml.h"
#include <hip/hip_runtime.h>
@@ -129,14 +130,19 @@ Metrics run_product(Context & ctx, hipStream_t stream, const std::string & name,
DeviceBuffer dx(x.size() * sizeof(float));
DeviceBuffer dsrc(src_ids.size() * sizeof(int32_t));
DeviceBuffer ddst(dst_ids.size() * sizeof(int32_t));
// llama.cpp's mul_mat_q and its stream-k fixup read the row-id table up to a J-column tile (J <= 128) past the last row, up to roundup(max_rows, J) from an
// expert's first row (mmq_ids_margin.hpp): the table has the margin, its extra entries hold row index 0 (valid; they are read, never used)
const size_t ids_rows = (size_t) strata::prefill::mmq::mmq_ids_table_rows((uint64_t) rows, (uint64_t) *std::max_element(counts.begin(), counts.end()));
std::vector<int32_t> dst_table(dst_ids);
dst_table.resize(ids_rows, 0);
DeviceBuffer ddst(dst_table.size() * sizeof(int32_t));
DeviceBuffer dbounds(bounds.size() * sizeof(int32_t));
DeviceBuffer dw(w.size());
DeviceBuffer dxq(q8_bytes(rows, cols));
DeviceBuffer dy((size_t) rows * (size_t) out_rows * sizeof(float));
hip_check(hipMemcpy(dx.p, x.data(), x.size() * sizeof(float), hipMemcpyHostToDevice), "copy x");
hip_check(hipMemcpy(dsrc.p, src_ids.data(), src_ids.size() * sizeof(int32_t), hipMemcpyHostToDevice), "copy src ids");
hip_check(hipMemcpy(ddst.p, dst_ids.data(), dst_ids.size() * sizeof(int32_t), hipMemcpyHostToDevice), "copy dst ids");
hip_check(hipMemcpy(ddst.p, dst_table.data(), dst_table.size() * sizeof(int32_t), hipMemcpyHostToDevice), "copy dst ids");
hip_check(hipMemcpy(dbounds.p, bounds.data(), bounds.size() * sizeof(int32_t), hipMemcpyHostToDevice), "copy bounds");
hip_check(hipMemcpy(dw.p, w.data(), w.size(), hipMemcpyHostToDevice), "copy weights and zero tail");
// On the product's stream: a plain hipMemset runs on the null stream, which a non-blocking stream does not wait