okami.mom/strata

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

Niko1221Merge f-0216-core into f-0216-allbd0e878f4

main
5.6 KiB128 linesraw
1// P0.1 (lite): the run manifest.  Off by default and free when off; the same decisions give the same text whatever order
2// they were recorded in; a later record of a path replaces the earlier; the file is written whole and only when something
3// changed.  No GPU.   run_manifest_test [--out FILE]   (FILE keeps the last document for a JSON parser to check)
4#include <cstdio>
5#include <cstdlib>
6#include <cstring>
7#include <filesystem>
8#include <fstream>
9#include <sstream>
10#include <string>
11
12#include "strata/core/run_manifest.hpp"
13
14using strata::core::manifest::Manifest;
15
16static int failed = 0;
17#define CHECK(x) do { if (!(x)) { std::fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #x); ++failed; } } while (0)
18
19static std::string slurp(const std::string& p) {
20    std::ifstream f(p, std::ios::binary);
21    std::stringstream s;
22    s << f.rdbuf();
23    return s.str();
24}
25
26static void run_a(Manifest& m) {
27    m.engine("kind", "native-single");
28    m.engine("backend", "cuda");
29    m.budget("expert_cache_mib", 1500);
30    m.budget("vram_reserve_mib", 960);
31    m.path("prefill", "mmq", true, "tile found for IQ3_S gate/up and IQ4_NL down", "layers 0-47");
32    m.path("prefill", "fused-native", false, "MMQ covers these formats");
33    m.path("decode", "route-tail-skip", true, "serve default");
34    m.tensors("dense", "Q8_0", 3, 3000);
35    m.tensors("dense", "F32", 10, 400);
36    m.tensors("dense", "Q8_0", 2, 2000);          // accumulates
37    m.tensors("head", "Q6_K", 1, 123456);
38}
39static void run_b(Manifest& m) {   // the same decisions, recorded in another order, with one superseded record
40    m.tensors("head", "Q6_K", 1, 123456);
41    m.tensors("dense", "F32", 10, 400);
42    m.path("decode", "route-tail-skip", true, "serve default");
43    m.path("prefill", "fused-native", true, "provisional");
44    m.path("prefill", "fused-native", false, "MMQ covers these formats");   // replaces the line above
45    m.budget("vram_reserve_mib", 960);
46    m.engine("backend", "cuda");
47    m.tensors("dense", "Q8_0", 5, 5000);
48    m.path("prefill", "mmq", true, "tile found for IQ3_S gate/up and IQ4_NL down", "layers 0-47");
49    m.budget("expert_cache_mib", 1500);
50    m.engine("kind", "native-single");
51}
52
53int main(int argc, char** argv) {
54    std::string keep;
55    for (int i = 1; i + 1 < argc; ++i)
56        if (!std::strcmp(argv[i], "--out")) keep = argv[i + 1];
57    const std::filesystem::path dir = std::filesystem::temp_directory_path() / "strata_manifest_test";
58    std::filesystem::create_directories(dir);
59    const std::string f1 = (dir / "a.json").string(), f2 = (dir / "b.json").string();
60    std::filesystem::remove(f1);
61    std::filesystem::remove(f2);
62
63    // off: nothing recorded, nothing written, no file
64    {
65        Manifest off;
66        CHECK(!off.enabled());
67        run_a(off);
68        CHECK(!off.write("ready") && !off.flush_if_dirty("ready"));
69        CHECK(off.json("x").find("\"kind\"") == std::string::npos);
70    }
71
72    // two matched runs: the same text, in either recording order
73    Manifest a(f1), b(f2);
74    CHECK(a.enabled() && a.file() == f1);
75    run_a(a);
76    run_b(b);
77    CHECK(a.json("ready") == b.json("ready"));
78    CHECK(a.json("ready") != a.json("done"));                 // the stage is the one field that is meant to differ
79    CHECK(a.write("ready") && b.write("ready"));
80    CHECK(slurp(f1) == slurp(f2) && !slurp(f1).empty());
81    CHECK(!std::filesystem::exists(f1 + ".tmp"));             // renamed over, no leftover
82
83    // keys are sorted and the structure is there
84    const std::string doc = slurp(f1);
85    CHECK(doc.find("\"manifest_version\": 1") != std::string::npos);
86    CHECK(doc.find("\"selected\": false, \"reason\": \"MMQ covers these formats\"") != std::string::npos);
87    CHECK(doc.find("\"Q8_0\": {\"count\": 5, \"bytes\": 5000}") != std::string::npos);
88    CHECK(doc.find("\"backend\"") < doc.find("\"kind\""));
89    CHECK(doc.find("\"decode\"") < doc.find("\"prefill\""));
90    CHECK(doc.find("\"provisional\"") == std::string::npos);
91
92    // dirty tracking: no rewrite when nothing new, one when something is
93    CHECK(!a.flush_if_dirty("request"));
94    a.path("decode", "route-tail-skip", true, "serve default");     // the same record again
95    CHECK(!a.flush_if_dirty("request"));
96    a.path("decode", "route-tail-skip", false, "STRATA_ROUTE_TAIL_SKIP=0");
97    CHECK(a.flush_if_dirty("request"));
98    CHECK(slurp(f1).find("STRATA_ROUTE_TAIL_SKIP=0") != std::string::npos);
99    CHECK(slurp(f1).find("\"stage\": \"request\"") != std::string::npos);
100    CHECK(!a.flush_if_dirty("request"));
101    a.tensors("dense", "F32", 1, 4);
102    CHECK(a.flush_if_dirty("request"));
103
104    // an empty manifest is a valid document, and text is escaped
105    Manifest e((dir / "e.json").string());
106    CHECK(e.write("ready"));
107    CHECK(slurp((dir / "e.json").string()).find("\"paths\": [],") != std::string::npos);
108    e.path("x", "quote\"back\\slash", true, "line1\nline2\ttab \x01");
109    const std::string et = e.json("ready");
110    CHECK(et.find("quote\\\"back\\\\slash") != std::string::npos);
111    CHECK(et.find("line1\\nline2\\ttab \\u0001") != std::string::npos);
112
113    // an unwritable target does not throw, returns false, and the run goes on
114    Manifest bad((dir / "no_such_dir" / "m.json").string());
115    bad.path("x", "y", true, "z");
116    CHECK(!bad.write("ready"));
117
118    if (!keep.empty()) {
119        std::ofstream k(keep, std::ios::binary);
120        k << e.json("ready");
121        std::ofstream k2(keep + ".run", std::ios::binary);
122        k2 << a.json("ready");
123    }
124    std::filesystem::remove_all(dir);
125    if (failed) { std::fprintf(stderr, "%d check(s) failed\n", failed); return 1; }
126    std::puts("run_manifest_test: ok");
127    return 0;
128}