Kyojin: open-source engine that runs 100 GB-class open models (GLM-5.3, Qwen3.8, DeepSeek V4.1) on one desktop machine. AMD Strix Halo, NVIDIA RTX and Apple Mac. OpenAI API, built on ExLlamaV3.
Python amdapple-siliconcudadeepseekexl3exllamav3glminferencellmllm-inferencelocal-llmnvidiaopenai-apiquantizationqwenrocmstrix-halo
Tests: CPU proofs for the K5, K6 and K7 routes
YanissAmz57e4dfa00abc
diff --git a/tests/moe_fused/dec_k7_check.cpp b/tests/moe_fused/dec_k7_check.cppnew file mode 100644index 0000000..1460666--- /dev/null+++ b/tests/moe_fused/dec_k7_check.cpp@@ -0,0 +1,49 @@+// CPU proof of the K7 (KB2 = 14) tile format of the batch-1 / R-row decode kernels (exl3_dec.cu, Fmt<KB2>). Re-implements Fmt::wstart and window() word for word and compares every+// 16 bit window of a 256-weight trellis tile with the engine's dq2x2<7> arithmetic (exl3_dq.cuh, cyclic over the 56 tile words) for random tiles. Also checks the word indices stay+// inside the tile and that the tile is 56 words = 14 vectors of 4 words. Run for K5 / K6 too, so the check is anchored on formats that are already in production.+#include <cstdio>+#include <cstdint>+#include <cstdlib>+static int fails = 0;+#define CHECK(c, ...) do { if (!(c)) { if (fails < 20) { printf("FAIL %s: ", #c); printf(__VA_ARGS__); printf("\n"); } ++fails; } } while (0)+template <int KB2> struct Fmt+{+ static constexpr int KA = KB2 / 2, HALF = KB2 & 1, BPB = 16 * KA + 8 * HALF, L = 16 * BPB, WORDS = L / 32, VEC = WORDS / 4;+ static constexpr int send(int p) { return (p >> 4) * BPB + ((p & 15) + 1) * KA + (HALF ? ((p & 15) + 1) / 2 : 0); }+ static constexpr int wstart(int p) { return ((send(p) - 16) % L + L) % L; }+};+static uint32_t ubfe(uint32_t x, int off, int w) { return (x >> off) & ((1u << w) - 1u); }+static uint32_t alignbit(uint32_t hi, uint32_t lo, int s) { return (uint32_t) ((((uint64_t) hi << 32) | lo) >> s); }+static uint32_t fshift_eng(uint32_t b, uint32_t a, int shift) { return (uint32_t) ((((uint64_t) a << 32) | (uint64_t) b) >> shift); }+template <int KB2> static void run()+{+ using F = Fmt<KB2>;+ const int bits = KB2 / 2, tw = bits * 256 / 32;+ CHECK(F::WORDS == tw && F::WORDS % 4 == 0 && F::VEC * 4 == F::WORDS, "format KB2=%d", KB2);+ srand(11 + KB2);+ for (int trial = 0; trial < 50; ++trial)+ {+ uint32_t w[64];+ for (int i = 0; i < tw; ++i) w[i] = ((uint32_t) rand() << 16) ^ (uint32_t) rand() ^ ((uint32_t) rand() << 3);+ for (int p = 0; p < 256; ++p)+ {+ const int s = F::wstart(p), i = s >> 5, o = s & 31;+ CHECK(i >= 0 && i < F::WORDS && (i + 1) % F::WORDS < F::WORDS, "index KB2=%d p=%d", KB2, p);+ const uint32_t got = (o <= 16 ? ubfe(w[i], 16 - o, 16) : alignbit(w[i], w[(i + 1) % F::WORDS], 48 - o)) & 0xffffu;+ // engine: value p of the tile; dq2x2 / dq4 numbering: t_offset + j with end bit (t_offset + j + 257) * bits+ const int e = p; // position p of the ring is weight p of the tile+ const int b2 = (e + 257) * bits; // end of the window+ const int b0 = b2 - 16;+ const int i0 = b0 / 32, i2 = (b2 - 1) / 32, s2 = (i2 + 1) * 32 - b2;+ const uint32_t a = w[i0 % tw], b = w[i2 % tw];+ const uint32_t want = fshift_eng(b, a, s2) & 0xffffu;+ CHECK(got == want, "window mismatch KB2=%d p=%d got=%x want=%x", KB2, p, got, want);+ }+ }+}+int main()+{+ run<10>(); run<12>(); run<14>();+ printf(fails ? "DEC K7 CHECK FAILED (%d)\n" : "DEC K7 CHECK OK\n", fails);+ return fails ? 1 : 0;+}diff --git a/tests/moe_fused/k56_check.cpp b/tests/moe_fused/k56_check.cppnew file mode 100644index 0000000..46858e5--- /dev/null+++ b/tests/moe_fused/k56_check.cpp@@ -0,0 +1,62 @@+// CPU proof for the K5 routed / K4 and K5 routed with K6 shared shapes added to the fused MoE half-layer, plus the grouped GEMV staging bounds for K5 / K6.+// Includes the REAL index header. For each new shape: plan constants (dims, expert and shared-expert word counts), every tile of every k-slice of the gate / up / down+// matrix inside its matrix for routed bits and shared bits (native layout), the workspace offsets are monotonic and 256-aligned. For the grouped GEMV (CFG 0/1/2): the+// staged-word count WNT * TWORDS never exceeds the padded load window, and the last group of a matrix reads inside the matrix.+#include "exl3_moe_fused_idx.h"+#include <cstdio>+#include <cstdlib>+static int fails = 0;+#define CHECK(c, ...) do { if (!(c)) { if (fails < 20) { printf("FAIL %s: ", #c); printf(__VA_ARGS__); printf("\n"); } ++fails; } } while (0)+static void matrix(int bits, int ks, int nt, const char* what)+{+ const long words = mf::mat_words(bits, ks, nt);+ CHECK(words == (long) ks * nt * 8 * bits, "%s words", what);+ for (int tile = 0; tile < nt; ++tile)+ for (int s = 0; s < ks; ++s)+ for (int g8 = 0; g8 < 8; ++g8)+ {+ const long base = mf::tile_off(true, bits, ks, nt, tile), ss = mf::tile_ss(true, bits, nt);+ const long own = base + mf::own_i(bits, g8, s, ss), wrp = base + mf::wrp_i(bits, g8, s, ss);+ CHECK(own >= 0 && own + bits <= words && wrp >= 0 && wrp < words, "%s range bits=%d tile=%d s=%d", what, bits, tile, s);+ }+}+template <class S> static void shape(const char* name)+{+ using DM = mf::Dm<S>;+ printf("shape %s RB=%d SB=%d\n", name, S::RB, S::SB);+ CHECK(DM::GU_GRP == DM::KSG * 16 * S::RB && DM::SH_GU_GRP == DM::KSG * 16 * S::SB, "group words");+ CHECK(DM::EXPERT_U32 == 2L * DM::NGU * DM::GU_GRP + (long) DM::NGD * DM::DN_GRP, "expert words");+ // an expert is gate + up (D x INTER) + down (INTER x D): 3 * D * INTER * bits / 16 ... in u32: 3 * (D / 16) * (INTER / 16) * 8 * bits+ CHECK(DM::EXPERT_U32 == 3L * (S::D / 16) * (S::INTER / 16) * 8 * S::RB, "expert size independent");+ CHECK(DM::SHARED_U32 == 3L * (S::D / 16) * (S::INTER / 16) * 8 * S::SB, "shared size independent");+ matrix(S::RB, S::D / 16, S::INTER / 16, "routed gate/up");+ matrix(S::RB, S::INTER / 16, S::D / 16, "routed down");+ matrix(S::SB, S::D / 16, S::INTER / 16, "shared gate/up");+ matrix(S::SB, S::INTER / 16, S::D / 16, "shared down");+}+int main()+{+ shape<mf::QwenK4S6>("QwenK4S6"); shape<mf::QwenK5S6>("QwenK5S6"); shape<mf::SmallK4S6>("SmallK4S6"); shape<mf::SmallK5S6>("SmallK5S6");+ // grouped GEMV staging: TWORDS = 8 * bits per tile, WNT tiles per warp (CFG 0: 2, CFG 1/2: 4), loads of LSTRIDE = min(TWORDS, 32) lanes+ for (int bits = 2; bits <= 6; ++bits)+ for (int cfg = 0; cfg < 3; ++cfg)+ {+ const int wnt = cfg == 0 ? 2 : 4, tw = 8 * bits, ls = bits == 2 ? 2 * tw : (tw < 32 ? tw : 32);+ const int loads = (wnt * tw + ls - 1) / ls;+ CHECK(loads * ls >= wnt * tw && loads * ls < wnt * tw + ls, "load cover bits=%d cfg=%d", bits, cfg);+ // guarded tail: words staged = exactly wnt * tw+ int staged = 0;+ for (int l = 0; l < loads; ++l) for (int lane = 0; lane < 32; ++lane) if (lane < ls && l * ls + lane < wnt * tw) ++staged;+ CHECK(staged == wnt * tw, "staged words bits=%d cfg=%d", bits, cfg);+ // last output group of the matrices of the real 5 bpw pack (gate/up N=640, down N=2560): group * WNT * TWORDS + staged words <= ntiles * TWORDS+ const int ns[] = {640, 2560};+ for (int n : ns)+ {+ const int ntiles = n / 16, groups = n / (wnt * 16);+ CHECK(n % (wnt * 16) == 0, "N divisible bits=%d cfg=%d n=%d", bits, cfg, n);+ CHECK((long) (groups - 1) * wnt * tw + wnt * tw == (long) ntiles * tw, "last group ends at the slice end bits=%d cfg=%d n=%d", bits, cfg, n);+ }+ }+ printf(fails ? "K56 CHECK FAILED (%d)\n" : "K56 CHECK OK\n", fails);+ return fails ? 1 : 0;+}diff --git a/tests/test_moe_k56_cpu.py b/tests/test_moe_k56_cpu.pynew file mode 100644index 0000000..fdf709c--- /dev/null+++ b/tests/test_moe_k56_cpu.py@@ -0,0 +1,61 @@+"""CPU proofs for the 5 bpw routes: fused shapes K4S6 / K5S6 (index and size arithmetic), grouped GEMV staging bounds for K5 / K6, and source guards+that every dispatch carries the new bitrates and that unsupported ones fail loudly. No GPU."""+import os, re, subprocess+import pytest++ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))+QD = os.path.join(ROOT, "exllamav3", "exllamav3_ext", "quant")+CPP = os.path.join(ROOT, "tests", "moe_fused", "k56_check.cpp")++def _run(src, out, *flags):+ r = subprocess.run(["g++", "-std=c++17", "-O1", "-I", QD, *flags, src, "-o", out], capture_output=True, text=True)+ assert r.returncode == 0, r.stderr+ return subprocess.run([out], capture_output=True, text=True)++def _read(name):+ return open(os.path.join(QD, name)).read()++def test_k56_shapes_and_staging(tmp_path):+ r = _run(CPP, str(tmp_path / "chk"))+ assert r.returncode == 0 and "K56 CHECK OK" in r.stdout, r.stdout++def test_k56_check_fails_on_wrong_shared_bits(tmp_path):+ src = open(CPP).read().replace("DM::SHARED_U32 == 3L * (S::D / 16) * (S::INTER / 16) * 8 * S::SB", "DM::SHARED_U32 == 3L * (S::D / 16) * (S::INTER / 16) * 8 * S::RB")+ p = tmp_path / "mut.cpp"; p.write_text(src)+ assert _run(str(p), str(tmp_path / "mut")).returncode != 0++def test_new_shapes_are_instantiated_everywhere():+ for s in ("QwenK4S6", "QwenK5S6", "SmallK4S6", "SmallK5S6"):+ assert f"X(mf::{s})" in _read("exl3_moe_fused.cu")+ assert f"X9D(mf::{s})" in _read("exl3_moe_fused9.cu")+ assert f"XW(mf::{s})" in _read("exl3_moe_fused9.cu")++def test_grouped_decode_dispatch_covers_k5_k6_and_fails_loudly():+ t = _read("exl3_gemv.cu")+ m = re.search(r"static void launch_moe_grouped_k.*?\n\}\n", t, re.S).group(0)+ assert "case 10: launch_moe_grouped<5, false" in m and "case 12: launch_moe_grouped<6, false" in m+ assert "default: TORCH_CHECK(false" in m and "default: launch_moe_grouped<4" not in m+ assert "k2 == 10 || k2 == 12" in t++def test_wmma_prefill_dispatch_covers_k5_k6():+ t = _read("exl3_moe_prefill.cu")+ assert t.count("case 10: MPW2_K(5, false)") == 1 and t.count("case 12: MPW2_K(6, false)") == 1+ assert t.count("case 10: MPW_K(5, false)") == 1 and t.count("case 12: MPW_K(6, false)") == 1++def test_dense_decode_dispatch_covers_k7():+ t = _read("exl3_dec.cu")+ assert "kb2 <= 14" in t and "case 14: { constexpr int KB2 = 14;" in t and "case 14: gemv_kernel<14, CB>" in t+ # the routed-MoE launches must still reject K7+ assert "moe_gu_kernel<14" not in t and "moe_down_kernel<14" not in t++def test_python_gates():+ b = open(os.path.join(ROOT, "exllamav3", "modules", "block_sparse_mlp.py")).read()+ assert b.count("multi.K in (2, 2.5, 3, 4, 5, 6)") == 2+ e = open(os.path.join(ROOT, "exllamav3", "modules", "quant", "exl3.py")).read()+ assert "_DEC_KB2_DENSE = _DEC_KB2 + (14,)" in e+ assert re.search(r"def dec_supported.*?_DEC_KB2_DENSE", e, re.S)+ assert re.search(r"def dec_moe_supported.*?in _DEC_KB2 and", e, re.S)++def test_dense_k7_tile_windows_match_engine(tmp_path):+ r = _run(os.path.join(ROOT, "tests", "moe_fused", "dec_k7_check.cpp"), str(tmp_path / "dk7"))+ assert r.returncode == 0 and "DEC K7 CHECK OK" in r.stdout, r.stdout