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++
metal: gemv_pair (shared expert gate + up + SwiGLU in one dispatch), raw-activation Q2_0 GEMV; pair measured slower
Niko12215f18e8dcf803
diff --git a/metal/kernels/gemv.metal b/metal/kernels/gemv.metalindex 9d3294417..b3aae3fc9 100644--- a/metal/kernels/gemv.metal+++ b/metal/kernels/gemv.metal@@ -80,3 +80,62 @@ kernel void gemv(device const uchar* W [[buffer(0)]], device const float* xp [[bGV_SET(T_Q2_0) GV_SET(T_IQ3_S) GV_SET(T_Q3_K) GV_SET(T_Q4_K) GV_SET(T_Q5_K) GV_SET(T_Q6_K)#endifGEMV_TYPES++// The shared expert's gate and up in one dispatch: h[t][r] = silu(Wg[r] . x[t]) * (Wu[r] . x[t]), each with its own+// format (and so its own prepared activation kind); h is written raw (its consumer reads raw activations).+// KS = 1; tiles as gemv. Buffers: Wg, xg (Wg's kind), Wu, xu (Wu's kind), s16, h, args (row_bytes = Wg's), rb_up.+template <int F1, int F2, int LPR, int SG>+kernel void gemv_pair(device const uchar* Wg [[buffer(0)]], device const float* xg [[buffer(1)]],+ device const uchar* Wu [[buffer(2)]], device const float* xu [[buffer(3)]],+ device const float* s16 [[buffer(4)]], device float* h [[buffer(5)]],+ constant GemvArgs& a [[buffer(6)]], constant uint& rb_up [[buffer(7)]],+ uint3 tg [[threadgroup_position_in_grid]], ushort sg [[simdgroup_index_in_threadgroup]],+ ushort lane [[thread_index_in_simdgroup]], ushort tid [[thread_index_in_threadgroup]]) {+ constexpr int U1 = fmt<F1>::UNIT, U2 = fmt<F2>::UNIT, RG = 32 / LPR;+ constexpr int TB = fmt<F1>::TAB > fmt<F2>::TAB ? fmt<F1>::TAB : fmt<F2>::TAB;+ threadgroup uint tab1[fmt<F1>::TAB];+ threadgroup uint tab2[fmt<F2>::TAB];+ (void)TB;+ load_tab<F1>(tab1, tid, SG * 32);+ load_tab<F2>(tab2, tid, SG * 32);+ threadgroup_barrier(mem_flags::mem_threadgroup);+ const int li = lane % LPR;+ const int row = (int(tg.x) * SG + sg) * RG + lane / LPR;+ const int rr = min(row, int(a.n_out) - 1);+ device const uchar* rg = Wg + ulong(rr) * a.row_bytes;+ device const uchar* ru = Wu + ulong(rr) * rb_up;+ const int s16n = int(a.n_in) / 16;+ float ag[8], au[8];+ for (int t = 0; t < 8; ++t) ag[t] = au[t] = 0.0f;+ for (int u = li; u < int(a.n_in) / U1; u += LPR) {+ float q[U1];+ float4 sc;+ Dec<F1>::run(rg, u, q, sc, tab1);+ for (int t = 0; t < NTOK; ++t) {+ float xs[U1], xsum[2];+ X<F1>::load(xg + t * int(a.x_stride), s16 + t * s16n, u, xs, xsum);+ ag[t] += unit_combine<U1>(q, sc, xs, xsum);+ }+ }+ for (int u = li; u < int(a.n_in) / U2; u += LPR) {+ float q[U2];+ float4 sc;+ Dec<F2>::run(ru, u, q, sc, tab2);+ for (int t = 0; t < NTOK; ++t) {+ float xs[U2], xsum[2];+ X<F2>::load(xu + t * int(a.x_stride), s16 + t * s16n, u, xs, xsum);+ au[t] += unit_combine<U2>(q, sc, xs, xsum);+ }+ }+ for (int t = 0; t < NTOK; ++t) {+ const float gv = lane_sum<LPR>(ag[t]), uv = lane_sum<LPR>(au[t]);+ if (li == 0 && row < int(a.n_out)) h[t * int(a.y_stride) + row] = gv / (1.0f + exp(-gv)) * uv;+ }+}++#define GP_(F1, F2) template [[host_name("gemv_pair_" #F1 "_" #F2)]] kernel decltype(gemv_pair<F1, F2, 32, 4>) gemv_pair<F1, F2, 32, 4>;+#define GP(F1, F2) GP_(F1, F2)+#define GP_ROW(F1) GP(F1, T_IQ3_S) GP(F1, T_IQ4_XS) GP(F1, T_Q2_0) GP(F1, T_Q3_K) GP(F1, T_Q4_K) GP(F1, T_Q5_K) GP(F1, T_Q6_K)+GP_ROW(T_IQ3_S) GP_ROW(T_IQ4_XS) GP_ROW(T_Q2_0) GP_ROW(T_Q3_K) GP_ROW(T_Q4_K) GP_ROW(T_Q5_K) GP_ROW(T_Q6_K)+// Q2_0 with raw activations (the shared expert's down reading the pair's raw h)+GV_SET(T_Q2_0R)diff --git a/metal/tests/pair_parity.cpp b/metal/tests/pair_parity.cppnew file mode 100644index 000000000..1e40d19f7--- /dev/null+++ b/metal/tests/pair_parity.cpp@@ -0,0 +1,118 @@+// metal/tests/pair_parity.cpp - gemv_pair (the shared expert's gate + up + SwiGLU in one dispatch) vs double, for+// format pairs of the packs, 1 / 3 / 8 tokens; and the raw-activation Q2_0 GEMV (type 142) the down then uses.+#include "mtl_rt.h"+#include "ref_quant.h"++#include <cmath>+#include <cstdio>+#include <string>+#include <utility>+#include <vector>++namespace {+struct GemvArgs { uint32_t n_in, n_out, n_tok, row_bytes, x_stride, y_stride; };+int xkind(int t) {+ switch (t) {+ case ref::Q2_0: return 1;+ case ref::Q4_K: case ref::Q5_K: return 2;+ case ref::Q6_K: case ref::Q3_K: return 3;+ default: return 0;+ }+}+} // namespace++int main() {+ mtl::Context cx;+ const int lib = cx.compile({"quant.metal", "units.metal", "gemv.metal"});+ mtl::Pipeline pprep = cx.pipeline(lib, "xprep");+ const int NIN = 2560, NOUT = 640, MAXT = 8;+ mtl::Buffer x = cx.alloc(4 * NIN * MAXT), xp = cx.alloc(4 * (4 * NIN * MAXT + NIN / 16 * MAXT)), h = cx.alloc(4 * NOUT * MAXT);+ ref::Rng r(4);+ for (int i = 0; i < NIN * MAXT; ++i) x.as<float>()[i] = r.sym();+ {+ mtl::Dispatch d;+ d.p = &pprep;+ const uint32_t n = NIN, nt = MAXT;+ d.args = {mtl::Arg::B(x), mtl::Arg::B(xp), mtl::Arg::V(n), mtl::Arg::V(nt)};+ d.tg[0] = 256;+ d.grid[0] = (NIN / 16 * MAXT + 255) / 256;+ cx.run({d});+ }+ const size_t plane = (size_t)4 * NIN * MAXT;+ const std::vector<std::pair<int, int>> pairs = {{ref::Q2_0, ref::Q3_K}, {ref::Q3_K, ref::Q2_0}, {ref::IQ4_XS, ref::IQ3_S},+ {ref::Q4_K, ref::Q6_K}, {ref::Q5_K, ref::Q4_K}, {ref::Q6_K, ref::IQ3_S},+ {ref::Q2_0, ref::Q2_0}, {ref::IQ3_S, ref::Q4_K}};+ int fails = 0;+ for (auto [fg, fu] : pairs) {+ const size_t rg = ref::row_bytes(fg, NIN), ru = ref::row_bytes(fu, NIN);+ mtl::Buffer Wg = cx.alloc(rg * NOUT), Wu = cx.alloc(ru * NOUT);+ ref::random_blocks(fg, Wg.as<uint8_t>(), NOUT, NIN, 7 + fg, 0.02f);+ ref::random_blocks(fu, Wu.as<uint8_t>(), NOUT, NIN, 9 + fu, 0.02f);+ std::vector<float> wg((size_t)NIN * NOUT), wu((size_t)NIN * NOUT);+ ref::dequant(fg, Wg.ptr, wg.data(), wg.size());+ ref::dequant(fu, Wu.ptr, wu.data(), wu.size());+ for (int nt : {1, 3, 8}) {+ mtl::Pipeline p = cx.pipeline(lib, "gemv_pair_" + std::to_string(fg) + "_" + std::to_string(fu),+ mtl::Constants().i(0, nt));+ GemvArgs a{(uint32_t)NIN, (uint32_t)NOUT, (uint32_t)nt, (uint32_t)rg, (uint32_t)NIN, (uint32_t)NOUT};+ const uint32_t rbu = (uint32_t)ru;+ mtl::Dispatch d;+ d.p = &p;+ d.args = {mtl::Arg::B(Wg), mtl::Arg::B(xp, plane * xkind(fg)), mtl::Arg::B(Wu), mtl::Arg::B(xp, plane * xkind(fu)),+ mtl::Arg::B(xp, plane * 4), mtl::Arg::B(h), mtl::Arg::V(a), mtl::Arg::V(rbu)};+ d.grid[0] = NOUT / 4;+ d.tg[0] = 128;+ cx.run({d});+ double se = 0, sr = 0;+ for (int t = 0; t < nt; ++t)+ for (int o = 0; o < NOUT; ++o) {+ double g = 0, u = 0;+ for (int k = 0; k < NIN; ++k) {+ g += (double)wg[(size_t)o * NIN + k] * x.as<float>()[t * NIN + k];+ u += (double)wu[(size_t)o * NIN + k] * x.as<float>()[t * NIN + k];+ }+ const double ref = g / (1.0 + std::exp(-g)) * u;+ const double e = h.as<float>()[t * NOUT + o] - ref;+ se += e * e;+ sr += ref * ref;+ }+ const double rel = std::sqrt(se / sr);+ fails += !(rel < 1e-5);+ std::printf("pair %-7s + %-7s ntok %d: rel L2 %.2e %s\n", ref::type_name(fg), ref::type_name(fu), nt, rel,+ rel < 1e-5 ? "PASS" : "FAIL");+ }+ cx.free(Wg);+ cx.free(Wu);+ }+ // the raw-activation Q2_0 GEMV+ {+ const int ni = 640, no = 2560;+ const size_t rb = ref::row_bytes(ref::Q2_0, ni);+ mtl::Buffer W = cx.alloc(rb * no), y = cx.alloc(4 * no * MAXT);+ ref::random_blocks(ref::Q2_0, W.as<uint8_t>(), no, ni, 3, 0.02f);+ std::vector<float> wf((size_t)ni * no);+ ref::dequant(ref::Q2_0, W.ptr, wf.data(), wf.size());+ mtl::Pipeline p = cx.pipeline(lib, "gemv_142_8_1_4_1", mtl::Constants().i(0, 3));+ GemvArgs a{(uint32_t)ni, (uint32_t)no, 3, (uint32_t)rb, (uint32_t)ni, (uint32_t)no};+ mtl::Dispatch d;+ d.p = &p;+ d.args = {mtl::Arg::B(W), mtl::Arg::B(x), mtl::Arg::B(x), mtl::Arg::B(y), mtl::Arg::V(a)};+ d.grid[0] = no / 16;+ d.tg[0] = 128;+ cx.run({d});+ double se = 0, sr = 0;+ for (int t = 0; t < 3; ++t)+ for (int o = 0; o < no; ++o) {+ double ref = 0;+ for (int k = 0; k < ni; ++k) ref += (double)wf[(size_t)o * ni + k] * x.as<float>()[t * ni + k];+ const double e = y.as<float>()[t * no + o] - ref;+ se += e * e;+ sr += ref * ref;+ }+ const double rel = std::sqrt(se / sr);+ fails += !(rel < 1e-5);+ std::printf("Q2_0 raw-x GEMV 640 -> 2560, 3 tokens: rel L2 %.2e %s\n", rel, rel < 1e-5 ? "PASS" : "FAIL");+ }+ std::printf("%s\n", fails ? "SOME FAILED" : "ALL PASS");+ return fails ? 1 : 0;+}diff --git a/metal/tests/token_chain.cpp b/metal/tests/token_chain.cppindex 671224a3e..b6774ae98 100644--- a/metal/tests/token_chain.cpp+++ b/metal/tests/token_chain.cpp@@ -171,8 +171,10 @@ int main(int argc, char** argv) {return &it->second;};// ---- GEMV by tensor, with a tile from the measured table- auto gemv2 = [&](const gguf::Tensor& t, const Act& x, const mtl::Buffer& y, size_t y_stride, const std::string& c) {- const int n_in = (int)t.shape[0], n_out = t.shape.size() > 1 ? (int)t.shape[1] : 1, ty = t.type;+ auto gemv2 = [&](const gguf::Tensor& t, const Act& x, const mtl::Buffer& y, size_t y_stride, const std::string& c,+ int ty_override = -1) {+ const int n_in = (int)t.shape[0], n_out = t.shape.size() > 1 ? (int)t.shape[1] : 1;+ const int ty = ty_override >= 0 ? ty_override : t.type;int L = 16, Rr = 1, S = 4, K = 1;if (n_in >= 6144) { L = 32; Rr = 1; S = 8; K = 2; }else if (n_in == 640) { L = 8; Rr = 1; S = 4; K = 1; }@@ -180,7 +182,7 @@ int main(int argc, char** argv) {const std::string fn = "gemv_" + std::to_string(ty) + "_" + std::to_string(L) + "_" + std::to_string(Rr) + "_" +std::to_string(S) + "_" + std::to_string(K);const mtl::Pipeline* p = Pn(lib, fn, fn + "#" + std::to_string(ntok), mtl::Constants().i(0, ntok));- GemvArgs a{(uint32_t)n_in, (uint32_t)n_out, (uint32_t)ntok, (uint32_t)ref::row_bytes(ty, n_in), (uint32_t)n_in,+ GemvArgs a{(uint32_t)n_in, (uint32_t)n_out, (uint32_t)ntok, (uint32_t)ref::row_bytes(t.type, n_in), (uint32_t)n_in,(uint32_t)y_stride};mtl::Dispatch d;d.p = p;@@ -405,9 +407,27 @@ int main(int argc, char** argv) {gr_read(b + "hc_ffn", true, XF, false);gemv2(T(b + "ffn_gate_inp.weight"), XF, logits, 512, "router");nb = true;- gemv2(T(b + "ffn_gate_shexp.weight"), XF, gsh, NFF, "shared");- nb = true;- gemv2(T(b + "ffn_up_shexp.weight"), XF, ush, NFF, "shared");+ const bool pair = fuse && std::getenv("CHAIN_PAIR"); // measured slower (+0.11 ms): opt-in+ if (pair) {+ // the shared expert's gate + up + SwiGLU in one dispatch, h raw into XH's raw plane+ const gguf::Tensor &tgs = T(b + "ffn_gate_shexp.weight"), &tus = T(b + "ffn_up_shexp.weight");+ const std::string fn = "gemv_pair_" + std::to_string(tgs.type) + "_" + std::to_string(tus.type);+ mtl::Dispatch d;+ d.p = Pn(lib, fn, fn + "#" + std::to_string(ntok), mtl::Constants().i(0, ntok));+ GemvArgs ga{(uint32_t)N, (uint32_t)NFF, (uint32_t)ntok, (uint32_t)ref::row_bytes(tgs.type, N), (uint32_t)N,+ (uint32_t)NFF};+ const uint32_t rbu = (uint32_t)ref::row_bytes(tus.type, N);+ d.args = {mtl::Arg::B(W, tgs.offset), mtl::Arg::B(XF.b, XF.plane_bytes() * xkind(tgs.type)),+ mtl::Arg::B(W, tus.offset), mtl::Arg::B(XF.b, XF.plane_bytes() * xkind(tus.type)),+ mtl::Arg::B(XF.b, XF.plane_bytes() * 4), mtl::Arg::B(XH.b), mtl::Arg::V(ga), mtl::Arg::V(rbu)};+ d.grid[0] = NFF / 4;+ d.tg[0] = 128;+ push(d, "shared", (double)tgs.bytes + tus.bytes);+ } else {+ gemv2(T(b + "ffn_gate_shexp.weight"), XF, gsh, NFF, "shared");+ nb = true;+ gemv2(T(b + "ffn_up_shexp.weight"), XF, ush, NFF, "shared");+ }nb = true;gemv2(T(b + "ffn_gate_inp_shexp.weight"), XF, sg, 1, "shared");{@@ -447,7 +467,8 @@ int main(int argc, char** argv) {d.grid[1] = ntok;d.tg[0] = 16;nb = true;- push(d, "shared", 0);+ if (!pair) push(d, "shared", 0);+ nb = false;}const std::string gun = "moe_gu_" + std::to_string(tg_.type) + "_" + std::to_string(gl) + "_" +std::to_string(gr) + "_" + std::to_string(gs);@@ -462,7 +483,11 @@ int main(int argc, char** argv) {g.ind_off = P_IGU * 4;push(g, "experts", 10.0 * ntok * 2 * a.gu_stride);nb = true;- gemv2(T(b + "ffn_down_shexp.weight"), XH, ysh, N, "shared");+ {+ const gguf::Tensor& tds = T(b + "ffn_down_shexp.weight");+ // with --fuse h is raw: a Q2_0 down reads it through the raw-activation decoder (type 142)+ gemv2(tds, XH, ysh, N, "shared", pair && tds.type == ref::Q2_0 ? 142 : -1);+ }mtl::Dispatch dd;dd.p = Pn(lib, dnn, dnn, {});dd.args = {mtl::Arg::B(W), mtl::Arg::B(h), mtl::Arg::B(ye), mtl::Arg::B(plan), mtl::Arg::V(a), mtl::Arg::B(h)};