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: GDN step writes the prepared activations of its output; token_chain --fuse / --fold-comb
Niko1221dca7c384057d
diff --git a/metal/kernels/gdn.metal b/metal/kernels/gdn.metalindex 3d2d69d43..021c34f32 100644--- a/metal/kernels/gdn.metal+++ b/metal/kernels/gdn.metal@@ -21,6 +21,8 @@ struct GdnArgs {uint channels; // 10240 = (2 h_k + h_v) * 128uint commit;float eps; // 1e-6+ uint plane; // 0: y only; else y is the raw plane of prepared activations (xprep layout, n = h_v * 128,+ // `plane` floats between planes) and the other planes and the per-16 sums are written too};kernel void gdn_step(device float* state [[buffer(0)]], device float* hist [[buffer(1)]],@@ -37,6 +39,7 @@ kernel void gdn_step(device float* state [[buffer(0)]], device float* hist [[bufthreadgroup float red[4][S];threadgroup float part[16];threadgroup float gb[2];+ threadgroup float ys[S];const int h = int(tg.x), hk = h % int(a.h_k), hv = int(a.h_v);const int cb = sg & 3, ib = sg >> 2;const int j = 32 * cb + lane, i0 = 32 * ib;@@ -117,9 +120,26 @@ kernel void gdn_step(device float* state [[buffer(0)]], device float* hist [[bufconst float tot = (part[0] + part[1]) + (part[2] + part[3]);const float rs = rsqrt(tot / float(S) + a.eps);const float zz = z[t * hv * S + h * S + j];- y[t * hv * S + h * S + j] = ((rs * o) * gamma[j]) * (1.0f / (1.0f + exp(-zz)));+ const float yv = ((rs * o) * gamma[j]) * (1.0f / (1.0f + exp(-zz)));+ y[t * hv * S + h * S + j] = yv;+ if (a.plane) {+ const float q2s[8] = {1.0f, 0.25f, 0.0625f, 0.015625f, 0.00390625f, 0.0009765625f, 0.000244140625f,+ 0.00006103515625f};+ const float p4s[4] = {1.0f, 0.00390625f, 1.52587890625e-05f, 5.9604644775390625e-08f};+ device float* raw = y + t * hv * S + h * S + j;+ const int jj = j & 15;+ raw[a.plane] = yv * q2s[jj & 7];+ raw[2 * a.plane] = yv * p4s[jj & 3];+ raw[3 * a.plane] = (jj & 1) ? yv * 0.00390625f : yv;+ ys[j] = yv;+ }}threadgroup_barrier(mem_flags::mem_threadgroup);+ if (a.plane && ib == 0 && (j & 15) == 0) {+ float s0 = 0.0f, s1 = 0.0f;+ for (int k = 0; k < 16; k += 2) { s0 += ys[j + k]; s1 += ys[j + k + 1]; }+ y[4 * a.plane + t * (hv * S / 16) + (h * S + j) / 16] = s0 + s1;+ }}if (a.commit) {for (int r = 0; r < 32; ++r) state[((i0 + r) * hv + h) * S + j] = st[r];diff --git a/metal/kernels/gr2.metal b/metal/kernels/gr2.metalindex c0e502f25..a856f74f8 100644--- a/metal/kernels/gr2.metal+++ b/metal/kernels/gr2.metal@@ -13,6 +13,10 @@// Per element the arithmetic is gr_norm's (fma(bo, g, R), then (R' * w) * rs); the K order differs (per stream, then// the 4 streams in order).+#ifndef P_TOKENT+#define P_TOKENT 532 // moe.metal's plan layout+#endif+struct GrArgs3 {uint n_embd;uint hc;@@ -22,14 +26,29 @@ struct GrArgs3 {float eps;uint rb_down; // bytes per row of w_down / w_injectuint rb_up; // bytes per row of w_up+ uint comb; // 1: bo is not given; it is the MoE combine (moe_combine_sh's arithmetic) of buffers 10..13+ uint topk;};+// bo[t][d], given or the MoE combine: sum_k ye[entry(t, k)][d] (k in order) + sigmoid(sg[t]) * ysh[t][d]+static inline float bo_at(device const float* bo, device const int* plan, device const float* ye,+ device const float* ysh, device const float* sg, constant GrArgs3& a, int t, int d) {+ const int n = int(a.n_embd);+ if (!a.comb) return bo[t * n + d];+ float s = 0.0f;+ for (uint j = 0; j < a.topk; ++j) s += ye[plan[P_TOKENT + t * int(a.topk) + j] * n + d];+ const float gs = 1.0f / (1.0f + exp(-sg[t]));+ return fma(gs, ysh[t * n + d], s);+}+template <int F, int TT, int RPS>kernel void gr_down3(device const uchar* w_down [[buffer(0)]], device const uchar* w_inject [[buffer(1)]],device const float* Rin [[buffer(2)]], device float* Rout [[buffer(3)]],device const float* bo [[buffer(4)]], device const float* Dprev [[buffer(5)]],device const float* wn [[buffer(6)]], device float* D [[buffer(7)]],device float* rs_out [[buffer(8)]], constant GrArgs3& a [[buffer(9)]],+ device const int* plan [[buffer(10)]], device const float* ye [[buffer(11)]],+ device const float* ysh [[buffer(12)]], device const float* sgv [[buffer(13)]],uint3 tg [[threadgroup_position_in_grid]], ushort tid [[thread_index_in_threadgroup]],ushort sg [[simdgroup_index_in_threadgroup]], ushort lane [[thread_index_in_simdgroup]]) {constexpr int UNIT = fmt<F>::UNIT;@@ -57,7 +76,7 @@ kernel void gr_down3(device const uchar* w_down [[buffer(0)]], device const uchafloat ss = 0.0f;for (int d = tid; d < n; d += 256) {float v = rt[d];- if (a.apply) v = fma(bo[(t0 + t) * n + d], g_tg[t], v);+ if (a.apply) v = fma(bo_at(bo, plan, ye, ysh, sgv, a, t0 + t, d), g_tg[t], v);xs_tg[t][d] = v;ss = fma(v, v, ss);if (tg.x == 0) Rout[(t0 + t) * NIN + c * n + d] = v;@@ -193,6 +212,8 @@ kernel void gr_down3b(device const uchar* w_down [[buffer(0)]], device const uchdevice const float* bo [[buffer(4)]], device const float* Dprev [[buffer(5)]],device const float* wn [[buffer(6)]], device float* D [[buffer(7)]],device float* rs_out [[buffer(8)]], constant GrArgs3& a [[buffer(9)]],+ device const int* plan [[buffer(10)]], device const float* ye [[buffer(11)]],+ device const float* ysh [[buffer(12)]], device const float* sgv [[buffer(13)]],uint3 tg [[threadgroup_position_in_grid]], ushort tid [[thread_index_in_threadgroup]],ushort sg [[simdgroup_index_in_threadgroup]], ushort lane [[thread_index_in_simdgroup]]) {threadgroup float xs_tg[TT][2560];@@ -229,7 +250,7 @@ kernel void gr_down3b(device const uchar* w_down [[buffer(0)]], device const uchfloat ss = 0.0f;for (int d = tid; d < n; d += 256) {float v = rt[d];- if (a.apply) v = fma(bo[(t0 + t) * n + d], g_tg[t], v);+ if (a.apply) v = fma(bo_at(bo, plan, ye, ysh, sgv, a, t0 + t, d), g_tg[t], v);xs_tg[t][d] = v;ss = fma(v, v, ss);if (tg.x == 0) Rout[(t0 + t) * NIN + c * n + d] = v;diff --git a/metal/tests/gdn_parity.cpp b/metal/tests/gdn_parity.cppindex 9acb209b8..8276495f0 100644--- a/metal/tests/gdn_parity.cpp+++ b/metal/tests/gdn_parity.cpp@@ -17,7 +17,7 @@namespace {constexpr int S = 128, HK = 16, HV = 48, C = (2 * HK + HV) * S, MAXT = 8;-struct GdnArgs { uint32_t h_k, h_v, channels, commit; float eps; };+struct GdnArgs { uint32_t h_k, h_v, channels, commit; float eps; uint32_t plane; };double median(std::vector<double> v) { std::sort(v.begin(), v.end()); return v[v.size() / 2]; }struct Err {double se = 0, sr = 0;@@ -52,7 +52,7 @@ int main(int argc, char** argv) {std::memcpy(hist.as<float>() + l * C * 3, h0.ptr, 4 * C * 3);}auto disp = [&](const mtl::Pipeline& p, int layer, int commit, const mtl::Buffer& out) {- GdnArgs a{HK, HV, C, (uint32_t)commit, 1e-6f};+ GdnArgs a{HK, HV, C, (uint32_t)commit, 1e-6f, 0};mtl::Dispatch d;d.p = &p;d.args = {mtl::Arg::B(state, 4 * st_elems * layer), mtl::Arg::B(hist, 4 * (size_t)C * 3 * layer), mtl::Arg::B(qkv),diff --git a/metal/tests/gr2_parity.cpp b/metal/tests/gr2_parity.cppindex d2d3c78de..fdc2337b6 100644--- a/metal/tests/gr2_parity.cpp+++ b/metal/tests/gr2_parity.cpp@@ -18,7 +18,7 @@namespace {constexpr int N = 2560, HC = 4, LR = 320, NIN = HC * N, NR = LR + HC, MAXT = 8;struct GrArgs { uint32_t n_embd, hc, hc_lr, n_tok, apply; float eps; };-struct GrArgs3 { uint32_t n_embd, hc, hc_lr, n_tok, apply; float eps; uint32_t rb_down, rb_up; };+struct GrArgs3 { uint32_t n_embd, hc, hc_lr, n_tok, apply; float eps; uint32_t rb_down, rb_up, comb, topk; };double median(std::vector<double> v) { std::sort(v.begin(), v.end()); return v[v.size() / 2]; }struct Err {double se = 0, sr = 0;diff --git a/metal/tests/token_chain.cpp b/metal/tests/token_chain.cppindex f0c19673a..ffd16724c 100644--- a/metal/tests/token_chain.cpp+++ b/metal/tests/token_chain.cpp@@ -27,12 +27,12 @@ constexpr int P_MAXE = 128, P_GEXP = 16, P_GSTART = P_GEXP + P_MAXE, P_ETOK = P_P_EW = P_ETOK + P_MAXE, P_TOKENT = P_EW + P_MAXE, P_INTS = P_TOKENT + P_MAXE, P_IGU = 4, P_IDN = 8;struct GemvArgs { uint32_t n_in, n_out, n_tok, row_bytes, x_stride, y_stride; };struct GrArgs { uint32_t n_embd, hc, hc_lr, n_tok, apply; float eps; };-struct GrArgs3 { uint32_t n_embd, hc, hc_lr, n_tok, apply; float eps; uint32_t rb_down, rb_up; };+struct GrArgs3 { uint32_t n_embd, hc, hc_lr, n_tok, apply; float eps; uint32_t rb_down, rb_up, comb, topk; };struct MoeArgs {uint64_t gate_off, up_off, down_off, gu_stride, d_stride;uint32_t gu_row, d_row, n_embd, n_ff, n_tok, topk, n_expert, gx_gu, gx_dn, pad;};-struct GdnArgs { uint32_t h_k, h_v, channels, commit; float eps; };+struct GdnArgs { uint32_t h_k, h_v, channels, commit; float eps; uint32_t plane; };struct QsaArgs { uint32_t pos0, pos_base, max_cells, n_tok; float eps; };struct GlueArgs { uint32_t n, n_tok, n_embd, topk; };struct ArgArgs { uint32_t n_vocab, parts; };@@ -61,6 +61,8 @@ int main(int argc, char** argv) {int ntok = 1, ctx = 1000, reps = 10;bool skip_qsa = false, graph = false;std::string hc_mode = "3"; // 3: BF16, 3 dispatches; 2: BF16, 2 dispatches; q8: Q8_0 copies, 2 dispatches+ bool fuse = false; // the GDN y prep in the step; no QSA score/top-k within the selection budget+ bool fold_comb = false; // the MoE combine folded into the next 2-dispatch HC read (measured slower)std::string only;for (int i = 2; i < argc; ++i) {std::string a = argv[i];@@ -70,6 +72,8 @@ int main(int argc, char** argv) {else if (a == "--skip-qsa") skip_qsa = true;else if (a == "--graph") graph = true;else if (a == "--hc") hc_mode = argv[++i];+ else if (a == "--fuse") fuse = true;+ else if (a == "--fold-comb") fold_comb = true;else if (a == "--only") only = argv[++i];}gguf::File f;@@ -140,6 +144,8 @@ int main(int argc, char** argv) {pooled.push_back(cx.alloc(4 * (size_t)MB * 128));}}+ for (int t = 0; t < MAXT; ++t)+ for (int c = 0; c < 2051; ++c) qids.as<int>()[t * 2051 + c] = c;for (auto& b : vsc) for (size_t i = 0; i < b.bytes / 2; ++i) b.as<uint16_t>()[i] = 0x2000; // small fp16 scalesfor (auto& b : ksc) for (size_t i = 0; i < b.bytes / 2; ++i) b.as<uint16_t>()[i] = 0x2000;@@ -239,8 +245,10 @@ int main(int argc, char** argv) {const int tt = std::min(ntok, 3);const std::string dn = std::string("gr_down3_") + (q8 ? "8_" : "30_") + std::to_string(tt) + "_2";const std::string up = std::string("gr_up3_") + (q8 ? "8" : "30");+ // after an MoE (the next layer's mixer read, the final read) the combine is folded in here+ const bool comb = fold_comb && apply && pre.find("hc_ffn") == std::string::npos;GrArgs3 a{N, HC, LR, MAXT, apply ? 1u : 0u, 1e-6f, (uint32_t)(q8 ? HC * N / 32 * 34 : HC * N * 2),- (uint32_t)(q8 ? LR / 32 * 34 : LR * 2)};+ (uint32_t)(q8 ? LR / 32 * 34 : LR * 2), comb ? 1u : 0u, (uint32_t)TOPK};const mtl::Buffer& WB = q8 ? Q8 : W;const size_t od = q8 ? q8off.at(tdn.name) : tdn.offset, oi = q8 ? q8off.at(tin.name) : tin.offset,ou = q8 ? q8off.at(tup.name) : tup.offset;@@ -248,7 +256,7 @@ int main(int argc, char** argv) {d.p = Pn(lib, dn, dn + "#" + std::to_string(ntok), mtl::Constants().i(1, ntok));d.args = {mtl::Arg::B(WB, od), mtl::Arg::B(WB, oi), mtl::Arg::B(Rb[cur_r]), mtl::Arg::B(Rb[1 - cur_r]),mtl::Arg::B(bo), mtl::Arg::B(Db[cur_d]), mtl::Arg::B(W, tn.offset), mtl::Arg::B(Db[1 - cur_d]),- mtl::Arg::B(rsb), mtl::Arg::V(a)};+ mtl::Arg::B(rsb), mtl::Arg::V(a), mtl::Arg::B(plan), mtl::Arg::B(ye), mtl::Arg::B(ysh), mtl::Arg::B(sg)};d.grid[0] = (LR + HC + 15) / 16;d.grid[1] = HC;d.tg[0] = 256;@@ -318,17 +326,17 @@ int main(int argc, char** argv) {gemv2(T(b + "ssm_alpha.weight"), XA, al, 48, "gdn proj");nb = true;gemv2(T(b + "ssm_beta.weight"), XA, be, 48, "gdn proj");- GdnArgs a{16, 48, 10240, 1, 1e-6f};+ GdnArgs a{16, 48, 10240, 1, 1e-6f, fuse ? (uint32_t)(MAXT * 6144) : 0u};mtl::Dispatch d;d.p = pgdn;d.args = {mtl::Arg::B(gst[gi]), mtl::Arg::B(ghist[gi]), mtl::Arg::B(qkv), mtl::Arg::B(al), mtl::Arg::B(be),mtl::Arg::B(z), mtl::Arg::B(W, T(b + "ssm_conv1d.weight").offset), mtl::Arg::B(W, T(b + "ssm_dt.bias").offset),- mtl::Arg::B(W, T(b + "ssm_a").offset), mtl::Arg::B(W, T(b + "ssm_norm.weight").offset), mtl::Arg::B(yg),+ mtl::Arg::B(W, T(b + "ssm_a").offset), mtl::Arg::B(W, T(b + "ssm_norm.weight").offset), mtl::Arg::B(fuse ? XY.b : yg),mtl::Arg::V(a), mtl::Arg::B(ghist2[gi])};d.grid[0] = 48;d.tg[0] = 512;push(d, "gdn step", 2.0 * 4 * 128 * 48 * 128);- xprep(yg, XY, 6144, "gdn proj");+ if (!fuse) xprep(yg, XY, 6144, "gdn proj");gemv2(T(b + "ssm_out.weight"), XY, bo, N, "gdn proj");++gi;} else if (!skip_qsa) {@@ -354,19 +362,20 @@ int main(int argc, char** argv) {d.grid[1] = ntok;d.tg[0] = 256;push(d, "qsa attn", 0);+ const bool sel = !(fuse && ctx + ntok <= 2051); // within the budget the selection is the identitymtl::Dispatch s;s.p = qscore;s.args = {mtl::Arg::B(qidx), mtl::Arg::B(pooled[qi]), mtl::Arg::B(dead[qi]), mtl::Arg::B(scores), mtl::Arg::V(a)};s.grid[0] = ((ctx + ntok) / 4 + 1 + 7) / 8;s.grid[1] = ntok;s.tg[0] = 256;- push(s, "qsa attn", 0);+ if (sel) push(s, "qsa attn", 0);mtl::Dispatch k;k.p = qtopk;k.args = {mtl::Arg::B(scores), mtl::Arg::B(qids), mtl::Arg::V(a)};k.grid[0] = ntok;k.tg[0] = 1024;- push(k, "qsa attn", 0);+ if (sel) push(k, "qsa attn", 0);mtl::Dispatch at;at.p = qattn;at.args = {mtl::Arg::B(qcur), mtl::Arg::B(kq[qi]), mtl::Arg::B(vq[qi]), mtl::Arg::B(ksc[qi]), mtl::Arg::B(vsc[qi]),@@ -460,7 +469,7 @@ int main(int argc, char** argv) {d.args = {mtl::Arg::B(ye), mtl::Arg::B(plan), mtl::Arg::B(ysh), mtl::Arg::B(sg), mtl::Arg::B(bo), mtl::Arg::V(a)};d.grid[0] = (ntok * N + 255) / 256;d.tg[0] = 256;- push(d, "shared", 0);+ if (!(fold_comb && hc_mode != "3")) push(d, "shared", 0); // else folded into the next HC read}}// ---- the final mixer and the head