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: Q8_0 HC through the 3-dispatch read (gr_down/gr_up templated on the format) - the best HC variant

Niko1221ac36e3d4c065
diff --git a/metal/kernels/gr.metal b/metal/kernels/gr.metal
index 52156b022..e4da6d173 100644
--- a/metal/kernels/gr.metal
+++ b/metal/kernels/gr.metal
@@ -53,8 +53,8 @@ kernel void gr_norm(device float* R [[buffer(0)]], device const float* bo [[buff
// rows: 0 .. hc_lr - 1 = w_down, hc_lr .. hc_lr + hc - 1 = w_inject (both bf16 rows of hc * n_embd).
// SG simdgroups split the row's units KS ways; LPR = 32 (a simdgroup per row); RS = SG / KS rows per threadgroup.
-template <int SG, int KS>
-kernel void gr_down(device const uchar* w_down [[buffer(0)]], device const uchar* w_inject [[buffer(1)]],
+template <int SG, int KS, int F>
+kernel void gr_down_f(device const uchar* w_down [[buffer(0)]], device const uchar* w_inject [[buffer(1)]],
device const float* xn [[buffer(2)]], device float* lo [[buffer(3)]],
device float* inject [[buffer(4)]], constant GrArgs& a [[buffer(5)]],
uint3 tg [[threadgroup_position_in_grid]], ushort sg [[simdgroup_index_in_threadgroup]],
@@ -67,19 +67,20 @@ kernel void gr_down(device const uchar* w_down [[buffer(0)]], device const uchar
const int nrows = int(a.hc_lr + a.hc);
const int rr = min(row, nrows - 1);
const int nin = int(a.hc * a.n_embd);
- const uint rb = uint(nin) * 2;
+ constexpr int UNIT = fmt<F>::UNIT;
+ const uint rb = uint(nin / fmt<F>::QK * fmt<F>::BB);
device const uchar* w = rr < int(a.hc_lr) ? w_down + ulong(rr) * rb : w_inject + ulong(rr - int(a.hc_lr)) * rb;
- const int U = nin / 16, u0 = ks * U / KS, u1 = (ks + 1) * U / KS;
+ const int U = nin / UNIT, u0 = ks * U / KS, u1 = (ks + 1) * U / KS;
float acc[8];
for (int t = 0; t < 8; ++t) acc[t] = 0.0f;
for (int u = u0 + lane; u < u1; u += 32) {
- float q[16];
+ float q[UNIT];
float4 sc;
- Dec<T_BF16>::run(w, u, q, sc, notab);
+ Dec<F>::run(w, u, q, sc, notab);
for (int t = 0; t < GR_NTOK; ++t) {
- float xs[16], xsum[2] = {0.0f, 0.0f};
- load16(xn + t * nin + 16 * u, xs);
- acc[t] += unit_combine<16>(q, sc, xs, xsum);
+ float xs[UNIT], xsum[2] = {0.0f, 0.0f};
+ for (int k = 0; k < UNIT; k += 16) load16(xn + t * nin + UNIT * u + k, xs + k);
+ acc[t] += unit_combine<UNIT>(q, sc, xs, xsum);
}
}
for (int t = 0; t < GR_NTOK; ++t) {
@@ -103,7 +104,8 @@ kernel void gr_down(device const uchar* w_down [[buffer(0)]], device const uchar
// A threadgroup owns 16 consecutive d for all hc = 4 streams: 8 simdgroups, simdgroup s -> stream s / 2 and
// d0 + 8 (s % 2) .. + 8; LPR = 4 lanes per row (320 = 20 units of 16, 5 per lane).
// XP: [4 kinds][n_tok][n] then S16 [n_tok][n / 16] (units.metal's xprep layout).
-kernel void gr_up(device const uchar* w_up [[buffer(0)]], device const float* lo [[buffer(1)]],
+template <int F>
+kernel void gr_up_f(device const uchar* w_up [[buffer(0)]], device const float* lo [[buffer(1)]],
device const float* xn [[buffer(2)]], device float* xp [[buffer(3)]],
constant GrArgs& a [[buffer(4)]],
uint3 tg [[threadgroup_position_in_grid]], ushort sg [[simdgroup_index_in_threadgroup]],
@@ -113,18 +115,19 @@ kernel void gr_up(device const uchar* w_up [[buffer(0)]], device const float* lo
threadgroup uint notab[1];
const int n = int(a.n_embd), c = sg >> 1, dl = 8 * (sg & 1) + lane / 4, li = lane & 3;
const int d = int(tg.x) * 16 + dl;
- device const uchar* w = w_up + ulong(c * n + d) * (a.hc_lr * 2);
- const int U = int(a.hc_lr) / 16;
+ constexpr int UNIT = fmt<F>::UNIT;
+ device const uchar* w = w_up + ulong(c * n + d) * (a.hc_lr / fmt<F>::QK * fmt<F>::BB);
+ const int U = int(a.hc_lr) / UNIT;
float acc[8];
for (int t = 0; t < 8; ++t) acc[t] = 0.0f;
for (int u = li; u < U; u += 4) {
- float q[16];
+ float q[UNIT];
float4 sc;
- Dec<T_BF16>::run(w, u, q, sc, notab);
+ Dec<F>::run(w, u, q, sc, notab);
for (int t = 0; t < GR_NTOK; ++t) {
- float xs[16], xsum[2] = {0.0f, 0.0f};
- load16(lo + t * int(a.hc_lr) + 16 * u, xs);
- acc[t] += unit_combine<16>(q, sc, xs, xsum);
+ float xs[UNIT], xsum[2] = {0.0f, 0.0f};
+ for (int k = 0; k < UNIT; k += 16) load16(lo + t * int(a.hc_lr) + UNIT * u + k, xs + k);
+ acc[t] += unit_combine<UNIT>(q, sc, xs, xsum);
}
}
for (int t = 0; t < GR_NTOK; ++t) {
@@ -160,9 +163,8 @@ kernel void gr_up(device const uchar* w_up [[buffer(0)]], device const float* lo
}
}
-template [[host_name("gr_down_8_8")]] kernel decltype(gr_down<8, 8>) gr_down<8, 8>;
-template [[host_name("gr_down_8_4")]] kernel decltype(gr_down<8, 4>) gr_down<8, 4>;
-template [[host_name("gr_down_4_4")]] kernel decltype(gr_down<4, 4>) gr_down<4, 4>;
-template [[host_name("gr_down_4_2")]] kernel decltype(gr_down<4, 2>) gr_down<4, 2>;
-template [[host_name("gr_down_16_16")]] kernel decltype(gr_down<16, 16>) gr_down<16, 16>;
-template [[host_name("gr_down_16_8")]] kernel decltype(gr_down<16, 8>) gr_down<16, 8>;
+#define GRD(S, K) template [[host_name("gr_down_" #S "_" #K)]] kernel decltype(gr_down_f<S, K, T_BF16>) gr_down_f<S, K, T_BF16>; \
+ template [[host_name("gr_down_q8_" #S "_" #K)]] kernel decltype(gr_down_f<S, K, T_Q8_0>) gr_down_f<S, K, T_Q8_0>;
+GRD(8, 8) GRD(8, 4) GRD(4, 4) GRD(4, 2) GRD(16, 16) GRD(16, 8)
+template [[host_name("gr_up")]] kernel decltype(gr_up_f<T_BF16>) gr_up_f<T_BF16>;
+template [[host_name("gr_up_q8")]] kernel decltype(gr_up_f<T_Q8_0>) gr_up_f<T_Q8_0>;
diff --git a/metal/tests/gr_parity.cpp b/metal/tests/gr_parity.cpp
index 642a2e258..e42e2356a 100644
--- a/metal/tests/gr_parity.cpp
+++ b/metal/tests/gr_parity.cpp
@@ -37,6 +37,8 @@ int main(int argc, char** argv) {
const int lib = cx.compile({"quant.metal", "units.metal", "gr.metal"});
std::printf("device %s\n", cx.device_name().c_str());
const int SETS = bench ? 48 : 1;
+ const bool q8 = std::getenv("GR_Q8") != nullptr; // the Q8_0 form of the HC matrices (gr_down_q8_*, gr_up_q8)
+ const int wty = q8 ? ref::Q8_0 : ref::BF16;
const size_t wd_bytes = (size_t)LR * NIN * 2, wi_bytes = (size_t)HC * NIN * 2, wu_bytes = (size_t)NIN * LR * 2;
const size_t set_bytes = wd_bytes + wi_bytes + wu_bytes + NIN * 4;
mtl::Buffer W = cx.alloc(set_bytes * SETS);
@@ -48,6 +50,16 @@ int main(int argc, char** argv) {
ref::random_blocks(ref::BF16, b + wd_bytes + wi_bytes, NIN, LR, 33 + s, 0.1f);
float* wn = (float*)(b + wd_bytes + wi_bytes + wu_bytes);
for (int i = 0; i < NIN; ++i) wn[i] = 1.0f + 0.1f * rng.sym();
+ if (q8) {
+ // GR_Q8: the three matrices as Q8_0 (quantized from the BF16 values), each at its BF16 offset (it fits)
+ const size_t offs[3] = {0, wd_bytes, wd_bytes + wi_bytes};
+ const size_t ns[3] = {(size_t)LR * NIN, (size_t)HC * NIN, (size_t)NIN * LR};
+ for (int m = 0; m < 3; ++m) {
+ std::vector<float> f(ns[m]);
+ ref::dequant(ref::BF16, b + offs[m], f.data(), ns[m]);
+ ref::quantize_q8_0(f.data(), ns[m], b + offs[m]);
+ }
+ }
}
const int MAXT = 8;
mtl::Buffer R = cx.alloc(4 * MAXT * NIN), R0 = cx.alloc(4 * MAXT * NIN), bo = cx.alloc(4 * MAXT * N),
@@ -60,8 +72,9 @@ int main(int argc, char** argv) {
mtl::Pipeline pprep = cx.pipeline(lib, "xprep");
int fails = 0;
for (int nt : ntoks) {
- mtl::Pipeline pn = cx.pipeline(lib, "gr_norm"), pd = cx.pipeline(lib, "gr_down_8_8", mtl::Constants().i(1, nt)),
- pu = cx.pipeline(lib, "gr_up", mtl::Constants().i(1, nt));
+ mtl::Pipeline pn = cx.pipeline(lib, "gr_norm"),
+ pd = cx.pipeline(lib, q8 ? "gr_down_q8_8_8" : "gr_down_8_8", mtl::Constants().i(1, nt)),
+ pu = cx.pipeline(lib, q8 ? "gr_up_q8" : "gr_up", mtl::Constants().i(1, nt));
for (int apply = 0; apply < 2; ++apply) {
GrArgs a{N, HC, LR, (uint32_t)nt, (uint32_t)apply, 1e-6f};
std::memcpy(R.ptr, R0.ptr, R.bytes);
@@ -103,9 +116,9 @@ int main(int argc, char** argv) {
// reference
const uint8_t* b = W.as<uint8_t>();
std::vector<float> wd((size_t)LR * NIN), wi((size_t)HC * NIN), wu((size_t)NIN * LR);
- ref::dequant(ref::BF16, b, wd.data(), wd.size());
- ref::dequant(ref::BF16, b + wd_bytes, wi.data(), wi.size());
- ref::dequant(ref::BF16, b + wd_bytes + wi_bytes, wu.data(), wu.size());
+ ref::dequant(wty, b, wd.data(), wd.size());
+ ref::dequant(wty, b + wd_bytes, wi.data(), wi.size());
+ ref::dequant(wty, b + wd_bytes + wi_bytes, wu.data(), wu.size());
const float* wn = (const float*)(b + wd_bytes + wi_bytes + wu_bytes);
Err eR, elo, einj, emix;
for (int t = 0; t < nt; ++t) {
diff --git a/metal/tests/token_chain.cpp b/metal/tests/token_chain.cpp
index b6774ae98..bb6550786 100644
--- a/metal/tests/token_chain.cpp
+++ b/metal/tests/token_chain.cpp
@@ -196,6 +196,8 @@ int main(int argc, char** argv) {
const mtl::Pipeline* pnorm = Pn(lib, "gr_norm", "gr_norm", {});
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));
+ const mtl::Pipeline* pupq = Pn(lib, "gr_up_q8", "gr_up_q8", mtl::Constants().i(1, ntok));
const mtl::Pipeline* pprep = Pn(lib, "xprep", "xprep", {});
const mtl::Pipeline* proute = Pn(lib, "moe_route", "moe_route", {});
const mtl::Pipeline* pswi = Pn(lib, "swiglu_prep", "swiglu_prep", {});
@@ -217,7 +219,7 @@ int main(int argc, char** argv) {
int cur_r = 0, cur_d = 0;
std::map<std::string, size_t> q8off;
mtl::Buffer Q8;
- if (hc_mode == "q8") {
+ if (hc_mode == "q8" || hc_mode == "q8_3") {
size_t tot = 0;
for (const auto& kv : f.tensors) {
const gguf::Tensor& t = kv.second;
@@ -242,7 +244,7 @@ int main(int argc, char** argv) {
}
int cur_inj = 0;
auto gr_read = [&](const std::string& pre, bool apply, const Act& out, bool final_) {
- if (hc_mode != "3") {
+ if (hc_mode != "3" && hc_mode != "q8_3") {
const bool q8 = hc_mode == "q8";
const gguf::Tensor& tn = T(pre + "_norm.weight");
const gguf::Tensor& tdn = T(pre + "_down.weight");
@@ -284,6 +286,10 @@ int main(int argc, char** argv) {
const gguf::Tensor& tup = T(pre + "_up.weight");
const gguf::Tensor& tin = final_ ? tdn : T(pre + "_inject.weight");
GrArgs a{N, HC, LR, (uint32_t)ntok, apply ? 1u : 0u, 1e-6f};
+ const bool q83 = hc_mode == "q8_3";
+ const mtl::Buffer& WB = q83 ? Q8 : W;
+ const size_t od = q83 ? q8off.at(tdn.name) : tdn.offset, oi = q83 ? q8off.at(tin.name) : tin.offset,
+ ou = q83 ? q8off.at(tup.name) : tup.offset;
mtl::Dispatch d1;
d1.p = pnorm;
d1.args = {mtl::Arg::B(R), mtl::Arg::B(bo), mtl::Arg::B(inj[cur_inj]), mtl::Arg::B(W, tn.offset), mtl::Arg::B(xn),
@@ -293,15 +299,15 @@ int main(int argc, char** argv) {
d1.tg[0] = 256;
push(d1, "hc", 0);
mtl::Dispatch d2;
- d2.p = pdown;
- d2.args = {mtl::Arg::B(W, tdn.offset), mtl::Arg::B(W, tin.offset), mtl::Arg::B(xn), mtl::Arg::B(lo),
+ d2.p = q83 ? pdownq : pdown;
+ d2.args = {mtl::Arg::B(WB, od), mtl::Arg::B(WB, oi), mtl::Arg::B(xn), mtl::Arg::B(lo),
mtl::Arg::B(inj[1 - cur_inj]), mtl::Arg::V(a)};
d2.grid[0] = LR + HC;
d2.tg[0] = 256;
push(d2, "hc", (double)tdn.bytes + (final_ ? 0 : tin.bytes));
mtl::Dispatch d3;
- d3.p = pup;
- d3.args = {mtl::Arg::B(W, tup.offset), mtl::Arg::B(lo), mtl::Arg::B(xn), mtl::Arg::B(out.b), mtl::Arg::V(a)};
+ d3.p = q83 ? pupq : pup;
+ d3.args = {mtl::Arg::B(WB, ou), mtl::Arg::B(lo), mtl::Arg::B(xn), mtl::Arg::B(out.b), mtl::Arg::V(a)};
d3.grid[0] = N / 16;
d3.tg[0] = 256;
push(d3, "hc", (double)tup.bytes);
@@ -527,10 +533,10 @@ int main(int argc, char** argv) {
// the producers' plane stride must be MAXT rows: gr_up's and swiglu's args carry n_tok for the stride, so the
// gr_up launches above use a.n_tok = ntok - patch them to MAXT (the kernels loop over GR_NTOK tokens anyway)
for (auto& d : ds)
- if (d.p == pup || d.p == pnorm || d.p == pdown) {
+ if (d.p == pup || d.p == pupq || d.p == pnorm || d.p == pdown || d.p == pdownq) {
GrArgs a;
std::memcpy(&a, d.args.back().bytes.data(), sizeof a);
- if (d.p == pup) a.n_tok = MAXT;
+ if (d.p == pup || d.p == pupq) a.n_tok = MAXT;
std::memcpy(d.args.back().bytes.data(), &a, sizeof a);
}
@@ -543,7 +549,7 @@ int main(int argc, char** argv) {
while (mtl::now_s() - tw < 1.0) run();
std::vector<double> t;
for (int i = 0; i < reps; ++i) t.push_back(run());
- std::printf("HC read: %s\n", hc_mode == "3" ? "BF16, 3 dispatches" : hc_mode == "2" ? "BF16, 2 dispatches" : "Q8_0, 2 dispatches");
+ std::printf("HC read: %s\n", hc_mode == "3" ? "BF16, 3 dispatches" : hc_mode == "2" ? "BF16, 2 dispatches" : hc_mode == "q8_3" ? "Q8_0, 3 dispatches" : "Q8_0, 2 dispatches");
std::printf("encoding: %s\n", graph ? "concurrent encoder, barriers at dependencies" : "serial encoder");
const double m = median(t);
std::printf("decode step (%d token(s)): GPU %.3f ms (min %.3f, max %.3f) -> %.1f GB/s effective; %.1f tok/s if "