okami.mom/strata

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

Niko1221split: one constant for the expert window floor, and the IQ3_S pool testa7cc8faa5

main
8.2 KiB139 linesraw
1#include "../src/expert_residency.hpp"
2
3#include <cstdio>
4#include <cstdlib>
5#include <random>
6
7using namespace fnx;
8
9static void check(bool ok, const char* what) {
10    if (!ok) { std::fprintf(stderr, "FAIL: %s\n", what); std::exit(1); }
11}
12template <typename F> static void refuses(F f) {
13    try { f(); } catch (const std::runtime_error&) { return; }
14    check(false, "impossible placement must fail");
15}
16
17int main() {
18    // A cold one-layer pool must keep its hottest slots, even while a larger pool can still shrink.
19    const std::vector<std::pair<size_t, size_t>> sizes{{3, 2}, {4, 3}, {3, 2}};
20    const std::vector<double> score{10, 11, 12, 13, 0, 1, 2, 3, 14, 15, 16, 17};
21    auto p = expert_residency(sizes, score, 4, 2, 0, 6);
22    check(p.resident == std::vector<std::vector<char>>{{0,0,0,0}, {0,0,1,1}, {1,1,1,1}}, "rare pool floor and next cold pool");
23    check(p.count == 6 && p.moved == 34, "extra counts only experts actually evicted");
24    p = expert_residency(sizes, score, 4, 2, 8, 2);
25    check(p.count == 4 && p.moved == 24, "byte target followed by extra evictions");
26    p = expert_residency(sizes, score, 4, 2, 0, 0);
27    check(p.count == 0, "zero request retains all experts");
28    p = expert_residency(sizes, score, 4, 2, 44, 0);
29    check(p.count == 8 && p.moved == 44, "exact capacity succeeds");
30    refuses([&] { expert_residency(sizes, score, 4, 2, 45, 0); });
31    refuses([&] { expert_residency(sizes, score, 4, 2, 44, 1); });
32    refuses([&] { expert_residency(sizes, score, 4, 5, 0, 0); });
33
34    // Equal total bytes do not make equal pools: gate/up and down strides both matter.
35    p = expert_residency({{3,2}, {2,3}}, {0,0,0,0,0,0,0,0}, 4, 2, 0, 4);
36    check(p.resident == std::vector<std::vector<char>>{{0,0,1,1}, {0,0,1,1}}, "pool strides and stable ties");
37
38    // Prod-shaped 43/1/4 layer pools; the old global ordering empties the rare pool at this pressure.
39    std::vector<std::pair<size_t, size_t>> prod(48, {1843200,1228800});
40    prod[2] = {2252800,1740800};
41    for (int l : {4,30,46,47}) prod[(size_t) l] = {1843200,1740800};
42    std::vector<double> ranks(48 * 512);
43    std::mt19937 rng(42);
44    for (double& s : ranks) s = (double) rng() / rng.max();
45    for (int e = 0; e < 512; ++e) ranks[2*512 + e] = -1 + e / 512.0;
46    p = expert_residency(prod, ranks, 512, 320, 3.0 * (1ull << 30), 18253);
47    check(std::count(p.resident[2].begin(), p.resident[2].end(), 1) == 320, "320 slots survive automatic VRAM eviction");
48    for (int e = 0; e < 512; ++e) check(p.resident[2][(size_t)e] == (e >= 192), "rare pool keeps hottest experts");
49
50    // A large explicit host budget must obey the same pool floor as VRAM-pressure evictions.
51    p = expert_residency(prod, ranks, 512, 320, 59.0 * (1ull << 30), 0);
52    check(p.moved >= 59.0 * (1ull << 30), "explicit RAM target reached");
53    check(std::count(p.resident[2].begin(), p.resident[2].end(), 1) == 320, "explicit RAM target keeps rare pool floor");
54
55    // Where no floor is hit, placement stays byte-for-byte the former global ranking, including ties.
56    for (int run = 0; run < 1000; ++run) {
57        std::vector<double> ranks(12);
58        for (double& s : ranks) s = rng() % 5;
59        std::vector<int> order(12);
60        std::iota(order.begin(), order.end(), 0);
61        std::stable_sort(order.begin(), order.end(), [&](int a, int b) { return ranks[a] < ranks[b]; });
62        p = expert_residency(sizes, ranks, 4, 2, 0, 2);
63        for (int i = 0; i < 12; ++i)
64            check(p.resident[i/4][i%4] == (i != order[0] && i != order[1]), "unconstrained placement unchanged");
65    }
66    // ---- the GSQ-RCO IQ3_S set, read off its GGUF: 7 pools by (gate+up, down) bytes of one expert. Layer 21 and layer 47 are pools of 512 experts
67    // each, layers 12/20/30 one of 1,536. The old global ranking (a copy of the code the planner replaced) takes experts coldest per byte
68    // first, so a profile that is cold on a small pool empties it below the window floor; the planner must not.
69    {
70        struct PoolDef { size_t gu, dn; std::vector<int> layers; };
71        const std::vector<PoolDef> defs{
72            {1049600, 921600, {2, 3, 4, 5, 6, 9, 11, 15, 19, 23, 24, 26, 32, 34, 39}},
73            {1254400, 921600, {0, 7, 14, 16, 18, 22, 31, 33, 37, 38, 41, 42, 43, 46}},
74            {1408000, 921600, {17, 25, 27, 28, 29, 35, 40, 44, 45}},
75            {1049600, 460800, {1, 8, 10, 13, 36}},
76            {1254400, 460800, {12, 20, 30}},
77            {1408000, 460800, {21}},
78            {1740800, 921600, {47}}};
79        std::vector<std::pair<size_t, size_t>> iq3(48);
80        int n_l = 0;
81        for (const auto& d : defs)
82            for (int l : d.layers) { iq3[(size_t) l] = {d.gu, d.dn}; ++n_l; }
83        check(n_l == 48, "IQ3_S table has 48 layers");
84        const int floor = 2 * 16 * 10;   // EXPERT_WINDOW_SLOTS: MAXT rows x TOPK picks, twice
85        const double gib1 = (double) (1ull << 30);
86        // the engine scores per byte; this profile is cold exactly on the two single-layer pools and the 3-layer pool
87        std::vector<double> sc(48 * 512);
88        std::mt19937 r2(7);
89        for (size_t i = 0; i < sc.size(); ++i) {
90            const size_t l = i / 512;
91            const bool rare = l == 21 || l == 47 || l == 12 || l == 20 || l == 30;
92            sc[i] = (rare ? 0.0 : 0.5) + ((double) (r2() % 1000) / 1000.0) * 0.4;
93            sc[i] /= (double) iq3[l].first + iq3[l].second;
94        }
95        auto old_left = [&](double want, size_t layer) {   // experts of `layer` still resident after the old ranking moved `want` bytes
96            std::vector<int> order(48 * 512);
97            std::iota(order.begin(), order.end(), 0);
98            std::stable_sort(order.begin(), order.end(), [&](int a, int b) { return sc[(size_t) a] < sc[(size_t) b]; });
99            double moved = 0;
100            int left = 512;
101            for (int i : order) {
102                if (moved >= want) break;
103                moved += (double) iq3[(size_t) (i / 512)].first + iq3[(size_t) (i / 512)].second;
104                if ((size_t) (i / 512) == layer) --left;
105            }
106            return left;
107        };
108        int old_failures = 0, targets = 0;
109        for (double g = 2; g <= 40; g += 2, ++targets) {
110            auto q = expert_residency(iq3, sc, 512, floor, g * gib1, 0);   // must place every target up to 40 GiB (the set has 46.8 GiB)
111            check(q.moved >= g * gib1, "IQ3_S: RAM target reached");
112            for (const auto& d : defs) {
113                int kept = 0;
114                for (int l : d.layers) kept += (int) std::count(q.resident[(size_t) l].begin(), q.resident[(size_t) l].end(), 1);
115                check(kept >= floor, "IQ3_S: every size pool keeps a window of resident experts");
116            }
117            if (old_left(g * gib1, 21) < floor) ++old_failures;
118        }
119        check(old_failures > 0, "IQ3_S negative control: the old ranking empties the 1-layer pool below the window at some target");
120        // beyond what the floors allow, the request is refused whole (nothing is uploaded)
121        refuses([&] { expert_residency(iq3, sc, 512, floor, 46.0 * gib1, 0); });
122        // an explicit window larger than a pool: refused at once
123        refuses([&] { expert_residency(iq3, sc, 512, 513, 0, 0); });
124        std::printf("IQ3_S sweep (2..40 GiB in RAM, %d targets): the old ranking leaves layer 21 under %d slots at %d of them, the planner at none\n",
125                    targets, floor, old_failures);
126    }
127    // injected failure: the card's pool keys are the sliced, 16-byte-padded strides; they partition the layers exactly like the whole-expert bytes
128    {
129        const std::vector<std::pair<size_t, size_t>> whole{{1049600, 921600}, {1049600, 460800}, {1254400, 921600}, {1254400, 460800}, {1408000, 921600}};
130        for (int cards : {2, 3, 4}) {
131            std::vector<std::pair<size_t, size_t>> sliced;
132            for (auto& w : whole) sliced.push_back({(w.first / (size_t) cards + 15) / 16 * 16, (w.second / (size_t) cards + 15) / 16 * 16});
133            for (size_t a = 0; a < whole.size(); ++a)
134                for (size_t b = 0; b < whole.size(); ++b)
135                    check((whole[a] == whole[b]) == (sliced[a] == sliced[b]), "pools by whole-expert bytes and by a card's sliced strides are the same partition");
136        }
137    }
138    std::puts("expert_residency_test: PASS");
139}