okami.mom/strata

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

Niko1221Merge f-equal-min2 (49b61d0d4): two equal chunks take the equal cut from 5,400 tokens each (a prompt of 10,800 tokens or more), three or more from 6,1444e5eef6e3

main
14.1 KiB197 linesraw
1// How a long prompt segment is cut into chunks (equal_cut_rule.hpp): no GPU, no model.
2#include <algorithm>
3#include <cstdint>
4#include <cstdio>
5#include <initializer_list>
6#include <limits>
7
8#include "strata/prefill/equal_cut_rule.hpp"
9
10namespace ec = strata::prefill::equal_cut;
11
12static int failed = 0;
13#define CHECK(x) do { if (!(x)) { std::fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #x); ++failed; } } while (0)
14
15// The older opt-in's lambda as it stands in generate.cpp (equal_chunk with STRATA_PREFILL_EQUAL=1), kept here as the reference
16static int64_t old_request_chunk(int64_t tokens, int64_t max_chunk) {
17    if (tokens <= 0 || max_chunk <= 0) return 0;
18    const int64_t rounded = tokens > std::numeric_limits<int64_t>::max() - 255 ? tokens : ((tokens + 255) / 256) * 256;
19    return std::min(max_chunk, rounded);
20}
21static int64_t old_equal_chunk(int64_t tokens, int64_t max_chunk, int64_t floor) {
22    const int64_t plain = old_request_chunk(tokens, max_chunk);
23    if (tokens <= 0 || max_chunk <= 0) return plain;
24    const int64_t n = tokens / max_chunk + (tokens % max_chunk != 0);
25    const int64_t last = tokens - (n - 1) * max_chunk;
26    if (n <= 1 || last < floor) return plain;
27    const int64_t per = tokens / n + (tokens % n != 0);
28    const int64_t rounded = per > std::numeric_limits<int64_t>::max() - 255 ? per : ((per + 255) / 256) * 256;
29    return std::min(max_chunk, rounded);
30}
31
32// the chunks the reading loop makes of `tokens` at chunk `c` (prefill.cpp run_impl: T = min(C, n - c0)): count, smallest, largest
33struct Cutout { int64_t n, lo, hi, last; };
34static Cutout cut_of(int64_t tokens, int64_t c) {
35    Cutout r{0, std::numeric_limits<int64_t>::max(), 0, 0};
36    for (int64_t c0 = 0; c0 < tokens; c0 += c) {
37        const int64_t t = std::min(c, tokens - c0);
38        ++r.n;
39        r.lo = std::min(r.lo, t);
40        r.hi = std::max(r.hi, t);
41        r.last = t;
42    }
43    return r;
44}
45
46int main() {
47    const int64_t F = 1536;   // the plain pack's floor on a discrete card in a build without MMQ
48    // ---- the switch
49    CHECK(ec::pick(nullptr, true) == ec::Cut::equal);     // unset, one GPU: the equal cut
50    CHECK(ec::pick("1", true) == ec::Cut::equal);
51    CHECK(ec::pick("0", true) == ec::Cut::today);         // the way back
52    CHECK(ec::pick("0", false) == ec::Cut::today);
53    CHECK(ec::pick(nullptr, false) == ec::Cut::today);    // a layer split: today's cut
54    CHECK(ec::pick("1", false) == ec::Cut::opt_in);       // a layer split that asks for the older opt-in keeps it
55    CHECK(ec::pick("", true) == ec::Cut::equal);
56    CHECK(ec::pick("", false) == ec::Cut::today);
57
58    // ---- the examples measured on the RTX 5070 (auto chunk 10,240 without MMQ, 9,984 with it)
59    {
60        const int64_t c = ec::equal_chunk(20932, 10240, F);   // today 10,240 x 2 + 452
61        CHECK(c == 6978);
62        const Cutout k = cut_of(20932, c);
63        CHECK(k.n == 3 && k.hi == 6978 && k.last == 6976);
64    }
65    CHECK(ec::equal_chunk(32000, 10240, F) == 8000 && cut_of(32000, 8000).n == 4 && cut_of(32000, 8000).lo == 8000);
66    CHECK(ec::equal_chunk(14000, 10240, F) == 7000 && cut_of(14000, 7000).n == 2);
67    CHECK(ec::equal_chunk(20000, 9984, F) == 6667 && cut_of(20000, 6667).n == 3 && cut_of(20000, 6667).last == 6666);
68    // 2 x 5,250 is under kEqualMinChunk at a 10,240 chunk: today's 10,240 + 260 (before the kEqualMinChunk rule: 5,250)
69    CHECK(ec::equal_chunk(10500, 10240, F) == 10240 && ec::declined_equal(10500, 10240, F) == 5250);
70    CHECK(ec::equal_chunk(20000, 10240, F) == 10000);
71    CHECK(ec::equal_chunk(22980, 10240, F) == 7660);
72    CHECK(ec::equal_chunk(18932, 10240, F) == 9466);
73    // the engine reads N - 1 prompt tokens in a batch: 20,931 -> 3 x 6,977
74    CHECK(ec::equal_chunk(20931, 10240, F) == 6977 && cut_of(20931, 6977).lo == 6977 && cut_of(20931, 6977).n == 3);
75    // ---- a segment that fits one chunk is unchanged (today's chunk: rounded up to 256, at most the chunk)
76    for (int64_t t : {1, 255, 256, 300, 999, 1000, 1536, 8000, 10239, 10240})
77        CHECK(ec::equal_chunk(t, 10240, F) == ec::today_chunk(t, 10240));
78    CHECK(ec::equal_chunk(8000, 10240, F) == 8192);
79    CHECK(ec::equal_chunk(10240, 10240, F) == 10240);
80    CHECK(ec::equal_chunk(10241, 10240, F) == 10240);      // 2 x 5,121 is short: today's 10,240 + 1 (before: 5,121)
81    // ---- kEqualMinChunk2 / kEqualMinChunk (RTX 4090 24 GB, IQ3_S, chunk 8,192; RTX 5070 12 GB, Q2_0, chunk 10,496): with a
82    // configured chunk of 8,192 or more, two equal chunks under 5,400 (a segment under 10,799 tokens, a prompt under 10,800)
83    // or three or more under 6,144 keep today's cut
84    CHECK(ec::equal_chunk(8279, 8192, 3072) == 8192 && ec::declined_equal(8279, 8192, 3072) == 4140);    // 8,192 + 87
85    CHECK(ec::equal_chunk(10798, 8192, 3072) == 8192 && ec::declined_equal(10798, 8192, 3072) == 5399);  // two: 5,399 short
86    CHECK(ec::equal_chunk(10799, 8192, 3072) == 5400 && ec::declined_equal(10799, 8192, 3072) == 0);     // a 10,800 prompt: 2 x 5,400
87    CHECK(ec::equal_chunk(10800, 8192, 3072) == 5400 && ec::declined_equal(10800, 8192, 3072) == 0);     // 2 x 5,400
88    CHECK(ec::equal_chunk(10998, 8192, 3072) == 5499 && ec::declined_equal(10998, 8192, 3072) == 0);     // two: 5,499 equal
89    CHECK(ec::equal_chunk(11000, 8192, 3072) == 5500 && ec::declined_equal(11000, 8192, 3072) == 0);     // 2 x 5,500
90    CHECK(ec::equal_chunk(11192, 8192, 3072) == 5596 && ec::declined_equal(11192, 8192, 3072) == 0);     // 2 x 5,596 (before: 8,192 + 3,000)
91    CHECK(ec::equal_chunk(11450, 8192, 3072) == 5725 && ec::declined_equal(11450, 8192, 3072) == 0);     // 2 x 5,725
92    CHECK(ec::equal_chunk(12250, 8192, 3072) == 6125 && ec::declined_equal(12250, 8192, 3072) == 0);     // 2 x 6,125
93    CHECK(ec::equal_chunk(12286, 8192, 3072) == 6143);     // two: 6,143 is above 5,400, equal (before: today's 8,192 + 4,094)
94    CHECK(ec::equal_chunk(12287, 8192, 3072) == 6144);
95    CHECK(ec::equal_chunk(12288, 8192, 3072) == 6144 && ec::declined_equal(12288, 8192, 3072) == 0);
96    CHECK(ec::equal_chunk(16884, 8192, 3072) == 8192 && ec::declined_equal(16884, 8192, 3072) == 5628);  // 8,192 x 2 + 500
97    CHECK(ec::equal_chunk(17150, 8192, 3072) == 8192 && ec::declined_equal(17150, 8192, 3072) == 5717);  // three: 5,717 short
98    CHECK(ec::equal_chunk(18400, 8192, 3072) == 8192 && ec::declined_equal(18400, 8192, 3072) == 6134);  // three: 6,134 short
99    CHECK(ec::equal_chunk(18429, 8192, 3072) == 8192 && ec::declined_equal(18429, 8192, 3072) == 6143);  // three: 6,143 short
100    CHECK(ec::equal_chunk(18430, 8192, 3072) == 6144);     // ceil(18,430 / 3) = 6,144: at the limit, equal
101    CHECK(ec::equal_chunk(18432, 8192, 3072) == 6144 && ec::declined_equal(18432, 8192, 3072) == 0);     // 3 x 6,144 equal
102    CHECK(ec::equal_chunk(20000, 8192, 3072) == 6667 && ec::declined_equal(20000, 8192, 3072) == 0);     // 3 x 6,667 stays
103    CHECK(ec::equal_chunk(32000, 8192, 3072) == 8000 && ec::declined_equal(32000, 8192, 3072) == 0);     // 4 x 8,000 stays
104    // the RTX 5070's auto chunk 10,496 (floor 1,536)
105    CHECK(ec::equal_chunk(10500, 10496, F) == 10496 && ec::declined_equal(10500, 10496, F) == 5250);     // 10,496 + 4
106    CHECK(ec::equal_chunk(10799, 10496, F) == 5400 && ec::declined_equal(10799, 10496, F) == 0);         // a 10,800 prompt: 2 x 5,400
107    CHECK(ec::equal_chunk(10800, 10496, F) == 5400 && ec::declined_equal(10800, 10496, F) == 0);         // 2 x 5,400 (before: 10,496 + 304)
108    CHECK(ec::equal_chunk(10798, 10496, F) == 10496 && ec::declined_equal(10798, 10496, F) == 5399);     // 10,496 + 302
109    CHECK(ec::equal_chunk(11000, 10496, F) == 5500 && ec::declined_equal(11000, 10496, F) == 0);
110    CHECK(ec::equal_chunk(11300, 10496, F) == 5650 && ec::declined_equal(11300, 10496, F) == 0);
111    CHECK(ec::equal_chunk(11400, 10496, F) == 5700 && ec::declined_equal(11400, 10496, F) == 0);
112    CHECK(ec::equal_chunk(11500, 10496, F) == 5750 && ec::declined_equal(11500, 10496, F) == 0);
113    CHECK(ec::equal_chunk(12200, 10496, F) == 6100 && ec::declined_equal(12200, 10496, F) == 0);
114    // a configured chunk under 8,192 keeps the rule as before (4,096: 2 x 3,500 stays equal; 7,000: 2 x 3,600 too)
115    CHECK(ec::equal_chunk(7000, 4096, 3072) == 3500 && ec::declined_equal(7000, 4096, 3072) == 0);
116    CHECK(ec::equal_chunk(7200, 7000, 3072) == 3600 && ec::declined_equal(7200, 7000, 3072) == 0);
117    CHECK(ec::equal_min_chunk(2) == 5400 && ec::equal_min_chunk(3) == 6144 && ec::equal_min_chunk(4) == 6144);
118    CHECK(!ec::short_equal(3500, 2, 4096) && !ec::short_equal(3500, 3, 8191));
119    CHECK(ec::short_equal(5399, 2, 8192) && !ec::short_equal(5400, 2, 8192));
120    CHECK(ec::short_equal(6143, 3, 8192) && !ec::short_equal(6144, 3, 8192) && ec::short_equal(6143, 5, 10496));
121    // ---- keep_equal: today's cut was handed back but cannot be lent; the equal cut can (no GPU needed)
122    CHECK(ec::keep_equal(900, 700, 800));                  // today's loan over the room, the equal one under it: keep equal
123    CHECK(ec::keep_equal(0, 700, 800));                    // today's cannot be laid out at all
124    CHECK(!ec::keep_equal(700, 600, 800));                 // today's fits: today's cut
125    CHECK(!ec::keep_equal(900, 850, 800));                 // neither fits: today's cut, halved by the caller (#2090)
126    CHECK(!ec::keep_equal(900, 0, 800));                   // the equal one cannot be laid out
127    CHECK(ec::keep_equal(801, 800, 800) && !ec::keep_equal(800, 800, 800));   // the room is inclusive
128    // ---- the cases the change is for: today's cut leaves a short tail under the floor, the equal cut evens it out
129    CHECK(ec::today_chunk(20932, 10240) == 10240 && cut_of(20932, 10240).last == 452);   // today: 10,240 x 2 + 452
130    CHECK(cut_of(20932, ec::equal_chunk(20932, 10240, F)).n == 3 && cut_of(20932, ec::equal_chunk(20932, 10240, F)).lo >= 6976);
131    CHECK(cut_of(32000, 10240).last == 1280);                                             // today: 10,240 x 3 + 1,280
132    CHECK(ec::equal_chunk(32000, 10240, F) == 8000);                                      // 4 x 8,000
133    CHECK(ec::equal_chunk(32000, 10240, 3072) == 8000);                                   // the CPU share's floor too
134    // ---- a small chunk: the EQUAL chunk itself would fall under the floor (auto chunk 4,096, CPU share floor 3,072):
135    // today's 4,096 + 904 keeps one chunk on the streamed walk; 2 x 2,500 would put both under the floor
136    CHECK(ec::equal_chunk(5000, 4096, 3072) == 4096);
137    CHECK(ec::equal_chunk(7000, 4096, 3072) == 3500);      // 2 x 3,500: both at or above the floor, evened
138    CHECK(ec::equal_chunk(6200, 4096, 3072) == 3100);      // evened from 2 x 3,072 up
139    CHECK(ec::equal_chunk(6100, 4096, 3072) == 4096);      // 2 x 3,050: under the floor, today's cut
140    // ---- a cut whose chunks would fall under the floor keeps today's cut (a small chunk)
141    CHECK(ec::equal_chunk(2100, 2048, F) == 2048);         // 2 x 1,050 would both walk staged
142    CHECK(ec::equal_chunk(3100, 2048, F) == 1550);         // 2 x 1,550: both at or above 1,536
143    CHECK(ec::equal_chunk(3071, 2048, F) == 2048);         // 1,536 + 1,535: the last under the floor
144    CHECK(ec::equal_chunk(3072, 2048, F) == 1536);
145    // ---- degenerate input
146    CHECK(ec::equal_chunk(0, 10240, F) == 0 && ec::equal_chunk(-5, 10240, F) == 0 && ec::equal_chunk(5000, 0, F) == 0);
147    CHECK(ec::chunk_count(0, 5) == 0 && ec::chunk_count(5, 0) == 0 && ec::chunk_count(10, 5) == 2 && ec::chunk_count(11, 5) == 3);
148
149    // ---- the properties, over a sweep of segment lengths and chunks (non-multiples of 256 too: a numeric --prefill)
150    long long cases = 0;
151    for (int64_t mc : {1024, 2048, 3072, 4096, 6144, 7000, 8192, 9216, 9984, 10240, 10496, 16384, 32768}) {
152        for (int64_t floor : {64, 1024, 1536, 3072}) {
153            for (int64_t t = 1; t <= 140000; t += (t < 4 * mc ? 7 : 997)) {
154                ++cases;
155                const int64_t today = ec::today_chunk(t, mc);
156                const int64_t e = ec::equal_chunk(t, mc, floor);
157                const Cutout kt = cut_of(t, today), ke = cut_of(t, e);
158                CHECK(e > 0 && e <= mc);                       // within the buffers laid out for the chunk
159                CHECK(ke.n <= kt.n);                           // never more chunks than today's cut
160                if (t <= mc) { CHECK(e == today); continue; }   // one chunk: unchanged
161                if (e == today) { CHECK(ke.n == kt.n); continue; }   // kept today's cut (a chunk would fall under the floor)
162                CHECK(ke.n == ec::chunk_count(t, mc));           // exactly ceil(t / chunk) chunks
163                CHECK(ke.hi - ke.lo <= ke.n - 1);                // equal, within the n - 1 tokens the division leaves
164                CHECK(ke.lo == ke.last && ke.lo >= floor);       // only the last is shorter, and it streams
165            }
166        }
167    }
168    // ---- the older opt-in rule is the old lambda, value for value
169    for (int64_t mc : {1024, 3072, 7000, 9984, 10240}) {
170        for (int64_t floor : {1024, 1536, 3072}) {
171            for (int64_t t = 0; t <= 70000; t += 13) {
172                CHECK(ec::opt_in_chunk(t, mc, floor) == old_equal_chunk(t, mc, floor));
173                CHECK(ec::today_chunk(t, mc) == old_request_chunk(t, mc));
174            }
175        }
176    }
177    // ---- chunk(): the three cuts
178    CHECK(ec::chunk(ec::Cut::today, 20932, 10240, F) == 10240);
179    CHECK(ec::chunk(ec::Cut::equal, 20932, 10240, F) == 6978);
180    CHECK(ec::chunk(ec::Cut::opt_in, 20932, 10240, F) == 10240);   // the opt-in keeps a 452-token last chunk under the floor
181    CHECK(ec::chunk(ec::Cut::opt_in, 22980, 10240, F) == 7680);    // 2,500 above the floor: evened, on the 256 grid
182    CHECK(ec::chunk(ec::Cut::equal, 22980, 10240, F) == 7660);
183    // ---- the loan guard
184    CHECK(ec::loan_ok(100, 120) && ec::loan_ok(120, 120));
185    CHECK(!ec::loan_ok(121, 120));      // a larger loan than today's: keep today's cut
186    CHECK(!ec::loan_ok(0, 120) && !ec::loan_ok(-1, 120) && !ec::loan_ok(100, 0));
187    // constexpr: usable at compile time
188    static_assert(ec::equal_chunk(32000, 10240, 1536) == 8000, "constexpr equal cut");
189    static_assert(ec::pick("0", true) == ec::Cut::today, "constexpr switch");
190
191    if (failed) {
192        std::fprintf(stderr, "equal_cut_rule_test: %d check(s) failed\n", failed);
193        return 1;
194    }
195    std::printf("equal_cut_rule_test: all checks passed (%lld swept cases)\n", cases);
196    return 0;
197}