okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
b2de2641f
main
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 () { 28char raw [PATH_MAX * 2 ]; 29uint32_t n = sizeof (raw ); 30if (_NSGetExecutablePath (raw ,& n )!= 0 )return "" ; 31char real [PATH_MAX ]; 32if (!realpath (raw ,real ))return "" ; 33std ::string s (real ); 34const size_t slash = s .rfind ('/' ); 35return slash == std ::string ::npos ?"" :s .substr (0 ,slash ); 36} 37 38inline bool exists (const std ::string & path ) { 39struct stat st ; 40return 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 ) { 45if (env ) { 46if (const char * e = std ::getenv (env ))return e ; 47 } 48const std ::string dir = exe_dir (); 49if (!dir .empty ()) { 50const std ::string beside = dir + "/metal/" + sub ; 51if (exists (beside ))return beside ; 52 } 53return 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 ) { 59if (env ) { 60if (const char * e = std ::getenv (env ))return e ; 61 } 62const std ::string dir = exe_dir (); 63if (!dir .empty ()) { 64const std ::string beside = dir + "/metal/" + sub ; 65if (exists (beside ))return beside ; 66const std ::string old_beside = dir + "/metal/" + old_sub ; 67if (exists (old_beside ))return old_beside ; 68 } 69return compiled ; 70} 71 72}// namespace strata_metal