okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
4e5eef6e3
main
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 ) { 17if (tokens <=0 || max_chunk <=0 )return 0 ; 18const int64_t rounded = tokens > std::numeric_limits < int64_t > ::max ()- 255 ?tokens : ((tokens + 255 ) /256 )* 256 ; 19return std::min (max_chunk ,rounded ); 20} 21static int64_t old_equal_chunk (int64_t tokens ,int64_t max_chunk ,int64_t floor ) { 22const int64_t plain = old_request_chunk (tokens ,max_chunk ); 23if (tokens <=0 || max_chunk <=0 )return plain ; 24const int64_t n = tokens /max_chunk + (tokens %max_chunk != 0 ); 25const int64_t last = tokens - (n - 1 )* max_chunk ; 26if (n <=1 || last < floor )return plain ; 27const int64_t per = tokens /n + (tokens %n != 0 ); 28const int64_t rounded = per > std::numeric_limits < int64_t > ::max ()- 255 ?per : ((per + 255 ) /256 )* 256 ; 29return 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 ) { 35Cutout r {0 , std::numeric_limits < int64_t > ::max (),0 ,0 }; 36for (int64_t c0 = 0 ;c0 < tokens ;c0 += c ) { 37const int64_t t = std::min (c ,tokens - c0 ); 38++ r .n ; 39r .lo = std::min (r .lo ,t ); 40r .hi = std::max (r .hi ,t ); 41r .last = t ; 42 } 43return r ; 44} 45 46int main () { 47const int64_t F = 1536 ;// the plain pack's floor on a discrete card in a build without MMQ 48// ---- the switch 49CHECK (ec::pick (nullptr , true)== ec::Cut ::equal );// unset, one GPU: the equal cut 50CHECK (ec::pick ("1" , true)== ec::Cut ::equal ); 51CHECK (ec::pick ("0" , true)== ec::Cut ::today );// the way back 52CHECK (ec::pick ("0" , false)== ec::Cut ::today ); 53CHECK (ec::pick (nullptr , false)== ec::Cut ::today );// a layer split: today's cut 54CHECK (ec::pick ("1" , false)== ec::Cut ::opt_in );// a layer split that asks for the older opt-in keeps it 55CHECK (ec::pick ("" , true)== ec::Cut ::equal ); 56CHECK (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 { 60const int64_t c = ec::equal_chunk (20932 ,10240 ,F );// today 10,240 x 2 + 452 61CHECK (c == 6978 ); 62const Cutout k = cut_of (20932 ,c ); 63CHECK (k .n == 3 && k .hi == 6978 && k .last == 6976 ); 64 } 65CHECK (ec::equal_chunk (32000 ,10240 ,F )== 8000 && cut_of (32000 ,8000 ).n == 4 && cut_of (32000 ,8000 ).lo == 8000 ); 66CHECK (ec::equal_chunk (14000 ,10240 ,F )== 7000 && cut_of (14000 ,7000 ).n == 2 ); 67CHECK (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) 69CHECK (ec::equal_chunk (10500 ,10240 ,F )== 10240 && ec::declined_equal (10500 ,10240 ,F )== 5250 ); 70CHECK (ec::equal_chunk (20000 ,10240 ,F )== 10000 ); 71CHECK (ec::equal_chunk (22980 ,10240 ,F )== 7660 ); 72CHECK (ec::equal_chunk (18932 ,10240 ,F )== 9466 ); 73// the engine reads N - 1 prompt tokens in a batch: 20,931 -> 3 x 6,977 74CHECK (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) 76for (int64_t t : {1 ,255 ,256 ,300 ,999 ,1000 ,1536 ,8000 ,10239 ,10240 }) 77CHECK (ec::equal_chunk (t ,10240 ,F )== ec::today_chunk (t ,10240 )); 78CHECK (ec::equal_chunk (8000 ,10240 ,F )== 8192 ); 79CHECK (ec::equal_chunk (10240 ,10240 ,F )== 10240 ); 80CHECK (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 84CHECK (ec::equal_chunk (8279 ,8192 ,3072 )== 8192 && ec::declined_equal (8279 ,8192 ,3072 )== 4140 );// 8,192 + 87 85CHECK (ec::equal_chunk (10798 ,8192 ,3072 )== 8192 && ec::declined_equal (10798 ,8192 ,3072 )== 5399 );// two: 5,399 short 86CHECK (ec::equal_chunk (10799 ,8192 ,3072 )== 5400 && ec::declined_equal (10799 ,8192 ,3072 )== 0 );// a 10,800 prompt: 2 x 5,400 87CHECK (ec::equal_chunk (10800 ,8192 ,3072 )== 5400 && ec::declined_equal (10800 ,8192 ,3072 )== 0 );// 2 x 5,400 88CHECK (ec::equal_chunk (10998 ,8192 ,3072 )== 5499 && ec::declined_equal (10998 ,8192 ,3072 )== 0 );// two: 5,499 equal 89CHECK (ec::equal_chunk (11000 ,8192 ,3072 )== 5500 && ec::declined_equal (11000 ,8192 ,3072 )== 0 );// 2 x 5,500 90CHECK (ec::equal_chunk (11192 ,8192 ,3072 )== 5596 && ec::declined_equal (11192 ,8192 ,3072 )== 0 );// 2 x 5,596 (before: 8,192 + 3,000) 91CHECK (ec::equal_chunk (11450 ,8192 ,3072 )== 5725 && ec::declined_equal (11450 ,8192 ,3072 )== 0 );// 2 x 5,725 92CHECK (ec::equal_chunk (12250 ,8192 ,3072 )== 6125 && ec::declined_equal (12250 ,8192 ,3072 )== 0 );// 2 x 6,125 93CHECK (ec::equal_chunk (12286 ,8192 ,3072 )== 6143 );// two: 6,143 is above 5,400, equal (before: today's 8,192 + 4,094) 94CHECK (ec::equal_chunk (12287 ,8192 ,3072 )== 6144 ); 95CHECK (ec::equal_chunk (12288 ,8192 ,3072 )== 6144 && ec::declined_equal (12288 ,8192 ,3072 )== 0 ); 96CHECK (ec::equal_chunk (16884 ,8192 ,3072 )== 8192 && ec::declined_equal (16884 ,8192 ,3072 )== 5628 );// 8,192 x 2 + 500 97CHECK (ec::equal_chunk (17150 ,8192 ,3072 )== 8192 && ec::declined_equal (17150 ,8192 ,3072 )== 5717 );// three: 5,717 short 98CHECK (ec::equal_chunk (18400 ,8192 ,3072 )== 8192 && ec::declined_equal (18400 ,8192 ,3072 )== 6134 );// three: 6,134 short 99CHECK (ec::equal_chunk (18429 ,8192 ,3072 )== 8192 && ec::declined_equal (18429 ,8192 ,3072 )== 6143 );// three: 6,143 short 100CHECK (ec::equal_chunk (18430 ,8192 ,3072 )== 6144 );// ceil(18,430 / 3) = 6,144: at the limit, equal 101CHECK (ec::equal_chunk (18432 ,8192 ,3072 )== 6144 && ec::declined_equal (18432 ,8192 ,3072 )== 0 );// 3 x 6,144 equal 102CHECK (ec::equal_chunk (20000 ,8192 ,3072 )== 6667 && ec::declined_equal (20000 ,8192 ,3072 )== 0 );// 3 x 6,667 stays 103CHECK (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) 105CHECK (ec::equal_chunk (10500 ,10496 ,F )== 10496 && ec::declined_equal (10500 ,10496 ,F )== 5250 );// 10,496 + 4 106CHECK (ec::equal_chunk (10799 ,10496 ,F )== 5400 && ec::declined_equal (10799 ,10496 ,F )== 0 );// a 10,800 prompt: 2 x 5,400 107CHECK (ec::equal_chunk (10800 ,10496 ,F )== 5400 && ec::declined_equal (10800 ,10496 ,F )== 0 );// 2 x 5,400 (before: 10,496 + 304) 108CHECK (ec::equal_chunk (10798 ,10496 ,F )== 10496 && ec::declined_equal (10798 ,10496 ,F )== 5399 );// 10,496 + 302 109CHECK (ec::equal_chunk (11000 ,10496 ,F )== 5500 && ec::declined_equal (11000 ,10496 ,F )== 0 ); 110CHECK (ec::equal_chunk (11300 ,10496 ,F )== 5650 && ec::declined_equal (11300 ,10496 ,F )== 0 ); 111CHECK (ec::equal_chunk (11400 ,10496 ,F )== 5700 && ec::declined_equal (11400 ,10496 ,F )== 0 ); 112CHECK (ec::equal_chunk (11500 ,10496 ,F )== 5750 && ec::declined_equal (11500 ,10496 ,F )== 0 ); 113CHECK (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) 115CHECK (ec::equal_chunk (7000 ,4096 ,3072 )== 3500 && ec::declined_equal (7000 ,4096 ,3072 )== 0 ); 116CHECK (ec::equal_chunk (7200 ,7000 ,3072 )== 3600 && ec::declined_equal (7200 ,7000 ,3072 )== 0 ); 117CHECK (ec::equal_min_chunk (2 )== 5400 && ec::equal_min_chunk (3 )== 6144 && ec::equal_min_chunk (4 )== 6144 ); 118CHECK (!ec::short_equal (3500 ,2 ,4096 )&& !ec::short_equal (3500 ,3 ,8191 )); 119CHECK (ec::short_equal (5399 ,2 ,8192 )&& !ec::short_equal (5400 ,2 ,8192 )); 120CHECK (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) 122CHECK (ec::keep_equal (900 ,700 ,800 ));// today's loan over the room, the equal one under it: keep equal 123CHECK (ec::keep_equal (0 ,700 ,800 ));// today's cannot be laid out at all 124CHECK (!ec::keep_equal (700 ,600 ,800 ));// today's fits: today's cut 125CHECK (!ec::keep_equal (900 ,850 ,800 ));// neither fits: today's cut, halved by the caller (#2090) 126CHECK (!ec::keep_equal (900 ,0 ,800 ));// the equal one cannot be laid out 127CHECK (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 129CHECK (ec::today_chunk (20932 ,10240 )== 10240 && cut_of (20932 ,10240 ).last == 452 );// today: 10,240 x 2 + 452 130CHECK (cut_of (20932 , ec::equal_chunk (20932 ,10240 ,F )).n == 3 && cut_of (20932 , ec::equal_chunk (20932 ,10240 ,F )).lo >=6976 ); 131CHECK (cut_of (32000 ,10240 ).last == 1280 );// today: 10,240 x 3 + 1,280 132CHECK (ec::equal_chunk (32000 ,10240 ,F )== 8000 );// 4 x 8,000 133CHECK (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 136CHECK (ec::equal_chunk (5000 ,4096 ,3072 )== 4096 ); 137CHECK (ec::equal_chunk (7000 ,4096 ,3072 )== 3500 );// 2 x 3,500: both at or above the floor, evened 138CHECK (ec::equal_chunk (6200 ,4096 ,3072 )== 3100 );// evened from 2 x 3,072 up 139CHECK (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) 141CHECK (ec::equal_chunk (2100 ,2048 ,F )== 2048 );// 2 x 1,050 would both walk staged 142CHECK (ec::equal_chunk (3100 ,2048 ,F )== 1550 );// 2 x 1,550: both at or above 1,536 143CHECK (ec::equal_chunk (3071 ,2048 ,F )== 2048 );// 1,536 + 1,535: the last under the floor 144CHECK (ec::equal_chunk (3072 ,2048 ,F )== 1536 ); 145// ---- degenerate input 146CHECK (ec::equal_chunk (0 ,10240 ,F )== 0 && ec::equal_chunk (-5 ,10240 ,F )== 0 && ec::equal_chunk (5000 ,0 ,F )== 0 ); 147CHECK (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) 150long long cases = 0 ; 151for (int64_t mc : {1024 ,2048 ,3072 ,4096 ,6144 ,7000 ,8192 ,9216 ,9984 ,10240 ,10496 ,16384 ,32768 }) { 152for (int64_t floor : {64 ,1024 ,1536 ,3072 }) { 153for (int64_t t = 1 ;t <=140000 ;t += (t < 4 * mc ?7 :997 )) { 154++ cases ; 155const int64_t today = ec::today_chunk (t ,mc ); 156const int64_t e = ec::equal_chunk (t ,mc ,floor ); 157const Cutout kt = cut_of (t ,today ),ke = cut_of (t ,e ); 158CHECK (e > 0 && e <=mc );// within the buffers laid out for the chunk 159CHECK (ke .n <=kt .n );// never more chunks than today's cut 160if (t <=mc ) {CHECK (e == today );continue ; }// one chunk: unchanged 161if (e == today ) {CHECK (ke .n == kt .n );continue ; }// kept today's cut (a chunk would fall under the floor) 162CHECK (ke .n == ec::chunk_count (t ,mc ));// exactly ceil(t / chunk) chunks 163CHECK (ke .hi - ke .lo <=ke .n - 1 );// equal, within the n - 1 tokens the division leaves 164CHECK (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 169for (int64_t mc : {1024 ,3072 ,7000 ,9984 ,10240 }) { 170for (int64_t floor : {1024 ,1536 ,3072 }) { 171for (int64_t t = 0 ;t <=70000 ;t += 13 ) { 172CHECK (ec::opt_in_chunk (t ,mc ,floor )== old_equal_chunk (t ,mc ,floor )); 173CHECK (ec::today_chunk (t ,mc )== old_request_chunk (t ,mc )); 174 } 175 } 176 } 177// ---- chunk(): the three cuts 178CHECK (ec::chunk (ec::Cut ::today ,20932 ,10240 ,F )== 10240 ); 179CHECK (ec::chunk (ec::Cut ::equal ,20932 ,10240 ,F )== 6978 ); 180CHECK (ec::chunk (ec::Cut ::opt_in ,20932 ,10240 ,F )== 10240 );// the opt-in keeps a 452-token last chunk under the floor 181CHECK (ec::chunk (ec::Cut ::opt_in ,22980 ,10240 ,F )== 7680 );// 2,500 above the floor: evened, on the 256 grid 182CHECK (ec::chunk (ec::Cut ::equal ,22980 ,10240 ,F )== 7660 ); 183// ---- the loan guard 184CHECK (ec::loan_ok (100 ,120 )&& ec::loan_ok (120 ,120 )); 185CHECK (!ec::loan_ok (121 ,120 ));// a larger loan than today's: keep today's cut 186CHECK (!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 191if (failed ) { 192 std::fprintf (stderr ,"equal_cut_rule_test: %d check(s) failed\n" ,failed ); 193return 1 ; 194 } 195 std::printf ("equal_cut_rule_test: all checks passed (%lld swept cases)\n" ,cases ); 196return 0 ; 197}