From ae84bcdc14b62070614e34c887a7ebd02fb69009 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Mon, 12 Oct 2026 12:27:11 +0200 Subject: [PATCH] metal: GDN step templated on the row blocks (8: no gain), token_chain expert tiles by environment - gdn.metal: gdn_step_t; gdn_step8 (1024 threads, 16 state rows a thread) passes and is not faster than gdn_step (16.3 vs 16.4 us commit, n=1): the state write after the compute is what a commit adds. - token_chain: CHAIN_GU / CHAIN_DN / CHAIN_DN4 select the routed-expert tiles; measured in the chain (Q8_0 HC, --fuse, --graph): gate/up 32,1,4 stays; the Q2_0 down 8,1,4 over 8,2,4 (10.63 vs 10.67 ms, 3 tokens 17.23 vs 17.50 ms), now the default. --- metal/kernels/gdn.metal | 30 +++++++++++++++++++----------- metal/tests/gdn_parity.cpp | 9 +++++---- metal/tests/token_chain.cpp | 19 ++++++++++++------- 3 files changed, 36 insertions(+), 22 deletions(-) diff --git a/metal/kernels/gdn.metal b/metal/kernels/gdn.metal index 021c34f3..128ae54c 100644 --- a/metal/kernels/gdn.metal +++ b/metal/kernels/gdn.metal @@ -25,7 +25,8 @@ struct GdnArgs { // `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)]], +template +kernel void gdn_step_t(device float* state [[buffer(0)]], device float* hist [[buffer(1)]], device const float* qkv [[buffer(2)]], device const float* alpha [[buffer(3)]], device const float* beta [[buffer(4)]], device const float* z [[buffer(5)]], device const float* conv_w [[buffer(6)]], device const float* dt [[buffer(7)]], @@ -36,13 +37,14 @@ kernel void gdn_step(device float* state [[buffer(0)]], device float* hist [[buf ushort sg [[simdgroup_index_in_threadgroup]], ushort lane [[thread_index_in_simdgroup]]) { constexpr int S = 128; threadgroup float qs[S], ks[S], vs[S]; - threadgroup float red[4][S]; - threadgroup float part[16]; + constexpr int RPT = S / IB; // state rows per thread + threadgroup float red[IB][S]; + threadgroup float part[32]; 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; + const int j = 32 * cb + lane, i0 = RPT * ib; const float scale = 1.0f / sqrt(float(S)); // the channel this thread convolves: 0..127 q of head hk, 128..255 k of head hk, 256..383 v of head h @@ -55,8 +57,8 @@ kernel void gdn_step(device float* state [[buffer(0)]], device float* hist [[buf for (int k = 0; k < 3; ++k) hst[k] = hist[ch * 3 + k]; for (int k = 0; k < 4; ++k) cw[k] = conv_w[ch * 4 + k]; } - float st[32]; - for (int r = 0; r < 32; ++r) st[r] = state[((i0 + r) * hv + h) * S + j]; + float st[RPT]; + for (int r = 0; r < RPT; ++r) st[r] = state[((i0 + r) * hv + h) * S + j]; for (int t = 0; t < GDN_NTOK; ++t) { // conv + SiLU @@ -98,13 +100,14 @@ kernel void gdn_step(device float* state [[buffer(0)]], device float* hist [[buf const float g = gb[0], be = gb[1]; // kv_j float kv = 0.0f; - for (int r = 0; r < 32; ++r) kv = fma(st[r], ks[i0 + r], kv); + for (int r = 0; r < RPT; ++r) kv = fma(st[r], ks[i0 + r], kv); red[ib][j] = kv; threadgroup_barrier(mem_flags::mem_threadgroup); - kv = (red[0][j] + red[1][j]) + (red[2][j] + red[3][j]); + kv = 0.0f; + for (int k = 0; k < IB; ++k) kv += red[k][j]; const float delta = (vs[j] - g * kv) * be; float o = 0.0f; - for (int r = 0; r < 32; ++r) { + for (int r = 0; r < RPT; ++r) { st[r] = fma(g, st[r], ks[i0 + r] * delta); o = fma(st[r], qs[i0 + r], o); } @@ -112,7 +115,9 @@ kernel void gdn_step(device float* state [[buffer(0)]], device float* hist [[buf red[ib][j] = o; threadgroup_barrier(mem_flags::mem_threadgroup); // the readout and the output norm: column j's o (every i-block thread of the column holds the same value) - o = ((red[0][j] + red[1][j]) + (red[2][j] + red[3][j])) * scale; + o = 0.0f; + for (int k = 0; k < IB; ++k) o += red[k][j]; + o = o * scale; const float osq = ib == 0 ? simd_sum(o * o) : 0.0f; if (ib == 0 && lane == 0) part[cb] = osq; threadgroup_barrier(mem_flags::mem_threadgroup); @@ -142,7 +147,7 @@ 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]; + for (int r = 0; r < RPT; ++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 @@ -150,3 +155,6 @@ kernel void gdn_step(device float* state [[buffer(0)]], device float* hist [[buf for (int k = 0; k < 3; ++k) hist_out[ch * 3 + k] = hst[k]; } } + +template [[host_name("gdn_step")]] kernel decltype(gdn_step_t<4>) gdn_step_t<4>; +template [[host_name("gdn_step8")]] kernel decltype(gdn_step_t<8>) gdn_step_t<8>; diff --git a/metal/tests/gdn_parity.cpp b/metal/tests/gdn_parity.cpp index 8276495f..d7a30e1a 100644 --- a/metal/tests/gdn_parity.cpp +++ b/metal/tests/gdn_parity.cpp @@ -59,7 +59,7 @@ int main(int argc, char** argv) { 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(hist2, 4 * (size_t)C * 3 * layer)}; d.grid[0] = HV; - d.tg[0] = 512; + d.tg[0] = std::getenv("GDN_IB8") ? 1024 : 512; return d; }; { @@ -68,7 +68,8 @@ int main(int argc, char** argv) { } if (bench) { for (int nt : {1, 2, 3, 4}) { - mtl::Pipeline p = cx.pipeline(lib, "gdn_step", mtl::Constants().i(2, nt)); + const char* kn = std::getenv("GDN_IB8") ? "gdn_step8" : "gdn_step"; + mtl::Pipeline p = cx.pipeline(lib, kn, mtl::Constants().i(2, nt)); for (int commit : {1, 0}) { std::vector ds; for (int k = 0; k < 72; ++k) ds.push_back(disp(p, k % LAYERS, commit, y)); @@ -84,7 +85,7 @@ 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)); + mtl::Pipeline p = cx.pipeline(lib, std::getenv("GDN_IB8") ? "gdn_step8" : "gdn_step", mtl::Constants().i(2, nt)); // determinism: the same verify call three times { std::memcpy(state.ptr, st0.ptr, 4 * st_elems); @@ -163,7 +164,7 @@ int main(int argc, char** argv) { // fold property: a window of nt + 1 tokens (no commit) gives bitwise the same first nt outputs bool prefix = true; if (nt < 4) { - mtl::Pipeline p2 = cx.pipeline(lib, "gdn_step", mtl::Constants().i(2, nt + 1)); + mtl::Pipeline p2 = cx.pipeline(lib, std::getenv("GDN_IB8") ? "gdn_step8" : "gdn_step", mtl::Constants().i(2, nt + 1)); std::memcpy(state.ptr, st0.ptr, 4 * st_elems); std::memcpy(hist.ptr, h0.ptr, 4 * C * 3); cx.run({disp(p2, 0, 0, y2)}); diff --git a/metal/tests/token_chain.cpp b/metal/tests/token_chain.cpp index 7d9d4103..671224a3 100644 --- a/metal/tests/token_chain.cpp +++ b/metal/tests/token_chain.cpp @@ -426,10 +426,13 @@ int main(int argc, char** argv) { a.n_tok = ntok; a.topk = TOPK; a.n_expert = NEXP; - a.gx_gu = NFF / 4; // gu tile 32,1,4: 4 rows per threadgroup + // tiles (lanes per row, rows per lane, simdgroups): CHAIN_GU / CHAIN_DN / CHAIN_DN4 override const bool dq2 = td.type == ref::Q2_0; - const int dn_rpt = dq2 ? 4 * (32 / 8) * 2 : 4 * (32 / 4) * 2; // tiles 8,2,4 and 4,2,4 - a.gx_dn = N / dn_rpt; + int gl = 32, gr = 1, gs = 4, dl = dq2 ? 8 : 4, dr = dq2 ? 1 : 2, ds_ = 4; + if (const char* e = std::getenv("CHAIN_GU")) std::sscanf(e, "%d,%d,%d", &gl, &gr, &gs); + if (const char* e = std::getenv(dq2 ? "CHAIN_DN" : "CHAIN_DN4")) std::sscanf(e, "%d,%d,%d", &dl, &dr, &ds_); + a.gx_gu = NFF / (gs * (32 / gl) * gr); + a.gx_dn = N / (ds_ * (32 / dl) * dr); mtl::Dispatch r; r.p = proute; r.args = {mtl::Arg::B(logits), mtl::Arg::B(ids), mtl::Arg::B(wts), mtl::Arg::B(plan), mtl::Arg::V(a)}; @@ -446,13 +449,15 @@ int main(int argc, char** argv) { nb = true; push(d, "shared", 0); } - const std::string gun = "moe_gu_" + std::to_string(tg_.type) + "_32_1_4"; - const std::string dnn = dq2 ? "moe_down_142_8_2_4" : "moe_down_" + std::to_string(td.type) + "_4_2_4"; + const std::string gun = "moe_gu_" + std::to_string(tg_.type) + "_" + std::to_string(gl) + "_" + + std::to_string(gr) + "_" + std::to_string(gs); + const std::string dnn = "moe_down_" + std::to_string(dq2 ? 142 : td.type) + "_" + std::to_string(dl) + "_" + + std::to_string(dr) + "_" + std::to_string(ds_); mtl::Dispatch g; g.p = Pn(lib, gun, gun, {}); g.args = {mtl::Arg::B(W), mtl::Arg::B(XF.b, XF.plane_bytes() * xkind(tg_.type)), mtl::Arg::B(h), mtl::Arg::B(plan), mtl::Arg::V(a), mtl::Arg::B(XF.b, XF.plane_bytes() * 4)}; - g.tg[0] = 128; + g.tg[0] = 32 * gs; g.indirect = &plan; g.ind_off = P_IGU * 4; push(g, "experts", 10.0 * ntok * 2 * a.gu_stride); @@ -461,7 +466,7 @@ int main(int argc, char** argv) { mtl::Dispatch dd; dd.p = Pn(lib, dnn, dnn, {}); dd.args = {mtl::Arg::B(W), mtl::Arg::B(h), mtl::Arg::B(ye), mtl::Arg::B(plan), mtl::Arg::V(a), mtl::Arg::B(h)}; - dd.tg[0] = 128; + dd.tg[0] = 32 * ds_; dd.indirect = &plan; dd.ind_off = P_IDN * 4; push(dd, "experts", 10.0 * ntok * a.d_stride);