okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
30c2b84f2
main
1// switch_point_rule.hpp: the floor from which a prompt chunk takes the streamed walk, from the card's share of non-resident experts. No GPU, no model. 2#include <cmath> 3#include <cstdio> 4 5#include "strata/prefill/switch_point_rule.hpp" 6 7namespace sp= strata::prefill::switch_point; 8 9static int failed = 0 ; 10#define CHECK (x ) do { if (!(x)) { std::fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #x); ++failed; } } while (0) 11 12int main () { 13 static_assert(sp::kFloorDefault == 1024 ,"the default floor is 1,024" ); 14const sp::Constants k ; 15const int64_t total = 24576 ;// 48 layers x 512 experts (Flash-Next) 16 17// every expert resident (or an unknown card): exactly the default 18CHECK (sp::floor_for (total ,total )== 1024 ); 19CHECK (sp::floor_for (total + 5 ,total )== 1024 ); 20CHECK (sp::floor_for (0 ,0 )== 1024 ); 21CHECK (sp::floor_for (-1 ,total )== 1024 ); 22CHECK (sp::floor_for (5000 ,0 )== 1024 ); 23 24// the floor is a multiple of the chunk grid, within [1024, kFloorMax], for every resident count 25int64_t prev = 0 ; 26bool seen_move = false; 27for (int64_t res = 0 ;res <=total ;res += 64 ) { 28const int64_t f = sp::floor_for (res ,total ); 29CHECK (f >=1024 && f <= sp::kFloorMax ); 30CHECK ((f - 1024 ) % sp::kGrid == 0 ); 31if (f != 1024 )seen_move = true; 32 (void )prev ; 33prev = f ; 34 } 35CHECK (seen_move ); 36 37// the A6000 / UD-Q4_K_XL card (55% resident): between the measured 2,049 (staged 1.28x faster) and 8,193 (staged 26% slower), about the measured crossing at 4,097 38 { 39const int64_t res = (int64_t ) (0.55 * (double )total ); 40const int64_t f = sp::floor_for (res ,total ); 41CHECK (f > 2049 && f < 8193 ); 42 } 43// the same card with the measured X (about 12,097 non-resident): the model's own crossing is 4,0xx 44 { 45const int64_t f = sp::floor_for (total - 12097 ,total ); 46CHECK (f >=3584 && f <=4608 ); 47CHECK (sp::advantage_ms (1100 ,12097 ,k )> 1000.0 );// measured 1,390 ms (3,236 against 1,838) 48CHECK (sp::advantage_ms (2049 ,12097 ,k )> 500.0 );// measured 710 ms 49CHECK (std::fabs (sp::advantage_ms (4097 ,12097 ,k ))< 120.0 );// measured 18 ms 50CHECK (sp::advantage_ms (8193 ,12097 ,k )< -700.0 );// measured -950 ms 51 } 52// a card with fewer non-resident experts stages less often: the floor does not rise as more experts are resident 53 { 54int64_t last = sp::kFloorMax + 1 ; 55for (int64_t res = total /8 ;res <=total ;res += 256 ) { 56const int64_t f = sp::floor_for (res ,total ); 57CHECK (f <=last ); 58last = f ; 59 } 60 } 61// the uncovered share: 1 below t0, falling, never above 1 62CHECK (sp::uncovered (100 ,k )== 1.0 ); 63CHECK (sp::uncovered (1100 ,k )> sp::uncovered (4097 ,k )); 64CHECK (sp::uncovered (8193 ,k )< 0.2 ); 65CHECK (sp::uncovered (0 ,k )== 1.0 ); 66 67// per-card scaling: the A6000 (3.19 MB experts, 12.2 GB/s, 84 SMs, 13,580 of 24,576 resident) and the A4000 (1.44 MB, 10.5 GB/s, 48 SMs, 7,836 resident), CPU share off: 68// measured crossings about 3,840 and 1,930 tokens, the rule's floors are within one grid step of them 69 { 70const sp::Card a6000 {3.19 ,12.2 ,84 },a4000 {1.44 ,10.5 ,48 }; 71const int64_t f6 = sp::floor_for_card (13580 ,total ,a6000 ); 72const int64_t f4 = sp::floor_for_card (7836 ,total ,a4000 ); 73CHECK (f6 >=3584 && f6 <=4096 ); 74CHECK (f4 >=1792 && f4 <=2048 ); 75CHECK (f4 < f6 );// the slower card of the two crosses earlier, as measured 76CHECK ((f6 - 1024 ) % sp::kGrid == 0 && (f4 - 1024 ) % sp::kGrid == 0 ); 77// an unknown card (no probe, no SM count): the default 78CHECK (sp::floor_for_card (13580 ,total , sp::Card {0.0 ,12.2 ,84 })== 1024 ); 79CHECK (sp::floor_for_card (13580 ,total , sp::Card {3.19 ,0.0 ,84 })== 1024 ); 80CHECK (sp::floor_for_card (13580 ,total , sp::Card {3.19 ,12.2 ,0 })== 1024 ); 81// every expert resident: the default whatever the card 82CHECK (sp::floor_for_card (total ,total ,a6000 )== 1024 ); 83// the A6000-class card's constants are the fit's own 84const sp::Constants kc = sp::constants_for (sp::Card {}); 85CHECK (std::fabs (kc .c_p - 0.2612 )< 1e-9 && std::fabs (kc .a_s - 0.2294 )< 1e-9 ); 86// faster link, same card: a smaller floor; more SMs, same link: a higher one 87CHECK (sp::floor_for_card (13580 ,total , sp::Card {3.19 ,24.0 ,84 }) <=f6 ); 88CHECK (sp::floor_for_card (13580 ,total , sp::Card {3.19 ,12.2 ,120 }) >=f6 ); 89 } 90// the share's limit: the floor times the measured ratio on the grid, 0 (the share's own default stays) when the floor did not move 91CHECK (sp::share_limit_for (1024 )== 0 ); 92CHECK (sp::share_limit_for (2048 )== 3072 );// A4000: measured break-even about 2,900 93CHECK (sp::share_limit_for (3840 ) % sp::kGrid == 0 && sp::share_limit_for (3840 )> 3840 ); 94 95// the class the rule is on for by default: CUDA, discrete, one GPU, link probe ran, compute capability 8.6; STRATA_SWITCH_POINT=0 is the way back, =1 asks for it elsewhere 96 { 97 sp::DeviceClass in ; 98in .cuda = true;in .discrete = true;in .single_gpu = true;in .link_probed = true;in .cc_major = 8 ;in .cc_minor = 6 ; 99CHECK (sp::in_default_class (in )); 100CHECK (sp::enabled (in ,-1 ));// nothing set: on 101CHECK (!sp::enabled (in ,0 ));// STRATA_SWITCH_POINT=0 (or any value but 1): the way back 102CHECK (sp::enabled (in ,1 ));// STRATA_SWITCH_POINT=1: on 103// every other compute capability is out of the class with nothing set 104const int other [][2 ]= {{6 ,0 }, {6 ,1 }, {7 ,0 }, {7 ,5 }, {8 ,0 }, {8 ,7 }, {8 ,9 }, {9 ,0 }, {10 ,0 }, {12 ,0 }, {12 ,1 }, {0 ,0 }}; 105for (const auto & cc :other ) { 106 sp::DeviceClass d = in ; 107d .cc_major = cc [0 ];d .cc_minor = cc [1 ]; 108CHECK (!sp::in_default_class (d )); 109CHECK (!sp::enabled (d ,-1 )); 110CHECK (sp::enabled (d ,1 ));// the explicit opt-in still works on another CUDA card 111CHECK (!sp::enabled (d ,0 )); 112 } 113// each other condition on its own takes a card out of the class 114 { sp::DeviceClass d = in ;d .single_gpu = false;CHECK (!sp::enabled (d ,-1 ));CHECK (!sp::enabled (d ,0 )); }// two cards / a layer split 115 { sp::DeviceClass d = in ;d .link_probed = false;CHECK (!sp::enabled (d ,-1 )); }// no probe 116 { sp::DeviceClass d = in ;d .discrete = false;CHECK (!sp::enabled (d ,-1 )); }// integrated / shared memory 117 { sp::DeviceClass d = in ;d .cuda = false;CHECK (!sp::enabled (d ,-1 ));CHECK (!sp::enabled (d ,1 ));CHECK (!sp::enabled (d ,0 )); }// HIP: never, whatever the cc says 118 { sp::DeviceClass d ;CHECK (!sp::enabled (d ,-1 ));CHECK (!sp::enabled (d ,1 )); }// nothing known 119// the variable: unset or empty = default, exactly "1" = on, anything else = off 120CHECK (sp::env_state (nullptr )== -1 ); 121CHECK (sp::env_state ("" )== -1 ); 122CHECK (sp::env_state ("1" )== 1 ); 123CHECK (sp::env_state ("0" )== 0 ); 124CHECK (sp::env_state ("off" )== 0 ); 125CHECK (sp::env_state ("11" )== 0 ); 126// the class gives the measured floors: the A6000 and the A4000 are both 8.6 127CHECK (sp::kClassCcMajor == 8 && sp::kClassCcMinor == 6 ); 128CHECK (sp::floor_for_card (13580 ,total , sp::Card {3.14 ,12.2 ,84 })== 3584 ); 129CHECK (sp::share_limit_for (3584 )== 5632 ); 130CHECK (sp::floor_for_card (7836 ,total , sp::Card {1.44 ,11.0 ,48 })== 2048 ); 131CHECK (sp::share_limit_for (2048 )== 3072 ); 132 } 133 134if (failed ) { 135 std::fprintf (stderr ,"switch_point_rule_test: %d check(s) failed\n" ,failed ); 136return 1 ; 137 } 138 std::printf ("switch_point_rule_test: OK\n" ); 139return 0 ; 140}