Skip to content

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.

Fused MoE: add the K6 instance (routed K6 / shared K6) for the MTP drafter layer

YanissAmz7de1ac29861d
diff --git a/exllamav3/exllamav3_ext/quant/exl3_moe_fused.cu b/exllamav3/exllamav3_ext/quant/exl3_moe_fused.cu
index 5f39bd3..d249f96 100644
--- a/exllamav3/exllamav3_ext/quant/exl3_moe_fused.cu
+++ b/exllamav3/exllamav3_ext/quant/exl3_moe_fused.cu
@@ -102,7 +102,7 @@ template <class S> void mf_launch
}
} // namespace
-#define MF_SHAPES(X) X(mf::QwenShape) X(mf::SmallTestShape) X(mf::SmallK3Shape) X(mf::QwenK3S4) X(mf::QwenK3S5) X(mf::QwenK4S4) X(mf::QwenK4S5) X(mf::SmallK3S4) X(mf::SmallK3S5) X(mf::SmallK4S4) X(mf::SmallK4S5)
+#define MF_SHAPES(X) X(mf::QwenShape) X(mf::SmallTestShape) X(mf::SmallK3Shape) X(mf::QwenK3S4) X(mf::QwenK3S5) X(mf::QwenK4S4) X(mf::QwenK4S5) X(mf::SmallK3S4) X(mf::SmallK3S5) X(mf::SmallK4S4) X(mf::SmallK4S5) X(mf::QwenK6S6) X(mf::SmallK6S6)
bool exl3_moe_fused_supported(int64_t D, int64_t H, int64_t LR, int64_t NEXP, int64_t TOPK, int64_t INTER, int64_t RB, int64_t SB)
{
@@ -193,6 +193,7 @@ void exl3_moe_fused_tile_test(const at::Tensor& A, const at::Tensor& B, at::Tens
else if (bits == 3) ok = mf_tt_dispatch<3>(ks, cf, A, B, C, R, nt, (int) native, stream);
else if (bits == 4) ok = mf_tt_dispatch<4>(ks, cf, A, B, C, R, nt, (int) native, stream);
else if (bits == 5) ok = mf_tt_dispatch<5>(ks, cf, A, B, C, R, nt, (int) native, stream);
+ else if (bits == 6) ok = mf_tt_dispatch<6>(ks, cf, A, B, C, R, nt, (int) native, stream);
TORCH_CHECK(ok, "tile test: (bits, K, output dtype) not instantiated");
}
#endif
diff --git a/exllamav3/exllamav3_ext/quant/exl3_moe_fused.cuh b/exllamav3/exllamav3_ext/quant/exl3_moe_fused.cuh
index aca8bc0..4217540 100644
--- a/exllamav3/exllamav3_ext/quant/exl3_moe_fused.cuh
+++ b/exllamav3/exllamav3_ext/quant/exl3_moe_fused.cuh
@@ -203,6 +203,14 @@ __device__ __forceinline__ void mf_dq8_k5(const uint32_t (&w)[6], const int t, F
exl3_gemv_ns::decode8<2>(v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7], f0, f1);
}
+// K6 decode of one engine lane (t) from the lane set {wrap, own0..own5}: same arithmetic as the engine's dq4<6, cb> twice; the word extraction is the CPU-proven kn_words
+__device__ __forceinline__ void mf_dq8_k6(const uint32_t (&w)[7], const int t, FragB& f0, FragB& f1)
+{
+ unsigned v[8];
+ kn_words(6, w, t, v);
+ exl3_gemv_ns::decode8<2>(v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7], f0, f1);
+}
+
// ---------------------------------------------------------------- VALU tile on a trellis tile (native or repacked layout)
// One wave = one 16-column tile, all k. Same decode body as core3's mv_tile; Bt = base of the tile's first k-slice, ss = u32 between k-slices.
// Lane (cq, g8): g8 = column pair (g8, g8 + 8), cq = chunk lane. Words: wrap word + `BITS` own words; K3 own words are three scalar loads (3 g8 is not
@@ -212,7 +220,7 @@ template <int BITS, bool CF32, int RW, int KS>
__device__ __forceinline__ void mf_tile(const uint32_t* __restrict__ A32, const uint32_t* __restrict__ Bt, long ss, void* __restrict__ C,
int size_n, int tile, float* red, int wave, int lane)
{
- static_assert(BITS >= 2 && BITS <= 5, "K1, K6.. need their own per-lane word set and decode body: not implemented");
+ static_assert(BITS >= 2 && BITS <= 6, "K1, K7.. need their own per-lane word set and decode body: not implemented");
constexpr int NWD = BITS + 1, CH = (KS + 15) / 16, NRND = 4, K = KS * 16;
constexpr int VU = CH > 5 ? 5 : CH;
const int g8 = lane & 7, cq = lane >> 3;
@@ -227,6 +235,7 @@ __device__ __forceinline__ void mf_tile(const uint32_t* __restrict__ A32, const
if constexpr (BITS == 2) { const uint2 v = *(const uint2*) q; w[1] = v.x; w[2] = v.y; }
else if constexpr (BITS == 3) { w[1] = q[0]; w[2] = q[1]; w[3] = q[2]; }
else if constexpr (BITS == 5) { w[1] = q[0]; w[2] = q[1]; w[3] = q[2]; w[4] = q[3]; w[5] = q[4]; }
+ else if constexpr (BITS == 6) { const uint2 v0 = *(const uint2*) q, v1 = *(const uint2*) (q + 2), v2 = *(const uint2*) (q + 4); w[1] = v0.x; w[2] = v0.y; w[3] = v1.x; w[4] = v1.y; w[5] = v2.x; w[6] = v2.y; }
else { const uint4 v = *(const uint4*) q; w[1] = v.x; w[2] = v.y; w[3] = v.z; w[4] = v.w; }
};
auto ldst = [&](uint32_t (&w)[VU][NWD], int rnd, int sg0)
@@ -263,6 +272,7 @@ __device__ __forceinline__ void mf_tile(const uint32_t* __restrict__ A32, const
if constexpr (BITS == 2) exl3_gemv_ns::dq8_regs_2bits<2>(wa[u][t >> 1], wa[u][1 + (t >> 1)], t << 3, f0[t], f1[t]);
else if constexpr (BITS == 3) exl3_gemv_ns::dq8_regs_3bits<2>(wa[u][t == 0 ? 0 : (t == 1 ? 1 : 2)], wa[u][t == 0 ? 1 : (t == 1 ? 2 : 3)], t == 0 ? 8 : (t == 1 ? 16 : (t == 2 ? 24 : 0)), f0[t], f1[t]);
else if constexpr (BITS == 5) mf_dq8_k5(wa[u], t, f0[t], f1[t]);
+ else if constexpr (BITS == 6) mf_dq8_k6(wa[u], t, f0[t], f1[t]);
else exl3_gemv_ns::dq8_regs_4bits<2>(wa[u][t], wa[u][t + 1], f0[t], f1[t]);
}
#pragma unroll
diff --git a/exllamav3/exllamav3_ext/quant/exl3_moe_fused7_mv.cuh b/exllamav3/exllamav3_ext/quant/exl3_moe_fused7_mv.cuh
index a02347e..6a4dc8d 100644
--- a/exllamav3/exllamav3_ext/quant/exl3_moe_fused7_mv.cuh
+++ b/exllamav3/exllamav3_ext/quant/exl3_moe_fused7_mv.cuh
@@ -55,7 +55,7 @@ template <int BITS, bool CF32, int RW, int KS, int ABL>
__device__ __forceinline__ void mf7_tile(const uint32_t* __restrict__ A32, const uint32_t* __restrict__ Bt, long ss, void* __restrict__ C,
int size_n, int tile, float* red, int wave, int lane)
{
- static_assert(BITS >= 2 && BITS <= 5, "K1, K6.. need their own per-lane word set and decode body: not implemented");
+ static_assert(BITS >= 2 && BITS <= 6, "K1, K7.. need their own per-lane word set and decode body: not implemented");
constexpr int NWD = BITS + 1, CH = (KS + 15) / 16, NRND = 4, K = KS * 16;
constexpr int VU = CH > 5 ? 5 : CH;
const int g8 = lane & 7, cq = lane >> 3;
@@ -71,6 +71,7 @@ __device__ __forceinline__ void mf7_tile(const uint32_t* __restrict__ A32, const
if constexpr (BITS == 2) { const uint2 v = *(const uint2*) q; w[1] = v.x; w[2] = v.y; }
else if constexpr (BITS == 3) { w[1] = q[0]; w[2] = q[1]; w[3] = q[2]; }
else if constexpr (BITS == 5) { w[1] = q[0]; w[2] = q[1]; w[3] = q[2]; w[4] = q[3]; w[5] = q[4]; }
+ else if constexpr (BITS == 6) { const uint2 v0 = *(const uint2*) q, v1 = *(const uint2*) (q + 2), v2 = *(const uint2*) (q + 4); w[1] = v0.x; w[2] = v0.y; w[3] = v1.x; w[4] = v1.y; w[5] = v2.x; w[6] = v2.y; }
else { const uint4 v = *(const uint4*) q; w[1] = v.x; w[2] = v.y; w[3] = v.z; w[4] = v.w; }
};
auto ldst = [&](uint32_t (&w)[VU][NWD], int rnd, int sg0)
@@ -114,6 +115,7 @@ __device__ __forceinline__ void mf7_tile(const uint32_t* __restrict__ A32, const
else if constexpr (BITS == 2) exl3_gemv_ns::dq8_regs_2bits<2>(wa[u][t >> 1], wa[u][1 + (t >> 1)], t << 3, f0[t], f1[t]);
else if constexpr (BITS == 3) exl3_gemv_ns::dq8_regs_3bits<2>(wa[u][t == 0 ? 0 : (t == 1 ? 1 : 2)], wa[u][t == 0 ? 1 : (t == 1 ? 2 : 3)], t == 0 ? 8 : (t == 1 ? 16 : (t == 2 ? 24 : 0)), f0[t], f1[t]);
else if constexpr (BITS == 5) mf_dq8_k5(wa[u], t, f0[t], f1[t]);
+ else if constexpr (BITS == 6) mf_dq8_k6(wa[u], t, f0[t], f1[t]);
else exl3_gemv_ns::dq8_regs_4bits<2>(wa[u][t], wa[u][t + 1], f0[t], f1[t]);
}
#pragma unroll
diff --git a/exllamav3/exllamav3_ext/quant/exl3_moe_fused9.cu b/exllamav3/exllamav3_ext/quant/exl3_moe_fused9.cu
index d034ce2..6be02db 100644
--- a/exllamav3/exllamav3_ext/quant/exl3_moe_fused9.cu
+++ b/exllamav3/exllamav3_ext/quant/exl3_moe_fused9.cu
@@ -112,6 +112,7 @@ void mf9_half(at::Tensor& x, const at::Tensor& fn, const at::Tensor& fn_scale, c
if (Match7<mf::QwenK4S5>::eq(D, H, LR, NEXP, TOPK, INTER, RB, SB) && (variant & 0x300) && timing) { TORCH_CHECK((variant & 0x70000) == 0x60000, "mf9: ablation needs 0x60000");
switch ((variant >> 8) & 3) { case 1: L7A(mf::QwenK4S5, true, 2, 1, 1); break; case 2: L7A(mf::QwenK4S5, true, 2, 1, 2); break; default: L7A(mf::QwenK4S5, true, 2, 1, 3); } return; }
X9D(mf::QwenK3S4) X9D(mf::QwenK3S5) X9D(mf::QwenK4S4) X9D(mf::QwenK4S5)
+ X9D(mf::QwenK6S6) X9D(mf::SmallK6S6)
X9D(mf::SmallK3S4) X9D(mf::SmallK3S5) X9D(mf::SmallK4S4) X9D(mf::SmallK4S5)
#undef X9D
TORCH_CHECK(false, "mf9: shape not instantiated");
diff --git a/exllamav3/exllamav3_ext/quant/exl3_moe_fused_idx.h b/exllamav3/exllamav3_ext/quant/exl3_moe_fused_idx.h
index 4fdd60c..c64e174 100644
--- a/exllamav3/exllamav3_ext/quant/exl3_moe_fused_idx.h
+++ b/exllamav3/exllamav3_ext/quant/exl3_moe_fused_idx.h
@@ -38,6 +38,8 @@ struct QwenK4S5 { static constexpr int D = 2560, H = 4, LR = 320, NEXP = 512, TO
struct SmallK3S4 { static constexpr int D = 256, H = 4, LR = 32, NEXP = 64, TOPK = 4, INTER = 128, RB = 3, SB = 4; };
struct SmallK3S5 { static constexpr int D = 256, H = 4, LR = 32, NEXP = 64, TOPK = 4, INTER = 128, RB = 3, SB = 5; };
struct SmallK4S4 { static constexpr int D = 256, H = 4, LR = 32, NEXP = 64, TOPK = 4, INTER = 128, RB = 4, SB = 4; };
+struct QwenK6S6 { static constexpr int D = 2560, H = 4, LR = 320, NEXP = 512, TOPK = 10, INTER = 640, RB = 6, SB = 6; }; // the MTP drafter layer of the public pack (routed K6, shared K6)
+struct SmallK6S6 { static constexpr int D = 256, H = 4, LR = 32, NEXP = 64, TOPK = 4, INTER = 128, RB = 6, SB = 6; };
struct SmallK4S5 { static constexpr int D = 256, H = 4, LR = 32, NEXP = 64, TOPK = 4, INTER = 128, RB = 4, SB = 5; };
template <class S>
@@ -70,7 +72,7 @@ struct Dm
static_assert(NEXP % 32 == 0 && NEXP <= 1024, "top-k lanes");
static_assert(TOPK >= 1 && TOPK <= 15, "combine uses wave <= TOPK, 16 waves");
static_assert(MAXR * TOPK <= THREADS && MAXR * TOPK <= 40, "sort/group arrays");
- static_assert((RB >= 2 && RB <= 5) && (SB >= 2 && SB <= 5), "K2, K3, K4, K5");
+ static_assert((RB >= 2 && RB <= 6) && (SB >= 2 && SB <= 6), "K2, K3, K4, K5, K6");
static_assert(H >= 1 && H <= 4, "dred/rmr scratch");
static_assert(LR % 1 == 0 && LR <= 1024, "t scratch");
// dynamic-free LDS: A + A2 for MAXR rows of the larger of D / INTER
@@ -101,6 +103,24 @@ MF_FN int k5_rel(int t, int g) { return 40
MF_FN int k5_i0(int t, int g) { return k5_rel(t, g) / 32; }
MF_FN int k5_i2(int t, int g) { return (k5_rel(t, g) + 30) / 32; }
MF_FN int k5_s2(int t, int g) { return (k5_i2(t, g) + 1) * 32 - (k5_rel(t, g) + 31); }
+// K6 (and any K whose 4-value group spans <= 2 words, K5 / K6 / K8 style): lane (g8, t) is the engine's lane L = 4 g8 + t; its group g (values 4g .. 4g + 3, engine dq4<bits>)
+// starts at bit (8L + 4g + 257) bits - 16; relative to the first bit of the lane set's wrap word (global word bits g8 - 1, cyclic) that is
+// 8 bits t + 4 bits g + bits + 16 (256 bits = 8 tiles of 32 bits cancels). The group ends 3 bits + 16 bits later; both words of the funnel shift are local
+// indices 0 .. bits of {wrap, own0 .. own(bits-1)}. K6: 34 bits per group, the engine's alignment keeps it inside two words (checked on CPU for every lane).
+MF_FN int kn_rel(int bits, int t, int g) { return 8 * bits * t + 4 * bits * g + bits + 16; }
+MF_FN int kn_i0(int bits, int t, int g) { return kn_rel(bits, t, g) / 32; }
+MF_FN int kn_i2(int bits, int t, int g) { return (kn_rel(bits, t, g) + 3 * bits + 15) / 32; }
+MF_FN int kn_s2(int bits, int t, int g) { return (kn_i2(bits, t, g) + 1) * 32 - (kn_rel(bits, t, g) + 3 * bits + 16); }
+// the 8 trellis words (16 bit each, low half) of lane (g8, t) from its lane set w[0 .. bits] (wrap, own0 ..), in the order the decoder takes them
+template <class W> MF_FN void kn_words(int bits, const W (&w)[7], int t, unsigned (&v)[8])
+{
+ for (int g = 0; g < 2; ++g)
+ {
+ const unsigned long long m = ((unsigned long long) w[kn_i0(bits, t, g)] << 32) | (unsigned long long) w[kn_i2(bits, t, g)];
+ const int s2 = kn_s2(bits, t, g);
+ for (int j = 0; j < 4; ++j) v[4 * g + 3 - j] = (unsigned) (m >> (s2 + bits * j)) & 0xffffu;
+ }
+}
MF_FN long own_i(int bits, int g8, int s, long ss) { return s * ss + (long) bits * g8; }
MF_FN long wrp_i(int bits, int g8, int s, long ss) { return s * ss + (bits * g8 > 0 ? bits * g8 - 1 : tw(bits) - 1); }
MF_FN long a_idx(int r, int k, int s) { return (long) r * (k / 2) + 8 * s; } // u32 in the A staging buffer
diff --git a/tests/moe_fused/k6_check.cpp b/tests/moe_fused/k6_check.cpp
new file mode 100644
index 0000000..9d1f77a
--- /dev/null
+++ b/tests/moe_fused/k6_check.cpp
@@ -0,0 +1,85 @@
+// CPU proof for the K6 (and K5 reference) lane word sets of the fused MoE half-layer. Includes the REAL index header of the kernel.
+// For every lane of a trellis tile (32 lanes x 2 groups) it compares the kernel's word extraction (mf::kn_words on the lane set {wrap, own0..}) with the engine's
+// dq4<bits> arithmetic (exl3_dq.cuh, copied formula by formula, cyclic over the tile), checks every index is inside the lane set / the tile, the 64 bit shifts stay < 64,
+// the group stays inside two words, the lane sets of the 8 g8 cover the tile once, and the 8 byte alignment of the uint2 loads.
+#include "exl3_moe_fused_idx.h"
+#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)
+static uint32_t fshift_eng(uint32_t b, uint32_t a, int shift) { uint64_t merged = ((uint64_t) a << 32) | (uint64_t) b; return (uint32_t) (merged >> shift); }
+int main(int argc, char** argv)
+{
+ int bitsv[] = {5, 6};
+ for (int bits : bitsv)
+ {
+ const int tw = mf::tw(bits);
+ srand(7 + bits);
+ for (int trial = 0; trial < 20; ++trial)
+ {
+ uint32_t tile[64];
+ for (int i = 0; i < tw; ++i) tile[i] = ((uint32_t) rand() << 16) ^ (uint32_t) rand() ^ ((uint32_t) rand() << 3);
+ int cover[64] = {0};
+ for (int g8 = 0; g8 < 8; ++g8)
+ {
+ const long own = mf::own_i(bits, g8, 0, 0), wrp = mf::wrp_i(bits, g8, 0, 0);
+ CHECK(own >= 0 && own + bits <= tw, "own range bits=%d g8=%d", bits, g8);
+ CHECK(wrp >= 0 && wrp < tw, "wrap range");
+ CHECK(wrp == (own + tw - 1) % tw, "wrap is the word before own (cyclic)");
+ CHECK(((own * 4) % 8) == 0 || bits % 2 == 1, "uint2 alignment of own words, bits=%d g8=%d", bits, g8);
+ for (int k = 0; k < bits; ++k) cover[own + k]++;
+ uint32_t w[7] = {0, 0, 0, 0, 0, 0, 0};
+ w[0] = tile[wrp];
+ for (int k = 0; k < bits; ++k) w[1 + k] = tile[own + k];
+ for (int t = 0; t < 4; ++t)
+ {
+ unsigned v[8];
+ mf::kn_words(bits, w, t, v);
+ const int L = 4 * g8 + t;
+ for (int g = 0; g < 2; ++g)
+ {
+ // engine dq4<bits>(ptr, t_offset = 8 L + 4 g, frag)
+ const int t_offset = 8 * L + 4 * g;
+ int b0 = (t_offset + 257) * bits - 16, b1 = b0 + 3 * bits, b2 = b1 + 16;
+ int i0 = b0 / 32, i2 = (b2 - 1) / 32, s2 = (i2 + 1) * 32 - b2;
+ uint32_t a = tile[i0 % tw], b = tile[i2 % tw];
+ uint32_t ew[4] = {fshift_eng(b, a, s2 + bits * 3) & 0xffff, fshift_eng(b, a, s2 + bits * 2) & 0xffff, fshift_eng(b, a, s2 + bits) & 0xffff, fshift_eng(b, a, s2) & 0xffff};
+ for (int j = 0; j < 4; ++j) CHECK(v[4 * g + j] == ew[j], "word mismatch bits=%d L=%d g=%d j=%d", bits, L, g, j);
+ // indices
+ const int l0 = mf::kn_i0(bits, t, g), l2 = mf::kn_i2(bits, t, g), ls = mf::kn_s2(bits, t, g);
+ CHECK(l0 >= 0 && l0 <= bits && l2 >= 0 && l2 <= bits, "local index range bits=%d L=%d g=%d (%d %d)", bits, L, g, l0, l2);
+ CHECK(l2 - l0 == 0 || l2 - l0 == 1, "group inside two words");
+ CHECK(s2 == ls && (l0 == l2) == (i0 == i2), "engine shift / word match");
+ CHECK(ls >= 0 && ls + 3 * bits < 64, "shift < 64");
+ const int gl0 = (l0 == 0 ? (int) wrp : (int) own + l0 - 1), gl2 = (l2 == 0 ? (int) wrp : (int) own + l2 - 1);
+ CHECK(gl0 == i0 % tw && gl2 == i2 % tw, "local words are the engine's words bits=%d L=%d g=%d", bits, L, g);
+ }
+ }
+ }
+ for (int i = 0; i < tw; ++i) CHECK(cover[i] == 1, "own words cover the tile once bits=%d word=%d cover=%d", bits, i, cover[i]);
+ }
+ // matrix level: for the real Qwen shapes every tile of every k-slice of every expert matrix is inside its matrix (native layout)
+ const int shapes[][2] = {{160, 40}, {40, 160}}; // (KS, NT): gate/up K2560 N640 -> KS 160 NT 40 ; down K640 N2560 -> KS 40 NT 160
+ for (auto& sh : shapes)
+ {
+ const int ks = sh[0], nt = sh[1];
+ for (int nat = 0; nat < 2; ++nat)
+ {
+ if (!nat && nt % 2) continue;
+ const long words = mf::mat_words(bits, ks, nt);
+ 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(nat, bits, ks, nt, tile), ss = mf::tile_ss(nat, 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, "matrix range bits=%d nat=%d tile=%d s=%d", bits, nat, tile, s);
+ CHECK(bits != 6 || own % 2 == 0, "uint2 loads 8-byte aligned in the matrix (even word offset) bits=%d", bits);
+ }
+ }
+ }
+ }
+ printf(fails ? "K6 CHECK FAILED (%d)\n" : "K6 CHECK OK\n", fails);
+ return fails ? 1 : 0;
+}
diff --git a/tests/test_moe_fused_k6_cpu.py b/tests/test_moe_fused_k6_cpu.py
new file mode 100644
index 0000000..42cd18b
--- /dev/null
+++ b/tests/test_moe_fused_k6_cpu.py
@@ -0,0 +1,47 @@
+"""CPU proof of the K6 instance of the fused MoE half-layer (word extraction vs the engine's dq4<6>, index bounds, alignment), with mutants that must fail,
+plus a source guard that the kernels use the proven helpers. No GPU."""
+import os, re, shutil, subprocess, sys
+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", "k6_check.cpp")
+
+MUTANTS = {
+ "rel_const": ("bits + 16; }", "bits + 17; }"),
+ "s2_offset": ("(kn_rel(bits, t, g) + 3 * bits + 16); }", "(kn_rel(bits, t, g) + 3 * bits + 15); }"),
+ "word_order": ("((unsigned long long) w[kn_i0(bits, t, g)] << 32) | (unsigned long long) w[kn_i2(bits, t, g)]", "((unsigned long long) w[kn_i2(bits, t, g)] << 32) | (unsigned long long) w[kn_i0(bits, t, g)]"),
+ "i2_end": ("(kn_rel(bits, t, g) + 3 * bits + 15) / 32", "(kn_rel(bits, t, g) + 3 * bits + 16) / 32"),
+ "wrap_index": ("bits * g8 - 1 : tw(bits) - 1", "bits * g8 - 2 : tw(bits) - 1"),
+ "stride": ("t + 4 * bits * g + bits", "t + 3 * bits * g + bits"),
+}
+
+def _build(srcdir, out):
+ r = subprocess.run(["g++", "-std=c++17", "-O1", "-I", srcdir, CPP, "-o", out], capture_output=True, text=True)
+ assert r.returncode == 0, r.stderr
+ return subprocess.run([out], capture_output=True, text=True)
+
+def test_k6_words_match_engine(tmp_path):
+ r = _build(QD, str(tmp_path / "chk"))
+ assert r.returncode == 0 and "K6 CHECK OK" in r.stdout, r.stdout
+
[email protected]("name", sorted(MUTANTS))
+def test_k6_mutants_fail(tmp_path, name):
+ d = tmp_path / "src"; d.mkdir()
+ txt = open(os.path.join(QD, "exl3_moe_fused_idx.h")).read()
+ old, new = MUTANTS[name]
+ assert old in txt, "mutant anchor missing: " + name
+ open(d / "exl3_moe_fused_idx.h", "w").write(txt.replace(old, new, 1))
+ r = _build(str(d), str(tmp_path / "chk"))
+ assert r.returncode != 0 and "FAILED" in r.stdout, "mutant survived: " + name
+
+def test_kernels_use_proven_helpers():
+ for f in ("exl3_moe_fused.cuh", "exl3_moe_fused7_mv.cuh"):
+ t = open(os.path.join(QD, f)).read()
+ assert "BITS == 6) mf_dq8_k6(wa[u], t, f0[t], f1[t])" in t
+ assert "const uint2 v0 = *(const uint2*) q, v1 = *(const uint2*) (q + 2), v2 = *(const uint2*) (q + 4); w[1] = v0.x; w[2] = v0.y; w[3] = v1.x; w[4] = v1.y; w[5] = v2.x; w[6] = v2.y;" in t
+ t = open(os.path.join(QD, "exl3_moe_fused.cuh")).read()
+ m = re.search(r"void mf_dq8_k6\(.*?\n\}\n", t, re.S)
+ assert m and "kn_words(6, w, t, v);" in m.group(0) and "decode8<2>(v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7], f0, f1)" in m.group(0)
+ assert "X9D(mf::QwenK6S6)" in open(os.path.join(QD, "exl3_moe_fused9.cu")).read()
+ assert "X(mf::QwenK6S6)" in open(os.path.join(QD, "exl3_moe_fused.cu")).read()