okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
fd46e34d5
main
1// The prompt planner's host-memory model (#2824): no GPU, no model. 2#include <cstdio> 3#include <cstdlib> 4#include <vector> 5 6#include "strata/program/prefill_plan.hpp" 7 8using namespace strata::program::prefill_plan ; 9 10static int failed = 0 ; 11#define CHECK (x ) do { if (!(x)) { std::fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #x); ++failed; } } while (0) 12 13static uint64_t gib (double g ) {return (uint64_t ) (g * kGiB ); } 14 15// options at the lend sizes the reporter's engine printed: 2048 1,688 slots 2.3 GiB, 4096 2.97K 4.0, 6144 5.8, 8192 7.5 16static std::vector < Option > issue_options (int64_t full_ring ) { 17 std::vector < Option > v ; 18const struct {int64_t c ;double g ; }L []= {{1024 ,1.6 }, {2048 ,2.3 }, {3072 ,3.1 }, {4096 ,4.0 }, {5120 ,4.9 }, {6144 ,5.8 }, {7168 ,6.6 }, {8192 ,7.5 }}; 19for (const auto & l :L )v .push_back ({l .c ,full_ring ,full_ring ,gib (l .g ),0 }); 20return v ; 21} 22 23int main () { 24// #2824: 2x RTX 3060, 32 GB, IQ2_XS: the experts total 33.0 GiB, the GPU caches 13.84, the RAM copy 12.0 of the rest 25Host h ; 26h .page_cache = gib (14.5 ); 27h .h2d_gbps = 8.0 ; 28h .ssd_gbps = 0.45 ; 29Tier t ; 30t .experts_total = gib (33.0 ); 31t .gpu_cache = gib (13.84 ); 32t .file_tier = gib (7.18 ); 33t .extra_file = gib (0.35 ); 34const auto opts = issue_options (64 ); 35// the file sets the reporter computed: 15.1 GiB at 8192, 13.3 at 6144, 11.6 at 4096 36CHECK (std::abs (price (h ,t ,29000 ,opts [7 ]).file_set /kGiB - 15.03 )< 0.15 ); 37CHECK (std::abs (price (h ,t ,29000 ,opts [5 ]).file_set /kGiB - 13.33 )< 0.15 ); 38CHECK (std::abs (price (h ,t ,29000 ,opts [3 ]).file_set /kGiB - 11.53 )< 0.15 ); 39// measured drive reads per 29K prompt: 8192: 28-45 GB, 4096: 0.9-1.4 GB, 6144: 1.2-2.8 GB 40const Cost c8 = price (h ,t ,28700 ,opts [7 ]),c6 = price (h ,t ,28700 ,opts [5 ]),c4 = price (h ,t ,28700 ,opts [3 ]); 41 std::printf ("8192: set %.1f GiB miss %.2f reads %.1f GB %.0f s | 6144: %.1f GiB %.2f %.1f GB %.0f s | 4096: %.1f GiB %.2f %.1f GB %.0f s\n" , 42c8 .file_set /kGiB ,c8 .miss ,c8 .read_bytes /1e9 ,c8 .seconds ,c6 .file_set /kGiB ,c6 .miss ,c6 .read_bytes /1e9 , 43c6 .seconds ,c4 .file_set /kGiB ,c4 .miss ,c4 .read_bytes /1e9 ,c4 .seconds ); 44CHECK (c8 .read_bytes /1e9 > 28 && c8 .read_bytes /1e9 < 46 ); 45CHECK (c4 .read_bytes == 0 && c4 .fits ); 46CHECK (c6 .read_bytes /1e9 < 15 );// measured 1.2-2.8 GB: the model is conservative here 47// the planner leaves 8192 for the one that fits 48const Pick p = choose (h ,t ,28700 ,opts ,7 ); 49 std::printf ("%s\n" ,describe (p ,h ,28700 ).c_str ()); 50CHECK (p .changed ); 51CHECK (p .best .opt .chunk >=3072 && p .best .opt .chunk <=5120 ); 52CHECK (p .best .read_bytes < 3e9 ); 53 54// P2003 (RTX 3060, IQ2_XS, cgroup 32G, NVMe): 8192 was the fastest of 2048/3328/4096/6144/8192 although it thrashes (measured 480 vs 391 tok/s at 4096). 55// file sets 19.1 / 18.2 / 17.5 / 17.0 / 16.4 GiB against a 17.0 GiB cache; the fast drive makes the extra passes the dearer cost 56Host nv ; 57nv .page_cache = gib (17.0 ); 58nv .h2d_gbps = 25.6 ; 59nv .ssd_gbps = 2.5 ; 60Tier t2 ; 61t2 .experts_total = gib (33.0 ); 62t2 .gpu_cache = gib (6.0 ); 63t2 .file_tier = gib (15.0 ); 64t2 .extra_file = gib (0.35 ); 65 std::vector < Option > o4 = {{2048 ,96 ,96 ,gib (1.1 ),0 }, {3328 ,96 ,96 ,gib (1.63 ),0 }, {4096 ,96 ,96 ,gib (2.0 ),0 }, {6144 ,96 ,96 ,gib (2.9 ),0 }, {8192 ,96 ,96 ,gib (3.8 ),0 }}; 66const Pick n = choose (nv ,t2 ,28887 ,o4 ,4 ); 67 std::puts (describe (n ,nv ,28887 ).c_str ()); 68CHECK (!n .changed ); 69 70// a box where everything fits keeps today's choice 71Host big = h ; 72big .page_cache = gib (40 ); 73const Pick q = choose (big ,t ,28700 ,opts ,7 ); 74CHECK (!q .changed && q .best .opt .chunk == 8192 && q .best .read_bytes == 0 ); 75 76// a box whose file set can never fit (8 GB card + 32 GB RAM): no refusal, no blind shrink: the fewest passes win 77Tier hard ; 78hard .experts_total = gib (48 ); 79hard .gpu_cache = gib (3.0 ); 80hard .file_tier = gib (40 ); 81hard .extra_file = gib (0.4 ); 82Host small = h ; 83small .page_cache = gib (22 ); 84 std::vector < Option > o2 ; 85for (int64_t c = 1024 ;c <=8192 ;c += 1024 )o2 .push_back ({c ,64 ,64 ,gib (0.9 * (double )c /1024.0 ),0 }); 86const Pick r = choose (small ,hard ,28700 ,o2 ,7 ); 87 std::printf ("%s\n" ,describe (r ,small ,28700 ).c_str ()); 88CHECK (r .best .opt .chunk == 8192 ); 89// and from a small today's choice it moves UP to fewer passes when nothing fits either way 90const Pick r2 = choose (small ,hard ,28700 ,o2 ,1 ); 91CHECK (r2 .best .opt .chunk > 2048 ); 92 93// unbuffered reads (no page cache): every chunk reads its set; the biggest chunk reads least 94Host ub = h ; 95ub .page_cache = 0 ; 96CHECK (price (ub ,t ,28700 ,opts [7 ]).read_bytes < price (ub ,t ,28700 ,opts [3 ]).read_bytes ); 97 98// lent experts the RAM copy keeps leave the file set: 8192 with all 7.5 GiB kept is 7.5 GiB smaller 99Option kept = opts [7 ]; 100kept .lend_kept = kept .lend_bytes ; 101CHECK (price (h ,t ,28700 ,kept ).file_set < price (h ,t ,28700 ,opts [7 ]).file_set - gib (7.4 )); 102CHECK (price (h ,t ,28700 ,kept ).fits ); 103 104// a smaller ring only wins when it brings the set into the cache 105Tier edge = t ; 106edge .file_tier = gib (6.2 );// 8192 with the 64-slot ring: 6.2+7.5+.35 = 14.05 GiB, 0.97 of the cache 107 std::vector < Option > o3 = {{8192 ,64 ,64 ,gib (7.5 ),0 }, {8192 ,16 ,64 ,gib (7.0 ),0 }}; 108const Pick s = choose (h ,edge ,28700 ,o3 ,0 ); 109 std::printf ("%s\n" ,describe (s ,h ,28700 ).c_str ()); 110// request-sized chunks: a prompt that fits today's 8192 stays one chunk; a longer one is cut into equal chunks of at most the plan's 111CHECK (balanced_chunk (4101 ,8192 ,3840 )== 4352 ); 112CHECK (balanced_chunk (8192 ,8192 ,3840 )== 8192 ); 113CHECK (balanced_chunk (300 ,8192 ,3840 )== 512 ); 114CHECK (balanced_chunk (20400 ,8192 ,3840 )== 3584 );// 6 equal chunks (last 2,480), no stub 115CHECK (balanced_chunk (28887 ,8192 ,3840 )== 3840 );// 8 chunks: 7 x 3840 + 2,007 116CHECK (balanced_chunk (8200 ,8192 ,3840 )== 2816 );// 3 chunks of 2,816 instead of 3,840 + 3,840 + 520 117CHECK (balanced_chunk (20400 ,8192 ,0 )== 8192 );// no plan: today's cap 118for (int64_t t = 8193 ;t < 40000 ;t += 37 ) { 119const int64_t c = balanced_chunk (t ,8192 ,3840 ); 120const int64_t n = (t + c - 1 ) /c ; 121CHECK (c %256 == 0 && c <=8192 ); 122CHECK ((t - (n - 1 )* c )* 5 >=c * 2 || n == 1 );// the last chunk is at least 40% of a chunk 123CHECK (c <=3840 ); 124CHECK (n <= (t + 3839 ) /3840 + 1 ); 125 } 126 std::printf ("%s\n" ,failed ?"FAILED" :"prefill_plan_test: ok" ); 127return failed ?1 :0 ; 128}