okami.mom/strata

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

Niko1221Merge branch 'f-142-mgpu' into f-14243b105a75

main
4.6 KiB84 linesraw
1#include "strata/program/split_ring.hpp"
2
3#include <cstdint>
4#include <cstdio>
5#include <vector>
6
7using namespace strata::program;
8
9#define CHECK(cond)                                                                       \
10    do {                                                                                  \
11        if (!(cond)) {                                                                    \
12            std::printf("split_ring_test FAIL line %d: %s\n", __LINE__, #cond);           \
13            return 1;                                                                     \
14        }                                                                                 \
15        ++checks;                                                                         \
16    } while (0)
17
18// a cache of `slots` slots whose blobs vary per layer like a native pack's (2.4 .. 3.6 MB), as the prefix sum the
19// expert cache keeps
20static std::vector<uint64_t> make_offsets(int64_t slots) {
21    std::vector<uint64_t> off((size_t) slots + 1, 0);
22    for (int64_t i = 0; i < slots; ++i) off[(size_t) i + 1] = off[(size_t) i] + 2400000ull + (uint64_t) (i % 13) * 100000ull;
23    return off;
24}
25
26int main() {
27    int checks = 0;
28
29    // 1. the ring the split chooses: the env wins, else 96 when most experts are resident, 0 = the pinned-share rule
30    CHECK(split_ring_slots(nullptr, 0.9) == 96);
31    CHECK(split_ring_slots(nullptr, 0.75) == 96);
32    CHECK(split_ring_slots(nullptr, 0.74) == 0);
33    CHECK(split_ring_slots("384", 0.9) == 384);
34    CHECK(split_ring_slots("384", 0.1) == 384);
35    CHECK(split_ring_slots("512", 0.9) == 512);
36    CHECK(split_ring_slots("0", 0.9) == 0);
37    CHECK(split_ring_slots("-3", 0.9) == 0);
38    CHECK(split_ring_slots("junk", 0.9) == 0);
39
40    // 2. the lend count: uniform slots round up, sized slots walk from the end
41    CHECK(split_lend_slots(1000, 1000 * 100, nullptr, 100, 0) == 0);
42    CHECK(split_lend_slots(1000, 1000 * 100, nullptr, 100, 1) == 1);
43    CHECK(split_lend_slots(1000, 1000 * 100, nullptr, 100, 100) == 1);
44    CHECK(split_lend_slots(1000, 1000 * 100, nullptr, 100, 101) == 2);
45    CHECK(split_lend_slots(1000, 0, nullptr, 0, 500) == 0);   // no blob size: nothing to count
46    {
47        const std::vector<uint64_t> off = make_offsets(50);
48        const int64_t bytes = (int64_t) off.back();
49        CHECK(split_lend_slots(50, bytes, off.data(), 3600000, 0) == 0);
50        const uint64_t last = off[50] - off[49];
51        CHECK(split_lend_slots(50, bytes, off.data(), 3600000, last) == 1);
52        CHECK(split_lend_slots(50, bytes, off.data(), 3600000, last + 1) == 2);
53        CHECK(split_lend_slots(50, bytes, off.data(), 3600000, off[50]) == 50);
54        CHECK(split_lend_slots(50, bytes, off.data(), 3600000, off[50] + 1) == 50);   // never past the cache
55    }
56
57    // 3. the order: the RAM copy's lend region and the borrow must be sized with the SAME ring.  The need of a chunk is
58    //    its own buffers plus ring_slots * max_blob (Prefill::bytes_needed); the engine used to size the region with the
59    //    default ring (96) and apply the STRATA_SPLIT_RING override afterwards, so a ring of 384 borrowed more slots
60    //    than the region kept.
61    {
62        const int64_t slots = 6000;
63        const std::vector<uint64_t> off = make_offsets(slots);
64        const int64_t bytes = (int64_t) off.back();
65        const uint64_t max_blob = 3600000, buffers = 3000ull << 20;
66        auto need = [&](int ring) { return buffers + (uint64_t) ring * max_blob; };
67        auto lend = [&](int ring) { return split_lend_slots(slots, bytes, off.data(), max_blob, need(ring)); };
68
69        const int default_ring = 96;
70        const int ring = split_ring_slots("384", 0.9);   // STRATA_SPLIT_RING=384
71        const int64_t borrowed = lend(ring);             // what the prompt path borrows with the override in force
72        const int64_t kept_old_order = lend(default_ring);   // the region sized before the override applied
73        const int64_t kept_new_order = lend(split_ring_slots("384", 0.9));   // decided first, then sized
74        CHECK(kept_old_order < borrowed);                // the bug: borrowed experts without a RAM copy
75        CHECK(kept_new_order == borrowed);               // the fix: the copy keeps every borrowed slot
76        // no override in play (production: STRATA_SPLIT_RING unset, ring 96 either way): nothing changes
77        CHECK(lend(split_ring_slots(nullptr, 0.5) > 0 ? split_ring_slots(nullptr, 0.5) : default_ring) ==
78              kept_old_order);
79        // a bigger ring borrows monotonically more
80        CHECK(lend(96) <= lend(256) && lend(256) <= lend(384) && lend(384) <= lend(512));
81    }
82
83    std::printf("split_ring_test OK (%d checks)\n", checks);
84}