okami.mom/strata

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

Niko1221Merge f-q4-metal (ed1f58d27): the Mac (Metal) engine follows the compiled geometry (q4_lite/q4_mid/q4_big kernel parity 40/40, gate 2 like CUDA), plus the PLE row-buffer fix in src/program/generate.cpp (sized by the compiled hidden size, not 2560; it overran 24 KB at hidden 4096 and crashed Metal serve); default build unchanged (CUDA SASS 3573/3573 and 160/160 host objects, HIP gfx1201/gfx1151 string offsets only, SYCL 129/129 objects and 22/22 checksums, Metal per-token functions identical, UD-Q4_K_XL 10/10 tokens, CUDA IQ3_S 26/26 tokens) (Qwen4 prep)7f46672a4

main
3.9 KiB100 linesraw
1// metal/src/engine/missing.cpp - what a kernel entry point that has no Metal implementation yet does when called.
2//
3// Default: say which one and stop (exit code 86), so a run never continues on outputs nobody wrote.
4// STRATA_METAL_STUBS=log: say it once per entry point and return (outputs untouched) - for listing which entry points a
5// code path reaches, never for answers.
6#include <cstdio>
7#include <cstdlib>
8#include <mutex>
9#include <set>
10#include <string>
11
12namespace strata::metal_engine {
13
14void missing(const char* name) {
15    static const bool log_only = [] {
16        const char* v = std::getenv("STRATA_METAL_STUBS");
17        return v != nullptr && std::string(v) == "log";
18    }();
19    if (!log_only) {
20        std::fprintf(stderr, "strata metal: %s has no Metal implementation yet\n", name);
21        std::fflush(stderr);
22        std::_Exit(86);
23    }
24    static std::mutex mu;
25    static std::set<std::string> seen;
26    std::lock_guard<std::mutex> lk(mu);
27    if (seen.insert(name).second) std::fprintf(stderr, "strata metal: STUB %s\n", name);
28}
29
30}  // namespace strata::metal_engine
31
32// ---- entry points the shared host loop asks every request, with nothing to do on Metal yet
33#include "strata/kernels/cvec.hpp"
34namespace strata::kernels {
35// no control vector on Metal (--control-vector-scaled is not ported): the empty one, `loaded()` false
36const Cvec& cvec() {
37    static const Cvec none{};
38    return none;
39}
40}  // namespace strata::kernels
41
42// an uncaught exception or abort on the Metal build prints where it came from (macOS gives no core dump by default and
43// lldb needs a debugging permission a user's session may not have)
44#include <csignal>
45#include <signal.h>
46#include <execinfo.h>
47#include <exception>
48#include <unistd.h>
49#include <dirent.h>
50#include <sys/resource.h>
51#include <mach/mach.h>
52namespace {
53[[noreturn]] void metal_terminate() {
54    void* fr[64];
55    const int n = backtrace(fr, 64);
56    const char msg[] = "strata metal: terminate - the call stack:\n";
57    (void) !write(2, msg, sizeof msg - 1);
58    backtrace_symbols_fd(fr, n, 2);
59    std::abort();
60}
61const bool g_terminate_set = (std::set_terminate(metal_terminate), true);
62// a crash (SIGSEGV / SIGBUS) prints its call stack too: macOS keeps no report for a command-line process by default
63void metal_crash(int sig, siginfo_t* si, void*) {
64    void* fr[64];
65    const int n = backtrace(fr, 64);
66    char msg[160];
67    const int m = std::snprintf(msg, sizeof msg, "strata metal: crashed (signal %d at address %p) - the call stack:\n", sig,
68                                si ? si->si_addr : nullptr);
69    (void) !write(2, msg, (size_t) m);
70    backtrace_symbols_fd(fr, n, 2);
71    {   // what the process held when it crashed (a crash inside the system's Metal compiler has no other clue): descriptors against the limit, memory
72        struct rlimit rl {};
73        getrlimit(RLIMIT_NOFILE, &rl);
74        int fds = 0;
75        if (DIR* d = opendir("/dev/fd")) {
76            while (readdir(d) != nullptr) ++fds;
77            closedir(d);
78        }
79        task_vm_info_data_t ti;
80        mach_msg_type_number_t cnt = TASK_VM_INFO_COUNT;
81        const bool ok = task_info(mach_task_self(), TASK_VM_INFO, (task_info_t) &ti, &cnt) == KERN_SUCCESS;
82        char m2[256];
83        const int k = std::snprintf(m2, sizeof m2, "strata metal: open descriptors %d (limit %llu / %llu), virtual %.1f GB, footprint %.2f GB, resident %.2f GB\n", fds,
84                                    (unsigned long long) rl.rlim_cur, (unsigned long long) rl.rlim_max, ok ? ti.virtual_size / 1e9 : -1.0,
85                                    ok ? ti.phys_footprint / 1e9 : -1.0, ok ? ti.resident_size / 1e9 : -1.0);
86        (void) !write(2, m2, (size_t) k);
87    }
88    signal(sig, SIG_DFL);
89    raise(sig);
90}
91bool set_crash() {
92    struct sigaction sa {};
93    sa.sa_sigaction = metal_crash;
94    sa.sa_flags = SA_SIGINFO;
95    sigaction(SIGSEGV, &sa, nullptr);
96    sigaction(SIGBUS, &sa, nullptr);
97    return true;
98}
99const bool g_crash_set = set_crash();
100}  // namespace