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: M4 window GEMV probes - gemv_x (x staged in threadgroup memory), GEMV_PROBE (function constant 9)
Niko12215565a347def5
diff --git a/metal/kernels/gemv.metal b/metal/kernels/gemv.metalindex e27545ec3..5c5884202 100644--- a/metal/kernels/gemv.metal+++ b/metal/kernels/gemv.metal@@ -16,6 +16,10 @@ struct GemvArgs {};constant int NTOK [[function_constant(0)]]; // tokens of the window (1..8): the token loops are specialised+// a bench probe (function constant 9): 1 = tokens past the first reuse token 0's x (no load; wrong values),+// 2 = tokens past the first skip the combine (wrong values)+constant int GEMV_PROBE [[function_constant(9)]];+constant int gemv_probe = is_function_constant_defined(GEMV_PROBE) ? GEMV_PROBE : 0;template <int F, int LPR, int NR, int SG, int KS>kernel void gemv(device const uchar* W [[buffer(0)]], device const float* xp [[buffer(1)]],@@ -39,14 +43,29 @@ kernel void gemv(device const uchar* W [[buffer(0)]], device const float* xp [[bdevice const uchar* rp[NR];for (int r = 0; r < NR; ++r) rp[r] = W + (ulong)min(row0 + r, int(a.n_out) - 1) * a.row_bytes;const int s16n = int(a.n_in) / 16;+ if (gemv_probe == 3) {+ // rows outer, tokens inner: one row's decoded unit in registers at a time (x re-read per row from L1)+ for (int u = u0 + li; u < u1; u += LPR)+ for (int r = 0; r < NR; ++r) {+ float q[UNIT];+ float4 sc;+ Dec<F>::run(rp[r], u, q, sc, tab);+ for (int t = 0; t < NTOK; ++t) {+ float xs[UNIT], xsum[2];+ X<F>::load(xp + t * int(a.x_stride), s16 + t * s16n, u, xs, xsum);+ acc[r][t] += unit_combine<UNIT>(q, sc, xs, xsum);+ }+ }+ } elsefor (int u = u0 + li; u < u1; u += LPR) {float q[NR][UNIT];float4 sc[NR];for (int r = 0; r < NR; ++r) Dec<F>::run(rp[r], u, q[r], sc[r], tab);+ float xs[UNIT], xsum[2];for (int t = 0; t < NTOK; ++t) {- float xs[UNIT], xsum[2];- X<F>::load(xp + t * int(a.x_stride), s16 + t * s16n, u, xs, xsum);- for (int r = 0; r < NR; ++r) acc[r][t] += unit_combine<UNIT>(q[r], sc[r], xs, xsum);+ if (gemv_probe != 1 || t == 0) X<F>::load(xp + t * int(a.x_stride), s16 + t * s16n, u, xs, xsum);+ if (gemv_probe != 2 || t == 0)+ for (int r = 0; r < NR; ++r) acc[r][t] += unit_combine<UNIT>(q[r], sc[r], xs, xsum);}}constexpr int RPT = RS * RG * NR;@@ -189,3 +208,56 @@ kernel void gemv_cat(device const uchar* W [[buffer(0)]], device const float* xp#define GVC_(F, L, R, S) template [[host_name("gemvc_" #F "_" #L "_" #R "_" #S)]] kernel decltype(gemv_cat<F, L, R, S>) gemv_cat<F, L, R, S>;#define GVC(F, L, R, S) GVC_(F, L, R, S)GVC(T_BF16, 32, 1, 4) GVC(T_BF16, 32, 1, 8) GVC(T_BF16, 16, 1, 4) GVC(T_BF16, 32, 2, 4)++// gemv_x: gemv (KS = 1) with the window's prepared x (the format's plane, NTOK rows, and its per-16 sums) staged in+// threadgroup memory once per threadgroup, so the per-unit x reads of every token come from threadgroup memory+// instead of L1 (on the M4 the x traffic through L1 - 4 B a weight a token, shared by the rows in a simdgroup - is+// what a window token costs). Same tile meaning and the same arithmetic as gemv_<F>_<LPR>_<NR>_<SG>_1: bitwise.+// Dynamic threadgroup memory 0: 4 * NTOK * (n_in + n_in / 16) bytes.+template <int F, int LPR, int NR, int SG>+kernel void gemv_x(device const uchar* W [[buffer(0)]], device const float* xp [[buffer(1)]],+ device const float* s16 [[buffer(2)]], device float* y [[buffer(3)]], constant GemvArgs& a [[buffer(4)]],+ threadgroup float* xt [[threadgroup(0)]],+ 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 UNIT = fmt<F>::UNIT, RG = 32 / LPR;+ threadgroup uint tab[fmt<F>::TAB];+ load_tab<F>(tab, tid, SG * 32);+ const int n = int(a.n_in), s16n = n / 16;+ threadgroup float* st = xt + NTOK * n;+ for (int t = 0; t < NTOK; ++t) {+ device const float4* src = (device const float4*)(xp + t * int(a.x_stride));+ threadgroup float4* dst = (threadgroup float4*)(xt + t * n);+ for (int i = tid; i < n / 4; i += SG * 32) dst[i] = src[i];+ for (int i = tid; i < s16n; i += SG * 32) st[t * s16n + i] = s16[t * s16n + i];+ }+ threadgroup_barrier(mem_flags::mem_threadgroup);+ const int li = lane % LPR;+ const int row0 = int(tg.x) * SG * RG * NR + (sg * RG + lane / LPR) * NR;+ const int U = n / UNIT;+ float acc[NR][8];+ for (int r = 0; r < NR; ++r)+ for (int t = 0; t < NTOK; ++t) acc[r][t] = 0.0f;+ device const uchar* rp[NR];+ for (int r = 0; r < NR; ++r) rp[r] = W + (ulong)min(row0 + r, int(a.n_out) - 1) * a.row_bytes;+ for (int u = li; u < U; u += LPR) {+ float q[NR][UNIT];+ float4 sc[NR];+ for (int r = 0; r < NR; ++r) Dec<F>::run(rp[r], u, q[r], sc[r], tab);+ for (int t = 0; t < NTOK; ++t) {+ float xs[UNIT], xsum[2];+ X<F>::load((threadgroup const float*)(xt + t * n), (threadgroup const float*)(st + t * s16n), u, xs, xsum);+ for (int r = 0; r < NR; ++r) acc[r][t] += unit_combine<UNIT>(q[r], sc[r], xs, xsum);+ }+ }+ for (int r = 0; r < NR; ++r)+ for (int t = 0; t < NTOK; ++t) {+ const float v = lane_sum<LPR>(acc[r][t]);+ if (li == 0 && row0 + r < int(a.n_out)) y[t * int(a.y_stride) + row0 + r] = v;+ }+}+#define GVX(F, L, R, S) template [[host_name("gemvx_" #F "_" #L "_" #R "_" #S)]] kernel decltype(gemv_x<F, L, R, S>) gemv_x<F, L, R, S>;+#define GVX_SET(F) GVX(F, 32, 1, 4) GVX(F, 32, 2, 4) GVX(F, 16, 1, 4) GVX(F, 16, 2, 4) GVX(F, 8, 1, 4) GVX(F, 8, 2, 4) \+ GVX(F, 4, 2, 4) GVX(F, 16, 2, 8) GVX(F, 8, 2, 8) GVX(F, 16, 4, 4) GVX(F, 32, 2, 8) GVX(F, 2, 2, 4)+GVX_SET(T_BF16) GVX_SET(T_Q8_0) GVX_SET(T_Q4_0) GVX_SET(T_Q5_0) GVX_SET(T_IQ4_NL) GVX_SET(T_IQ4_XS) GVX_SET(T_Q2_0)+GVX_SET(T_IQ3_S) GVX_SET(T_Q3_K) GVX_SET(T_Q4_K) GVX_SET(T_Q5_K) GVX_SET(T_Q6_K)diff --git a/metal/kernels/units.metal b/metal/kernels/units.metalindex 35d78b9d8..611d65704 100644--- a/metal/kernels/units.metal+++ b/metal/kernels/units.metal@@ -6,6 +6,17 @@// (an AND keeps a field in place, so the code arrives multiplied by 2^k), the x values are pre-scaled by 2^-k here:// exact in float.+// the threadgroup-memory form (x staged once per threadgroup; the same values)+static inline void load16(threadgroup const float* p, thread float* xs) {+ threadgroup const float4* v = (threadgroup const float4*)p;+ for (int j = 0; j < 4; ++j) {+ const float4 a = v[j];+ xs[4 * j] = a.x;+ xs[4 * j + 1] = a.y;+ xs[4 * j + 2] = a.z;+ xs[4 * j + 3] = a.w;+ }+}static inline void load16(device const float* p, thread float* xs) {device const float4* v = (device const float4*)p;for (int j = 0; j < 4; ++j) {@@ -36,14 +47,14 @@ template <> struct XKind<T_Q3_K> { enum { K = XK_P2 }; };// X<F>::load(xp, s16, u, xs, xsum): unit u's prepared x (xp = the token's row of the format's kind, s16 = its sums)template <int F> struct X {- static inline void load(device const float* xp, device const float*, int u, thread float* xs, thread float* xsum) {+ template <typename P> static inline void load(P xp, P, int u, thread float* xs, thread float* xsum) {constexpr int UNIT = fmt<F>::UNIT;for (int k = 0; k < UNIT; k += 16) load16(xp + u * UNIT + k, xs + k);xsum[0] = xsum[1] = 0.0f;}};template <> struct X<T_Q2_0> {- static inline void load(device const float* xp, device const float* s16, int u, thread float* xs, thread float* xsum) {+ template <typename P> static inline void load(P xp, P s16, int u, thread float* xs, thread float* xsum) {load16(xp + 16 * u, xs);xsum[0] = s16[u];xsum[1] = 0.0f;@@ -51,7 +62,7 @@ template <> struct X<T_Q2_0> {};// Q4_K / Q5_K: unit (block b, chunk c, half m): run0 at 256b + 64c + 16m, run1 = run0 + 32template <> struct X<T_Q4_K> {- static inline void load(device const float* xp, device const float* s16, int u, thread float* xs, thread float* xsum) {+ template <typename P> static inline void load(P xp, P s16, int u, thread float* xs, thread float* xsum) {const int p0 = 256 * (u >> 3) + 64 * ((u >> 1) & 3) + 16 * (u & 1);load16(xp + p0, xs);load16(xp + p0 + 32, xs + 16);@@ -60,13 +71,13 @@ template <> struct X<T_Q4_K> {}};template <> struct X<T_Q5_K> {- static inline void load(device const float* xp, device const float* s16, int u, thread float* xs, thread float* xsum) {+ template <typename P> static inline void load(P xp, P s16, int u, thread float* xs, thread float* xsum) {X<T_Q4_K>::load(xp, s16, u, xs, xsum);}};// Q6_K: unit (block b, half n, m, p): run0 at 256b + 128n + 32p + 16m, run1 = run0 + 64template <> struct X<T_Q6_K> {- static inline void load(device const float* xp, device const float* s16, int u, thread float* xs, thread float* xsum) {+ template <typename P> static inline void load(P xp, P s16, int u, thread float* xs, thread float* xsum) {const int p0 = 256 * (u >> 3) + 128 * ((u >> 2) & 1) + 32 * (u & 1) + 16 * ((u >> 1) & 1);load16(xp + p0, xs);load16(xp + p0 + 64, xs + 16);@@ -76,7 +87,7 @@ template <> struct X<T_Q6_K> {};// Q3_K: 32 contiguous, sums per 16template <> struct X<T_Q3_K> {- static inline void load(device const float* xp, device const float* s16, int u, thread float* xs, thread float* xsum) {+ template <typename P> static inline void load(P xp, P s16, int u, thread float* xs, thread float* xsum) {load16(xp + 32 * u, xs);load16(xp + 32 * u + 16, xs + 16);xsum[0] = s16[2 * u];diff --git a/metal/tests/gemv_parity.cpp b/metal/tests/gemv_parity.cppindex 78f0c4953..11141bf85 100644--- a/metal/tests/gemv_parity.cpp+++ b/metal/tests/gemv_parity.cpp@@ -287,10 +287,19 @@ int main(int argc, char** argv) {for (int nt : ntoks) {double best = 1e9;Cfg bc{};- for (Cfg c : kCfgs) {+ const bool gx = std::getenv("GEMV_X") != nullptr; // gemvx_* (x staged in threadgroup memory)+ const uint32_t tgm = (uint32_t)(4 * nt * (s.n_in + s.n_in / 16) + 15) / 16 * 16;+ for (Cfg c : gx ? std::vector<Cfg>{{32, 1, 4, 1}, {32, 2, 4, 1}, {16, 1, 4, 1}, {16, 2, 4, 1}, {8, 1, 4, 1},+ {8, 2, 4, 1}, {4, 2, 4, 1}, {16, 2, 8, 1}, {8, 2, 8, 1}, {16, 4, 4, 1},+ {32, 2, 8, 1}, {2, 2, 4, 1}}+ : kCfgs) {if (!cfg_ok(c, s.type, s.n_in)) continue;if (!all && !(c.K == 1 || s.n_in >= 6144)) continue;- mtl::Pipeline p = cx.pipeline(lib, kname(s.type, c), mtl::Constants().i(0, nt));+ if (gx && tgm > 32768) continue;+ const std::string kn = gx ? "gemvx_" + std::to_string(s.type) + "_" + std::to_string(c.L) + "_" + std::to_string(c.R) ++ "_" + std::to_string(c.S)+ : kname(s.type, c);+ mtl::Pipeline p = cx.pipeline(lib, kn, mtl::Constants().i(0, nt).i(9, std::getenv("GEMV_PROBE") ? std::atoi(std::getenv("GEMV_PROBE")) : 0));GemvArgs a{(uint32_t)s.n_in, (uint32_t)s.n_out, (uint32_t)nt, (uint32_t)rb, (uint32_t)s.n_in,(uint32_t)s.n_out};std::vector<mtl::Dispatch> ds;@@ -302,6 +311,7 @@ int main(int argc, char** argv) {mtl::Arg::B(xp, plane * 4), mtl::Arg::B(y), mtl::Arg::V(a)};d.grid[0] = (s.n_out + rows_per_tg(c) - 1) / rows_per_tg(c);d.tg[0] = c.S * 32;+ if (gx) d.tg_mem = tgm;ds.push_back(d);}cx.warm(ds, 0.05);