okami.mom/strata

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

Niko1221Merge f-switch-point3 216af487d into f-p4-int (the per-card prompt switch rule is on by default for NVIDIA sm_86 cards)30c2b84f2

main
7.8 KiB140 linesraw
1// switch_point_rule.hpp: the floor from which a prompt chunk takes the streamed walk, from the card's share of non-resident experts.  No GPU, no model.
2#include <cmath>
3#include <cstdio>
4
5#include "strata/prefill/switch_point_rule.hpp"
6
7namespace sp = strata::prefill::switch_point;
8
9static int failed = 0;
10#define CHECK(x) do { if (!(x)) { std::fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #x); ++failed; } } while (0)
11
12int main() {
13    static_assert(sp::kFloorDefault == 1024, "the default floor is 1,024");
14    const sp::Constants k;
15    const int64_t total = 24576;   // 48 layers x 512 experts (Flash-Next)
16
17    // every expert resident (or an unknown card): exactly the default
18    CHECK(sp::floor_for(total, total) == 1024);
19    CHECK(sp::floor_for(total + 5, total) == 1024);
20    CHECK(sp::floor_for(0, 0) == 1024);
21    CHECK(sp::floor_for(-1, total) == 1024);
22    CHECK(sp::floor_for(5000, 0) == 1024);
23
24    // the floor is a multiple of the chunk grid, within [1024, kFloorMax], for every resident count
25    int64_t prev = 0;
26    bool seen_move = false;
27    for (int64_t res = 0; res <= total; res += 64) {
28        const int64_t f = sp::floor_for(res, total);
29        CHECK(f >= 1024 && f <= sp::kFloorMax);
30        CHECK((f - 1024) % sp::kGrid == 0);
31        if (f != 1024) seen_move = true;
32        (void) prev;
33        prev = f;
34    }
35    CHECK(seen_move);
36
37    // the A6000 / UD-Q4_K_XL card (55% resident): between the measured 2,049 (staged 1.28x faster) and 8,193 (staged 26% slower), about the measured crossing at 4,097
38    {
39        const int64_t res = (int64_t) (0.55 * (double) total);
40        const int64_t f = sp::floor_for(res, total);
41        CHECK(f > 2049 && f < 8193);
42    }
43    // the same card with the measured X (about 12,097 non-resident): the model's own crossing is 4,0xx
44    {
45        const int64_t f = sp::floor_for(total - 12097, total);
46        CHECK(f >= 3584 && f <= 4608);
47        CHECK(sp::advantage_ms(1100, 12097, k) > 1000.0);     // measured 1,390 ms (3,236 against 1,838)
48        CHECK(sp::advantage_ms(2049, 12097, k) > 500.0);      // measured 710 ms
49        CHECK(std::fabs(sp::advantage_ms(4097, 12097, k)) < 120.0);   // measured 18 ms
50        CHECK(sp::advantage_ms(8193, 12097, k) < -700.0);     // measured -950 ms
51    }
52    // a card with fewer non-resident experts stages less often: the floor does not rise as more experts are resident
53    {
54        int64_t last = sp::kFloorMax + 1;
55        for (int64_t res = total / 8; res <= total; res += 256) {
56            const int64_t f = sp::floor_for(res, total);
57            CHECK(f <= last);
58            last = f;
59        }
60    }
61    // the uncovered share: 1 below t0, falling, never above 1
62    CHECK(sp::uncovered(100, k) == 1.0);
63    CHECK(sp::uncovered(1100, k) > sp::uncovered(4097, k));
64    CHECK(sp::uncovered(8193, k) < 0.2);
65    CHECK(sp::uncovered(0, k) == 1.0);
66
67    // per-card scaling: the A6000 (3.19 MB experts, 12.2 GB/s, 84 SMs, 13,580 of 24,576 resident) and the A4000 (1.44 MB, 10.5 GB/s, 48 SMs, 7,836 resident), CPU share off:
68    // measured crossings about 3,840 and 1,930 tokens, the rule's floors are within one grid step of them
69    {
70        const sp::Card a6000{3.19, 12.2, 84}, a4000{1.44, 10.5, 48};
71        const int64_t f6 = sp::floor_for_card(13580, total, a6000);
72        const int64_t f4 = sp::floor_for_card(7836, total, a4000);
73        CHECK(f6 >= 3584 && f6 <= 4096);
74        CHECK(f4 >= 1792 && f4 <= 2048);
75        CHECK(f4 < f6);                                              // the slower card of the two crosses earlier, as measured
76        CHECK((f6 - 1024) % sp::kGrid == 0 && (f4 - 1024) % sp::kGrid == 0);
77        // an unknown card (no probe, no SM count): the default
78        CHECK(sp::floor_for_card(13580, total, sp::Card{0.0, 12.2, 84}) == 1024);
79        CHECK(sp::floor_for_card(13580, total, sp::Card{3.19, 0.0, 84}) == 1024);
80        CHECK(sp::floor_for_card(13580, total, sp::Card{3.19, 12.2, 0}) == 1024);
81        // every expert resident: the default whatever the card
82        CHECK(sp::floor_for_card(total, total, a6000) == 1024);
83        // the A6000-class card's constants are the fit's own
84        const sp::Constants kc = sp::constants_for(sp::Card{});
85        CHECK(std::fabs(kc.c_p - 0.2612) < 1e-9 && std::fabs(kc.a_s - 0.2294) < 1e-9);
86        // faster link, same card: a smaller floor; more SMs, same link: a higher one
87        CHECK(sp::floor_for_card(13580, total, sp::Card{3.19, 24.0, 84}) <= f6);
88        CHECK(sp::floor_for_card(13580, total, sp::Card{3.19, 12.2, 120}) >= f6);
89    }
90    // the share's limit: the floor times the measured ratio on the grid, 0 (the share's own default stays) when the floor did not move
91    CHECK(sp::share_limit_for(1024) == 0);
92    CHECK(sp::share_limit_for(2048) == 3072);                        // A4000: measured break-even about 2,900
93    CHECK(sp::share_limit_for(3840) % sp::kGrid == 0 && sp::share_limit_for(3840) > 3840);
94
95    // the class the rule is on for by default: CUDA, discrete, one GPU, link probe ran, compute capability 8.6; STRATA_SWITCH_POINT=0 is the way back, =1 asks for it elsewhere
96    {
97        sp::DeviceClass in;
98        in.cuda = true; in.discrete = true; in.single_gpu = true; in.link_probed = true; in.cc_major = 8; in.cc_minor = 6;
99        CHECK(sp::in_default_class(in));
100        CHECK(sp::enabled(in, -1));                                  // nothing set: on
101        CHECK(!sp::enabled(in, 0));                                  // STRATA_SWITCH_POINT=0 (or any value but 1): the way back
102        CHECK(sp::enabled(in, 1));                                   // STRATA_SWITCH_POINT=1: on
103        // every other compute capability is out of the class with nothing set
104        const int other[][2] = {{6, 0}, {6, 1}, {7, 0}, {7, 5}, {8, 0}, {8, 7}, {8, 9}, {9, 0}, {10, 0}, {12, 0}, {12, 1}, {0, 0}};
105        for (const auto& cc : other) {
106            sp::DeviceClass d = in;
107            d.cc_major = cc[0]; d.cc_minor = cc[1];
108            CHECK(!sp::in_default_class(d));
109            CHECK(!sp::enabled(d, -1));
110            CHECK(sp::enabled(d, 1));                                // the explicit opt-in still works on another CUDA card
111            CHECK(!sp::enabled(d, 0));
112        }
113        // each other condition on its own takes a card out of the class
114        { sp::DeviceClass d = in; d.single_gpu = false; CHECK(!sp::enabled(d, -1)); CHECK(!sp::enabled(d, 0)); }    // two cards / a layer split
115        { sp::DeviceClass d = in; d.link_probed = false; CHECK(!sp::enabled(d, -1)); }                              // no probe
116        { sp::DeviceClass d = in; d.discrete = false; CHECK(!sp::enabled(d, -1)); }                                 // integrated / shared memory
117        { sp::DeviceClass d = in; d.cuda = false; CHECK(!sp::enabled(d, -1)); CHECK(!sp::enabled(d, 1)); CHECK(!sp::enabled(d, 0)); }   // HIP: never, whatever the cc says
118        { sp::DeviceClass d; CHECK(!sp::enabled(d, -1)); CHECK(!sp::enabled(d, 1)); }                               // nothing known
119        // the variable: unset or empty = default, exactly "1" = on, anything else = off
120        CHECK(sp::env_state(nullptr) == -1);
121        CHECK(sp::env_state("") == -1);
122        CHECK(sp::env_state("1") == 1);
123        CHECK(sp::env_state("0") == 0);
124        CHECK(sp::env_state("off") == 0);
125        CHECK(sp::env_state("11") == 0);
126        // the class gives the measured floors: the A6000 and the A4000 are both 8.6
127        CHECK(sp::kClassCcMajor == 8 && sp::kClassCcMinor == 6);
128        CHECK(sp::floor_for_card(13580, total, sp::Card{3.14, 12.2, 84}) == 3584);
129        CHECK(sp::share_limit_for(3584) == 5632);
130        CHECK(sp::floor_for_card(7836, total, sp::Card{1.44, 11.0, 48}) == 2048);
131        CHECK(sp::share_limit_for(2048) == 3072);
132    }
133
134    if (failed) {
135        std::fprintf(stderr, "switch_point_rule_test: %d check(s) failed\n", failed);
136        return 1;
137    }
138    std::printf("switch_point_rule_test: OK\n");
139    return 0;
140}