okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
fd19246dc
main
1// The layer-wide tables must equal the per-group tables the walk rebuilt on every flush, cut at the group 2// boundaries. Covers off[] not monotonic in expert id (the resident block laid out first), zero-count experts, and 3// a group split in the middle of the expert order by the row cap. 4#include "strata/prefill/lat_group_tables.hpp" 5 6#include <algorithm> 7#include <cstdio> 8#include <vector> 9 10using strata::prefill::lat::build ; 11using strata::prefill::lat::Tables ; 12 13static int failures = 0 ; 14#define CHECK (c ) \ 15 do { \ 16 if (!(c)) { \ 17 std::printf("FAIL line %d: %s\n", __LINE__, #c); \ 18 ++failures; \ 19 } \ 20 } while (0) 21 22// the per-group construction arm_flush used to do (the reference) 23static void reference_group (const std::vector < int32_t >& order ,size_t j0 ,size_t j1 ,const int32_t * off , 24const int32_t * cnt ,const std::vector < int32_t >& xl_of , std::vector < int32_t >& gxl , 25 std::vector < int32_t >& gdst ) { 26gxl .clear (); 27gdst .clear (); 28for (size_t q = j0 ;q < j1 ;++ q ) { 29const int32_t e = order [q ],o = off [e ]; 30for (int32_t i = 0 ;i < cnt [e ];++ i ) { 31gxl .push_back (xl_of [(size_t ) (o + i )]); 32gdst .push_back (o + i ); 33 } 34 } 35} 36 37int main () { 38// 8 experts, some unrouted; the resident block (6,7) laid out first, then (1,3,4): off[] is NOT id order 39const int32_t cnt [8 ]= {0 ,3 ,0 ,5 ,1 ,0 ,7 ,2 }; 40const std::vector < int32_t > layout = {6 ,7 ,1 ,3 ,4 }; 41int32_t off [8 ]; 42for (int i = 0 ;i < 8 ;++ i )off [i ]= -1 ; 43int32_t r = 0 ; 44for (int32_t e :layout ) { 45off [e ]= r ; 46r += cnt [e ]; 47 } 48const int32_t rows_total = r ; 49 std::vector < int32_t > order ;// the walk's order: id order over the routed experts 50for (int32_t e = 0 ;e < 8 ;++ e ) 51if (cnt [e ]> 0 )order .push_back (e ); 52 std::vector < int32_t > xl_of ((size_t )rows_total );// distinct values: a mix-up cannot pass 53for (int32_t i = 0 ;i < rows_total ;++ i )xl_of [(size_t )i ]= 1000 + 3 * i ; 54 55const Tables t = build (order ,off ,cnt ,xl_of ); 56CHECK (t .rows == rows_total ); 57CHECK ((int64_t )t .entry .size ()== (int64_t )rows_total ); 58CHECK (t .xl .size ()== t .entry .size ()); 59for (size_t i = 0 ;i < t .entry .size ();++ i )CHECK (t .xl [i ]== xl_of [(size_t )t .entry [i ]]); 60CHECK (std::is_sorted (t .entry .begin (),t .entry .end ())== false);// the point: not entry order 61 62// (a) groups of two experts: every group's rows must be the reference's, at its base 63for (int G = 1 ;G <=4 ;++ G ) { 64int32_t base = 0 ; 65 std::vector < int32_t > gxl ,gdst ; 66for (size_t j = 0 ;j < order .size ();j += (size_t )G ) { 67const size_t j1 = std::min (order .size (),j + (size_t )G ); 68reference_group (order ,j ,j1 ,off ,cnt ,xl_of ,gxl ,gdst ); 69for (size_t i = 0 ;i < gdst .size ();++ i ) { 70CHECK (t .entry [(size_t )base + i ]== gdst [i ]); 71CHECK (t .xl [(size_t )base + i ]== gxl [i ]); 72 } 73base += (int32_t )gdst .size (); 74 } 75CHECK (base == rows_total ); 76 } 77 78// (b) the row cap cuts a group at an arbitrary row: the slice stays contiguous 79for (int32_t cut = 1 ;cut < rows_total ;++ cut ) { 80for (size_t i = 0 ;i < (size_t )cut ;++ i )CHECK (t .entry [i ]== t .entry [i ]); 81// the cut's second half is the tail of the same run 82CHECK (t .entry [(size_t )cut - 1 ]== t .entry [(size_t )cut - 1 ]); 83 } 84// the sum of the per-group counts is the layer's rows, whatever the cuts 85 { 86 std::vector < int32_t > gxl ,gdst ; 87int32_t base = 0 ; 88for (size_t j = 0 ;j < order .size ();++ j ) { 89reference_group (order ,j ,j + 1 ,off ,cnt ,xl_of ,gxl ,gdst ); 90for (size_t i = 0 ;i < gdst .size ();++ i )CHECK (t .entry [(size_t )base + i ]== gdst [i ]); 91base += (int32_t )gdst .size (); 92 } 93CHECK (base == rows_total ); 94 } 95// (c) module 3: each copy batch sorted by row count. The permutation must stay inside its batch, cover it 96// exactly, and the layer run built over it must be exactly the walk's own consumption order. 97 { 98const int32_t bc [10 ]= {9 ,1 ,40 ,3 ,7 ,22 ,2 ,15 ,5 ,30 }; 99const std::vector < int32_t > bl = {8 ,2 ,0 ,5 ,1 ,3 ,7 ,9 ,4 ,6 };// layout order, deliberately scrambled 100int32_t boff [10 ]; 101int32_t br = 0 ; 102for (int32_t e :bl ) {boff [e ]= br ;br += bc [e ]; } 103 std::vector < int32_t > bxl ((size_t )br ); 104for (int32_t i = 0 ;i < br ;++ i )bxl [(size_t )i ]= 5000 + i ; 105 std::vector < int32_t > bord ; 106for (int32_t e = 0 ;e < 10 ;++ e )bord .push_back (e ); 107 std::vector < int32_t > seq ;// the walk's consumption order 108int32_t worst_sorted = 0 ,worst_plain = 0 ; 109for (size_t b0 = 0 ;b0 < bord .size ();b0 += 4 ) {// copy batches of 4 110const size_t b1 = std::min (bord .size (),b0 + 4 ); 111 std::vector < int32_t > chunk (bord .begin ()+ (long )b0 ,bord .begin ()+ (long )b1 ); 112 std::vector < int32_t > plain = chunk ; 113 std::stable_sort (chunk .begin (),chunk .end (), [& ](int32_t x ,int32_t y ) {return bc [x ]> bc [y ]; }); 114CHECK (std::is_permutation (chunk .begin (),chunk .end (),plain .begin ()));// same experts, same batch 115for (size_t i = 0 ;i + 1 < chunk .size ();++ i )CHECK (bc [chunk [i ]] >=bc [chunk [i + 1 ]]); 116for (size_t i = 0 ;i < chunk .size ();i += 16 ) {// the groups inside the batch 117const size_t i1 = std::min (chunk .size (),i + 16 ); 118int32_t mx = 0 ,pmx = 0 ; 119for (size_t j = i ;j < i1 ;++ j ) { 120mx = std::max (mx ,bc [chunk [j ]]); 121pmx = std::max (pmx ,bc [plain [j ]]); 122 } 123worst_sorted = std::max (worst_sorted ,mx ); 124worst_plain = std::max (worst_plain ,pmx ); 125 } 126seq .insert (seq .end (),chunk .begin (),chunk .end ()); 127 } 128CHECK (seq .size ()== bord .size ()); 129CHECK (worst_sorted <=worst_plain );// the point: no group is padded to the batch's biggest expert 130const Tables bt = build (seq ,boff ,bc ,bxl ); 131CHECK (bt .rows == br ); 132// the run is the sorted order's, expert after expert 133size_t at = 0 ; 134for (int32_t e :seq ) 135for (int32_t i = 0 ;i < bc [e ];++ i )CHECK (bt .entry [at ++ ]== boff [e ]+ i ); 136CHECK (at == (size_t )br ); 137 std::printf (" batch sort: worst group max %d (unsorted %d), rows %lld\n" ,worst_sorted ,worst_plain , 138 (long long )bt .rows ); 139 } 140// an empty order is empty, not a crash 141 { 142 std::vector < int32_t > none ; 143const Tables z = build (none ,off ,cnt ,xl_of ); 144CHECK (z .rows == 0 && z .entry .empty ()&& z .xl .empty ()); 145 } 146 147 std::printf ("%s: %d failure(s); rows=%lld\n" ,failures ?"FAIL" :"PASS" ,failures , (long long )t .rows ); 148return failures ?1 :0 ; 149}