okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
331828134
main
1// own_only_line (include/strata/prefill/own_only_line.hpp): the start-up sentence of an engine built without llama.cpp's MMQ prompt kernels, from the 2// layer counts per path. No GPU, no model. 3#include <cstdio> 4#include <string> 5 6#include "strata/prefill/own_only_line.hpp" 7#include "strata/prefill/plain_floor_rule.hpp" 8 9using strata::prefill::OwnOnlySplit ; 10using strata::prefill::own_only_line ; 11 12static int failed = 0 ; 13#define CHECK_LINE (got ,want ) do { const std::string g_ = (got); if (g_ != (want)) { std::fprintf(stderr, "FAIL %s:%d\n got: %s\n want: %s\n", __FILE__, __LINE__, g_.c_str(), (want)); ++failed; } } while (0) 14 15int main () { 16// Strix-like: every layer on the fused kernels for big chunks, on the staged fused entry for small ones 17 { 18OwnOnlySplit big ;big .fused = 48 ; 19OwnOnlySplit small ;small .staged = 48 ; 20CHECK_LINE (own_only_line (48 ,1024 ,big ,small , false), 21"strata: prompt kernels: chunks of 1024 tokens or more run 48 of 48 layers on the fused kernels; shorter chunks run 48 on the staged fused entry." ); 22 } 23// an IQ pack no fused kernel covers: every layer on the int8 kernels at every size -> one clause 24 { 25OwnOnlySplit s ;s .i8 = 48 ; 26CHECK_LINE (own_only_line (48 ,1024 ,s ,s , false),"strata: prompt kernels: every chunk runs 48 of 48 layers on the int8 kernels." ); 27 } 28// mixed: 20 fused / 28 int8 at the floor and above, the staged entry for the 20 below it 29 { 30OwnOnlySplit big ;big .fused = 20 ;big .i8 = 28 ; 31OwnOnlySplit small ;small .staged = 20 ;small .i8 = 28 ; 32CHECK_LINE (own_only_line (48 ,1024 ,big ,small , false), 33"strata: prompt kernels: chunks of 1024 tokens or more run 20 of 48 layers on the fused kernels and 28 on the int8 kernels; " 34"shorter chunks run 20 on the staged fused entry and 28 on the int8 kernels." ); 35// the same card without the staged entry: the fused layers of a short chunk take the int8 kernels 36OwnOnlySplit small2 ;small2 .i8 = 48 ; 37CHECK_LINE (own_only_line (48 ,1024 ,big ,small2 , false), 38"strata: prompt kernels: chunks of 1024 tokens or more run 20 of 48 layers on the fused kernels and 28 on the int8 kernels; " 39"shorter chunks run 48 on the int8 kernels." ); 40 } 41// FP16 above 0: a further part 42 { 43OwnOnlySplit big ;big .fused = 20 ;big .i8 = 25 ;big .fp16 = 3 ; 44OwnOnlySplit small ;small .staged = 20 ;small .i8 = 25 ;small .fp16 = 3 ; 45CHECK_LINE (own_only_line (48 ,1024 ,big ,small , false), 46"strata: prompt kernels: chunks of 1024 tokens or more run 20 of 48 layers on the fused kernels, 25 on the int8 kernels and 3 on the FP16 path; " 47"shorter chunks run 20 on the staged fused entry, 25 on the int8 kernels and 3 on the FP16 path." ); 48OwnOnlySplit all ;all .fp16 = 48 ; 49CHECK_LINE (own_only_line (48 ,1024 ,all ,all , false),"strata: prompt kernels: every chunk runs 48 of 48 layers on the FP16 path." ); 50 } 51// --peer-device: the clause, the counts as they then are 52 { 53OwnOnlySplit s ;s .i8 = 40 ;s .fp16 = 8 ; 54CHECK_LINE (own_only_line (48 ,1024 ,s ,s , true), 55"strata: prompt kernels: every chunk runs 40 of 48 layers on the int8 kernels and 8 on the FP16 path (--peer-device: the fused kernels are off)." ); 56 } 57// peer_keeps_mmq: only an engine with MMQ keeps the walk on MMQ for --peer-device; without MMQ the share is declined and the walk keeps its own kernels 58 { 59 static_assert(!strata::prefill::peer_keeps_mmq (false, false),"no flag, with MMQ: the walk's own kernels" ); 60 static_assert(!strata::prefill::peer_keeps_mmq (false, true),"no flag, without MMQ: the walk's own kernels" ); 61 static_assert(strata::prefill::peer_keeps_mmq (true, false),"--peer-device with MMQ: MMQ only" ); 62 static_assert(!strata::prefill::peer_keeps_mmq (true, true),"--peer-device without MMQ: the walk's own kernels (the share is declined)" ); 63// the start-up line of a build without MMQ with --peer-device on: the counts without the flag and no clause 64OwnOnlySplit big ;big .fused = 48 ; 65OwnOnlySplit small ;small .staged = 48 ; 66CHECK_LINE (own_only_line (48 ,1024 ,big ,small , strata::prefill::peer_keeps_mmq (true, true)), 67"strata: prompt kernels: chunks of 1024 tokens or more run 48 of 48 layers on the fused kernels; shorter chunks run 48 on the staged fused entry." ); 68#if defined(STRATA_PF_OWN_ONLY )// the test built as an engine without MMQ is (prefill.cpp kPfOwnOnlyBuild): the same answers from this build's flag 69constexpr bool own_only_build = true; 70#else 71constexpr bool own_only_build = false; 72#endif 73const std::string with_flag = own_only_line (48 ,1024 ,big ,small , strata::prefill::peer_keeps_mmq (true,own_only_build )); 74const bool has_clause = with_flag .find ("(--peer-device: the fused kernels are off)" )!= std::string::npos ; 75if (has_clause == own_only_build ) { std::fprintf (stderr ,"FAIL %s:%d: --peer-device clause %s in this build (own-only %d)\n" ,__FILE__ ,__LINE__ ,has_clause ?"present" :"missing" , (int )own_only_build );++ failed ; } 76 } 77// the Tesla P100 in a build without MMQ (no fused kernels; the int8 default under the chunk limit, the FP16 path from it): the stream floor is the limit: FP16 from it up 78 { 79OwnOnlySplit big ;big .fp16 = 48 ; 80OwnOnlySplit small ;small .i8 = 48 ; 81CHECK_LINE (own_only_line (48 ,1024 ,big ,small , false), 82"strata: prompt kernels: chunks of 1024 tokens or more run 48 of 48 layers on the FP16 path; shorter chunks run 48 on the int8 kernels." ); 83 } 84// the counts of own_only_count_layer: 48 Hadamard-folded fused-only layers (Gyro-S): the staged entry and the int8 kernels read no folded layer, so below the floor they are on the FP16 path 85 { 86OwnOnlySplit big ,small ; 87for (int l = 0 ;l < 48 ;++ l ) strata::prefill::own_only_count_layer (big ,small , true, false, false, true, false); 88CHECK_LINE (own_only_line (48 ,1024 ,big ,small , false), 89"strata: prompt kernels: chunks of 1024 tokens or more run 48 of 48 layers on the fused kernels; shorter chunks run 48 on the FP16 path." ); 90 } 91// the same layers unfolded: the staged entry takes them (the line as before the change) 92 { 93OwnOnlySplit big ,small ; 94for (int l = 0 ;l < 48 ;++ l ) strata::prefill::own_only_count_layer (big ,small , true, false, false, true, true); 95CHECK_LINE (own_only_line (48 ,1024 ,big ,small , false), 96"strata: prompt kernels: chunks of 1024 tokens or more run 48 of 48 layers on the fused kernels; shorter chunks run 48 on the staged fused entry." ); 97 } 98// a mix: 40 folded and 8 unfolded fused-only layers; and an int8 layer and a gated class keep their counts 99 { 100OwnOnlySplit big ,small ; 101for (int l = 0 ;l < 40 ;++ l ) strata::prefill::own_only_count_layer (big ,small , true, false, false, true, false); 102for (int l = 0 ;l < 8 ;++ l ) strata::prefill::own_only_count_layer (big ,small , true, false, false, true, true); 103CHECK_LINE (own_only_line (48 ,1024 ,big ,small , false), 104"strata: prompt kernels: chunks of 1024 tokens or more run 48 of 48 layers on the fused kernels; shorter chunks run 8 on the staged fused entry and 40 on the FP16 path." ); 105OwnOnlySplit b2 ,s2 ; 106 strata::prefill::own_only_count_layer (b2 ,s2 , false, true, true, true, true);// an int8 layer on a gated class: FP16 in the long class, int8 below 107if (!(b2 .fp16 == 1 && s2 .i8 == 1 && b2 .i8 == 0 )) { std::fprintf (stderr ,"FAIL %s:%d: gated int8 layer\n" ,__FILE__ ,__LINE__ );++ failed ; } 108 } 109// The printed floor is the one the rule returns once the share is armed and the card has answered (plain_floor_rule.hpp floor_with_share), not the value at the 110// time the plan is made. Plain Q2_0 pack in a build without MMQ: 48 layers on the fused kernels at the floor and above, 48 on the FP16 path below it. 111 { 112using strata::prefill::floor_with_share ; 113using strata::prefill::OwnOnlyLineDeferred ; 114OwnOnlySplit big ;big .fused = 48 ; 115OwnOnlySplit small ;small .fp16 = 48 ; 116const char * line_1536 = "strata: prompt kernels: chunks of 1536 tokens or more run 48 of 48 layers on the fused kernels; shorter chunks run 48 on the FP16 path." ; 117const char * line_3072 = "strata: prompt kernels: chunks of 3072 tokens or more run 48 of 48 layers on the fused kernels; shorter chunks run 48 on the FP16 path." ; 118const char * line_64 = "strata: prompt kernels: chunks of 64 tokens or more run 48 of 48 layers on the fused kernels; shorter chunks run 48 on the FP16 path." ; 119// the plan is made first (the old line printed 64 here): nothing is due until the share is armed 120OwnOnlyLineDeferred d ; 121d .plan (48 ,big ,small , false); 122if (d .due ()) { std::fprintf (stderr ,"FAIL %s:%d: due before the share is armed\n" ,__FILE__ ,__LINE__ );++ failed ; } 123d .mark_ready (); 124if (!d .due ()) { std::fprintf (stderr ,"FAIL %s:%d: not due after the share is armed\n" ,__FILE__ ,__LINE__ );++ failed ; } 125// a discrete CUDA card, share armed: the plain pack's floor 1,536 126CHECK_LINE (d .take (floor_with_share (true,3072 ,64 )),line_1536 ); 127if (d .due ()) { std::fprintf (stderr ,"FAIL %s:%d: printed twice\n" ,__FILE__ ,__LINE__ );++ failed ; } 128// the same plan with the rule switched off (STRATA_PF_PLAIN_FLOOR=0) or on an integrated card: the share's limit when armed, else the pack's 64 129OwnOnlyLineDeferred e ; 130e .plan (48 ,big ,small , false); 131e .mark_ready (); 132CHECK_LINE (e .take (floor_with_share (false,3072 ,64 )),line_3072 ); 133OwnOnlyLineDeferred f ; 134f .mark_ready ();// armed before the plan was made: the plan prints at once 135if (f .due ()) { std::fprintf (stderr ,"FAIL %s:%d: due without counts\n" ,__FILE__ ,__LINE__ );++ failed ; } 136f .plan (48 ,big ,small , false); 137if (!f .due ()) { std::fprintf (stderr ,"FAIL %s:%d: a plan made after the share was armed is not due\n" ,__FILE__ ,__LINE__ );++ failed ; } 138CHECK_LINE (f .take (floor_with_share (false,0 ,64 )),line_64 ); 139 } 140// The "prompt pieces" line (default_stream_floor() caches the pack's own 64): the same wait, and the floor in force when it prints, not the 64 it was cached with. 141 { 142using strata::prefill::floor_with_share ; 143using strata::prefill::OwnOnlyLineDeferred ; 144const char * tail = " tokens or more read on the fused kernels, as long prompts do. STRATA_PF_PLAIN_FLOOR=0 keeps the earlier rule; STRATA_PREFILL_STREAM_MIN=N sets the number by hand." ; 145const std::string p1536 = std::string ("prefill: prompt pieces of 1536" )+ tail ; 146const std::string p3072 = std::string ("prefill: prompt pieces of 3072" )+ tail ; 147const std::string p64 = std::string ("prefill: prompt pieces of 64" )+ tail ; 148OwnOnlyLineDeferred d ; 149d .want_pieces (true); 150if (d .pieces_due ()|| d .due ()) { std::fprintf (stderr ,"FAIL %s:%d: pieces line due before the share is armed\n" ,__FILE__ ,__LINE__ );++ failed ; } 151d .mark_ready (); 152if (!d .pieces_due ()|| d .due ()) { std::fprintf (stderr ,"FAIL %s:%d: pieces line not due (or the plan line due without counts)\n" ,__FILE__ ,__LINE__ );++ failed ; } 153CHECK_LINE (d .take_pieces (floor_with_share (true,3072 ,64 )),p1536 .c_str ());// discrete card, plain pack, share armed 154if (d .pieces_due ()) { std::fprintf (stderr ,"FAIL %s:%d: pieces line printed twice\n" ,__FILE__ ,__LINE__ );++ failed ; } 155CHECK_LINE (strata::prefill::stream_floor_pieces_line (floor_with_share (false,3072 ,64 ), true),p3072 .c_str ());// rule off / integrated, share armed 156CHECK_LINE (strata::prefill::stream_floor_pieces_line (floor_with_share (false,0 ,64 ), true),p64 .c_str ());// rule off / integrated, no share: as before 157// a trellis pack's line does not name STRATA_PF_PLAIN_FLOOR, which never moves its floor 158CHECK_LINE (strata::prefill::stream_floor_pieces_line (1536 , false), 159"prefill: prompt pieces of 1536 tokens or more read on the fused kernels, as long prompts do. STRATA_PREFILL_STREAM_MIN=N sets the number by hand." ); 160OwnOnlyLineDeferred t ; 161t .want_pieces (false); 162t .mark_ready (); 163CHECK_LINE (t .take_pieces (1536 ),"prefill: prompt pieces of 1536 tokens or more read on the fused kernels, as long prompts do. STRATA_PREFILL_STREAM_MIN=N sets the number by hand." ); 164 } 165if (failed ) { 166 std::fprintf (stderr ,"own_only_line_test: %d check(s) failed\n" ,failed ); 167return 1 ; 168 } 169 std::printf ("own_only_line_test: OK\n" ); 170return 0 ; 171}