Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.
C++
MMVQ_IL rows table per architecture: kIlArch hook, STRATA_MMVQ_IL_ROWS override, mmvq_il_parity --emit-table, the sm_89 method
Niko12212d6c6ae5ce4d
diff --git a/docs/MMVQ_IL_TABLE.md b/docs/MMVQ_IL_TABLE.mdnew file mode 100644index 000000000..93cf1b53b--- /dev/null+++ b/docs/MMVQ_IL_TABLE.md@@ -0,0 +1,32 @@+# The interleaved small-batch matvec (STRATA_MMVQ_IL): how to measure a table for your card++The decode and verify windows run their dense projections (2 to 4 tokens at a time) through `native_mmvq_il`, which reads an+interleaved copy of the q8_1 activations. Every choice it makes gives the same bits as the plain kernels+(`STRATA_MMVQ_IL=0` turns the whole path off); only the speed differs. How many matrix rows one warp takes+(1, 2 or 4, or 0 = use the plain kernel) is a table in `src/kernels/cuda/native_mmvq.cu`, indexed by the+quantisation type (IQ4_XS, Q4_K, Q5_K, Q6_K), the token count (2-4) and the matrix's row count (five classes).++Which table a card gets:++| card | table |+|---|---|+| sm_86 (Ampere consumer) and sm_120 (Blackwell consumer) | `kIlRowsShared`, measured on an RTX 3060 and an RTX 5070 together (a cell is non-zero only if the worse of the two cards still gains 3%) |+| sm_89 (Ada) and every other sm_80+ card | `kIlRowsShared` too. Nobody has measured sm_89 yet (we have no such card). |+| sm_75 and older | the path is off; the plain kernels run |+| a card listed in `kIlArch` | its own table |++## Measuring sm_89 (or any other card)++1. Build with tests: `cmake -S . -B build -DSTRATA_BUILD_TESTS=ON` and `cmake --build build --target mmvq_il_parity`.+2. Close everything else on the GPU, then run `build/mmvq_il_parity --bench --emit-table` (a few minutes).+ It first checks bit-for-bit parity of every shape and every rows choice (it must end with `mmvq_il_parity: OK`),+ prints a `BENCH` line per shape and token count, and last a ready-to-paste block, one line per type.+3. Post the whole output on the issue. A maintainer pastes the block as `kIlRows_89[]` and adds+ `{89, kIlRows_89, sizeof(kIlRows_89) / sizeof(IlRows)}` to `kIlArch`.+4. To try a table without rebuilding, set `STRATA_MMVQ_IL_ROWS="type:ncols:r0,r1,r2,r3,r4;..."`, for example+ `STRATA_MMVQ_IL_ROWS="23:3:0,0,2,4,4;12:2:0,1,1,1,1"` (the five numbers are the row classes below 2048, 4096, 8192,+ 12288 and above; each is 0, 1, 2 or 4). Then A/B the decode: same flags, `STRATA_MMVQ_IL_ROWS` set or unset, 10+ interleaved pairs, medians.++The emit rule: per cell, the fastest rows count if it takes at least 3% off the plain kernel's time, else 0; shapes of one+class that disagree give 0; a class with no measured shape copies its neighbour below.diff --git a/include/strata/kernels/native_mmvq.hpp b/include/strata/kernels/native_mmvq.hppindex 799ef7db1..fef71027d 100644--- a/include/strata/kernels/native_mmvq.hpp+++ b/include/strata/kernels/native_mmvq.hpp@@ -148,6 +148,9 @@ bool native_mmvq_il_supported(int ggml_type, int ncols, int n_out);// warp takes 1, 2 or 4 rows and reads the columns from the interleaved copy; native_mmvq (x_q8_1) when not supported.void native_mmvq_il(int ggml_type, const void* weights, const void* x_q8_1, const void* x_il, float* y, int n_in,int n_out, int ncols, void* stream);+// The table's rows a warp (0, 1, 2, 4; 0 = native_mmvq's kernels) for a card of compute capability `cc` (major * 10 + minor).+// Per-architecture tables plus STRATA_MMVQ_IL_ROWS; see native_mmvq.cu and docs/MMVQ_IL_TABLE.md.+int native_mmvq_il_rows_for(int cc, int ggml_type, int ncols, int n_out);// Tests and benchmarks: every native_mmvq_il call takes `rows` a warp (0: the table).void native_mmvq_il_tune(int rows);diff --git a/src/kernels/cuda/native_mmvq.cu b/src/kernels/cuda/native_mmvq.cuindex 3ef2dcbfa..3384d4157 100644--- a/src/kernels/cuda/native_mmvq.cu+++ b/src/kernels/cuda/native_mmvq.cu@@ -2498,18 +2498,85 @@ namespace {// worse card still takes at least 3% off native_mmvq's time, else 0; classes without a measured shape take their// neighbour's value. Every choice is bitwise the same output, so the table is only speed.struct IlRows { int type; uint8_t r[3][5]; };-constexpr IlRows kIlRows[] = {+constexpr IlRows kIlRowsShared[] = { // sm_86 + sm_120, and the fallback for every other sm_80+ card{23, {{0, 0, 0, 0, 0}, {0, 0, 2, 4, 4}, {0, 1, 1, 1, 1}}}, // IQ4_XS{12, {{0, 0, 1, 1, 1}, {0, 1, 1, 1, 1}, {0, 1, 1, 1, 1}}}, // Q4_K{13, {{0, 0, 1, 1, 1}, {0, 4, 1, 1, 1}, {0, 1, 1, 1, 2}}}, // Q5_K{14, {{0, 0, 1, 1, 0}, {0, 0, 1, 1, 2}, {0, 0, 1, 1, 1}}}, // Q6_K};-int il_rows(int type, int ncols, int n_out) {+// Per-architecture tables (P10). A card whose compute capability (major * 10 + minor) is listed here uses its own table;+// every other sm_80+ card uses kIlRowsShared. sm_89 (Ada) has none yet: nobody measured it (we have no such card), so+// it takes the shared table, which sits between the measured sm_86 and sm_120. To add one, run+// `mmvq_il_parity --bench --emit-table` on the card (docs/MMVQ_IL_TABLE.md), paste the printed block as a+// `constexpr IlRows kIlRows_89[]` and add {89, kIlRows_89, sizeof(kIlRows_89) / sizeof(IlRows)} below. Each choice+// is bitwise the same output, so a table is only speed. Without a rebuild: STRATA_MMVQ_IL_ROWS (see il_env_rows).+struct IlArch { int cc; const IlRows* t; size_t n; };+constexpr IlArch kIlArch[] = {+ {0, nullptr, 0}, // (placeholder so the array is never empty)+};+// STRATA_MMVQ_IL_ROWS="type:ncols:r0,r1,r2,r3,r4;..." overrides single rows of the table for every card (type = ggml+// type 23/12/13/14, ncols 2-4, r* = rows a warp 0/1/2/4 for the five n_out classes). Parsed once.+struct IlEnvRow { int type, ncols; uint8_t r[5]; };+const std::vector<IlEnvRow>& il_env_rows() {+ static const std::vector<IlEnvRow> v = [] {+ std::vector<IlEnvRow> out;+ const char* e = std::getenv("STRATA_MMVQ_IL_ROWS");+ if (!e) return out;+ std::string str(e);+ size_t pos = 0;+ while (pos < str.size()) {+ size_t end = str.find(';', pos);+ if (end == std::string::npos) end = str.size();+ int t = 0, n = 0, r[5] = {0, 0, 0, 0, 0}, used = 0;+ if (std::sscanf(str.substr(pos, end - pos).c_str(), "%d:%d:%d,%d,%d,%d,%d%n", &t, &n, &r[0], &r[1], &r[2], &r[3],+ &r[4], &used) >= 7 && n >= 2 && n <= 4) {+ IlEnvRow x{t, n, {}};+ bool ok = true;+ for (int i = 0; i < 5; ++i) { ok = ok && (r[i] == 0 || r[i] == 1 || r[i] == 2 || r[i] == 4); x.r[i] = (uint8_t) r[i]; }+ if (ok) out.push_back(x);+ else std::fprintf(stderr, "strata: STRATA_MMVQ_IL_ROWS: rows must be 0/1/2/4 - entry ignored+");+ } else {+ std::fprintf(stderr, "strata: STRATA_MMVQ_IL_ROWS: cannot read '%s' (type:ncols:r0,r1,r2,r3,r4) - ignored+",+ str.substr(pos, end - pos).c_str());+ }+ pos = end + 1;+ }+ return out;+ }();+ return v;+}+} // namespace+// The rows a warp for a card of compute capability `cc` (major * 10 + minor); 0: native_mmvq's kernels. Pure, so a test+// can check every architecture's table without that card.+int native_mmvq_il_rows_for(int cc, int type, int ncols, int n_out) {+ if (ncols < 2 || ncols > 4) return 0;const int cls = n_out < 2048 ? 0 : n_out < 4096 ? 1 : n_out < 8192 ? 2 : n_out < 12288 ? 3 : 4;- for (const IlRows& e : kIlRows)- if (e.type == type) return e.r[ncols - 2][cls];+ for (const IlEnvRow& e : il_env_rows())+ if (e.type == type && e.ncols == ncols) return e.r[cls];+ const IlRows* t = kIlRowsShared;+ size_t n = sizeof(kIlRowsShared) / sizeof(IlRows);+ for (const IlArch& a : kIlArch)+ if (a.cc == cc && a.t != nullptr) { t = a.t; n = a.n; }+ for (size_t i = 0; i < n; ++i)+ if (t[i].type == type) return t[i].r[ncols - 2][cls];return 0;}+namespace {+int il_cc() {+ static int cc[16] = {}; // by device ordinal, 0 unknown+ int dev = 0;+ if (cudaGetDevice(&dev) != cudaSuccess || dev < 0 || dev >= 16) return 0;+ if (cc[dev] == 0) {+ int maj = 0, mnr = 0;+ if (cudaDeviceGetAttribute(&maj, cudaDevAttrComputeCapabilityMajor, dev) != cudaSuccess ||+ cudaDeviceGetAttribute(&mnr, cudaDevAttrComputeCapabilityMinor, dev) != cudaSuccess) { cudaGetLastError(); return 0; }+ cc[dev] = maj * 10 + mnr;+ }+ return cc[dev];+}+int il_rows(int type, int ncols, int n_out) { return native_mmvq_il_rows_for(il_cc(), type, ncols, n_out); }int g_tune_rows = 0; // native_mmvq_il_tune (tests, benchmarks): rows a warp for every shape (0: the table)} // namespacevoid native_mmvq_il_tune(int rows) { g_tune_rows = rows; }@@ -2562,6 +2629,7 @@ void native_q8_1_interleave(const void*, void*, int, int, void*) {throw std::invalid_argument("native_q8_1_interleave is CUDA only");}bool native_mmvq_il_supported(int, int, int) { return false; }+int native_mmvq_il_rows_for(int, int, int, int) { return 0; }void native_mmvq_il(int ggml_type, const void* weights, const void* x_q8_1, const void*, float* y, int n_in, int n_out,int ncols, void* stream) {native_mmvq(ggml_type, weights, x_q8_1, y, n_in, n_out, ncols, stream);diff --git a/src/kernels/mmvq_il_parity.cpp b/src/kernels/mmvq_il_parity.cppindex 9c08b6e57..ac6046789 100644--- a/src/kernels/mmvq_il_parity.cpp+++ b/src/kernels/mmvq_il_parity.cpp@@ -1,7 +1,10 @@// src/kernels/mmvq_il_parity.cpp - native_mmvq_il (2-4 columns from the interleaved q8_1 copy, fork F4) against// native_mmvq's multi-column kernels, bitwise, for every rows-a-warp choice; --bench times both per call.//-// build/mmvq_il_parity [--bench]+// build/mmvq_il_parity [--bench [--emit-table]]+//+// --emit-table (with --bench) prints the rows-a-warp table this card measured, in the source form of kIlRows in+// native_mmvq.cu: per cell the rows count (1/2/4) that takes at least 3% off native_mmvq's time, else 0.//// The same Q8_1 bytes feed both paths (quantize_q8_1_rows, then native_q8_1_interleave). Weights are random bytes with// the block scales rewritten as normal fp16 values, so every output is finite (a NaN would compare equal whatever@@ -24,6 +27,8 @@namespace {using namespace strata::kernels;+struct Cell { int type, T, n_out; double ref; double il[5]; };+std::vector<Cell> g_cells;struct Case {const char* name;int type, n_in, n_out, block_elems, block_bytes, scale_at[2];@@ -157,6 +162,7 @@ int run_case(const Case& c, bool bench, cudaStream_t s) {cudaEventElapsedTime(&ms, e0, e1);best_ref = 1000.0 * ms / reps;(void) table_r;+ g_cells.push_back({c.type, T, c.n_out, best_ref, {0, best_il[1], best_il[2], 0, best_il[4]}});std::printf("BENCH %-26s T=%d multi %.1f us | il rows1 %.1f rows2 %.1f rows4 %.1f us\n", c.name, T, best_ref,best_il[1], best_il[2], best_il[4]);}@@ -165,14 +171,77 @@ int run_case(const Case& c, bool bench, cudaStream_t s) {for (void* p : dw) cudaFree(p);return bad;}++// the table as kIlRows prints it: per (type, ncols, n_out class) the fastest rows count if >= 3% under native_mmvq, else 0.+// A cell with several measured shapes takes the worst (smallest) gain of the best choice, as the shipped table does for+// two cards; a class with no measured shape prints the value of its neighbour below it (or 0).+void emit_table() {+ const int types[] = {23, 12, 13, 14};+ const char* names[] = {"IQ4_XS", "Q4_K", "Q5_K", "Q6_K"};+ std::printf("// measured on this card by mmvq_il_parity --bench --emit-table+");+ for (int ti = 0; ti < 4; ++ti) {+ std::printf(" {%d, {", types[ti]);+ for (int nc = 2; nc <= 4; ++nc) {+ int prev = 0;+ std::printf("{");+ for (int cls = 0; cls < 5; ++cls) {+ int pick = -1;+ for (const Cell& c : g_cells) {+ if (c.type != types[ti] || c.T != nc) continue;+ const int cc = c.n_out < 2048 ? 0 : c.n_out < 4096 ? 1 : c.n_out < 8192 ? 2 : c.n_out < 12288 ? 3 : 4;+ if (cc != cls) continue;+ int best = 0;+ double bt = c.ref * 0.97;+ for (int r : {1, 2, 4})+ if (c.il[r] > 0 && c.il[r] < bt) { bt = c.il[r]; best = r; }+ pick = pick < 0 ? best : (pick == best ? pick : 0); // shapes disagree: native_mmvq+ }+ if (pick < 0) pick = prev;+ prev = pick;+ std::printf("%d%s", pick, cls < 4 ? ", " : "");+ }+ std::printf("}%s", nc < 4 ? ", " : "");+ }+ std::printf("}}, // %s+", names[ti]);+ }+}++// the per-architecture lookup, no GPU needed for its answers+int check_tables() {+ int bad = 0;+ for (int type : {23, 12, 13, 14})+ for (int nc = 2; nc <= 4; ++nc)+ for (int n_out : {512, 2560, 6144, 10240, 12288, 248320}) {+ const int a = native_mmvq_il_rows_for(86, type, nc, n_out);+ // sm_89 has no table of its own: it must answer exactly what the measured Ampere table says+ if (native_mmvq_il_rows_for(89, type, nc, n_out) != a || native_mmvq_il_rows_for(120, type, nc, n_out) != a) {+ ++bad;+ std::printf("FAIL table: sm_89/sm_120 differ from sm_86 at type %d ncols %d n_out %d+", type, nc, n_out);+ }+ if (a != 0 && a != 1 && a != 2 && a != 4) { ++bad; std::printf("FAIL table: rows %d+", a); }+ }+ if (native_mmvq_il_rows_for(89, 99, 2, 2560) != 0 || native_mmvq_il_rows_for(89, 12, 5, 2560) != 0) {+ ++bad;+ std::printf("FAIL table: an unknown type or ncols must be 0+");+ }+ return bad;+}} // namespaceint main(int argc, char** argv) {const bool bench = argc > 1 && std::string(argv[1]) == "--bench";+ const bool emit = bench && argc > 2 && std::string(argv[2]) == "--emit-table";cudaStream_t s;cudaStreamCreate(&s);int bad = 0;+ bad += check_tables();for (const Case& c : CASES) bad += run_case(c, bench, s);+ if (emit) emit_table();std::printf("%s\n", bad ? "mmvq_il_parity: FAIL" : "mmvq_il_parity: OK (every case, T 2-4, rows 1/2/4 and the table, bitwise)");return bad ? 1 : 0;}