From 026d7cd4a12d12bd2a331d539a1c4917f0763452 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Mon, 12 Oct 2026 15:07:25 +0200 Subject: [PATCH] metal: the MTP draft round in one command buffer; catch-up embeddings fixed msl/mtp_chain.metal: mtp_pick (the pick through the draft vocabulary and its softmax probability) and mtp_embed_F (the picked token's embedding row through mac-kern's unit decoders), so every draft step of a round runs in one command buffer. At load the GPU embedding is checked against the host's dequant (max |diff| 0 on Q3_K and IQ3_S; any difference falls back to a command buffer per step). STRATA_METAL_MTP_CHAIN=0 keeps the per-step path. Fix: with accepted drafts (a > 0) draft 0's embedding overwrote the catch-up's row 0 before the GPU read it; draft 0's row now goes to memb_ row a. The dense attention's cell list is per step. M5 Max, 200 tokens, --spec 4, greedy output equal to no MTP in every run: Q2_0 109.5 / 84.4 tok/s (was 101.4 / 80.0), IQ3_XXS 107.9 / 86.0 (was 101.5 / 82.8). --- metal/src/engine/msl/mtp_chain.metal | 92 +++++++++++++++++++++ metal/src/engine/window.cpp | 115 ++++++++++++++++++++++++--- 2 files changed, 197 insertions(+), 10 deletions(-) create mode 100644 metal/src/engine/msl/mtp_chain.metal diff --git a/metal/src/engine/msl/mtp_chain.metal b/metal/src/engine/msl/mtp_chain.metal new file mode 100644 index 00000000..62500df2 --- /dev/null +++ b/metal/src/engine/msl/mtp_chain.metal @@ -0,0 +1,92 @@ +// metal/src/engine/msl/mtp_chain.metal - the two steps that keep a whole MTP draft round on the GPU (one command +// buffer for every draft step; compiled after mac-kern's quant.metal and units.metal): +// +// mtp_pick draft j: the pick's token id (through the draft vocabulary) and its probability, the softmax of +// the draft head's logits at the pick (the host computed it after every step before) +// mtp_embed_F the next step's input: the picked token's embedding row dequantized from the model's token_embd +// (format F) - the host dequantized it before +// +// mtp_embed reads a row with mac-kern's unit decoders (units.metal Dec): a unit's dot with x is +// d1 * sum_{j < UNIT/2} q[j] xs[j] + d2 * sum_{j >= UNIT/2} q[j] xs[j] - (m1 xsum0 + m2 xsum1) +// with xs the prepared x (raw x times the kind's 2^-k) and xsum the raw sums of 16, so weight j of the unit is +// w = d * (q[j] * 2^-k) - m (d = d1 or d2 by half, m = m1 for the first 16 x values, m2 for the rest; +// a 16-wide unit has one sum: m1) +// and its position in the row is what X::load reads when x holds the positions 0 .. n-1. + +struct PickArgs { + uint nv; // the draft head's rows (the draft vocabulary, or the whole vocabulary) + uint j; // the draft step + uint use_map; // 1: map the head's row through the draft vocabulary +}; + +// one threadgroup of 1024 +kernel void mtp_pick(device const float* lg [[buffer(0)]], device const int* pick [[buffer(1)]], + device const int* vmap [[buffer(2)]], device int* dout [[buffer(3)]], device float* dprob [[buffer(4)]], + constant PickArgs& a [[buffer(5)]], ushort tid [[thread_index_in_threadgroup]], + ushort sg [[simdgroup_index_in_threadgroup]], ushort lane [[thread_index_in_simdgroup]]) { + threadgroup float part[32]; + const int di = pick[0]; + const float mx = lg[di]; + float s = 0.0f; + for (uint i = tid; i < a.nv; i += 1024) s += exp(lg[i] - mx); + s = simd_sum(s); + if (lane == 0) part[sg] = s; + threadgroup_barrier(mem_flags::mem_threadgroup); + if (tid == 0) { + float t = 0.0f; + for (int k = 0; k < 32; ++k) t += part[k]; + dout[a.j] = a.use_map ? vmap[di] : di; + dprob[a.j] = 1.0f / t; + } +} + +struct EmbArgs { + uint n; // the row's width + uint row_bytes; + uint j; // dout[j] is the token +}; + +template static inline float kind_scale(int pos) { + const int k = XKind::K; + if (k == XK_Q2) { + const float q2s[8] = {1.0f, 0.25f, 0.0625f, 0.015625f, 0.00390625f, 0.0009765625f, 0.000244140625f, 0.00006103515625f}; + return q2s[pos & 7]; + } + if (k == XK_P4) { + const float p4s[4] = {1.0f, 0.00390625f, 1.52587890625e-05f, 5.9604644775390625e-08f}; + return p4s[pos & 3]; + } + if (k == XK_P2) return (pos & 1) ? 0.00390625f : 1.0f; + return 1.0f; +} + +// one threadgroup of 256; posv = the floats 0 .. n-1 +template +kernel void mtp_embed(device const uchar* W [[buffer(0)]], device const int* dout [[buffer(1)]], + device const float* posv [[buffer(2)]], device float* y [[buffer(3)]], constant EmbArgs& a [[buffer(4)]], + ushort tid [[thread_index_in_threadgroup]]) { + constexpr int UNIT = fmt::UNIT, H = UNIT / 2; + threadgroup uint tab[fmt::TAB]; + load_tab(tab, tid, 256); + threadgroup_barrier(mem_flags::mem_threadgroup); + device const uchar* row = W + (ulong) dout[a.j] * a.row_bytes; + const int U = int(a.n) / UNIT; + for (int u = tid; u < U; u += 256) { + float q[UNIT], ps[UNIT], xsum[2]; + float4 sc; + Dec::run(row, u, q, sc, tab); + X::load(posv, posv, u, ps, xsum); + for (int j = 0; j < UNIT; ++j) { + const int pos = int(ps[j]); + const float d = j < H ? sc.x : sc.y; + const float m = (UNIT == 16 || j < 16) ? sc.z : sc.w; + y[pos] = d * (q[j] * kind_scale(pos)) - m; + } + } +} + +// host names by the format's GGUF type number (mtp_embed_11 = Q3_K): EMB expands F before EMB_ stringifies it +#define EMB_(F) template [[host_name("mtp_embed_" #F)]] kernel decltype(mtp_embed) mtp_embed; +#define EMB(F) EMB_(F) +EMB(T_BF16) EMB(T_Q8_0) EMB(T_Q4_0) EMB(T_Q5_0) EMB(T_IQ4_NL) EMB(T_IQ4_XS) EMB(T_Q2_0) EMB(T_IQ3_S) EMB(T_Q3_K) +EMB(T_Q4_K) EMB(T_Q5_K) EMB(T_Q6_K) EMB(T_IQ3_XXS) diff --git a/metal/src/engine/window.cpp b/metal/src/engine/window.cpp index 8e9ab84e..bda72776 100644 --- a/metal/src/engine/window.cpp +++ b/metal/src/engine/window.cpp @@ -112,9 +112,14 @@ private: void layer(std::vector& ds, int l, int ntok, int64_t pos0); void ple_cpu(int T); // the MTP layer: the input branches + attention HC read + K/V append for `n` rows (srcR rows, host tokens) at - // cells cell0..; with `full` (n == 1) the rest of the layer too and the draft head into mlm_/tokout_ + // cells cell0..; with `full` (n == 1) the rest of the layer too and the draft head into mlm_/tokout_. The + // embedding rows go to memb_ rows erow..; tokens == nullptr: memb_ row erow already holds the embedding (put + // there by mtp_embed on the GPU). `slot`: the dense attention's cell list (one per draft step of a round). void mtp_encode(std::vector& ds, const mtl::Buffer& srcR, size_t src_off, int n, const int32_t* tokens, - int64_t cell0, bool full); + int64_t cell0, bool full, int erow = 0, int slot = 0); + // a draft round's step j on the GPU: the pick and its probability (dout_/dprob_ [j]), and with `embed` the picked + // token's embedding into memb_ row 0 (the next step's input) + void mtp_pick(std::vector& ds, int j, bool embed); void gr_read_m(std::vector& ds, const std::string& pre, mtl::Buffer& Rb, const mtl::Buffer& bo, const mtl::Buffer& inj_in, const mtl::Buffer& inj_out, bool apply, const Act& out, bool final_, int ntok); const gguf::Tensor& TM(const std::string& name) const { @@ -187,6 +192,11 @@ private: return it != mq8t_.end() ? it->second : TM(name); } Act XEn_, XHn_; + // the draft round in one command buffer (msl/mtp_chain.metal): STRATA_METAL_MTP_CHAIN=0 runs a command buffer + // per step with the host's pick and embedding (as before) + int lchain_ = -1; + bool chain_ = false; + mtl::Buffer dout_, dprob_, posv_, dmap_; }; bool Window::init(const strata::core::ModelGeometry& g, strata::core::SessionState& ss, const std::string& shard1, @@ -881,6 +891,45 @@ bool Window::mtp_load(const std::string& gguf_path, int max_t, std::string& err) } } mtp_on_ = true; + { + const int ty = T("token_embd.weight").type; + const int ok_types[] = {30, 8, 2, 6, 20, 23, 42, 21, 11, 12, 13, 14, 18}; + const char* e = std::getenv("STRATA_METAL_MTP_CHAIN"); + chain_ = !(e && std::string(e) == "0") && std::find(std::begin(ok_types), std::end(ok_types), ty) != std::end(ok_types); + if (chain_) { + lchain_ = cx_->compile({"quant.metal", "units.metal", std::string(STRATA_METAL_ENGINE_KERNELS_DEFAULT) + "/mtp_chain.metal"}); + dout_ = A(4 * MAXT); dprob_ = A(4 * MAXT); posv_ = A(4 * N); + for (int i = 0; i < N; ++i) posv_.as()[i] = (float) i; + dmap_ = A(4 * std::max(1, dvocab_.size())); + if (!dvocab_.empty()) std::memcpy(dmap_.as(), dvocab_.data(), 4 * dvocab_.size()); + // parity: a few tokens' embeddings, GPU vs the host's dequant (the per-step path's) + const gguf::Tensor& te = T("token_embd.weight"); + const size_t rb = ref::row_bytes(te.type, N); + const int32_t probe[MAXT] = {0, 1, 271, 760, 6511, 11751, 100000, (int32_t) n_vocab_ - 1}; + double worst = 0.0; + std::vector ref_row(N); + for (int k = 0; k < MAXT; ++k) { + dout_.as()[0] = probe[k]; + std::vector ds; + nb_ = false; + mtl::Dispatch d; + d.p = P(lchain_, "mtp_embed_" + std::to_string(ty), {}, "mtp_embed_" + std::to_string(ty)); + struct EmbArgs { uint32_t n, row_bytes, j; } ea{(uint32_t) N, (uint32_t) rb, 0}; + d.args = {mtl::Arg::B(W_, te.offset), mtl::Arg::B(dout_), mtl::Arg::B(posv_), mtl::Arg::B(memb_), mtl::Arg::V(ea)}; + d.tg[0] = 256; + push(ds, d); + cx_->run_graph(ds); + ref::dequant(te.type, (const uint8_t*) f_.map + te.offset + (size_t) probe[k] * rb, ref_row.data(), N); + for (int i = 0; i < N; ++i) worst = std::max(worst, (double) std::fabs(ref_row[i] - memb_.as()[i])); + } + std::fprintf(stderr, "strata metal: MTP draft round in one command buffer (embedding type %d on the GPU: max " + "|GPU - host| %.3g over %d rows)\n", ty, worst, MAXT); + if (worst > 1e-6) { + std::fprintf(stderr, "strata metal: the GPU embedding differs from the host's: a command buffer per step\n"); + chain_ = false; + } + } + } std::fprintf(stderr, "strata metal: MTP draft layer from %s (%.2f GB, %zu norms folded), draft head over %s tokens\n", gguf_path.c_str(), fm_.file_bytes / 1e9, mnorm_off_.size(), dvocab_.empty() ? "all" : std::to_string(dvocab_.size()).c_str()); @@ -920,15 +969,17 @@ void Window::gr_read_m(std::vector& ds, const std::string& pre, m } void Window::mtp_encode(std::vector& ds, const mtl::Buffer& srcR, size_t src_off, int n, const int32_t* tokens, - int64_t cell0, bool full) { + int64_t cell0, bool full, int erow, int slot) { struct RmsArgs { uint32_t n; float eps; }; struct XpArgs { uint32_t n, n_tok, plane_tok, stride; }; struct CombArgs { uint32_t n, hc, n_tok, h_stride; }; // ---- the embedding rows (host tokens: dequantized on the CPU into the shared buffer) const gguf::Tensor& te = T("token_embd.weight"); const size_t rb = ref::row_bytes(te.type, N); - for (int i = 0; i < n; ++i) - ref::dequant(te.type, (const uint8_t*) f_.map + te.offset + (size_t) tokens[i] * rb, memb_.as() + (size_t) i * N, N); + if (tokens) + for (int i = 0; i < n; ++i) + ref::dequant(te.type, (const uint8_t*) f_.map + te.offset + (size_t) tokens[i] * rb, + memb_.as() + (size_t) (erow + i) * N, N); const std::string L = "mtp.layers.0."; auto rms = [&](const mtl::Buffer& x, size_t xo, const std::string& w, mtl::Buffer& y, int width, int rows) { mtl::Dispatch d; @@ -967,7 +1018,7 @@ void Window::mtp_encode(std::vector& ds, const mtl::Buffer& srcR, push(ds, d); }; // ---- e = fc_embedding(rms(embed(tok)) * (1 + w)), h = fc_hidden per stream (rms_10240(R) * (1 + w)), R = h + e - rms(memb_, 0, "mtp.pre_fc_norm_embedding.weight", men_, N, n); + rms(memb_, (size_t) erow * N * 4, "mtp.pre_fc_norm_embedding.weight", men_, N, n); xp(men_, 0, XEn_, N, n, N); gm("mtp.fc_embedding.weight", XEn_, me2_, N, n); rms(srcR, src_off, "mtp.pre_fc_norm_hidden.weight", mhn_, HC * N, n); @@ -1011,13 +1062,13 @@ void Window::mtp_encode(std::vector& ds, const mtl::Buffer& srcR, if (!full) return; // ---- dense attention over the window of cells up to this one (the draft layer's rule), the output gate, o_proj { - int* ids = mqids_.as(); + int* ids = mqids_.as() + (size_t) slot * 2051; const int64_t first = std::max(0, cell0 - 2050); for (int64_t c = first; c <= cell0; ++c) ids[c - first] = (int) c; mtl::Dispatch at; at.p = P(lqsa_, "qsa_attn", {}, "qsa_attn"); at.args = {mtl::Arg::B(qcur_), mtl::Arg::B(mkq_[0]), mtl::Arg::B(mvq_[0]), mtl::Arg::B(mksc_[0]), mtl::Arg::B(mvsc_[0]), - mtl::Arg::B(mqids_), mtl::Arg::B(part_), mtl::Arg::V(qa)}; + mtl::Arg::B(mqids_, (size_t) slot * 2051 * 4), mtl::Arg::B(part_), mtl::Arg::V(qa)}; at.grid[0] = 33; at.grid[1] = 2; at.grid[2] = 1; at.tg[0] = 256; push(ds, at); mtl::Dispatch m; @@ -1127,20 +1178,64 @@ bool Window::mtp_prefill(const int32_t* next_tokens, int64_t n, int64_t cell0, s return true; } +void Window::mtp_pick(std::vector& ds, int j, bool embed) { + struct PickArgs { uint32_t nv, j, use_map; }; + const int64_t nv = dvocab_.empty() ? n_vocab_ : (int64_t) dvocab_.size(); + mtl::Dispatch d; + d.p = P(lchain_, "mtp_pick", {}, "mtp_pick"); + PickArgs pa{(uint32_t) nv, (uint32_t) j, dvocab_.empty() ? 0u : 1u}; + d.args = {mtl::Arg::B(mlm_), mtl::Arg::B(tokout_), mtl::Arg::B(dmap_), mtl::Arg::B(dout_), mtl::Arg::B(dprob_), + mtl::Arg::V(pa)}; + d.tg[0] = 1024; + push(ds, d); + if (!embed) return; + const gguf::Tensor& te = T("token_embd.weight"); + struct EmbArgs { uint32_t n, row_bytes, j; } ea{(uint32_t) N, (uint32_t) ref::row_bytes(te.type, N), (uint32_t) j}; + mtl::Dispatch e; + const std::string fn = "mtp_embed_" + std::to_string(te.type); + e.p = P(lchain_, fn, {}, fn); + e.args = {mtl::Arg::B(W_, te.offset), mtl::Arg::B(dout_), mtl::Arg::B(posv_), mtl::Arg::B(memb_), mtl::Arg::V(ea)}; + e.tg[0] = 256; + push(ds, e); +} + // One round (mtp.hpp's draft()): the window's rows 0..a-1 caught up (K/V), then the full layer on row a (draft 0) and -// on its own output for the next drafts; each step's pick read by the host (its embedding is the next step's input). +// on its own output for the next drafts. By default the whole round is one command buffer (the pick, its +// probability and the next step's embedding on the GPU); STRATA_METAL_MTP_CHAIN=0: a command buffer per step, the +// host picking and dequantizing. The embedding rows: the catch-up's at memb_ rows 0..a-1, draft 0's at row a. bool Window::mtp_draft(int Tn, const int32_t* tokens, int64_t p, int a, int max_steps, int32_t* drafts, float* probs, float min_p, int* n_drafts, std::string& err) { if (!mtp_on_) { err = "metal mtp: not loaded"; return false; } (void) Tn; int n = 0; int32_t tok = tokens[a]; + if (chain_ && max_steps > 0) { + std::vector ds; + nb_ = false; + if (a > 0) mtp_encode(ds, R_, 0, a, tokens, p, false); + mtp_encode(ds, R_, (size_t) a * HC * N * 4, 1, &tok, p + a, true, a, 0); + mtp_pick(ds, 0, max_steps > 1); + for (int j = 1; j < max_steps; ++j) { + mtp_encode(ds, mR_, 0, 1, nullptr, p + a + j, true, 0, j); + mtp_pick(ds, j, j + 1 < max_steps); + } + cx_->run_graph(ds); + for (int j = 0; j < max_steps; ++j) { + const float pj = dprob_.as()[j]; + drafts[j] = dout_.as()[j]; + if (probs) probs[j] = pj; + ++n; + if (min_p > 0.0f && pj < min_p) break; + } + if (n_drafts) *n_drafts = n; + return true; + } for (int j = 0; j < max_steps; ++j) { std::vector ds; nb_ = false; if (j == 0) { if (a > 0) mtp_encode(ds, R_, 0, a, tokens, p, false); - mtp_encode(ds, R_, (size_t) a * HC * N * 4, 1, &tok, p + a, true); + mtp_encode(ds, R_, (size_t) a * HC * N * 4, 1, &tok, p + a, true, a, 0); } else { mtp_encode(ds, mR_, 0, 1, &tok, p + a + j, true); }