okami.mom/strata

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

Niko1221Merge f-pr-split-n4534 cca88a011 into f-p4-int (each card keeps only its own layers' dense weights on a layer split; the last card's draft-head room)638533e3f

main
8.8 KiB154 linesraw
1// The auto layer split's stage-size rule (#1616): no GPU, no model.
2#include <cstdio>
3#include <cstdlib>
4#include <vector>
5
6#include "strata/program/split_rules.hpp"
7
8using namespace strata::program::split_rules;
9
10static int failed = 0;
11#define CHECK(x) do { if (!(x)) { std::fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #x); ++failed; } } while (0)
12
13int main() {
14    // the reporter's 4-GPU pick on a 48-layer model: layers 0-1, 2, 3-19, 20-47 -> cuts 2,3,20
15    CHECK(smallest_stage({2, 3, 20}, 48) == 1);
16    CHECK(has_tiny_stage({2, 3, 20}, 48));
17    // the manual split that fixed it: 4,8,24 is not a cut list of 4 stages; as cuts 4,8,24 -> 4,4,16,24
18    CHECK(smallest_stage({4, 8, 24}, 48) == 4);
19    CHECK(!has_tiny_stage({4, 8, 24}, 48));
20    // identical cards: the balanced split has no tiny stage, so the rule never fires and the pick stays
21    CHECK(!has_tiny_stage({12, 24, 36}, 48));
22    CHECK(!has_tiny_stage({24}, 48));
23    CHECK(has_tiny_stage({47}, 48));
24    CHECK(has_tiny_stage({2}, 48));
25    CHECK(!has_tiny_stage({3}, 48));
26    CHECK(smallest_stage({}, 48) == 48);
27    // the restriction needs the layers for it
28    CHECK(restriction_possible(48, 4));
29    CHECK(restriction_possible(12, 4));
30    CHECK(!restriction_possible(11, 4));
31    CHECK(!restriction_possible(48, 1));
32
33    // #1440: two equal cards, 48 layers x 512 experts of ~2 MiB (IQ3_S-like).  Card 0 has more room than card 1 (the
34    // last stage also carries the drafter).  Every K whose two caches hold all their pairs predicts the same time.
35    {
36        const int64_t L = 48, E = 512, blob = 2 << 20, MiB = 1 << 20;
37        const std::vector<int64_t> room{29000 * MiB, 26200 * MiB};
38        std::vector<int64_t> need(L + 1, 0);
39        for (int64_t l = 0; l < L; ++l) need[(size_t) l + 1] = need[(size_t) l] + E * blob;
40        auto session = [&](int64_t lb, int64_t le) { return (le - lb) * 40 * MiB; };
41        std::vector<Placement> c;
42        for (int64_t k = 2; k < L; ++k) {
43            Placement p;
44            p.cuts = {k};
45            p.min_spare = min_spare(p.cuts, L, room, need, session);
46            // the miss term: the pairs a stage's cache cannot hold (each costs the same here)
47            double missing = 0;
48            for (int i = 0; i < 2; ++i) {
49                const int64_t lb = i == 0 ? 0 : k, le = i == 0 ? k : L;
50                const int64_t fit = (room[(size_t) i] - session(lb, le)) / blob;
51                const int64_t want = (le - lb) * E;
52                if (fit < want) missing += (double) (want - (fit > 0 ? fit : 0));
53            }
54            p.ms = 48 * 0.5 + 190.0 * missing / (double) (L * E);
55            c.push_back(p);
56        }
57        const size_t plain = fastest(c), head = pick_with_headroom(c, 0.002);
58        // the plain pick is the first full placement, and it leaves card 1 (almost) nothing
59        CHECK(c[plain].ms == c[head].ms);
60        CHECK(c[plain].cuts[0] < c[head].cuts[0]);
61        CHECK(c[plain].min_spare < 1024 * MiB);
62        CHECK(c[head].min_spare > 1536 * MiB);
63        // the pick is the most balanced full placement: no other full one keeps more on its tightest card
64        for (const Placement& p : c)
65            if (p.ms == c[plain].ms) CHECK(p.min_spare <= c[head].min_spare);
66        std::printf("split_rules_test: #1440 plain K=%lld (spare %lld MiB), headroom K=%lld (spare %lld MiB)\n",
67                    (long long) c[plain].cuts[0], (long long) (c[plain].min_spare / MiB), (long long) c[head].cuts[0],
68                    (long long) (c[head].min_spare / MiB));
69        // a slower placement is never taken for its headroom beyond the tolerance
70        std::vector<Placement> two{{{10}, 100.0, 0}, {{20}, 101.0, 5000 * MiB}};
71        CHECK(pick_with_headroom(two, 0.002) == 0);
72        CHECK(pick_with_headroom(two, 0.02) == 1);
73        // exact ties with equal spare keep the first (the old pick)
74        std::vector<Placement> same{{{10}, 100.0, 7}, {{20}, 100.0, 7}};
75        CHECK(pick_with_headroom(same, 0.0) == 0);
76        CHECK(pick_with_headroom({}, 0.1) == 0);
77    }
78    // #2007: the miss scale per link
79    {
80        // the case 0.1.41's scale was made for: CUDA1 on a physical x1 link beside an x16 one
81        const std::vector<double> x1 = link_miss_scale({24.0, 1.6});
82        CHECK(x1[0] == 1.0 && x1[1] > 14.9 && x1[1] < 15.1);
83        // two equal cards on gen3 x16 (2x Tesla T10, P100s): the fitted formula, whatever the link speed
84        const std::vector<double> eq = link_miss_scale({12.0, 12.0});
85        CHECK(eq[0] == 1.0 && eq[1] == 1.0);
86        // a moderate gap (x8 beside x16, or the probe's noise): no scale
87        const std::vector<double> mod = link_miss_scale({11.4, 5.9});
88        CHECK(mod[0] == 1.0 && mod[1] == 1.0);
89        // a 4x gap and more scales by the ratio, capped at 64; unprobed stages stay 1
90        const std::vector<double> big = link_miss_scale({26.0, 6.0, 0.2, -1.0});
91        CHECK(big[0] == 1.0 && big[1] > 4.3 && big[1] < 4.4 && big[2] == 64.0 && big[3] == 1.0);
92        CHECK(link_miss_scale({-1.0, -1.0})[1] == 1.0);
93    }
94    // #2644: RTX 4060 Ti 16 GB + RTX 3060 12 GB, 48 layers.  The reporter's numbers: CUDA1 5.50 GiB free of 11.63 (the
95    // 3060 side), CUDA0 the 16 GB card with ~14.2 GiB room.  The #1616 retry alone took K=3 (3 layers on the second card)
96    // although the first pick K=2 was the same problem; the share of the room gives it 13 layers of 48.
97    {
98        const int64_t MiB = 1 << 20, L = 48;
99        const std::vector<int64_t> room{14500 * MiB, 5632 * MiB};
100        const std::vector<int64_t> f = share_floors(room, L);
101        CHECK(f.size() == 2 && f[0] == 34 && f[1] == 13);
102        CHECK(floors_exceed_minimum(f));
103        // the cut K = the second stage's first layer.  With the big card first, the old rule's edge is K=45 (the second card
104        // with 3 layers): below its floor.
105        CHECK(below_floors({45}, L, f));
106        CHECK(below_floors({47}, L, f));
107        CHECK(!below_floors({35}, L, f));   // second card: 13 layers
108        CHECK(!below_floors({34}, L, f));   // 14 layers
109        CHECK(below_floors({36}, L, f));    // 12 layers: under its share
110        CHECK(below_floors({33}, L, f));    // the big card under its share
111        // the same pair the other way round: the small card first
112        const std::vector<int64_t> r = share_floors({room[1], room[0]}, L);
113        CHECK(r[0] == 13 && r[1] == 34);
114        CHECK(below_floors({3}, L, r));     // the old retry's K=3 starves the small card
115        CHECK(!below_floors({13}, L, r));
116        CHECK(!below_floors({14}, L, r));
117        // a predicted-time pick under the floors sits at the edge of the floor, not at the three-layer edge
118        // (layer ms 0.82 on the 4060 Ti, 1.45 on the 3060 as the engine prices them: fewer layers on the 3060 predict faster)
119        auto pick = [&](const std::vector<int64_t>& fl) {
120            int64_t best_k = -1;
121            double best = 1e30;
122            for (int64_t k = 2; k < L; ++k) {
123                if (!fl.empty() && below_floors({k}, L, fl)) continue;
124                if (smallest_stage({k}, L) < (fl.empty() ? kMinStageLayers : 0)) continue;
125                const double ms = (double) k * 1.45 + (double) (L - k) * 0.82;   // card 0 = the 3060 here
126                if (ms < best) { best = ms; best_k = k; }
127            }
128            return best_k;
129        };
130        CHECK(pick({}) == 3);              // today: the 3060 gets 3 layers
131        CHECK(pick(r) == 13);              // with the floors: its share
132        std::printf("split_rules_test: #2644 floors %lld,%lld; 3060 first: old retry K=%lld, share retry K=%lld\n",
133                    (long long) f[0], (long long) f[1], (long long) pick({}), (long long) pick(r));
134        // identical cards: the even split, and the floors never exceed the layers
135        const std::vector<int64_t> eq = share_floors({10000 * MiB, 10000 * MiB}, L);
136        CHECK(eq[0] == 24 && eq[1] == 24);
137        const std::vector<int64_t> three = share_floors({1 * MiB, 1 * MiB, 30000 * MiB}, 12);
138        CHECK(three[0] == 3 && three[1] == 3 && three[2] == 6);   // lifted to 3, the big one trimmed to fit 12
139        CHECK(share_floors({0, 0}, L)[0] == kMinStageLayers && share_floors({-5, 0}, L)[1] == kMinStageLayers);
140        CHECK(!floors_exceed_minimum(share_floors({0, 0}, L)));
141        CHECK(!below_floors({}, 48, {}));
142    }
143    // PR #4522: only CUDA0 (stage 0, weights loaded in full when the search runs) is credited what the trim frees; later stages never
144    {
145        const int64_t MiB = 1 << 20;
146        CHECK(trim_credit(0, 2169 * MiB) == 2169 * MiB);
147        CHECK(trim_credit(1, 2169 * MiB) == 0);
148        CHECK(trim_credit(2, 3000 * MiB) == 0);
149        CHECK(trim_credit(0, 0) == 0);
150        CHECK(trim_credit(0, -5) == 0);   // trim off: trim_frees is 0 (never negative); a negative figure is no credit
151    }
152    if (failed == 0) std::printf("split_rules_test: ok\n");
153    return failed ? 1 : 0;
154}