okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
8ac8ce32e
main
1// Strata. The experts-per-word rule of the DeepSeek V4.1 engine (src/dsv41/ds4_experts_rule.hpp): 5 on a card with 16 GiB or less, 6 2// elsewhere, --experts overrides, DS4_TOPK over both. No GPU needed. 3#include "ds4_experts_rule.hpp" 4 5#include <cstdio> 6#include <cstdlib> 7 8using strata::ds4::experts_per_word ; 9using strata::ds4::experts_test_override ; 10using strata::ds4::copy_all_fits ; 11 12static int fails = 0 ; 13#define CHECK_EQ (a ,b ) \ 14 do { \ 15 const long long x = (a), y = (b); \ 16 if (x != y) { \ 17 std::printf("FAIL %s:%d: %s = %lld, expected %lld\n", __FILE__, __LINE__, #a, x, y); \ 18 ++fails; \ 19 } \ 20 } while (0) 21 22int main () { 23const uint64_t GiB = 1ull <<30 ; 24// the owner's 12 GB-class cards (RTX 5070 12227 MiB, RTX 3060 12 GB), 16 GiB cards: faster mode by default 25CHECK_EQ (experts_per_word (-1 ,12227ull <<20 ,6 ),5 ); 26CHECK_EQ (experts_per_word (-1 ,12 * GiB ,6 ),5 ); 27CHECK_EQ (experts_per_word (-1 ,16 * GiB ,6 ),5 ); 28CHECK_EQ (experts_per_word (-1 ,8 * GiB ,6 ),5 ); 29// 24 GB and bigger, and several cards: the model's own 6 30CHECK_EQ (experts_per_word (-1 ,16 * GiB + 1 ,6 ),6 ); 31CHECK_EQ (experts_per_word (-1 ,24 * GiB ,6 ),6 ); 32CHECK_EQ (experts_per_word (-1 ,4 * 32 * GiB ,6 ),6 ); 33CHECK_EQ (experts_per_word (-1 ,2 * 12 * GiB ,6 ),6 );// two 12 GB cards hold 24 GiB in all 34// a GPU that shares the PC's RAM (Strix Halo, ~91 GiB visible): faster mode whatever the size; discrete cards by size only 35CHECK_EQ (experts_per_word (-1 ,91 * GiB ,6 , true),5 ); 36CHECK_EQ (experts_per_word (-1 ,24 * GiB ,6 , false),6 );// a discrete 24 GB card 37CHECK_EQ (experts_per_word (-1 ,12 * GiB ,6 , false),5 );// a discrete 12 GB card 38CHECK_EQ (experts_per_word (6 ,91 * GiB ,6 , true),6 );// --experts 6 wins on the unified-memory GPU too 39CHECK_EQ (experts_per_word (-1 ,91 * GiB ,4 , true),4 );// never raised 40// unknown memory (0): the model's count, never a silent cut 41CHECK_EQ (experts_per_word (-1 ,0 ,6 ),6 ); 42// the flag decides, both ways 43CHECK_EQ (experts_per_word (6 ,12 * GiB ,6 ),6 ); 44CHECK_EQ (experts_per_word (4 ,12 * GiB ,6 ),4 ); 45CHECK_EQ (experts_per_word (5 ,96 * GiB ,6 ),5 ); 46CHECK_EQ (experts_per_word (9 ,12 * GiB ,6 ),6 );// never more than the model has 47CHECK_EQ (experts_per_word (1 ,12 * GiB ,6 ),1 ); 48// a model that routes to 1 (or no) expert: left alone 49CHECK_EQ (experts_per_word (-1 ,12 * GiB ,1 ),1 ); 50CHECK_EQ (experts_per_word (-1 ,12 * GiB ,0 ),0 ); 51// a model that routes to fewer than 5: the rule never raises it 52CHECK_EQ (experts_per_word (-1 ,12 * GiB ,4 ),4 ); 53// the test override wins over the rule and the flag 54#if defined(_WIN32 ) 55_putenv ("DS4_TOPK=4" ); 56#else 57setenv ("DS4_TOPK" ,"4" ,1 ); 58#endif 59CHECK_EQ (experts_test_override (experts_per_word (-1 ,96 * GiB ,6 ),6 ),4 ); 60CHECK_EQ (experts_test_override (experts_per_word (6 ,12 * GiB ,6 ),6 ),4 ); 61#if defined(_WIN32 ) 62_putenv ("DS4_TOPK=9" ); 63#else 64setenv ("DS4_TOPK" ,"9" ,1 ); 65#endif 66CHECK_EQ (experts_test_override (5 ,6 ),5 );// out of range: ignored 67#if defined(_WIN32 ) 68_putenv ("DS4_TOPK=" ); 69#else 70unsetenv ("DS4_TOPK" ); 71#endif 72CHECK_EQ (experts_test_override (5 ,6 ),5 ); 73// prompt chunk: prefill_chunk_default(own_default, gpu_share, gguf_copy, file_experts, hip, min_vram, big=8192). What `strata launch` runs on one card 74// passes neither --tp nor --threads, but the engine's default tp is 2, so own_default is false there; a launch with several cards passes --tp 1. 75using strata::ds4::prefill_chunk_default ; 76// RTX 5070 / RTX 3060 (12 GB), launch default: experts in place on a card under 16 GiB -> 4096 (their logs said 8192 before this rule) 77CHECK_EQ (prefill_chunk_default (false,0.06 , false, true, false,12227ull <<20 ),4096 ); 78CHECK_EQ (prefill_chunk_default (false,0.06 , false, true, false,12 * GiB ),4096 ); 79// the same cards with --tp 1 on one card: share 0.06 < 0.5 -> the same 4096 80CHECK_EQ (prefill_chunk_default (true,0.06 , false, true, false,12 * GiB ),4096 ); 81// 16 GB cards (15.9 GiB total) 4096; a card with exactly 16 GiB or more keeps the big chunk; short VRAM (8 GB) 4096; unknown VRAM the big one 82CHECK_EQ (prefill_chunk_default (false,0.10 , false, true, false,16 * GiB - 1 ),4096 ); 83CHECK_EQ (prefill_chunk_default (false,0.10 , false, true, false,16 * GiB ),8192 ); 84CHECK_EQ (prefill_chunk_default (false,0.10 , false, true, false,24 * GiB ),8192 ); 85CHECK_EQ (prefill_chunk_default (false,0.0 , false, true, false,8 * GiB ),4096 ); 86CHECK_EQ (prefill_chunk_default (false,0.0 , false, true, false,0 ),8192 ); 87// a 12 GB HIP card is not measured: the big chunk 88CHECK_EQ (prefill_chunk_default (false,0.06 , false, true, true,12 * GiB ),8192 ); 89// a 5090-class card (32 GB, share 0.19): in place 8192; with the experts copied into RAM 4096 90CHECK_EQ (prefill_chunk_default (false,0.19 , false, true, false,32 * GiB ),8192 ); 91CHECK_EQ (prefill_chunk_default (false,0.19 , true, true, false,32 * GiB ),4096 ); 92CHECK_EQ (prefill_chunk_default (true,0.19 , true, true, false,32 * GiB ),4096 ); 93CHECK_EQ (prefill_chunk_default (false,0.10 , true, true, false,16 * GiB ),4096 ); 94CHECK_EQ (prefill_chunk_default (false,0.05 , true, true, false,24 * GiB ),4096 ); 95CHECK_EQ (prefill_chunk_default (false,0.06 , true, true, false,12227ull <<20 ),4096 ); 96// copied into RAM on a card under 10 GiB (short VRAM): not the copy rule; the in-place rule for a small card gives 4096, unknown VRAM 8192 97CHECK_EQ (prefill_chunk_default (false,0.0 , true, true, false,8 * GiB ),4096 ); 98CHECK_EQ (prefill_chunk_default (false,0.0 , true, true, false,0 ),8192 ); 99// 1 x R9700 (HIP, 32 GB, share 0.19), single-card default run (tp 1 passed by a multi-card box): 8192, also when copied (HIP is not measured at 4096) 100CHECK_EQ (prefill_chunk_default (true,0.19 , false, true, true,32 * GiB ),8192 ); 101CHECK_EQ (prefill_chunk_default (true,0.19 , true, true, true,32 * GiB ),8192 ); 102// 4 x R9700 (share 0.86), single-card default run (--tp 1 --threads off): the small chunk 103CHECK_EQ (prefill_chunk_default (true,0.86 , false, true, true,32 * GiB ),1024 ); 104CHECK_EQ (prefill_chunk_default (true,0.50 , false, true, true,32 * GiB ),1024 ); 105CHECK_EQ (prefill_chunk_default (true,0.49 , false, true, true,32 * GiB ),8192 ); 106// threads / tp runs (own_default false) keep their defaults whatever the share: 8192 streamed, 2048 for a pack that is not a GGUF 107CHECK_EQ (prefill_chunk_default (false,0.86 , false, true, true,32 * GiB ),8192 ); 108CHECK_EQ (prefill_chunk_default (false,0.30 , false, false, false,24 * GiB ),2048 ); 109// the rule takes no --max-context: a 128K session on a 12 GB card gets the same chunk, because the chunk's buffers and the KV are 110// allocated whole at load for that context and a chunk that does not fit there is halved before the first prompt (4096 -> 2048 -> 1024 ...) 111// copy_all_fits: the 251 GB R9700 box (108 GB lane) copies; a 64 GB PC, a 125 GB box with the 2.25 pack's lane, an unknown RAM do not 112 { 113const double GB = 1e9 ,margin = 8.0 * (1ull <<30 ); 114CHECK_EQ (copy_all_fits (108 * GB , (uint64_t ) (236 * GB ),margin ,1e300 ),1 ); 115CHECK_EQ (copy_all_fits (108 * GB , (uint64_t ) (110 * GB ),margin ,1e300 ),0 );// inside the margin 116CHECK_EQ (copy_all_fits (108 * GB , (uint64_t ) (116 * GB ),margin ,1e300 ),0 );// 108 + 8.6 > 116 117CHECK_EQ (copy_all_fits (108 * GB , (uint64_t ) (118 * GB ),margin ,1e300 ),1 ); 118CHECK_EQ (copy_all_fits (108 * GB , (uint64_t ) (101 * GB ),margin ,1e300 ),0 );// the 125 GB box, free RAM 101 GB 119CHECK_EQ (copy_all_fits (92 * GB , ~uint64_t {0 },margin ,1e300 ),0 );// RAM not known 120CHECK_EQ (copy_all_fits (0 , (uint64_t ) (236 * GB ),margin ,1e300 ),0 );// nothing on the CPU lane 121// Windows: the commit room (RAM + page file beside its margin) must hold the lane too, whatever the free RAM says 122CHECK_EQ (copy_all_fits (108 * GB , (uint64_t ) (236 * GB ),margin ,90 * GB ),0 ); 123CHECK_EQ (copy_all_fits (108 * GB , (uint64_t ) (236 * GB ),margin ,108 * GB ),1 ); 124// the owner-PC class: 64 GB RAM, a 12 GB card (a lane of ~120 GB), 40 GB free, a 90 GB commit limit 125CHECK_EQ (copy_all_fits (120 * GB , (uint64_t ) (40 * GB ),margin ,60 * GB ),0 ); 126// a 192 GB Windows PC with a 4 GB page file: 150 GB free RAM but only 100 GB of commit room: no 127CHECK_EQ (copy_all_fits (108 * GB , (uint64_t ) (150 * GB ),margin ,100 * GB ),0 ); 128 } 129if (fails == 0 ) std::printf ("experts rule: all checks passed\n" ); 130return fails == 0 ?0 :1 ; 131}