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
9.1 KiB147 linesraw
1// A prompt piece below the walk floor lends buffers for the staged walk, not for the rounded chunk's streamed one: no GPU, no model.
2#include <cstdint>
3#include <cstdio>
4#include <initializer_list>
5
6#include "strata/prefill/loan_rules.hpp"
7
8using strata::prefill::loan_rules::walk_chunk;
9using strata::prefill::loan_rules::tail_need;
10using strata::prefill::loan_rules::tail_layout;
11using strata::prefill::loan_rules::segment_tail;
12using strata::prefill::loan_rules::reuse_ok;
13using strata::prefill::loan_rules::next_tail;
14
15static int failed = 0;
16#define CHECK(x) do { if (!(x)) { std::fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #x); ++failed; } } while (0)
17
18// the request's own rounding (generate.cpp request_chunk): up to the 256-token grid
19static int64_t grid(int64_t t) { return (t + 255) / 256 * 256; }
20
21int main() {
22    // the default floor: 999 tokens round to 1,024 (the streamed layout) and walk staged -> 1,023
23    CHECK(grid(999) == 1024 && walk_chunk(999, grid(999), 1024) == 1023);
24    CHECK(walk_chunk(769, grid(769), 1024) == 1023);      // the first piece that rounds to the floor
25    CHECK(walk_chunk(768, grid(768), 1024) == 768);       // an exact multiple under the floor: nothing to correct
26    CHECK(walk_chunk(1023, grid(1023), 1024) == 1023);
27    CHECK(walk_chunk(1024, grid(1024), 1024) == 1024);    // at the floor: the streamed walk, the chunk stays
28    CHECK(walk_chunk(1025, grid(1025), 1024) == 1280);
29    CHECK(walk_chunk(300, grid(300), 1024) == 512);       // far under the floor: the staged layout already
30    // the CPU share's limit
31    CHECK(walk_chunk(3071, grid(3071), 3072) == 3071);
32    CHECK(walk_chunk(2817, grid(2817), 3072) == 3071);
33    CHECK(walk_chunk(2816, grid(2816), 3072) == 2816);
34    CHECK(walk_chunk(3000, grid(3000), 3072) == 3071);
35    CHECK(walk_chunk(3072, grid(3072), 3072) == 3072);
36    CHECK(walk_chunk(3073, grid(3073), 3072) == 3328);
37    // exact multiples of 256 on a floor that is one
38    for (int64_t t = 256; t <= 8192; t += 256) CHECK(walk_chunk(t, t, 1024) == t);
39    // a floor of 64 (the Windows gfx1151 rule): a 63-token piece rounds to 256 and walks staged
40    CHECK(walk_chunk(63, grid(63), 64) == 63);
41    CHECK(walk_chunk(1, grid(1), 64) == 63);
42    CHECK(walk_chunk(64, grid(64), 64) == 256);
43    CHECK(walk_chunk(100, grid(100), 64) == 256);
44    // a floor of 0 or 1: every piece streams, nothing to correct
45    CHECK(walk_chunk(999, grid(999), 0) == 1024);
46    CHECK(walk_chunk(1, grid(1), 1) == 256);
47    // the chunk configured below the floor stays: a piece of 999 under a 512-token chunk reads in several pieces
48    CHECK(walk_chunk(999, 512, 1024) == 512);
49    // never larger than the chunk the caller chose, never below the piece itself
50    for (int64_t floor : std::initializer_list<int64_t>{64, 1024, 2048, 3072, 4096})
51        for (int64_t t = 1; t <= 9000; t += 7) {
52            const int64_t c = walk_chunk(t, grid(t), floor);
53            CHECK(c <= grid(t));
54            CHECK(c >= t || grid(t) < t);
55            if (t < floor) CHECK(c < floor);
56        }
57    // empty or negative requests are passed through
58    CHECK(walk_chunk(0, 256, 1024) == 256 && walk_chunk(10, 0, 1024) == 0);
59    static_assert(walk_chunk(999, 1024, 1024) == 1023, "constexpr");
60
61    // ---- the tail of a lent fused layout (tail_need / tail_layout / segment_tail / reuse_ok)
62    // one chunk at or above the floor: no MMQ rows at all
63    CHECK(tail_need(1024, 1024, 1024) == 0);
64    CHECK(tail_need(8192, 8192, 1024) == 0);
65    CHECK(tail_need(4096, 2048, 1024) == 0);              // two full chunks
66    // a short last chunk is the whole need
67    CHECK(tail_need(4097, 2048, 1024) == 1);
68    CHECK(tail_need(5000, 2048, 1024) == 904);
69    CHECK(tail_need(5120 + 1023, 5120, 1024) == 1023);
70    CHECK(tail_need(5120 + 1024, 5120, 1024) == 0);       // a last chunk at the floor streams: fused
71    // a floor of 3,584 (the rule on an RTX A6000): the same segments now have a tail up to 3,583
72    CHECK(tail_need(8193, 8192, 3584) == 1);
73    CHECK(tail_need(4097, 2048, 3584) == 2048);           // a layout of 2,048-token chunks is below the floor: not fused, every chunk is MMQ's
74    CHECK(tail_need(8193 + 2000, 8192, 3584) == 2001);
75    CHECK(tail_need(8192 + 3584, 8192, 3584) == 0);
76    // a segment read in one chunk below the floor, in a layout whose chunk reaches it (a reused big loan)
77    CHECK(tail_need(2000, 8192, 3584) == 2000);
78    CHECK(tail_need(2000, 8192, 1024) == 0);
79    // a layout whose own chunk is below the floor is not fused: every chunk is MMQ's
80    CHECK(tail_need(5000, 1023, 1024) == 1023 && tail_need(500, 1023, 1024) == 500);
81    CHECK(tail_need(0, 4096, 1024) == 0 && tail_need(10, 0, 1024) == 0);
82    // never above floor - 1, never negative, last chunk exact for every size
83    for (int64_t floor : std::initializer_list<int64_t>{1, 64, 1024, 3072, 3584})
84        for (int64_t c : std::initializer_list<int64_t>{256, 1023, 1024, 2048, 4096, 8192})
85            for (int64_t t = 1; t <= 20000; t += 37) {
86                const int64_t need = tail_need(t, c, floor);
87                CHECK(need >= 0 && need <= (floor > 1 ? floor - 1 : 0));
88                int64_t worst = 0;                          // walk the chunks one by one
89                for (int64_t at = 0; at < t; at += c) {
90                    const int64_t len = t - at < c ? t - at : c;
91                    if (len < floor && len > worst) worst = len;
92                }
93                CHECK(need == worst);
94                const int64_t lay = tail_layout(need, floor);
95                CHECK(lay >= need && (need == 0 ? lay == 0 : lay % 256 == 0 || lay == floor - 1));
96                CHECK(lay <= (floor > 1 ? floor - 1 : 0));
97                CHECK(segment_tail(t, c, floor) == lay);
98            }
99    // the grid: 904 -> 1,024; clamped to today's floor - 1
100    CHECK(tail_layout(904, 3584) == 1024 && tail_layout(1, 3584) == 256 && tail_layout(1023, 1024) == 1023);
101    CHECK(tail_layout(0, 3584) == 0 && tail_layout(5, 1) == 0);
102    // the overflow case: a loan made for an 8,193+ segment whose last chunk is 1 token, then a 2,000-token segment
103    // (the 2,000 tokens read as ONE chunk of the loan's layout, staged under a 3,584 floor)
104    {
105        const int64_t floor = 3584, lent_chunk = 8192;
106        const int64_t lent_tail = segment_tail(8193, lent_chunk, floor);            // 256: the last chunk is 1 token
107        CHECK(lent_tail == 256);
108        CHECK(!reuse_ok(2048, 2000, lent_chunk, lent_tail, floor));                 // needs 2,000 (grid 2,048): relayout
109        CHECK(tail_need(2000, lent_chunk, floor) > lent_tail);
110        CHECK(reuse_ok(256, 100, lent_chunk, lent_tail, floor));                    // 100 <= 256: reuse
111        CHECK(tail_need(4196, lent_chunk, floor) == 0);                              // one chunk of 4,196 >= floor: fused, no tail
112        CHECK(reuse_ok(2048, 4196, lent_chunk, lent_tail, floor));                  // so it reuses
113    }
114    // a smaller want never reuses a smaller loan, a bigger tail never reuses a smaller one
115    CHECK(!reuse_ok(4096, 4096, 2048, 4095, 1024));
116    CHECK(reuse_ok(2048, 4096, 2048, 0, 1024));
117    CHECK(reuse_ok(2048, 4096, 4096, 0, 1024) && !reuse_ok(2048, 4097 + 4096, 4096, 0, 8192));
118    // ---- grow-only (next_tail): need grows, need shrinks, chunk grows
119    {
120        const int64_t floor = 3584;
121        CHECK(next_tail(8192, -1, 8192, 256, floor) == 256);          // a start-up layout (default-sized): the need of the moment
122        CHECK(next_tail(8192, 1024, 8192, 256, floor) == 1024);       // need shrinks: the tail stays (no relayout)
123        CHECK(next_tail(8192, 1024, 8192, 0, floor) == 1024);
124        CHECK(next_tail(8192, 1024, 8192, 1024, floor) == 1024);      // equal: stays
125        CHECK(next_tail(8192, 1024, 8192, 1280, floor) == 3583);      // need grows: today's size, once
126        CHECK(next_tail(8192, 3583, 8192, 3000, floor) == 3583);      // and then it is today's: nothing grows past it
127        CHECK(next_tail(8192, 1024, 4096, 256, floor) == 256);        // chunk shrinks: another layout, from its own need
128        CHECK(next_tail(4096, 1024, 8192, 2048, floor) == 2048);      // chunk grows: from the need of that moment
129        CHECK(next_tail(8192, 0, 8192, 0, floor) == 0);               // no tail needed, none carved
130        CHECK(next_tail(8192, 0, 8192, 256, floor) == 3583);
131        CHECK(next_tail(8192, 256, 8192, 256, 1) == 256 && next_tail(8192, 0, 8192, 5, 1) == 0);
132        // a sequence of needs on one chunk: at most one growth, every layout tail covers its segment, never shrinks
133        int64_t cap = -1, grown = 0;
134        for (int64_t need : std::initializer_list<int64_t>{256, 0, 1024, 256, 2048, 3583, 0, 512, 3000}) {
135            const int64_t t = next_tail(8192, cap, 8192, need, floor);
136            CHECK(t >= need);
137            if (cap >= 0) CHECK(t >= cap);
138            if (t != cap) ++grown;
139            cap = t;
140        }
141        CHECK(grown == 2);   // the first carve (256), one growth (1024 > 256 -> today's 3583), then it stays
142    }
143    static_assert(tail_need(5000, 2048, 1024) == 904 && segment_tail(5000, 2048, 1024) == 1023, "constexpr");
144    if (failed != 0) return 1;
145    std::printf("loan_rules_test: all checks passed\n");
146    return 0;
147}