okami.mom/strata

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

waveringanaRestore the DeepSeek V4.1 engine and CPU tests removed upstream8ac8ce32e

main
5.4 KiB112 linesraw
1// Strata. ExpertStore (src/dsv41/ds4_store.hpp) on real expert files, no device: the bytes a slot holds equal the file's, the
2// unbuffered reads' rate, the policy's hits on a skewed random access stream.
3//   ds4_store_test <gguf> <layer0 gate off> <layer0 up off> <layer0 down off> <expert bytes (gate/up/down)> [budget GB] [policy 0|1|2] [accesses]
4// (offsets from the GGUF's tensor directory: tools/dsv41 header scan; the three tensors of a layer are [384][...] so expert e of
5// a matrix lies at off + e * bytes)
6#include "ds4_store.hpp"
7
8#include <chrono>
9#include <cstdio>
10#include <cstdlib>
11#include <random>
12#include <thread>
13
14using namespace strata::ds4;
15
16int main(int argc, char** argv) {
17    if (argc < 6) {
18        std::printf("usage: ds4_store_test <gguf> <gate off> <up off> <down off> <matrix bytes> [budget GB] [policy] [accesses]\n");
19        return 2;
20    }
21    const std::string path = argv[1];
22    const uint64_t offs[3] = {std::strtoull(argv[2], nullptr, 10), std::strtoull(argv[3], nullptr, 10), std::strtoull(argv[4], nullptr, 10)};
23    const uint64_t mb = std::strtoull(argv[5], nullptr, 10);
24    const double gb = argc > 6 ? std::atof(argv[6]) : 1.0;
25    const int policy = argc > 7 ? std::atoi(argv[7]) : 0;
26    const int n_acc = argc > 8 ? std::atoi(argv[8]) : 2000;
27    const os::fd_t fd = os::open_ro(path), dfd = os::open_direct(path), pfd = os::open_par(path);
28    if (fd == os::kBadFd || pfd == os::kBadFd) {
29        std::printf("cannot open %s\n", path.c_str());
30        return 2;
31    }
32    std::printf("direct handle: %s\n", dfd != os::kBadFd ? "yes" : "NO (buffered)");
33    ExpertStore::LayerGeo geo;
34    geo.n_exp = 384;
35    uint64_t at = 0;
36    for (int r = 0; r < 3; ++r) {
37        geo.file[r] = 0;
38        geo.off[r] = offs[r];
39        geo.stride[r] = mb;
40        geo.bytes[r] = mb;
41        geo.tr[r] = at;   // the engine's blob layout with the store's 8192-byte gaps
42        at += ((mb + 255) & ~(uint64_t) 255) + ExpertStore::kSlack;
43    }
44    geo.blob = at;
45    ExpertStore st;
46    ExpertStore::Cfg cfg;
47    cfg.budget_bytes = (size_t) (gb * 1e9);
48    cfg.threads = std::getenv("STORE_TEST_THREADS") ? std::atoi(std::getenv("STORE_TEST_THREADS")) : 8;
49    cfg.policy = (ExpertStore::Policy) policy;
50    cfg.floor_bytes = 4ull << 30;
51    cfg.ram_avail = [] { return os::ram_avail(); };
52    if (!st.init(cfg, {geo}, {dfd}, {pfd})) {
53        std::printf("init failed\n");
54        return 2;
55    }
56    std::printf("slot %.2f MB, up to %zu slots\n", st.slot_bytes() / 1e6, st.slots_max());
57    // a skewed stream: 70% of the accesses from the 64 hottest experts, the rest uniform; 6 a "token"
58    std::mt19937_64 rng(7);
59    auto pick = [&] { return rng() % 100 < 70 ? (int) (rng() % 64) : (int) (rng() % 384); };
60    std::vector<uint8_t> ref(mb);
61    int bad = 0;
62    long checked = 0;
63    const auto t0 = std::chrono::steady_clock::now();
64    for (int a = 0; a < n_acc; ++a) {
65        int es[6];
66        for (int i = 0; i < 6; ++i) {
67            es[i] = pick();
68            for (int j = 0; j < i; ++j)
69                if (es[j] == es[i]) es[i] = (es[i] + 1 + j) % 384;
70        }
71        std::vector<char> ok;
72        st.ensure(0, es, 6, ok);
73        if (a % 17 == 0 && !std::getenv("STORE_TEST_NOCMP"))   // a sample compared with the file read plainly
74            for (int i = 0; i < 6; ++i)
75                if (ok[(size_t) i])
76                    for (int r = 0; r < 3; ++r) {
77                        const int s = st.find(0, es[i]);
78                        os::pread_full(fd, ref.data(), mb, offs[r] + (uint64_t) es[i] * mb);
79                        ++checked;
80                        if (s < 0 || std::memcmp(st.mat(s, r), ref.data(), mb) != 0) ++bad;
81                    }
82        st.release(0, es, 6, ok);
83    }
84    // guesses queued behind demand, cancelled, and a shrink: the store must still give the file's bytes
85    for (int round = 0; round < 3; ++round) {
86        for (int e = 100; e < 140; ++e) st.prefetch(0, e);
87        st.cancel_stale(0);
88        int es[6] = {5 + round, 17, 200, 201, 202, 250 + round};
89        std::vector<char> ok;
90        st.ensure(0, es, 6, ok);
91        for (int i = 0; i < 6; ++i)
92            if (ok[(size_t) i]) {
93                const int sl = st.find(0, es[i]);
94                for (int r = 0; r < 3; ++r) {
95                    os::pread_full(fd, ref.data(), mb, offs[r] + (uint64_t) es[i] * mb);
96                    ++checked;
97                    if (sl < 0 || std::memcmp(st.mat(sl, r), ref.data(), mb) != 0) ++bad;
98                }
99            }
100        st.release(0, es, 6, ok);
101        std::this_thread::sleep_for(std::chrono::milliseconds(800));   // (the guesses read by now)
102        const size_t g = st.shrink_chunks(1);
103        std::printf("round %d: shrank %zu chunk(s), %zu slots left, cancelled %lld\n", round, g, st.slots_allocated(), (long long) st.stats().cancelled.load());
104    }
105    const double dt = std::chrono::duration<double>(std::chrono::steady_clock::now() - t0).count();
106    const auto& s = st.stats();
107    std::printf("%d accesses of 6 experts in %.2f s: hits %lld misses %lld evictions %lld fails %lld; read %.2f GB (%.2f GB/s); waited %.2f s; "
108                "compared %ld matrices, %d differ\n",
109                n_acc, dt, (long long) s.hits.load(), (long long) s.misses.load(), (long long) s.evictions.load(), (long long) s.fails.load(),
110                s.bytes_read.load() / 1e9, s.bytes_read.load() / 1e9 / dt, s.wait_ns.load() / 1e9, checked, bad);
111    return bad == 0 ? 0 : 1;
112}