Skip to content

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 commit writes the conv history to a second buffer; GEMV parity on real GGUF tensors

Niko122199614041badd
diff --git a/metal/kernels/gdn.metal b/metal/kernels/gdn.metal
index e4260db92..3d2d69d43 100644
--- a/metal/kernels/gdn.metal
+++ b/metal/kernels/gdn.metal
@@ -10,7 +10,8 @@
// One threadgroup per value head, 512 threads = 16 simdgroups: simdgroup s holds columns j = 32 (s % 4) + lane and
// rows i = 32 (s / 4) .. + 32 of the head's 128 x 128 state in registers. GDN_NTOK tokens run in order; with
// `commit` the state and the conv history are written back after the last one (a verify window runs without commit,
-// the fold of m accepted tokens is the same kernel with GDN_NTOK = m and commit).
+// the fold of m accepted tokens is the same kernel with GDN_NTOK = m and commit). The conv history is read from
+// `hist` and the committed one written to `hist_out` (ping-pong: the q/k channels are shared by three threadgroups).
constant int GDN_NTOK [[function_constant(2)]];
@@ -28,6 +29,7 @@ kernel void gdn_step(device float* state [[buffer(0)]], device float* hist [[buf
device const float* conv_w [[buffer(6)]], device const float* dt [[buffer(7)]],
device const float* ssm_a [[buffer(8)]], device const float* gamma [[buffer(9)]],
device float* y [[buffer(10)]], constant GdnArgs& a [[buffer(11)]],
+ device float* hist_out [[buffer(12)]],
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 S = 128;
@@ -122,7 +124,9 @@ kernel void gdn_step(device float* state [[buffer(0)]], device float* hist [[buf
if (a.commit) {
for (int r = 0; r < 32; ++r) state[((i0 + r) * hv + h) * S + j] = st[r];
// q/k history: the threadgroups h < h_k own their q/k head (h == hk); v history: every head its own
+ // to hist_out, NOT in place: a q/k channel is read by the three threadgroups h % h_k at their start, and
+ // a threadgroup may start after its owner has finished (seen on the 10-core M4); the caller swaps the two
if (ch >= 0 && (tid >= 256 || h < int(a.h_k)))
- for (int k = 0; k < 3; ++k) hist[ch * 3 + k] = hst[k];
+ for (int k = 0; k < 3; ++k) hist_out[ch * 3 + k] = hst[k];
}
}
diff --git a/metal/tests/gdn_parity.cpp b/metal/tests/gdn_parity.cpp
index 50af3e25a..9acb209b8 100644
--- a/metal/tests/gdn_parity.cpp
+++ b/metal/tests/gdn_parity.cpp
@@ -37,7 +37,7 @@ int main(int argc, char** argv) {
h0 = cx.alloc(4 * C * 3), qkv = cx.alloc(4 * C * MAXT), al = cx.alloc(4 * HV * MAXT),
be = cx.alloc(4 * HV * MAXT), z = cx.alloc(4 * HV * S * MAXT), cw = cx.alloc(4 * C * 4),
dt = cx.alloc(4 * HV), sa = cx.alloc(4 * HV), gm = cx.alloc(4 * S), y = cx.alloc(4 * HV * S * MAXT),
- y2 = cx.alloc(4 * HV * S * MAXT);
+ y2 = cx.alloc(4 * HV * S * MAXT), hist2 = cx.alloc(4 * C * 3 * LAYERS);
ref::Rng r(17);
for (size_t i = 0; i < st_elems; ++i) st0.as<float>()[i] = r.sym() * 0.1f;
for (int i = 0; i < C * 3; ++i) h0.as<float>()[i] = r.sym();
@@ -57,11 +57,15 @@ int main(int argc, char** argv) {
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),
mtl::Arg::B(al), mtl::Arg::B(be), mtl::Arg::B(z), mtl::Arg::B(cw), mtl::Arg::B(dt), mtl::Arg::B(sa),
- mtl::Arg::B(gm), mtl::Arg::B(out), mtl::Arg::V(a)};
+ mtl::Arg::B(gm), mtl::Arg::B(out), mtl::Arg::V(a), mtl::Arg::B(hist2, 4 * (size_t)C * 3 * layer)};
d.grid[0] = HV;
d.tg[0] = 512;
return d;
};
+ {
+ mtl::Pipeline p1 = cx.pipeline(lib, "gdn_step", mtl::Constants().i(2, 1));
+ std::printf("gdn_step pipeline: max %d threads per threadgroup (the kernel needs 512)\n", p1.max_threads);
+ }
if (bench) {
for (int nt : {1, 2, 3, 4}) {
mtl::Pipeline p = cx.pipeline(lib, "gdn_step", mtl::Constants().i(2, nt));
@@ -81,6 +85,19 @@ int main(int argc, char** argv) {
int fails = 0;
for (int nt = 1; nt <= 4; ++nt) {
mtl::Pipeline p = cx.pipeline(lib, "gdn_step", mtl::Constants().i(2, nt));
+ // determinism: the same verify call three times
+ {
+ std::memcpy(state.ptr, st0.ptr, 4 * st_elems);
+ std::memcpy(hist.ptr, h0.ptr, 4 * C * 3);
+ cx.run({disp(p, 0, 0, y2)});
+ std::vector<float> first((float*)y2.ptr, (float*)y2.ptr + HV * S * nt);
+ int diff = 0;
+ for (int k = 0; k < 2; ++k) {
+ cx.run({disp(p, 0, 0, y2)});
+ diff += std::memcmp(first.data(), y2.ptr, 4 * HV * S * nt) != 0;
+ }
+ if (diff) std::printf(" ntok %d: NOT deterministic (%d of 2 repeats differ)\n", nt, diff);
+ }
// verify: state untouched
std::memcpy(state.ptr, st0.ptr, 4 * st_elems);
std::memcpy(hist.ptr, h0.ptr, 4 * C * 3);
@@ -142,7 +159,7 @@ int main(int argc, char** argv) {
}
}
for (size_t i = 0; i < st_elems; ++i) es.add(state.as<float>()[i], Sd[i]);
- for (int i = 0; i < C * 3; ++i) eh.add(hist.as<float>()[i], Hd[i]);
+ for (int i = 0; i < C * 3; ++i) eh.add(hist2.as<float>()[i], Hd[i]);
// fold property: a window of nt + 1 tokens (no commit) gives bitwise the same first nt outputs
bool prefix = true;
if (nt < 4) {
diff --git a/metal/tests/gemv_parity.cpp b/metal/tests/gemv_parity.cpp
index a7c16f5a1..78f0c4953 100644
--- a/metal/tests/gemv_parity.cpp
+++ b/metal/tests/gemv_parity.cpp
@@ -4,6 +4,7 @@
// gemv_parity parity: every format, n_in 2560/640/10240, 1..8 tokens
// gemv_parity --bench [--pack Q2_0|IQ3_XXS] [--ntok N] [--all] speed on the pack's shapes (best tile per shape)
// gemv_parity --one TYPE NIN NOUT [--ntok N] sweep one shape
+#include "gguf_lite.h"
#include "mtl_rt.h"
#include "ref_quant.h"
@@ -74,6 +75,7 @@ struct Shape { const char* name; int type, n_in, n_out, per_token; }; // per_t
int main(int argc, char** argv) {
bool bench = false, all = false;
int ntok = 1;
+ std::string gguf_path;
std::string pack = "Q2_0";
std::vector<Shape> one;
for (int i = 1; i < argc; ++i) {
@@ -82,6 +84,7 @@ int main(int argc, char** argv) {
else if (a == "--all") all = true;
else if (a == "--ntok") ntok = std::atoi(argv[++i]);
else if (a == "--pack") pack = argv[++i];
+ else if (a == "--gguf") gguf_path = argv[++i];
else if (a == "--one") {
const int t = type_of(argv[i + 1]);
one.push_back({"one", t, std::atoi(argv[i + 2]), std::atoi(argv[i + 3]), 1});
@@ -96,6 +99,68 @@ int main(int argc, char** argv) {
const int MAXT = 8;
mtl::Pipeline pprep = cx.pipeline(lib, "xprep");
+ if (!gguf_path.empty()) {
+ // real tensors: the first type-distinct ones of the file (experts: expert 0's matrix), 1 and 3 tokens
+ gguf::File f;
+ std::string err;
+ if (!f.open(gguf_path, err)) { std::fprintf(stderr, "%s\n", err.c_str()); return 2; }
+ mtl::Buffer Wf = cx.wrap(f.map, f.map_bytes);
+ std::map<int, const gguf::Tensor*> pick;
+ for (const auto& kv : f.tensors) {
+ const gguf::Tensor& t = kv.second;
+ if (t.shape.size() < 2 || t.name.find("token_embd") != std::string::npos ||
+ t.name.find("per_layer") != std::string::npos)
+ continue;
+ const int ty = t.type;
+ const bool gemv_type = ty == ref::BF16 || ty == ref::Q8_0 || ty == ref::Q4_0 || ty == ref::Q5_0 ||
+ ty == ref::IQ4_NL || ty == ref::IQ4_XS || ty == ref::Q2_0 || ty == ref::IQ3_S ||
+ ty == ref::Q3_K || ty == ref::Q4_K || ty == ref::Q5_K || ty == ref::Q6_K;
+ if (gemv_type && !pick.count(ty)) pick[ty] = &t;
+ }
+ int fails = 0;
+ for (const auto& kv : pick) {
+ const gguf::Tensor& t = *kv.second;
+ const int ty = t.type, n_in = (int)t.shape[0], n_out = std::min<int>((int)t.shape[1], 4096);
+ const size_t rb = ref::row_bytes(ty, n_in);
+ mtl::Buffer x = cx.alloc(4 * n_in * MAXT), y = cx.alloc(4 * (size_t)n_out * MAXT),
+ xp = cx.alloc(4 * (4 * n_in * MAXT + n_in / 16 * MAXT));
+ ref::Rng r(3);
+ for (int i = 0; i < n_in * MAXT; ++i) x.as<float>()[i] = r.sym();
+ cx.run({xprep_dispatch(pprep, x, xp, n_in, MAXT)});
+ const size_t plane = (size_t)4 * n_in * MAXT;
+ std::vector<float> wf((size_t)n_in * n_out);
+ ref::dequant(ty, (const uint8_t*)f.map + t.offset, wf.data(), wf.size());
+ Cfg c = n_in >= 6144 ? Cfg{32, 1, 8, 2} : n_in <= 640 ? Cfg{8, 1, 4, 1} : Cfg{16, 1, 4, 1};
+ if (!cfg_ok(c, ty, n_in)) c = Cfg{32, 1, 4, 1};
+ for (int nt : {1, 3}) {
+ mtl::Pipeline p = cx.pipeline(lib, kname(ty, c), mtl::Constants().i(0, nt));
+ GemvArgs a{(uint32_t)n_in, (uint32_t)n_out, (uint32_t)nt, (uint32_t)rb, (uint32_t)n_in, (uint32_t)n_out};
+ mtl::Dispatch d;
+ d.p = &p;
+ d.args = {mtl::Arg::B(Wf, t.offset), mtl::Arg::B(xp, plane * xkind(ty)), mtl::Arg::B(xp, plane * 4),
+ mtl::Arg::B(y), mtl::Arg::V(a)};
+ d.grid[0] = (n_out + rows_per_tg(c) - 1) / rows_per_tg(c);
+ d.tg[0] = c.S * 32;
+ cx.run({d});
+ double se = 0, sr = 0;
+ for (int tt = 0; tt < nt; ++tt)
+ for (int o = 0; o < n_out; ++o) {
+ double ref = 0;
+ for (int k = 0; k < n_in; ++k) ref += (double)wf[(size_t)o * n_in + k] * x.as<float>()[tt * n_in + k];
+ const double e = y.as<float>()[tt * n_out + o] - ref;
+ se += e * e;
+ sr += ref * ref;
+ }
+ const double rel = std::sqrt(se / sr);
+ const bool ok = rel < 1e-5 && std::isfinite(rel);
+ fails += !ok;
+ std::printf("real %-7s %-34s %5d x %5d ntok %d: rel L2 %.2e %s\n", ref::type_name(ty), t.name.c_str(),
+ n_out, n_in, nt, rel, ok ? "PASS" : "FAIL");
+ }
+ }
+ std::printf("%s\n", fails ? "SOME FAILED" : "ALL PASS");
+ return fails ? 1 : 0;
+ }
if (!bench) {
int fails = 0;
for (int t : {ref::BF16, ref::Q8_0, ref::Q4_0, ref::Q5_0, ref::IQ4_NL, ref::IQ4_XS, ref::Q2_0, ref::IQ3_S,
diff --git a/metal/tests/token_chain.cpp b/metal/tests/token_chain.cpp
index 4e874fc71..4e80f6d40 100644
--- a/metal/tests/token_chain.cpp
+++ b/metal/tests/token_chain.cpp
@@ -121,11 +121,12 @@ int main(int argc, char** argv) {
cs.as<float>()[(p * 32 + i) * 2 + 1] = (float)std::sin(th);
}
// per-layer recurrent state and QSA caches
- std::vector<mtl::Buffer> gst, ghist, kq, vq, ksc, vsc, tail, dead, pooled;
+ std::vector<mtl::Buffer> gst, ghist, ghist2, kq, vq, ksc, vsc, tail, dead, pooled;
for (int l = 0; l < 48; ++l) {
if (l % 4 != 3) {
gst.push_back(cx.alloc(4 * 128 * 48 * 128));
ghist.push_back(cx.alloc(4 * 10240 * 3));
+ ghist2.push_back(cx.alloc(4 * 10240 * 3));
} else {
kq.push_back(cx.alloc((size_t)MAXC * 2 * 256));
vq.push_back(cx.alloc((size_t)MAXC * 2 * 256));
@@ -256,7 +257,7 @@ int main(int argc, char** argv) {
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::V(a)};
+ 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);