okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
6572fbaf1
main
1// i8_expert_rule (include/strata/kernels/i8_expert_rule.hpp): when a prompt layer's expert groups take our own int8 kernels (native_expert_i8). 2// Nothing set: the HIP cards gfx1030 / gfx1031 / gfx1032 take them (groups averaging 256 entries or more, the layout and the device allowing it); every 3// other card keeps MMQ. STRATA_EXPERT_I8=1 asks for them on any card, =0 goes back to the old kernels on the class. No GPU, no model. 4#include <cstdio> 5#include <initializer_list> 6 7#include "strata/kernels/i8_expert_rule.hpp" 8 9using namespace strata::kernels ; 10 11static int failed = 0 ; 12#define CHECK (x ) do { if (!(x)) { std::fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #x); ++failed; } } while (0) 13 14int main () { 15// the state of the variable 16CHECK (i8_switch_parse (nullptr )== -1 ); 17CHECK (i8_switch_parse ("" )== -1 ); 18CHECK (i8_switch_parse ("0" )== 0 ); 19CHECK (i8_switch_parse ("1" )== 1 ); 20CHECK (i8_switch_parse ("yes" )== 1 ); 21CHECK (i8_switch_parse ("0x" )== 0 ); 22// the class: exactly the three RDNA2 targets, with or without the feature suffix hipGetDeviceProperties adds 23CHECK (gfx_arch_is_gfx103x_i8 ("gfx1030" )); 24CHECK (gfx_arch_is_gfx103x_i8 ("gfx1031" )); 25CHECK (gfx_arch_is_gfx103x_i8 ("gfx1032" )); 26CHECK (gfx_arch_is_gfx103x_i8 ("gfx1031:sramecc-:xnack-" )); 27CHECK (!gfx_arch_is_gfx103x_i8 ("gfx1034" )); 28CHECK (!gfx_arch_is_gfx103x_i8 ("gfx1035" )); 29CHECK (!gfx_arch_is_gfx103x_i8 ("gfx1036" )); 30CHECK (!gfx_arch_is_gfx103x_i8 ("gfx1100" )); 31CHECK (!gfx_arch_is_gfx103x_i8 ("gfx1151" )); 32CHECK (!gfx_arch_is_gfx103x_i8 ("gfx1201" )); 33CHECK (!gfx_arch_is_gfx103x_i8 ("gfx906" )); 34CHECK (!gfx_arch_is_gfx103x_i8 ("gfx10310" )); 35CHECK (!gfx_arch_is_gfx103x_i8 ("" )); 36CHECK (!gfx_arch_is_gfx103x_i8 (nullptr ));// a CUDA card 37// nothing set: the class takes them from 256 entries per group, smaller groups and every other card keep today's path 38CHECK (i8_expert_takes (-1 ,"gfx1031" ,256 , true)); 39CHECK (i8_expert_takes (-1 ,"gfx1030" ,4096 , true)); 40CHECK (i8_expert_takes (-1 ,"gfx1032:sramecc-:xnack-" ,300 , true)); 41CHECK (!i8_expert_takes (-1 ,"gfx1031" ,255 , true)); 42CHECK (!i8_expert_takes (-1 ,"gfx1031" ,0 , true)); 43CHECK (!i8_expert_takes (-1 ,"gfx1031" ,4096 , false));// a format or a size the kernel does not cover, or a card whose shared memory is too small 44CHECK (!i8_expert_takes (-1 ,"gfx1201" ,4096 , true));// RDNA4: MMQ / the fused kernels 45CHECK (!i8_expert_takes (-1 ,"gfx1100" ,4096 , true)); 46CHECK (!i8_expert_takes (-1 ,"gfx1034" ,4096 , true)); 47CHECK (!i8_expert_takes (-1 ,nullptr ,4096 , true));// CUDA 48CHECK (!i8_expert_takes (-1 ,"" ,4096 , true)); 49// =0: the way back, on the class and everywhere 50CHECK (!i8_expert_takes (0 ,"gfx1031" ,4096 , true)); 51CHECK (!i8_expert_takes (0 ,"gfx1030" ,100000 , true)); 52CHECK (!i8_expert_takes (0 ,nullptr ,4096 , true)); 53// =1: any card that the kernel covers, same size floor 54CHECK (i8_expert_takes (1 ,"gfx1201" ,256 , true)); 55CHECK (i8_expert_takes (1 ,nullptr ,4096 , true)); 56CHECK (i8_expert_takes (1 ,"gfx1031" ,256 , true)); 57CHECK (!i8_expert_takes (1 ,"gfx1201" ,255 , true)); 58CHECK (!i8_expert_takes (1 ,"gfx1201" ,4096 , false)); 59// which of the two reasons: the class default only when the variable is unset 60CHECK (i8_expert_class_default (-1 ,"gfx1031" )); 61CHECK (!i8_expert_class_default (1 ,"gfx1031" )); 62CHECK (!i8_expert_class_default (0 ,"gfx1031" )); 63CHECK (!i8_expert_class_default (-1 ,"gfx1201" )); 64CHECK (!i8_expert_class_default (-1 ,nullptr )); 65// a build without llama.cpp's MMQ: an unset switch reads as 1 on every card, a value the user set is kept; a build with MMQ is the table above 66CHECK (i8_switch_effective (-1 , true)== 1 ); 67CHECK (i8_switch_effective (0 , true)== 0 ); 68CHECK (i8_switch_effective (1 , true)== 1 ); 69CHECK (i8_switch_effective (-1 , false)== -1 ); 70CHECK (i8_switch_effective (0 , false)== 0 ); 71CHECK (i8_switch_effective (1 , false)== 1 ); 72 { 73// state {unset, 0, 1} x build {with MMQ, without} x card {RDNA2 gfx1030, gfx1100, Strix gfx1151, R9700 gfx1201, an NVIDIA card}: takes at 4,096 entries, the formats covered 74const char * const cards []= {"gfx1030" ,"gfx1100" ,"gfx1151" ,"gfx1201:sramecc-:xnack-" ,nullptr ,"" }; 75for (const char * gcn :cards ) { 76const bool rdna2 = gfx_arch_is_gfx103x_i8 (gcn ); 77// with MMQ: unchanged (only RDNA2 by default) 78CHECK (i8_expert_takes (i8_switch_effective (-1 , false),gcn ,4096 , true)== rdna2 ); 79CHECK (!i8_expert_takes (i8_switch_effective (0 , false),gcn ,4096 , true)); 80CHECK (i8_expert_takes (i8_switch_effective (1 , false),gcn ,4096 , true)); 81// without MMQ: on by default everywhere, =0 the way back, =1 as before 82CHECK (i8_expert_takes (i8_switch_effective (-1 , true),gcn ,4096 , true)); 83CHECK (!i8_expert_takes (i8_switch_effective (0 , true),gcn ,4096 , true)); 84CHECK (i8_expert_takes (i8_switch_effective (1 , true),gcn ,4096 , true)); 85// the size floor and the format check are the same in both builds 86CHECK (!i8_expert_takes (i8_switch_effective (-1 , true),gcn ,255 , true)); 87CHECK (!i8_expert_takes (i8_switch_effective (-1 , true),gcn ,4096 , false)); 88// which reason the engine's start line gives 89CHECK (i8_expert_class_default (-1 ,gcn , true)); 90CHECK (!i8_expert_class_default (1 ,gcn , true)); 91CHECK (!i8_expert_class_default (0 ,gcn , true)); 92CHECK (i8_expert_class_default (-1 ,gcn , false)== rdna2 ); 93CHECK (i8_expert_class_default (-1 ,gcn )== rdna2 );// (the old two-argument form) 94 } 95 } 96// the order of the paths for a layer of a chunk: the fused kernels, then the staged group entry, then int8, then FP16 (a column "fused covers this layer and is on" and one for the staged entry); 97// the same for a switch left unset in a build without MMQ and for a user-set 1: the int8 kernels get only what nothing faster takes 98for (const char * gcn : {"gfx1201" ,"gfx1151" ,"gfx1100" ,"" ,"gfx1030" }) { 99for (int raw : {-1 ,1 }) { 100CHECK (i8_expert_takes (i8_switch_effective (raw , true),gcn ,4096 , true));// the kernel itself would take the layer on every card 101CHECK (!i8_layer_takes (raw , true,gcn ,4096 , true, true, false));// fused covers the layer and is on: not int8 102CHECK (!i8_layer_takes (raw , true,gcn ,4096 , true, false, true));// a short chunk the staged group entry takes: not int8 103CHECK (!i8_layer_takes (raw , true,gcn ,4096 , true, true, true)); 104CHECK (i8_layer_takes (raw , true,gcn ,4096 , true, false, false));// what is left: int8 105CHECK (!i8_layer_takes (raw , true,gcn ,4096 , false, false, false));// a format the kernel does not cover: FP16 106 } 107CHECK (!i8_layer_takes (0 , true,gcn ,4096 , true, false, false));// the way back 108CHECK (!i8_layer_takes (-1 , false,gcn ,4096 , true, true, false));// (with MMQ: fused first as well) 109CHECK (i8_layer_takes (-1 , false,gcn ,4096 , true, false, false)== gfx_arch_is_gfx103x_i8 (gcn )); 110 } 111// the few-rows route: the limit (STRATA_I8_FEWROWS: not set = the class default, 0 = off, N = N rows per expert) 112CHECK (i8_fewrows_limit (nullptr ,10 )== 10 ); 113CHECK (i8_fewrows_limit ("" ,8 )== 8 ); 114CHECK (i8_fewrows_limit ("0" ,10 )== 0 ); 115CHECK (i8_fewrows_limit ("0x" ,10 )== 0 ); 116CHECK (i8_fewrows_limit ("12" ,10 )== 12 ); 117CHECK (i8_fewrows_limit ("6" ,8 )== 6 ); 118CHECK (i8_fewrows_limit ("yes" ,10 )== 10 ); 119CHECK (i8_fewrows_limit ("-3" ,8 )== 8 ); 120CHECK (i8_fewrows_limit ("99999999999" ,10 )> 100000 );// a huge number does not wrap to a small one 121// the class default: the P100 (6.0) 6, every other card 7 (6.1, 7.0, 7.5, 8.6, 12.0, a HIP card with no capability at all) 122CHECK (i8_fewrows_class_default (6 ,0 )== 6 ); 123CHECK (i8_fewrows_class_default (6 ,1 )== 7 ); 124CHECK (i8_fewrows_class_default (7 ,0 )== 7 ); 125CHECK (i8_fewrows_class_default (8 ,6 )== 7 ); 126CHECK (i8_fewrows_class_default (12 ,0 )== 7 ); 127CHECK (i8_fewrows_class_default (0 ,0 )== 7 ); 128// the decision: rows strictly under limit x experts go to the grouped kernels (512 experts: 5,119 rows is under 10 per expert, 5,120 is 10) 129CHECK (i8_fewrows_takes (10 ,5119 ,512 )); 130CHECK (!i8_fewrows_takes (10 ,5120 ,512 )); 131CHECK (!i8_fewrows_takes (10 ,20480 ,512 )); 132CHECK (i8_fewrows_takes (8 ,4095 ,512 )); 133CHECK (!i8_fewrows_takes (8 ,4096 ,512 )); 134CHECK (i8_fewrows_takes (10 ,2560 ,400 ));// 256 tokens x 10 over the 400 experts that got rows: 6.4 per expert 135CHECK (!i8_fewrows_takes (0 ,100 ,512 ));// the route is off 136CHECK (!i8_fewrows_takes (10 ,100 ,0 ));// no experts: nothing to decide 137CHECK (!i8_fewrows_takes (-1 ,100 ,512 )); 138// the 8-entry slices: not set (-1) = tile 2 only, 1 = tiles 2-4, 0 = none; tile 8 (the 128-entry kernel) never has them 139CHECK (i8_n8_takes (-1 ,2 )); 140CHECK (!i8_n8_takes (-1 ,3 )); 141CHECK (!i8_n8_takes (-1 ,4 )); 142CHECK (!i8_n8_takes (-1 ,8 )); 143CHECK (i8_n8_takes (1 ,2 )); 144CHECK (i8_n8_takes (1 ,3 )); 145CHECK (i8_n8_takes (1 ,4 )); 146CHECK (!i8_n8_takes (1 ,8 )); 147CHECK (!i8_n8_takes (0 ,2 )); 148CHECK (!i8_n8_takes (0 ,4 )); 149CHECK (i8_n8_takes (i8_switch_parse (nullptr ),2 )); 150CHECK (i8_n8_takes (i8_switch_parse ("" ),2 )); 151CHECK (!i8_n8_takes (i8_switch_parse ("0" ),2 )); 152CHECK (i8_n8_takes (i8_switch_parse ("1" ),4 )); 153// the P100 class rule (build without MMQ, switch not set): the int8 default applies under the stream floor (stream_all_min, 1,024 tokens by default) only; the FP16 path from it up (the build with MMQ's default there) 154 { 155const int64_t lim = 1024 ;// stream_all_min() by default: the rule is the comparison, rows at floor - 1, floor, floor + 1 156CHECK (i8_chunk_default_allows (-1 , true, true,1 ,lim )); 157CHECK (i8_chunk_default_allows (-1 , true, true,lim - 1 ,lim )); 158CHECK (!i8_chunk_default_allows (-1 , true, true,lim ,lim )); 159CHECK (!i8_chunk_default_allows (-1 , true, true,lim + 1 ,lim )); 160CHECK (i8_chunk_default_allows (-1 , true, true,300 ,lim )); 161CHECK (i8_chunk_default_allows (-1 , true, true,1000 ,lim )); 162CHECK (i8_chunk_default_allows (-1 , true, true,lim - 1 ,lim )); 163CHECK (!i8_chunk_default_allows (-1 , true, true,lim ,lim )); 164CHECK (!i8_chunk_default_allows (-1 , true, true,2048 ,lim )); 165CHECK (!i8_chunk_default_allows (-1 , true, true,8192 ,lim )); 166// a value the user set keeps its meaning at every size (the rule is not asked to veto it) 167CHECK (i8_chunk_default_allows (1 , true, true,8192 ,lim )); 168CHECK (i8_chunk_default_allows (0 , true, true,8192 ,lim )); 169// not the P100 (6.1, 7.x, 8.x, a HIP card): no limit; a build with MMQ: no limit (its P100 default is the FP16 path already) 170CHECK (i8_chunk_default_allows (-1 , true, false,8192 ,lim )); 171CHECK (i8_chunk_default_allows (-1 , false, true,8192 ,lim )); 172CHECK (i8_chunk_default_allows (-1 , false, false,8192 ,lim )); 173 } 174if (failed ) { 175 std::fprintf (stderr ,"i8_expert_rule_test: %d check(s) failed\n" ,failed ); 176return 1 ; 177 } 178 std::printf ("i8_expert_rule_test passed\n" ); 179return 0 ; 180}