Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.
C++
metal: find the kernel sources, ggml-common.h and default tables beside the executable first, so an engine package can move away from the source tree
Niko1221dc1bc972ba88
diff --git a/metal/src/engine/window.cpp b/metal/src/engine/window.cppindex 9a6d1d325..18cfdd870 100644--- a/metal/src/engine/window.cpp+++ b/metal/src/engine/window.cpp@@ -13,6 +13,7 @@// (layer 0 | layers 1..47 + head).#include "gguf_lite.h"#include "mtl_rt.h"+#include "resource_path.h"#include "ref_quant.h"#include "arena_budget.h"#include "stream.h"@@ -441,7 +442,7 @@ bool Window::init(const strata::core::ModelGeometry& g, strata::core::SessionStalgdn_ = cx_->compile({"gdn.metal"}, "#define NO_GGML_COMMON 1\n");lqsa_ = cx_->compile({"qsa.metal", "qsa2.metal"}, "#define NO_GGML_COMMON 1\n");lsmp_ = cx_->compile({"sample.metal"}, "#define NO_GGML_COMMON 1\n");- lple_ = cx_->compile({std::string(STRATA_METAL_ENGINE_KERNELS_DEFAULT) + "/ple.metal"}, "#define NO_GGML_COMMON 1\n");+ lple_ = cx_->compile({strata_metal::resource("STRATA_METAL_ENGINE_KERNELS", "engine-msl", STRATA_METAL_ENGINE_KERNELS_DEFAULT) + "/ple.metal"}, "#define NO_GGML_COMMON 1\n");const int MAXC = (int) std::max<int64_t>(4096, (max_cells + 64) / 4 * 4 + 64), MB = MAXC / 4 + 2;max_cells_ = MAXC;@@ -1175,7 +1176,7 @@ bool Window::stream_arena(std::string& err) {}std::string prof;if (const char* e = std::getenv("STRATA_METAL_EXPERT_PROFILE")) prof = e;- else prof = std::string(STRATA_METAL_DATA_DEFAULT) + "/expert-profile.bin";+ else prof = strata_metal::resource(nullptr, "data", STRATA_METAL_DATA_DEFAULT) + "/expert-profile.bin";std::fprintf(stderr, "strata metal stream: %.2f GB allocated before the arena (the process holds %.2f GB, %.2f GB resident); ""RAM %.1f GB (%.1f GB available, %.1f GB kept for macOS), GPU working set %.1f GB\n", used / gb,pm0.footprint / gb, pm0.rss / gb, phys / gb, avail / gb, keep / gb, cx_->recommended_working_set() / gb);@@ -1503,7 +1504,7 @@ bool Window::mtp_load(const std::string& gguf_path, int max_t, std::string& err)float* dst = (float*) (mnorm_.as<uint8_t>() + kv.second);for (uint64_t i = 0; i < x.bytes / 4; ++i) dst[i] = src[i] + 1.0f;}- lmtp_ = cx_->compile({std::string(STRATA_METAL_ENGINE_KERNELS_DEFAULT) + "/mtp.metal"}, "#define NO_GGML_COMMON 1\n");+ lmtp_ = cx_->compile({strata_metal::resource("STRATA_METAL_ENGINE_KERNELS", "engine-msl", STRATA_METAL_ENGINE_KERNELS_DEFAULT) + "/mtp.metal"}, "#define NO_GGML_COMMON 1\n");{const char* e = std::getenv("STRATA_METAL_MTP_Q8");if (!(e && std::string(e) == "0")) {@@ -1556,7 +1557,7 @@ bool Window::mtp_load(const std::string& gguf_path, int max_t, std::string& err)const std::string beside = gguf_path.substr(0, gguf_path.find_last_of('/') + 1) + "rt/draft_vocab.bin";if (const char* e = std::getenv("STRATA_METAL_DRAFT_VOCAB")) vp = e;else if (FILE* t = std::fopen(beside.c_str(), "rb")) { std::fclose(t); vp = beside; }- else vp = std::string(STRATA_METAL_DATA_DEFAULT) + "/draft_vocab.bin";+ else vp = strata_metal::resource(nullptr, "data", STRATA_METAL_DATA_DEFAULT) + "/draft_vocab.bin";FILE* f = vp == "full" ? nullptr : std::fopen(vp.c_str(), "rb");if (f) {int32_t id;@@ -1582,7 +1583,7 @@ bool Window::mtp_load(const std::string& gguf_path, int max_t, std::string& err)const char* e = std::getenv("STRATA_METAL_MTP_CHAIN");chain_ = !(e && std::string(e) == "0") && std::find(std::begin(ok_types), std::end(ok_types), ty) != std::end(ok_types);if (chain_) {- lchain_ = cx_->compile({"quant.metal", "units.metal", std::string(STRATA_METAL_ENGINE_KERNELS_DEFAULT) + "/mtp_chain.metal"});+ lchain_ = cx_->compile({"quant.metal", "units.metal", strata_metal::resource("STRATA_METAL_ENGINE_KERNELS", "engine-msl", STRATA_METAL_ENGINE_KERNELS_DEFAULT) + "/mtp_chain.metal"});dout_ = A(4 * MAXT); dprob_ = A(4 * MAXT); posv_ = A(4 * N);for (int i = 0; i < N; ++i) posv_.as<float>()[i] = (float) i;dmap_ = A(4 * std::max<size_t>(1, dvocab_.size()));diff --git a/metal/src/mtl_rt.mm b/metal/src/mtl_rt.mmindex 4c6045b2f..8ca808a2a 100644--- a/metal/src/mtl_rt.mm+++ b/metal/src/mtl_rt.mm@@ -3,6 +3,7 @@#import <Metal/Metal.h>#include "mtl_rt.h"+#include "resource_path.h"#include <chrono>#include <cstdio>@@ -46,10 +47,9 @@ Context::Context() : impl_(new Impl) {std::exit(2);}impl_->q = [impl_->dev newCommandQueue];- const char* g = std::getenv("STRATA_GGML_COMMON");- ggml_common_path = g ? g : STRATA_GGML_COMMON_DEFAULT;- const char* k = std::getenv("STRATA_METAL_KERNELS");- kernel_dir = k ? k : STRATA_METAL_KERNELS_DEFAULT;+ // env, then <exe dir>/metal/ (a component package), then the compiled-in source-tree path+ ggml_common_path = strata_metal::resource("STRATA_GGML_COMMON", "ggml-common.h", STRATA_GGML_COMMON_DEFAULT);+ kernel_dir = strata_metal::resource("STRATA_METAL_KERNELS", "kernels", STRATA_METAL_KERNELS_DEFAULT);}Context::~Context() { delete impl_; }diff --git a/metal/src/resource_path.h b/metal/src/resource_path.hnew file mode 100644index 000000000..2e93d57b2--- /dev/null+++ b/metal/src/resource_path.h@@ -0,0 +1,56 @@+// Where the Metal engine finds the files it reads at run time: the .metal kernel sources it compiles when it starts,+// ggml-common.h, and the default expert-profile / draft-vocabulary tables.+//+// A build used from its own source tree finds them through the paths compiled in (STRATA_*_DEFAULT). A component package+// (strata_control/engine_component.py) is moved away from the tree that built it, so the engine also looks beside its+// own executable first:+//+// <exe dir>/metal/kernels/*.metal the kernel sources (metal/kernels)+// <exe dir>/metal/engine-msl/*.metal the engine's own kernels (metal/src/engine/msl)+// <exe dir>/metal/ggml-common.h+// <exe dir>/metal/data/expert-profile.bin draft_vocab.bin+//+// Order: the environment variable (STRATA_METAL_KERNELS, ...), then that folder when it exists, then the compiled-in+// path. Nothing here touches a build run from its source tree: the folder does not exist there.+#pragma once++#include <mach-o/dyld.h>+#include <sys/stat.h>++#include <climits>+#include <cstdint>+#include <cstdlib>+#include <string>++namespace strata_metal {++inline std::string exe_dir() {+ char raw[PATH_MAX * 2];+ uint32_t n = sizeof(raw);+ if (_NSGetExecutablePath(raw, &n) != 0) return "";+ char real[PATH_MAX];+ if (!realpath(raw, real)) return "";+ std::string s(real);+ const size_t slash = s.rfind('/');+ return slash == std::string::npos ? "" : s.substr(0, slash);+}++inline bool exists(const std::string& path) {+ struct stat st;+ return stat(path.c_str(), &st) == 0;+}++// `sub` is relative to <exe dir>/metal/, `compiled` is the path compiled into this build.+inline std::string resource(const char* env, const char* sub, const char* compiled) {+ if (env) {+ if (const char* e = std::getenv(env)) return e;+ }+ const std::string dir = exe_dir();+ if (!dir.empty()) {+ const std::string beside = dir + "/metal/" + sub;+ if (exists(beside)) return beside;+ }+ return compiled;+}++} // namespace strata_metal