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: the M4's expert tiles for the Unsloth formats (mac-kern 513fc615): decode down Q5_1 4,1,4 and Q8_0 8,1,4 without the M5 layout; v2 prompt K-quant gate/up 16_16_128_2

Niko122113ed80333441
diff --git a/metal/src/engine/window.cpp b/metal/src/engine/window.cpp
index dee2eb574..81c9d0a54 100644
--- a/metal/src/engine/window.cpp
+++ b/metal/src/engine/window.cpp
@@ -330,6 +330,8 @@ private:
const mtl::Buffer* membp_ = nullptr; // mtp_encode's embedding rows (nullptr: memb_)
const mtl::Buffer* mpsrc_ = nullptr; // the drafter's prompt rows (nullptr: R_)
bool coop_k4();
+ int m5_layout_ = -1; // the M5's matmul2d layout (coop_k4), asked once: the M4's tiles differ
+ bool m5_layout() { if (m5_layout_ < 0) m5_layout_ = coop_k4() ? 1 : 0; return m5_layout_ == 1; }
bool pc_setup(int C);
bool pc_chunk(const int64_t* tokens, int nT, int64_t pos0, std::string& err);
// the draft round in one command buffer (msl/mtp_chain.metal): STRATA_METAL_MTP_CHAIN=0 runs a command buffer
@@ -721,7 +723,12 @@ void Window::layer(std::vector<mtl::Dispatch>& ds, int l, int ntok, int64_t pos0
a.topk = TOPK;
a.n_expert = NEXP;
const bool dq2 = td.type == ref::Q2_0;
- const int gl = 32, gr = 1, gs = 4, dl = dq2 ? 8 : 4, dr = dq2 ? 1 : 2, ds_ = 4;
+ const int gl = 32, gr = 1, gs = 4, ds_ = 4;
+ // the down tile: Q2_0 8,1; else 4,2 - but on the M4 (no M5 layout) Q5_1 4,1 and Q8_0 8,1 (4,2 spills there:
+ // 24-36 GB/s, mac-kern 513fc615)
+ const bool m4 = !m5_layout();
+ const int dl = dq2 || (m4 && td.type == ref::Q8_0) ? 8 : 4,
+ dr = dq2 || (m4 && (td.type == ref::Q8_0 || td.type == ref::Q5_1)) ? 1 : 2;
a.gx_gu = NFF / (gs * (32 / gl) * gr);
a.gx_dn = N / (ds_ * (32 / dl) * dr);
mtl::Dispatch r;
@@ -1620,7 +1627,12 @@ void Window::mtp_encode(std::vector<mtl::Dispatch>& ds, const mtl::Buffer& srcR,
a.d_stride = (uint64_t) a.d_row * N;
a.n_embd = N; a.n_ff = NFF; a.n_tok = 1; a.topk = TOPK; a.n_expert = NEXP;
const bool dq2 = td.type == ref::Q2_0;
- const int gl = 32, gr = 1, gs = 4, dl = dq2 ? 8 : 4, dr = dq2 ? 1 : 2, ds_ = 4;
+ const int gl = 32, gr = 1, gs = 4, ds_ = 4;
+ // the down tile: Q2_0 8,1; else 4,2 - but on the M4 (no M5 layout) Q5_1 4,1 and Q8_0 8,1 (4,2 spills there:
+ // 24-36 GB/s, mac-kern 513fc615)
+ const bool m4 = !m5_layout();
+ const int dl = dq2 || (m4 && td.type == ref::Q8_0) ? 8 : 4,
+ dr = dq2 || (m4 && (td.type == ref::Q8_0 || td.type == ref::Q5_1)) ? 1 : 2;
a.gx_gu = NFF / (gs * (32 / gl) * gr);
a.gx_dn = N / (ds_ * (32 / dl) * dr);
mtl::Dispatch r;
@@ -1812,7 +1824,7 @@ struct PgMm { uint64_t gate_off, up_off, down_off, gu_stride, d_stride; uint32_t
int pg_unit(int t) { return t == ref::Q2_0 || t == ref::IQ3_S || t == ref::BF16 || t == ref::F32 ? 16 : 32; }
// the v2 (M4) expert tiles: the K-quants need TK 128, Q8_0 / IQ4_XS TK 32 (their 32-value units), the rest TK 16
std::string gu2_tile(int t) {
- if (t == ref::Q4_K || t == ref::Q5_K || t == ref::Q6_K) return "_16_32_128_2";
+ if (t == ref::Q4_K || t == ref::Q5_K || t == ref::Q6_K) return "_16_16_128_2"; // the M4's best (513fc615)
if (t == ref::Q8_0 || t == ref::IQ4_XS) return "_16_64_32_2";
return "_16_64_16_2";
}