okami.mom/strata

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

Niko1221Merge branch 'f-0212-int' into f-0212-splitdf2c65b9f8be

main
5.6 KiB100 linesraw
1// tests/program/split_walk_test.cpp - the layer split's spill walk (include/strata/program/split_walk.hpp): who is short, who is not, and what is given up in which order.
2#include "strata/program/split_walk.hpp"
3
4#include <cstdint>
5#include <cstdio>
6#include <string>
7
8using namespace strata::program::split_walk;
9
10namespace {
11int g_fail = 0;
12void check(bool ok, const std::string& what) {
13    if (ok) return;
14    std::fprintf(stderr, "FAIL: %s\n", what.c_str());
15    ++g_fail;
16}
17// IQ3_S on Qwen3.8-Flash-Next, 128K, int8 KV, window 32768, MTP: the figures the 8 GB start log gave (3485 MiB dense set, 835 MiB draft layer, 410 MiB head)
18Model iq3s() {
19    Model m;
20    m.dense_mib = 1466 + 2019;
21    m.session_mib = 400;
22    m.head_mib = 410;
23    m.drafter_mib = 1000;
24    m.slot_mib = 3;
25    m.reserve_mib = 796;
26    return m;
27}
28}  // namespace
29
30int main() {
31    const Model m = iq3s();
32    // 1. the trim: 8 GB cards are short, 12 GB and bigger are not (a big card never reaches the walk)
33    check(trim_needed(7635, m, true), "an 8 GB card holding the head and the drafter is short");
34    check(trim_needed(7635, m, false), "an 8 GB card without them is short too");
35    check(!trim_needed(12288, m, true), "a 12 GB card is not short");
36    check(!trim_needed(16384, m, true), "a 16 GB card is not short");
37    check(!trim_needed(97000, m, true), "a 96 GB card is never short");
38    check(!trim_needed(0, m, true), "an unreadable card (0 free) decides nothing");
39    check(untrimmed_need_mib(m, true) > untrimmed_need_mib(m, false), "the last stage needs more");
40    // monotone in the card: the threshold is one number
41    int64_t edge = 0;
42    for (int64_t f = 1000; f < 40000; ++f)
43        if (!trim_needed(f, m, true)) { edge = f; break; }
44    check(edge == untrimmed_need_mib(m, true), "the threshold is the need");
45    for (int64_t f = edge; f < edge + 5000; f += 97) check(!trim_needed(f, m, true), "never short above the threshold");
46    // a model with a smaller dense set (a smaller quant) is short later
47    Model small = m;
48    small.dense_mib = 1500;
49    check(!trim_needed(7635, small, false), "a small dense set fits an 8 GB card untrimmed");
50    // 2. fewer cards
51    check(!card_usable(2048), "a card with 2 GB free leaves the split");
52    check(card_usable(4250), "a 6 GB card (4.15 GiB free) stays: the other steps make it fit");
53    check(card_usable(7635), "an 8 GB card stays");
54    check(card_usable(kMinCardMib) && !card_usable(kMinCardMib - 1), "the edge");
55    // 3. the draft layer: the single card's arithmetic, unchanged
56    const uint64_t layer = 835ull << 20, head = 410ull << 20;
57    check(draft_layer_need_bytes(layer, head) == layer + head + (220ull << 20) + (1550ull << 20), "the single card's need");
58    check(draft_layer_short(1830ull << 20, layer, head), "1.8 GiB free: the layer goes");
59    check(!draft_layer_short(3640ull << 20, layer, head), "3.6 GiB free (trimmed 8 GB card): the layer stays");
60    check(!draft_layer_short(0, layer, head), "0 free is an unreadable figure");
61    check(dflash2_need_bytes(715ull << 20, 213ull << 20) < draft_layer_need_bytes(layer, head), "DFlash2 needs less than the draft layer");
62    // 4. the KV window walks 32768 -> 20480 -> 8192 -> 4096 -> stop, and never goes past the context or the engine's minimum
63    check(next_kv_window(32768, 131072, 4096) == 20480, "32768 -> 20480");
64    check(next_kv_window(20480, 131072, 4096) == 8192, "20480 -> 8192");
65    check(next_kv_window(8192, 131072, 4096) == 4096, "8192 -> 4096");
66    check(next_kv_window(4096, 131072, 4096) == 0, "4096 is the last");
67    check(next_kv_window(0, 131072, 4096) == 20480, "the whole context in VRAM goes to 20480");
68    check(next_kv_window(0, 16384, 4096) == 8192, "a context of 16K has no 20480 step");
69    check(next_kv_window(0, 4096, 4096) == 0, "a context at the minimum has none");
70    check(next_kv_window(8192, 131072, 8192) == 0, "a larger engine minimum stops the walk");
71    // 5. the shares of the layers
72    int64_t fm[2] = {5000, 5000}, out[2] = {0, 0};
73    proportional_layers(fm, 2, 48, out);
74    check(out[0] == 24 && out[1] == 24, "two equal rooms share 48 layers 24/24");
75    int64_t fm1[2] = {5285, 3875};
76    proportional_layers(fm1, 2, 48, out);
77    check(out[0] + out[1] == 48 && out[0] == 28 && out[1] == 20, "the card that carries the head and the drafter takes fewer layers (28/20)");
78    int64_t fm2[2] = {9000, 5000};
79    proportional_layers(fm2, 2, 48, out);
80    check(out[0] + out[1] == 48 && out[0] > out[1], "the bigger room takes more layers");
81    int64_t fm3[3] = {5000, 5000, 5000}, out3[3];
82    proportional_layers(fm3, 3, 48, out3);
83    check(out3[0] == 16 && out3[1] == 16 && out3[2] == 16, "three equal rooms 16/16/16");
84    int64_t fm4[2] = {90000, 100}, out4[2];
85    proportional_layers(fm4, 2, 48, out4);
86    check(out4[0] + out4[1] == 48 && out4[0] >= 2 && out4[1] >= 0, "a tiny room leaves the sum right");
87    int64_t fm5[2] = {0, 0}, out5[2];
88    proportional_layers(fm5, 2, 48, out5);
89    check(out5[0] + out5[1] == 48, "rooms of 0 still sum to the layers");
90    // 6. fewer cards: feasible when the rooms plus the relief pay for every layer
91    int64_t r8[2] = {5200, 5200};
92    check(split_feasible(r8, 2, 48, 90, 1410), "two 8 GB cards (5.2 GiB of room each) hold the model");
93    int64_t r6[2] = {1744, 1552};
94    check(split_feasible(r6, 2, 48, 87, 1410), "two 6 GB cards hold it only with the drafter and the head moved off");
95    check(!split_feasible(r6, 2, 48, 87, 0), "...and not without that relief");
96    int64_t r3[2] = {2000, 100};
97    check(!split_feasible(r3, 2, 48, 90, 1410), "two cards with 2 GB and 0.1 GB of room do not: fewer cards");
98    if (g_fail == 0) std::printf("split_walk_test: ok\n");
99    return g_fail == 0 ? 0 : 1;
100}