| 2985 | } |
| 2986 | |
| 2987 | void Window::mtp_encode(std::vector<mtl::Dispatch>& ds, const mtl::Buffer& srcR, size_t src_off, int n, const int32_t* tokens, |
| 2988 | int64_t cell0, bool full, int erow, int slot) { |
| 2989 | struct RmsArgs { uint32_t n; float eps; }; |
| 2990 | struct XpArgs { uint32_t n, n_tok, plane_tok, stride; }; |
| 2991 | struct CombArgs { uint32_t n, hc, n_tok, h_stride; }; |
| 2992 | // ---- the embedding rows (host tokens: dequantized on the CPU into the shared buffer) |
| 2993 | const gguf::Tensor& te = T("token_embd.weight"); |
| 2994 | const size_t rb = ref::row_bytes(te.type, N); |
| 2995 | if (tokens) |
| 2996 | for (int i = 0; i < n; ++i) |
| 2997 | ref::dequant(te.type, (const uint8_t*) W_.ptr + te.offset + (size_t) tokens[i] * rb, |
| 2998 | (membp_ ? *membp_ : memb_).as<float>() + (size_t) (erow + i) * N, N); |
| 2999 | const std::string L = "mtp.layers.0."; |
| 3000 | auto rms = [&](const mtl::Buffer& x, size_t xo, const std::string& w, mtl::Buffer& y, int width, int rows) { |
| 3001 | mtl::Dispatch d; |
| 3002 | const char* rk = ldb_ && width == geo::N && geo::N == 2560 ? "mtp_rms_b10" : ldb_ && width == geo::HCD && geo::HCD == 10240 ? "mtp_rms_b40" : "mtp_rms"; // (loads batched: same values) |
| 3003 | d.p = P(lmtp_, rk, {}, rk); |
| 3004 | RmsArgs a{(uint32_t) width, eps_}; |
| 3005 | d.args = {mtl::Arg::B(x, xo), mtl::Arg::B(mnorm_, NM(w)), mtl::Arg::B(y), mtl::Arg::V(a)}; |
| 3006 | d.grid[0] = rows; d.tg[0] = 256; |
| 3007 | push(ds, d); |
| 3008 | }; |
| 3009 | auto xp = [&](const mtl::Buffer& x, size_t xo, const Act& out, int width, int rows, int stride) { |
| 3010 | mtl::Dispatch d; |
| 3011 | d.p = P(lmtp_, "mtp_xprep", {}, "mtp_xprep"); |
| 3012 | XpArgs a{(uint32_t) width, (uint32_t) rows, MAXT, (uint32_t) stride}; |
| 3013 | d.args = {mtl::Arg::B(x, xo), mtl::Arg::B(out.b), mtl::Arg::V(a)}; |
| 3014 | d.grid[0] = (width / 16 * rows + 255) / 256; d.tg[0] = 256; |
| 3015 | push(ds, d); |
| 3016 | }; |
| 3017 | auto gm = [&](const std::string& name, const Act& x, const mtl::Buffer& y, size_t y_stride, int rows, size_t y_off = 0) { |
| 3018 | // a GEMV on the MTP model's tensor (the window's gemv() reads the main model's buffer) |
| 3019 | const gguf::Tensor& tt = TQ(name); |
| 3020 | const int n_in = (int) tt.shape[0], n_out = tt.shape.size() > 1 ? (int) tt.shape[1] : 1, ty = tt.type; |
| 3021 | int Lq = 16, Rr = 1, S = 4, K = 1; |
| 3022 | if (n_in >= 6144) { Lq = 32; Rr = 1; S = 8; K = 2; } |
| 3023 | else if (n_in == 640) { Lq = 8; Rr = 1; S = 4; K = 1; } |
| 3024 | else if (n_out <= 640 || ty == ref::Q2_0 || ty == ref::BF16) { Lq = 32; Rr = 1; S = 4; K = 1; } |
| 3025 | const std::string fn = "gemv_" + std::to_string(ty) + "_" + std::to_string(Lq) + "_" + std::to_string(Rr) + "_" + |
| 3026 | std::to_string(S) + "_" + std::to_string(K); |
| 3027 | GemvArgs a{(uint32_t) n_in, (uint32_t) n_out, (uint32_t) rows, (uint32_t) ref::row_bytes(ty, n_in), (uint32_t) n_in, |
| 3028 | (uint32_t) y_stride}; |
| 3029 | mtl::Dispatch d; |
| 3030 | d.p = P(lib_, fn, mtl::Constants().i(0, rows), fn + "#" + std::to_string(rows)); |
| 3031 | d.args = {mtl::Arg::B(MB(name), tt.offset), mtl::Arg::B(x.b, x.plane_bytes() * xkind(ty)), |
| 3032 | mtl::Arg::B(x.b, x.plane_bytes() * 4), mtl::Arg::B(y, y_off), mtl::Arg::V(a)}; |
| 3033 | const int rpt = (S / K) * (32 / Lq) * Rr; |
| 3034 | d.grid[0] = (n_out + rpt - 1) / rpt; d.tg[0] = S * 32; |
| 3035 | push(ds, d); |
| 3036 | }; |
| 3037 | // ---- e = fc_embedding(rms(embed(tok)) * (1 + w)), h = fc_hidden per stream (rms_10240(R) * (1 + w)), R = h + e |
| 3038 | rms(membp_ ? *membp_ : memb_, (size_t) erow * N * 4, "mtp.pre_fc_norm_embedding.weight", men_, N, n); |
| 3039 | xp(men_, 0, XEn_, N, n, N); |
| 3040 | gm("mtp.fc_embedding.weight", XEn_, me2_, N, n); |
| 3041 | rms(srcR, src_off, "mtp.pre_fc_norm_hidden.weight", mhn_, HC * N, n); |
| 3042 | for (int c = 0; c < HC; ++c) { |
| 3043 | xp(mhn_, (size_t) c * N * 4, XHn_, N, n, HC * N); |
| 3044 | gm("mtp.fc_hidden.weight", XHn_, mh2_, N, n, (size_t) c * MAXT * N * 4); |
nothing calls this directly
no test coverage detected