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
7.6 KiB121 linesraw
1// metal/probe/window_probe.cpp - the Metal decode window on its own: Window::init and a few windows of one token, without the engine around it.
2//
3//   window_probe <model-metal.gguf> [tokens N] [--ple <model.gguf>]        (built with the same -DSTRATA_GEOMETRY_FILE as the model)
4//   --requests F --logits-out G   the gate-2 requests without the engine: every line of F is a token list (comma separated); each is read as a prompt
5//             (the prompt chain from 32 tokens, windows of 8 below, as the engine reads it), its last token runs as a head window, and the logits of
6//             that position (the vocabulary's floats) are appended to G.  The judge (g3kl_mac.py --from-dump G) compares them with llama.cpp.
7//   --session N   carve the engine's session state for N cells of context (session_init, as generate.cpp does) before the window comes up
8//   --ple F  open F's PLE table as the engine does (direct reads, the I/O thread) and run the PLE block: the engine's first window
9//
10// A session with no PLE and no QSA state is enough for the window (the PLE block is skipped, the K/V live in the window's own buffers), so this is
11// the engine's process state - the model mapped and wrapped, every library compiled, every buffer allocated, the first pipelines created - for
12// a Metal fault that has to be looked at without an engine start.  Not a correctness test: the tokens are whatever the model makes of them.
13#include "strata/core/session.hpp"
14#include "strata/core/geometry.hpp"
15#include "strata/core/layer.hpp"
16#include "strata/kernels/ngram.hpp"
17#include <cuda_runtime.h>
18
19#include <chrono>
20#include <fstream>
21#include <functional>
22#include <sstream>
23#include <cstdio>
24#include <cstdlib>
25#include <string>
26#include <vector>
27
28namespace strata::metal_engine {
29void set_model_shard(const char* shard1);
30void* window_create();
31bool 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);
32bool window_run(void* w, int T, const int32_t* tokens, int64_t pos0, bool head, int32_t* out, std::string& err);
33bool window_commit(void* w, int n_keep, std::string& err);
34bool window_copy_logits(void* w, int t, float* host);
35void window_reset(const strata::core::SessionState* ss);
36bool prompt_run(const int64_t* tokens, int64_t n, int64_t pos0, const float* const* embd_rows,
37                const std::function<bool(const float*, int64_t, int64_t, std::string&)>& on_chunk, std::string& err);
38}  // namespace strata::metal_engine
39
40int main(int argc, char** argv) {
41    if (argc < 2) { std::fprintf(stderr, "usage: window_probe <model-metal.gguf> [tokens N]\n"); return 2; }
42    int n = argc > 2 && argv[2][0] >= '0' && argv[2][0] <= '9' ? std::atoi(argv[2]) : 4;
43    setenv("STRATA_METAL_STREAM", "0", 1);
44    strata::metal_engine::set_model_shard(argv[1]);
45    strata::core::SessionState ss;
46    std::string err;
47    strata::kernels::PleTable table;
48    static float dummy_f[16] = {};
49    static uint16_t dummy_h[16] = {};
50    static int32_t dummy_i[4] = {-1, -1, -1, -1};
51    for (int i = 2; i + 1 < argc; ++i)
52        if (std::string(argv[i]) == "--ple") {
53            strata::kernels::PleIoOptions pio;
54            if (!table.open(argv[i + 1], err, pio)) { std::printf("PLE table: %s\n", err.c_str()); return 5; }
55            ss.ple.table = &table;   // (the window reads its weights from the model file; the rest of ready() is pointers it does not use)
56            ss.ple.consts = strata::kernels::ple_artifact_consts();
57            ss.ple.hist = dummy_f; ss.ple.emb_dev = dummy_f; ss.ple.emb_host = dummy_f; ss.ple.scratch = dummy_f;
58            ss.ple.token = dummy_i; ss.ple.prev = dummy_i;
59            ss.ple.w.key_bf16 = dummy_h; ss.ple.w.value_bf16 = dummy_h; ss.ple.w.norm_key = dummy_f; ss.ple.w.norm_query = dummy_f;
60            ss.ple.w.norm_conv = dummy_f; ss.ple.w.conv1d_f16 = dummy_h;
61            std::printf("window_probe: PLE table %s open, %llu rows\n", argv[i + 1], (unsigned long long) table.rows());
62        }
63    for (int i = 2; i + 1 < argc; ++i)
64        if (std::string(argv[i]) == "--session") {
65            const int64_t cells = std::atoll(argv[i + 1]);
66            const int64_t K = strata::core::kCompiledGeometry.n_expert_used;
67            void* sbuf = nullptr;
68            const uint64_t bytes = strata::core::session_bytes(strata::core::kCompiledGeometry, cells, K, 0, -1);
69            if (cudaMalloc(&sbuf, bytes) != cudaSuccess) { std::printf("session allocation of %.1f GB failed\n", bytes / 1e9); return 9; }
70            if (strata::core::session_init(strata::core::kCompiledGeometry, cells, K, sbuf, ss, 0, -1) == 0) { std::printf("session_init failed\n"); return 9; }
71            std::printf("window_probe: session state for %lld cells carved (%.2f GB)\n", (long long) cells, bytes / 1e9);
72        }
73    int64_t nv = 0;
74    void* w = strata::metal_engine::window_create();
75    const auto t0 = std::chrono::steady_clock::now();
76    if (!strata::metal_engine::window_init(w, strata::core::kCompiledGeometry, ss, 8, 4096, &nv, err)) { std::printf("init failed: %s\n", err.c_str()); return 3; }
77    std::printf("window_probe: init ok, vocabulary %lld, %.0f ms\n", (long long) nv, std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - t0).count());
78    std::vector<int32_t> toks = {1, 2, 3, 4, 5, 6, 7, 8};
79    int32_t out[8] = {};
80    for (int i = 0; i < n; ++i) {
81        const auto t1 = std::chrono::steady_clock::now();
82        if (!strata::metal_engine::window_run(w, 1, &toks[(size_t) i % 8], i, true, out, err) || !strata::metal_engine::window_commit(w, 1, err)) {
83            std::printf("window %d failed: %s\n", i, err.c_str());
84            return 4;
85        }
86        std::printf("window %d: token %d, %.1f ms\n", i, out[0], std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - t1).count());
87    }
88    std::string reqf, outf;
89    for (int i = 2; i + 1 < argc; ++i) {
90        if (std::string(argv[i]) == "--requests") reqf = argv[i + 1];
91        if (std::string(argv[i]) == "--logits-out") outf = argv[i + 1];
92    }
93    if (!reqf.empty() && !outf.empty()) {
94        std::ifstream in(reqf);
95        std::ofstream outl(outf, std::ios::binary | std::ios::trunc);
96        std::vector<float> lg((size_t) nv);
97        std::string line;
98        int nreq = 0;
99        const auto t2 = std::chrono::steady_clock::now();
100        while (std::getline(in, line)) {
101            std::vector<int64_t> ids;
102            std::stringstream ss2(line);
103            std::string tok;
104            while (std::getline(ss2, tok, ',')) if (!tok.empty()) ids.push_back(std::atoll(tok.c_str()));
105            if (ids.size() < 2) continue;
106            strata::metal_engine::window_reset(&ss);
107            ss.ple_prev[0] = ss.ple_prev[1] = -1;
108            const int64_t npre = (int64_t) ids.size() - 1;
109            if (!strata::metal_engine::prompt_run(ids.data(), npre, 0, nullptr, nullptr, err)) { std::printf("request %d: prompt failed: %s\n", nreq, err.c_str()); return 6; }
110            int32_t last = (int32_t) ids.back(), o = 0;
111            if (!strata::metal_engine::window_run(w, 1, &last, npre, true, &o, err)) { std::printf("request %d: window failed: %s\n", nreq, err.c_str()); return 7; }
112            if (!strata::metal_engine::window_copy_logits(w, 0, lg.data())) { std::printf("request %d: no logits\n", nreq); return 8; }
113            outl.write((const char*) lg.data(), (std::streamsize) lg.size() * 4);
114            ++nreq;
115        }
116        std::printf("window_probe: %d requests, logits in %s, %.1f s\n", nreq, outf.c_str(), std::chrono::duration<double>(std::chrono::steady_clock::now() - t2).count());
117        return 0;
118    }
119    std::printf("window_probe: ok\n");
120    return 0;
121}