From 5d76056a4a985a34ed287cee1545a88c52831bcf Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Mon, 12 Oct 2026 13:18:11 +0200 Subject: [PATCH] metal: the engine runs on the M5 Max - the shared host loop over a Metal decode window (C3, first greedy answers) The Metal build of `strata` is the shared host code (src/, unchanged except preprocessor-only Metal/arm64 branches) over two Metal-only parts in metal/src/engine: - window.cpp: the decode window, the one backend hook (Verifier::init/run/commit/copy_logits under STRATA_GPU_METAL). It is mac-kern's chain on the model GGUF itself (shard 1 mmapped and wrapped no-copy, every weight read in place: no upload, no expert cache): HC reads, GDN/QSA mixers with their own state (recurrence, conv history ping-pong, int8 K/V, indexer), router + plan, routed and shared experts, combine, the final mixer, the LM head and the greedy pick, encoded as one dependency graph. The embedding is dequantized on the CPU into the shared buffer; the PLE rows come from the shared table reader, their projections run on the GPU and the block's norms / gate / conv on the CPU between two command buffers. One-token windows (--mtp-max-t 1) for now. - host helpers copied from the CUDA kernel files the host code reads while it starts (format tables, sizes, path switches, the RoPE table/config); every other kernel entry point is a generated stub that stops with its name (stubs.cpp, tools/gen_stubs.py, from clang's AST of our headers). Shared host code, all inside #if STRATA_GPU_METAL / __APPLE__ / __aarch64__ (the PC objects do not change): - verify.hpp/.cpp: the hook (an opaque window pointer; init/run/commit/wait_commit/copy_logits hand over) - generate.cpp: no x86 ISA refusal; a native pack without --prefill reads its prompt through one-token windows; the model shard handed to the window - device.cu: the poison fill through the allocation's CPU view; pool_affinity_linux.hpp: a macOS branch (no pinning); expert_layout.cpp: no xgetbv asm on arm64 metal/include/metal_compat: cpuid.h (all zero on arm64), linux/mman.h (no hugetlb/THP: the fallback taken). M5 Max, Q2_0, greedy, "What is the capital of France?": "The capital of France is Paris." - the same 36 first tokens as the HIP engine (R9700, same flags) on that prompt and 39 on a story prompt, then near-tie flips (margins 0.08-0.41 when following the HIP tokens); decode 74-75 tok/s. --- include/strata/core/verify.hpp | 3 + metal/include/metal_compat/cpuid.h | 6 + metal/include/metal_compat/cuda_runtime.h | 1 + metal/include/metal_compat/linux/mman.h | 12 + metal/src/engine/iq_host.cpp | 90 +++ metal/src/engine/missing.cpp | 30 + metal/src/engine/mmvq_host.cpp | 66 +++ metal/src/engine/setup_host.cpp | 159 ++++++ metal/src/engine/stubs.cpp | 410 ++++++++++++++ metal/src/engine/window.cpp | 632 ++++++++++++++++++++++ metal/tools/chat_tokens.py | 23 + metal/tools/gen_stubs.py | 184 +++++++ metal/tools/gguf_list.cpp | 25 + src/core/device.cu | 11 +- src/core/verify.cpp | 38 ++ src/kernels/cpu/expert_layout.cpp | 8 + src/kernels/cpu/pool_affinity_linux.hpp | 18 + src/program/generate.cpp | 37 ++ 18 files changed, 1752 insertions(+), 1 deletion(-) create mode 100644 metal/include/metal_compat/cpuid.h create mode 100644 metal/include/metal_compat/linux/mman.h create mode 100644 metal/src/engine/iq_host.cpp create mode 100644 metal/src/engine/missing.cpp create mode 100644 metal/src/engine/mmvq_host.cpp create mode 100644 metal/src/engine/setup_host.cpp create mode 100644 metal/src/engine/stubs.cpp create mode 100644 metal/src/engine/window.cpp create mode 100644 metal/tools/chat_tokens.py create mode 100644 metal/tools/gen_stubs.py create mode 100644 metal/tools/gguf_list.cpp diff --git a/include/strata/core/verify.hpp b/include/strata/core/verify.hpp index ffaea059..a9dc19db 100644 --- a/include/strata/core/verify.hpp +++ b/include/strata/core/verify.hpp @@ -389,6 +389,9 @@ private: int32_t* d_spec_ = nullptr; ///< device: kVerifyMaxT draft ids, then kVerifyMaxT q rows int hist_len_ = 0; bool head_sampling_ = true; ///< set_head_sampling +#if defined(STRATA_GPU_METAL) + void* metal_ = nullptr; ///< the Metal decode window (metal/src/engine/window.cpp): init/run/commit go there +#endif std::vector logit_bias_host_; float* logit_bias_device_ = nullptr; int device_ = -1; ///< the device `init` ran on: run/commit switch to it (layer split) diff --git a/metal/include/metal_compat/cpuid.h b/metal/include/metal_compat/cpuid.h new file mode 100644 index 00000000..8fd3eb52 --- /dev/null +++ b/metal/include/metal_compat/cpuid.h @@ -0,0 +1,6 @@ +// metal/include/metal_compat/cpuid.h - GCC's on arm64: every leaf reads as zero, so the x86 feature +// probes (src/kernels/cpu/expert_layout.cpp) report no AVX / AVX-512 / VNNI and the engine never picks an x86 kernel. +#pragma once +#define __cpuid(leaf, a, b, c, d) ((void) (leaf), (a) = 0, (b) = 0, (c) = 0, (d) = 0) +#define __cpuid_count(leaf, sub, a, b, c, d) ((void) (leaf), (void) (sub), (a) = 0, (b) = 0, (c) = 0, (d) = 0) +static inline unsigned __get_cpuid_max(unsigned, unsigned* sig) { if (sig) *sig = 0; return 0; } diff --git a/metal/include/metal_compat/cuda_runtime.h b/metal/include/metal_compat/cuda_runtime.h index afd72bd3..eb64243e 100644 --- a/metal/include/metal_compat/cuda_runtime.h +++ b/metal/include/metal_compat/cuda_runtime.h @@ -59,6 +59,7 @@ inline cudaError_t cudaDeviceSynchronize() { return strata::gpu::device_synchron inline cudaError_t cudaMemGetInfo(size_t* f, size_t* t) { return strata::gpu::mem_get_info(f, t); } inline cudaError_t cudaGetDeviceProperties(cudaDeviceProp* p, int d) { return strata::gpu::get_device_properties(p, d); } inline cudaError_t cudaRuntimeGetVersion(int* v) { return strata::gpu::runtime_version(v); } +inline cudaError_t cudaDriverGetVersion(int* v) { return strata::gpu::runtime_version(v); } inline cudaError_t cudaDeviceGetAttribute(int* v, cudaDeviceAttr a, int d) { switch (a) { case cudaDevAttrIntegrated: return strata::gpu::device_integrated(v, d); diff --git a/metal/include/metal_compat/linux/mman.h b/metal/include/metal_compat/linux/mman.h new file mode 100644 index 00000000..5b90ec33 --- /dev/null +++ b/metal/include/metal_compat/linux/mman.h @@ -0,0 +1,12 @@ +// metal/include/metal_compat/linux/mman.h - the Linux mmap flags src/core/pinned.cu asks for, on macOS. There are no +// hugetlb pages and no transparent huge pages: MAP_HUGETLB is spelled as MAP_FIXED at address 0, which macOS refuses +// (the 4 GiB __PAGEZERO), and MADV_HUGEPAGE as an advice madvise refuses, so pinned.cu takes its 4 KB-page fallback +// and reports it, exactly as on a Linux box without a hugetlb pool. +#pragma once +#include +#ifndef MAP_HUGETLB +#define MAP_HUGETLB MAP_FIXED +#endif +#ifndef MADV_HUGEPAGE +#define MADV_HUGEPAGE (-1) +#endif diff --git a/metal/src/engine/iq_host.cpp b/metal/src/engine/iq_host.cpp new file mode 100644 index 00000000..93be603c --- /dev/null +++ b/metal/src/engine/iq_host.cpp @@ -0,0 +1,90 @@ +// metal/src/engine/iq_host.cpp - the host-side helpers of src/kernels/cuda/iq_kernels.cu the shared host code reads +// (format tables, row sizes, the native expert blob layout). The kernels themselves are not ported: on Metal the +// decode window runs mac-kern's chain (metal/src/engine/window.cpp). Copied from iq_kernels.cu; keep in step. +#include "strata/kernels/iq_kernels.hpp" + +#define GGML_COMMON_DECL_CPP +#include "ggml-common.h" + +#include +#include + +namespace strata::kernels { +namespace { +bool is_iq(int t) { + return t == 16 || t == 17 || t == 18 || t == 20 || t == 21 || t == 22 || t == 23 || t == 29 || t == 42 || t == 11 || + t == 12 || t == 13 || t == 7 || t == 6 || t == 2 || t == 3 || t == 8; +} +// values per block of the types the grouped expert kernels take (0 = none): STRATA_GU_FMTS / STRATA_D_FMTS +int gu_qk(int t) { + switch (t) { + case 16: case 17: case 18: case 21: case 22: case 23: case 29: case 12: case 13: return 256; + case 42: return 64; + case 6: case 2: case 3: case 8: return 32; + default: return 0; + } +} +int d_qk(int t) { + switch (t) { + case 23: return 256; + case 42: return 64; + case 20: case 7: case 6: case 2: case 3: case 8: return 32; + default: return 0; + } +} +} // namespace + +bool iq_supported(int t) noexcept { return is_iq(t); } +bool embed_type_supported(int t) noexcept { return is_iq(t) || t == 30; } + +size_t iq_row_bytes(int t, int64_t n) noexcept { + switch (t) { + case 16: return (size_t) (n / 256) * sizeof(block_iq2_xxs); + case 17: return (size_t) (n / 256) * sizeof(block_iq2_xs); + case 18: return (size_t) (n / 256) * sizeof(block_iq3_xxs); + case 20: return (size_t) (n / 32) * sizeof(block_iq4_nl); + case 21: return (size_t) (n / 256) * sizeof(block_iq3_s); + case 22: return (size_t) (n / 256) * sizeof(block_iq2_s); + case 29: return (size_t) (n / 256) * sizeof(block_iq1_m); + case 23: return (size_t) (n / 256) * sizeof(block_iq4_xs); + case 11: return (size_t) (n / 256) * sizeof(block_q3_K); + case 42: return (size_t) (n / 64) * sizeof(block_q2_0); + case 12: return (size_t) (n / 256) * sizeof(block_q4_K); + case 13: return (size_t) (n / 256) * sizeof(block_q5_K); + case 14: return (size_t) (n / 256) * sizeof(block_q6_K); + case 7: return (size_t) (n / 32) * sizeof(block_q5_1); + case 6: return (size_t) (n / 32) * sizeof(block_q5_0); + case 2: return (size_t) (n / 32) * sizeof(block_q4_0); + case 3: return (size_t) (n / 32) * sizeof(block_q4_1); + case 8: return (size_t) (n / 32) * sizeof(block_q8_0); + case 30: return (size_t) n * 2; + default: return 0; + } +} + +bool native_expert_supported(int gu_type, int d_type, int64_t n_embd, int64_t n_ff) noexcept { + const int qg = gu_qk(gu_type), qd = d_qk(d_type); + return qg > 0 && qd > 0 && is_iq(gu_type) && is_iq(d_type) && n_embd % qg == 0 && n_ff % qd == 0 && + n_embd % 256 == 0 && (n_ff * n_embd) % 256 == 0; +} + +NativeExpertLayout native_expert_layout(int gu_type, int d_type, int64_t n_embd, int64_t n_ff) { + NativeExpertLayout L; + L.gu_type = gu_type; + L.d_type = d_type; + L.n_embd = n_embd; + L.n_ff = n_ff; + L.gu_row = iq_row_bytes(gu_type, n_embd); + L.d_row = iq_row_bytes(d_type, n_ff); + L.up_off = (size_t) n_ff * L.gu_row; + L.down_off = 2 * L.up_off; + L.bytes = L.down_off + (size_t) n_embd * L.d_row; + return L; +} + +size_t native_expert_scratch_bytes(int64_t cap, int64_t n_ff) { + const size_t f = (size_t) cap * (size_t) n_ff * sizeof(float); + return 3 * ((f + 255) & ~(size_t) 255) + (((size_t) cap * (size_t) (n_ff / 32) * sizeof(block_q8_1) + 255) & ~(size_t) 255); +} + +} // namespace strata::kernels diff --git a/metal/src/engine/missing.cpp b/metal/src/engine/missing.cpp new file mode 100644 index 00000000..98be7854 --- /dev/null +++ b/metal/src/engine/missing.cpp @@ -0,0 +1,30 @@ +// metal/src/engine/missing.cpp - what a kernel entry point that has no Metal implementation yet does when called. +// +// Default: say which one and stop (exit code 86), so a run never continues on outputs nobody wrote. +// STRATA_METAL_STUBS=log: say it once per entry point and return (outputs untouched) - for listing which entry points a +// code path reaches, never for answers. +#include +#include +#include +#include +#include + +namespace strata::metal_engine { + +void missing(const char* name) { + static const bool log_only = [] { + const char* v = std::getenv("STRATA_METAL_STUBS"); + return v != nullptr && std::string(v) == "log"; + }(); + if (!log_only) { + std::fprintf(stderr, "strata metal: %s has no Metal implementation yet\n", name); + std::fflush(stderr); + std::_Exit(86); + } + static std::mutex mu; + static std::set seen; + std::lock_guard lk(mu); + if (seen.insert(name).second) std::fprintf(stderr, "strata metal: STUB %s\n", name); +} + +} // namespace strata::metal_engine diff --git a/metal/src/engine/mmvq_host.cpp b/metal/src/engine/mmvq_host.cpp new file mode 100644 index 00000000..af39859b --- /dev/null +++ b/metal/src/engine/mmvq_host.cpp @@ -0,0 +1,66 @@ +// metal/src/engine/mmvq_host.cpp - the host-side helpers of src/kernels/cuda/native_mmvq.cu the shared host code reads +// (which GGUF types the dense projections take, their byte sizes). Copied from native_mmvq.cu; keep in step. The +// kernels are not ported: on Metal the decode window runs mac-kern's GEMVs (metal/src/engine/window.cpp). +#include "strata/kernels/iq_kernels.hpp" +#include "strata/kernels/native_mmvq.hpp" + +#include +#include +#include +#include + +namespace strata::kernels { +namespace { +constexpr int Q8K = 32; +constexpr std::size_t kQ81Block = 4 + 32; // half2 ds + int8 qs[32] +void validate_shape(int n_in, int ncols, int block_elems = Q8K) { + if (n_in <= 0 || n_in % block_elems != 0) + throw std::invalid_argument("native MMVQ requires n_in > 0 and divisible by its block element count"); + if (ncols < 1 || ncols > 8) throw std::invalid_argument("native MMVQ requires 1 <= ncols <= 8"); +} +} // namespace + +bool native_mmvq_supported(int ggml_type) noexcept { + return ggml_type == 2 || ggml_type == 6 || ggml_type == 7 || ggml_type == 8 || ggml_type == 11 || + ggml_type == 12 || ggml_type == 13 || ggml_type == 14 || ggml_type == 20 || + ggml_type == 23 || ggml_type == 42 || ggml_type == 16 || ggml_type == 17 || ggml_type == 18 || + ggml_type == 21 || ggml_type == 22 || ggml_type == 29; +} + +std::size_t native_mmvq_weight_bytes(int ggml_type, int n_in, int n_out) { + int block_elems, block_bytes; + switch (ggml_type) { + case 2: block_elems = 32; block_bytes = 18; break; + case 6: block_elems = 32; block_bytes = 22; break; + case 7: block_elems = 32; block_bytes = 24; break; + case 8: block_elems = 32; block_bytes = 34; break; + case 20: block_elems = 32; block_bytes = 18; break; + case 11: block_elems = 256; block_bytes = 110; break; + case 12: block_elems = 256; block_bytes = 144; break; + case 13: block_elems = 256; block_bytes = 176; break; + case 14: block_elems = 256; block_bytes = 210; break; + case 23: block_elems = 256; block_bytes = 136; break; + case 42: block_elems = 64; block_bytes = 18; break; + case 16: case 17: case 18: case 21: case 22: case 29: + block_elems = 256; block_bytes = (int) iq_row_bytes(ggml_type, 256); break; + default: throw std::invalid_argument("unsupported native MMVQ GGML type"); + } + validate_shape(n_in, 1, block_elems); + if (n_out <= 0) throw std::invalid_argument("native MMVQ requires n_out > 0"); + const std::size_t row_bytes = std::size_t(n_in / block_elems) * block_bytes; + if (row_bytes > std::numeric_limits::max() / std::size_t(n_out)) + throw std::length_error("native MMVQ weight byte count overflows size_t"); + return row_bytes * std::size_t(n_out); +} + +std::size_t native_q8_1_bytes(int n_in, int ncols) { + validate_shape(n_in, ncols); + return std::size_t(ncols) * std::size_t(n_in / Q8K) * kQ81Block; +} +std::size_t native_q8_1_il_bytes(int n_in, int ncols) { return native_q8_1_bytes(n_in, ncols) * 2; } +bool native_mmvq_il_supported(int, int, int) { return false; } +bool native_q8_0_packed_eligible(int, int) { return false; } +bool native_q8_0_packed_enabled() { return false; } +bool native_q6_k_packed_enabled() { return false; } + +} // namespace strata::kernels diff --git a/metal/src/engine/setup_host.cpp b/metal/src/engine/setup_host.cpp new file mode 100644 index 00000000..8dff0031 --- /dev/null +++ b/metal/src/engine/setup_host.cpp @@ -0,0 +1,159 @@ +// metal/src/engine/setup_host.cpp - the host-side state and sizes of the CUDA kernel files that the shared host code +// sets and reads while it starts the engine (path switches, scratch sizes, the RoPE table and its config). Copied +// from gr.cu, shared_expert.cu, native_*.cu, ple.cu, qsa_decode_attn.cu, rope.cu, native_rope.cu, +// s2_expert_grouped.cu; keep in step. On Metal none of these steers a kernel: the decode window runs mac-kern's +// chain (metal/src/engine/window.cpp); they keep the host side's allocations and checks as on the PC. +#include "strata/kernels/gr.hpp" +#include "strata/kernels/native_gdn.hpp" +#include "strata/kernels/native_moe.hpp" +#include "strata/kernels/native_qsa.hpp" +#include "strata/kernels/native_qsa_indexer.hpp" +#include "strata/kernels/native_rope.hpp" +#include "strata/kernels/native_router.hpp" +#include "strata/kernels/ngram.hpp" +#include "strata/kernels/ple.hpp" +#include "strata/kernels/qsa_decode_attn.hpp" +#include "strata/kernels/rope.hpp" +#include "strata/kernels/rope_scaling.hpp" +#include "strata/kernels/s2_expert_grouped.hpp" +#include "strata/kernels/shared_expert.hpp" +#include "strata/kernels/fused_gr.hpp" +#include "strata/prefill/gemm.hpp" + +#include +#include +#include +#include + +namespace strata::kernels { + +namespace { +bool g_fp32_activations = false, g_native_mmvf = false, g_sh_native_bf16 = false, g_ple_native_bf16 = false, + g_ple_native_postops = false; +std::atomic g_moe_combine{false}, g_gdn{false}, g_router{false}, g_qsa{false}, g_qsa_indexer{false}, g_rope{false}; +RopeScaling g_rope_scaling; +} // namespace + +// ---- path switches (each .cu file's own flag) +void gr_set_fp32_activations(bool enabled) { g_fp32_activations = enabled; } +void gr_set_native_mmvf(bool enabled) { g_native_mmvf = enabled; } +bool gr_native_mmvf_enabled() { return g_native_mmvf; } +void shared_expert_set_native_bf16(bool enabled) { g_sh_native_bf16 = enabled; } +bool shared_expert_native_bf16_enabled() { return g_sh_native_bf16; } +void ple_set_native_bf16(bool enabled) { g_ple_native_bf16 = enabled; } +bool ple_native_bf16_enabled() { return g_ple_native_bf16; } +void ple_set_native_postops(bool enabled) { g_ple_native_postops = enabled; } +bool ple_native_postops_enabled() { return g_ple_native_postops; } +void native_moe_combine_set_enabled(bool v) { g_moe_combine.store(v, std::memory_order_relaxed); } +bool native_moe_combine_enabled() { return g_moe_combine.load(std::memory_order_relaxed); } +void native_gdn_set_enabled(bool v) { g_gdn.store(v, std::memory_order_relaxed); } +bool native_gdn_enabled() { return g_gdn.load(std::memory_order_relaxed); } +void native_router_set_enabled(bool v) { g_router.store(v, std::memory_order_relaxed); } +bool native_router_enabled() { return g_router.load(std::memory_order_relaxed); } +void native_qsa_set_enabled(bool v) { g_qsa.store(v, std::memory_order_relaxed); } +bool native_qsa_enabled() { return g_qsa.load(std::memory_order_relaxed); } +void native_qsa_indexer_set_enabled(bool v) { g_qsa_indexer.store(v, std::memory_order_relaxed); } +bool native_qsa_indexer_enabled() { return g_qsa_indexer.load(std::memory_order_relaxed); } +void native_rope_set_enabled(bool v) { g_rope.store(v, std::memory_order_relaxed); } +bool native_rope_enabled() { return g_rope.load(std::memory_order_relaxed); } +void fused_gr_check() {} // the CUDA read variants' self-test: no variants on Metal + +// ---- scratch sizes +uint64_t ple_block_scratch_bytes() { + const size_t f = (size_t) (5 * NG_HC_DIM + NG_N_EMBD + NG_HC) * sizeof(float); + const size_t q = (size_t) (NG_N_EMBD / 32) * 34; + const size_t e = (size_t) NG_N_EMBD * sizeof(uint16_t); + return ((f + 15) & ~(size_t) 15) + ((q + 15) & ~(size_t) 15) + e + 256; +} +uint64_t qsa_decode_attn_scratch_floats(int64_t cap, const QsaShapes& s) { + constexpr int HD = 256, CHUNK = 64; + const int64_t chunks = (cap + CHUNK - 1) / CHUNK; + return (uint64_t) chunks * (uint64_t) s.n_head * (HD + 2) + 64; +} +uint64_t shared_expert_scratch_bytes(int64_t n_ff) { + const uint64_t a = ((uint64_t) n_ff * 4 + 15) & ~15ull; + const uint64_t q0 = ((uint64_t) (n_ff / 32) * 34 + 15) & ~15ull; + const uint64_t qk = ((uint64_t) (n_ff / 256) * 292 + 15) & ~15ull; + return a * 2 + q0 + qk + 32; +} +uint64_t moe_hit_grouped_scratch_bytes(int64_t n_hits, int64_t n_embd, int64_t n_ff) { + if (n_hits <= 0) return 0; + const uint64_t gu = (uint64_t) n_hits * (uint64_t) (2 * n_ff) * 4; + const uint64_t q8 = (uint64_t) n_hits * (uint64_t) (n_ff / 32) * 34; + const uint64_t hs = (uint64_t) n_hits * (uint64_t) (n_ff / 32) * 4; + const uint64_t xh = (uint64_t) (n_embd / 32) * 4; + return ((gu + 15) & ~15ull) + ((q8 + 15) & ~15ull) + 2 * ((hs + 15) & ~15ull) + ((xh + 15) & ~15ull); +} +size_t gr_workspace_init(const GrShapes& s, void* base, GrWorkspace& out) { + const size_t hc_dim = (size_t) s.hc * (size_t) s.n_embd; + const size_t sz[5] = {hc_dim * sizeof(float), hc_dim * sizeof(uint16_t), (size_t) s.hc_lr * sizeof(uint16_t), + hc_dim * sizeof(float), (size_t) s.hc_lr * sizeof(float)}; + size_t al[5], bytes = 0; + for (int k = 0; k < 5; ++k) { + al[k] = (sz[k] + 15) & ~(size_t) 15; + bytes += al[k]; + } + out.bytes = bytes; + if (base != nullptr) { + unsigned char* p = (unsigned char*) base; + void* ptr[5]; + for (int k = 0; k < 5; ++k) { + ptr[k] = p; + p += al[k]; + } + out.xn = (float*) ptr[0]; + out.xq = (uint16_t*) ptr[1]; + out.lq = (uint16_t*) ptr[2]; + out.gated = (float*) ptr[3]; + out.lo = (float*) ptr[4]; + } + return bytes; +} + +// ---- RoPE (rope.cu, native_rope.cu): the config and the host-built angle table +void rope_scaling_set(const RopeScaling& scaling) { g_rope_scaling = scaling; } +const RopeScaling& rope_scaling() { return g_rope_scaling; } +void build_rope_table(int n_rot, double theta, int max_pos, float* cos_tab, float* sin_tab) { + const int half = n_rot / 2; + for (int p = 0; p < max_pos; ++p) + for (int i = 0; i < half; ++i) { + const double inv = std::pow(theta, -2.0 * (double) i / (double) n_rot); + const double ang = (double) p * inv; + cos_tab[(size_t) p * half + i] = (float) std::cos(ang); + sin_tab[(size_t) p * half + i] = (float) std::sin(ang); + } +} +void build_rope_table(int n_rot, const RopeScaling& sc, int max_pos, float* cos_tab, float* sin_tab) { + if (sc.type == RopeScalingType::None) { + build_rope_table(n_rot, sc.freq_base, max_pos, cos_tab, sin_tab); + return; + } + const int half = n_rot / 2; + const double fs = sc.freq_scale(); + const double ms = sc.mscale(); + double cd[2]; + sc.corr_dims(n_rot, cd); + const bool correct = sc.ext_factor != 0; + for (int p = 0; p < max_pos; ++p) + for (int i = 0; i < half; ++i) { + const double inv = std::pow(sc.freq_base, -2.0 * (double) i / (double) n_rot); + const double extrap = (double) p * inv; + const double interp = fs * extrap; + double ang = interp; + if (correct) { + const double ramp = (double) rope_yarn_ramp((float) cd[0], (float) cd[1], i) * sc.ext_factor; + ang = interp * (1.0 - ramp) + extrap * ramp; + } + cos_tab[(size_t) p * half + i] = (float) (std::cos(ang) * ms); + sin_tab[(size_t) p * half + i] = (float) (std::sin(ang) * ms); + } +} +void rope_table_set(const float*, const float*, int, const RopeScaling&) {} // the window keeps its own table +void rope_table_release(const float*) {} +void native_q6_k_unpack(const void*) {} // nothing packed on Metal + +} // namespace strata::kernels + +namespace strata::prefill { +Gemm::~Gemm() {} // nothing created on Metal (no prompt GEMMs yet) +} // namespace strata::prefill diff --git a/metal/src/engine/stubs.cpp b/metal/src/engine/stubs.cpp new file mode 100644 index 00000000..7fd0a04a --- /dev/null +++ b/metal/src/engine/stubs.cpp @@ -0,0 +1,410 @@ +// metal/src/engine/stubs.cpp - GENERATED by metal/tools/gen_stubs.py: do not edit. +// The kernel-library entry points with no Metal implementation yet: each stops the engine with its name +// (metal/src/engine/missing.cpp). Regenerate after porting one. + +#include "strata/kernels/bf16_bits.hpp" +#include "strata/kernels/bf16_gemv.hpp" +#include "strata/kernels/cvec.hpp" +#include "strata/kernels/dequant_bf16.hpp" +#include "strata/kernels/dequant_s2.hpp" +#include "strata/kernels/elementwise.hpp" +#include "strata/kernels/f16_bits.hpp" +#include "strata/kernels/fused_gdn.hpp" +#include "strata/kernels/fused_gr.hpp" +#include "strata/kernels/gdn.hpp" +#include "strata/kernels/gfx_arch.hpp" +#include "strata/kernels/gr.hpp" +#include "strata/kernels/iq_kernels.hpp" +#include "strata/kernels/kv_q4.hpp" +#include "strata/kernels/kv_q8.hpp" +#include "strata/kernels/kv_stream.hpp" +#include "strata/kernels/mrope.hpp" +#include "strata/kernels/native_flash_attn.hpp" +#include "strata/kernels/native_gdn.hpp" +#include "strata/kernels/native_gdn_preprocess.hpp" +#include "strata/kernels/native_gr_norm.hpp" +#include "strata/kernels/native_gr_postops.hpp" +#include "strata/kernels/native_mmvq.hpp" +#include "strata/kernels/native_moe.hpp" +#include "strata/kernels/native_ple_postops.hpp" +#include "strata/kernels/native_qsa.hpp" +#include "strata/kernels/native_qsa_indexer.hpp" +#include "strata/kernels/native_qsa_score.hpp" +#include "strata/kernels/native_rope.hpp" +#include "strata/kernels/native_router.hpp" +#include "strata/kernels/ngram.hpp" +#include "strata/kernels/pdl.hpp" +#include "strata/kernels/ple.hpp" +#include "strata/kernels/qsa.hpp" +#include "strata/kernels/qsa_decode_attn.hpp" +#include "strata/kernels/qsa_prompt_attn.hpp" +#include "strata/kernels/qsa_select.hpp" +#include "strata/kernels/quantize_act.hpp" +#include "strata/kernels/rope.hpp" +#include "strata/kernels/rope_scaling.hpp" +#include "strata/kernels/route_prior.hpp" +#include "strata/kernels/router_top10.hpp" +#include "strata/kernels/s2_expert_grouped.hpp" +#include "strata/kernels/s2_gemv.hpp" +#include "strata/kernels/s2_gemv_q8.hpp" +#include "strata/kernels/s_gemv.hpp" +#include "strata/kernels/sampler.hpp" +#include "strata/kernels/shared_expert.hpp" +#include "strata/kernels/verify_kernels.hpp" +#include "strata/kernels/cpu/expert.hpp" +#include "strata/kernels/cpu/expert_layout.hpp" +#include "strata/kernels/cpu/iq_avx1.hpp" +#include "strata/kernels/cpu/iq_avx2.hpp" +#include "strata/kernels/cpu/iq_avx512.hpp" +#include "strata/kernels/cpu/kq_avx1.hpp" +#include "strata/kernels/cpu/kq_avx2.hpp" +#include "strata/kernels/cpu/native_expert.hpp" +#include "strata/kernels/cpu/pool.hpp" +#include "strata/prefill/gemm.hpp" +#include "strata/prefill/kernels.hpp" +#include "strata/prefill/moe_fused.hpp" +#include "strata/prefill/moe_fused_iq.hpp" +#include "strata/prefill/moe_mmq.hpp" +#include "strata/prefill/prefill.hpp" +#include "strata/prefill/share_rules.hpp" +#include "strata/prefill/xmx_moe.hpp" +#include "strata/core/arch_defaults.hpp" +#include "strata/core/batch_rows.hpp" +#include "strata/core/conversation_buffer.hpp" +#include "strata/core/conversation_cache.hpp" +#include "strata/core/conversation_file.hpp" +#include "strata/core/conversation_memory.hpp" +#include "strata/core/conversation_snapshot.hpp" +#include "strata/core/coupled_draft.hpp" +#include "strata/core/device.hpp" +#include "strata/core/dma_batch.hpp" +#include "strata/core/emulate.hpp" +#include "strata/core/exchange_storage.hpp" +#include "strata/core/expert_cache.hpp" +#include "strata/core/expert_source.hpp" +#include "strata/core/foresight_swap.hpp" +#include "strata/core/graph.hpp" +#include "strata/core/hit_hook.hpp" +#include "strata/core/layer.hpp" +#include "strata/core/layout.hpp" +#include "strata/core/logit_bias.hpp" +#include "strata/core/mtp.hpp" +#include "strata/core/native_dense.hpp" +#include "strata/core/native_head.hpp" +#include "strata/core/on_device.hpp" +#include "strata/core/peer_experts.hpp" +#include "strata/core/pinned.hpp" +#include "strata/core/pinned_ring.hpp" +#include "strata/core/progress.hpp" +#include "strata/core/remote_expert_opt.hpp" +#include "strata/core/remote_experts.hpp" +#include "strata/core/session.hpp" +#include "strata/core/session_save_reclaim.hpp" +#include "strata/core/spec_prob.hpp" +#include "strata/core/verify.hpp" +#include "strata/core/vmm.hpp" +#include "strata/core/weights.hpp" +#include "strata/platform/aux_cpus.hpp" +#include "strata/platform/direct_file.hpp" +#include "strata/platform/host_limits.hpp" +#include "strata/platform/memory.hpp" +#include "strata/ngram/ple_reader.hpp" +#include "strata/spec/controller.hpp" +#include "strata/spec/draft_policy.hpp" +#include "strata/spec/draft_source.hpp" +#include "strata/spec/suffix_drafter.hpp" +#include + +namespace strata::metal_engine { void missing(const char* name); } + +auto strata::core::RemoteExpertOpt::accumulate(const RemoteExperts & p0) -> void { strata::metal_engine::missing("strata::core::RemoteExpertOpt::accumulate"); } +auto strata::core::RemoteExpertOpt::adapt(const std::vector & p0, const std::vector & p1, const std::vector> & p2, int p3, ExpertSource & p4) -> bool { strata::metal_engine::missing("strata::core::RemoteExpertOpt::adapt"); return {}; } +auto strata::core::RemoteExpertOpt::attach(RemoteExperts & p0) -> void { strata::metal_engine::missing("strata::core::RemoteExpertOpt::attach"); } +auto strata::core::RemoteExpertOpt::begin(const float * p0, int p1, int p2) -> void { strata::metal_engine::missing("strata::core::RemoteExpertOpt::begin"); } +auto strata::core::RemoteExpertOpt::combine(float * p0, float * p1, int p2, int p3, const uint32_t * p4, uint32_t p5, void * p6) const -> void { strata::metal_engine::missing("strata::core::RemoteExpertOpt::combine"); } +auto strata::core::RemoteExpertOpt::copy_rows(float * p0, const float * p1, int p2, int p3, const int32_t * p4, const int32_t * p5, void * p6) const -> void { strata::metal_engine::missing("strata::core::RemoteExpertOpt::copy_rows"); } +auto strata::core::RemoteExpertOpt::init(std::string & p0) -> bool { strata::metal_engine::missing("strata::core::RemoteExpertOpt::init"); return {}; } +auto strata::core::RemoteExpertOpt::metadata_bytes() -> size_t { strata::metal_engine::missing("strata::core::RemoteExpertOpt::metadata_bytes"); return {}; } +auto strata::core::RemoteExpertOpt::owns(int64_t p0, int32_t p1) const -> bool { strata::metal_engine::missing("strata::core::RemoteExpertOpt::owns"); return {}; } +auto strata::core::RemoteExpertOpt::prepare(const RemoteExperts & p0, void * p1) const -> void { strata::metal_engine::missing("strata::core::RemoteExpertOpt::prepare"); } +auto strata::core::RemoteExpertOpt::reduce(RemoteExperts & p0, const void * p1, std::string & p2) -> bool { strata::metal_engine::missing("strata::core::RemoteExpertOpt::reduce"); return {}; } +strata::core::RemoteExpertOpt::~RemoteExpertOpt() { strata::metal_engine::missing("strata::core::RemoteExpertOpt::~RemoteExpertOpt"); } +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"); } } +namespace strata::kernels { void bf16_gemv_fp32_mmvf_cols(const float * p0, const uint16_t * p1, float * p2, int64_t p3, int64_t p4, int p5, void * p6) { strata::metal_engine::missing("strata::kernels::bf16_gemv_fp32_mmvf_cols"); } } +namespace strata::kernels { void bf16_gemv_fp32_mmvf_multi(const float * p0, int64_t p1, const uint16_t * p2, float * p3, int64_t p4, int64_t p5, int64_t p6, int p7, void * p8) { strata::metal_engine::missing("strata::kernels::bf16_gemv_fp32_mmvf_multi"); } } +namespace strata::kernels { bool bf16_gemv_fp32_mmvf_multi_aux(const float * p0, int64_t p1, const uint16_t * p2, float * p3, int64_t p4, int64_t p5, int64_t p6, int p7, const uint16_t * p8, float * p9, int64_t p10, void * p11) { strata::metal_engine::missing("strata::kernels::bf16_gemv_fp32_mmvf_multi_aux"); return {}; } } +namespace strata::kernels { void bf16_gemv_split(const uint16_t * p0, const uint16_t * p1, float * p2, int64_t p3, int64_t p4, int p5, void * p6) { strata::metal_engine::missing("strata::kernels::bf16_gemv_split"); } } +namespace strata::kernels { void broadcast_streams(const float * p0, float * p1, int64_t p2, int p3, int p4, void * p5) { strata::metal_engine::missing("strata::kernels::broadcast_streams"); } } +namespace strata::kernels { void build_rope_table(int p0, double p1, int p2, float * p3, float * p4) { strata::metal_engine::missing("strata::kernels::build_rope_table"); } } +namespace strata::kernels { void build_rope_table(int p0, const RopeScaling & p1, int p2, float * p3, float * p4) { strata::metal_engine::missing("strata::kernels::build_rope_table"); } } +namespace strata::kernels { void copy_from_mapped(float * p0, const float * p1, int64_t p2, void * p3) { strata::metal_engine::missing("strata::kernels::copy_from_mapped"); } } +namespace strata::kernels { void copy_from_mapped_multi(const MappedCopy * p0, int p1, void * p2) { strata::metal_engine::missing("strata::kernels::copy_from_mapped_multi"); } } +namespace strata::kernels { void copy_i32_from_mapped(int32_t * p0, const int32_t * p1, int64_t p2, void * p3) { strata::metal_engine::missing("strata::kernels::copy_i32_from_mapped"); } } +namespace strata::kernels { void copy_i32_from_mapped_unless(int32_t * p0, const int32_t * p1, long long p2, const uint32_t * p3, uint32_t p4, void * p5) { strata::metal_engine::missing("strata::kernels::copy_i32_from_mapped_unless"); } } +namespace strata::kernels { void copy_indexed(float * p0, const float * p1, int64_t p2, const int32_t * p3, int64_t p4, void * p5) { strata::metal_engine::missing("strata::kernels::copy_indexed"); } } +namespace strata::kernels { void copy_or_zero_from_mapped(float * p0, const float * p1, long long p2, const uint32_t * p3, uint32_t p4, void * p5) { strata::metal_engine::missing("strata::kernels::copy_or_zero_from_mapped"); } } +namespace strata::kernels { void copy_row_to_first(const int32_t * p0, float * p1, int64_t p2, float * p3, int64_t p4, float * p5, int64_t p6, void * p7) { strata::metal_engine::missing("strata::kernels::copy_row_to_first"); } } +namespace strata::kernels { void copy_rows_from_mapped(float * p0, const float * p1, int64_t p2, int64_t p3, const int32_t * p4, const int32_t * p5, void * p6) { strata::metal_engine::missing("strata::kernels::copy_rows_from_mapped"); } } +namespace strata::kernels { void copy_rows_strided(float * p0, const float * p1, int64_t p2, int64_t p3, int64_t p4, void * p5) { strata::metal_engine::missing("strata::kernels::copy_rows_strided"); } } +namespace strata::kernels { void coupled_draft_sample(float * p0, int p1, const int32_t * p2, const int32_t * p3, int p4, const SamplerParams * p5, int32_t * p6, int p7, int p8, const int32_t * p9, void * p10, int32_t * p11, float * p12, void * p13) { strata::metal_engine::missing("strata::kernels::coupled_draft_sample"); } } +namespace strata::kernels { size_t coupled_draft_scratch_bytes(int p0) { strata::metal_engine::missing("strata::kernels::coupled_draft_scratch_bytes"); return {}; } } +namespace strata::kernels { void coupled_draft_stage(const SamplerParams * p0, const int32_t * p1, SamplerParams * p2, int32_t * p3, int p4, void * p5) { strata::metal_engine::missing("strata::kernels::coupled_draft_stage"); } } +namespace strata::kernels::cpu { void act_quant_q8_1(const float * p0, int p1, ActQ & p2) { strata::metal_engine::missing("strata::kernels::cpu::act_quant_q8_1"); } } +namespace strata::kernels::cpu { void act_quant_q8_1_avx1(const float * p0, int p1, ActQ & p2) { strata::metal_engine::missing("strata::kernels::cpu::act_quant_q8_1_avx1"); } } +namespace strata::kernels::cpu { void act_quant_q8_1_avx2(const float * p0, int p1, ActQ & p2) { strata::metal_engine::missing("strata::kernels::cpu::act_quant_q8_1_avx2"); } } +namespace strata::kernels::cpu { void bf16_rows_dot_multi(const uint16_t * p0, int p1, int p2, const float * p3, int p4, float * p5) { strata::metal_engine::missing("strata::kernels::cpu::bf16_rows_dot_multi"); } } +namespace strata::kernels::cpu { void bf16_rows_dot_multi_avx1(const uint16_t * p0, int p1, int p2, const float * p3, int p4, float * p5) { strata::metal_engine::missing("strata::kernels::cpu::bf16_rows_dot_multi_avx1"); } } +namespace strata::kernels::cpu { void iq128_down_rows(int p0, const uint8_t * p1, size_t p2, int p3, const void *const * p4, int p5, float *const * p6, int p7, int p8) { strata::metal_engine::missing("strata::kernels::cpu::iq128_down_rows"); } } +namespace strata::kernels::cpu { bool iq128_down_supported(int p0) noexcept { strata::metal_engine::missing("strata::kernels::cpu::iq128_down_supported"); return {}; } } +namespace strata::kernels::cpu { void iq128_gu_rows(int p0, const uint8_t * p1, size_t p2, size_t p3, int p4, const void *const * p5, int p6, float *const * p7, int p8, int p9) { strata::metal_engine::missing("strata::kernels::cpu::iq128_gu_rows"); } } +namespace strata::kernels::cpu { bool iq128_supported(int p0) noexcept { strata::metal_engine::missing("strata::kernels::cpu::iq128_supported"); return {}; } } +namespace strata::kernels::cpu { void iq256_gu_rows(int p0, const uint8_t * p1, size_t p2, size_t p3, int p4, const void *const * p5, int p6, float *const * p7, int p8, int p9) { strata::metal_engine::missing("strata::kernels::cpu::iq256_gu_rows"); } } +namespace strata::kernels::cpu { void iq4nl256_down_rows(const uint8_t * p0, size_t p1, int p2, const void *const * p3, int p4, float *const * p5, int p6, int p7) { strata::metal_engine::missing("strata::kernels::cpu::iq4nl256_down_rows"); } } +namespace strata::kernels::cpu { void iq512_gu_rows(int p0, const uint8_t * p1, size_t p2, size_t p3, int p4, const void *const * p5, int p6, float *const * p7, int p8, int p9) { strata::metal_engine::missing("strata::kernels::cpu::iq512_gu_rows"); } } +namespace strata::kernels::cpu { void kq256_gu_rows(int p0, const uint8_t * p1, size_t p2, size_t p3, int p4, const void *const * p5, int p6, float *const * p7, int p8, int p9) { strata::metal_engine::missing("strata::kernels::cpu::kq256_gu_rows"); } } +namespace strata::kernels::cpu { void kq256_rows(int p0, const uint8_t * p1, size_t p2, int p3, const void *const * p4, int p5, float *const * p6, int p7, int p8) { strata::metal_engine::missing("strata::kernels::cpu::kq256_rows"); } } +namespace strata::kernels::cpu { void q2_0_gguf_rows_multi(const uint8_t * p0, size_t p1, int p2, const ActQ *const * p3, int p4, float *const * p5, int p6, int p7) { strata::metal_engine::missing("strata::kernels::cpu::q2_0_gguf_rows_multi"); } } +namespace strata::kernels::cpu { void q2_0_gguf_rows_multi_avx1(const uint8_t * p0, size_t p1, int p2, const ActQ *const * p3, int p4, float *const * p5, int p6, int p7) { strata::metal_engine::missing("strata::kernels::cpu::q2_0_gguf_rows_multi_avx1"); } } +namespace strata::kernels::cpu { void q2_0_gguf_rows_multi_avx1_legacy(const uint8_t * p0, size_t p1, int p2, const ActQ *const * p3, int p4, float *const * p5, int p6, int p7) { strata::metal_engine::missing("strata::kernels::cpu::q2_0_gguf_rows_multi_avx1_legacy"); } } +namespace strata::kernels::cpu { void q2_0_gguf_rows_multi_avx2(const uint8_t * p0, size_t p1, int p2, const ActQ *const * p3, int p4, float *const * p5, int p6, int p7) { strata::metal_engine::missing("strata::kernels::cpu::q2_0_gguf_rows_multi_avx2"); } } +namespace strata::kernels::cpu { void q2_0_gguf_rows_multi_avx2_legacy(const uint8_t * p0, size_t p1, int p2, const ActQ *const * p3, int p4, float *const * p5, int p6, int p7) { strata::metal_engine::missing("strata::kernels::cpu::q2_0_gguf_rows_multi_avx2_legacy"); } } +namespace strata::kernels::cpu { void q8k_quant_avx1(const float * p0, void * p1, int64_t p2) { strata::metal_engine::missing("strata::kernels::cpu::q8k_quant_avx1"); } } +namespace strata::kernels::cpu { void q8k_quant_avx2(const float * p0, void * p1, int64_t p2) { strata::metal_engine::missing("strata::kernels::cpu::q8k_quant_avx2"); } } +namespace strata::kernels::cpu { void s2_expert_down_rows(const uint8_t * p0, const ActQ & p1, float * p2, int p3, int p4) { strata::metal_engine::missing("strata::kernels::cpu::s2_expert_down_rows"); } } +namespace strata::kernels::cpu { void s2_expert_down_rows_multi(const uint8_t * p0, const ActQ *const * p1, int p2, float *const * p3, int p4, int p5) { strata::metal_engine::missing("strata::kernels::cpu::s2_expert_down_rows_multi"); } } +namespace strata::kernels::cpu { void s2_expert_gu_rows(const uint8_t * p0, const ActQ & p1, float * p2, int p3, int p4) { strata::metal_engine::missing("strata::kernels::cpu::s2_expert_gu_rows"); } } +namespace strata::kernels::cpu { void s2_expert_gu_rows_multi(const uint8_t * p0, const ActQ *const * p1, int p2, float *const * p3, int p4, int p5) { strata::metal_engine::missing("strata::kernels::cpu::s2_expert_gu_rows_multi"); } } +namespace strata::kernels::cpu { void s2_expert_vnni_q(const uint8_t * p0, const ActQ & p1, float * p2, ExpertScratch & p3) { strata::metal_engine::missing("strata::kernels::cpu::s2_expert_vnni_q"); } } +namespace strata::kernels { const Cvec & cvec() { strata::metal_engine::missing("strata::kernels::cvec"); static std::remove_cv_t> s{}; return s; } } +namespace strata::kernels { void cvec_apply(float * p0, int64_t p1, int64_t p2, int64_t p3, const float * p4, int64_t p5, const float * p6, int64_t p7, bool p8, void * p9) { strata::metal_engine::missing("strata::kernels::cvec_apply"); } } +namespace strata::kernels { bool cvec_replicate(std::string & p0) { strata::metal_engine::missing("strata::kernels::cvec_replicate"); return {}; } } +namespace strata::kernels { void cvec_set_enabled(bool p0) { strata::metal_engine::missing("strata::kernels::cvec_set_enabled"); } } +namespace strata::kernels { bool cvec_tables(const float ** p0, const float ** p1, const int ** p2) { strata::metal_engine::missing("strata::kernels::cvec_tables"); return {}; } } +namespace strata::kernels { bool cvec_upload(const std::vector & p0, const std::vector & p1, int p2, int p3, int p4, int64_t p5, int64_t p6, std::string & p7) { strata::metal_engine::missing("strata::kernels::cvec_upload"); return {}; } } +namespace strata::kernels { void doorbell_publish(const float * p0, const int32_t * p1, const float * p2, int64_t p3, int64_t p4, float * p5, int32_t * p6, float * p7, uint32_t * p8, void * p9) { strata::metal_engine::missing("strata::kernels::doorbell_publish"); } } +namespace strata::kernels { void doorbell_publish_res(const float * p0, const int32_t * p1, const int32_t * p2, int p3, int64_t p4, int64_t p5, float * p6, int32_t * p7, uint32_t * p8, void * p9) { strata::metal_engine::missing("strata::kernels::doorbell_publish_res"); } } +namespace strata::kernels { void doorbell_ring(uint32_t * p0, void * p1) { strata::metal_engine::missing("strata::kernels::doorbell_ring"); } } +namespace strata::kernels { void doorbell_wait(const uint32_t * p0, const uint32_t * p1, void * p2) { strata::metal_engine::missing("strata::kernels::doorbell_wait"); } } +namespace strata::kernels { void embedding_gather(const uint8_t * p0, const float * p1, const float * p2, int64_t p3, int p4, int p5, int p6, float * p7, void * p8) { strata::metal_engine::missing("strata::kernels::embedding_gather"); } } +namespace strata::kernels { void embedding_gather_dev(const uint8_t * p0, const float * p1, const float * p2, const int32_t * p3, int p4, int64_t p5, int p6, int p7, int p8, uint64_t p9, uint64_t p10, float * p11, void * p12) { strata::metal_engine::missing("strata::kernels::embedding_gather_dev"); } } +namespace strata::kernels { void f32_to_bf16_bulk(const float * p0, uint16_t * p1, int64_t p2, void * p3) { strata::metal_engine::missing("strata::kernels::f32_to_bf16_bulk"); } } +namespace strata::kernels { void fetch_blobs(const unsigned long long * p0, const int32_t * p1, uint8_t * p2, int64_t p3, int p4, void * p5) { strata::metal_engine::missing("strata::kernels::fetch_blobs"); } } +namespace strata::kernels { void force_token(int32_t * p0, const int32_t * p1, int p2, void * p3) { strata::metal_engine::missing("strata::kernels::force_token"); } } +namespace strata::kernels { void fused_gdn_ab(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, void * p9) { strata::metal_engine::missing("strata::kernels::fused_gdn_ab"); } } +namespace strata::kernels { void fused_gdn_conv_l2(float * p0, const float * p1, const float * p2, float * p3, int p4, int p5, float p6, void * p7) { strata::metal_engine::missing("strata::kernels::fused_gdn_conv_l2"); } } +namespace strata::kernels { void fused_gdn_step_norm(float * p0, const float * p1, const float * p2, const float * p3, const float * p4, const float * p5, const float * p6, const float * p7, float p8, float * p9, int p10, int p11, void * p12) { strata::metal_engine::missing("strata::kernels::fused_gdn_step_norm"); } } +namespace strata::kernels { void fused_gr_check() { strata::metal_engine::missing("strata::kernels::fused_gr_check"); } } +namespace strata::kernels { void fused_gr_dequant_q8_0(const uint8_t * p0, uint16_t * p1, int64_t p2, void * p3) { strata::metal_engine::missing("strata::kernels::fused_gr_dequant_q8_0"); } } +namespace strata::kernels { void fused_gr_quantize_q8_0(const uint16_t * p0, uint8_t * p1, int64_t p2, void * p3) { strata::metal_engine::missing("strata::kernels::fused_gr_quantize_q8_0"); } } +namespace strata::kernels { void fused_gr_read(const FusedGrArgs & p0, void * p1) { strata::metal_engine::missing("strata::kernels::fused_gr_read"); } } +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"); } } +namespace strata::kernels { void gdn_conv_l2_multi(const float * p0, const float * p1, const float * p2, float * p3, int p4, int p5, float p6, int p7, void * p8, int p9, bool p10) { strata::metal_engine::missing("strata::kernels::gdn_conv_l2_multi"); } } +namespace strata::kernels { void gdn_conv_step(float * p0, const float * p1, const float * p2, float * p3, int64_t p4, int64_t p5, void * p6) { strata::metal_engine::missing("strata::kernels::gdn_conv_step"); } } +namespace strata::kernels { void gdn_gate(const float * p0, const float * p1, const float * p2, float * p3, int64_t p4, int64_t p5, void * p6) { strata::metal_engine::missing("strata::kernels::gdn_gate"); } } +namespace strata::kernels { void gdn_l2_norm(float * p0, int64_t p1, int64_t p2, float p3, void * p4) { strata::metal_engine::missing("strata::kernels::gdn_l2_norm"); } } +namespace strata::kernels { void gdn_out_norm(const float * p0, const float * p1, const float * p2, float * p3, int64_t p4, int64_t p5, float p6, void * p7) { strata::metal_engine::missing("strata::kernels::gdn_out_norm"); } } +namespace strata::kernels { void gdn_step(float * p0, const float * p1, const float * p2, const float * p3, const float * p4, const float * p5, float * p6, const GdnShapes & p7, void * p8) { strata::metal_engine::missing("strata::kernels::gdn_step"); } } +namespace strata::kernels { void gdn_step_norm_multi(float * p0, const float * p1, int p2, const float * p3, const float * p4, const float * p5, const float * p6, float p7, float * p8, int p9, int p10, int p11, const int32_t * p12, void * p13, int p14, void * p15) { strata::metal_engine::missing("strata::kernels::gdn_step_norm_multi"); } } +namespace strata::kernels { void gpu_noop(void * p0) { strata::metal_engine::missing("strata::kernels::gpu_noop"); } } +namespace strata::kernels { void gpu_stamp(unsigned long long * p0, int p1, void * p2) { strata::metal_engine::missing("strata::kernels::gpu_stamp"); } } +namespace strata::kernels { bool gr_native_mmvf_enabled() { strata::metal_engine::missing("strata::kernels::gr_native_mmvf_enabled"); return {}; } } +namespace strata::kernels { void gr_read(const float * p0, const float * p1, const uint16_t * p2, const uint16_t * p3, const uint16_t * p4, float p5, const GrShapes & p6, const GrWorkspace & p7, float * p8, float * p9, void * p10) { strata::metal_engine::missing("strata::kernels::gr_read"); } } +namespace strata::kernels { void gr_set_fp32_activations(bool p0) { strata::metal_engine::missing("strata::kernels::gr_set_fp32_activations"); } } +namespace strata::kernels { void gr_set_native_mmvf(bool p0) { strata::metal_engine::missing("strata::kernels::gr_set_native_mmvf"); } } +namespace strata::kernels { size_t gr_workspace_init(const GrShapes & p0, void * p1, GrWorkspace & p2) { strata::metal_engine::missing("strata::kernels::gr_workspace_init"); return {}; } } +namespace strata::kernels { void gr_write(const float * p0, const float * p1, const float * p2, const GrShapes & p3, float * p4, void * p5) { strata::metal_engine::missing("strata::kernels::gr_write"); } } +namespace strata::kernels { void gr_write_multi(const float * p0, const float * p1, const float * p2, const GrShapes & p3, float * p4, int p5, void * p6) { strata::metal_engine::missing("strata::kernels::gr_write_multi"); } } +namespace strata::kernels { void indexer_key_append(const float * p0, const int32_t * p1, int32_t p2, const float * p3, float p4, const QsaIndexerBuffers & p5, const QsaShapes & p6, const float * p7, const float * p8, void * p9) { strata::metal_engine::missing("strata::kernels::indexer_key_append"); } } +namespace strata::kernels { void iq_dequant_f16(int p0, const void * p1, int64_t p2, uint16_t * p3, void * p4) { strata::metal_engine::missing("strata::kernels::iq_dequant_f16"); } } +namespace strata::kernels { void iq_dequant_f32(int p0, const void * p1, int64_t p2, float * p3, void * p4) { strata::metal_engine::missing("strata::kernels::iq_dequant_f32"); } } +namespace strata::kernels { void iq_dequant_gu_f16(int p0, const void * p1, const void * p2, int64_t p3, int64_t p4, uint16_t * p5, void * p6) { strata::metal_engine::missing("strata::kernels::iq_dequant_gu_f16"); } } +namespace strata::kernels { void iq_embed_rows(int p0, const void * p1, size_t p2, const int32_t * p3, int64_t p4, int64_t p5, float * p6, void * p7) { strata::metal_engine::missing("strata::kernels::iq_embed_rows"); } } +namespace strata::kernels { void kv_append_q4(uint8_t * p0, uint8_t * p1, const int32_t * p2, int64_t p3, int64_t p4, const float * p5, const float * p6, const QsaShapes & p7, void * p8, const KvHostPools * p9, const KvHostPools * p10) { strata::metal_engine::missing("strata::kernels::kv_append_q4"); } } +namespace strata::kernels { void kv_append_q4_step(uint8_t * p0, uint8_t * p1, const int32_t * p2, const int32_t * p3, const float * p4, const float * p5, const QsaShapes & p6, void * p7, const KvHostPools * p8) { strata::metal_engine::missing("strata::kernels::kv_append_q4_step"); } } +namespace strata::kernels { void kv_append_q4_steps(uint8_t * p0, uint8_t * p1, const int32_t * p2, const int32_t * p3, int p4, int p5, const float * p6, const float * p7, const QsaShapes & p8, void * p9, const KvHostPools * p10) { strata::metal_engine::missing("strata::kernels::kv_append_q4_steps"); } } +namespace strata::kernels { void kv_append_q8_step(int8_t * p0, int8_t * p1, uint16_t * p2, uint16_t * p3, const int32_t * p4, const int32_t * p5, const float * p6, const float * p7, const QsaShapes & p8, void * p9, const KvHostPools * p10) { strata::metal_engine::missing("strata::kernels::kv_append_q8_step"); } } +namespace strata::kernels { void kv_append_q8_steps(int8_t * p0, int8_t * p1, uint16_t * p2, uint16_t * p3, const int32_t * p4, const int32_t * p5, int p6, const float * p7, const float * p8, int p9, int p10, const QsaShapes & p11, void * p12, const KvHostPools * p13) { strata::metal_engine::missing("strata::kernels::kv_append_q8_steps"); } } +namespace strata::kernels { void kv_append_step(uint16_t * p0, uint16_t * p1, const int32_t * p2, const int32_t * p3, const float * p4, const float * p5, const QsaShapes & p6, void * p7, const KvHostPools * p8) { strata::metal_engine::missing("strata::kernels::kv_append_step"); } } +namespace strata::kernels { uint64_t kv_block_bytes(const QsaShapes & p0, int p1) { strata::metal_engine::missing("strata::kernels::kv_block_bytes"); return {}; } } +namespace strata::kernels { void kv_gather_q4_step(const uint8_t * p0, const uint8_t * p1, const int32_t * p2, const int32_t * p3, const int32_t * p4, int64_t p5, const QsaShapes & p6, uint16_t * p7, uint16_t * p8, void * p9) { strata::metal_engine::missing("strata::kernels::kv_gather_q4_step"); } } +namespace strata::kernels { void kv_gather_q8_step(const int8_t * p0, const int8_t * p1, const uint16_t * p2, const uint16_t * p3, const int32_t * p4, const int32_t * p5, const int32_t * p6, int64_t p7, const QsaShapes & p8, uint16_t * p9, uint16_t * p10, void * p11) { strata::metal_engine::missing("strata::kernels::kv_gather_q8_step"); } } +namespace strata::kernels { void kv_gather_step(const uint16_t * p0, const uint16_t * p1, const int32_t * p2, const int32_t * p3, const int32_t * p4, int64_t p5, const QsaShapes & p6, uint16_t * p7, uint16_t * p8, void * p9) { strata::metal_engine::missing("strata::kernels::kv_gather_step"); } } +namespace strata::kernels { void kv_ring_restore(const QsaAttnPools & p0, const KvHostPools & p1, int p2, int64_t p3, int64_t p4, int64_t p5, const QsaShapes & p6, void * p7) { strata::metal_engine::missing("strata::kernels::kv_ring_restore"); } } +namespace strata::kernels { void kv_ring_table(int32_t * p0, int64_t p1, int64_t p2, void * p3) { strata::metal_engine::missing("strata::kernels::kv_ring_table"); } } +namespace strata::kernels { void kv_stage_from_host(const QsaAttnPools & p0, const KvHostPools & p1, int p2, int64_t p3, const QsaShapes & p4, void * p5) { strata::metal_engine::missing("strata::kernels::kv_stage_from_host"); } } +namespace strata::kernels { KvStreamCounters kv_stream_counters(const KvStreamMap & p0) { strata::metal_engine::missing("strata::kernels::kv_stream_counters"); return {}; } } +namespace strata::kernels { void kv_stream_reset(const KvStreamMap & p0, void * p1) { strata::metal_engine::missing("strata::kernels::kv_stream_reset"); } } +namespace strata::kernels { void kv_stream_resolve(const KvStreamMap & p0, const QsaAttnPools & p1, const KvHostPools & p2, int p3, const int32_t * p4, const int32_t * p5, int64_t p6, int64_t p7, const QsaShapes & p8, void * p9) { strata::metal_engine::missing("strata::kernels::kv_stream_resolve"); } } +namespace strata::kernels { void logits_nonfinite_rows(const float * p0, int p1, int p2, uint32_t * p3, void * p4) { strata::metal_engine::missing("strata::kernels::logits_nonfinite_rows"); } } +namespace strata::kernels { void map_ids(int32_t * p0, const int32_t * p1, int p2, void * p3) { strata::metal_engine::missing("strata::kernels::map_ids"); } } +namespace strata::kernels { void moe_combine(const float * p0, const float * p1, const float * p2, float * p3, int64_t p4, int64_t p5, void * p6) { strata::metal_engine::missing("strata::kernels::moe_combine"); } } +namespace strata::kernels { void moe_group_resident(const int32_t * p0, int p1, int p2, const uint8_t * p3, int64_t p4, unsigned long long * p5, int32_t * p6, int32_t * p7, int32_t * p8, int32_t * p9, void * p10) { strata::metal_engine::missing("strata::kernels::moe_group_resident"); } } +namespace strata::kernels { void moe_grouped_s2(const unsigned long long * p0, const int32_t * p1, const int32_t * p2, const int32_t * p3, const int32_t * p4, int64_t p5, int64_t p6, const uint8_t * p7, const float * p8, void * p9, float * p10, void * p11) { strata::metal_engine::missing("strata::kernels::moe_grouped_s2"); } } +namespace strata::kernels { void moe_hit_add(float * p0, const float * p1, const int32_t * p2, const int32_t * p3, int64_t p4, int64_t p5, void * p6) { strata::metal_engine::missing("strata::kernels::moe_hit_add"); } } +namespace strata::kernels { void moe_hit_grouped_s2(const uint8_t * p0, const int32_t * p1, const int32_t * p2, int64_t p3, int64_t p4, const uint8_t * p5, void * p6, float * p7, void * p8, const float * p9) { strata::metal_engine::missing("strata::kernels::moe_hit_grouped_s2"); } } +namespace strata::kernels { void moe_hit_grouped_s2_cpu_order(const uint8_t * p0, const int32_t * p1, const int32_t * p2, int64_t p3, int64_t p4, const uint8_t * p5, void * p6, float * p7, void * p8, const float * p9, float * p10) { strata::metal_engine::missing("strata::kernels::moe_hit_grouped_s2_cpu_order"); } } +namespace strata::kernels { void moe_hit_grouped_s2_dev(const uint8_t * p0, const int32_t * p1, const int32_t * p2, const int32_t * p3, int64_t p4, int64_t p5, const uint8_t * p6, void * p7, float * p8, void * p9, const float * p10) { strata::metal_engine::missing("strata::kernels::moe_hit_grouped_s2_dev"); } } +namespace strata::kernels { uint64_t moe_hit_grouped_scratch_bytes(int64_t p0, int64_t p1, int64_t p2) { strata::metal_engine::missing("strata::kernels::moe_hit_grouped_scratch_bytes"); return {}; } } +namespace strata::kernels { void moe_hit_select(const int32_t * p0, const int32_t * p1, int p2, int p3, int32_t * p4, int32_t * p5, int32_t * p6, void * p7) { strata::metal_engine::missing("strata::kernels::moe_hit_select"); } } +namespace strata::kernels { void mrope_table_set(const int32_t * p0) { strata::metal_engine::missing("strata::kernels::mrope_table_set"); } } +namespace strata::kernels { void mtp_select(const float * p0, int64_t p1, const int32_t * p2, const int32_t * p3, float * p4, int32_t * p5, int32_t * p6, int p7, void * p8, const float * p9, float * p10) { strata::metal_engine::missing("strata::kernels::mtp_select"); } } +namespace strata::kernels { bool multi_block_head_ops() { strata::metal_engine::missing("strata::kernels::multi_block_head_ops"); return {}; } } +namespace strata::kernels { void native_expert_grouped(const NativeExpertLayout & p0, const unsigned long long * p1, const int32_t * p2, const int32_t * p3, const int32_t * p4, const int32_t * p5, int64_t p6, int64_t p7, const void * p8, void * p9, float * p10, void * p11, int64_t p12) { strata::metal_engine::missing("strata::kernels::native_expert_grouped"); } } +namespace strata::kernels { void native_flash_attn_short_step(const float * p0, const uint16_t * p1, const uint16_t * p2, const int32_t * p3, int64_t p4, int p5, const QsaShapes & p6, float * p7, int32_t * p8, const uint16_t * p9, void * p10) { strata::metal_engine::missing("strata::kernels::native_flash_attn_short_step"); } } +namespace strata::kernels { void native_gdn_beta_gate(float * p0, int64_t p1, void * p2) { strata::metal_engine::missing("strata::kernels::native_gdn_beta_gate"); } } +namespace strata::kernels { void native_gdn_conv_silu(float * p0, const float * p1, const float * p2, float * p3, float * p4, int64_t p5, int64_t p6, void * p7) { strata::metal_engine::missing("strata::kernels::native_gdn_conv_silu"); } } +namespace strata::kernels { bool native_gdn_enabled() { strata::metal_engine::missing("strata::kernels::native_gdn_enabled"); return {}; } } +namespace strata::kernels { void native_gdn_gate(const float * p0, const float * p1, const float * p2, float * p3, int64_t p4, void * p5) { strata::metal_engine::missing("strata::kernels::native_gdn_gate"); } } +namespace strata::kernels { void native_gdn_l2_norm(float * p0, int64_t p1, int64_t p2, float p3, void * p4) { strata::metal_engine::missing("strata::kernels::native_gdn_l2_norm"); } } +namespace strata::kernels { void native_gdn_out_norm(const float * p0, const float * p1, const float * p2, float * p3, int64_t p4, int64_t p5, float p6, void * p7) { strata::metal_engine::missing("strata::kernels::native_gdn_out_norm"); } } +namespace strata::kernels { void native_gdn_set_enabled(bool p0) { strata::metal_engine::missing("strata::kernels::native_gdn_set_enabled"); } } +namespace strata::kernels { void native_gdn_step(float * p0, const float * p1, const float * p2, const float * p3, const float * p4, const float * p5, float * p6, const GdnShapes & p7, void * p8) { strata::metal_engine::missing("strata::kernels::native_gdn_step"); } } +namespace strata::kernels { void native_gr_post_multi(const float * p0, const float * p1, const float * p2, float * p3, int p4, int p5, int p6, long long p7, long long p8, long long p9, void * p10) { strata::metal_engine::missing("strata::kernels::native_gr_post_multi"); } } +namespace strata::kernels { void native_mmvq(int p0, const void * p1, const void * p2, float * p3, int p4, int p5, int p6, void * p7) { strata::metal_engine::missing("strata::kernels::native_mmvq"); } } +namespace strata::kernels { void native_mmvq_il(int p0, const void * p1, const void * p2, const void * p3, float * p4, int p5, int p6, int p7, void * p8) { strata::metal_engine::missing("strata::kernels::native_mmvq_il"); } } +namespace strata::kernels { void native_moe_combine(const float * p0, const float * p1, const float * p2, float * p3, int64_t p4, int64_t p5, void * p6) { strata::metal_engine::missing("strata::kernels::native_moe_combine"); } } +namespace strata::kernels { bool native_moe_combine_enabled() { strata::metal_engine::missing("strata::kernels::native_moe_combine_enabled"); return {}; } } +namespace strata::kernels { void native_moe_combine_multi(const float * p0, const float * p1, const float * p2, float * p3, int64_t p4, int64_t p5, int p6, void * p7) { strata::metal_engine::missing("strata::kernels::native_moe_combine_multi"); } } +namespace strata::kernels { void native_moe_combine_multi_hits_gated(const float * p0, const float * p1, const float * p2, const float * p3, float * p4, int64_t p5, int64_t p6, int p7, void * p8) { strata::metal_engine::missing("strata::kernels::native_moe_combine_multi_hits_gated"); } } +namespace strata::kernels { void native_moe_combine_set_enabled(bool p0) { strata::metal_engine::missing("strata::kernels::native_moe_combine_set_enabled"); } } +namespace strata::kernels { bool native_norm_rope_usable(int p0, int p1) { strata::metal_engine::missing("strata::kernels::native_norm_rope_usable"); return {}; } } +namespace strata::kernels { void native_ple_postops_batch(float * p0, float * p1, const float * p2, float * p3, const PleWeights & p4, float * p5, float * p6, float * p7, int p8, void * p9) { strata::metal_engine::missing("strata::kernels::native_ple_postops_batch"); } } +namespace strata::kernels { void native_ple_postops_batch_snap(float * p0, float * p1, const float * p2, float * p3, const PleWeights & p4, float * p5, float * p6, float * p7, int p8, float * p9, void * p10) { strata::metal_engine::missing("strata::kernels::native_ple_postops_batch_snap"); } } +namespace strata::kernels { void native_q5_k_f32(const void * p0, const float * p1, void * p2, float * p3, int p4, int p5, int p6, void * p7) { strata::metal_engine::missing("strata::kernels::native_q5_k_f32"); } } +namespace strata::kernels { bool native_q6_k_pack(const void * p0, int p1, int p2, const char * p3) { strata::metal_engine::missing("strata::kernels::native_q6_k_pack"); return {}; } } +namespace strata::kernels { void native_q6_k_unpack(const void * p0) { strata::metal_engine::missing("strata::kernels::native_q6_k_unpack"); } } +namespace strata::kernels { void native_q8_0_pack_host(const void * p0, void * p1, int p2, int p3) { strata::metal_engine::missing("strata::kernels::native_q8_0_pack_host"); } } +namespace strata::kernels { void native_q8_0_packed_register(const void * p0, const void * p1, int p2, int p3) { strata::metal_engine::missing("strata::kernels::native_q8_0_packed_register"); } } +namespace strata::kernels { void native_q8_0_packed_unregister(const void * p0) { strata::metal_engine::missing("strata::kernels::native_q8_0_packed_unregister"); } } +namespace strata::kernels { void native_q8_1_interleave(const void * p0, void * p1, int p2, int p3, void * p4) { strata::metal_engine::missing("strata::kernels::native_q8_1_interleave"); } } +namespace strata::kernels { bool native_qsa_enabled() { strata::metal_engine::missing("strata::kernels::native_qsa_enabled"); return {}; } } +namespace strata::kernels { void native_qsa_gate_apply(const float * p0, const float * p1, float * p2, int p3, int p4, void * p5) { strata::metal_engine::missing("strata::kernels::native_qsa_gate_apply"); } } +namespace strata::kernels { void native_qsa_indexer_append(const float * p0, const int32_t * p1, int32_t p2, const float * p3, float p4, const QsaIndexerBuffers & p5, const QsaShapes & p6, int64_t p7, const RopeScaling & p8, void * p9) { strata::metal_engine::missing("strata::kernels::native_qsa_indexer_append"); } } +namespace strata::kernels { void native_qsa_indexer_append_batch(const float * p0, int64_t p1, int64_t p2, int32_t p3, const float * p4, float p5, const QsaIndexerBuffers & p6, const QsaShapes & p7, int64_t p8, const RopeScaling & p9, void * p10) { strata::metal_engine::missing("strata::kernels::native_qsa_indexer_append_batch"); } } +namespace strata::kernels { void native_qsa_indexer_append_steps(const float * p0, const int32_t * p1, int p2, int p3, int32_t p4, const float * p5, float p6, const QsaIndexerBuffers & p7, const QsaShapes & p8, int64_t p9, const RopeScaling & p10, void * p11) { strata::metal_engine::missing("strata::kernels::native_qsa_indexer_append_steps"); } } +namespace strata::kernels { bool native_qsa_indexer_enabled() { strata::metal_engine::missing("strata::kernels::native_qsa_indexer_enabled"); return {}; } } +namespace strata::kernels { void native_qsa_indexer_set_enabled(bool p0) { strata::metal_engine::missing("strata::kernels::native_qsa_indexer_set_enabled"); } } +namespace strata::kernels { void native_qsa_rms_norm_grouped(const float * p0, const float * p1, float * p2, int p3, int p4, int p5, float p6, void * p7) { strata::metal_engine::missing("strata::kernels::native_qsa_rms_norm_grouped"); } } +namespace strata::kernels { void native_qsa_rms_norm_rope(const float * p0, int p1, const float * p2, float * p3, int p4, int p5, int p6, float p7, const RopeScaling & p8, const int * p9, void * p10) { strata::metal_engine::missing("strata::kernels::native_qsa_rms_norm_rope"); } } +namespace strata::kernels { void native_qsa_rms_norm_weighted(const float * p0, const float * p1, float * p2, int p3, int p4, float p5, void * p6) { strata::metal_engine::missing("strata::kernels::native_qsa_rms_norm_weighted"); } } +namespace strata::kernels { void native_qsa_set_enabled(bool p0) { strata::metal_engine::missing("strata::kernels::native_qsa_set_enabled"); } } +namespace strata::kernels { void native_quantize_q8_1(const float * p0, void * p1, int p2, int p3, void * p4) { strata::metal_engine::missing("strata::kernels::native_quantize_q8_1"); } } +namespace strata::kernels { void native_rope_apply(const float * p0, float * p1, int p2, int p3, int p4, const RopeScaling & p5, const int * p6, void * p7) { strata::metal_engine::missing("strata::kernels::native_rope_apply"); } } +namespace strata::kernels { bool native_rope_enabled() { strata::metal_engine::missing("strata::kernels::native_rope_enabled"); return {}; } } +namespace strata::kernels { void native_rope_set_enabled(bool p0) { strata::metal_engine::missing("strata::kernels::native_rope_set_enabled"); } } +namespace strata::kernels { void native_route_resident(const float * p0, int32_t * p1, float * p2, const int32_t * p3, int p4, float p5, int p6, int p7, unsigned long long * p8, void * p9) { strata::metal_engine::missing("strata::kernels::native_route_resident"); } } +namespace strata::kernels { bool native_router_enabled() { strata::metal_engine::missing("strata::kernels::native_router_enabled"); return {}; } } +namespace strata::kernels { void native_router_set_enabled(bool p0) { strata::metal_engine::missing("strata::kernels::native_router_set_enabled"); } } +namespace strata::kernels { void native_router_top10(const float * p0, int32_t * p1, float * p2, void * p3) { strata::metal_engine::missing("strata::kernels::native_router_top10"); } } +namespace strata::kernels { void native_router_top10_multi(const float * p0, int32_t * p1, float * p2, int p3, void * p4) { strata::metal_engine::missing("strata::kernels::native_router_top10_multi"); } } +namespace strata::kernels { bool & pdl_scope() { strata::metal_engine::missing("strata::kernels::pdl_scope"); static std::remove_cv_t> s{}; return s; } } +namespace strata::kernels { bool pdl_supported() { strata::metal_engine::missing("strata::kernels::pdl_supported"); return {}; } } +namespace strata::kernels { void ple_block(const float * p0, const float * p1, const float * p2, const PleWeights & p3, PleOut & p4, void * p5, void * p6) { strata::metal_engine::missing("strata::kernels::ple_block"); } } +namespace strata::kernels { void ple_block_projected(const float * p0, const float * p1, const float * p2, const float * p3, const PleWeights & p4, PleOut & p5, void * p6, void * p7) { strata::metal_engine::missing("strata::kernels::ple_block_projected"); } } +namespace strata::kernels { uint64_t ple_block_scratch_bytes() { strata::metal_engine::missing("strata::kernels::ple_block_scratch_bytes"); return {}; } } +namespace strata::kernels { void ple_history_advance(float * p0, const float * p1, void * p2) { strata::metal_engine::missing("strata::kernels::ple_history_advance"); } } +namespace strata::kernels { bool ple_native_bf16_enabled() { strata::metal_engine::missing("strata::kernels::ple_native_bf16_enabled"); return {}; } } +namespace strata::kernels { bool ple_native_postops_enabled() { strata::metal_engine::missing("strata::kernels::ple_native_postops_enabled"); return {}; } } +namespace strata::kernels { void ple_set_native_bf16(bool p0) { strata::metal_engine::missing("strata::kernels::ple_set_native_bf16"); } } +namespace strata::kernels { void ple_set_native_postops(bool p0) { strata::metal_engine::missing("strata::kernels::ple_set_native_postops"); } } +namespace strata::kernels { void qsa_attend_step(const float * p0, const uint16_t * p1, const uint16_t * p2, const int32_t * p3, int64_t p4, const QsaShapes & p5, float * p6, float * p7, void * p8) { strata::metal_engine::missing("strata::kernels::qsa_attend_step"); } } +namespace strata::kernels { void qsa_block_scores(const float * p0, const float * p1, const float * p2, const int32_t * p3, int64_t p4, int64_t p5, const QsaShapes & p6, float * p7, void * p8, int64_t p9) { strata::metal_engine::missing("strata::kernels::qsa_block_scores"); } } +namespace strata::kernels { bool qsa_block_scores_tc(const float * p0, const float * p1, const float * p2, const int32_t * p3, int64_t p4, int64_t p5, const QsaShapes & p6, float * p7, void * p8, int64_t p9) { strata::metal_engine::missing("strata::kernels::qsa_block_scores_tc"); return {}; } } +namespace strata::kernels { void qsa_block_topk(const float * p0, const int32_t * p1, int64_t p2, int64_t p3, int64_t p4, const QsaShapes & p5, int32_t * p6, void * p7, int64_t p8) { strata::metal_engine::missing("strata::kernels::qsa_block_topk"); } } +namespace strata::kernels { void qsa_decode_attn_batch(const float * p0, const QsaAttnPools & p1, const int32_t * p2, const int32_t * p3, int64_t p4, const QsaShapes & p5, float * p6, float * p7, int64_t p8, void * p9) { strata::metal_engine::missing("strata::kernels::qsa_decode_attn_batch"); } } +namespace strata::kernels { uint64_t qsa_decode_attn_scratch_floats(int64_t p0, const QsaShapes & p1) { strata::metal_engine::missing("strata::kernels::qsa_decode_attn_scratch_floats"); return {}; } } +namespace strata::kernels { void qsa_decode_attn_step(const float * p0, const QsaAttnPools & p1, const int32_t * p2, const int32_t * p3, int64_t p4, const QsaShapes & p5, float * p6, float * p7, void * p8) { strata::metal_engine::missing("strata::kernels::qsa_decode_attn_step"); } } +namespace strata::kernels { void qsa_gate_apply_f32(const float * p0, const float * p1, const QsaShapes & p2, float * p3, void * p4) { strata::metal_engine::missing("strata::kernels::qsa_gate_apply_f32"); } } +namespace strata::kernels { void qsa_index_step(const float * p0, const float * p1, const float * p2, const QsaShapes & p3, const int32_t * p4, int64_t p5, float * p6, void * p7) { strata::metal_engine::missing("strata::kernels::qsa_index_step"); } } +namespace strata::kernels { bool qsa_prompt_attn_batch(const float * p0, const QsaAttnPools & p1, const int32_t * p2, const int32_t * p3, int64_t p4, const QsaShapes & p5, float * p6, int64_t p7, void * p8) { strata::metal_engine::missing("strata::kernels::qsa_prompt_attn_batch"); return {}; } } +namespace strata::kernels { void qsa_step_fill(int32_t * p0, int64_t p1, const QsaShapes & p2) { strata::metal_engine::missing("strata::kernels::qsa_step_fill"); } } +namespace strata::kernels { void quantize_q8_0(const float * p0, uint8_t * p1, int64_t p2, void * p3) { strata::metal_engine::missing("strata::kernels::quantize_q8_0"); } } +namespace strata::kernels { void quantize_q8_0_scaled(const float * p0, uint8_t * p1, float * p2, int64_t p3, void * p4) { strata::metal_engine::missing("strata::kernels::quantize_q8_0_scaled"); } } +namespace strata::kernels { void quantize_q8_1_rows(const float * p0, int64_t p1, int64_t p2, void * p3, void * p4) { strata::metal_engine::missing("strata::kernels::quantize_q8_1_rows"); } } +namespace strata::kernels { void quantize_q8_K(const float * p0, uint8_t * p1, int64_t p2, void * p3) { strata::metal_engine::missing("strata::kernels::quantize_q8_K"); } } +namespace strata::kernels { void rebase_ptrs(unsigned long long * p0, const int32_t * p1, uint8_t * p2, int64_t p3, void * p4) { strata::metal_engine::missing("strata::kernels::rebase_ptrs"); } } +namespace strata::kernels { void resident_plan(const int32_t * p0, int p1, int p2, const int32_t * p3, int p4, const uint8_t * p5, const unsigned long long * p6, long long p7, int32_t * p8, long long p9, uint32_t * p10, uint32_t p11, void * p12, uint32_t * p13) { strata::metal_engine::missing("strata::kernels::resident_plan"); } } +namespace strata::kernels { void rms_norm_weighted(float * p0, const float * p1, int64_t p2, int64_t p3, float p4, void * p5) { strata::metal_engine::missing("strata::kernels::rms_norm_weighted"); } } +namespace strata::kernels { void rope_neox_apply(const float * p0, float * p1, int64_t p2, int p3, int p4, const float * p5, const float * p6, const int * p7, void * p8) { strata::metal_engine::missing("strata::kernels::rope_neox_apply"); } } +namespace strata::kernels { const RopeScaling & rope_scaling() { strata::metal_engine::missing("strata::kernels::rope_scaling"); static std::remove_cv_t> s{}; return s; } } +namespace strata::kernels { void rope_scaling_set(const RopeScaling & p0) { strata::metal_engine::missing("strata::kernels::rope_scaling_set"); } } +namespace strata::kernels { void rope_table_release(const float * p0) { strata::metal_engine::missing("strata::kernels::rope_table_release"); } } +namespace strata::kernels { void rope_table_set(const float * p0, const float * p1, int p2, const RopeScaling & p3) { strata::metal_engine::missing("strata::kernels::rope_table_set"); } } +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"); } } +namespace strata::kernels { void s_gemv_q8k_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_q8k_split"); } } +namespace strata::kernels { void sample_tokens(const float * p0, int p1, int p2, const int * p3, int p4, const SamplerParams & p5, int * p6, void * p7) { strata::metal_engine::missing("strata::kernels::sample_tokens"); } } +namespace strata::kernels { void scale_inplace(float * p0, int64_t p1, float p2, void * p3) { strata::metal_engine::missing("strata::kernels::scale_inplace"); } } +namespace strata::kernels { void scatter_rows_f32(const float * p0, float * p1, const int32_t * p2, int64_t p3, int64_t p4, void * p5) { strata::metal_engine::missing("strata::kernels::scatter_rows_f32"); } } +namespace strata::kernels { void shared_expert(const uint8_t * p0, const uint8_t * p1, const uint16_t * p2, const SForm & p3, const uint8_t * p4, const float * p5, const float * p6, const SForm & p7, const uint8_t * p8, const float * p9, const float * p10, const SForm & p11, const uint8_t * p12, const float * p13, const float * p14, const uint16_t * p15, float * p16, float * p17, int64_t p18, int64_t p19, int p20, void * p21, const float * p22, const NativeSharedWeights * p23) { strata::metal_engine::missing("strata::kernels::shared_expert"); } } +namespace strata::kernels { void shared_expert_multi(int p0, const float * p1, const uint16_t * p2, const NativeSharedWeights & p3, const uint16_t * p4, float * p5, float * p6, float * p7, float * p8, int64_t p9, int64_t p10, void * p11, const void * p12, int p13) { strata::metal_engine::missing("strata::kernels::shared_expert_multi"); } } +namespace strata::kernels { bool shared_expert_native_bf16_enabled() { strata::metal_engine::missing("strata::kernels::shared_expert_native_bf16_enabled"); return {}; } } +namespace strata::kernels { uint64_t shared_expert_scratch_bytes(int64_t p0) { strata::metal_engine::missing("strata::kernels::shared_expert_scratch_bytes"); return {}; } } +namespace strata::kernels { void shared_expert_set_native_bf16(bool p0) { strata::metal_engine::missing("strata::kernels::shared_expert_set_native_bf16"); } } +namespace strata::kernels { void silu_inplace(float * p0, int64_t p1, void * p2) { strata::metal_engine::missing("strata::kernels::silu_inplace"); } } +namespace strata::kernels { void spec_draft_sample(float * p0, int p1, const int32_t * p2, const int32_t * p3, int p4, const SamplerParams * p5, int32_t * p6, int p7, int p8, const int32_t * p9, void * p10, int32_t * p11, float * p12, int32_t * p13, int p14, float p15, void * p16) { strata::metal_engine::missing("strata::kernels::spec_draft_sample"); } } +namespace strata::kernels { void topk_512_step(const float * p0, const QsaShapes & p1, int64_t p2, const int32_t * p3, int32_t * p4, void * p5) { strata::metal_engine::missing("strata::kernels::topk_512_step"); } } +namespace strata::kernels { void wait_flag_ge(const uint32_t * p0, uint32_t p1, void * p2) { strata::metal_engine::missing("strata::kernels::wait_flag_ge"); } } +namespace strata::kernels { void wait_flag_ge_or(const uint32_t * p0, uint32_t p1, const uint32_t * p2, void * p3) { strata::metal_engine::missing("strata::kernels::wait_flag_ge_or"); } } +namespace strata::kernels { void window_ids(int32_t * p0, int p1, int p2, int32_t * p3, int64_t p4, void * p5) { strata::metal_engine::missing("strata::kernels::window_ids"); } } +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"); } +strata::prefill::Gemm::~Gemm() { strata::metal_engine::missing("strata::prefill::Gemm::~Gemm"); } +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"); } } +namespace strata::prefill { void copy_f32_wide(float * p0, const float * p1, int64_t p2, void * p3) { strata::metal_engine::missing("strata::prefill::copy_f32_wide"); } } +namespace strata::prefill { void copy_i32(int32_t * p0, const int32_t * p1, int64_t p2, void * p3) { strata::metal_engine::missing("strata::prefill::copy_i32"); } } +namespace strata::prefill { void f16_to_f32_wide(float * p0, const uint16_t * p1, int64_t p2, void * p3) { strata::metal_engine::missing("strata::prefill::f16_to_f32_wide"); } } +namespace strata::prefill { void gate_attn(const float * p0, const float * p1, uint16_t * p2, int64_t p3, void * p4, int64_t p5) { strata::metal_engine::missing("strata::prefill::gate_attn"); } } +namespace strata::prefill { void gather_rows16(const uint16_t * p0, const int32_t * p1, uint16_t * p2, int64_t p3, int64_t p4, void * p5) { strata::metal_engine::missing("strata::prefill::gather_rows16"); } } +namespace strata::prefill { void gdn_conv(float * p0, const float * p1, const float * p2, float * p3, int64_t p4, float p5, void * p6) { strata::metal_engine::missing("strata::prefill::gdn_conv"); } } +namespace strata::prefill { void gdn_gates(const float * p0, const float * p1, const float * p2, float * p3, float * p4, int64_t p5, void * p6) { strata::metal_engine::missing("strata::prefill::gdn_gates"); } } +namespace strata::prefill { void gdn_recurrence(float * p0, const float * p1, const float * p2, const float * p3, const float * p4, const float * p5, float p6, float * p7, uint16_t * p8, int64_t p9, void * p10, int64_t p11) { strata::metal_engine::missing("strata::prefill::gdn_recurrence"); } } +namespace strata::prefill { void gr_broadcast(const float * p0, float * p1, int64_t p2, void * p3) { strata::metal_engine::missing("strata::prefill::gr_broadcast"); } } +namespace strata::prefill { void gr_mix(const float * p0, const float * p1, float * p2, uint16_t * p3, int64_t p4, void * p5, uint16_t * p6, uint16_t * p7) { strata::metal_engine::missing("strata::prefill::gr_mix"); } } +namespace strata::prefill { void gr_mix_r(const float * p0, const float * p1, const float * p2, const float * p3, float * p4, uint16_t * p5, int64_t p6, void * p7, uint16_t * p8, uint16_t * p9) { strata::metal_engine::missing("strata::prefill::gr_mix_r"); } } +namespace strata::prefill { void gr_norm(const float * p0, const float * p1, float p2, float * p3, uint16_t * p4, int64_t p5, void * p6, uint16_t * p7) { strata::metal_engine::missing("strata::prefill::gr_norm"); } } +namespace strata::prefill { void gr_norm_rs(const float * p0, const float * p1, float p2, float * p3, uint16_t * p4, int64_t p5, void * p6, uint16_t * p7, int64_t p8) { strata::metal_engine::missing("strata::prefill::gr_norm_rs"); } } +namespace strata::prefill { void gr_silu(const float * p0, uint16_t * p1, int64_t p2, void * p3, uint16_t * p4) { strata::metal_engine::missing("strata::prefill::gr_silu"); } } +namespace strata::prefill { bool gr_upmix(const uint16_t * p0, const uint16_t * p1, const float * p2, const float * p3, const float * p4, float * p5, uint16_t * p6, uint16_t * p7, int64_t p8, void * p9) { strata::metal_engine::missing("strata::prefill::gr_upmix"); return {}; } } +namespace strata::prefill { void gr_write(float * p0, const float * p1, const float * p2, int64_t p3, int64_t p4, void * p5) { strata::metal_engine::missing("strata::prefill::gr_write"); } } +namespace strata::prefill { void gr_write_cvec_norm_rs(float * p0, const float * p1, const float * p2, int64_t p3, const float * p4, const float * p5, const int * p6, int p7, const float * p8, float p9, float * p10, uint16_t * p11, int64_t p12, void * p13, uint16_t * p14, int64_t p15) { strata::metal_engine::missing("strata::prefill::gr_write_cvec_norm_rs"); } } +namespace strata::prefill { void gr_write_norm_rs(float * p0, const float * p1, const float * p2, int64_t p3, const float * p4, float p5, float * p6, uint16_t * p7, int64_t p8, void * p9, uint16_t * p10, int64_t p11) { strata::metal_engine::missing("strata::prefill::gr_write_norm_rs"); } } +namespace strata::prefill { void kv_append(const float * p0, const float * p1, int64_t p2, int64_t p3, const int32_t * p4, int64_t p5, uint16_t * p6, uint16_t * p7, int8_t * p8, int8_t * p9, uint16_t * p10, uint16_t * p11, void * p12, const strata::kernels::KvHostPools * p13, const strata::kernels::KvHostPools * p14) { strata::metal_engine::missing("strata::prefill::kv_append"); } } +namespace strata::prefill { void moe_combine(const float * p0, const int32_t * p1, const float * p2, const float * p3, const float * p4, float * p5, int64_t p6, void * p7) { strata::metal_engine::missing("strata::prefill::moe_combine"); } } +namespace strata::prefill { void moe_combine_peer(const float * p0, const int32_t * p1, const float * p2, const float * p3, const float * p4, const float * p5, int64_t p6, float * p7, int64_t p8, void * p9) { strata::metal_engine::missing("strata::prefill::moe_combine_peer"); } } +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"); } } +namespace strata::prefill { void swiglu_pair(const float * p0, const float * p1, uint16_t * p2, int64_t p3, void * p4) { strata::metal_engine::missing("strata::prefill::swiglu_pair"); } } +namespace strata::prefill { void to_bf16(const float * p0, uint16_t * p1, int64_t p2, void * p3, uint16_t * p4) { strata::metal_engine::missing("strata::prefill::to_bf16"); } } +namespace strata::prefill { void to_f16(const float * p0, uint16_t * p1, int64_t p2, void * p3) { strata::metal_engine::missing("strata::prefill::to_f16"); } } diff --git a/metal/src/engine/window.cpp b/metal/src/engine/window.cpp new file mode 100644 index 00000000..4ba2d32f --- /dev/null +++ b/metal/src/engine/window.cpp @@ -0,0 +1,632 @@ +// metal/src/engine/window.cpp - the decode window on Metal: Verifier::init/run/commit's Metal side (the one backend +// hook, src/core/verify.cpp under STRATA_GPU_METAL). Everything above it - the decode loop, MTP control, the serve +// protocol, the PLE table reads, the file tier - is the shared host code. +// +// The window is mac-kern's chain (metal/kernels, tests/token_chain.cpp) on the model GGUF itself: shard 1 is mmapped +// and wrapped no-copy as one MTLBuffer, every weight is read where it lies (unified memory: no upload, no expert +// cache), and one window is encoded as a dependency graph in one command buffer. Its own state lives here (the GDN +// recurrence and conv history, the int8 K/V and the indexer per QSA layer, the PLE history): the engine's +// SessionState buffers are not used by it, except the PLE token window (ss.ple_prev) and table. +// +// First version (MAC-PLAN Phase 3 start): windows of one token (no MTP drafts yet: --mtp-max-t 1), greedy. The PLE +// block's projections run on the GPU; its norms, gate and conv (10240 values) on the CPU between two command buffers +// (layer 0 | layers 1..47 + head). +#include "gguf_lite.h" +#include "mtl_rt.h" +#include "ref_quant.h" + +#include "strata/core/layer.hpp" +#include "strata/core/session.hpp" +#include "strata/kernels/ngram.hpp" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace strata::metal_engine { + +namespace { + +constexpr int N = 2560, HC = 4, LR = 320, NFF = 640, TOPK = 10, NEXP = 512, MAXT = 8, NVOCAB_MAX = 248320; +constexpr int P_MAXE = 128, P_GEXP = 16, P_GSTART = P_GEXP + P_MAXE, P_ETOK = P_GSTART + P_MAXE + 4, + P_EW = P_ETOK + P_MAXE, P_TOKENT = P_EW + P_MAXE, P_INTS = P_TOKENT + P_MAXE, P_IGU = 4, P_IDN = 8; +struct GemvArgs { uint32_t n_in, n_out, n_tok, row_bytes, x_stride, y_stride; }; +struct GrArgs { uint32_t n_embd, hc, hc_lr, n_tok, apply; float eps; }; +struct MoeArgs { + uint64_t gate_off, up_off, down_off, gu_stride, d_stride; + uint32_t gu_row, d_row, n_embd, n_ff, n_tok, topk, n_expert, gx_gu, gx_dn, pad; +}; +struct GdnArgs { uint32_t h_k, h_v, channels, commit; float eps; uint32_t plane; }; +struct QsaArgs { uint32_t pos0, pos_base, max_cells, n_tok; float eps; }; +struct GlueArgs { uint32_t n, n_tok, n_embd, topk; }; +struct ArgArgs { uint32_t n_vocab, parts; }; + +int xkind(int t) { + switch (t) { + case ref::Q2_0: return 1; + case ref::Q4_K: case ref::Q5_K: return 2; + case ref::Q6_K: case ref::Q3_K: return 3; + default: return 0; + } +} + +// a prepared activation: 4 planes of MAXT rows of n, then the per-16 sums +struct Act { + mtl::Buffer b; + int n = 0; + size_t plane_bytes() const { return (size_t) 4 * MAXT * n; } +}; + +double now_ms() { return std::chrono::duration(std::chrono::steady_clock::now().time_since_epoch()).count(); } + +} // namespace + +class Window { +public: + bool init(const strata::core::ModelGeometry& g, strata::core::SessionState& ss, const std::string& shard1, int max_t, + 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 copy_logits(int t, float* host) const; + int64_t vocab() const { return n_vocab_; } + +private: + const gguf::Tensor& T(const std::string& name) const { + const gguf::Tensor* t = f_.find(name); + if (!t) { std::fprintf(stderr, "strata metal: the model has no tensor %s\n", name.c_str()); std::exit(2); } + return *t; + } + const mtl::Pipeline* P(int lib, const std::string& fn, const mtl::Constants& c, const std::string& key) { + auto it = pcache_.find(key); + if (it == pcache_.end()) it = pcache_.emplace(key, cx_->pipeline(lib, fn, c)).first; + return &it->second; + } + void push(std::vector& ds, mtl::Dispatch d) { + d.barrier = !nb_; + nb_ = false; + 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); + 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); + + strata::core::SessionState* ss_ = nullptr; + gguf::File f_; + std::unique_ptr cx_; + mtl::Buffer W_; + int lib_ = -1, lgdn_ = -1, lqsa_ = -1, lsmp_ = -1; + std::map pcache_; + bool nb_ = false; + int max_t_ = 1; + int64_t max_cells_ = 0; + int64_t n_vocab_ = 0; + float eps_ = 1e-6f; + // 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_; + 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 + std::vector ple_hist_; // NG_HIST x hc_dim normalized rows, row-fastest + bool ple_on_ = false; + int last_T_ = 0; + int32_t last_tokens_[MAXT] = {}; + std::vector last_norm_; // the window's normalized PLE rows (T x hc_dim), for commit +}; + +bool Window::init(const strata::core::ModelGeometry& g, strata::core::SessionState& ss, const std::string& shard1, + int max_t, int64_t max_cells, std::string& err) { + ss_ = &ss; + max_t_ = std::min(max_t, MAXT); + max_cells_ = max_cells; + if (g.n_embd != N || g.hc != HC || g.hc_lr != LR || g.n_ff != NFF || g.n_expert != NEXP || g.n_layers != 48) { + err = "metal window: the geometry differs from Qwen3.8-Flash-Next's"; + return false; + } + eps_ = 1e-6f; + const double t0 = now_ms(); + if (!f_.open(shard1, err)) return false; + cx_ = std::make_unique(); + madvise(f_.map, f_.map_bytes, MADV_WILLNEED); + W_ = cx_->wrap(f_.map, f_.map_bytes); + if (W_.handle == nullptr) { err = "metal window: cannot wrap " + shard1 + " as a Metal buffer"; return false; } + n_vocab_ = (int64_t) T("output.weight").shape[1]; + lib_ = cx_->compile({"quant.metal", "units.metal", "moe.metal", "gemv.metal", "gr.metal", "gr2.metal", "glue.metal"}); + 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"); + + const int MAXC = (int) std::max(4096, (max_cells + 64) / 4 * 4 + 64), MB = MAXC / 4 + 2; + max_cells_ = MAXC; + auto A = [&](size_t bytes) { return cx_->alloc(bytes); }; + R_ = A(4 * MAXT * HC * N); bo_ = A(4 * MAXT * N); inj_[0] = A(4 * MAXT * HC); inj_[1] = A(4 * MAXT * HC); + xn_ = A(4 * MAXT * HC * N); lo_ = A(4 * MAXT * LR); qkv_ = A(4 * MAXT * 10240); z_ = A(4 * MAXT * 6144); + al_ = A(4 * MAXT * 48); be_ = A(4 * MAXT * 48); yg_ = A(4 * MAXT * 6144); logits_ = A(4 * MAXT * 512); + ids_ = A(4 * MAXT * TOPK); wts_ = A(4 * MAXT * TOPK); plan_ = A(4 * P_INTS); h_ = A(4 * MAXT * TOPK * NFF); + ye_ = A(4 * MAXT * TOPK * N); gsh_ = A(4 * MAXT * NFF); ush_ = A(4 * MAXT * NFF); ysh_ = A(4 * MAXT * N); + sg_ = A(4 * MAXT * 4); qfull_ = A(4 * MAXT * 12288); kcur_ = A(4 * MAXT * 512); vcur_ = A(4 * MAXT * 512); + iqr_ = A(4 * MAXT * 512); ikr_ = A(4 * MAXT * 128); qcur_ = A(4 * MAXT * 24 * 256); qidx_ = A(4 * MAXT * 4 * 128); + scores_ = A(4 * (size_t) MAXT * MB); qids_ = A(4 * MAXT * 2051); part_ = A(4 * (size_t) MAXT * 65 * 24 * 258); + cs_ = A(8 * (size_t) (MAXC + 16) * 32); lm_ = A(4 * (size_t) MAXT * n_vocab_); apv_ = A(4 * MAXT * 120); + api_ = A(4 * MAXT * 120); tokout_ = A(4 * MAXT); emb_ = A(4 * MAXT * N); pkey_ = A(4 * MAXT * 10240); + pval_ = A(4 * MAXT * N); + 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 + const double base = f_.meta_num.count("qwen4exp.rope.freq_base") ? f_.meta_num["qwen4exp.rope.freq_base"] : 1e7; + for (int p = 0; p < MAXC + 16; ++p) + for (int i = 0; i < 32; ++i) { + const double th = (double) p * std::pow(base, -2.0 * i / 64.0); + cs_.as()[(p * 32 + i) * 2] = (float) std::cos(th); + cs_.as()[(p * 32 + i) * 2 + 1] = (float) std::sin(th); + } + for (int t = 0; t < MAXT; ++t) + for (int c = 0; c < 2051; ++c) qids_.as()[t * 2051 + c] = c; + for (int l = 0; l < 48; ++l) { + if (l % 4 != 3) { + 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)); + ksc_.push_back(A(2 * (size_t) MAXC * 2 * 4)); + vsc_.push_back(A(2 * (size_t) MAXC * 2 * 4)); + tail_.push_back(A(4 * 4 * 128)); + dead_.push_back(A(4 * 128)); + pooled_.push_back(A(4 * (size_t) MB * 128)); + } + } + 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); + std::fprintf(stderr, "strata metal: decode window on %s: %s wrapped (%.1f GB), %d cells, PLE %s, ready in %.0f ms\n", + cx_->device_name().c_str(), shard1.c_str(), f_.file_bytes / 1e9, MAXC, ple_on_ ? "on" : "OFF", + now_ms() - t0); + return true; +} + +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) { + 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; + if (n_in >= 6144) { L = 32; Rr = 1; S = 8; K = 2; } + else if (n_in == 640) { L = 8; Rr = 1; S = 4; K = 1; } + else if (n_out <= 640 || ty == ref::Q2_0 || ty == ref::BF16) { L = 32; Rr = 1; S = 4; K = 1; } + const std::string fn = "gemv_" + std::to_string(ty) + "_" + std::to_string(L) + "_" + std::to_string(Rr) + "_" + + std::to_string(S) + "_" + std::to_string(K); + const mtl::Pipeline* p = P(lib_, fn, mtl::Constants().i(0, ntok), fn + "#" + std::to_string(ntok)); + GemvArgs a{(uint32_t) n_in, (uint32_t) n_out, (uint32_t) ntok, (uint32_t) ref::row_bytes(t.type, n_in), (uint32_t) n_in, + (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)}; + const int rpt = (S / K) * (32 / L) * Rr; + d.grid[0] = (n_out + rpt - 1) / rpt; + d.tg[0] = S * 32; + push(ds, d); +} + +void Window::xprep(std::vector& ds, const mtl::Buffer& x, const Act& out, int n) { + mtl::Dispatch d; + d.p = P(lib_, "xprep", {}, "xprep"); + const uint32_t nn = n, nt = MAXT; + d.args = {mtl::Arg::B(x), mtl::Arg::B(out.b), mtl::Arg::V(nn), mtl::Arg::V(nt)}; + d.tg[0] = 256; + d.grid[0] = (n / 16 * MAXT + 255) / 256; + push(ds, d); +} + +// the hyper-connection read (gr.metal, 3 dispatches): the previous sublayer's write folded in when `apply` +void Window::gr_read(std::vector& ds, const std::string& pre, bool apply, const Act& out, bool final_, + int ntok) { + const gguf::Tensor& tn = T(pre + "_norm.weight"); + const gguf::Tensor& tdn = T(pre + "_down.weight"); + const gguf::Tensor& tup = T(pre + "_up.weight"); + const gguf::Tensor& tin = final_ ? tdn : T(pre + "_inject.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(R_), mtl::Arg::B(bo_), mtl::Arg::B(inj_[cur_inj_]), mtl::Arg::B(W_, tn.offset), 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(W_, tdn.offset), mtl::Arg::B(W_, tin.offset), 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)); + 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.grid[0] = N / 16; + d3.tg[0] = 256; + push(ds, d3); + cur_inj_ = 1 - cur_inj_; +} + +void Window::layer(std::vector& ds, int l, int ntok, int64_t pos0) { + const std::string b = "blk." + std::to_string(l) + "."; + // ---- the mixer: layer 1's read after the PLE is not applied again (its pending write was applied before it) + gr_read(ds, b + "hc_attn", l > 0 && !(l == 1 && ple_on_), XA_, false, ntok); + if (l % 4 != 3) { + const int gi = l - l / 4; + gemv(ds, T(b + "attn_qkv.weight"), XA_, qkv_, 10240, ntok); + nb_ = true; + gemv(ds, T(b + "attn_gate.weight"), XA_, z_, 6144, ntok); + nb_ = true; + gemv(ds, T(b + "ssm_alpha.weight"), XA_, al_, 48, ntok); + nb_ = true; + gemv(ds, T(b + "ssm_beta.weight"), XA_, be_, 48, ntok); + GdnArgs a{16, 48, 10240, 1, 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]; + d.args = {mtl::Arg::B(gst_[(size_t) gi]), mtl::Arg::B(ghist_[hp][(size_t) gi]), mtl::Arg::B(qkv_), mtl::Arg::B(al_), + mtl::Arg::B(be_), mtl::Arg::B(z_), mtl::Arg::B(W_, T(b + "ssm_conv1d.weight").offset), + mtl::Arg::B(W_, T(b + "ssm_dt.bias").offset), mtl::Arg::B(W_, T(b + "ssm_a").offset), + mtl::Arg::B(W_, T(b + "ssm_norm.weight").offset), mtl::Arg::B(yg_), mtl::Arg::V(a), + mtl::Arg::B(ghist_[1 - hp][(size_t) gi])}; + d.grid[0] = 48; + d.tg[0] = 512; + push(ds, d); + hpar_[(size_t) gi] = 1 - hp; // committed (T = 1 windows commit in the step) + xprep(ds, yg_, XY_, 6144); + gemv(ds, T(b + "ssm_out.weight"), XY_, bo_, N, ntok); + } else { + const int qi = l / 4; + gemv(ds, T(b + "attn_q.weight"), XA_, qfull_, 12288, ntok); + nb_ = true; + gemv(ds, T(b + "attn_k.weight"), XA_, kcur_, 512, ntok); + nb_ = true; + gemv(ds, T(b + "attn_v.weight"), XA_, vcur_, 512, ntok); + nb_ = true; + gemv(ds, T(b + "indexer.q_proj.weight"), XA_, iqr_, 512, ntok); + nb_ = true; + gemv(ds, T(b + "indexer.k_proj.weight"), XA_, ikr_, 128, ntok); + QsaArgs a{(uint32_t) pos0, 0, (uint32_t) max_cells_, (uint32_t) ntok, 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(iqr_), mtl::Arg::B(ikr_), + mtl::Arg::B(W_, T(b + "attn_q_norm.weight").offset), mtl::Arg::B(W_, T(b + "attn_k_norm.weight").offset), + mtl::Arg::B(W_, T(b + "indexer.q_norm.weight").offset), mtl::Arg::B(W_, T(b + "indexer.k_norm.weight").offset), + mtl::Arg::B(cs_), mtl::Arg::B(qcur_), mtl::Arg::B(qidx_), mtl::Arg::B(kq_[(size_t) qi]), + mtl::Arg::B(vq_[(size_t) qi]), mtl::Arg::B(ksc_[(size_t) qi]), mtl::Arg::B(vsc_[(size_t) qi]), + mtl::Arg::B(tail_[(size_t) qi]), mtl::Arg::B(dead_[(size_t) qi]), mtl::Arg::B(pooled_[(size_t) qi]), + mtl::Arg::V(a)}; + d.grid[0] = 33; + d.grid[1] = ntok; + d.tg[0] = 256; + push(ds, d); + 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_), + mtl::Arg::V(a)}; + s.grid[0] = (uint32_t) (((pos0 + ntok) / 4 + 1 + 7) / 8); + s.grid[1] = ntok; + s.tg[0] = 256; + push(ds, s); + 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); + 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]), + mtl::Arg::B(vsc_[(size_t) qi]), mtl::Arg::B(qids_), mtl::Arg::B(part_), mtl::Arg::V(a)}; + at.grid[0] = 33; + at.grid[1] = 2; + at.grid[2] = ntok; + at.tg[0] = 256; + push(ds, at); + mtl::Dispatch m; + m.p = P(lqsa_, "qsa_merge", {}, "qsa_merge"); + QsaArgs am = a; + am.n_tok = MAXT; // the plane stride + 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] = ntok; + m.tg[0] = 256; + push(ds, m); + gemv(ds, T(b + "attn_output.weight"), XQ_, bo_, N, ntok); + } + // ---- the MoE + gr_read(ds, b + "hc_ffn", true, XF_, false, ntok); + gemv(ds, T(b + "ffn_gate_inp.weight"), XF_, logits_, 512, ntok); + nb_ = true; + gemv(ds, T(b + "ffn_gate_shexp.weight"), XF_, gsh_, NFF, ntok); + nb_ = true; + gemv(ds, T(b + "ffn_up_shexp.weight"), XF_, ush_, NFF, ntok); + nb_ = true; + gemv(ds, T(b + "ffn_gate_inp_shexp.weight"), XF_, sg_, 1, ntok); + { + MoeArgs a{}; + const gguf::Tensor &tg_ = T(b + "ffn_gate_exps.weight"), &tu = T(b + "ffn_up_exps.weight"), + &td = T(b + "ffn_down_exps.weight"); + a.gate_off = tg_.offset; + a.up_off = tu.offset; + a.down_off = td.offset; + a.gu_row = (uint32_t) ref::row_bytes(tg_.type, N); + a.d_row = (uint32_t) ref::row_bytes(td.type, NFF); + a.gu_stride = (uint64_t) a.gu_row * NFF; + a.d_stride = (uint64_t) a.d_row * N; + a.n_embd = N; + a.n_ff = NFF; + a.n_tok = ntok; + 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] = ntok; + d.tg[0] = 16; + nb_ = true; + push(ds, d); + } + const std::string gun = "moe_gu_" + std::to_string(tg_.type) + "_" + std::to_string(gl) + "_" + std::to_string(gr) + + "_" + std::to_string(gs); + const std::string dnn = "moe_down_" + std::to_string(dq2 ? 142 : td.type) + "_" + std::to_string(dl) + "_" + + std::to_string(dr) + "_" + std::to_string(ds_); + mtl::Dispatch g; + g.p = P(lib_, gun, {}, gun); + g.args = {mtl::Arg::B(W_), mtl::Arg::B(XF_.b, XF_.plane_bytes() * xkind(tg_.type)), mtl::Arg::B(h_), mtl::Arg::B(plan_), + mtl::Arg::V(a), mtl::Arg::B(XF_.b, XF_.plane_bytes() * 4)}; + g.tg[0] = 32 * gs; + g.indirect = &plan_; + g.ind_off = P_IGU * 4; + push(ds, g); + nb_ = true; + gemv(ds, T(b + "ffn_down_shexp.weight"), XH_, ysh_, N, ntok); + mtl::Dispatch dd; + dd.p = P(lib_, dnn, {}, dnn); + dd.args = {mtl::Arg::B(W_), mtl::Arg::B(h_), mtl::Arg::B(ye_), mtl::Arg::B(plan_), mtl::Arg::V(a), mtl::Arg::B(h_)}; + dd.tg[0] = 32 * ds_; + dd.indirect = &plan_; + dd.ind_off = P_IDN * 4; + push(ds, dd); + } + { + GlueArgs a{N, (uint32_t) ntok, N, TOPK}; + mtl::Dispatch d; + d.p = P(lib_, "moe_combine_sh", {}, "moe_combine_sh"); + d.args = {mtl::Arg::B(ye_), mtl::Arg::B(plan_), mtl::Arg::B(ysh_), mtl::Arg::B(sg_), mtl::Arg::B(bo_), mtl::Arg::V(a)}; + d.grid[0] = (ntok * N + 255) / 256; + d.tg[0] = 256; + push(ds, d); + } +} + +namespace { +float bf16f(uint16_t h) { uint32_t i = (uint32_t) h << 16; float f; std::memcpy(&f, &i, 4); return f; } +// ple.cu's gnorm_kernel: per stream, the squares rounded in f32, summed in double +void gnorm(const float* x, const float* w, float* y, int n_embd, int hc, float eps) { + for (int c = 0; c < hc; ++c) { + const float* xc = x + (size_t) c * n_embd; + double acc = 0.0; + for (int d = 0; d < n_embd; ++d) { const float sq = xc[d] * xc[d]; acc += (double) sq; } + const float mean = (float) (acc / (double) n_embd); + const float scale = 1.0f / std::sqrt(mean + eps); + for (int d = 0; d < n_embd; ++d) y[(size_t) c * n_embd + d] = xc[d] * scale * w[(size_t) c * n_embd + d]; + } +} +} // namespace + +// The PLE block (ple.hpp) for the window's T rows on the CPU, after layer 0 (its pending write applied to R): R += +// gated + silu(conv); the normalized rows go to last_norm_ (the history advances at commit). +void Window::ple_cpu(int Tn) { + using namespace strata::kernels; + const int hc_dim = NG_HC_DIM; + const float* nkey = (const float*) ((const uint8_t*) f_.map + T("blk.1.ple_norm_key.weight").offset); + const float* nqry = (const float*) ((const uint8_t*) f_.map + T("blk.1.ple_norm_query.weight").offset); + const float* nconv = (const float*) ((const uint8_t*) f_.map + T("blk.1.ple_norm_conv.weight").offset); + const uint16_t* kw = (const uint16_t*) ((const uint8_t*) f_.map + T("blk.1.ple_conv1d.weight").offset); + std::vector key(hc_dim), query(hc_dim), gated(hc_dim), norm(hc_dim), hist = ple_hist_; + for (int t = 0; t < Tn; ++t) { + float* hidden = R_.as() + (size_t) t * HC * N; + gnorm(pkey_.as() + (size_t) t * hc_dim, nkey, key.data(), N, HC, NG_RMS_EPS); + gnorm(hidden, nqry, query.data(), N, HC, NG_RMS_EPS); + const float* value = pval_.as() + (size_t) t * N; + float gate[HC]; + const float inv_sqrt_n = 1.0f / std::sqrt((float) N); + for (int c = 0; c < HC; ++c) { + double acc = 0.0; + for (int d = 0; d < N; ++d) acc += (double) (key[(size_t) c * N + d] * query[(size_t) c * N + d]); + const float s = (float) acc * inv_sqrt_n; + const float mag = std::sqrt(std::max(std::fabs(s), 1e-6f)); + const float sgn = s > 0.0f ? 1.0f : (s < 0.0f ? -1.0f : 0.0f); + gate[c] = 1.0f / (1.0f + std::exp(-(sgn * mag))); + } + for (int i = 0; i < hc_dim; ++i) gated[(size_t) i] = value[i % N] * gate[i / N]; + gnorm(gated.data(), nconv, norm.data(), N, HC, NG_RMS_EPS); + for (int c = 0; c < hc_dim; ++c) { + float acc = 0.0f; + for (int k = 0; k < PLE_CONV_KERNEL; ++k) { + const int row = NG_HIST - (PLE_CONV_KERNEL - 1 - k) * NGRAM_SIZE; + const float v = row == NG_HIST ? norm[(size_t) c] : hist[(size_t) row + (size_t) NG_HIST * c]; + acc += ref::f16_to_f32(kw[k + PLE_CONV_KERNEL * c]) * v; + } + const float conv = acc / (1.0f + std::exp(-acc)); + hidden[c] = hidden[c] + gated[(size_t) c] + conv; + } + std::memcpy(last_norm_.data() + (size_t) t * hc_dim, norm.data(), (size_t) hc_dim * 4); + // the next row of this window sees this one in its history + for (int c = 0; c < hc_dim; ++c) { + float* col = hist.data() + (size_t) c * NG_HIST; + for (int r = 0; r + 1 < NG_HIST; ++r) col[r] = col[r + 1]; + col[NG_HIST - 1] = norm[(size_t) c]; + } + } +} + +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) { err = "metal window: only one-token windows so far (run with --mtp-max-t 1)"; 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 + const gguf::Tensor& te = T("token_embd.weight"); + const size_t rb = ref::row_bytes(te.type, N); + for (int t = 0; t < Tn; ++t) { + float* r = R_.as() + (size_t) t * HC * N; + 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; + if (ple_on_) { + strata::core::SessionState& ss = *ss_; + uint32_t rows[MAXT * strata::kernels::PLE_N_HEADS]; + int32_t prev[2] = {ss.ple_prev[0], ss.ple_prev[1]}; + for (int t = 0; t < Tn; ++t) { + strata::kernels::ngram_rows(&tokens[t], prev, 1, ss.ple.consts, rows + t * strata::kernels::PLE_N_HEADS); + prev[0] = prev[1]; + prev[1] = tokens[t]; + } + if (!ss.ple.table->gather_batch(rows, (size_t) Tn, emb_.as(), err)) return false; + xprep(ds, emb_, XE_, N); + gemv(ds, T("blk.1.ple_key.weight"), XE_, pkey_, 10240, Tn); + nb_ = true; + gemv(ds, T("blk.1.ple_value.weight"), XE_, pval_, N, Tn); + } + cur_inj_ = 0; + layer(ds, 0, Tn, pos0); + if (ple_on_) { + // layer 0's FFN write applied to R (gr_norm with apply writes R back; its xn is not used) + const gguf::Tensor& tn = T("blk.1.hc_attn_norm.weight"); + GrArgs a{N, HC, LR, (uint32_t) Tn, 1u, eps_}; + mtl::Dispatch d1; + d1.p = P(lib_, "gr_norm", {}, "gr_norm"); + d1.args = {mtl::Arg::B(R_), mtl::Arg::B(bo_), mtl::Arg::B(inj_[cur_inj_]), mtl::Arg::B(W_, tn.offset), mtl::Arg::B(xn_), + mtl::Arg::V(a)}; + d1.grid[0] = HC; + d1.grid[1] = Tn; + d1.tg[0] = 256; + push(ds, d1); + cx_->run_graph(ds); + ple_cpu(Tn); + ds.clear(); + nb_ = false; + } + 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); + { + ArgArgs a{(uint32_t) n_vocab_, 120}; + mtl::Dispatch d1, d2; + d1.p = P(lsmp_, "argmax_part", {}, "argmax_part"); + d1.args = {mtl::Arg::B(lm_), mtl::Arg::B(apv_), mtl::Arg::B(api_), mtl::Arg::V(a)}; + d1.grid[0] = 120; + d1.grid[1] = Tn; + 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] = Tn; + d2.tg[0] = 256; + push(ds, d2); + } + cx_->run_graph(ds); + for (int t = 0; t < Tn; ++t) out[t] = tokout_.as()[t]; + 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) { + // 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; + for (int i = 0; i < n_vocab_; ++i) { + 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); + } + return true; +} + +bool Window::commit(int n_keep, std::string& err) { + if (n_keep != last_T_) { err = "metal window: partial commits come with the MTP windows"; return false; } + using namespace strata::kernels; + 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; + for (int c = 0; c < NG_HC_DIM; ++c) { + float* col = ple_hist_.data() + (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]; + } + } + ss.ple_prev[0] = ss.ple_prev[1]; + ss.ple_prev[1] = last_tokens_[t]; + } + 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); + return true; +} + +// ---------------------------------------------------------------------------------------------- the hook's entry points +namespace { +std::string g_shard1; +} +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; } +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(); + return true; +} +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); } + +} // namespace strata::metal_engine diff --git a/metal/tools/chat_tokens.py b/metal/tools/chat_tokens.py new file mode 100644 index 00000000..8bbf2c11 --- /dev/null +++ b/metal/tools/chat_tokens.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python3 +"""chat_tokens.py REPO TOKENIZER_DIR "question" : the token ids of a one-turn chat prompt (thinking off), comma +separated, for `strata generate --tokens` (the same template as the gates' rbench.py). +With --decode IDS instead of the question: the text of comma-separated ids.""" +import json, sys +from pathlib import Path + +repo, tdir = Path(sys.argv[1]), Path(sys.argv[2]) +sys.path.insert(0, str(repo / "tools")) +import strata_tokenizer as ST + +vocab = json.loads((tdir / "vocab.json").read_text(encoding="utf-8")) +toks = [None] * len(vocab) +for t, i in vocab.items(): + toks[i] = t +tok = ST.Tokenizer(toks, (tdir / "merges.txt").read_text(encoding="utf-8").split("\n"), + json.loads((tdir / "token_type.json").read_text())) +if sys.argv[3] == "--decode": + print(tok.decode([int(x) for x in sys.argv[4].replace(" ", "").split(",") if x])) +else: + ids = tok.encode("<|im_start|>user\n%s<|im_end|>\n<|im_start|>assistant\n\n\n\n\n" % sys.argv[3], + parse_special=True) + print(",".join(str(i) for i in ids)) diff --git a/metal/tools/gen_stubs.py b/metal/tools/gen_stubs.py new file mode 100644 index 00000000..f70ea34d --- /dev/null +++ b/metal/tools/gen_stubs.py @@ -0,0 +1,184 @@ +#!/usr/bin/env python3 +"""gen_stubs.py LINK_LOG OUT.cpp CLANG_ARGS... : stubs for the kernel-library entry points the Metal engine leaves undefined. + +LINK_LOG is the output of a failed link of `strata` (ld64 prints each undefined symbol demangled). The declarations +come from the engine's own headers through clang's AST dump (-ast-dump-filter), so every stub has the header's exact +types; it is defined in the declaring namespace (free functions) or out of line (members, trailing return type), and +calls strata::metal_engine::missing("name") (metal/src/engine/missing.cpp). Entry points that have a Metal +implementation are defined in metal/src/engine/*.cpp and are therefore not undefined: they never get a stub. +""" +import os, re, subprocess, sys, tempfile + +log, out = sys.argv[1], sys.argv[2] +clang_args = sys.argv[3:] +root = None +for a in clang_args: + if a.startswith("-I") and a.endswith("/include") and os.path.isdir(os.path.join(a[2:], "strata")): + root = a[2:] +assert root, "need -I/include among the clang args" + +# ---- the undefined symbols (demangled), from ld64's report +undef = [] +for m in re.finditer(r'^\s+"(.+)", referenced from:', open(log, errors="replace").read(), re.M): + undef.append(m.group(1)) +undef = sorted(set(undef)) + +def split_top(s): + out, d, cur = [], 0, "" + for ch in s: + if ch in "(<[": d += 1 + if ch in ")>]": d -= 1 + if ch == "," and d == 0: + out.append(cur.strip()); cur = "" + else: + cur += ch + if cur.strip(): out.append(cur.strip()) + return out + +def parse_demangled(s): + """'ns::f(int, float*) const' -> ('ns::f', nparams)""" + s = re.sub(r"\s+const$", "", s) + d, i = 0, len(s) - 1 + while i >= 0: + if s[i] == ")": d += 1 + elif s[i] == "(": + d -= 1 + if d == 0: break + i -= 1 + name, params = s[:i], s[i + 1:-1] + p = split_top(params) + if p == ["void"]: p = [] + return name, len(p) + +wanted = {} +skipped = [] +for u in undef: + if "(" not in u or u.startswith("_") or "::" not in u: + skipped.append(u); continue + n, k = parse_demangled(u) + wanted.setdefault(n, set()).add(k) + +# ---- declarations from the headers +hdrs = [] +for d in ["kernels", "kernels/cpu", "prefill", "core", "platform", "ngram", "spec"]: + p = os.path.join(root, "strata", d) + if os.path.isdir(p): + hdrs += sorted("strata/%s/%s" % (d, f) for f in os.listdir(p) if f.endswith(".hpp")) +def compiles(h): + f = tempfile.NamedTemporaryFile("w", suffix=".cpp", delete=False) + f.write('#include "%s"\n' % h); f.close() + r = subprocess.run(["clang++", "-fsyntax-only"] + clang_args + [f.name], capture_output=True, text=True) + os.unlink(f.name) + return r.returncode == 0 +bad = [h for h in hdrs if not compiles(h)] +hdrs = [h for h in hdrs if h not in bad] +for h in bad: print("HEADER-SKIPPED", h) +tu = tempfile.NamedTemporaryFile("w", suffix=".cpp", delete=False) +for h in hdrs: tu.write('#include "%s"\n' % h) +tu.close() +decls = {} # qualname -> list of (kind, type_str, params[(name,type)], is_const, is_static, file) +cmd = ["clang++", "-fsyntax-only", "-Xclang", "-ast-dump", "-Xclang", "-ast-dump-filter=strata", "-fno-color-diagnostics"] + clang_args + [tu.name] +r = subprocess.run(cmd, capture_output=True, text=True) +if r.returncode != 0: sys.stderr.write(r.stderr[-4000:]) +# the AST dump is a tree ("|-", "`-", "| " prefixes, two characters per level): walk it, keeping the enclosing +# namespaces and records, and collect every function declaration that has no body +FN = ("FunctionDecl", "CXXMethodDecl", "CXXConstructorDecl", "CXXDestructorDecl") +stack = [] # (depth, kind, name) +cur = None # the function being read: [qual, kind, type, params, depth, has_body, rest] +def flush(): + global cur + if cur and not cur[5] and cur[0] in wanted: + q, kind, ty, params, d, hb, rest = cur + decls.setdefault(q, []).append((kind, ty, params, bool(re.search(r"\)\s*const\s*(noexcept)?\s*$", ty)), " static" in rest, "")) + cur = None +for line in r.stdout.splitlines(): + if line.startswith("Dumping "): flush(); stack = []; continue + m = re.match(r"^([| `-]*)(\w+) ", line) + if not m: continue + depth = len(m.group(1)) // 2 + kind = m.group(2) + if cur and depth <= cur[4]: flush() + while stack and stack[-1][0] >= depth: stack.pop() + if cur: + if kind == "CompoundStmt" and depth == cur[4] + 1: cur[5] = True + if kind == "ParmVarDecl" and depth == cur[4] + 1: + pm = re.search(r"(?:col|line):[\d:]+(?: used| referenced)?(?: (\w+))? '([^']*)'(?::'[^']*')?", line) + if pm: cur[3].append((pm.group(1) or "", pm.group(2))) + continue + if kind == "NamespaceDecl": + nm = re.search(r"> (?:prev 0x\w+ )?(?:line|col):[\d:]+ (\w+)", line) + stack.append((depth, kind, nm.group(1) if nm else "(anonymous)")) + elif kind in ("CXXRecordDecl", "ClassTemplateDecl"): + nm = re.search(r"(?:class|struct|union) (\w+)", line) + if " implicit " in line or not nm: stack.append((depth, kind, "(implicit)")) + else: stack.append((depth, kind, nm.group(1))) + elif kind in FN and " implicit " not in line: + fm = re.findall(r"(~?\w+) '([^']*)'(?::'[^']*')?", line) + if not fm: continue + name, ty = fm[-1] + rest = line[line.rfind("'") + 1:] + names = [s[2] for s in stack] + if any(n.startswith("(") for n in names) or any(s[1] == "ClassTemplateDecl" for s in stack): continue + cur = [("::".join(names + [name])), kind, ty, [], depth, False, rest] + elif kind in ("FunctionTemplateDecl",): + stack.append((depth, kind, "(template)")) +flush() + +def nx(ty): + return " noexcept" if re.search(r"\)\s*(const\s*)?noexcept\s*$", ty) else "" + +def ret_stmt(r): + if r == "void": return "" + if r.endswith("&"): # a reference: to a zero-initialised object of its own + return " static std::remove_cv_t> s{}; return s;" % r + return " return {};" + +def ret_of(ty): + d, i = 0, len(ty) - 1 + # the function type is "R (P...)" [ const]; find the '(' that opens the parameter list + s = re.sub(r"\)\s*(const)?\s*(noexcept)?\s*$", ")", ty) + i = len(s) - 1; d = 0 + while i >= 0: + if s[i] == ")": d += 1 + elif s[i] == "(": + d -= 1 + if d == 0: break + i -= 1 + return s[:i].strip() + +lines = ['// metal/src/engine/stubs.cpp - GENERATED by metal/tools/gen_stubs.py: do not edit.', + '// The kernel-library entry points with no Metal implementation yet: each stops the engine with its name', + '// (metal/src/engine/missing.cpp). Regenerate after porting one.', ''] +lines += ['#include "%s"' % h for h in hdrs] +lines += ['#include ', '', 'namespace strata::metal_engine { void missing(const char* name); }', ''] +done, unmatched = 0, [] +for qn in sorted(wanted): + ds = decls.get(qn, []) + ds = [d for d in ds if len(d[2]) in wanted[qn]] + if not ds: + unmatched.append(qn); continue + seen = set() + for kind, ty, params, is_const, is_static, loc in ds: + key = (ty, kind) + if key in seen: continue + seen.add(key) + ps = ", ".join("%s p%d" % (t, k) if "(*)" not in t else t.replace("(*)", "(*p%d)" % k) for k, (n, t) in enumerate(params)) + if kind == "FunctionDecl": + ns, fn = qn.rsplit("::", 1) + r = ret_of(ty) + body = 'strata::metal_engine::missing("%s");' % qn + body += ret_stmt(r) + lines.append("namespace %s { %s %s(%s)%s { %s } }" % (ns, r, fn, ps, nx(ty), body)) + elif kind == "CXXMethodDecl": + r = ret_of(ty) + body = 'strata::metal_engine::missing("%s");' % qn + body += ret_stmt(r) + lines.append("auto %s(%s)%s%s -> %s { %s }" % (qn, ps, " const" if is_const else "", nx(ty), r, body)) + elif kind in ("CXXConstructorDecl", "CXXDestructorDecl"): + lines.append('%s(%s) { strata::metal_engine::missing("%s"); }' % (qn, ps, qn)) + done += 1 +open(out, "w").write("\n".join(lines) + "\n") +print("undefined %d, stubs %d, unmatched %d, skipped %d" % (len(undef), done, len(unmatched), len(skipped))) +for u in unmatched: print("UNMATCHED", u) +for u in skipped: print("SKIPPED", u) +os.unlink(tu.name) diff --git a/metal/tools/gguf_list.cpp b/metal/tools/gguf_list.cpp new file mode 100644 index 00000000..1376170e --- /dev/null +++ b/metal/tools/gguf_list.cpp @@ -0,0 +1,25 @@ +// metal/tools/gguf_list.cpp - tensor directory and scalar metadata of a GGUF (name type shape offset), for the +// Metal window's wiring. clang++ -std=c++20 -I include metal/tools/gguf_list.cpp -o gguf_list +#include "strata/artifact/gguf_reader.hpp" + +#include +#include + +int main(int argc, char** argv) { + if (argc < 2) { std::printf("usage: gguf_list [substring]\n"); return 2; } + strata::GgufFile g(argv[1]); + const std::string f = argc > 2 ? argv[2] : ""; + for (const auto& [k, v] : g.metadata()) { + if (!f.empty() && k.find(f) == std::string::npos) continue; + if (v.is_num()) std::printf("META %s = %g\n", k.c_str(), v.num()); + else if (v.type == strata::MetaType::STRING) std::printf("META %s = %s\n", k.c_str(), v.s.substr(0, 80).c_str()); + else std::printf("META %s = [%llu items]\n", k.c_str(), (unsigned long long) v.count); + } + for (const auto& t : g.tensors()) { + if (!f.empty() && t.name.find(f) == std::string::npos) continue; + std::printf("%s %s [", t.name.c_str(), t.type_name()); + for (size_t i = 0; i < t.shape.size(); ++i) std::printf(i ? ",%llu" : "%llu", (unsigned long long) t.shape[i]); + std::printf("] off %llu\n", (unsigned long long) t.offset); + } + return 0; +} diff --git a/src/core/device.cu b/src/core/device.cu index a614f248..34bc679d 100644 --- a/src/core/device.cu +++ b/src/core/device.cu @@ -19,10 +19,12 @@ void check(cudaError_t e, const char* what) { // A NaN pattern, not zero. Zeros read from uninitialised memory are indistinguishable from real zeros in a // dequantized weight or a masked attention score, which is exactly the kind of wrong-but-plausible value the // Phase 1 harnesses kept catching. +#if !defined(STRATA_GPU_METAL) __global__ void poison_kernel(float* p, uint64_t n_floats) { const uint64_t i = (uint64_t) blockIdx.x * blockDim.x + threadIdx.x; if (i < n_floats) p[i] = __int_as_float(0x7fc00000); } +#endif #if defined(STRATA_USE_HIP) #if !defined(STRATA_HIP_ARCHS) @@ -232,7 +234,7 @@ std::string gpu_arch_problem(int ordinal) { } std::string device_code_error() { -#if defined(STRATA_USE_HIP) || defined(STRATA_HIP_GFX906) +#if defined(STRATA_USE_HIP) || defined(STRATA_HIP_GFX906) || defined(STRATA_GPU_METAL) return ""; // gpu_arch_problem() checks the HIP architectures against STRATA_HIP_ARCHS, before this point #else // every .cu of the engine is compiled for the same CMAKE_CUDA_ARCHITECTURES, so this kernel stands for all @@ -322,6 +324,12 @@ DeviceArena::DeviceArena(uint64_t bytes, int ordinal, bool poison) // One allocation for the whole region. cudaMalloc of a large block is the thing that can fail late, so it // happens once, here, before anything depends on it. check(cudaMalloc(&base_, (size_t) bytes), "cudaMalloc"); +#if defined(STRATA_GPU_METAL) // unified memory: the CPU writes the pattern through the allocation's CPU view + if (poison_) { + uint32_t* h = (uint32_t*) strata::gpu::host_view(base_); + for (uint64_t i = 0; i < bytes / sizeof(float); ++i) h[i] = 0x7fc00000u; + } +#else if (poison_) { const int threads = 256; const uint64_t n = bytes / sizeof(float); @@ -336,6 +344,7 @@ DeviceArena::DeviceArena(uint64_t bytes, int ordinal, bool poison) } check(cudaDeviceSynchronize(), "poison sync"); } +#endif } DeviceArena::~DeviceArena() { diff --git a/src/core/verify.cpp b/src/core/verify.cpp index 5757a72f..ca9876f5 100644 --- a/src/core/verify.cpp +++ b/src/core/verify.cpp @@ -60,6 +60,19 @@ #include #include +#if defined(STRATA_GPU_METAL) +// The Metal backend's decode window (metal/src/engine/window.cpp): this file's init/run/commit hand over to it. +namespace strata::metal_engine { +void window_destroy(void* w); +void* window_create(); +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); +bool window_run(void* w, int T, const int32_t* tokens, int64_t pos0, bool head, int32_t* out, std::string& err); +bool window_commit(void* w, int n_keep, std::string& err); +bool window_copy_logits(void* w, int t, float* host); +} // namespace strata::metal_engine +#endif + namespace strata::core { namespace { @@ -474,7 +487,12 @@ void Verifier::diag(std::FILE* f) const { trace_dump(f); // #649: STRATA_VERIFY_TRACE=1 only } + Verifier::~Verifier() { +#if defined(STRATA_GPU_METAL) + strata::metal_engine::window_destroy(metal_); + metal_ = nullptr; +#endif const Verifier* self = this; g_diag_verifier.compare_exchange_strong(self, nullptr); for (auto& slot : g_live) { @@ -557,6 +575,11 @@ bool Verifier::init(const WeightTable& wt, const ModelGeometry& g, SessionState& max_t_ = max_t; sampling_.greedy = true; // a fresh verifier samples greedily until set_sampling says otherwise sampling_.temperature = 0.0f; +#if defined(STRATA_GPU_METAL) + if (metal_ == nullptr) metal_ = strata::metal_engine::window_create(); + return strata::metal_engine::window_init(metal_, g, ss, max_t, ss.qsa_states[ss.qsa_primary()].max_cells, &n_vocab_, + err); +#endif if (max_t < 2 || max_t > strata::kernels::kVerifyMaxT || max_t > strata::kernels::cpu::MAXT) { err = "verify: the window must hold 2.." + std::to_string(strata::kernels::kVerifyMaxT) + " tokens"; return false; @@ -2135,6 +2158,11 @@ bool Verifier::adapt_gate_wait(int32_t layer, std::string& err) { bool Verifier::run(int T, const int32_t* tokens, int64_t pos0, PoolMultiFn pool, void* user, int32_t* out, std::string& err) { +#if defined(STRATA_GPU_METAL) + (void) pool; + (void) user; + return strata::metal_engine::window_run(metal_, T, tokens, pos0, head_sampling_, out, err); +#endif using namespace strata::kernels; const OnDevice on_device(device_); last_batch_ = false; @@ -2495,6 +2523,9 @@ namespace { bool g_commit_async = false; } void Verifier::set_commit_async(bool on) { g_commit_async = on && std::getenv("STRATA_COMMIT_SYNC") == nullptr; } bool Verifier::commit(int n_keep, std::string& err) { +#if defined(STRATA_GPU_METAL) + return strata::metal_engine::window_commit(metal_, n_keep, err); +#endif const OnDevice on_device(device_); if (n_keep < 1 || n_keep > last_t_) { err = "verify: commit count out of range"; return false; } const Clock::time_point t0 = Clock::now(); @@ -2530,6 +2561,10 @@ bool Verifier::commit(int n_keep, std::string& err) { } bool Verifier::wait_commit(std::string& err) { +#if defined(STRATA_GPU_METAL) + (void) err; + return true; +#endif if (commit_pending_) { const OnDevice on_device(device_); commit_pending_ = false; @@ -3155,6 +3190,9 @@ void Verifier::note_nonfinite(int T, const int64_t* pos_b) { } bool Verifier::copy_logits(int t, float* host) const { +#if defined(STRATA_GPU_METAL) + return strata::metal_engine::window_copy_logits(metal_, t, host); +#endif if (next_ != nullptr) return next_->copy_logits(t, host); // a layer split: the head is on the last stage if (head_logits_ == nullptr || host == nullptr || t < 0 || n_vocab_ <= 0) return false; return cudaMemcpy(host, head_logits_ + (size_t) t * (size_t) n_vocab_, (size_t) n_vocab_ * sizeof(float), diff --git a/src/kernels/cpu/expert_layout.cpp b/src/kernels/cpu/expert_layout.cpp index d8adb260..001fa5d4 100644 --- a/src/kernels/cpu/expert_layout.cpp +++ b/src/kernels/cpu/expert_layout.cpp @@ -60,7 +60,9 @@ bool cpu_avx512_ok() { const unsigned long long xcr0 = _xgetbv(0); #else unsigned lo = 0, hi = 0; +#if !defined(__aarch64__) // arm64 (the Metal build): no XCR0; cpuid reads zero there, so this is never reached __asm__ volatile("xgetbv" : "=a"(lo), "=d"(hi) : "c"(0)); +#endif const unsigned long long xcr0 = ((unsigned long long) hi << 32) | lo; #endif if ((xcr0 & 0xE6) != 0xE6) return false; // the OS saves the AVX-512 state @@ -92,7 +94,9 @@ bool cpu_avx512bw_ok() { const unsigned long long xcr0 = _xgetbv(0); #else unsigned lo = 0, hi = 0; +#if !defined(__aarch64__) // arm64 (the Metal build): no XCR0; cpuid reads zero there, so this is never reached __asm__ volatile("xgetbv" : "=a"(lo), "=d"(hi) : "c"(0)); +#endif const unsigned long long xcr0 = ((unsigned long long) hi << 32) | lo; #endif if ((xcr0 & 0xE6) != 0xE6) return false; // the OS saves the AVX-512 state @@ -126,7 +130,9 @@ bool cpu_avx2_ok() { const unsigned long long xcr0 = _xgetbv(0); #else unsigned lo = 0, hi = 0; +#if !defined(__aarch64__) // arm64 (the Metal build): no XCR0; cpuid reads zero there, so this is never reached __asm__ volatile("xgetbv" : "=a"(lo), "=d"(hi) : "c"(0)); +#endif const unsigned long long xcr0 = ((unsigned long long) hi << 32) | lo; #endif if ((xcr0 & 0x6) != 0x6) return false; // the OS saves the SSE and AVX state @@ -154,7 +160,9 @@ bool cpu_avx1_ok() { const unsigned long long xcr0 = _xgetbv(0); #else unsigned lo = 0, hi = 0; +#if !defined(__aarch64__) // arm64 (the Metal build): no XCR0; cpuid reads zero there, so this is never reached __asm__ volatile("xgetbv" : "=a"(lo), "=d"(hi) : "c"(0)); +#endif const unsigned long long xcr0 = ((unsigned long long) hi << 32) | lo; #endif return (xcr0 & 0x6) == 0x6; diff --git a/src/kernels/cpu/pool_affinity_linux.hpp b/src/kernels/cpu/pool_affinity_linux.hpp index 0791ac5c..bb84420c 100644 --- a/src/kernels/cpu/pool_affinity_linux.hpp +++ b/src/kernels/cpu/pool_affinity_linux.hpp @@ -65,5 +65,23 @@ inline int pin_thread_to_cpu(int core) { return pthread_setaffinity_np(pthread_self(), target.bytes, target.set); } +} // namespace strata::kernels::cpu::detail +#elif defined(__APPLE__) +#include +#include + +// macOS has no thread-to-core pinning (the scheduler places threads; affinity tags are hints at most): every CPU is +// allowed and a pin is a no-op, so the pool keeps its threads where the OS puts them. +namespace strata::kernels::cpu::detail { +inline int get_thread_affinity(std::vector& mask, std::vector* cpus = nullptr) { + mask.assign(1, ~0ul); + if (cpus) { + cpus->clear(); + for (unsigned i = 0; i < std::thread::hardware_concurrency(); ++i) cpus->push_back((int) i); + } + return 0; +} +inline int set_thread_affinity(const std::vector&) { return 0; } +inline int pin_thread_to_cpu(int) { return 0; } // nothing to do: the OS places the thread } // namespace strata::kernels::cpu::detail #endif diff --git a/src/program/generate.cpp b/src/program/generate.cpp index 649cdbdf..511b7960 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -98,6 +98,9 @@ #endif #include +#if defined(STRATA_GPU_METAL) +namespace strata::metal_engine { void set_model_shard(const char* shard1); } // metal/src/engine/window.cpp +#endif #include #include @@ -2680,7 +2683,12 @@ int main(int argc, char** argv) { // The experimental older-CPU build (STRATA_ISA_FLOOR=avx|none, compiled on that PC; #394 #595 #623) has ggml-cpu // for that floor, so a native pack's experts run there on ggml-cpu (every AVX2 kernel is behind cpu_avx2_ok). const char* isa_floor = strata::kernels::cpu::isa_floor_build(); +#if defined(STRATA_GPU_METAL) // Apple Silicon: every expert is on the GPU; ggml-cpu (NEON) is the only CPU expert code + strata::metal_engine::set_model_shard(o.native_preset.c_str()); // the decode window reads the model GGUF itself + if (false) { +#else if (!strata::kernels::cpu::cpu_avx2_ok() && isa_floor[0] == '\0') { +#endif std::fprintf(stderr, "strata generate: this CPU (%s) does not support AVX2 with FMA and F16C, which this engine's " "CPU expert kernels need; Strata runs on Intel Haswell (2013), AMD Zen (2017) or newer. " "Older CPUs are EXPERIMENTAL and slow: setup compiles an engine for them on this PC " @@ -2909,8 +2917,12 @@ 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; @@ -12890,7 +12902,11 @@ 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()); @@ -13087,6 +13103,27 @@ 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);