okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-own-1b (033cfdd8c) into f-own-int: nothing downloads llama.cppb2de2641f

main
2.9 KiB72 linesraw
1// Where the Metal engine finds the files it reads at run time: the .metal kernel sources it compiles when it starts,
2// strata-quant-common.h (formerly ggml-common.h), and the default expert-profile / draft-vocabulary tables.
3//
4// A build used from its own source tree finds them through the paths compiled in (STRATA_*_DEFAULT). A component package
5// (strata_control/engine_component.py) is moved away from the tree that built it, so the engine also looks beside its
6// own executable first:
7//
8//     <exe dir>/metal/kernels/*.metal            the kernel sources (metal/kernels)
9//     <exe dir>/metal/engine-msl/*.metal         the engine's own kernels (metal/src/engine/msl)
10//     <exe dir>/metal/strata-quant-common.h      (a package of an earlier release has it as <exe dir>/metal/ggml-common.h: read as the second choice)
11//     <exe dir>/metal/data/expert-profile.bin  draft_vocab.bin
12//
13// Order: the environment variable (STRATA_METAL_KERNELS, ...), then that folder when it exists, then the compiled-in
14// path. Nothing here touches a build run from its source tree: the folder does not exist there.
15#pragma once
16
17#include <mach-o/dyld.h>
18#include <sys/stat.h>
19
20#include <climits>
21#include <cstdint>
22#include <cstdlib>
23#include <string>
24
25namespace strata_metal {
26
27inline std::string exe_dir() {
28    char raw[PATH_MAX * 2];
29    uint32_t n = sizeof(raw);
30    if (_NSGetExecutablePath(raw, &n) != 0) return "";
31    char real[PATH_MAX];
32    if (!realpath(raw, real)) return "";
33    std::string s(real);
34    const size_t slash = s.rfind('/');
35    return slash == std::string::npos ? "" : s.substr(0, slash);
36}
37
38inline bool exists(const std::string& path) {
39    struct stat st;
40    return stat(path.c_str(), &st) == 0;
41}
42
43// `sub` is relative to <exe dir>/metal/, `compiled` is the path compiled into this build.
44inline std::string resource(const char* env, const char* sub, const char* compiled) {
45    if (env) {
46        if (const char* e = std::getenv(env)) return e;
47    }
48    const std::string dir = exe_dir();
49    if (!dir.empty()) {
50        const std::string beside = dir + "/metal/" + sub;
51        if (exists(beside)) return beside;
52    }
53    return compiled;
54}
55
56// The same for a file that has two names (own-stack phase 1B: strata-quant-common.h, formerly ggml-common.h): the environment variable, then the new name
57// beside the executable, then the old name beside the executable (a package of an earlier release), then the compiled-in path.
58inline std::string resource2(const char* env, const char* sub, const char* old_sub, const char* compiled) {
59    if (env) {
60        if (const char* e = std::getenv(env)) return e;
61    }
62    const std::string dir = exe_dir();
63    if (!dir.empty()) {
64        const std::string beside = dir + "/metal/" + sub;
65        if (exists(beside)) return beside;
66        const std::string old_beside = dir + "/metal/" + old_sub;
67        if (exists(old_beside)) return old_beside;
68    }
69    return compiled;
70}
71
72}  // namespace strata_metal