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: mm_gu3 paired-run decode without the runtime layout check (Q2_0 gate/up 4.9 -> 3.5 ms a layer); SLC prefetch probe
Niko1221072c19f60fc3
diff --git a/metal/kernels/glue.metal b/metal/kernels/glue.metalindex 4f4612a2d..b7bc8bcc7 100644--- a/metal/kernels/glue.metal+++ b/metal/kernels/glue.metal@@ -45,3 +45,19 @@ kernel void moe_combine_sh(device const float* ye [[buffer(0)]], device const inconst float gs = 1.0f / (1.0f + exp(-sg[t]));bo[gid] = fma(gs, ysh[gid], s);}++// slc_touch: reads n16 16-byte words (a weight range the next kernels will need) so they sit in the SLC; the xor is+// written only if it equals a value it never takes (keeps the loads alive). grid (ceil(n16 / 1024)), 256 threads,+// 4 words a thread.+kernel void slc_touch(device const uint4* src [[buffer(0)]], device uint* sink [[buffer(1)]], constant uint& n16 [[buffer(2)]],+ uint gid [[thread_position_in_grid]]) {+ uint s = 0;+ for (uint k = 0; k < 4; ++k) {+ const uint j = (gid >> 8) * 1024 + k * 256 + (gid & 255);+ if (j < n16) {+ const uint4 v = src[j];+ s ^= v.x ^ v.y ^ v.z ^ v.w;+ }+ }+ if (s == 0x9e3779b9u) sink[0] = s;+}diff --git a/metal/kernels/mm_bench.metal b/metal/kernels/mm_bench.metalindex 62c684a80..c04659fc5 100644--- a/metal/kernels/mm_bench.metal+++ b/metal/kernels/mm_bench.metal@@ -82,3 +82,4 @@ kernel void coop_layout(device int* out [[buffer(0)]], ushort lane [[thread_indetemplate [[host_name("coop_layout_16_32_16")]] kernel decltype(coop_layout<16, 32, 16>) coop_layout<16, 32, 16>;template [[host_name("coop_layout_32_32_16")]] kernel decltype(coop_layout<32, 32, 16>) coop_layout<32, 32, 16>;template [[host_name("coop_layout_16_64_32")]] kernel decltype(coop_layout<16, 64, 32>) coop_layout<16, 64, 32>;+template [[host_name("coop_layout_16_32_32")]] kernel decltype(coop_layout<16, 32, 32>) coop_layout<16, 32, 32>;diff --git a/metal/kernels/moe_mm.metal b/metal/kernels/moe_mm.metalindex 4e70e9d0a..04889a7a7 100644--- a/metal/kernels/moe_mm.metal+++ b/metal/kernels/moe_mm.metal@@ -389,6 +389,7 @@ static inline half4 w4_signed(uint g, uint s, float sc) {return half4(q * sc);}template <> struct W4<T_Q2_0> { // 64 values a block: fp16 d, 16 bytes of 2-bit codes (value v at byte v / 4)+ // fp32 math then fp16 (an fp16-math decode was measured slower: 4.2 vs 3.5 ms a layer)static inline half4 run(device const uchar* row, int k, threadgroup const uint*) {device const uchar* b = row + (k >> 6) * 18;const float d = float(*(device const half*)b);@@ -478,6 +479,7 @@ template <int F> struct W8 {b = W4<F>::run(row, k + 16, tab);}};+// (a W8<T_Q2_0> sharing the block scale was measured slower than the two W4 of the generic one: 5.5 vs 3.5-4.2 ms)template <> struct W8<T_IQ3_XXS> {static inline void run(device const uchar* row, int k, threadgroup const uint* tab, thread half4& a, thread half4& b) {device const uchar* bl = row + (k >> 8) * 98;@@ -593,8 +595,11 @@ kernel void mm_gu3(device const uchar* W [[buffer(0)]], device const half* XS [[device const uchar* rgw = g0 + ulong(n) * a.gu_row;device const uchar* ruw = u0 + ulong(n) * a.gu_row;half4 wg, wu, wg2, wu2;- if (TK == 32 && iy[0] == ix[0] + 16 && iy[1] == ix[1] && (ix[0] & 31) < 16) {- // the lane's two runs of this row sit in one 32-group (the layout: element i + cap / 2 is k + 16)+ if (mm_probe == 1) { // the bench probe: no decode (constant weights)+ wg = half4(half(n & 7)); wu = wg; wg2 = half4(half(k & 7)); wu2 = wg2;+ } else if (TK == 32) {+ // the lane's two runs of this row sit in one 32-group: element i + cap / 2 is (k + 16, n) - part of+ // the COOP_K4 layout (coop_probe checks it); a runtime check of it here cost 30%W8<F>::run(rgw, k, tab, wg, wg2);W8<F>::run(ruw, k, tab, wu, wu2);} else {diff --git a/metal/tests/coop_probe.h b/metal/tests/coop_probe.hindex 936b2252a..dc781ff46 100644--- a/metal/tests/coop_probe.h+++ b/metal/tests/coop_probe.h@@ -10,14 +10,16 @@inline bool coop_k4(mtl::Context& cx) {if (const char* e = std::getenv("COOP_K4")) return std::atoi(e) != 0;const int lib = cx.compile({"mm_bench.metal"}, "#define NO_GGML_COMMON 1\n");- mtl::Pipeline p = cx.pipeline(lib, "coop_layout_16_64_32");+ for (const char* tile : {"coop_layout_16_64_32", "coop_layout_16_32_32"}) {+ mtl::Pipeline p = cx.pipeline(lib, tile);mtl::Buffer out = cx.alloc(4 * 3 * 32 * 128 * 4);mtl::Dispatch d;d.p = &p;d.args = {mtl::Arg::B(out)};d.tg[0] = 32;cx.run({d});- // every lane: elements 4j .. 4j + 3 are (k, n), (k + 1, n), (k + 2, n), (k + 3, n)+ // every lane: elements 4j .. 4j + 3 are (k, n), (k + 1, n), (k + 2, n), (k + 3, n), and element i + cap / 2 is+ // (k + 16, n) with k % 32 < 16 (mm_gu3's paired-run decode relies on it)const int* o = out.as<int>();for (int l = 0; l < 32; ++l) {const int* q = o + l * 128 * 4;@@ -25,6 +27,10 @@ inline bool coop_k4(mtl::Context& cx) {for (int i = 0; i + 3 < cap; i += 4)for (int j = 1; j < 4; ++j)if (q[(i + j) * 4] != q[i * 4] + j || q[(i + j) * 4 + 1] != q[i * 4 + 1]) return false;+ for (int i = 0; i < cap / 2; i += 4)+ if (q[(i + cap / 2) * 4] != q[i * 4] + 16 || q[(i + cap / 2) * 4 + 1] != q[i * 4 + 1] || (q[i * 4] & 31) >= 16)+ return false;+ }}return true;}diff --git a/metal/tests/moe_mm_parity.cpp b/metal/tests/moe_mm_parity.cppindex e7053df47..9c517a661 100644--- a/metal/tests/moe_mm_parity.cpp+++ b/metal/tests/moe_mm_parity.cpp@@ -93,7 +93,7 @@ int main(int argc, char** argv) {probe.i(5, std::getenv("MM_PROBE") ? std::atoi(std::getenv("MM_PROBE")) : 0);const bool k4 = coop_k4(cx);if (v3) std::printf("right-input layout: %s\n", k4 ? "runs of 4 k" : "other (per-element decode)");- mtl::Pipeline pgu = v3 ? cx.pipeline(lib, "mm_gu3_" + std::to_string(gu) + "_" + tl, mtl::Constants().b(7, k4))+ mtl::Pipeline pgu = v3 ? cx.pipeline(lib, "mm_gu3_" + std::to_string(gu) + "_" + tl, mtl::Constants().b(7, k4).i(5, std::getenv("MM_PROBE") ? std::atoi(std::getenv("MM_PROBE")) : 0)): cx.pipeline(lib, (v2 ? "mm_gu2_" : "mm_gu_") + std::to_string(gu) + "_" + tl, probe),pdn = v3 ? cx.pipeline(lib, "mm_down3_" + std::to_string(dn) + "_" + dtl, mtl::Constants().b(7, k4)): cx.pipeline(lib, (v2 ? "mm_down2_" : "mm_down_") + std::to_string(dn) + "_" + (v2 ? dtl : tl)),diff --git a/metal/tests/token_chain.cpp b/metal/tests/token_chain.cppindex a6752b0d5..6e6f5cd69 100644--- a/metal/tests/token_chain.cpp+++ b/metal/tests/token_chain.cpp@@ -174,6 +174,26 @@ int main(int argc, char** argv) {if (it == pcache.end()) it = pcache.emplace(key, cx.pipeline(l, fn, c)).first;return &it->second;};+ // ---- SLC prefetch (--prefetch): the next HC read's weights touched concurrently with the first projection GEMV+ // after the current read, so the latency-bound read finds them in the SLC+ const bool prefetch = std::getenv("CHAIN_PREFETCH") != nullptr;+ struct Range { const mtl::Buffer* b; size_t off, bytes; };+ std::vector<Range> pending;+ mtl::Buffer sink = cx.alloc(16);+ const mtl::Pipeline* ptouch = nullptr;+ auto push_prefetch = [&]() {+ for (const Range& r : pending) {+ const uint32_t n16 = (uint32_t)(r.bytes / 16);+ mtl::Dispatch d;+ d.p = ptouch;+ d.args = {mtl::Arg::B(*r.b, r.off), mtl::Arg::B(sink), mtl::Arg::V(n16)};+ d.grid[0] = (n16 + 1023) / 1024;+ d.tg[0] = 256;+ nb = true;+ push(d, "prefetch", (double)r.bytes);+ }+ pending.clear();+ };// ---- GEMV by tensor, with a tile from the measured tableauto gemv2 = [&](const gguf::Tensor& t, const Act& x, const mtl::Buffer& y, size_t y_stride, const std::string& c,int ty_override = -1) {@@ -196,8 +216,10 @@ int main(int argc, char** argv) {d.grid[0] = (n_out + rpt - 1) / rpt;d.tg[0] = S * 32;push(d, c, (double)t.bytes);+ if (!pending.empty()) push_prefetch();};const mtl::Pipeline* pnorm = Pn(lib, "gr_norm", "gr_norm", {});+ ptouch = Pn(lib, "slc_touch", "slc_touch", {});const mtl::Pipeline* pdown = Pn(lib, "gr_down_8_8", "gr_down_8_8", mtl::Constants().i(1, ntok));const mtl::Pipeline* pup = Pn(lib, "gr_up", "gr_up", mtl::Constants().i(1, ntok));const mtl::Pipeline* pdownq = Pn(lib, "gr_down_q8_8_8", "gr_down_q8_8_8", mtl::Constants().i(1, ntok));@@ -316,6 +338,28 @@ int main(int argc, char** argv) {d3.tg[0] = 256;push(d3, "hc", (double)tup.bytes);cur_inj = 1 - cur_inj;+ if (prefetch && !final_) {+ // the next read: blk.l.hc_attn -> blk.l.hc_ffn -> blk.(l+1).hc_attn ... -> output_hc+ std::string nx;+ const size_t at = pre.find(".hc_attn");+ if (at != std::string::npos) nx = pre.substr(0, at) + ".hc_ffn";+ else {+ const int l = std::atoi(pre.c_str() + 4);+ nx = l == 47 ? std::string("output_hc") : "blk." + std::to_string(l + 1) + ".hc_attn";+ }+ const bool fin = nx == "output_hc";+ for (const std::string& part : fin ? std::vector<std::string>{"_down", "_up"}+ : std::vector<std::string>{"_down", "_inject", "_up"}) {+ const gguf::Tensor& tt = T(nx + part + ".weight");+ if (q83) {+ uint64_t ne = 1;+ for (uint64_t dd : tt.shape) ne *= dd;+ pending.push_back({&Q8, q8off.at(tt.name), (size_t)(ne / 32 * 34) / 16 * 16});+ } else {+ pending.push_back({&W, tt.offset, (size_t)tt.bytes / 16 * 16});+ }+ }+ }};auto xprep = [&](const mtl::Buffer& x, const Act& out, int n, const std::string& c) {mtl::Dispatch d;