diff --git a/metal/CMakeLists.txt b/metal/CMakeLists.txt index 0cac9492..42978519 100644 --- a/metal/CMakeLists.txt +++ b/metal/CMakeLists.txt @@ -115,7 +115,8 @@ add_library(strata_metal_kernels STATIC ${STRATA_METAL_ENGINE_SOURCES}) target_include_directories(strata_metal_kernels PRIVATE src ${STRATA_ROOT}/third_party/ggml) target_compile_definitions(strata_metal_kernels PRIVATE STRATA_GGML_COMMON_DEFAULT="${GGML_COMMON}" STRATA_METAL_KERNELS_DEFAULT="${CMAKE_CURRENT_SOURCE_DIR}/kernels" - STRATA_METAL_ENGINE_KERNELS_DEFAULT="${CMAKE_CURRENT_SOURCE_DIR}/src/engine/msl") + STRATA_METAL_ENGINE_KERNELS_DEFAULT="${CMAKE_CURRENT_SOURCE_DIR}/src/engine/msl" + STRATA_METAL_DATA_DEFAULT="${STRATA_ROOT}/data") target_link_libraries(strata_metal_kernels PUBLIC strata_metal_host mtl_rt) add_executable(strata ${S}/program/generate.cpp) diff --git a/metal/src/engine/msl/mtp.metal b/metal/src/engine/msl/mtp.metal new file mode 100644 index 00000000..7159330f --- /dev/null +++ b/metal/src/engine/msl/mtp.metal @@ -0,0 +1,68 @@ +// metal/src/engine/msl/mtp.metal - the small steps of the MTP draft layer's input branches (include/strata/core/mtp.hpp) +// around mac-kern's kernels (GEMVs, the hyper-connection read, QSA, MoE, the head): +// +// mtp_rms y[r] = x[r] * rsqrt(mean(x[r]^2) + eps) * w rows of width n (w: (1 + w) folded by the host) +// mtp_xprep xprep (units.metal) of rows read with a stride: the prepared activation of one stream of rows +// mtp_combine R[t][c][d] = h[c][t][d] + e[t][d] the streams of fc_hidden plus fc_embedding +// +// Prepared activations: [4 kinds][n_tok][n] then S16 [n_tok][n / 16] (units.metal's xprep layout). + +#include +using namespace metal; + +struct RmsArgs { uint n; float eps; }; + +// grid (rows), 256 threads +kernel void mtp_rms(device const float* x [[buffer(0)]], device const float* w [[buffer(1)]], device float* y [[buffer(2)]], + constant RmsArgs& a [[buffer(3)]], uint3 tg [[threadgroup_position_in_grid]], + ushort tid [[thread_index_in_threadgroup]], ushort sg [[simdgroup_index_in_threadgroup]], + ushort lane [[thread_index_in_simdgroup]]) { + threadgroup float part[8]; + device const float* xr = x + tg.x * a.n; + float ss = 0.0f; + for (uint d = tid; d < a.n; d += 256) ss = fma(xr[d], xr[d], ss); + ss = simd_sum(ss); + if (lane == 0) part[sg] = ss; + threadgroup_barrier(mem_flags::mem_threadgroup); + float tot = 0.0f; + for (int i = 0; i < 8; ++i) tot += part[i]; + const float rs = rsqrt(tot / float(a.n) + a.eps); + device float* yr = y + tg.x * a.n; + for (uint d = tid; d < a.n; d += 256) yr[d] = xr[d] * rs * w[d]; +} + +struct XpArgs { uint n; uint n_tok; uint plane_tok; uint stride; }; + +// one thread per 16 values of one row: row t at x + t * stride; out planes of plane_tok rows +kernel void mtp_xprep(device const float* x [[buffer(0)]], device float* xp [[buffer(1)]], constant XpArgs& a [[buffer(2)]], + uint gid [[thread_position_in_grid]]) { + const uint per = a.n / 16; + if (gid >= per * a.n_tok) return; + const uint t = gid / per, i = gid % per; + device const float* src = x + t * a.stride + 16 * i; + const uint plane = a.plane_tok * a.n; + device float* raw = xp + t * a.n + 16 * i; + float v[16]; + for (int j = 0; j < 16; ++j) v[j] = src[j]; + float s0 = 0, s1 = 0; + for (int j = 0; j < 16; j += 2) { s0 += v[j]; s1 += v[j + 1]; } + const float q2s[8] = {1.0f, 0.25f, 0.0625f, 0.015625f, 0.00390625f, 0.0009765625f, 0.000244140625f, 0.00006103515625f}; + const float p4s[4] = {1.0f, 0.00390625f, 1.52587890625e-05f, 5.9604644775390625e-08f}; + for (int j = 0; j < 16; ++j) { + raw[j] = v[j]; + raw[plane + j] = v[j] * q2s[j & 7]; + raw[2 * plane + j] = v[j] * p4s[j & 3]; + raw[3 * plane + j] = (j & 1) ? v[j] * 0.00390625f : v[j]; + } + xp[4 * plane + t * per + i] = s0 + s1; +} + +struct CombArgs { uint n; uint hc; uint n_tok; uint h_stride; }; // h: stream c's rows at h + c * h_stride + +kernel void mtp_combine(device const float* h [[buffer(0)]], device const float* e [[buffer(1)]], device float* R [[buffer(2)]], + constant CombArgs& a [[buffer(3)]], uint gid [[thread_position_in_grid]]) { + const uint per = a.hc * a.n; + if (gid >= a.n_tok * per) return; + const uint t = gid / per, c = (gid % per) / a.n, d = gid % a.n; + R[gid] = h[c * a.h_stride + t * a.n + d] + e[t * a.n + d]; +} diff --git a/metal/src/engine/msl/ple.metal b/metal/src/engine/msl/ple.metal new file mode 100644 index 00000000..7036eaeb --- /dev/null +++ b/metal/src/engine/msl/ple.metal @@ -0,0 +1,91 @@ +// metal/src/engine/msl/ple.metal - the PLE block (include/strata/kernels/ple.hpp) after its two projections, for a +// window of T rows, on the GPU (the CUDA engine's ple.cu math; its block sums are double there, float here): +// +// key = grouped_norm(pkey, norm_key) per stream c of 4: x * rsqrt(mean(x^2) + eps) * w +// query = grouped_norm(R, norm_query) +// gate[c] = sigmoid(sign(s) sqrt(max(|s|, 1e-6))), s = key[c] . query[c] / sqrt(n_embd) +// gated = value[d] * gate[c]; normalized = grouped_norm(gated, norm_conv) +// conv[c] = silu(sum_k kW[k + 4c] * row(t - (3 - k) * 3)[c]) rows: the history, then the window's own +// R += gated + conv +// +// History: hist[row + 9 * channel] (row-fastest, oldest first, the committed rows); the window's normalized rows +// are written to norm_w[t][channel] (the host appends the kept ones at commit). + +#include +using namespace metal; + +struct PleArgs { + uint n_embd; // 2560 + uint hc; // 4 + float eps; +}; + +static inline float tg_sum(float v, threadgroup float* part, ushort sg, ushort lane) { + v = simd_sum(v); + if (lane == 0) part[sg] = v; + threadgroup_barrier(mem_flags::mem_threadgroup); + float t = 0.0f; + for (int i = 0; i < 8; ++i) t += part[i]; + threadgroup_barrier(mem_flags::mem_threadgroup); + return t; +} + +// grid (hc, T), 256 threads: y[t][c] = grouped_norm(x[t][c]) with w[c] +kernel void ple_gnorm(device const float* x [[buffer(0)]], device const float* w [[buffer(1)]], device float* y [[buffer(2)]], + constant PleArgs& a [[buffer(3)]], uint3 tg [[threadgroup_position_in_grid]], + ushort tid [[thread_index_in_threadgroup]], ushort sg [[simdgroup_index_in_threadgroup]], + ushort lane [[thread_index_in_simdgroup]]) { + threadgroup float part[8]; + const uint c = tg.x, t = tg.y, n = a.n_embd; + device const float* xc = x + (t * a.hc + c) * n; + float ss = 0.0f; + for (uint d = tid; d < n; d += 256) ss += xc[d] * xc[d]; + const float mean = tg_sum(ss, part, sg, lane) / float(n); + const float scale = 1.0f / sqrt(mean + a.eps); + device float* yc = y + (t * a.hc + c) * n; + for (uint d = tid; d < n; d += 256) yc[d] = xc[d] * scale * w[c * n + d]; +} + +// grid (hc, T), 256 threads: the gate, then gated = value * gate and normalized = grouped_norm(gated, norm_conv) +kernel void ple_gate_norm(device const float* key [[buffer(0)]], device const float* query [[buffer(1)]], + device const float* value [[buffer(2)]], device const float* w_conv [[buffer(3)]], + device float* gated [[buffer(4)]], device float* norm_w [[buffer(5)]], + constant PleArgs& a [[buffer(6)]], uint3 tg [[threadgroup_position_in_grid]], + ushort tid [[thread_index_in_threadgroup]], ushort sg [[simdgroup_index_in_threadgroup]], + ushort lane [[thread_index_in_simdgroup]]) { + threadgroup float part[8]; + const uint c = tg.x, t = tg.y, n = a.n_embd, row = (t * a.hc + c) * n; + float acc = 0.0f; + for (uint d = tid; d < n; d += 256) acc += key[row + d] * query[row + d]; + const float s = tg_sum(acc, part, sg, lane) * (1.0f / sqrt(float(n))); + const float mag = sqrt(max(fabs(s), 1e-6f)); + const float sgn = s > 0.0f ? 1.0f : (s < 0.0f ? -1.0f : 0.0f); + const float gate = 1.0f / (1.0f + exp(-(sgn * mag))); + float ss = 0.0f; + for (uint d = tid; d < n; d += 256) { + const float g = value[t * n + d] * gate; + gated[row + d] = g; + ss += g * g; + } + const float mean = tg_sum(ss, part, sg, lane) / float(n); + const float scale = 1.0f / sqrt(mean + a.eps); + for (uint d = tid; d < n; d += 256) norm_w[row + d] = gated[row + d] * scale * w_conv[c * n + d]; +} + +// grid (hc * n_embd / 256, T), 256 threads: the conv over [history | the window's rows], SiLU, and the residual +kernel void ple_conv_add(device const float* hist [[buffer(0)]], device const float* norm_w [[buffer(1)]], + device const half* kw [[buffer(2)]], device const float* gated [[buffer(3)]], + device float* R [[buffer(4)]], constant PleArgs& a [[buffer(5)]], + uint3 tg [[threadgroup_position_in_grid]], ushort tid [[thread_index_in_threadgroup]]) { + const uint ch = tg.x * 256 + tid, t = tg.y, hcd = a.hc * a.n_embd; + if (ch >= hcd) return; + float acc = 0.0f; + for (int k = 0; k < 4; ++k) { + const int p = int(t) - (3 - k) * 3; // the row this tap reads: < 0 = the committed history + const float v = p >= 0 ? norm_w[uint(p) * hcd + ch] : hist[uint(9 + p) + 9 * ch]; + acc += float(kw[k + 4 * ch]) * v; + } + const float conv = acc / (1.0f + exp(-acc)); + device float* r = R + t * hcd; + r[ch] = r[ch] + gated[t * hcd + ch] + conv; +} diff --git a/metal/src/engine/setup_host.cpp b/metal/src/engine/setup_host.cpp index 8dff0031..e4599092 100644 --- a/metal/src/engine/setup_host.cpp +++ b/metal/src/engine/setup_host.cpp @@ -156,4 +156,5 @@ void native_q6_k_unpack(const void*) {} // nothing packed on Metal namespace strata::prefill { Gemm::~Gemm() {} // nothing created on Metal (no prompt GEMMs yet) +bool prompt_f16() { return false; } // the CUDA/HIP prompt path's FP16 image mode: not used on Metal } // namespace strata::prefill diff --git a/metal/src/engine/stubs.cpp b/metal/src/engine/stubs.cpp index 3e6d62dc..54b78eb3 100644 --- a/metal/src/engine/stubs.cpp +++ b/metal/src/engine/stubs.cpp @@ -132,7 +132,6 @@ strata::core::RemoteExpertOpt::~RemoteExpertOpt() { strata::metal_engine::missin namespace strata::kernels { void add_inplace(float * p0, const float * p1, int64_t p2, void * p3) { strata::metal_engine::missing("strata::kernels::add_inplace"); } } namespace strata::kernels { void add_streams_broadcast(const float * p0, const float * p1, float * p2, int64_t p3, int p4, int p5, void * p6) { strata::metal_engine::missing("strata::kernels::add_streams_broadcast"); } } namespace strata::kernels { void argmax_rows(const float * p0, int p1, int p2, void * p3, int32_t * p4, void * p5) { strata::metal_engine::missing("strata::kernels::argmax_rows"); } } -namespace strata::kernels { uint64_t argmax_rows_scratch_bytes(int p0) { strata::metal_engine::missing("strata::kernels::argmax_rows_scratch_bytes"); return {}; } } namespace strata::kernels { bool argmax_rows_wanted() { strata::metal_engine::missing("strata::kernels::argmax_rows_wanted"); return {}; } } namespace strata::kernels { void bf16_gemv(const uint16_t * p0, const uint16_t * p1, float * p2, int64_t p3, int64_t p4, void * p5) { strata::metal_engine::missing("strata::kernels::bf16_gemv"); } } namespace strata::kernels { void bf16_gemv_fp32_mmvf(const float * p0, const uint16_t * p1, float * p2, int64_t p3, int64_t p4, void * p5) { strata::metal_engine::missing("strata::kernels::bf16_gemv_fp32_mmvf"); } } @@ -203,7 +202,6 @@ namespace strata::kernels { void fused_gr_read(const FusedGrArgs & p0, void * p1 namespace strata::kernels { bool fused_gr_read_multi(const FusedGrArgs * p0, int p1, float * p2, void * p3, unsigned long long * p4, int p5) { strata::metal_engine::missing("strata::kernels::fused_gr_read_multi"); return {}; } } namespace strata::kernels { bool fused_gr_supported(int64_t p0, int64_t p1, int64_t p2) { strata::metal_engine::missing("strata::kernels::fused_gr_supported"); return {}; } } namespace strata::kernels { void fwht256_cuda(const float * p0, float * p1, int64_t p2, void * p3) { strata::metal_engine::missing("strata::kernels::fwht256_cuda"); } } -namespace strata::kernels { void gather_rows(const uint8_t * p0, int64_t p1, const int32_t * p2, int64_t p3, uint8_t * p4, void * p5) { strata::metal_engine::missing("strata::kernels::gather_rows"); } } namespace strata::kernels { void gdn_ab_multi(const float * p0, const uint16_t * p1, const uint16_t * p2, const float * p3, const float * p4, float * p5, float * p6, int p7, int p8, int p9, void * p10) { strata::metal_engine::missing("strata::kernels::gdn_ab_multi"); } } namespace strata::kernels { void gdn_beta_gate(float * p0, int64_t p1, void * p2) { strata::metal_engine::missing("strata::kernels::gdn_beta_gate"); } } namespace strata::kernels { void gdn_conv_commit(float * p0, const float * p1, int p2, const int32_t * p3, void * p4) { strata::metal_engine::missing("strata::kernels::gdn_conv_commit"); } } @@ -314,7 +312,6 @@ namespace strata::kernels { void rope_neox_apply(const float * p0, float * p1, i namespace strata::kernels { void route_prior_top10(const float * p0, const int32_t * p1, float p2, int32_t * p3, float * p4, int p5, void * p6) { strata::metal_engine::missing("strata::kernels::route_prior_top10"); } } namespace strata::kernels { void router_top10(const float * p0, int p1, int p2, int p3, int * p4, float * p5, void * p6) { strata::metal_engine::missing("strata::kernels::router_top10"); } } namespace strata::kernels { void row_top_prob(const float * p0, int p1, int p2, const int32_t * p3, float * p4, void * p5) { strata::metal_engine::missing("strata::kernels::row_top_prob"); } } -namespace strata::kernels { uint64_t row_top_prob_scratch_bytes(int p0) { strata::metal_engine::missing("strata::kernels::row_top_prob_scratch_bytes"); return {}; } } namespace strata::kernels { void row_top_prob_split(const float * p0, int p1, int p2, const int32_t * p3, float * p4, void * p5, void * p6) { strata::metal_engine::missing("strata::kernels::row_top_prob_split"); } } namespace strata::kernels { void s2_gemv_q8(const uint8_t * p0, const uint8_t * p1, const float * p2, float * p3, int64_t p4, int64_t p5, int p6, void * p7) { strata::metal_engine::missing("strata::kernels::s2_gemv_q8"); } } namespace strata::kernels { void s_gemv_q8_0_split(const uint8_t * p0, const uint8_t * p1, const float * p2, const float * p3, float * p4, int64_t p5, int64_t p6, const SForm & p7, void * p8) { strata::metal_engine::missing("strata::kernels::s_gemv_q8_0_split"); } } @@ -333,7 +330,6 @@ namespace strata::kernels { void window_ids(int32_t * p0, int p1, int p2, int32_ auto strata::prefill::Gemm::bf16(const uint16_t * p0, const uint16_t * p1, float * p2, int64_t p3, int64_t p4, int64_t p5, int64_t p6, float p7, int64_t p8) -> void { strata::metal_engine::missing("strata::prefill::Gemm::bf16"); } auto strata::prefill::Gemm::bf16_hcd_exact(const uint16_t * p0, int64_t p1, const uint16_t * p2, float * p3, int64_t p4, int64_t p5, int64_t p6) -> bool { strata::metal_engine::missing("strata::prefill::Gemm::bf16_hcd_exact"); return {}; } auto strata::prefill::Gemm::f16(const uint16_t * p0, const uint16_t * p1, float * p2, int64_t p3, int64_t p4, int64_t p5, int64_t p6, float p7) -> void { strata::metal_engine::missing("strata::prefill::Gemm::f16"); } -auto strata::prefill::Gemm::init_external(void * p0, uint16_t * p1, int64_t p2, void * p3, size_t p4, std::string & p5) -> bool { strata::metal_engine::missing("strata::prefill::Gemm::init_external"); return {}; } auto strata::prefill::Gemm::native(const uint16_t * p0, int p1, const void * p2, float * p3, int64_t p4, int64_t p5, int64_t p6, int64_t p7, float p8, int64_t p9) -> void { strata::metal_engine::missing("strata::prefill::Gemm::native"); } auto strata::prefill::Gemm::rebind(uint16_t * p0, int64_t p1, void * p2, size_t p3) -> void { strata::metal_engine::missing("strata::prefill::Gemm::rebind"); } namespace strata::prefill { void blob_dequant_f16(const uint8_t * p0, uint16_t * p1, uint16_t * p2, void * p3) { strata::metal_engine::missing("strata::prefill::blob_dequant_f16"); } } @@ -361,12 +357,10 @@ namespace strata::prefill { void moe_combine_peer(const float * p0, const int32_ namespace strata::prefill { void moe_combine_peer16(const float * p0, const int32_t * p1, const float * p2, const float * p3, const float * p4, const uint16_t * p5, int64_t p6, float * p7, int64_t p8, void * p9) { strata::metal_engine::missing("strata::prefill::moe_combine_peer16"); } } namespace strata::prefill { void peer_gather_add(float * p0, const float * p1, int64_t p2, const float * p3, const int32_t * p4, const int32_t * p5, const int32_t * p6, const int32_t * p7, int64_t p8, void * p9) { strata::metal_engine::missing("strata::prefill::peer_gather_add"); } } namespace strata::prefill { void peer_scatter_add(float * p0, const float * p1, const float * p2, const int32_t * p3, int64_t p4, void * p5) { strata::metal_engine::missing("strata::prefill::peer_scatter_add"); } } -namespace strata::prefill { bool prompt_f16() { strata::metal_engine::missing("strata::prefill::prompt_f16"); return {}; } } namespace strata::prefill { void rms_rows(float * p0, const float * p1, int64_t p2, int64_t p3, int64_t p4, float p5, void * p6) { strata::metal_engine::missing("strata::prefill::rms_rows"); } } namespace strata::prefill { void rope(float * p0, int64_t p1, int64_t p2, int64_t p3, int64_t p4, int64_t p5, const strata::kernels::RopeScaling & p6, void * p7) { strata::metal_engine::missing("strata::prefill::rope"); } } namespace strata::prefill { void round_f16(const float * p0, float * p1, int64_t p2, void * p3) { strata::metal_engine::missing("strata::prefill::round_f16"); } } namespace strata::prefill { void route(const float * p0, int32_t * p1, float * p2, int64_t p3, int64_t p4, void * p5) { strata::metal_engine::missing("strata::prefill::route"); } } -namespace strata::prefill { void set_act_f16(bool p0) { strata::metal_engine::missing("strata::prefill::set_act_f16"); } } namespace strata::prefill { void split_q(const float * p0, float * p1, int64_t p2, void * p3) { strata::metal_engine::missing("strata::prefill::split_q"); } } namespace strata::prefill { void sums_to_f16(const float * p0, uint16_t * p1, int64_t p2, void * p3) { strata::metal_engine::missing("strata::prefill::sums_to_f16"); } } namespace strata::prefill { void swiglu_interleaved(const float * p0, uint16_t * p1, int64_t p2, void * p3) { strata::metal_engine::missing("strata::prefill::swiglu_interleaved"); } } diff --git a/metal/src/engine/window.cpp b/metal/src/engine/window.cpp index 762d485f..0eb64653 100644 --- a/metal/src/engine/window.cpp +++ b/metal/src/engine/window.cpp @@ -25,10 +25,12 @@ #include #include #include +#include #include #include #include #include +#include #include namespace strata::metal_engine { @@ -75,8 +77,17 @@ public: int64_t max_cells, std::string& err); bool run(int T, const int32_t* tokens, int64_t pos0, bool head, int32_t* out, std::string& err); bool commit(int n_keep, std::string& err); + bool finish(int Tn, const int32_t* tokens, int64_t pos0, bool head, int32_t* out, double t0); bool copy_logits(int t, float* host) const; int64_t vocab() const { return n_vocab_; } + bool ready() const { return cx_ != nullptr; } + void set_max_t(int t) { max_t_ = std::min(t, MAXT); } + // ---- the MTP draft layer (mtp.hpp's math on mac-kern's kernels; the MTP GGUF from tools/mtp_pack.py) + bool mtp_load(const std::string& gguf, int max_t, std::string& err); + bool mtp_prefill(const int32_t* next_tokens, int64_t n, int64_t cell0, std::string& err); + bool mtp_draft(int T, 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); + bool mtp_loaded() const { return mtp_on_; } private: const gguf::Tensor& T(const std::string& name) const { @@ -95,17 +106,33 @@ private: ds.push_back(std::move(d)); } void gemv(std::vector& ds, const gguf::Tensor& t, const Act& x, const mtl::Buffer& y, size_t y_stride, - int ntok, size_t y_off = 0); + int ntok, size_t y_off = 0, const mtl::Buffer* wb = nullptr); void xprep(std::vector& ds, const mtl::Buffer& x, const Act& out, int n); void gr_read(std::vector& ds, const std::string& pre, bool apply, const Act& out, bool final_, int ntok); 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_ + 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); + 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 { + const gguf::Tensor* x = fm_.find(name); + if (!x) { std::fprintf(stderr, "strata metal: the MTP model has no tensor %s\n", name.c_str()); std::exit(2); } + return *x; + } + size_t NM(const std::string& name) const { // the folded (1 + w) copy of an F32 norm of the MTP model + auto it = mnorm_off_.find(name); + if (it == mnorm_off_.end()) { std::fprintf(stderr, "strata metal: no MTP norm %s\n", name.c_str()); std::exit(2); } + return it->second; + } strata::core::SessionState* ss_ = nullptr; gguf::File f_; std::unique_ptr cx_; mtl::Buffer W_; - int lib_ = -1, lgdn_ = -1, lqsa_ = -1, lsmp_ = -1; + int lib_ = -1, lgdn_ = -1, lqsa_ = -1, lsmp_ = -1, lple_ = -1; std::map pcache_; bool nb_ = false; int max_t_ = 1; @@ -115,17 +142,42 @@ private: // buffers mtl::Buffer R_, bo_, inj_[2], xn_, lo_, qkv_, z_, al_, be_, yg_, logits_, ids_, wts_, plan_, h_, ye_, gsh_, ush_, ysh_, sg_, qfull_, kcur_, vcur_, iqr_, ikr_, qcur_, qidx_, scores_, qids_, part_, cs_, lm_, apv_, api_, tokout_, - emb_, pkey_, pval_, qkvL_, alL_, beL_, zL_, ydump_; + emb_, pkey_, pval_, qkvL_, alL_, beL_, zL_, ydump_, keyn_, qn_, gated_, normW_, pleHist_; Act XA_, XF_, XY_, XQ_, XH_, XE_; int cur_inj_ = 0; // state std::vector gst_, ghist_[2], kq_, vq_, ksc_, vsc_, tail_, dead_, pooled_; - std::vector hpar_; // per GDN layer: which history buffer holds the committed history + int hp_ = 0; // which of the two history buffers holds the committed conv history (all layers) + // the encoded window per (T, head, hp_): built once, then only the positions are patched (the host's encode cost) + struct Cached { + std::vector ds; + std::vector> qsa; // (dispatch, kind): 0 QsaArgs n_tok = T, 1 the merge (MAXT), 2 the scores + }; + std::map wcache_; + std::vector>* rec_ = nullptr; std::vector ple_hist_; // NG_HIST x hc_dim normalized rows, row-fastest bool ple_on_ = false; int last_T_ = 0; + double last_gpu_ms_ = 0; + int last_n_disp_ = 0; int32_t last_tokens_[MAXT] = {}; std::vector last_norm_; // the window's normalized PLE rows (T x hc_dim), for commit + // the Q8_0 copies of the hyper-connection matrices (the default; STRATA_HC_Q8=0: the BF16 originals) + bool hc_q8_ = false; + mtl::Buffer Q8_; + std::map q8off_; + // MTP + bool mtp_on_ = false; + int lmtp_ = -1; + gguf::File fm_; + mtl::Buffer Wm_, mnorm_, mR_, memb_, men_, me2_, mhn_, mh2_, midx_, mbo_, minj_[2], mqids_, mlm_; + std::map mnorm_off_; + std::vector mkq_, mvq_, mksc_, mvsc_, mtail_, mdead_, mpooled_; + // the draft head over a token subset (data/draft_vocab.bin, as the CUDA drafter): its rows gathered from the head + mtl::Buffer mhead_; + gguf::Tensor mhead_t_; + std::vector dvocab_; + Act XEn_, XHn_; }; bool Window::init(const strata::core::ModelGeometry& g, strata::core::SessionState& ss, const std::string& shard1, @@ -149,6 +201,7 @@ bool Window::init(const strata::core::ModelGeometry& g, strata::core::SessionSta lgdn_ = cx_->compile({"gdn.metal"}, "#define NO_GGML_COMMON 1\n"); lqsa_ = cx_->compile({"qsa.metal", "qsa2.metal"}, "#define NO_GGML_COMMON 1\n"); lsmp_ = cx_->compile({"sample.metal"}, "#define NO_GGML_COMMON 1\n"); + lple_ = cx_->compile({std::string(STRATA_METAL_ENGINE_KERNELS_DEFAULT) + "/ple.metal"}, "#define NO_GGML_COMMON 1\n"); const int MAXC = (int) std::max(4096, (max_cells + 64) / 4 * 4 + 64), MB = MAXC / 4 + 2; max_cells_ = MAXC; @@ -167,6 +220,8 @@ bool Window::init(const strata::core::ModelGeometry& g, strata::core::SessionSta // the GDN layers' window inputs, kept per layer so commit() can fold the accepted prefix into the state qkvL_ = A(4 * (size_t) 36 * MAXT * 10240); alL_ = A(4 * (size_t) 36 * MAXT * 48); beL_ = A(4 * (size_t) 36 * MAXT * 48); zL_ = A(4 * (size_t) 36 * MAXT * 6144); ydump_ = A(4 * MAXT * 6144); + keyn_ = A(4 * MAXT * 10240); qn_ = A(4 * MAXT * 10240); gated_ = A(4 * MAXT * 10240); normW_ = A(4 * MAXT * 10240); + pleHist_ = A(4 * 9 * 10240); auto act = [&](int n) { Act a; a.n = n; a.b = A(4 * ((size_t) 4 * MAXT * n + (size_t) MAXT * n / 16)); return a; }; XA_ = act(N); XF_ = act(N); XY_ = act(6144); XQ_ = act(6144); XH_ = act(NFF); XE_ = act(N); // RoPE: cos/sin [pos][32] for the first 64 dims, NEOX pairs (i, i + 32), in double as build_rope_table @@ -184,7 +239,6 @@ bool Window::init(const strata::core::ModelGeometry& g, strata::core::SessionSta gst_.push_back(A(4 * 128 * 48 * 128)); ghist_[0].push_back(A(4 * 10240 * 3)); ghist_[1].push_back(A(4 * 10240 * 3)); - hpar_.push_back(0); } else { kq_.push_back(A((size_t) MAXC * 2 * 256)); vq_.push_back(A((size_t) MAXC * 2 * 256)); @@ -195,6 +249,44 @@ bool Window::init(const strata::core::ModelGeometry& g, strata::core::SessionSta pooled_.push_back(A(4 * (size_t) MB * 128)); } } + // ---- the HC read in Q8_0 (the owner's default on Metal, as STRATA_HC_Q8 on gfx12): the BF16 down / up / inject + // matrices quantized once at load (ggml's q8_0 rule, ref_quant), read by gr_down_q8 / gr_up_q8 + { + const char* e = std::getenv("STRATA_HC_Q8"); + hc_q8_ = !(e && std::string(e) == "0"); + } + if (hc_q8_) { + const double tq = now_ms(); + size_t tot = 0; + std::vector names; + for (const auto& kv : f_.tensors) { + const gguf::Tensor& x = kv.second; + if (x.type != ref::BF16 || x.name.find("hc_") == std::string::npos || x.name.find("_norm") != std::string::npos) continue; + uint64_t ne = 1; + for (uint64_t d : x.shape) ne *= d; + q8off_[x.name] = tot; + names.push_back(x.name); + tot += ne / 32 * 34; + } + Q8_ = cx_->alloc((tot + 4095) / 4096 * 4096); + std::vector th; + const unsigned nth = std::max(1u, std::min(16u, std::thread::hardware_concurrency())); + for (unsigned k = 0; k < nth; ++k) + th.emplace_back([&, k] { + std::vector tmp; + for (size_t i = k; i < names.size(); i += nth) { + const gguf::Tensor& x = *f_.find(names[i]); + uint64_t ne = 1; + for (uint64_t d : x.shape) ne *= d; + tmp.resize(ne); + ref::dequant(ref::BF16, (const uint8_t*) f_.map + x.offset, tmp.data(), ne); + ref::quantize_q8_0(tmp.data(), ne, Q8_.as() + q8off_[names[i]]); + } + }); + for (auto& x : th) x.join(); + std::fprintf(stderr, "strata metal: HC read in Q8_0 (STRATA_HC_Q8=0: BF16): %zu matrices, %.0f MB, made in %.0f ms\n", + names.size(), tot / 1e6, now_ms() - tq); + } ple_on_ = ss.ple.ready() && f_.find("blk.1.ple_key.weight") != nullptr; ple_hist_.assign((size_t) strata::kernels::NG_HIST * strata::kernels::NG_HC_DIM, 0.0f); last_norm_.assign((size_t) MAXT * strata::kernels::NG_HC_DIM, 0.0f); @@ -205,7 +297,7 @@ bool Window::init(const strata::core::ModelGeometry& g, strata::core::SessionSta } void Window::gemv(std::vector& ds, const gguf::Tensor& t, const Act& x, const mtl::Buffer& y, - size_t y_stride, int ntok, size_t y_off) { + size_t y_stride, int ntok, size_t y_off, const mtl::Buffer* wb) { const int n_in = (int) t.shape[0], n_out = t.shape.size() > 1 ? (int) t.shape[1] : 1; const int ty = t.type; int L = 16, Rr = 1, S = 4, K = 1; @@ -219,8 +311,8 @@ void Window::gemv(std::vector& ds, const gguf::Tensor& t, const A (uint32_t) y_stride}; mtl::Dispatch d; d.p = p; - d.args = {mtl::Arg::B(W_, t.offset), mtl::Arg::B(x.b, x.plane_bytes() * xkind(ty)), mtl::Arg::B(x.b, x.plane_bytes() * 4), - mtl::Arg::B(y, y_off), mtl::Arg::V(a)}; + d.args = {mtl::Arg::B(wb ? *wb : W_, t.offset), mtl::Arg::B(x.b, x.plane_bytes() * xkind(ty)), + mtl::Arg::B(x.b, x.plane_bytes() * 4), mtl::Arg::B(y, y_off), mtl::Arg::V(a)}; const int rpt = (S / K) * (32 / L) * Rr; d.grid[0] = (n_out + rpt - 1) / rpt; d.tg[0] = S * 32; @@ -253,18 +345,23 @@ void Window::gr_read(std::vector& ds, const std::string& pre, boo d1.grid[1] = ntok; d1.tg[0] = 256; push(ds, d1); + const mtl::Buffer& WB = hc_q8_ ? Q8_ : W_; + const size_t od = hc_q8_ ? q8off_.at(tdn.name) : tdn.offset, oi = hc_q8_ ? q8off_.at(tin.name) : tin.offset, + ou = hc_q8_ ? q8off_.at(tup.name) : tup.offset; mtl::Dispatch d2; - d2.p = P(lib_, "gr_down_8_8", mtl::Constants().i(1, ntok), "gr_down_8_8#" + std::to_string(ntok)); - d2.args = {mtl::Arg::B(W_, tdn.offset), mtl::Arg::B(W_, tin.offset), mtl::Arg::B(xn_), mtl::Arg::B(lo_), + const char* dn = hc_q8_ ? "gr_down_q8_8_8" : "gr_down_8_8"; + d2.p = P(lib_, dn, mtl::Constants().i(1, ntok), std::string(dn) + "#" + std::to_string(ntok)); + 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(ds, d2); mtl::Dispatch d3; - d3.p = P(lib_, "gr_up", mtl::Constants().i(1, ntok), "gr_up#" + std::to_string(ntok)); + const char* up = hc_q8_ ? "gr_up_q8" : "gr_up"; + d3.p = P(lib_, up, mtl::Constants().i(1, ntok), std::string(up) + "#" + std::to_string(ntok)); GrArgs au = a; au.n_tok = MAXT; // the plane stride of the prepared activation - d3.args = {mtl::Arg::B(W_, tup.offset), mtl::Arg::B(lo_), mtl::Arg::B(xn_), mtl::Arg::B(out.b), mtl::Arg::V(au)}; + d3.args = {mtl::Arg::B(WB, ou), mtl::Arg::B(lo_), mtl::Arg::B(xn_), mtl::Arg::B(out.b), mtl::Arg::V(au)}; d3.grid[0] = N / 16; d3.tg[0] = 256; push(ds, d3); @@ -291,7 +388,7 @@ void Window::layer(std::vector& ds, int l, int ntok, int64_t pos0 GdnArgs a{16, 48, 10240, self_commit ? 1u : 0u, eps_, 0}; mtl::Dispatch d; d.p = P(lgdn_, "gdn_step", mtl::Constants().i(2, ntok), "gdn_step#" + std::to_string(ntok)); - const int hp = hpar_[(size_t) gi]; + const int hp = hp_; d.args = {mtl::Arg::B(gst_[(size_t) gi]), mtl::Arg::B(ghist_[hp][(size_t) gi]), mtl::Arg::B(qkvL_, o_qkv), mtl::Arg::B(alL_, o_ab), mtl::Arg::B(beL_, o_ab), mtl::Arg::B(zL_, o_z), mtl::Arg::B(W_, T(b + "ssm_conv1d.weight").offset), @@ -301,7 +398,7 @@ void Window::layer(std::vector& ds, int l, int ntok, int64_t pos0 d.grid[0] = 48; d.tg[0] = 512; push(ds, d); - if (self_commit) hpar_[(size_t) gi] = 1 - hp; // the history ping-pong turns with every commit + (void) self_commit; // the history ping-pong turns after the window (run) or at commit xprep(ds, yg_, XY_, 6144); gemv(ds, T(b + "ssm_out.weight"), XY_, bo_, N, ntok); } else { @@ -329,6 +426,7 @@ void Window::layer(std::vector& ds, int l, int ntok, int64_t pos0 d.grid[1] = ntok; d.tg[0] = 256; push(ds, d); + if (rec_) rec_->push_back({(int) ds.size() - 1, 0}); mtl::Dispatch s; s.p = P(lqsa_, "qsa_score", {}, "qsa_score"); s.args = {mtl::Arg::B(qidx_), mtl::Arg::B(pooled_[(size_t) qi]), mtl::Arg::B(dead_[(size_t) qi]), mtl::Arg::B(scores_), @@ -337,12 +435,14 @@ void Window::layer(std::vector& ds, int l, int ntok, int64_t pos0 s.grid[1] = ntok; s.tg[0] = 256; push(ds, s); + if (rec_) rec_->push_back({(int) ds.size() - 1, 2}); mtl::Dispatch k; k.p = P(lqsa_, "qsa_topk2", {}, "qsa_topk"); k.args = {mtl::Arg::B(scores_), mtl::Arg::B(qids_), mtl::Arg::V(a)}; k.grid[0] = ntok; k.tg[0] = 1024; push(ds, k); + if (rec_) rec_->push_back({(int) ds.size() - 1, 0}); mtl::Dispatch at; at.p = P(lqsa_, "qsa_attn", {}, "qsa_attn"); at.args = {mtl::Arg::B(qcur_), mtl::Arg::B(kq_[(size_t) qi]), mtl::Arg::B(vq_[(size_t) qi]), mtl::Arg::B(ksc_[(size_t) qi]), @@ -352,6 +452,7 @@ void Window::layer(std::vector& ds, int l, int ntok, int64_t pos0 at.grid[2] = ntok; at.tg[0] = 256; push(ds, at); + if (rec_) rec_->push_back({(int) ds.size() - 1, 0}); mtl::Dispatch m; m.p = P(lqsa_, "qsa_merge", {}, "qsa_merge"); QsaArgs am = a; @@ -361,6 +462,7 @@ void Window::layer(std::vector& ds, int l, int ntok, int64_t pos0 m.grid[1] = ntok; m.tg[0] = 256; push(ds, m); + if (rec_) rec_->push_back({(int) ds.size() - 1, 1}); gemv(ds, T(b + "attn_output.weight"), XQ_, bo_, N, ntok); } // ---- the MoE @@ -504,8 +606,7 @@ void Window::ple_cpu(int Tn) { } bool Window::run(int Tn, const int32_t* tokens, int64_t pos0, bool head, int32_t* out, std::string& err) { - (void) head; - if (Tn < 1 || Tn > max_t_) { err = "metal window: window size out of range"; return false; } + if (Tn < 1 || Tn > MAXT || (head && Tn > max_t_)) { err = "metal window: window size out of range"; return false; } if (pos0 + Tn + 1 >= max_cells_) { err = "metal window: the window runs past the context"; return false; } const double t0 = now_ms(); // ---- the embeddings (CPU dequant of the token rows straight into the shared buffer), broadcast to the streams @@ -516,9 +617,7 @@ bool Window::run(int Tn, const int32_t* tokens, int64_t pos0, bool head, int32_t ref::dequant(te.type, (const uint8_t*) f_.map + te.offset + (size_t) tokens[t] * rb, r, N); for (int c = 1; c < HC; ++c) std::memcpy(r + (size_t) c * N, r, (size_t) N * 4); } - // ---- the PLE rows (the table read is the shared host code's), projected on the GPU in the first command buffer - std::vector ds; - nb_ = false; + // ---- the PLE rows (the table read is the shared host code's), projected on the GPU if (ple_on_) { strata::core::SessionState& ss = *ss_; uint32_t rows[MAXT * strata::kernels::PLE_N_HEADS]; @@ -529,6 +628,27 @@ bool Window::run(int Tn, const int32_t* tokens, int64_t pos0, bool head, int32_t prev[1] = tokens[t]; } if (!ss.ple.table->gather_batch(rows, (size_t) Tn, emb_.as(), err)) return false; + } + const int key = (Tn * 2 + (head ? 1 : 0)) * 2 + hp_; + auto hit = wcache_.find(key); + if (hit != wcache_.end()) { + // the same window shape: only the positions change (QSA's cells, the score grid) + Cached& c = hit->second; + for (const auto& [i, kind] : c.qsa) { + mtl::Dispatch& d = c.ds[(size_t) i]; + QsaArgs a{(uint32_t) pos0, 0, (uint32_t) max_cells_, (uint32_t) (kind == 1 ? MAXT : Tn), eps_}; + std::memcpy(d.args.back().bytes.data(), &a, sizeof a); + if (kind == 2) d.grid[0] = (uint32_t) (((pos0 + Tn) / 4 + 1 + 7) / 8); + } + last_gpu_ms_ = 1000.0 * cx_->run_graph(c.ds); + last_n_disp_ = (int) c.ds.size(); + return finish(Tn, tokens, pos0, head, out, t0); + } + Cached& cw = wcache_[key]; + rec_ = &cw.qsa; + std::vector& ds = cw.ds; + nb_ = false; + if (ple_on_) { xprep(ds, emb_, XE_, N); gemv(ds, T("blk.1.ple_key.weight"), XE_, pkey_, 10240, Tn); nb_ = true; @@ -548,16 +668,35 @@ bool Window::run(int Tn, const int32_t* tokens, int64_t pos0, bool head, int32_t d1.grid[1] = Tn; d1.tg[0] = 256; push(ds, d1); - cx_->run_graph(ds); - ple_cpu(Tn); - ds.clear(); - nb_ = false; + // the PLE block on the GPU (msl/ple.metal): norms, gate, conv, the residual update + struct PleArgs { uint32_t n_embd, hc; float eps; } pa{N, HC, strata::kernels::NG_RMS_EPS}; + auto off = [&](const char* n) { return T(n).offset; }; + mtl::Dispatch g1, g2, g3, g4; + g1.p = P(lple_, "ple_gnorm", {}, "ple_gnorm"); + g1.args = {mtl::Arg::B(pkey_), mtl::Arg::B(W_, off("blk.1.ple_norm_key.weight")), mtl::Arg::B(keyn_), mtl::Arg::V(pa)}; + g1.grid[0] = HC; g1.grid[1] = Tn; g1.tg[0] = 256; + push(ds, g1); + g2.p = g1.p; + g2.args = {mtl::Arg::B(R_), mtl::Arg::B(W_, off("blk.1.ple_norm_query.weight")), mtl::Arg::B(qn_), mtl::Arg::V(pa)}; + g2.grid[0] = HC; g2.grid[1] = Tn; g2.tg[0] = 256; + nb_ = true; // beside the key norm + push(ds, g2); + g3.p = P(lple_, "ple_gate_norm", {}, "ple_gate_norm"); + g3.args = {mtl::Arg::B(keyn_), mtl::Arg::B(qn_), mtl::Arg::B(pval_), mtl::Arg::B(W_, off("blk.1.ple_norm_conv.weight")), + mtl::Arg::B(gated_), mtl::Arg::B(normW_), mtl::Arg::V(pa)}; + g3.grid[0] = HC; g3.grid[1] = Tn; g3.tg[0] = 256; + push(ds, g3); + g4.p = P(lple_, "ple_conv_add", {}, "ple_conv_add"); + g4.args = {mtl::Arg::B(pleHist_), mtl::Arg::B(normW_), mtl::Arg::B(W_, off("blk.1.ple_conv1d.weight")), + mtl::Arg::B(gated_), mtl::Arg::B(R_), mtl::Arg::V(pa)}; + g4.grid[0] = HC * N / 256; g4.grid[1] = Tn; g4.tg[0] = 256; + push(ds, g4); } for (int l = 1; l < 48; ++l) layer(ds, l, Tn, pos0); // ---- the final mixer and the head gr_read(ds, "output_hc", true, XA_, true, Tn); - gemv(ds, T("output.weight"), XA_, lm_, (size_t) n_vocab_, Tn); - { + if (head) gemv(ds, T("output.weight"), XA_, lm_, (size_t) n_vocab_, Tn); + if (head) { ArgArgs a{(uint32_t) n_vocab_, 120}; mtl::Dispatch d1, d2; d1.p = P(lsmp_, "argmax_part", {}, "argmax_part"); @@ -572,12 +711,19 @@ bool Window::run(int Tn, const int32_t* tokens, int64_t pos0, bool head, int32_t d2.tg[0] = 256; push(ds, d2); } - cx_->run_graph(ds); - for (int t = 0; t < Tn; ++t) out[t] = tokout_.as()[t]; + rec_ = nullptr; + last_gpu_ms_ = 1000.0 * cx_->run_graph(ds); + last_n_disp_ = (int) ds.size(); + return finish(Tn, tokens, pos0, head, out, t0); +} + +bool Window::finish(int Tn, const int32_t* tokens, int64_t pos0, bool head, int32_t* out, double t0) { + if (Tn == 1) hp_ = 1 - hp_; // a one-token window committed its GDN steps + for (int t = 0; t < Tn; ++t) out[t] = head ? tokout_.as()[t] : -1; last_T_ = Tn; for (int t = 0; t < Tn; ++t) last_tokens_[t] = tokens[t]; static const bool trace = std::getenv("STRATA_METAL_TRACE") != nullptr; - if (trace) { + if (trace && head) { // the pick and its margin over the runner-up (a near tie says a backend difference may flip it) const float* lg = lm_.as(); int i1 = -1, i2 = -1; @@ -585,8 +731,8 @@ bool Window::run(int Tn, const int32_t* tokens, int64_t pos0, bool head, int32_t if (i1 < 0 || lg[i] > lg[i1]) { i2 = i1; i1 = i; } else if (i2 < 0 || lg[i] > lg[i2]) i2 = i; } - std::fprintf(stderr, "strata metal: window pos %lld token %d -> %d (top %d %.4f, 2nd %d %.4f, margin %.4f) %.2f ms\n", - (long long) pos0, tokens[0], out[0], i1, lg[i1], i2, lg[i2], lg[i1] - lg[i2], now_ms() - t0); + std::fprintf(stderr, "strata metal: window pos %lld token %d -> %d (top %d %.4f, 2nd %d %.4f, margin %.4f) %.2f ms (GPU %.2f ms, %d dispatches)\n", + (long long) pos0, tokens[0], out[0], i1, lg[i1], i2, lg[i2], lg[i1] - lg[i2], now_ms() - t0, last_gpu_ms_, last_n_disp_); } return true; } @@ -608,7 +754,7 @@ bool Window::commit(int n_keep, std::string& err) { GdnArgs a{16, 48, 10240, 1u, eps_, 0}; mtl::Dispatch d; d.p = P(lgdn_, "gdn_step", mtl::Constants().i(2, n_keep), "gdn_step#" + std::to_string(n_keep)); - const int hp = hpar_[(size_t) gi]; + const int hp = hp_; d.args = {mtl::Arg::B(gst_[(size_t) gi]), mtl::Arg::B(ghist_[hp][(size_t) gi]), mtl::Arg::B(qkvL_, o_qkv), mtl::Arg::B(alL_, o_ab), mtl::Arg::B(beL_, o_ab), mtl::Arg::B(zL_, o_z), mtl::Arg::B(W_, T(b + "ssm_conv1d.weight").offset), mtl::Arg::B(W_, T(b + "ssm_dt.bias").offset), @@ -618,16 +764,16 @@ bool Window::commit(int n_keep, std::string& err) { d.tg[0] = 512; nb_ = true; // the layers' folds are independent of each other push(ds, d); - hpar_[(size_t) gi] = 1 - hp; } cx_->run_graph(ds); + hp_ = 1 - hp_; } strata::core::SessionState& ss = *ss_; for (int t = 0; t < n_keep; ++t) { if (ple_on_) { - const float* norm = last_norm_.data() + (size_t) t * NG_HC_DIM; + const float* norm = normW_.as() + (size_t) t * NG_HC_DIM; for (int c = 0; c < NG_HC_DIM; ++c) { - float* col = ple_hist_.data() + (size_t) c * NG_HIST; + float* col = pleHist_.as() + (size_t) c * NG_HIST; for (int r = 0; r + 1 < NG_HIST; ++r) col[r] = col[r + 1]; col[NG_HIST - 1] = norm[c]; } @@ -638,6 +784,338 @@ bool Window::commit(int n_keep, std::string& err) { return true; } + +// ================================================================================================ the MTP draft layer +bool Window::mtp_load(const std::string& gguf_path, int max_t, std::string& err) { + (void) max_t; + if (!fm_.open(gguf_path, err)) return false; + madvise(fm_.map, fm_.map_bytes, MADV_WILLNEED); + Wm_ = cx_->wrap(fm_.map, fm_.map_bytes); + if (Wm_.handle == nullptr) { err = "metal mtp: cannot wrap " + gguf_path; return false; } + // vLLM's GemmaRMSNorm scales by (1 + w): the F32 norms folded once (tools/mtp_rt.py does the same for CUDA) + size_t tot = 0; + for (const auto& kv : fm_.tensors) + if (kv.second.type == 0) { mnorm_off_[kv.first] = tot; tot += (kv.second.bytes + 255) / 256 * 256; } + mnorm_ = cx_->alloc(tot + 256); + for (const auto& kv : mnorm_off_) { + const gguf::Tensor& x = fm_.tensors.at(kv.first); + const float* src = (const float*) ((const uint8_t*) fm_.map + x.offset); + float* dst = (float*) (mnorm_.as() + kv.second); + for (uint64_t i = 0; i < x.bytes / 4; ++i) dst[i] = src[i] + 1.0f; + } + lmtp_ = cx_->compile({std::string(STRATA_METAL_ENGINE_KERNELS_DEFAULT) + "/mtp.metal"}, "#define NO_GGML_COMMON 1\n"); + auto A = [&](size_t bytes) { return cx_->alloc(bytes); }; + const int MAXC = (int) max_cells_, MB = MAXC / 4 + 2; + mR_ = A(4 * MAXT * HC * N); memb_ = A(4 * MAXT * N); men_ = A(4 * MAXT * N); me2_ = A(4 * MAXT * N); + mhn_ = A(4 * MAXT * HC * N); mh2_ = A(4 * HC * MAXT * N); midx_ = A(4 * MAXT * 640); mbo_ = A(4 * MAXT * N); + minj_[0] = A(4 * MAXT * HC); minj_[1] = A(4 * MAXT * HC); mqids_ = A(4 * MAXT * 2051); mlm_ = A(4 * (size_t) n_vocab_); + auto act = [&](int n) { Act x; x.n = n; x.b = A(4 * ((size_t) 4 * MAXT * n + (size_t) MAXT * n / 16)); return x; }; + XEn_ = act(N); XHn_ = act(N); + mkq_.push_back(A((size_t) MAXC * 2 * 256)); mvq_.push_back(A((size_t) MAXC * 2 * 256)); + mksc_.push_back(A(2 * (size_t) MAXC * 2 * 4)); mvsc_.push_back(A(2 * (size_t) MAXC * 2 * 4)); + mtail_.push_back(A(4 * 4 * 128)); mdead_.push_back(A(4 * 128)); mpooled_.push_back(A(4 * (size_t) MB * 128)); + { + std::string vp; + if (const char* e = std::getenv("STRATA_METAL_DRAFT_VOCAB")) vp = e; + else vp = std::string(STRATA_METAL_DATA_DEFAULT) + "/draft_vocab.bin"; + FILE* f = vp == "full" ? nullptr : std::fopen(vp.c_str(), "rb"); + if (f) { + int32_t id; + while (std::fread(&id, 4, 1, f) == 1) if (id >= 0 && id < n_vocab_) dvocab_.push_back(id); + std::fclose(f); + } + if (!dvocab_.empty()) { + const gguf::Tensor& h = T("output.weight"); + const size_t rb = ref::row_bytes(h.type, N); + mhead_ = A(dvocab_.size() * rb + 4096); + for (size_t i = 0; i < dvocab_.size(); ++i) + std::memcpy(mhead_.as() + i * rb, (const uint8_t*) f_.map + h.offset + (size_t) dvocab_[i] * rb, rb); + mhead_t_ = h; + mhead_t_.offset = 0; + mhead_t_.shape = {(uint64_t) N, (uint64_t) dvocab_.size()}; + mhead_t_.bytes = dvocab_.size() * rb; + } + } + mtp_on_ = true; + 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()); + return true; +} + +void Window::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& tdn = TM(pre + ".input_mix_weight_down.weight"); + const gguf::Tensor& tup = TM(pre + ".input_mix_weight_up.weight"); + const gguf::Tensor& tin = final_ ? tdn : TM(pre + ".block_inject_weight.weight"); + GrArgs a{N, HC, LR, (uint32_t) ntok, apply ? 1u : 0u, eps_}; + mtl::Dispatch d1; + d1.p = P(lib_, "gr_norm", {}, "gr_norm"); + d1.args = {mtl::Arg::B(Rb), mtl::Arg::B(bo), mtl::Arg::B(inj_in), mtl::Arg::B(mnorm_, NM(pre + ".hc_norm.weight")), + mtl::Arg::B(xn_), mtl::Arg::V(a)}; + d1.grid[0] = HC; d1.grid[1] = ntok; d1.tg[0] = 256; + push(ds, d1); + mtl::Dispatch d2; + d2.p = P(lib_, "gr_down_8_8", mtl::Constants().i(1, ntok), "gr_down_8_8#" + std::to_string(ntok)); + d2.args = {mtl::Arg::B(Wm_, tdn.offset), mtl::Arg::B(Wm_, tin.offset), mtl::Arg::B(xn_), mtl::Arg::B(lo_), + mtl::Arg::B(inj_out), mtl::Arg::V(a)}; + d2.grid[0] = LR + HC; d2.tg[0] = 256; + push(ds, d2); + mtl::Dispatch d3; + d3.p = P(lib_, "gr_up", mtl::Constants().i(1, ntok), "gr_up#" + std::to_string(ntok)); + GrArgs au = a; + au.n_tok = MAXT; + d3.args = {mtl::Arg::B(Wm_, tup.offset), mtl::Arg::B(lo_), mtl::Arg::B(xn_), mtl::Arg::B(out.b), mtl::Arg::V(au)}; + d3.grid[0] = N / 16; d3.tg[0] = 256; + push(ds, d3); +} + +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) { + 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); + 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; + d.p = P(lmtp_, "mtp_rms", {}, "mtp_rms"); + RmsArgs a{(uint32_t) width, eps_}; + d.args = {mtl::Arg::B(x, xo), mtl::Arg::B(mnorm_, NM(w)), mtl::Arg::B(y), mtl::Arg::V(a)}; + d.grid[0] = rows; d.tg[0] = 256; + push(ds, d); + }; + auto xp = [&](const mtl::Buffer& x, size_t xo, const Act& out, int width, int rows, int stride) { + mtl::Dispatch d; + d.p = P(lmtp_, "mtp_xprep", {}, "mtp_xprep"); + XpArgs a{(uint32_t) width, (uint32_t) rows, MAXT, (uint32_t) stride}; + d.args = {mtl::Arg::B(x, xo), mtl::Arg::B(out.b), mtl::Arg::V(a)}; + d.grid[0] = (width / 16 * rows + 255) / 256; d.tg[0] = 256; + push(ds, d); + }; + auto gm = [&](const std::string& name, const Act& x, const mtl::Buffer& y, size_t y_stride, int rows, size_t y_off = 0) { + // a GEMV on the MTP model's tensor (the window's gemv() reads the main model's buffer) + const gguf::Tensor& tt = TM(name); + const int n_in = (int) tt.shape[0], n_out = tt.shape.size() > 1 ? (int) tt.shape[1] : 1, ty = tt.type; + int Lq = 16, Rr = 1, S = 4, K = 1; + if (n_in >= 6144) { Lq = 32; Rr = 1; S = 8; K = 2; } + else if (n_in == 640) { Lq = 8; Rr = 1; S = 4; K = 1; } + else if (n_out <= 640 || ty == ref::Q2_0 || ty == ref::BF16) { Lq = 32; Rr = 1; S = 4; K = 1; } + const std::string fn = "gemv_" + std::to_string(ty) + "_" + std::to_string(Lq) + "_" + std::to_string(Rr) + "_" + + std::to_string(S) + "_" + std::to_string(K); + 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, + (uint32_t) y_stride}; + mtl::Dispatch d; + d.p = P(lib_, fn, mtl::Constants().i(0, rows), fn + "#" + std::to_string(rows)); + d.args = {mtl::Arg::B(Wm_, tt.offset), mtl::Arg::B(x.b, x.plane_bytes() * xkind(ty)), mtl::Arg::B(x.b, x.plane_bytes() * 4), + mtl::Arg::B(y, y_off), mtl::Arg::V(a)}; + const int rpt = (S / K) * (32 / Lq) * Rr; + d.grid[0] = (n_out + rpt - 1) / rpt; d.tg[0] = S * 32; + 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); + 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); + for (int c = 0; c < HC; ++c) { + xp(mhn_, (size_t) c * N * 4, XHn_, N, n, HC * N); + gm("mtp.fc_hidden.weight", XHn_, mh2_, N, n, (size_t) c * MAXT * N * 4); + } + { + mtl::Dispatch d; + d.p = P(lmtp_, "mtp_combine", {}, "mtp_combine"); + CombArgs a{N, HC, (uint32_t) n, (uint32_t) (MAXT * N)}; + d.args = {mtl::Arg::B(mh2_), mtl::Arg::B(me2_), mtl::Arg::B(mR_), mtl::Arg::V(a)}; + d.grid[0] = (n * HC * N + 255) / 256; d.tg[0] = 256; + push(ds, d); + } + // ---- the attention hyper-connection, K/V (and the indexer keys) into the layer's own cache + gr_read_m(ds, L + "attn_hyper_connection", mR_, mbo_, minj_[1], minj_[0], false, XA_, false, n); + gm(L + "self_attn.k_proj.weight", XA_, kcur_, 512, n); + nb_ = true; + gm(L + "self_attn.v_proj.weight", XA_, vcur_, 512, n); + nb_ = true; + gm(L + "self_attn.indexer.index_qk_proj.weight", XA_, midx_, 640, n); + if (full) { + nb_ = true; + gm(L + "self_attn.q_proj.weight", XA_, qfull_, 12288, n); + } + QsaArgs qa{(uint32_t) cell0, 0, (uint32_t) max_cells_, (uint32_t) n, eps_}; + { + mtl::Dispatch d; + d.p = P(lqsa_, "qsa_prep", {}, "qsa_prep"); + d.args = {mtl::Arg::B(qfull_), mtl::Arg::B(kcur_), mtl::Arg::B(vcur_), mtl::Arg::B(midx_), mtl::Arg::B(midx_, 512 * 4), + mtl::Arg::B(mnorm_, NM(L + "self_attn.q_norm.weight")), mtl::Arg::B(mnorm_, NM(L + "self_attn.k_norm.weight")), + mtl::Arg::B(mnorm_, NM(L + "self_attn.indexer.q_layernorm.weight")), + mtl::Arg::B(mnorm_, NM(L + "self_attn.indexer.k_layernorm.weight")), + mtl::Arg::B(cs_), mtl::Arg::B(qcur_), mtl::Arg::B(qidx_), mtl::Arg::B(mkq_[0]), mtl::Arg::B(mvq_[0]), + mtl::Arg::B(mksc_[0]), mtl::Arg::B(mvsc_[0]), mtl::Arg::B(mtail_[0]), mtl::Arg::B(mdead_[0]), + mtl::Arg::B(mpooled_[0]), mtl::Arg::V(qa)}; + d.grid[0] = 33; d.grid[1] = n; d.tg[0] = 256; + push(ds, d); + } + 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(); + 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)}; + at.grid[0] = 33; at.grid[1] = 2; at.grid[2] = 1; at.tg[0] = 256; + push(ds, at); + mtl::Dispatch m; + m.p = P(lqsa_, "qsa_merge", {}, "qsa_merge"); + QsaArgs am = qa; + am.n_tok = MAXT; + m.args = {mtl::Arg::B(part_), mtl::Arg::B(qfull_), mtl::Arg::B(XQ_.b), mtl::Arg::V(am)}; + m.grid[0] = 24; m.grid[1] = 1; m.tg[0] = 256; + push(ds, m); + gm(L + "self_attn.o_proj.weight", XQ_, mbo_, N, 1); + } + // ---- the MLP hyper-connection (the attention write folded in), the MoE, the write + final mixer, the head + gr_read_m(ds, L + "mlp_hyper_connection", mR_, mbo_, minj_[0], minj_[1], true, XF_, false, 1); + gm(L + "mlp.gate.weight", XF_, logits_, 512, 1); + nb_ = true; + gm(L + "mlp.shared_expert.gate_proj.weight", XF_, gsh_, NFF, 1); + nb_ = true; + gm(L + "mlp.shared_expert.up_proj.weight", XF_, ush_, NFF, 1); + nb_ = true; + gm(L + "mlp.shared_expert_gate.weight", XF_, sg_, 1, 1); + { + MoeArgs a{}; + const gguf::Tensor &tgu = TM(L + "mlp.experts.gate_up_proj"), &td = TM(L + "mlp.experts.down_proj"); + a.gu_row = (uint32_t) ref::row_bytes(tgu.type, N); + a.d_row = (uint32_t) ref::row_bytes(td.type, NFF); + a.gate_off = tgu.offset; + a.up_off = tgu.offset + (uint64_t) NFF * a.gu_row; // [gate rows | up rows] per expert + a.down_off = td.offset; + a.gu_stride = (uint64_t) a.gu_row * 2 * NFF; + a.d_stride = (uint64_t) a.d_row * N; + a.n_embd = N; a.n_ff = NFF; a.n_tok = 1; a.topk = TOPK; a.n_expert = NEXP; + const bool dq2 = td.type == ref::Q2_0; + const int gl = 32, gr = 1, gs = 4, dl = dq2 ? 8 : 4, dr = dq2 ? 1 : 2, ds_ = 4; + a.gx_gu = NFF / (gs * (32 / gl) * gr); + a.gx_dn = N / (ds_ * (32 / dl) * dr); + mtl::Dispatch r; + r.p = P(lib_, "moe_route", {}, "moe_route"); + r.args = {mtl::Arg::B(logits_), mtl::Arg::B(ids_), mtl::Arg::B(wts_), mtl::Arg::B(plan_), mtl::Arg::V(a)}; + r.tg[0] = 512; + push(ds, r); + { + GlueArgs ga{NFF, MAXT, N, TOPK}; + mtl::Dispatch d; + d.p = P(lib_, "swiglu_prep", {}, "swiglu_prep"); + d.args = {mtl::Arg::B(gsh_), mtl::Arg::B(ush_), mtl::Arg::B(XH_.b), mtl::Arg::V(ga)}; + d.grid[0] = NFF / 16; d.grid[1] = 1; d.tg[0] = 16; + nb_ = true; + push(ds, d); + } + const std::string gun = "moe_gu_" + std::to_string(tgu.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 = P(lib_, gun, {}, gun); + g.args = {mtl::Arg::B(Wm_), mtl::Arg::B(XF_.b, XF_.plane_bytes() * xkind(tgu.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] = 32 * gs; + g.indirect = &plan_; + g.ind_off = P_IGU * 4; + push(ds, g); + nb_ = true; + gm(L + "mlp.shared_expert.down_proj.weight", XH_, ysh_, N, 1); + mtl::Dispatch dd; + dd.p = P(lib_, dnn, {}, dnn); + dd.args = {mtl::Arg::B(Wm_), mtl::Arg::B(h_), mtl::Arg::B(ye_), mtl::Arg::B(plan_), mtl::Arg::V(a), mtl::Arg::B(h_)}; + dd.tg[0] = 32 * ds_; + dd.indirect = &plan_; + dd.ind_off = P_IDN * 4; + push(ds, dd); + GlueArgs ca{N, 1, N, TOPK}; + mtl::Dispatch cb; + cb.p = P(lib_, "moe_combine_sh", {}, "moe_combine_sh"); + cb.args = {mtl::Arg::B(ye_), mtl::Arg::B(plan_), mtl::Arg::B(ysh_), mtl::Arg::B(sg_), mtl::Arg::B(mbo_), mtl::Arg::V(ca)}; + cb.grid[0] = (N + 255) / 256; cb.tg[0] = 256; + push(ds, cb); + } + // the layer's write (R updated in place: the next draft step's input residual) and the final mixer + gr_read_m(ds, "mtp.hyper_connection_mixer", mR_, mbo_, minj_[1], minj_[0], true, XA_, true, 1); + const int64_t nv = dvocab_.empty() ? n_vocab_ : (int64_t) dvocab_.size(); + if (dvocab_.empty()) gemv(ds, T("output.weight"), XA_, mlm_, (size_t) n_vocab_, 1); + else gemv(ds, mhead_t_, XA_, mlm_, (size_t) nv, 1, 0, &mhead_); + { + ArgArgs a{(uint32_t) nv, 120}; + mtl::Dispatch d1, d2; + d1.p = P(lsmp_, "argmax_part", {}, "argmax_part"); + d1.args = {mtl::Arg::B(mlm_), mtl::Arg::B(apv_), mtl::Arg::B(api_), mtl::Arg::V(a)}; + d1.grid[0] = 120; d1.grid[1] = 1; d1.tg[0] = 256; + push(ds, d1); + d2.p = P(lsmp_, "argmax_final", {}, "argmax_final"); + d2.args = {mtl::Arg::B(apv_), mtl::Arg::B(api_), mtl::Arg::B(tokout_), mtl::Arg::V(a)}; + d2.grid[0] = 1; d2.tg[0] = 256; + push(ds, d2); + } +} + +// the prompt's K/V: cell cell0 + i pairs the main window's final residual row i with the token at cell0 + i + 1 +bool Window::mtp_prefill(const int32_t* next_tokens, int64_t n, int64_t cell0, std::string& err) { + if (!mtp_on_) { err = "metal mtp: not loaded"; return false; } + for (int64_t c = 0; c < n; c += MAXT) { + const int k = (int) std::min(MAXT, n - c); + std::vector ds; + nb_ = false; + mtp_encode(ds, R_, (size_t) c * HC * N * 4, k, next_tokens + c, cell0 + c, false); + cx_->run_graph(ds); + } + return true; +} + +// 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). +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]; + 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); + } else { + mtp_encode(ds, mR_, 0, 1, &tok, p + a + j, true); + } + cx_->run_graph(ds); + const int32_t di = tokout_.as()[0]; + const int32_t d = dvocab_.empty() ? di : dvocab_[(size_t) di]; + // the draft's probability: softmax of the draft head's logits at the pick + const float* lg = mlm_.as(); + const float mx = lg[di]; + const int64_t nv = dvocab_.empty() ? n_vocab_ : (int64_t) dvocab_.size(); + double s = 0.0; + for (int64_t i = 0; i < nv; ++i) s += std::exp((double) (lg[i] - mx)); + const float pj = (float) (1.0 / s); + drafts[j] = d; + if (probs) probs[j] = pj; + tok = d; + ++n; + if (min_p > 0.0f && pj < min_p) break; + } + if (n_drafts) *n_drafts = n; + return true; +} + bool Window::copy_logits(int t, float* host) const { if (t < 0 || t >= last_T_) return false; std::memcpy(host, lm_.as() + (size_t) t * n_vocab_, (size_t) n_vocab_ * 4); @@ -648,21 +1126,97 @@ bool Window::copy_logits(int t, float* host) const { namespace { std::string g_shard1; } +void window_register(void* w); +bool mtp_load_pending(Window* w, std::string& err); void set_model_shard(const char* shard1) { g_shard1 = shard1 ? shard1 : ""; } -void* window_create() { return new Window(); } -void window_destroy(void* w) { delete (Window*) w; } +// One decode window per process: the prompt path (Prefill::init) may bring it up before the Verifier does. +namespace { +Window* g_the_window = nullptr; +} +void* window_create() { + if (g_the_window == nullptr) g_the_window = new Window(); + return g_the_window; +} +void window_destroy(void* w) { + if (w != nullptr && w == g_the_window) { + delete g_the_window; + g_the_window = nullptr; + window_register(nullptr); + } +} bool window_init(void* w, const strata::core::ModelGeometry& g, strata::core::SessionState& ss, int max_t, int64_t max_cells, int64_t* n_vocab, std::string& err) { - if (g_shard1.empty()) { err = "metal window: the model shard is not set (--native SHARD1)"; return false; } - if (!((Window*) w)->init(g, ss, g_shard1, max_t, max_cells, err)) return false; - *n_vocab = ((Window*) w)->vocab(); + Window* x = (Window*) w; + if (!x->ready()) { + if (g_shard1.empty()) { err = "metal window: the model shard is not set (--native SHARD1)"; return false; } + if (!x->init(g, ss, g_shard1, MAXT, max_cells, err)) return false; + window_register(w); + if (!mtp_load_pending(x, err)) return false; + } + x->set_max_t(max_t); + *n_vocab = x->vocab(); return true; } +bool window_ensure(const strata::core::ModelGeometry& g, strata::core::SessionState& ss, int64_t max_cells, std::string& err) { + int64_t nv = 0; + return window_init(window_create(), g, ss, MAXT, max_cells, &nv, err); +} bool window_run(void* w, int T, const int32_t* tokens, int64_t pos0, bool head, int32_t* out, std::string& err) { return ((Window*) w)->run(T, tokens, pos0, head, out, err); } bool window_commit(void* w, int n_keep, std::string& err) { return ((Window*) w)->commit(n_keep, err); } bool window_copy_logits(void* w, int t, float* host) { return ((Window*) w)->copy_logits(t, host); } +// the MTP hook (src/core/mtp.cpp under STRATA_GPU_METAL): the drafter lives beside the decode window it reads +namespace { +Window* g_window = nullptr; +} +void window_register(void* w) { g_window = (Window*) w; } +// the engine loads the drafter before the decode window exists: the path is kept and the window loads it at its init +std::string g_mtp_path; +int g_mtp_max_t = 0; +bool mtp_load(const std::string& rt_dir, int max_t, std::string& err) { + (void) err; + // the MTP model in GGUF form (tools/mtp_pack.py) beside the rt directory: STRATA_METAL_MTP_GGUF, else + // /../mtp-q2_0.gguf + std::string path; + if (const char* e = std::getenv("STRATA_METAL_MTP_GGUF")) path = e; + else { + std::string d = rt_dir; + while (!d.empty() && d.back() == '/') d.pop_back(); + const size_t s = d.rfind('/'); + path = (s == std::string::npos ? std::string(".") : d.substr(0, s)) + "/mtp-q2_0.gguf"; + } + g_mtp_path = path; + g_mtp_max_t = max_t; + return true; +} +bool mtp_load_pending(Window* w, std::string& err) { + return g_mtp_path.empty() || w->mtp_load(g_mtp_path, g_mtp_max_t, err); +} +// Prefill::run's Metal side: the prompt read through the window, MAXT rows at a time (no head), each window +// committed; on_chunk (the MTP's K/V) after each, with the window's final residual rows in place. +bool prompt_run(const int64_t* tokens, int64_t n, int64_t pos0, + const std::function& on_chunk, std::string& err) { + if (g_window == nullptr) { err = "metal prompt: the decode window is not up yet"; return false; } + int32_t tk[MAXT], out[MAXT]; + for (int64_t c = 0; c < n; c += MAXT) { + const int k = (int) std::min(MAXT, n - c); + for (int i = 0; i < k; ++i) tk[i] = (int32_t) tokens[c + i]; + if (!g_window->run(k, tk, pos0 + c, false, out, err) || !g_window->commit(k, err)) return false; + if (on_chunk && !on_chunk(nullptr, k, pos0 + c, err)) return false; + } + return true; +} +bool mtp_prefill(const int32_t* next_tokens, int64_t n, int64_t cell0, std::string& err) { + if (g_window == nullptr) { err = "metal mtp: no decode window"; return false; } + return g_window->mtp_prefill(next_tokens, n, cell0, err); +} +bool mtp_draft(int T, 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 (g_window == nullptr) { err = "metal mtp: no decode window"; return false; } + return g_window->mtp_draft(T, tokens, p, a, max_steps, drafts, probs, min_p, n_drafts, err); +} + } // namespace strata::metal_engine diff --git a/src/core/mtp.cpp b/src/core/mtp.cpp index 0c26e54c..d9639a98 100644 --- a/src/core/mtp.cpp +++ b/src/core/mtp.cpp @@ -50,6 +50,17 @@ #include "ggml.h" // --mtp-q4: the head rows' and projections' formats, dequantized and requantized at load #endif +#if defined(STRATA_GPU_METAL) +// The Metal backend's MTP drafter (metal/src/engine/window.cpp, beside the decode window it reads): load, prefill and +// draft hand over to it; bind has nothing to bind there. +namespace strata::metal_engine { +bool mtp_load(const std::string& rt_dir, int max_t, std::string& err); +bool mtp_prefill(const int32_t* next_tokens, int64_t n, int64_t cell0, std::string& err); +bool mtp_draft(int T, 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); +} // namespace strata::metal_engine +#endif + namespace strata::core { namespace { @@ -243,6 +254,13 @@ bool MtpDrafter::make_q4_head(std::string& err) { bool MtpDrafter::load(const std::string& rt_dir, const ModelGeometry& g, SessionState& ss, int max_t, std::string& err, int64_t window, const MtpDrafter* shared) { +#if defined(STRATA_GPU_METAL) + (void) window; (void) shared; + g_ = &g; + ss_ = &ss; + max_t_ = max_t; + return strata::metal_engine::mtp_load(rt_dir, max_t, err); +#endif cudaGetDevice(&device_); // a layer split's last stage on another GPU: the drafter lives there g_ = &g; ss_ = &ss; @@ -609,6 +627,10 @@ void draft_head_hint(int64_t n_tokens, int64_t row_bytes) { bool MtpDrafter::bind(const WeightTable& wt, const NativeHead* head, const float* window_R, std::string& err, const MtpDrafter* shared) { +#if defined(STRATA_GPU_METAL) + (void) wt; (void) head; (void) window_R; (void) err; (void) shared; + return true; +#endif const OnDevice on_device(device_); wt_ = &wt; head_ = head; @@ -1174,6 +1196,10 @@ bool MtpDrafter::prepare_prefill(std::string& err) { bool MtpDrafter::prefill(const float* R_rows, const int32_t* next_tokens, int64_t n, int64_t cell0, std::string& err, bool sync) { +#if defined(STRATA_GPU_METAL) + (void) R_rows; (void) sync; // the rows are the decode window's own (the Metal drafter reads them in place) + return strata::metal_engine::mtp_prefill(next_tokens, n, cell0, err); +#endif const OnDevice on_device(device_); const Clock::time_point t0 = Clock::now(); const int64_t HCN = g_->hc * g_->n_embd; @@ -1282,6 +1308,20 @@ bool MtpDrafter::prefill(const float* R_rows, const int32_t* next_tokens, int64_ bool MtpDrafter::draft(int T, const int32_t* tokens, int64_t p, int a, int32_t* drafts, std::string& err, float* probs, float min_p, int* n_drafts) { +#if defined(STRATA_GPU_METAL) + { + if (T < 1 || T > max_t_ || a < 0 || a >= T) { err = "mtp: draft arguments out of range"; return false; } + const int max_steps = (int) std::min(std::min(max_t_ - 1, max_drafts_), (int64_t) 1 << 30); + const Clock::time_point t0 = Clock::now(); + int n = 0; + if (!strata::metal_engine::mtp_draft(T, tokens, p, a, max_steps, drafts, probs, min_p, &n, err)) return false; + for (int j = n; j < max_t_ - 1; ++j) { drafts[j] = 0; if (probs) probs[j] = 0.0f; } + if (n_drafts) *n_drafts = n; + ms_draft += ms_since(t0); + ++rounds; + return true; + } +#endif const OnDevice on_device(device_); if (T < 1 || T > max_t_ || a < 0 || a >= T) { err = "mtp: draft arguments out of range"; return false; } if (chain_live_) { err = "mtp: a pipelined chain is still in flight"; return false; } @@ -1527,6 +1567,13 @@ int MtpDrafter::chain_poll(std::string& err) { bool MtpDrafter::draft_first(int T, const float* R_row, int32_t token, int64_t cell, int32_t* drafts, std::string& err, float* probs, float min_p, int* n_drafts) { +#if defined(STRATA_GPU_METAL) + { // the decode window's row 0 is the residual (the window that just ran) + (void) R_row; + std::vector toks((size_t) T, token); + return draft(1, toks.data(), cell, 0, drafts, err, probs, min_p, n_drafts); + } +#endif const OnDevice on_device(device_); // row 0 is the real pair; rows 1.. repeat it and only write cells the next round overwrites const int64_t HCN = g_->hc * g_->n_embd; diff --git a/src/prefill/prefill.cpp b/src/prefill/prefill.cpp index 6c867253..2bd9da17 100644 --- a/src/prefill/prefill.cpp +++ b/src/prefill/prefill.cpp @@ -96,6 +96,16 @@ void experts_native(const Batch&, const NativeGeom&, int, int64_t, const void*, } // namespace strata::prefill::fused #endif +#if defined(STRATA_GPU_METAL) +// The Metal backend reads a prompt through its decode window (metal/src/engine/window.cpp): init keeps the chunk, run +// hands over, draft_kv declines (the MTP hook reads the window's rows itself). +namespace strata::metal_engine { +bool window_ensure(const strata::core::ModelGeometry& g, strata::core::SessionState& ss, int64_t max_cells, std::string& err); +bool prompt_run(const int64_t* tokens, int64_t n, int64_t pos0, + const std::function& on_chunk, std::string& err); +} // namespace strata::metal_engine +#endif + namespace strata::prefill { namespace { @@ -1062,6 +1072,12 @@ bool Prefill::init(const core::WeightTable& wt, const core::ModelGeometry& g, co core::ExpertSource* src, const core::ExpertCache* cache, const int32_t* host_res, int64_t chunk, void* stream, std::string& err, void* borrow, uint64_t borrow_bytes) { Impl& m = *impl_; +#if defined(STRATA_GPU_METAL) + (void) wt; (void) src; (void) cache; (void) host_res; (void) stream; (void) borrow; + (void) borrow_bytes; + m.T = chunk > 0 ? chunk : 1; + return strata::metal_engine::window_ensure(g, ss, ss.qsa_states[ss.qsa_primary()].max_cells, err); +#endif m.wt = &wt; m.g = &g; m.ss = &ss; m.src = src; m.cache = cache; m.host_res = host_res; m.T = chunk; m.cs = (cudaStream_t) stream; m.stats = &stats_; if (g.n_embd != N || g.hc != HC || g.hc_lr != LR || g.n_expert < 1 || ss.k != K) { @@ -1347,6 +1363,11 @@ int64_t Prefill::chunk() const { return impl_->T; } bool Prefill::draft_kv(core::MtpDrafter& mtp, const float* R_rows, const int32_t* next_tokens, int64_t n, int64_t cell0, std::string& err, const char** why) { Impl& m = *impl_; +#if defined(STRATA_GPU_METAL) + (void) m; (void) mtp; (void) R_rows; (void) next_tokens; (void) n; (void) cell0; (void) err; + if (why) *why = "the Metal window feeds the draft layer itself"; + return false; +#endif const core::OnDevice on_device(m.device); // the stage's own GPU current (a layer split's drafter: the last stage) auto decline = [&](const char* reason) { if (why != nullptr) *why = reason; return false; }; static const bool off = [] { const char* v = std::getenv("STRATA_MTP_BATCH"); return v != nullptr && v[0] == '0'; }(); @@ -4242,6 +4263,16 @@ bool Prefill::drain_pipeline(std::string& err) { } bool Prefill::run(const int64_t* tokens, int64_t n, int64_t pos0, std::string& err) { +#if defined(STRATA_GPU_METAL) + { + const auto t0 = std::chrono::steady_clock::now(); + const bool ok = strata::metal_engine::prompt_run(tokens, n, pos0, on_chunk, err); + stats_.tokens += n; + stats_.chunks += 1; + stats_.ms_total += std::chrono::duration(std::chrono::steady_clock::now() - t0).count(); + return ok; + } +#endif const bool body_ok = run_impl(tokens, n, pos0, err); std::string drain_err; diff --git a/src/program/generate.cpp b/src/program/generate.cpp index 22ea0d14..a601fbcc 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -2918,12 +2918,8 @@ int main(int argc, char** argv) { } strata::core::NativeEmbed native_embed; if (native_pack) { -#if defined(STRATA_GPU_METAL) // Metal: the prompt is read through the decode windows (no batched prompt path yet) - if (o.native_preset.empty() || o.spec < 2 || o.keep_canonical) { -#else if (o.native_preset.empty() || o.spec < 2 || o.keep_canonical || (o.prefill_chunk <= 0 && o.tokens.size() > 1)) { -#endif std::fprintf(stderr, "strata generate: %s is a native (IQ) pack: it needs --native SHARD1, --spec T (T >= 2) " "and --prefill CHUNK\n", o.pack.c_str()); return 2; @@ -12903,11 +12899,7 @@ int main(int argc, char** argv) { // round emits (accepted drafts + 1) tokens. `commit` keeps the state of the tokens that were emitted. const bool ended = o.stop_eos && !produced.empty() && std::find(o.eos_ids.begin(), o.eos_ids.end(), (int64_t) produced.back()) != o.eos_ids.end(); -#if defined(STRATA_GPU_METAL) // Metal: also from position 0 (the prompt is read through the windows below) - if ((spec_pos > 0 || (native_pack && o.prefill_chunk <= 0)) && (int64_t) produced.size() < o.max_new && !ended) { -#else if (spec_pos > 0 && (int64_t) produced.size() < o.max_new && !ended) { -#endif auto read_ids = [](const std::string& path, std::vector& ids) { std::ifstream in(path); std::string text((std::istreambuf_iterator(in)), std::istreambuf_iterator()); @@ -13104,27 +13096,6 @@ int main(int argc, char** argv) { }; int64_t p = spec_pos; int32_t x = (int32_t) tok; -#if defined(STRATA_GPU_METAL) - // Metal (no batched prompt path yet): the prompt's tokens before the last one go through one-token windows, - // each committed; the last one is the first window below, as after the batched path - if (native_pack && o.prefill_chunk <= 0 && spec_pos == 0 && n_prompt > 1) { - const Clock::time_point tpr = Clock::now(); - std::vector po(1); - ver.set_head_sampling(false); - for (int64_t i = 0; i + 1 < n_prompt; ++i) { - const int32_t ti = (int32_t) o.tokens[(size_t) i]; - if (!ver.run(1, &ti, i, &drive_pool_multi, &drive, po.data(), err) || !ver.commit(1, err)) { - std::fprintf(stderr, "strata generate: the prompt window at %lld: %s\n", (long long) i, err.c_str()); - return 1; - } - } - ver.set_head_sampling(true); - p = n_prompt - 1; - x = (int32_t) o.tokens[(size_t) (n_prompt - 1)]; - std::fprintf(stderr, "strata generate: Metal: the prompt's %lld tokens through one-token windows in %.1f ms\n", - (long long) (n_prompt - 1), std::chrono::duration(Clock::now() - tpr).count()); - } -#endif std::vector drafts((size_t) o.spec, 0); std::vector dprob((size_t) o.spec, 1.0f); std::vector window_hist((size_t) o.spec + 1, 0);