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
5.4 KiB93 linesraw
1// metal/tests/msl_dump.cpp - compile every kernel library the engine compiles (engine/msl_libs.h), without starting an engine.
2//
3//   msl_dump [--kq] [--f16] [--engine-msl DIR]        STRATA_METAL_MSL_DUMP=<dir> msl_dump   writes the text of each library
4//
5// The engine builds its libraries from the same lists (msl_libs.h), so this is "the MSL text the engine compiles": with the dump
6// directory set, mtl_rt.mm writes each library's full string (prelude, <metal_stdlib>, ggml-common, the kernel files) and an index;
7// tools/msl_text_check.py compares two dumps.  It also proves that the libraries compile at the geometry this binary was built for
8// (-DSTRATA_GEOMETRY / -DSTRATA_GEOMETRY_FILE): a compile error ends the run with the compiler's message.
9//   --kq    the prelude of a model with K-quant / IQ down experts (kq_down_prelude)
10//   --f16   STRATA_METAL_F16
11//   --wrap F  map and wrap the model file F as the engine does before it compiles (a process holding a model of that size)
12//   --touch   read every page of the wrapped file (the process holds the model resident)
13//   --map F   map a second file too (the PLE file of the engine)
14//   --pipes also build the pipeline of a few kernels of the core library (xprep, moe_route, moe_combine_sh2, gr_norm2): the GPU compiler, not only the front end
15#include "gguf_lite.h"
16#include "mtl_rt.h"
17#include "engine/msl_libs.h"
18
19#include <algorithm>
20#include <chrono>
21#include <cstdio>
22#include <cstring>
23#include <string>
24#include <sys/mman.h>
25
26int main(int argc, char** argv) {
27    bool kq = false, f16 = false, pipes = false;
28    std::string edir = "../src/engine/msl", wrapf, mapf;
29    bool touch = false;
30    for (int i = 1; i < argc; ++i) {
31        if (!std::strcmp(argv[i], "--kq")) kq = true;
32        else if (!std::strcmp(argv[i], "--f16")) f16 = true;
33        else if (!std::strcmp(argv[i], "--pipes")) pipes = true;
34        else if (!std::strcmp(argv[i], "--wrap") && i + 1 < argc) wrapf = argv[++i];
35        else if (!std::strcmp(argv[i], "--map") && i + 1 < argc) mapf = argv[++i];
36        else if (!std::strcmp(argv[i], "--touch")) touch = true;
37        else if (!std::strcmp(argv[i], "--engine-msl") && i + 1 < argc) edir = argv[++i];
38    }
39    using namespace strata_metal;
40    const std::string kqp = kq ? "#define STRATA_KQ_DOWN 1\n" : "";
41    const std::pair<const char*, MslLib> libs[] = {
42        {"core", lib_core(f16, kqp)},          {"gdn", lib_gdn()},
43        {"qsa", lib_qsa()},                    {"sample", lib_sample()},
44        {"ple", lib_ple(edir)},                {"prompt", lib_prompt(kqp)},
45        {"prompt_gdn", lib_prompt_gdn()},      {"prompt_qsa", lib_prompt_qsa()},
46        {"mtp", lib_mtp(edir)},                {"mtp_chain", lib_mtp_chain(edir)},
47        {"dflash2", lib_dflash2(edir)},        {"coop_probe", lib_coop_probe()},
48    };
49    gguf::File gf, gf2;
50    mtl::Buffer W;
51    std::string err;
52    // (the files are mapped before the device exists, as Window::init does)
53    if (!wrapf.empty() && !gf.open(wrapf, err)) { std::printf("cannot open %s: %s\n", wrapf.c_str(), err.c_str()); return 2; }
54    if (!mapf.empty() && !gf2.open(mapf, err)) { std::printf("cannot open %s: %s\n", mapf.c_str(), err.c_str()); return 2; }
55    mtl::Context cx;
56    if (!wrapf.empty()) {   // as Window::init: the file wrapped (chunks overlapping by the largest tensor when it is bigger than one buffer)
57        size_t span = 0;
58        for (const auto& kv : gf.tensors)
59            if (kv.first != "per_layer_token_embd.weight") span = std::max<size_t>(span, kv.second.bytes);
60        W = cx.wrap(gf.map, gf.map_bytes, span);
61        madvise(gf.map, gf.map_bytes, MADV_WILLNEED);   // (as Window::init)
62        if (touch) {   // read every page: the process then holds the model resident
63            unsigned sink = 0;
64            for (size_t o = 0; o < gf.map_bytes; o += 16384) sink += ((volatile unsigned char*) gf.map)[o];
65            std::printf("read every page of the file (%u)\n", sink);
66        }
67        std::printf("wrapped %s: %.1f GB, handle %s\n", wrapf.c_str(), gf.file_bytes / 1e9, W.handle ? "ok" : "NULL");
68    }
69    int bad = 0, core_id = -1;
70    for (const auto& [name, L] : libs) {
71        const auto t0 = std::chrono::steady_clock::now();
72        std::string why;
73        const int id = cx.compile_try(L.files, L.prelude, false, &why);
74        const double ms = std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - t0).count();
75        if (id < 0) {
76            std::printf("%-11s FAIL (%.0f ms): %s\n", name, ms, why.c_str());
77            ++bad;
78        } else {
79            std::printf("%-11s ok (%.0f ms)\n", name, ms);
80            if (std::string(name) == "core") core_id = id;
81        }
82        if (pipes && std::string(name) == "ple" && core_id >= 0) {   // (as the engine: the five decode-window libraries compiled, then the first pipeline)
83            for (const char* fn : {"xprep", "moe_route", "moe_combine_sh2", "gr_norm2"}) {
84                const auto t1 = std::chrono::steady_clock::now();
85                mtl::Pipeline pl = cx.pipeline(core_id, fn, mtl::Constants());
86                std::printf("            pipeline %-16s ok (%.0f ms, max threads %d)\n", fn,
87                            std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - t1).count(), pl.max_threads);
88            }
89        }
90    }
91    std::printf("%s\n", bad ? "msl_dump: FAILED" : "msl_dump: all libraries compile");
92    return bad ? 1 : 0;
93}