okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
3b0ca805f
main
1// pick_ww's rule (include/strata/prefill/fused_tile_rule.hpp) for every card class the fused-tile measurements cover, at the two sides of every threshold, and a 2// byte-for-byte comparison with the rule as it stood before the sm_120 40-79 SM class was added, for every other class: no GPU, no model. 3#include <cstdint> 4#include <cstdio> 5#include <initializer_list> 6 7#include "strata/prefill/fused_tile_rule.hpp" 8#include "strata/prefill/fused_tq_default.hpp" 9 10using strata::prefill::fused_tile::Card ; 11using strata::prefill::fused_tile::pick ; 12using strata::prefill::fused_tile::Pick ; 13using strata::prefill::fused_tile::Why ; 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// routed rows of 512 experts at a given average (avg * 512, rounded up) and the ones next to a threshold 19static constexpr int E = 512 ; 20static constexpr int64_t rows_at (double avg ) {return (int64_t ) (avg * E + 0.999 ); } 21 22// the rule of commit 083b1ac82 (the PRO 5000 tier included, the 40-79 SM class not yet there): the reference for "nothing changes" 23static int legacy (const Card & c ,int64_t n ,int n_expert ) { 24const double avg = (double )n / (n_expert > 1 ?n_expert :1 ); 25if (c .s121 )return n >=65536 ?2 :4 ; 26if (c .s120 && c .sms >=80 && (avg >=159.9 || (c .sms >=100 && c .sms < 150 && avg > 112.0 )))return 2 ; 27return avg > 56.0 && avg <=112.0 ?2 :4 ; 28} 29 30int main () { 31const Card rtx5070 {true, false,48 },rtx5070ti {true, false,70 },rtx5060ti {true, false,36 },rtx5080 {true, false,84 },pro5000 {true, false,110 }, 32rtx5090 {true, false,170 },pro6000 {true, false,188 },a6000 {false, false,84 },a4000 {false, false,48 },gb10 {false, true,48 },hip {false, false,64 }; 33 34// ---- the thresholds of the 40-79 SM class (the RTX 5070): the band is gone, one bound at 119.9 rows an expert 35CHECK (pick (rtx5070 ,rows_at (56.1 ),E ).ww == 4 );// inside the old band: 64 now 36CHECK (pick (rtx5070 ,57344 ,E ).ww == 4 );// 112.0 rows an expert: the old band's top, 64 now 37CHECK (pick (rtx5070 ,61388 ,E ).ww == 4 && pick (rtx5070 ,61389 ,E ).ww == 2 );// 119.898 / 119.9004 38CHECK (pick (rtx5070 ,61430 ,E ).ww == 2 );// a prompt of 6,144 tokens: 6,143 positions = 61,430 rows = 119.98 39CHECK (pick (rtx5070 ,81910 ,E ).ww == 2 && pick (rtx5070 ,200000 ,E ).ww == 2 && pick (rtx5070 ,320000 ,E ).ww == 2 ); 40CHECK (pick (rtx5070 ,40960 ,E ).ww == 4 );// 4,096 tokens: 64 41CHECK (pick (rtx5070 ,61430 ,E ).why == Why ::kS120Small && pick (rtx5070 ,28673 ,E ).why == Why ::kS120Small ); 42CHECK (pick (rtx5070ti ,61388 ,E ).ww == 4 && pick (rtx5070ti ,61389 ,E ).ww == 2 );// 70 SMs: in the class 43// the class edges: 39 SMs keep the old rule, 40 and 79 take the new one, 80 the large-card one 44CHECK (pick (Card {true, false,39 },40960 ,E ).why == Why ::kBand && pick (Card {true, false,39 },40960 ,E ).ww == 2 );// 80 rows: the old band 45CHECK (pick (Card {true, false,40 },40960 ,E ).why == Why ::kS120Small && pick (Card {true, false,40 },40960 ,E ).ww == 4 ); 46CHECK (pick (Card {true, false,79 },40960 ,E ).why == Why ::kS120Small && pick (Card {true, false,79 },40960 ,E ).ww == 4 ); 47CHECK (pick (Card {true, false,80 },40960 ,E ).why == Why ::kBand && pick (Card {true, false,80 },40960 ,E ).ww == 2 ); 48CHECK (pick (rtx5060ti ,40960 ,E ).why == Why ::kBand );// 36 SMs: not measured, keeps today's rule 49 50// ---- sm_120 with 80+ SMs: the PRO 6000 / 5090 / 5080 (80-99 and 150+ SMs): the band, and 128 from 159.9 (the PRO 6000 row: 8,191 positions = 81,910 rows) 51for (const Card & c : {pro6000 ,rtx5090 ,rtx5080 }) { 52CHECK (pick (c ,28672 ,E ).ww == 4 && pick (c ,28673 ,E ).ww == 2 );// the band's bottom: 56.0 / 56.002 53CHECK (pick (c ,57344 ,E ).ww == 2 && pick (c ,57345 ,E ).ww == 4 );// the band's top: 112.0 / 112.002 54CHECK (pick (c ,61430 ,E ).ww == 4 );// 6,144 tokens: 64 (the PRO 6000 loses at 120 rows) 55CHECK (pick (c ,81868 ,E ).ww == 4 && pick (c ,81869 ,E ).ww == 2 );// 159.898 / 159.900 56CHECK (pick (c ,81910 ,E ).ww == 2 && pick (c ,200000 ,E ).ww == 2 && pick (c ,320000 ,E ).ww == 2 ); 57CHECK (pick (c ,81910 ,E ).why == Why ::kS120Large ); 58 } 59// ---- sm_120 with 100-149 SMs (the PRO 5000): 128 rows from the band's bottom on, without a gap 60CHECK (pick (pro5000 ,28672 ,E ).ww == 4 && pick (pro5000 ,28673 ,E ).ww == 2 ); 61CHECK (pick (pro5000 ,57344 ,E ).ww == 2 && pick (pro5000 ,57345 ,E ).ww == 2 && pick (pro5000 ,57345 ,E ).why == Why ::kS120Mid ); 62CHECK (pick (pro5000 ,61430 ,E ).ww == 2 && pick (pro5000 ,81910 ,E ).ww == 2 && pick (pro5000 ,81910 ,E ).why == Why ::kS120Large ); 63CHECK (pick (Card {true, false,99 },61430 ,E ).ww == 4 && pick (Card {true, false,100 },61430 ,E ).ww == 2 );// 99 / 100 SMs 64CHECK (pick (Card {true, false,149 },61430 ,E ).ww == 2 && pick (Card {true, false,150 },61430 ,E ).ww == 4 );// 149 / 150 SMs 65 66// ---- sm_86 (A6000 84 SMs, A4000 48 SMs: not sm_120, so the SM count does not matter), HIP builds: the old band only 67for (const Card & c : {a6000 ,a4000 ,hip }) { 68CHECK (pick (c ,28672 ,E ).ww == 4 && pick (c ,28673 ,E ).ww == 2 && pick (c ,57344 ,E ).ww == 2 && pick (c ,57345 ,E ).ww == 4 ); 69CHECK (pick (c ,61430 ,E ).ww == 4 && pick (c ,81910 ,E ).ww == 4 && pick (c ,320000 ,E ).ww == 4 ); 70CHECK (pick (c ,81910 ,E ).why == Why ::kBand ); 71 } 72// ---- sm_121 (GB10): 128 rows from 65,536 routed rows, whatever the SM count 73CHECK (pick (gb10 ,65535 ,E ).ww == 4 && pick (gb10 ,65536 ,E ).ww == 2 && pick (gb10 ,65536 ,E ).why == Why ::kS121 ); 74CHECK (pick (Card {true, true,48 },65535 ,E ).ww == 4 && pick (Card {true, true,48 },65536 ,E ).ww == 2 );// s121 wins over s120 75// ---- no experts: the average is rows themselves, never a division by zero 76CHECK (pick (rtx5070 ,0 ,0 ).ww == 4 && pick (rtx5070 ,120 ,0 ).ww == 2 ); 77 78// ---- staged launches (weight staging, both kernels of the launch staged): 128 rows from every size on sm_86 / sm_120 cards with 1 to 79 SMs 79 { 80const Card a4000s {false, false,48 , true},rtx3060 {false, false,28 , true},s86at79 {false, false,79 , true},s86at80 {false, false,80 , true},a6000s {false, false,84 , true}, 81s120at79 {true, false,79 , false},s120at80 {true, false,80 , false},s89 {false, false,60 , false},unknown {false, false,0 , true}; 82for (int64_t n : {int64_t (0 ),int64_t (1 ),int64_t (10000 ),int64_t (28672 ),int64_t (40960 ),int64_t (57344 ),int64_t (61388 ),int64_t (81910 ),int64_t (200000 ),int64_t (700000 )}) { 83for (const Card & c : {a4000s ,rtx3060 ,s86at79 ,rtx5070 ,rtx5070ti ,s120at79 ,Card {true, false,40 , false}}) { 84CHECK (pick (c ,n ,E , true).ww == 2 && pick (c ,n ,E , true).why == Why ::kStaged ); 85 } 86// the same cards, unstaged: today's rule, byte for byte (the default argument is `false`) 87for (const Card & c : {a4000s ,rtx3060 ,s86at79 ,rtx5070 ,rtx5070ti ,s120at79 }) { 88CHECK (pick (c ,n ,E , false).ww == pick (c ,n ,E ).ww && pick (c ,n ,E , false).why == pick (c ,n ,E ).why ); 89CHECK (pick (c ,n ,E ).why != Why ::kStaged ); 90 } 91// outside the class a staged launch keeps the rule: 80+ SMs, sm_121, a card with neither flag (sm_89 / sm_80: not measured), an unknown SM count 92for (const Card & c : {s86at80 ,a6000s ,s120at80 ,rtx5080 ,pro5000 ,pro6000 ,rtx5090 ,gb10 ,Card {true, true,48 , true},s89 ,unknown }) { 93CHECK (pick (c ,n ,E , true).ww == pick (c ,n ,E , false).ww && pick (c ,n ,E , true).why == pick (c ,n ,E , false).why ); 94 } 95 } 96// the edges: 79 SMs in the class, 80 out of it 97CHECK (pick (s86at79 ,40960 ,E , true).why == Why ::kStaged && pick (s86at80 ,40960 ,E , true).why == Why ::kBand ); 98CHECK (pick (s120at79 ,40960 ,E , true).why == Why ::kStaged && pick (s120at80 ,40960 ,E , true).why == Why ::kBand ); 99// the cases the 5070 class refuses unstaged (below 119.9 rows an expert) are 128 when staged; the A4000 (sm_86) had no rule of its own below 80 SMs 100CHECK (pick (rtx5070 ,40960 ,E , false).ww == 4 && pick (rtx5070 ,40960 ,E , true).ww == 2 ); 101CHECK (pick (a4000s ,81910 ,E , false).ww == 4 && pick (a4000s ,81910 ,E , true).ww == 2 ); 102CHECK (pick (a4000s ,40960 ,E , false).ww == 2 && pick (a4000s ,20000 ,E , false).ww == 4 && pick (a4000s ,20000 ,E , true).ww == 2 );// band: 80 rows 128, 39 rows 64 103 } 104// ---- the Q6_K / Q8_0 gate/up class (wide) on sm_86 cards with 40+ SMs (the 40-79 SM cards and the 80+ SM ones such as the A6000): 128 rows above 112 rows an expert, everything else as it was 105 { 106const Card a4000w {false, false,48 , true},s86at39 {false, false,39 , true},s86at40 {false, false,40 , true},s86at79 {false, false,79 , true},s86at80 {false, false,80 , true}, 107s120at48 {true, false,48 , false},s89 {false, false,60 , false}; 108CHECK (pick (a4000w ,57344 ,E , false, true).ww == 2 && pick (a4000w ,57344 ,E , false, true).why == Why ::kBand );// 112.0 rows an expert: the old band's top, 128 as before 109CHECK (pick (a4000w ,57345 ,E , false, true).ww == 2 && pick (a4000w ,57345 ,E , false, true).why == Why ::kS86Wide );// 112.002: the line 110CHECK (pick (a4000w ,57345 ,E , false, false).ww == 4 && pick (a4000w ,57345 ,E , false, false).why == Why ::kBand );// not the class: 64 as before 111CHECK (pick (a4000w ,81910 ,E , false, true).ww == 2 && pick (a4000w ,320000 ,E , false, true).ww == 2 ); 112CHECK (pick (a4000w ,28672 ,E , false, true).ww == 4 && pick (a4000w ,28673 ,E , false, true).ww == 2 );// below 112: the band, unchanged 113// the same count of rows on Qwen3.6's 256 experts: 4,096 tokens x 8 = 32,768 rows = 128 an expert (the line), 2,048 tokens = 64 an expert (the band) 114CHECK (pick (a4000w ,32768 ,256 , false, true).ww == 2 && pick (a4000w ,32768 ,256 , false, true).why == Why ::kS86Wide && pick (a4000w ,16384 ,256 , false, true).why == Why ::kBand ); 115// the class edges: 39 SMs keeps the old rule, 40 / 79 / 80 take the line; other cards (sm_120, sm_89 / sm_80) never do 116CHECK (pick (s86at39 ,81910 ,E , false, true).ww == 4 ); 117CHECK (pick (s86at40 ,81910 ,E , false, true).ww == 2 && pick (s86at79 ,81910 ,E , false, true).ww == 2 && pick (s86at80 ,81910 ,E , false, true).ww == 2 ); 118// the RTX A6000 (sm_86, 84 SMs): both sides of 112 rows an expert, and the i-quant pairs (not wide) keep the rule OWN-4X gated (64 rows above the band) 119const Card a6000w {false, false,84 , true}; 120CHECK (pick (a6000w ,57344 ,E , false, true).ww == 2 && pick (a6000w ,57344 ,E , false, true).why == Why ::kBand ); 121CHECK (pick (a6000w ,57345 ,E , false, true).ww == 2 && pick (a6000w ,57345 ,E , false, true).why == Why ::kS86Wide ); 122CHECK (pick (a6000w ,57345 ,E , false, false).ww == 4 && pick (a6000w ,81910 ,E , false, false).ww == 4 && pick (a6000w ,81910 ,E , false, true).ww == 2 && pick (a6000w ,320000 ,E , false, true).ww == 2 ); 123CHECK (pick (a6000w ,32768 ,256 , false, true).why == Why ::kS86Wide && pick (a6000w ,16384 ,256 , false, true).why == Why ::kBand && pick (a6000w ,16384 ,256 , false, true).ww == 2 );// Qwen3.6: 4K tokens 128 an expert, 2K tokens 64 124CHECK (pick (s120at48 ,40960 ,E , false, true).why == pick (s120at48 ,40960 ,E , false, false).why && pick (s89 ,81910 ,E , false, true).ww == 4 ); 125// wide = false is the rule of the previous commit for every class; wide = true moves only sm_86 with 40+ SMs above 112 rows an expert, and only from 64 to 128 126long long cmpw = 0 ,badw = 0 ; 127for (int s120 = 0 ;s120 < 2 ;++ s120 ) 128for (int s121 = 0 ;s121 < 2 ;++ s121 ) 129for (int s86 = 0 ;s86 < 2 ;++ s86 ) 130for (int sms : {0 ,1 ,39 ,40 ,48 ,79 ,80 ,84 ,100 ,110 ,150 ,188 }) 131for (int staged = 0 ;staged < 2 ;++ staged ) 132for (int ne : {1 ,256 ,512 }) 133for (int64_t n = 0 ;n <=400000 ;n += 11 ) { 134const Card c {s120 != 0 ,s121 != 0 ,sms ,s86 != 0 }; 135const Pick a = pick (c ,n ,ne ,staged != 0 , false),b = pick (c ,n ,ne ,staged != 0 , true); 136++ cmpw ; 137const double avg = (double )n / (ne > 1 ?ne :1 ); 138const bool line = !s121 && !(staged && (s86 || s120 )&& sms > 0 && sms < 80 )&& !(s120 && sms >=40 && sms < 80 )&& !(s120 && sms >=80 && (avg >=159.9 || (sms >=100 && sms < 150 && avg > 112.0 )))&& 139s86 && sms >=40 && avg > 112.0 ; 140if (line ? !(b .ww == 2 && b .why == Why ::kS86Wide ) : (a .ww != b .ww || a .why != b .why ))++ badw ; 141 } 142CHECK (badw == 0 ); 143 std::printf ("fused_tile_rule_test: wide class: %lld card / launch combinations checked (%lld wrong)\n" ,cmpw ,badw ); 144 } 145// ---- the default rule (native_supported) with the 40-79 SM rule on or off (STRATA_PF_FUSED_SM40, on unless 0; see below): takes_by_default(card, sm40, gate_staged, down_ok) 146 { 147using strata::prefill::fused_tile::takes_by_default ; 148const Card a4000s {false, false,48 , true},s86at39 {false, false,39 , true},s86at40 {false, false,40 , true},s86at79 {false, false,79 , true},s86at80 {false, false,80 , true}, 149s120at48 {true, false,48 , false},s120at39 {true, false,39 , false},s89 {false, false,60 , false},gb10c {true, true,48 , false}; 150// a staged-gate pair (IQ3_S / IQ4_NL, IQ3_XXS / Q2_0, ...) on 48 SMs: on with the switch, today's behaviour (off) without it 151CHECK (takes_by_default (a4000s , true, true, true)&& !takes_by_default (a4000s , false, true, true)); 152CHECK (takes_by_default (s120at48 , true, true, true)&& !takes_by_default (s120at48 , false, true, true)); 153// IQ2_S / Q2_0 (gate/up not staged) and a down outside IQ4_NL / Q2_0: off, switch or not 154CHECK (!takes_by_default (a4000s , true, false, true)&& !takes_by_default (a4000s , true, true, false)&& !takes_by_default (a4000s , true, false, false)); 155// the bound: 39 SMs off, 40 and 79 on, 80 on without the switch 156CHECK (!takes_by_default (s86at39 , true, true, true)&& !takes_by_default (s120at39 , true, true, true)); 157CHECK (takes_by_default (s86at40 , true, true, true)&& takes_by_default (s86at79 , true, true, true)); 158CHECK (!takes_by_default (s86at79 , false, true, true)&& takes_by_default (s86at80 , false, false, false)); 159// other cards: sm_89 / sm_80 (neither flag) and an unknown SM count stay off with the switch; the s121 flag is not looked at by this rule (GB10 has its own tile rule only) 160CHECK (!takes_by_default (s89 , true, true, true)&& !takes_by_default (Card {false, false,0 , true}, true, true, true)); 161CHECK (takes_by_default (gb10c , true, true, true)== takes_by_default (s120at48 , true, true, true)); 162// switch unset: exactly today's rule (sm_86 / sm_120 with 80+ SMs) for every card flag set, SM count and launch kind 163long long cmpd = 0 ,bad = 0 ; 164for (int s120 = 0 ;s120 < 2 ;++ s120 ) 165for (int s86 = 0 ;s86 < 2 ;++ s86 ) 166for (int sms = 0 ;sms <=200 ;++ sms ) 167for (int g = 0 ;g < 2 ;++ g ) 168for (int d = 0 ;d < 2 ;++ d ) { 169++ cmpd ; 170const Card c {s120 != 0 , false,sms ,s86 != 0 }; 171if (takes_by_default (c , false,g != 0 ,d != 0 )!= ((s120 || s86 )&& sms >=80 ))++ bad ; 172if (sms >=80 && takes_by_default (c , true,g != 0 ,d != 0 )!= takes_by_default (c , false,g != 0 ,d != 0 ))++ bad ;// the switch moves nothing at 80+ SMs 173 } 174CHECK (bad == 0 ); 175 std::printf ("fused_tile_rule_test: default rule with the switch unset: %lld card / launch combinations compared with 'sm_86 / sm_120, 80+ SMs' (%lld differing)\n" ,cmpd ,bad ); 176 } 177// ---- the 2-bit gate/up formats in the default (owner 11-12): default_gate_format / default_takes(env, card, sm40, gate_staged, down_ok, gu_type) 178 { 179using namespace strata::prefill::fused_tile ; 180// the ids are ggml's (GGML_TYPE_*, asserted in moe_fused_iq.cu): IQ2_XXS 16, IQ2_XS 17, IQ3_XXS 18, IQ1_S 19, IQ4_NL 20, IQ3_S 21, IQ2_S 22, IQ4_XS 23 181CHECK (default_gate_format (16 )&& default_gate_format (17 ));// IQ2_XXS, IQ2_XS: new 182CHECK (default_gate_format (18 )&& default_gate_format (21 )&& default_gate_format (22 )&& default_gate_format (23 ));// IQ3_XXS, IQ3_S, IQ2_S, IQ4_XS: as before 183CHECK (!default_gate_format (19 )&& !default_gate_format (20 )&& !default_gate_format (29 )&& !default_gate_format (12 )&& !default_gate_format (0 ));// IQ1_S, IQ4_NL, IQ1_M, Q4_K, F32 184const int kIq2 [2 ]= {16 ,17 }; 185long long cmp2 = 0 ,bad2 = 0 ; 186for (int gu :kIq2 ) 187for (int s120 = 0 ;s120 < 2 ;++ s120 ) 188for (int s86 = 0 ;s86 < 2 ;++ s86 ) 189for (int s121 = 0 ;s121 < 2 ;++ s121 ) 190for (int sms = 0 ;sms <=200 ;++ sms ) 191for (int sm40 = 0 ;sm40 < 2 ;++ sm40 ) 192for (int d = 0 ;d < 2 ;++ d ) { 193++ cmp2 ; 194const Card c {s120 != 0 ,s121 != 0 ,sms ,s86 != 0 }; 195const bool want = (s120 || s86 )&& sms >=80 ;// sm_86 / sm_120, 80+ SMs; gate_staged is false for IQ2_XXS / IQ2_XS (sw_on), so sm40 adds nothing 196if (default_takes (-1 ,c ,sm40 != 0 , false,d != 0 ,gu )!= want )++ bad2 ; 197if (default_takes (0 ,c ,sm40 != 0 , false,d != 0 ,gu ))++ bad2 ;// STRATA_PF_FUSED=0: the default is off again 198if (default_takes (1 ,c ,sm40 != 0 , false,d != 0 ,gu ))++ bad2 ;// STRATA_PF_FUSED=1 is the opt-in, decided elsewhere 199 } 200CHECK (bad2 == 0 ); 201 std::printf ("fused_tile_rule_test: IQ2_XXS / IQ2_XS default: %lld card / switch combinations (%lld wrong)\n" ,cmp2 ,bad2 ); 202// named cards: 80 SMs yes, 79 no, 48 no, AMD (no s86 / s120 flag) no at any SM count, sm_89 / sm_80 (no flag) no 203CHECK (default_takes (-1 ,Card {false, false,84 , true}, false, false, true,16 )&& default_takes (-1 ,Card {false, false,80 , true}, false, false, true,17 ));// A6000, 3080 Ti 204CHECK (!default_takes (-1 ,Card {false, false,79 , true}, false, false, true,16 )&& !default_takes (-1 ,Card {false, false,79 , true}, true, false, true,17 ));// 79 SMs: no, switch or not 205CHECK (!default_takes (-1 ,Card {false, false,48 , true}, true, false, true,16 )&& !default_takes (-1 ,Card {true, false,48 , false}, true, false, true,17 ));// A4000, 5070 206CHECK (default_takes (-1 ,Card {true, false,100 , false}, false, false, true,16 ));// PRO 5000 (sm_120) 207CHECK (!default_takes (-1 ,Card {false, false,304 , false}, true, false, true,16 )&& !default_takes (-1 ,Card {false, false,128 , false}, false, false, true,17 ));// AMD / sm_89: no 208// the formats already in the list keep their rule: IQ3_XXS on 80+ SMs yes, with the sm40 switch and a staged gate also 40-79 209CHECK (default_takes (-1 ,Card {false, false,84 , true}, false, true, true,18 )&& !default_takes (-1 ,Card {false, false,60 , true}, false, true, true,18 )&& 210default_takes (-1 ,Card {false, false,60 , true}, true, true, true,18 )); 211// a format outside the list never takes the default 212CHECK (!default_takes (-1 ,Card {false, false,84 , true}, true, true, true,19 )&& !default_takes (-1 ,Card {false, false,84 , true}, true, true, true,20 )); 213 } 214// ---- the 40-79 SM cards in the default (owner 11-12): the staged path is the default there, STRATA_PF_FUSED_SM40=0 the way back. default_takes_env(env, card, sm40_env, gate_staged, down_ok, gu_type) 215 { 216using namespace strata::prefill::fused_tile ; 217CHECK (sm40_on (-1 )&& sm40_on (1 )&& !sm40_on (0 ));// unset and "1" on, "0" off 218// the whole grid against an oracle written from the sentence of the decision: STRATA_PF_FUSED unset; a format of the default list (IQ2_XXS 16, IQ2_XS 17, IQ3_XXS 18, IQ3_S 21, IQ2_S 22, IQ4_XS 23; not 219// Q8_0 8, Q4_K 12, Q5_K 13, Q6_K 14, IQ1_S 19, IQ4_NL 20, IQ1_M 29); a card with the sm_86 or sm_120 flag; then 80+ SMs always, and 40-79 SMs only for a staged gate/up with an IQ4_NL / Q2_0 down while 220// the switch is not "0" 221const int kTypes []= {8 ,12 ,13 ,14 ,16 ,17 ,18 ,19 ,20 ,21 ,22 ,23 ,29 }; 222long long cmp3 = 0 ,bad3 = 0 ,yes40 = 0 ,yes80 = 0 ; 223for (int gu :kTypes ) 224for (int env = -1 ;env <=1 ;++ env ) 225for (int sm40e = -1 ;sm40e <=1 ;++ sm40e ) 226for (int s120 = 0 ;s120 < 2 ;++ s120 ) 227for (int s86 = 0 ;s86 < 2 ;++ s86 ) 228for (int s121 = 0 ;s121 < 2 ;++ s121 ) 229for (int sms = 0 ;sms <=200 ;++ sms ) 230for (int g = 0 ;g < 2 ;++ g ) 231for (int d = 0 ;d < 2 ;++ d ) { 232++ cmp3 ; 233const Card c {s120 != 0 ,s121 != 0 ,sms ,s86 != 0 }; 234const bool inlist = gu == 16 || gu == 17 || gu == 18 || gu == 21 || gu == 22 || gu == 23 ; 235const bool card = s120 || s86 ; 236const bool want = env == -1 && inlist && card && (sms >=80 || (sms >=40 && sm40e != 0 && g != 0 && d != 0 )); 237if (default_takes_env (env ,c ,sm40e ,g != 0 ,d != 0 ,gu )!= want )++ bad3 ; 238if (want && sms < 80 )++ yes40 ; 239if (want && sms >=80 )++ yes80 ; 240 } 241CHECK (bad3 == 0 ); 242CHECK (yes40 > 0 && yes80 > 0 ); 243 std::printf ("fused_tile_rule_test: 40-79 SM default: %lld env / switch / card / SM count / launch combinations (%lld wrong; %lld yes below 80 SMs, %lld yes at 80+)\n" ,cmp3 ,bad3 ,yes40 ,yes80 ); 244// named cards, switch unset: the A4000 (sm_86, 48 SMs) and the RTX 5070 (sm_120, 48 SMs) take a staged gate/up with an IQ4_NL / Q2_0 down; switch "0" takes them back; "1" is the same as unset 245const Card a4000c {false, false,48 , true},r5070 {true, false,48 , false},a6000c {false, false,84 , true},tiny {false, false,39 , true},sm60 {false, false,56 , false},gb10c {true, true,48 , false}; 246for (int gu : {18 ,21 ,23 })// IQ3_XXS, IQ3_S, IQ4_XS 247for (bool dn : {true}) { 248CHECK (default_takes_env (-1 ,a4000c ,-1 , true,dn ,gu )&& default_takes_env (-1 ,r5070 ,-1 , true,dn ,gu )&& default_takes_env (-1 ,a4000c ,1 , true,dn ,gu )); 249CHECK (!default_takes_env (-1 ,a4000c ,0 , true,dn ,gu )&& !default_takes_env (-1 ,r5070 ,0 , true,dn ,gu )); 250CHECK (default_takes_env (-1 ,a6000c ,0 , true,dn ,gu ));// 80+ SMs: the switch moves nothing 251CHECK (!default_takes_env (-1 ,tiny ,-1 , true,dn ,gu )&& !default_takes_env (-1 ,sm60 ,-1 , true,dn ,gu ));// 39 SMs, a card with neither flag (sm_60 / sm_80 / sm_89 / AMD): as before 252CHECK (!default_takes_env (0 ,a4000c ,-1 , true,dn ,gu )&& !default_takes_env (1 ,a4000c ,-1 , true,dn ,gu ));// STRATA_PF_FUSED=0 is MMQ, =1 is the opt-in decided elsewhere 253 } 254CHECK (!default_takes_env (-1 ,a4000c ,-1 , false, true,22 ));// IQ2_S gate/up is not a staged instance: stays on MMQ on 48 SMs (IQ2_S / Q2_0 was 0.97 of MMQ at 32K) 255CHECK (!default_takes_env (-1 ,a4000c ,-1 , true, false,21 ));// an IQ3_S gate/up with a down outside IQ4_NL / Q2_0 (Q5_0, Q8_0, K-quants): not taken 256CHECK (!default_takes_env (-1 ,a4000c ,-1 , false, true,16 )&& !default_takes_env (-1 ,a4000c ,-1 , false, true,17 ));// IQ2_XXS / IQ2_XS: no staged instance, MMQ below 80 SMs 257CHECK (!default_takes_env (-1 ,a4000c ,-1 , true, true,12 )&& !default_takes_env (-1 ,a4000c ,-1 , true, true,13 )&& !default_takes_env (-1 ,a4000c ,-1 , true, true,14 ));// Q4_K / Q5_K / Q6_K: opt-in as before 258CHECK (default_takes_env (-1 ,gb10c ,-1 , true, true,21 )== default_takes_env (-1 ,r5070 ,-1 , true, true,21 ));// the s121 flag is not looked at by this rule 259 } 260// ---- the sibling classes (owner 11-12, "1 Yes"): compute capability 8.0 (A100 class) and 8.9 (RTX 40 / Ada class) take the rules of the cards we tested (8.6; 12.0). The grid below starts from the compute 261// capability, builds the card the way dev_info() in src/prefill/moe_fused_iq.cu does (s120 = 12.0 and STRATA_SM120_PERF not 0, s121 = 12.1, s86 = s86_class(cc, STRATA_PF_FUSED_SM86=0)) and checks the 262// default of native_supported (default_takes_env), the Gyro-S default (fused_tq::on) and the tile rule (pick) against oracles written from the sentence of the decision, not from the code 263 { 264using namespace strata::prefill::fused_tile ; 265namespace tq= strata::prefill::fused_tq; 266struct Cc {int major ,minor ; }; 267// every capability a CUDA card the engine sees can report, 8.7 (Jetson Orin) and 9.0 (Hopper) included: the class is 8.0, 8.6 and 8.9 only 268const Cc kCc []= {{6 ,0 }, {6 ,1 }, {7 ,0 }, {7 ,5 }, {8 ,0 }, {8 ,6 }, {8 ,7 }, {8 ,9 }, {9 ,0 }, {10 ,0 }, {12 ,0 }, {12 ,1 }}; 269const auto flagged = [](const Cc & c ,bool off ,bool perf120 ) {// the oracle: which cards the rules call tested-class 270const bool a = !off && c .major == 8 && (c .minor == 0 || c .minor == 6 || c .minor == 9 ); 271const bool b = c .major == 12 && c .minor == 0 && perf120 ; 272return a || b ; 273 }; 274const auto card_for = [](const Cc & c ,int sms ,bool off ,bool perf120 ) {// dev_info() 275return Card {c .major == 12 && c .minor == 0 && perf120 ,c .major == 12 && c .minor == 1 ,sms ,s86_class (c .major ,c .minor ,off )}; 276 }; 277// the class function itself over a wider range: 0.0 .. 15.9, only 8.0 / 8.6 / 8.9 are in; the old rule (8.6 only) differs from it at exactly 8.0 and 8.9 278long long cls = 0 ,cls_bad = 0 ,cls_moved = 0 ; 279for (int major = 0 ;major < 16 ;++ major ) 280for (int minor = 0 ;minor < 10 ;++ minor ) { 281++ cls ; 282if (s86_class (major ,minor )!= (major == 8 && (minor == 0 || minor == 6 || minor == 9 )))++ cls_bad ; 283if (s86_class (major ,minor , true))++ cls_bad ;// STRATA_PF_FUSED_SM86=0: nothing is in the class 284if (s86_class (major ,minor )!= (major == 8 && minor == 6 ))++ cls_moved ; 285 } 286CHECK (cls_bad == 0 && cls_moved == 2 ); 287// the default of native_supported: STRATA_PF_FUSED (unset / 0 / 1) x SM40 switch (unset / 0 / 1) x STRATA_PF_FUSED_SM86=0 x STRATA_SM120_PERF=0 x 12 capabilities x 0-200 SMs x gate_staged x down_ok x 13 formats 288const int kTypes []= {8 ,12 ,13 ,14 ,16 ,17 ,18 ,19 ,20 ,21 ,22 ,23 ,29 }; 289long long cmp = 0 ,bad = 0 ,yes_hi = 0 ,yes_mid = 0 ,yes_new = 0 ; 290for (const Cc & cc :kCc ) 291for (int off = 0 ;off < 2 ;++ off ) 292for (int perf = 0 ;perf < 2 ;++ perf ) 293for (int env = -1 ;env <=1 ;++ env ) 294for (int sm40e = -1 ;sm40e <=1 ;++ sm40e ) 295for (int sms = 0 ;sms <=200 ;++ sms ) 296for (int g = 0 ;g < 2 ;++ g ) 297for (int d = 0 ;d < 2 ;++ d ) 298for (int gu :kTypes ) { 299++ cmp ; 300const bool inlist = gu == 16 || gu == 17 || gu == 18 || gu == 21 || gu == 22 || gu == 23 ;// IQ2_XXS, IQ2_XS, IQ3_XXS, IQ3_S, IQ2_S, IQ4_XS 301const bool want = env == -1 && inlist && flagged (cc ,off != 0 ,perf != 0 )&& (sms >=80 || (sms >=40 && sm40e != 0 && g != 0 && d != 0 )); 302const bool got = default_takes_env (env ,card_for (cc ,sms ,off != 0 ,perf != 0 ),sm40e ,g != 0 ,d != 0 ,gu ); 303if (got != want )++ bad ; 304if (want && (cc .minor == 0 || cc .minor == 9 )&& cc .major == 8 ) {if (sms >=80 )++ yes_hi ;else ++ yes_mid ;++ yes_new ; } 305 } 306CHECK (bad == 0 ); 307CHECK (yes_hi > 0 && yes_mid > 0 && yes_new == yes_hi + yes_mid ); 308 std::printf ("fused_tile_rule_test: sibling classes, default rule: %lld capability / switch / SM count / launch / format combinations (%lld wrong; the new 8.0 / 8.9 cards: %lld yes at 80+ SMs, %lld yes at 40-79); class function: %lld capabilities (%lld wrong, %lld moved from the old rule)\n" , 309cmp ,bad ,yes_hi ,yes_mid ,cls ,cls_bad ,cls_moved ); 310// what must NOT move: for every capability but 8.0 / 8.9 the card, and with it every answer, is the one the old flag (8.6 only) gave 311long long same = 0 ,moved_card = 0 ; 312for (const Cc & cc :kCc ) 313for (int off = 0 ;off < 2 ;++ off ) 314for (int sms : {0 ,1 ,39 ,40 ,48 ,79 ,80 ,84 ,108 ,128 ,200 }) { 315const Card now = card_for (cc ,sms ,off != 0 , true); 316const Card old {cc .major == 12 && cc .minor == 0 ,cc .major == 12 && cc .minor == 1 ,sms ,cc .major == 8 && cc .minor == 6 && off == 0 }; 317++ same ; 318if (now .s120 != old .s120 || now .s121 != old .s121 || now .sms != old .sms || now .s86 != old .s86 ) { 319++ moved_card ; 320CHECK (cc .major == 8 && (cc .minor == 0 || cc .minor == 9 )&& off == 0 );// only the two new capabilities, and not under STRATA_PF_FUSED_SM86=0 321 } 322 } 323CHECK (moved_card == 2 * 11 );// 8.0 and 8.9, SM86 switch not 0, 11 SM counts 324 std::printf ("fused_tile_rule_test: sibling classes: %lld capability / SM count / switch cards built, %lld differ from the old flag (8.0 and 8.9 without STRATA_PF_FUSED_SM86=0 only)\n" ,same ,moved_card ); 325// Gyro-S default (fused_tq_default.hpp): STRATA_PF_FUSED_TQ unset / 0 / 1 (the parsed value) x capability x SM86 switch x SM count: on for env 1, else only the class with 80+ SMs 326long long cmpq = 0 ,badq = 0 ; 327for (const Cc & cc :kCc ) 328for (int off = 0 ;off < 2 ;++ off ) 329for (int tqe = -1 ;tqe <=1 ;++ tqe ) 330for (int sms = 0 ;sms <=200 ;++ sms ) { 331++ cmpq ; 332const bool want = tqe == 1 || (tqe == -1 && flagged (cc ,off != 0 , false)&& sms >=80 );// (perf120 false: the 12.0 card is not in the Gyro rule, whatever STRATA_SM120_PERF says) 333if (tq::on (tqe ,card_for (cc ,sms ,off != 0 , true).s86 ,sms )!= want )++ badq ; 334 } 335CHECK (badq == 0 ); 336CHECK (tq::on (-1 ,card_for ({8 ,0 },108 , false, true).s86 ,108 )&& tq::on (-1 ,card_for ({8 ,9 },128 , false, true).s86 ,128 )&& !tq::on (-1 ,card_for ({8 ,9 },60 , false, true).s86 ,60 )&& 337 !tq::on (-1 ,card_for ({12 ,0 },110 , false, true).s86 ,110 )&& !tq::on (-1 ,card_for ({8 ,0 },108 , true, true).s86 ,108 )&& !tq::on (-1 ,card_for ({8 ,7 },128 , false, true).s86 ,128 )); 338 std::printf ("fused_tile_rule_test: sibling classes, Gyro-S default: %lld capability / switch / SM count combinations (%lld wrong)\n" ,cmpq ,badq ); 339// the tile rule: an 8.0 or 8.9 card of N SMs picks, for every launch kind and size, what an 8.6 card of N SMs picks (the same class); 8.7 / 9.0 / 7.5 / 6.x pick what a card with no flag picks 340long long cmpt = 0 ,badt = 0 ; 341for (int sms : {0 ,1 ,39 ,40 ,48 ,56 ,60 ,76 ,79 ,80 ,84 ,108 ,128 ,142 ,200 }) 342for (int staged = 0 ;staged < 2 ;++ staged ) 343for (int wide = 0 ;wide < 2 ;++ wide ) 344for (int ne : {1 ,256 ,512 }) 345for (int64_t n = 0 ;n <=400000 ;n += 53 ) { 346const Pick ref = pick (card_for ({8 ,6 },sms , false, true),n ,ne ,staged != 0 ,wide != 0 ); 347const Pick none = pick (Card {false, false,sms , false},n ,ne ,staged != 0 ,wide != 0 ); 348for (const Cc & cc :kCc ) { 349if (cc .major == 12 )continue ; 350const Pick p = pick (card_for (cc ,sms , false, true),n ,ne ,staged != 0 ,wide != 0 ); 351const bool in_class = cc .major == 8 && (cc .minor == 0 || cc .minor == 6 || cc .minor == 9 ); 352const Pick & want = in_class ?ref :none ; 353++ cmpt ; 354if (p .ww != want .ww || p .why != want .why )++ badt ; 355 } 356 } 357CHECK (badt == 0 ); 358 std::printf ("fused_tile_rule_test: sibling classes, tile rule: %lld capability / SM count / launch combinations (%lld wrong)\n" ,cmpt ,badt ); 359// named cards, switches unset: A100 (8.0, 108 SMs) and RTX 4090 (8.9, 128): every format of the default list; A30 (8.0, 56) and RTX 4070 Ti (8.9, 60): the staged launches only; RTX 4060 Ti (8.9, 34): nothing 360for (int gu : {16 ,17 ,18 ,21 ,22 ,23 }) { 361CHECK (default_takes_env (-1 ,card_for ({8 ,0 },108 , false, true),-1 , false, true,gu )&& default_takes_env (-1 ,card_for ({8 ,9 },128 , false, true),-1 , false, true,gu )); 362CHECK (!default_takes_env (-1 ,card_for ({8 ,9 },34 , false, true),-1 , true, true,gu )&& !default_takes_env (-1 ,card_for ({8 ,0 },56 , false, true),0 , true, true,gu )); 363 } 364for (int gu : {18 ,21 ,23 })CHECK (default_takes_env (-1 ,card_for ({8 ,0 },56 , false, true),-1 , true, true,gu )&& default_takes_env (-1 ,card_for ({8 ,9 },60 , false, true),-1 , true, true,gu )); 365CHECK (!default_takes_env (-1 ,card_for ({8 ,9 },60 , false, true),-1 , false, true,22 )&& !default_takes_env (-1 ,card_for ({8 ,0 },56 , false, true),-1 , false, true,16 ));// IQ2_S / IQ2_XXS: unstaged, MMQ below 80 SMs 366CHECK (!default_takes_env (-1 ,card_for ({8 ,9 },128 , false, true),-1 , true, true,12 )&& !default_takes_env (-1 ,card_for ({8 ,0 },108 , false, true),-1 , true, true,14 )&& 367 !default_takes_env (-1 ,card_for ({8 ,9 },128 , false, true),-1 , true, true,8 ));// Q4_K / Q6_K / Q8_0: opt-in on every class 368CHECK (!default_takes_env (0 ,card_for ({8 ,0 },108 , false, true),-1 , true, true,21 )&& !default_takes_env (1 ,card_for ({8 ,9 },128 , false, true),-1 , true, true,21 ));// STRATA_PF_FUSED=0 is MMQ, =1 the opt-in decided elsewhere 369CHECK (!default_takes_env (-1 ,card_for ({8 ,0 },108 , true, true),-1 , true, true,21 )&& !default_takes_env (-1 ,card_for ({8 ,9 },128 , true, true),-1 , true, true,21 ));// STRATA_PF_FUSED_SM86=0: MMQ again 370CHECK (!default_takes_env (-1 ,card_for ({8 ,7 },128 , false, true),-1 , true, true,21 )&& !default_takes_env (-1 ,card_for ({9 ,0 },132 , false, true),-1 , true, true,21 )&& 371 !default_takes_env (-1 ,card_for ({7 ,5 },72 , false, true),-1 , true, true,21 )&& !default_takes_env (-1 ,card_for ({6 ,0 },56 , false, true),-1 , true, true,21 ));// Orin, Hopper, Turing, Pascal: untouched 372 } 373// every class, every launch size: staged = false gives the rule of commit 083b1ac82 exactly as before the argument existed, for the sm_86 cards too (the new flag does not move them) 374 { 375long long cmp = 0 ,diff = 0 ; 376for (const Card & c : {a6000 ,a4000 ,Card {false, false,48 , true},Card {false, false,84 , true},Card {false, false,28 , true},pro6000 ,pro5000 ,rtx5090 ,rtx5080 ,gb10 ,hip }) 377for (int ne : {1 ,64 ,256 ,512 ,1024 }) 378for (int64_t n = 0 ;n <=700000 ;n += 13 ) { 379++ cmp ; 380if (pick (c ,n ,ne , false).ww != legacy (c ,n ,ne ))++ diff ; 381 } 382CHECK (diff == 0 ); 383 std::printf ("fused_tile_rule_test: staged = false: %lld launches compared against the previous rule (%lld differing)\n" ,cmp ,diff ); 384 } 385 386// ---- "nothing changes": every class except the 40-79 SM one gives, for every launch size, what the rule of commit 083b1ac82 gave 387long long compared = 0 ,differing = 0 ; 388for (const Card & c : {pro6000 ,rtx5090 ,rtx5080 ,pro5000 ,a6000 ,a4000 ,gb10 ,hip ,rtx5060ti ,Card {true, false,39 },Card {true, false,80 },Card {true, false,99 }, 389Card {true, false,100 },Card {true, false,149 },Card {true, false,150 },Card {false, false,0 },Card {false, false,200 }}) 390for (int ne : {1 ,64 ,256 ,512 ,1024 }) 391for (int64_t n = 0 ;n <=700000 ;n += 13 ) { 392++ compared ; 393if (pick (c ,n ,ne ).ww != legacy (c ,n ,ne ))++ differing ; 394 } 395CHECK (differing == 0 ); 396// and the new class differs from the old rule exactly where intended: below 119.9 rows only in the old band (64 now), above it only above the band (128 now) 397long long new_class = 0 ,wrong = 0 ; 398for (const Card & c : {rtx5070 ,rtx5070ti ,Card {true, false,40 },Card {true, false,79 }}) 399for (int64_t n = 0 ;n <=700000 ;n += 7 ) { 400const double avg = (double )n /E ; 401const int got = pick (c ,n ,E ).ww ,old = legacy (c ,n ,E ); 402++ new_class ; 403const int want = avg >=119.9 ?2 :4 ; 404if (got != want )++ wrong ; 405if (got != old && !((avg > 56.0 && avg <=112.0 )|| avg >=119.9 ))++ wrong ;// it differs from the old rule only inside the old band or at 119.9 and up 406 } 407CHECK (wrong == 0 ); 408 std::printf ("fused_tile_rule_test: %lld launches compared against the previous rule (%lld differing), %lld launches of the 40-79 SM class checked, %d failed\n" ,compared , 409differing ,new_class ,failed ); 410// ---- the weight-staging guard (check_staged_blobs's decision): a failed check makes the launch take the unstaged kernel, it never ends the process 411 { 412using namespace strata::prefill::fused_tile ; 413// the blob: only device and managed memory at an even address pass; a pointer the driver does not know (kUnknown) is left to the launch's own error, as before 414long long cases = 0 ; 415for (BlobMem m : {BlobMem ::kDevice ,BlobMem ::kManaged ,BlobMem ::kHost ,BlobMem ::kUnregistered ,BlobMem ::kUnknown }) 416for (std::uintptr_t a : {(std::uintptr_t )0x7f0000001000 , (std::uintptr_t )0x7f0000001002 , (std::uintptr_t )0x7f0000001001 , (std::uintptr_t )0x7f0000001fff , (std::uintptr_t )0 }) { 417++ cases ; 418const bool want = m == BlobMem ::kUnknown || ((m == BlobMem ::kDevice || m == BlobMem ::kManaged )&& (a & 1 )== 0 ); 419CHECK (staged_blob_ok (m ,a )== want ); 420 } 421CHECK (cases == 25 ); 422 static_assert(staged_blob_ok (BlobMem ::kDevice ,0x1000 )&& !staged_blob_ok (BlobMem ::kHost ,0x1000 )&& !staged_blob_ok (BlobMem ::kDevice ,0x1001 )); 423// the geometry: every combination of odd / even for the four values; ok only when all four are even 424for (int bits = 0 ;bits < 16 ;++ bits ) { 425const std::size_t v [4 ]= {(std::size_t ) (1000 + (bits & 1 )), (std::size_t ) (2000 + ((bits >>1 )& 1 )), (std::size_t ) (3000 + ((bits >>2 )& 1 )), (std::size_t ) (4000 + ((bits >>3 )& 1 ))}; 426CHECK (staged_geometry_ok (v [0 ],v [1 ],v [2 ],v [3 ])== (bits == 0 )); 427 } 428CHECK (staged_geometry_ok (0 ,0 ,0 ,0 )&& staged_geometry_ok (136 * 1280 ,72 * 640 ,136 * 640 * 2 ,136 * 1280 * 2 )); 429// the per-process state: the first 256 launches are checked, then trusted; one failure makes every later launch unstaged, and only the first failure is logged 430 { 431StagedGuard g ; 432int checked = 0 ,trusted = 0 ; 433for (int i = 0 ;i < 1000 ;++ i ) { 434const auto s = g .next (); 435CHECK (s != StagedGuard ::Step ::kUnstaged ); 436if (s == StagedGuard ::Step ::kCheck ) {++ checked ;CHECK (!g .report (true)); }else ++ trusted ; 437 } 438CHECK (checked == 256 && trusted == 744 && !g .bad ); 439 } 440 { 441StagedGuard g ; 442for (int i = 0 ;i < 2 ;++ i )CHECK (g .next ()== StagedGuard ::Step ::kCheck && !g .report (true)); 443CHECK (g .next ()== StagedGuard ::Step ::kCheck ); 444CHECK (g .report (false));// the first failure: logged 445for (int i = 0 ;i < 2000 ;++ i )CHECK (g .next ()== StagedGuard ::Step ::kUnstaged );// sticky, also past the 256 checked launches 446CHECK (!g .report (false)&& !g .report (true)&& g .bad );// no second log line, and a passing report does not clear it 447CHECK (g .next ()== StagedGuard ::Step ::kUnstaged ); 448 } 449 {// a failure on the last checked launch, and one with the budget used up (a failure cannot come then: the launch is trusted, nothing to report) 450StagedGuard g ; 451for (int i = 0 ;i < 255 ;++ i )CHECK (g .next ()== StagedGuard ::Step ::kCheck ); 452CHECK (g .next ()== StagedGuard ::Step ::kCheck && g .report (false)); 453CHECK (g .next ()== StagedGuard ::Step ::kUnstaged ); 454 } 455// the fallback's item is the 64-row one (WW 4), which no staging instance shares, and the rule never answers it with a staged item for a launch it does not stage 456CHECK (kUnstagedWw == 4 ); 457CHECK (pick (Card {false, false,48 , true},81910 ,E , false, false).ww == 4 && pick (Card {false, false,48 , true},81910 ,E , true, true).ww == 2 );// the staged 128-row choice the fallback overrides 458 std::printf ("fused_tile_rule_test: staging guard: %lld blob cases, 16 geometries, the check budget and the sticky fallback checked, %d failed so far\n" ,cases ,failed ); 459 } 460// ---- the Qwen3.6 engine (strata-q36): which layers take the fused prompt kernels, q36_takes(env, card, sm40_env, gate_staged, down_ok, covered). The whole grid against an oracle written from the sentence of the 461// decision: a layer whose pair the kernels do not cover, or STRATA_PF_FUSED=0 (or any value not starting with '1'): MMQ. =1: the fused kernels on every card. Unset: the cards of the Flash-Next default rule, an 462// sm_86-class (8.0 / 8.6 / 8.9) or sm_120 card (not sm_121 alone, not a card with neither flag: sm_75, sm_87, sm_90, AMD) with 80+ SMs for every layer, 40-79 SMs for a staged gate/up with a staged down 463// while STRATA_PF_FUSED_SM40 is not "0", under 40 SMs never. 464 { 465using namespace strata::prefill::fused_tile ; 466CHECK (pf_env (nullptr )== -1 && pf_env ("" )== -1 && pf_env ("1" )== 1 && pf_env ("1x" )== 1 && pf_env ("0" )== 0 && pf_env ("2" )== 0 && pf_env ("yes" )== 0 && pf_env ("off" )== 0 ); 467CHECK (sm40_env_of (nullptr )== -1 && sm40_env_of ("0" )== 0 && sm40_env_of ("1" )== 1 && sm40_env_of ("" )== 1 && sm40_env_of ("on" )== 1 ); 468long long cmp = 0 ,bad = 0 ,yes_def = 0 ,yes_opt = 0 ,no_zero = 0 ,yes40 = 0 ,yes80 = 0 ; 469for (int env = -1 ;env <=1 ;++ env ) 470for (int sm40e = -1 ;sm40e <=1 ;++ sm40e ) 471for (int s120 = 0 ;s120 < 2 ;++ s120 ) 472for (int s86 = 0 ;s86 < 2 ;++ s86 ) 473for (int s121 = 0 ;s121 < 2 ;++ s121 ) 474for (int sms = 0 ;sms <=200 ;++ sms ) 475for (int g = 0 ;g < 2 ;++ g ) 476for (int d = 0 ;d < 2 ;++ d ) 477for (int cov = 0 ;cov < 2 ;++ cov ) { 478++ cmp ; 479const Card c {s120 != 0 ,s121 != 0 ,sms ,s86 != 0 }; 480const bool in_class = s120 || s86 ; 481bool want = false; 482if (cov != 0 && env == 1 )want = true; 483if (cov != 0 && env == -1 )want = in_class && (sms >=80 || (sms >=40 && sm40e != 0 && g != 0 && d != 0 )); 484if (q36_takes (env ,c ,sm40e ,g != 0 ,d != 0 ,cov != 0 )!= want )++ bad ; 485if (want && env == -1 )++ yes_def ; 486if (want && env == 1 )++ yes_opt ; 487if (want && env == -1 && sms < 80 )++ yes40 ; 488if (want && env == -1 && sms >=80 )++ yes80 ; 489if (env == 0 && !want )++ no_zero ; 490 } 491CHECK (bad == 0 && yes_def > 0 && yes_opt > 0 && yes40 > 0 && yes80 > 0 && no_zero > 0 ); 492 std::printf ("fused_tile_rule_test: Qwen3.6 engine: %lld switch / SM40 switch / card flag / SM count / format combinations (%lld wrong; %lld yes unset, %lld of them below 80 SMs, %lld at 80+; %lld yes under =1)\n" ,cmp ,bad ,yes_def ,yes40 ,yes80 ,yes_opt ); 493// named cards, formats of UD-Q4_K_XL (a staged K-quant gate/up with a staged Q5_K / Q6_K down), STRATA_PF_FUSED unset 494const Card a6000c {false, false,84 , true},a4000c {false, false,48 , true},a100 {false, false,108 , true},rtx4090 {false, false,128 , true},rtx3050 {false, false,20 , true},pro5000 {true, false,110 , false}, 495rtx5090 {true, false,170 , false},rtx5070 {true, false,48 , false},gb10c {false, true,48 , false},turing {false, false,68 , false},orin {false, false,16 , false},hopper {false, false,132 , false}; 496for (const Card & c : {a6000c ,a100 ,rtx4090 ,pro5000 ,rtx5090 })CHECK (q36_takes (-1 ,c ,-1 , true, true, true)&& q36_takes (-1 ,c ,0 , true, true, true));// 80+ SMs: the SM40 switch moves nothing 497for (const Card & c : {a4000c ,rtx5070 })CHECK (q36_takes (-1 ,c ,-1 , true, true, true)&& q36_takes (-1 ,c ,1 , true, true, true)&& !q36_takes (-1 ,c ,0 , true, true, true));// 40-79 SMs: staged, SM40 not "0" 498for (const Card & c : {rtx3050 ,gb10c ,turing ,orin ,hopper })CHECK (!q36_takes (-1 ,c ,-1 , true, true, true)&& !q36_takes (-1 ,c ,1 , true, true, true));// under 40 SMs, sm_121, no class flag: MMQ 499CHECK (!q36_takes (-1 ,a4000c ,-1 , false, true, true)&& !q36_takes (-1 ,a4000c ,-1 , true, false, true));// an unstaged gate/up or down on 48 SMs: MMQ 500CHECK (q36_takes (-1 ,a6000c ,-1 , false, false, true)&& !q36_takes (-1 ,a6000c ,-1 , true, true, false));// 80+ SMs take any covered pair, never an uncovered one 501// STRATA_PF_FUSED=0 is MMQ on every card; =1 is the fused kernels on every card that covers the pair (the way strata-q36 behaved with the opt-in) 502for (const Card & c : {a6000c ,a4000c ,a100 ,rtx4090 ,pro5000 ,rtx5090 ,rtx5070 ,rtx3050 ,gb10c ,turing ,orin ,hopper }) { 503CHECK (!q36_takes (0 ,c ,-1 , true, true, true)&& q36_takes (1 ,c ,-1 , false, false, true)&& !q36_takes (1 ,c ,-1 , true, true, false)); 504 } 505// the class is takes_by_default's: the same answer as the Flash-Next rule for a covered pair with the staged flags, every card above 506for (const Card & c : {a6000c ,a4000c ,a100 ,rtx4090 ,pro5000 ,rtx5090 ,rtx5070 ,rtx3050 ,gb10c ,turing ,orin ,hopper }) 507for (int sm40e = -1 ;sm40e <=1 ;++ sm40e ) 508for (int g = 0 ;g < 2 ;++ g ) 509for (int d = 0 ;d < 2 ;++ d )CHECK (q36_takes (-1 ,c ,sm40e ,g != 0 ,d != 0 , true)== takes_by_default (c ,sm40_on (sm40e ),g != 0 ,d != 0 )); 510// compute capability: 8.0 / 8.6 / 8.9 are in the class (s86_class), 8.7 / 9.0 / 7.5 are not, STRATA_PF_FUSED_SM86=0 empties it 511CHECK (q36_takes (-1 ,Card {false, false,108 ,s86_class (8 ,0 )},-1 , true, true, true)&& q36_takes (-1 ,Card {false, false,128 ,s86_class (8 ,9 )},-1 , true, true, true)); 512CHECK (!q36_takes (-1 ,Card {false, false,16 ,s86_class (8 ,7 )},-1 , true, true, true)&& !q36_takes (-1 ,Card {false, false,132 ,s86_class (9 ,0 )},-1 , true, true, true)&& 513 !q36_takes (-1 ,Card {false, false,68 ,s86_class (7 ,5 )},-1 , true, true, true)&& !q36_takes (-1 ,Card {false, false,84 ,s86_class (8 ,6 , true)},-1 , true, true, true)); 514 } 515// ---- the K-quant pairs of Qwen3.6's UD-Q4_K_XL in the wide class (tile 128 above 112 rows an expert on sm_86 cards with 40+ SMs): wide_pair(gu_type, d_type) and the rule at the engine's launch sizes 516 { 517using namespace strata::prefill::fused_tile ; 518// ggml type ids: Q8_0 8, Q4_K 12, Q5_K 13, Q6_K 14, IQ3_XXS 18, IQ3_S 21, IQ4_XS 23, IQ4_NL 20, Q5_1 7, Q2_0 (any), Q5_0 6 519long long bad = 0 ,yes = 0 ; 520for (int gu = 0 ;gu < 40 ;++ gu ) 521for (int d = 0 ;d < 40 ;++ d ) { 522const bool want = gu == 14 || gu == 8 || ((gu == 12 || gu == 13 )&& (d == 13 || d == 14 )); 523if (wide_pair (gu ,d )!= want )++ bad ; 524yes += want ?1 :0 ; 525 } 526CHECK (bad == 0 && yes == 84 );// gu 14 (40 downs) + gu 8 (40) + {12, 13} x {13, 14} (4); 527CHECK (wide_pair (12 ,13 )&& wide_pair (12 ,14 )&& wide_pair (13 ,13 )&& wide_pair (13 ,14 )&& wide_pair (14 ,8 )&& wide_pair (8 ,8 )); 528CHECK (!wide_pair (12 ,7 )&& !wide_pair (12 ,8 )&& !wide_pair (13 ,7 )&& !wide_pair (12 ,20 )&& !wide_pair (18 ,20 )&& !wide_pair (21 ,20 )&& !wide_pair (23 ,20 )&& !wide_pair (22 ,2 ));// Flash-Next's K-quant gate/up (Q5_1 / Q8_0 down) and the i-quant pairs keep their rule 529// Qwen3.6: 256 experts, top 8, a chunk of T tokens = T * 8 rows an expert-average of T / 32: 1,024 / 1,792 / 2,048 / 4,096 / 8,192 / 20,000 tokens 530const Card a6000 {false, false,84 , true},a4000 {false, false,48 , true},a100 {false, false,108 , true},rtx4090 {false, false,128 , true},pro5000 {true, false,110 , false},rtx5090 {true, false,170 , false}; 531const bool w = wide_pair (12 ,13 ); 532for (const Card & c : {a6000 ,a100 ,rtx4090 }) {// sm_86 class with 80+ SMs 533CHECK (pick (c ,1024 * 8 ,256 , true,w ).ww == 4 && pick (c ,1792 * 8 ,256 , true,w ).ww == 4 );// 32 and 56 rows an expert: the band's bottom, 64 as before 534CHECK (pick (c ,2048 * 8 ,256 , true,w ).ww == 2 && pick (c ,2048 * 8 ,256 , true,w ).why == Why ::kBand );// 64: the band, 128 as before 535for (int T : {4096 ,8192 ,20000 })CHECK (pick (c , (int64_t )T * 8 ,256 , true,w ).ww == 2 && pick (c , (int64_t )T * 8 ,256 , true,w ).why == Why ::kS86Wide );// 128 / 256 / 625: 128 rows now 536for (int T : {4096 ,8192 ,20000 })CHECK (pick (c , (int64_t )T * 8 ,256 , true, false).ww == 4 );// not the class: the old pick, 64 rows 537CHECK (pick (c ,4096 * 8 ,256 , true,w ).ww == 2 && pick (c ,3584 * 8 ,256 , true,w ).ww == 2 && pick (c ,3584 * 8 ,256 , true, false).ww == 2 );// 112 rows an expert (3,584 tokens): band, 128 either way 538CHECK (pick (c ,3585 * 8 ,256 , true,w ).ww == 2 && pick (c ,3585 * 8 ,256 , true, false).ww == 4 );// 112.03: the line 539 } 540// under 80 SMs the staged rule already takes 128 (A4000), sm_120 never looks at wide 541for (int T : {1024 ,2048 ,4096 ,8192 ,20000 }) { 542const int64_t n = (int64_t )T * 8 ; 543CHECK (pick (a4000 ,n ,256 , true,w ).ww == 2 && pick (a4000 ,n ,256 , true,w ).why == Why ::kStaged && pick (a4000 ,n ,256 , true, false).ww == 2 ); 544for (const Card & c : {pro5000 ,rtx5090 ,Card {true, false,48 , false}})CHECK (pick (c ,n ,256 , true,w ).ww == pick (c ,n ,256 , true, false).ww && pick (c ,n ,256 , true,w ).why == pick (c ,n ,256 , true, false).why ); 545 } 546CHECK (pick (pro5000 ,4096 * 8 ,256 , true,w ).ww == 2 && pick (pro5000 ,2048 * 8 ,256 , true,w ).ww == 2 && pick (rtx5090 ,4096 * 8 ,256 , true,w ).ww == 4 );// the 110-SM card picks 128 from 56 rows an expert; the 170-SM one is as before 547// the whole sm_86 grid at the Qwen3.6 shape against an oracle written from the sentence: staged and wide, an sm_86-class card: 1-79 SMs 128 rows; 80+ SMs 128 rows from above 56 rows an expert, else 64 548long long bad2 = 0 ,cmp2 = 0 ; 549for (int sms = 1 ;sms <=200 ;++ sms ) 550for (int T = 256 ;T <=24576 ;T += 64 ) { 551const int64_t n = (int64_t )T * 8 ; 552const double avg = (double )n /256.0 ; 553const int want = sms < 80 ?2 : (avg > 56.0 ?2 :4 ); 554++ cmp2 ; 555if (pick (Card {false, false,sms , true},n ,256 , true, true).ww != want )++ bad2 ; 556 } 557CHECK (bad2 == 0 ); 558 std::printf ("fused_tile_rule_test: Qwen3.6 K-quant pairs in the wide class: %lld sm_86 SM count / chunk combinations (%lld wrong)\n" ,cmp2 ,bad2 ); 559 } 560// ---- Flash-Next's K-quant pairs (UD-Q4_K_XL: gate/up Q4_K or Q5_K with a Q5_1 or Q8_0 down): kq_flash_kind(gu_type, d_type) and the tile rule's kq argument. Flash-Next: 512 experts, top 10, a launch of T tokens = T * 10 rows. 561 { 562using namespace strata::prefill::fused_tile ; 563// ggml type ids: Q5_1 7, Q8_0 8, Q4_K 12, Q5_K 13, Q6_K 14, IQ3_S 21, IQ4_NL 20 564long long bad = 0 ; 565for (int gu = 0 ;gu < 40 ;++ gu ) 566for (int d = 0 ;d < 40 ;++ d ) { 567const int want = (gu == 12 || gu == 13 ) ? (d == 7 ?1 :d == 8 ?2 :0 ) :0 ; 568if (kq_flash_kind (gu ,d )!= want )++ bad ; 569if (kq_flash_kind (gu ,d )!= 0 && wide_pair (gu ,d ))++ bad ;// disjoint from Qwen3.6's K-quant pairs (a Q5_K / Q6_K down) 570 } 571CHECK (bad == 0 ); 572CHECK (kq_flash_kind (12 ,7 )== 1 && kq_flash_kind (12 ,8 )== 2 && kq_flash_kind (13 ,8 )== 2 && kq_flash_kind (13 ,7 )== 1 );// the three UD-Q4_K_XL pairs (and Q5_K / Q5_1, not in the set) 573CHECK (kq_flash_kind (12 ,13 )== 0 && kq_flash_kind (13 ,14 )== 0 && kq_flash_kind (14 ,8 )== 0 && kq_flash_kind (8 ,8 )== 0 && kq_flash_kind (21 ,20 )== 0 && kq_flash_kind (18 ,20 )== 0 );// Qwen3.6's pairs, Q6_K / Q8_0, Q8_0 / Q8_0, the i-quant pairs: none 574// the whole grid against an oracle written from the sentence: kq == 0 is the rule as it was; kq 1 or 2 on an sm_86-class card with 80 to 99 SMs and at least 5.8 rows an expert: 128 rows (why kS86Kq); kq == 2 on an sm_120 card 575// with 100 to 149 SMs and at least 5.8 rows an expert: 128 rows (why kS120Kq); everything else the rule as it was 576long long cmp = 0 ,bad2 = 0 ,moved86 = 0 ,moved120 = 0 ; 577for (int s120 = 0 ;s120 < 2 ;++ s120 ) 578for (int s86 = 0 ;s86 < 2 ;++ s86 ) 579for (int s121 = 0 ;s121 < 2 ;++ s121 ) 580for (int sms = 0 ;sms <=200 ;++ sms ) 581for (int T = 64 ;T <=40000 ;T += 61 ) 582for (int st = 0 ;st < 2 ;++ st ) 583for (int wd = 0 ;wd < 2 ;++ wd ) 584for (int kq = 0 ;kq <=2 ;++ kq ) { 585const Card c {s120 != 0 ,s121 != 0 ,sms ,s86 != 0 }; 586const int64_t n = (int64_t )T * 10 ; 587const double avg = (double )n /512.0 ; 588const Pick base = pick (c ,n ,512 ,st != 0 ,wd != 0 ,0 ); 589const Pick got = pick (c ,n ,512 ,st != 0 ,wd != 0 ,kq ); 590bool s86_line = false,s120_line = false; 591if (!c .s121 && avg >=5.8 ) { 592// the lines before the new ones answer first: the staged rule (40-79 SMs) and the sm_120 rules 593const bool before = (c .s121 )|| (st != 0 && (c .s86 || c .s120 )&& sms > 0 && sms < 80 )|| 594 (c .s120 && sms >=80 && (avg >=159.9 || (sms >=100 && sms < 150 && avg > 112.0 )))|| (c .s120 && sms >=40 && sms < 80 )|| 595 (wd != 0 && c .s86 && sms >=40 && avg > 112.0 ); 596s86_line = kq != 0 && c .s86 && sms >=80 && sms < 100 && !before ; 597s120_line = kq == 2 && c .s120 && sms >=100 && sms < 150 && !before && !s86_line ; 598 } 599++ cmp ; 600if (s86_line ) { 601if (got .ww != 2 || got .why != Why ::kS86Kq )++ bad2 ; 602if (base .ww != 2 )++ moved86 ; 603 }else if (s120_line ) { 604if (got .ww != 2 || got .why != Why ::kS120Kq )++ bad2 ; 605if (base .ww != 2 )++ moved120 ; 606 }else if (got .ww != base .ww || got .why != base .why )++ bad2 ; 607 } 608CHECK (bad2 == 0 && moved86 > 0 && moved120 > 0 ); 609 std::printf ("fused_tile_rule_test: Flash-Next K-quant pairs: %lld card flag / SM count / launch size / switch combinations (%lld wrong; %lld launches move to 128 rows on sm_86 with 80-99 SMs, %lld on sm_120 with 100-149 SMs)\n" ,cmp ,bad2 ,moved86 ,moved120 ); 610// named cards and sizes (tokens x 10 rows over 512 experts): 300 / 1,000 / 2,048 / 4,096 / 8,192 / 20,000 / 32,768 tokens 611const Card a6000 {false, false,84 , true},a4000 {false, false,48 , true},a100 {false, false,108 , true},rtx4090 {false, false,128 , true},pro5000 {true, false,110 , false}, 612rtx5090 {true, false,170 , false},rtx5070 {true, false,48 , false},gb10 {false, true,48 , false}; 613for (int k : {1 ,2 }) 614for (int T : {300 ,1000 ,2048 ,4096 ,8192 ,20000 ,32768 }) { 615const int64_t n = (int64_t )T * 10 ; 616CHECK (pick (a6000 ,n ,512 , true, false,k ).ww == 2 && pick (a6000 ,n ,512 , true, false,k ).why == Why ::kS86Kq );// A6000: 128 rows at every size measured, MMQ / fused 1.65 1.49 1.52 1.29 1.04 0.97 0.88 (Q4_K / Q5_1) 617CHECK (pick (a4000 ,n ,512 , true, false,k ).ww == 2 && pick (a4000 ,n ,512 , true, false,k ).why == Why ::kStaged );// 48 SMs: the staged rule decides first, 128 rows from every size 618 } 619for (int T : {300 ,1000 ,2048 ,4096 ,8192 ,20000 ,32768 }) {// the classes not measured keep today's pick 620const int64_t n = (int64_t )T * 10 ; 621for (int k : {1 ,2 }) 622for (const Card & c : {a100 ,rtx4090 ,rtx5090 ,rtx5070 ,gb10 })CHECK (pick (c ,n ,512 , true, false,k ).ww == pick (c ,n ,512 , true, false,0 ).ww && pick (c ,n ,512 , true, false,k ).why == pick (c ,n ,512 , true, false,0 ).why ); 623CHECK (pick (pro5000 ,n ,512 , true, false,1 ).ww == pick (pro5000 ,n ,512 , true, false,0 ).ww );// the PRO 5000 and the Q5_1 down (43 layers): today's pick 624 } 625// the card's edges and the size's edge: 80 and 99 SMs in, 79 (not staged) and 100 out; 5.8 rows an expert = 2,969.6 rows 626CHECK (pick (Card {false, false,80 , true},2970 ,512 , true, false,1 ).why == Why ::kS86Kq && pick (Card {false, false,99 , true},2970 ,512 , true, false,1 ).why == Why ::kS86Kq ); 627CHECK (pick (Card {false, false,100 , true},2970 ,512 , true, false,1 ).why == Why ::kBand && pick (Card {false, false,100 , true},2970 ,512 , true, false,1 ).ww == 4 ); 628CHECK (pick (Card {false, false,84 , true},2969 ,512 , true, false,1 ).why == Why ::kBand && pick (Card {false, false,84 , true},2969 ,512 , true, false,1 ).ww == 4 );// under 5.8 rows an expert: as before 629CHECK (pick (Card {false, false,84 , true},2970 ,512 , true, false,1 ).ww == 2 && pick (Card {false, false,84 , true},2970 ,512 , true, false,0 ).ww == 4 ); 630// sm_120, the two Q8_0-down pairs: 128 rows from 5.8 rows an expert on 100 to 149 SMs; the Q5_1 down and every other sm_120 class keep today's pick 631CHECK (pick (pro5000 ,3000 ,512 , true, false,2 ).ww == 2 && pick (pro5000 ,3000 ,512 , true, false,2 ).why == Why ::kS120Kq && pick (pro5000 ,3000 ,512 , true, false,0 ).ww == 4 );// 300 tokens: 64 before 632CHECK (pick (pro5000 ,20480 ,512 , true, false,2 ).ww == 2 && pick (pro5000 ,20480 ,512 , true, false,2 ).why == Why ::kS120Kq );// 2,048 tokens: 64 before 633CHECK (pick (pro5000 ,40960 ,512 , true, false,2 ).ww == 2 && pick (pro5000 ,40960 ,512 , true, false,0 ).ww == 2 && pick (pro5000 ,61440 ,512 , true, false,2 ).why == Why ::kS120Mid && pick (pro5000 ,81920 ,512 , true, false,2 ).why == Why ::kS120Large );// 80 rows: 128 either way; above 112 the rules above decide 634CHECK (pick (Card {true, false,99 , false},3000 ,512 , true, false,2 ).ww == 4 && pick (Card {true, false,150 , false},3000 ,512 , true, false,2 ).ww == 4 && pick (Card {true, false,100 , false},3000 ,512 , true, false,2 ).ww == 2 ); 635CHECK (pick (pro5000 ,3000 ,512 , true, false,1 ).ww == 4 && pick (pro5000 ,20480 ,512 , true, false,1 ).ww == 4 );// Q5_1 down on the PRO 5000: 64 rows below 56 rows an expert, as measured 636// Qwen3.6 (256 experts, top 8) is a different call: kq is never passed there, and wide_pair's answers above are unchanged 637CHECK (pick (a6000 ,2048 * 8 ,256 , true, true,0 ).ww == pick (a6000 ,2048 * 8 ,256 , true, true).ww ); 638 } 639// ---- Flash-Next's K-quant experts by default: kq_takes(kq_env, card, sm40_env), whether dev_info sets the K-quant kernels up. The whole grid against an oracle written from the sentence of the decision: 640// STRATA_PF_FUSED_KQ=0 (or any value not starting with '1'): never. =1: every card. Unset: the cards of the i-quant default rule (an sm_86-class or sm_120 card, not sm_121 alone, not a card with neither 641// flag) with 80+ SMs, 40-79 SMs while STRATA_PF_FUSED_SM40 is not "0", under 40 SMs never. 642 { 643using namespace strata::prefill::fused_tile ; 644long long cmp = 0 ,bad = 0 ,yes_def = 0 ,yes40 = 0 ,yes80 = 0 ; 645for (int env = -1 ;env <=1 ;++ env ) 646for (int sm40e = -1 ;sm40e <=1 ;++ sm40e ) 647for (int s120 = 0 ;s120 < 2 ;++ s120 ) 648for (int s86 = 0 ;s86 < 2 ;++ s86 ) 649for (int s121 = 0 ;s121 < 2 ;++ s121 ) 650for (int sms = 0 ;sms <=200 ;++ sms ) { 651const Card c {s120 != 0 ,s121 != 0 ,sms ,s86 != 0 }; 652bool want = false; 653if (env == 1 )want = true; 654if (env == -1 )want = (s120 || s86 )&& (sms >=80 || (sms >=40 && sm40e != 0 )); 655++ cmp ; 656if (kq_takes (env ,c ,sm40e )!= want )++ bad ; 657if (want && env == -1 ) {++ yes_def ; (sms >=80 ?yes80 :yes40 )++ ; } 658 } 659CHECK (bad == 0 && yes_def > 0 && yes40 > 0 && yes80 > 0 ); 660 std::printf ("fused_tile_rule_test: Flash-Next K-quant default: %lld switch / SM40 switch / card flag / SM count combinations (%lld wrong; %lld yes unset: %lld at 80+ SMs, %lld at 40-79)\n" ,cmp ,bad ,yes_def ,yes80 ,yes40 ); 661// the switch as the engine reads it: the same reading as STRATA_PF_FUSED's (pf_env) 662CHECK (pf_env (nullptr )== -1 && pf_env ("" )== -1 && pf_env ("1" )== 1 && pf_env ("0" )== 0 && pf_env ("off" )== 0 ); 663// named cards, switch unset 664const Card a6000 {false, false,84 , true},a40 {false, false,84 , true},a4000 {false, false,48 , true},a100 {false, false,108 , true},rtx4090 {false, false,128 , true},pro5000 {true, false,110 , false}, 665rtx5090 {true, false,170 , false},rtx5070 {true, false,48 , false},gb10 {false, true,48 , false},rtx3050 {false, false,20 , true},turing {false, false,68 , false},hopper {false, false,132 , false}; 666for (const Card & c : {a6000 ,a40 ,a100 ,rtx4090 ,pro5000 ,rtx5090 })CHECK (kq_takes (-1 ,c ,-1 )&& kq_takes (-1 ,c ,0 )&& kq_takes (-1 ,c ,1 )&& !kq_takes (0 ,c ,-1 )&& kq_takes (1 ,c ,-1 ));// 80+ SMs: the SM40 switch moves nothing 667for (const Card & c : {a4000 ,rtx5070 })CHECK (kq_takes (-1 ,c ,-1 )&& kq_takes (-1 ,c ,1 )&& !kq_takes (-1 ,c ,0 )&& !kq_takes (0 ,c ,-1 )&& kq_takes (1 ,c ,0 ));// 40-79 SMs: on, STRATA_PF_FUSED_SM40=0 off 668for (const Card & c : {gb10 ,rtx3050 ,turing ,hopper })CHECK (!kq_takes (-1 ,c ,-1 )&& !kq_takes (-1 ,c ,1 )&& !kq_takes (0 ,c ,-1 )&& kq_takes (1 ,c ,-1 ));// sm_121, under 40 SMs, no class flag: the opt-in only 669// the same answer as the i-quant rule for a staged gate/up with a staged down, every card above 670for (const Card & c : {a6000 ,a4000 ,a100 ,rtx4090 ,pro5000 ,rtx5090 ,rtx5070 ,gb10 ,rtx3050 ,turing ,hopper }) 671for (int sm40e = -1 ;sm40e <=1 ;++ sm40e )CHECK (kq_takes (-1 ,c ,sm40e )== takes_by_default (c ,sm40_on (sm40e ), true, true)); 672// compute capability: 8.0 / 8.6 / 8.9 are in the class (s86_class), 8.7 / 9.0 / 7.5 are not, STRATA_PF_FUSED_SM86=0 empties it 673CHECK (kq_takes (-1 ,Card {false, false,108 ,s86_class (8 ,0 )},-1 )&& kq_takes (-1 ,Card {false, false,128 ,s86_class (8 ,9 )},-1 )); 674CHECK (!kq_takes (-1 ,Card {false, false,16 ,s86_class (8 ,7 )},-1 )&& !kq_takes (-1 ,Card {false, false,132 ,s86_class (9 ,0 )},-1 )&& !kq_takes (-1 ,Card {false, false,68 ,s86_class (7 ,5 )},-1 )&& 675 !kq_takes (-1 ,Card {false, false,84 ,s86_class (8 ,6 , true)},-1 )); 676 } 677// the Qwen3.6 engine on HIP: gfx12 only, STRATA_PF_FUSED=1 takes a covered pair, =0 never, unset never (a build with MMQ keeps its route); a build without MMQ reads unset as 1 678 { 679using namespace strata::prefill::fused_tile ; 680for (int env = -1 ;env <=1 ;++ env ) 681for (int gfx12 = 0 ;gfx12 <=1 ;++ gfx12 ) 682for (int covered = 0 ;covered <=1 ;++ covered )CHECK (q36_takes_gfx12 (env ,gfx12 != 0 ,covered != 0 )== (env == 1 && gfx12 && covered )); 683CHECK (q36_start_env (false,-1 )== 1 && q36_start_env (false,0 )== 0 && q36_start_env (false,1 )== 1 );// no MMQ: unset -> 1, the user's value kept 684CHECK (q36_start_env (true,-1 )== -1 && q36_start_env (true,0 )== 0 && q36_start_env (true,1 )== 1 );// with MMQ: unchanged 685// the two together: a build without MMQ takes a covered gfx12 layer by default and =0 takes it back; a build with MMQ takes none until the user sets 1 686for (int env = -1 ;env <=1 ;++ env ) { 687CHECK (q36_takes_gfx12 (q36_start_env (false,env ), true, true)== (env != 0 )); 688CHECK (q36_takes_gfx12 (q36_start_env (true,env ), true, true)== (env == 1 )); 689CHECK (!q36_takes_gfx12 (q36_start_env (false,env ), false, true)); 690 } 691 } 692// Turing (t75_pick): an i-quant gate/up takes 128 rows from above 56 rows an expert with no upper end; a Q2_0 gate/up keeps the band of pick (the rule as it was for it) 693 { 694using strata::prefill::fused_tile::t75_pick ; 695const Card none {}; 696CHECK (t75_pick (rows_at (5.0 ),E , false)== 4 && t75_pick (rows_at (19.5 ),E , false)== 4 && t75_pick (rows_at (40.0 ),E , false)== 4 );// 256 / 1,000 / 2,048 tokens: 64 rows 697CHECK (t75_pick (28672 ,E , false)== 4 && t75_pick (28673 ,E , false)== 2 );// 56.0 / 56.002 rows an expert 698CHECK (t75_pick (40950 ,E , false)== 2 && t75_pick (57344 ,E , false)== 2 );// 4,096 tokens; 112.0 (the band's top) 699CHECK (t75_pick (57345 ,E , false)== 2 && t75_pick (81910 ,E , false)== 2 && t75_pick (200000 ,E , false)== 2 && t75_pick (327670 ,E , false)== 2 );// above the band: 128 (pick says 64) 700CHECK (pick (none ,81910 ,E ).ww == 4 ); 701CHECK (t75_pick (100 ,0 , false)== 2 && t75_pick (56 ,1 , false)== 4 );// no experts counts as one 702// a Q2_0 gate/up: exactly pick's band at every size 703for (int64_t n : {int64_t (0 ),int64_t (1000 ),rows_at (5.0 ),int64_t (28672 ),int64_t (28673 ),int64_t (57344 ),int64_t (57345 ),int64_t (81910 ),int64_t (327670 )}) 704CHECK (t75_pick (n ,E , true)== pick (none ,n ,E ).ww ); 705// the i-quant rule against pick: the same up to 112 rows an expert (the band's lower bound is the rule's), 128 where pick goes back to 64 706for (int64_t n = 0 ;n <=400000 ;n += 977 ) { 707const double avg = (double )n /E ; 708CHECK (t75_pick (n ,E , false)== (avg > 112.0 ?2 :pick (none ,n ,E ).ww )); 709 } 710 } 711return failed == 0 ?0 :1 ; 712}