From 8c6d642e5d9047f3aeac41491bf2ecd0d12d64cb Mon Sep 17 00:00:00 2001 From: HugoLopes45 Date: Thu, 26 Nov 2026 14:58:05 +0100 Subject: [PATCH] [Engine] Metal: fit Inkling 2's mix kernels to the GPU's threadgroup limit On an Apple M1 Max the ink_conv_mix_rows_3_4 pipeline allows at most 832 threads per threadgroup, but the mix kernels were always launched with 1,024, so the first request stopped the engine (exit 6, the #2323 guard). The kernels stride by the threadgroup width and reduce over its SIMD groups, so they compute the same mix with fewer threads as long as those cover the row. Launch with the pipeline's limit, rounded down to whole SIMD groups, and refuse when it cannot cover the row. A GPU that allows 1,024 threads launches as before. inkling_parity now checks ink_conv_mix and ink_conv_mix_rows at Inkling 2 Mini's width (2,304) with 1 and 3 queries. --- engine/metal/poly_tests/inkling_parity.cpp | 53 ++++++++++++++++++++++ engine/metal/src/poly/inkling.cpp | 17 +++++-- 2 files changed, 66 insertions(+), 4 deletions(-) diff --git a/engine/metal/poly_tests/inkling_parity.cpp b/engine/metal/poly_tests/inkling_parity.cpp index 9d28051055..b889947dfc 100644 --- a/engine/metal/poly_tests/inkling_parity.cpp +++ b/engine/metal/poly_tests/inkling_parity.cpp @@ -266,6 +266,58 @@ int convolutions(int rows) { return bad + compare(SN.read(snap.size()), snap, label); } +// ---- a convolution's add fused with the next mix (ink_conv_mix(), ink_conv_mix_rows()) ---- +// Inkling 2 Mini's width; the launch fits the pipeline's threadgroup limit (832 on an M1 Max, 1,024 elsewhere). +int conv_mixes(int rows, int nq) { + const int n = 2304, taps = 4, ns = 3; + const float a = .5f, eps = 1e-6f, cnorm = 1 / std::sqrt(float(taps + 1)); + auto x = values((size_t) rows * n, .9f), state = values((size_t) (taps - 1) * n, .7f), w = values((size_t) n * taps, .3f), + acc = values((size_t) rows * n, 1.2f), s0 = values((size_t) rows * n, 1.f), s1 = values((size_t) rows * n, 1.5f), + q = values((size_t) nq * n, .05f), nw = values(n, .9f); + std::vector ao((size_t) rows * n), acc2 = acc; + for (int t = 0; t < rows; ++t) + for (int i = 0; i < n; ++i) { + const size_t k = (size_t) t * n + i; + ao[k] = conv_ref(x, state, w, nullptr, n, 1, taps, t, i, cnorm); + acc2[k] = acc[k] + ao[k] * a; + } + std::vector> ref; + mix_ref({s0, s1, acc2}, q, nq, n, rows, ref); + std::vector nref((size_t) rows * n); + for (int r = 0; r < rows; ++r) norm_ref(ref[0].data() + (size_t) r * n, nw.data(), nref.data() + (size_t) r * n, n, eps); + const size_t bytes = (size_t) rows * n * 4; + Buffer X(x), ST(state), W(w), AO(bytes), AC(acc), S0(s0), S1(s1), Q(q), NW(nw), N(bytes), O0(bytes), O1(bytes), O2(bytes); + InkStates st; + st.p[0] = S0.as(), st.p[1] = S1.as(), st.p[2] = AC.as(), st.n = ns; + float* out[3] = {O0.as(), O1.as(), O2.as()}; + char label[64]; + const char* what = rows == 1 ? "ink_conv_mix" : "ink_conv_mix_rows"; + if (rows == 1) + ink_conv_mix(X.as(), ST.as(), W.as(), AO.as(), AC.as(), nullptr, a, 0, taps, st, Q.as(), nq, out, n, NW.as(), + N.as(), eps, nullptr); + else + ink_conv_mix_rows(X.as(), ST.as(), W.as(), AO.as(), AC.as(), nullptr, a, 0, taps, st, Q.as(), nq, out, n, rows, + NW.as(), N.as(), eps, nullptr); + std::snprintf(label, sizeof label, "%s %d rows, %d queries: conv", what, rows, nq); + int bad = compare(AO.read(ao.size()), ao, label); + std::snprintf(label, sizeof label, "%s %d rows, %d queries: acc", what, rows, nq); + bad += compare(AC.read(acc2.size()), acc2, label); + Buffer* outs[3] = {&O0, &O1, &O2}; + for (int j = 0; j < nq; ++j) { + std::snprintf(label, sizeof label, "%s %d rows, %d queries: out %d", what, rows, nq, j); + bad += compare(outs[j]->read(rows * n), ref[(size_t) j], label); + } + std::snprintf(label, sizeof label, "%s %d rows, %d queries: norm", what, rows, nq); + bad += compare(N.read(nref.size()), nref, label); + if (rows == 1) { + std::vector after = state; + state_ref(x, after, n, taps, 1); + std::snprintf(label, sizeof label, "%s: state", what); + bad += compare(ST.read(after.size()), after, label); + } + return bad; +} + // ---- one token's Q, K and V (ink_qkv()) and a call's clips (ink_clips()) ---- void clip_ref(float* v, const float* sc, float thr, int dim, float alpha, float floor_, long p) { double ss = 0; @@ -502,6 +554,7 @@ int main() { bad += attention({"ink_attend_batch: 100 tokens after 300", 100, 300, 0, 0, 64, false, true}); bad += attention({"ink_attend_batch: 70 tokens, a window of 128 in a ring", 70, 500, 128, 198, 128, false, true}); bad += convolutions(5) + convolutions(40); + bad += conv_mixes(1, 1) + conv_mixes(1, 3) + conv_mixes(4, 1) + conv_mixes(4, 3); bad += qkv(); bad += situ_gates(); std::printf("%s\n", bad ? "FAILED" : "all passed"); diff --git a/engine/metal/src/poly/inkling.cpp b/engine/metal/src/poly/inkling.cpp index 33e499be8f..ad5d15365f 100644 --- a/engine/metal/src/poly/inkling.cpp +++ b/engine/metal/src/poly/inkling.cpp @@ -49,6 +49,15 @@ uint32_t pipe_max_threads(const char* name) { return gpu::metal::max_threads(gpu::metal::pipeline("poly_inkling", source().c_str(), name, err)); } +// The mix kernels stride by the threadgroup width over at least 3 slots a thread up to 3,072 values (4 above), so a +// GPU whose pipeline allows fewer than 1,024 threads (M1 Max: 832) runs them narrower, in whole SIMD groups. +uint32_t mix_threads(const char* name, int n_embd) { + const uint32_t t = std::min(1024, pipe_max_threads(name)) & ~31u; + if (t * (n_embd <= 3072 ? 3u : 4u) < uint32_t(n_embd)) + throw std::invalid_argument(std::string(name) + ": the GPU's threadgroup limit cannot cover the row"); + return t; +} + [[noreturn]] void unported(const char* what) { throw std::invalid_argument(std::string("Metal has no ") + what); } void elem(Args& a, int kind, size_t n, void* stream) { @@ -141,7 +150,7 @@ void mix(const InkStates& states, const float* const* query, int n_query, float* a.n[1] = n_query; a.n[2] = n_embd; a.f[0] = eps; - run("ink_mix", a, stream, {uint32_t(rows), uint32_t(jobs), 1}, 1024); + run("ink_mix", a, stream, {uint32_t(rows), uint32_t(jobs), 1}, mix_threads("ink_mix", n_embd)); } } // namespace @@ -417,7 +426,7 @@ void ink_conv_mix(const float* x, float* state, const float* w, float* ao, float g.f[1] = a; g.f[2] = conv_norm(nullptr, taps); const char* k = n_embd <= 3072 ? (n_query == 1 ? "ink_conv_mix_3_1" : "ink_conv_mix_3_4") : "ink_conv_mix_4_4"; - run(k, g, stream, {1, 1, 1}, 1024); + run(k, g, stream, {1, 1, 1}, mix_threads(k, n_embd)); } void ink_conv_mix_rows(const float* x, const float* state, const float* w, float* ao, float* acc, float* keep, float a, @@ -451,7 +460,7 @@ void ink_conv_mix_rows(const float* x, const float* state, const float* w, float g.f[2] = conv_norm(nullptr, taps); const char* k = n_embd <= 3072 ? (n_query == 1 ? "ink_conv_mix_rows_3_1" : "ink_conv_mix_rows_3_4") : "ink_conv_mix_rows_4_4"; - run(k, g, stream, {uint32_t(rows), 1, 1}, 1024); + run(k, g, stream, {uint32_t(rows), 1, 1}, mix_threads(k, n_embd)); } void ink_conv_state_update(const float* x, float* state, int channels, int taps, int rows, float* snap, @@ -509,7 +518,7 @@ void ink_moe_conv_mix(const float* x, const float* w, const float* scale, const g.f[2] = conv_norm(nullptr, taps); const char* k = n_embd <= 3072 ? (n_query == 1 ? "ink_moe_conv_mix_3_1" : "ink_moe_conv_mix_3_4") : "ink_moe_conv_mix_4_4"; - run(k, g, stream, {1, 1, 1}, 1024); + run(k, g, stream, {1, 1, 1}, mix_threads(k, n_embd)); } // ---- the clips ----