okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
30c2b84f2
main
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 23CHECK (grid (999 )== 1024 && walk_chunk (999 ,grid (999 ),1024 )== 1023 ); 24CHECK (walk_chunk (769 ,grid (769 ),1024 )== 1023 );// the first piece that rounds to the floor 25CHECK (walk_chunk (768 ,grid (768 ),1024 )== 768 );// an exact multiple under the floor: nothing to correct 26CHECK (walk_chunk (1023 ,grid (1023 ),1024 )== 1023 ); 27CHECK (walk_chunk (1024 ,grid (1024 ),1024 )== 1024 );// at the floor: the streamed walk, the chunk stays 28CHECK (walk_chunk (1025 ,grid (1025 ),1024 )== 1280 ); 29CHECK (walk_chunk (300 ,grid (300 ),1024 )== 512 );// far under the floor: the staged layout already 30// the CPU share's limit 31CHECK (walk_chunk (3071 ,grid (3071 ),3072 )== 3071 ); 32CHECK (walk_chunk (2817 ,grid (2817 ),3072 )== 3071 ); 33CHECK (walk_chunk (2816 ,grid (2816 ),3072 )== 2816 ); 34CHECK (walk_chunk (3000 ,grid (3000 ),3072 )== 3071 ); 35CHECK (walk_chunk (3072 ,grid (3072 ),3072 )== 3072 ); 36CHECK (walk_chunk (3073 ,grid (3073 ),3072 )== 3328 ); 37// exact multiples of 256 on a floor that is one 38for (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 40CHECK (walk_chunk (63 ,grid (63 ),64 )== 63 ); 41CHECK (walk_chunk (1 ,grid (1 ),64 )== 63 ); 42CHECK (walk_chunk (64 ,grid (64 ),64 )== 256 ); 43CHECK (walk_chunk (100 ,grid (100 ),64 )== 256 ); 44// a floor of 0 or 1: every piece streams, nothing to correct 45CHECK (walk_chunk (999 ,grid (999 ),0 )== 1024 ); 46CHECK (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 48CHECK (walk_chunk (999 ,512 ,1024 )== 512 ); 49// never larger than the chunk the caller chose, never below the piece itself 50for (int64_t floor : std::initializer_list < int64_t > {64 ,1024 ,2048 ,3072 ,4096 }) 51for (int64_t t = 1 ;t <=9000 ;t += 7 ) { 52const int64_t c = walk_chunk (t ,grid (t ),floor ); 53CHECK (c <=grid (t )); 54CHECK (c >=t || grid (t )< t ); 55if (t < floor )CHECK (c < floor ); 56 } 57// empty or negative requests are passed through 58CHECK (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 63CHECK (tail_need (1024 ,1024 ,1024 )== 0 ); 64CHECK (tail_need (8192 ,8192 ,1024 )== 0 ); 65CHECK (tail_need (4096 ,2048 ,1024 )== 0 );// two full chunks 66// a short last chunk is the whole need 67CHECK (tail_need (4097 ,2048 ,1024 )== 1 ); 68CHECK (tail_need (5000 ,2048 ,1024 )== 904 ); 69CHECK (tail_need (5120 + 1023 ,5120 ,1024 )== 1023 ); 70CHECK (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 72CHECK (tail_need (8193 ,8192 ,3584 )== 1 ); 73CHECK (tail_need (4097 ,2048 ,3584 )== 2048 );// a layout of 2,048-token chunks is below the floor: not fused, every chunk is MMQ's 74CHECK (tail_need (8193 + 2000 ,8192 ,3584 )== 2001 ); 75CHECK (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) 77CHECK (tail_need (2000 ,8192 ,3584 )== 2000 ); 78CHECK (tail_need (2000 ,8192 ,1024 )== 0 ); 79// a layout whose own chunk is below the floor is not fused: every chunk is MMQ's 80CHECK (tail_need (5000 ,1023 ,1024 )== 1023 && tail_need (500 ,1023 ,1024 )== 500 ); 81CHECK (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 83for (int64_t floor : std::initializer_list < int64_t > {1 ,64 ,1024 ,3072 ,3584 }) 84for (int64_t c : std::initializer_list < int64_t > {256 ,1023 ,1024 ,2048 ,4096 ,8192 }) 85for (int64_t t = 1 ;t <=20000 ;t += 37 ) { 86const int64_t need = tail_need (t ,c ,floor ); 87CHECK (need >=0 && need <= (floor > 1 ?floor - 1 :0 )); 88int64_t worst = 0 ;// walk the chunks one by one 89for (int64_t at = 0 ;at < t ;at += c ) { 90const int64_t len = t - at < c ?t - at :c ; 91if (len < floor && len > worst )worst = len ; 92 } 93CHECK (need == worst ); 94const int64_t lay = tail_layout (need ,floor ); 95CHECK (lay >=need && (need == 0 ?lay == 0 :lay %256 == 0 || lay == floor - 1 )); 96CHECK (lay <= (floor > 1 ?floor - 1 :0 )); 97CHECK (segment_tail (t ,c ,floor )== lay ); 98 } 99// the grid: 904 -> 1,024; clamped to today's floor - 1 100CHECK (tail_layout (904 ,3584 )== 1024 && tail_layout (1 ,3584 )== 256 && tail_layout (1023 ,1024 )== 1023 ); 101CHECK (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 { 105const int64_t floor = 3584 ,lent_chunk = 8192 ; 106const int64_t lent_tail = segment_tail (8193 ,lent_chunk ,floor );// 256: the last chunk is 1 token 107CHECK (lent_tail == 256 ); 108CHECK (!reuse_ok (2048 ,2000 ,lent_chunk ,lent_tail ,floor ));// needs 2,000 (grid 2,048): relayout 109CHECK (tail_need (2000 ,lent_chunk ,floor )> lent_tail ); 110CHECK (reuse_ok (256 ,100 ,lent_chunk ,lent_tail ,floor ));// 100 <= 256: reuse 111CHECK (tail_need (4196 ,lent_chunk ,floor )== 0 );// one chunk of 4,196 >= floor: fused, no tail 112CHECK (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 115CHECK (!reuse_ok (4096 ,4096 ,2048 ,4095 ,1024 )); 116CHECK (reuse_ok (2048 ,4096 ,2048 ,0 ,1024 )); 117CHECK (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 { 120const int64_t floor = 3584 ; 121CHECK (next_tail (8192 ,-1 ,8192 ,256 ,floor )== 256 );// a start-up layout (default-sized): the need of the moment 122CHECK (next_tail (8192 ,1024 ,8192 ,256 ,floor )== 1024 );// need shrinks: the tail stays (no relayout) 123CHECK (next_tail (8192 ,1024 ,8192 ,0 ,floor )== 1024 ); 124CHECK (next_tail (8192 ,1024 ,8192 ,1024 ,floor )== 1024 );// equal: stays 125CHECK (next_tail (8192 ,1024 ,8192 ,1280 ,floor )== 3583 );// need grows: today's size, once 126CHECK (next_tail (8192 ,3583 ,8192 ,3000 ,floor )== 3583 );// and then it is today's: nothing grows past it 127CHECK (next_tail (8192 ,1024 ,4096 ,256 ,floor )== 256 );// chunk shrinks: another layout, from its own need 128CHECK (next_tail (4096 ,1024 ,8192 ,2048 ,floor )== 2048 );// chunk grows: from the need of that moment 129CHECK (next_tail (8192 ,0 ,8192 ,0 ,floor )== 0 );// no tail needed, none carved 130CHECK (next_tail (8192 ,0 ,8192 ,256 ,floor )== 3583 ); 131CHECK (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 133int64_t cap = -1 ,grown = 0 ; 134for (int64_t need : std::initializer_list < int64_t > {256 ,0 ,1024 ,256 ,2048 ,3583 ,0 ,512 ,3000 }) { 135const int64_t t = next_tail (8192 ,cap ,8192 ,need ,floor ); 136CHECK (t >=need ); 137if (cap >=0 )CHECK (t >=cap ); 138if (t != cap )++ grown ; 139cap = t ; 140 } 141CHECK (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" ); 144if (failed != 0 )return 1 ; 145 std::printf ("loan_rules_test: all checks passed\n" ); 146return 0 ; 147}