okami.mom/strata

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

Niko1221Merge f-chunk-fit-out's test case: fit_out_rule_test records the RAM copy's 96-slot ring on a 12 GB cardbd53f645c

main
9.1 KiB147 linesraw
1// Which tail the serve auto chunk scan counts in a build without MMQ (fit_out_rule.hpp): no GPU, no model.
2//
3// The scan is modelled with the loans measured on an RTX 5070 12 GB (Q2_0 plain pack, build without MMQ, 2026-11-14,
4// RESULTS-CHUNK-BALANCE block 4k): 3,572 slots at 9,984 tokens with today's tail (the start-up borrow), 3,391 at 9,984 with
5// tail 0 (a request's relayout), 3,417 at 9,216 with today's tail; 0.2018 slots per token between 9,216 and 9,984.  Cache
6// slots from the auto sizing (1.3175 MiB a slot): 3,851 on the 12 GB card, about 6,960 for 16 GB.  The model is the scan's test
7// (loan_fits, kAutoLendPct 90, keep 128) and its bisection (biggest_chunk) applied to a linear loan; the engine's own loan
8// comes from Prefill::bytes_needed.
9#include <cstdint>
10#include <cstdio>
11#include <initializer_list>
12
13#include "strata/prefill/fit_out_rule.hpp"
14
15namespace fo = strata::prefill::fit_out;
16
17static int failed = 0;
18#define CHECK(x) do { if (!(x)) { std::fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #x); ++failed; } } while (0)
19
20// slots a chunk of t tokens lends (tail -1: floor - 1 rows; tail 0), from the 5070's loans
21static int64_t loan(int64_t t, int64_t tail) {
22    const int64_t a = tail < 0 ? 1558 : 1377;   // 3,572 - 2,014 and 3,391 - 2,014 (2,014 = 9,984 x 2,018 / 10,000)
23    return a + t * 2018 / 10000;
24}
25// the scan at one tail: the largest chunk up to `ceiling` whose loan passes the 90 % cap and the 128-slot floor
26static int64_t scan(int64_t slots, int64_t tail, int64_t ceiling = 32768) {
27    return fo::biggest_chunk(ceiling, [&](int64_t t) { return fo::loan_fits(loan(t, tail), slots, 128, 90, true); });
28}
29static fo::ScanCase one_gpu_case() {
30    fo::ScanCase c;
31    c.auto_chunk = c.one_gpu = c.tail_loan = c.equal_cut = c.fused_at_chunk = true;
32    return c;
33}
34
35int main() {
36    // the model reproduces the measured loans and today's chunk
37    CHECK(loan(9984, -1) == 3572);
38    CHECK(loan(9984, 0) == 3391);
39    CHECK(loan(9216, -1) == 3416 || loan(9216, -1) == 3417);
40
41    // which tail: 0 only in the case where no layout above small_max can carry a short tail
42    const fo::ScanCase on = one_gpu_case();
43    CHECK(fo::scan_tail(on) == 0);
44    {
45        fo::ScanCase c = on; c.auto_chunk = false; CHECK(fo::scan_tail(c) == -1);      // --prefill N
46        c = on; c.one_gpu = false; CHECK(fo::scan_tail(c) == -1);                      // a split, a peer card, slots, head alias
47        c = on; c.tail_loan = false; CHECK(fo::scan_tail(c) == -1);                    // STRATA_PREFILL_TAIL_LOAN=0
48        c = on; c.equal_cut = false; CHECK(fo::scan_tail(c) == -1);                    // STRATA_PREFILL_EQUAL=0: today's count
49        c = on; c.small_path = true; CHECK(fo::scan_tail(c) == -1);                    // the small-card prompt path
50        c = on; c.fused_at_chunk = false; CHECK(fo::scan_tail(c) == -1);               // no fused layout: no tail is carved
51    }
52    // with the equal cut off the chunk is today's whatever the tail-0 scan would find
53    CHECK(fo::pick(9216, 10240, -1) == 9216);
54    CHECK(fo::pick(9216, 10240, 0) == 10240);
55    CHECK(fo::pick(9216, 8960, 0) == 9216);   // never smaller than today's
56
57    // 12 GB (3,851 slots): today 9,216 (measured), tail 0 10,240 (the build with MMQ takes 9,984 there)
58    CHECK(scan(3851, -1) == 9216);
59    CHECK(scan(3851, 0) == 10240);
60    CHECK(fo::pick(scan(3851, -1), scan(3851, 0), fo::scan_tail(on)) == 10240);
61    // the start-up layout of 10,240 with today's tail keeps the 128-slot floor (the engine checks it before taking the chunk)
62    CHECK(fo::loan_fits(loan(10240, -1), 3851, 128, 90, false));
63    // 16 GB (about 6,960 slots): 23,296 today, 24,064 with tail 0
64    CHECK(scan(6960, -1) == 23296);
65    CHECK(scan(6960, 0) == 24064);
66    // 24 GB (about 13,178 slots): the 32,768 ceiling either way
67    CHECK(scan(13178, -1) == 32768 && scan(13178, 0) == 32768);
68
69    // the band where a tail-0 count would cross the small-card line (chunk 8,192): 3,409-3,566 slots.  The small-card decision is
70    // taken on today's count (generate.cpp: the tail-0 scan runs after it, and only when the path was not taken), so a card there
71    // keeps the path it takes today.
72    for (int64_t slots : {3409, 3480, 3566}) {
73        const int64_t today = scan(slots, -1);
74        CHECK(today < 8192);              // today: the small-card path is chosen
75        CHECK(scan(slots, 0) >= 8192);    // a tail-0 count would have left it
76        fo::ScanCase c = on;
77        c.small_path = today < 8192;      // decided on today's count
78        CHECK(fo::scan_tail(c) == -1);
79        CHECK(fo::pick(today, scan(slots, 0), fo::scan_tail(c)) == today);
80    }
81    // just above the band the small-card path is not chosen today and the tail-0 count applies
82    CHECK(scan(3567, -1) >= 8192 || scan(3600, -1) >= 8192);
83
84    // the request path's today's-cut fallback after a tail-0 scan: only at or below small_max (0.1.39's chunk, 8,192 here)
85    CHECK(fo::today_cut_ok(-1, 10240, 8192));
86    CHECK(fo::today_cut_ok(0, 8192, 8192));
87    CHECK(!fo::today_cut_ok(0, 10240, 8192));
88
89    // the RAM copy's lend region (--resident-experts, --resident-budget-gib, the automatic RAM fallback) is sized before the serve
90    // scan runs (generate.cpp, the resident block): it holds the start-up loan of the chunk the scan can take, with the equal cut on
91    // and off.  The scan takes the tail-0 chunk only when the copy keeps that whole loan (region_holds); a copy that keeps less
92    // (RAM short: the region's slots are kept from the last slot down while they fit the budget) gets today's chunk, so no start
93    // reads more lent experts from the file than today.  -1: the arena (every expert in RAM, no region).
94    struct Card { int64_t slots, today, tail0, extra; };
95    for (const Card cd : {Card{3851, 9216, 10240, 207}, Card{6960, 23296, 24064, 155}}) {
96        for (const bool eq : {true, false}) {
97            fo::ScanCase c = on;
98            c.equal_cut = eq;
99            const int64_t tail = fo::scan_tail(c);
100            const int64_t today = scan(cd.slots, -1);
101            CHECK(today == cd.today);
102            const int64_t k_today = loan(today, -1);
103            const int64_t c0 = scan(cd.slots, 0);
104            CHECK(c0 == cd.tail0);
105            int64_t k0 = c0 > today ? loan(c0, -1) : 0;
106            if (!fo::loan_fits(k0, cd.slots, 128, 90, false)) k0 = 0;   // the start-up layout keeps the 128-slot floor
107            CHECK(k0 - k_today == cd.extra);   // the region's extra slots (x 1.3175 MiB of RAM: 273 MiB at 12 GB, 204 at 16 GB)
108            const int64_t region = fo::region_slots(k_today, k0, tail);
109            CHECK(region == (eq ? k0 : k_today));
110            for (const int64_t kept : {region, region - 1, k_today, k_today - 1, int64_t(-1)}) {
111                int64_t chunk = fo::pick(today, c0, tail);
112                if (chunk != today && !fo::region_holds(loan(chunk, -1), kept)) chunk = today;   // the scan's guard
113                const int64_t startup = loan(chunk, -1);
114                CHECK(startup <= region);                    // the serve loan never outgrows the region
115                if (kept >= k_today) CHECK(startup <= kept); // every lent expert kept in RAM wherever today's were
116                CHECK(loan(chunk, 0) <= startup);            // a request's relayout at the chunk lends no more than the start-up
117                if (kept < 0 || kept >= region) CHECK(chunk == (eq ? cd.tail0 : cd.today));
118                else CHECK(chunk == cd.today);               // RAM short of the larger region: today's chunk
119            }
120        }
121    }
122    CHECK(fo::region_slots(3417, 0, 0) == 3417);       // no tail-0 chunk above today's: today's region
123    CHECK(fo::region_slots(3417, 3624, -1) == 3417);   // today's count: today's region
124    CHECK(fo::region_holds(3624, -1) && fo::region_holds(3624, 3624) && !fo::region_holds(3624, 3623));
125
126    // the RAM-copy mode on a 12 GB card: the pinned share is below 0.9, so the ring is 96 slots, not 1,024, and the cache 3,899
127    // slots.  Measured with and without this rule: chunk 13,056, start-up loan 3,277 slots, region 3,293; the tail-0 scan finds no
128    // larger chunk there.  With the region kept whole or short (2,469 at a 29.8 GiB budget) the chunk stays 13,056: the guard has
129    // nothing to undo, the layout is unchanged and nothing is refused.  One measured loan only: this is not a model of the 96-slot
130    // ring scan.
131    {
132        const int64_t slots = 3899, today = 13056, k_today = 3277, region = 3293;
133        CHECK(fo::loan_fits(k_today, slots, 128, 90, true));   // the scan's test passes at the measured loan
134        CHECK(fo::region_slots(k_today, 0, 0) == k_today);     // no tail-0 chunk above today's: today's loan
135        CHECK(k_today <= region);
136        CHECK(fo::region_holds(k_today, region) && !fo::region_holds(k_today, 2469));
137        for (const int64_t kept : {region, region - 1, k_today, int64_t(2469), int64_t(-1)}) {
138            int64_t chunk = fo::pick(today, today, fo::scan_tail(on));
139            if (chunk != today && !fo::region_holds(k_today, kept)) chunk = today;   // the scan's guard
140            CHECK(chunk == today);
141        }
142    }
143
144    if (failed) { std::fprintf(stderr, "fit_out_rule_test: %d failed\n", failed); return 1; }
145    std::printf("fit_out_rule_test: all passed\n");
146    return 0;
147}