okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
bd0e878f4
main
1// The MMQ row-table margin (PR #5108): every index the stream-k fixup and the main kernel can read stays inside the table. 2// No GPU: the reads are enumerated from the layout the way mul_mat_q_stream_k_fixup computes them 3// (ids_dst[col_low + jt*J + j], jt < ceil(max_rows / J), j < J, col_low = the expert's first row). 4#include <algorithm> 5#include <cstdint> 6#include <cstdio> 7#include <random> 8#include <vector> 9 10#include "strata/prefill/mmq_ids_margin.hpp" 11 12using namespace strata::prefill::mmq ; 13 14static int failed = 0 ; 15#define CHECK (x ) do { if (!(x)) { std::fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #x); ++failed; } } while (0) 16 17// the highest index (exclusive) one launch can read: bounds has n + 1 entries, max_rows the widest expert, J any tile width 18static int64_t fixup_read_end (const std::vector < int64_t >& bounds ,int64_t max_rows ,int J ) { 19const int64_t tiles = (max_rows + J - 1 ) /J ; 20int64_t end = 0 ; 21for (size_t e = 0 ;e + 1 < bounds .size ();++ e ) 22end = std::max (end ,bounds [e ]+ tiles * J );// every expert's fixup reads whole tiles from its first row 23return end ; 24} 25static int64_t main_read_end (const std::vector < int64_t >& bounds ,int J ) { 26int64_t end = 0 ; 27for (size_t e = 0 ;e + 1 < bounds .size ();++ e ) 28if (bounds [e + 1 ]> bounds [e ])end = std::max (end ,bounds [e + 1 ]+ J - 1 );// up to J - 1 rows after the expert's last 29return end ; 30} 31 32int main () { 33// the constants follow ggml's loop J = 8 .. 128 34CHECK (kMmqTileColsMax == 128 && kMmqTileColsStep == 8 ); 35 36// arithmetic: roundup(max_rows, J) + J 37int64_t m = -1 ; 38CHECK (mmq_ids_margin_rows (0 ,128 ,& m )&& m == 128 ); 39CHECK (mmq_ids_margin_rows (1 ,128 ,& m )&& m == 256 ); 40CHECK (mmq_ids_margin_rows (128 ,128 ,& m )&& m == 256 ); 41CHECK (mmq_ids_margin_rows (129 ,128 ,& m )&& m == 384 ); 42CHECK (mmq_ids_margin_rows (3072 ,128 ,& m )&& m == 3200 ); 43CHECK (mmq_ids_margin_rows (3072 ,8 ,& m )&& m == 3080 ); 44CHECK (mmq_ids_table_rows (30720 ,3072 )== 30720 + 3200 ); 45CHECK (mmq_ids_table_rows (0 ,0 )== 128 ); 46 47// checked arithmetic: nothing wraps to a small table 48m = 77 ; 49CHECK (!mmq_ids_margin_rows (-1 ,128 ,& m )&& m == 77 ); 50CHECK (!mmq_ids_margin_rows (10 ,0 ,& m )&& !mmq_ids_margin_rows (10 ,-8 ,& m )); 51CHECK (!mmq_ids_margin_rows (INT64_MAX ,128 ,& m )); 52CHECK (!mmq_ids_margin_rows (INT64_MAX - 100 ,128 ,& m )); 53CHECK (mmq_ids_table_rows (UINT64_MAX - 5 ,10 )== 0 ); 54CHECK (mmq_ids_table_rows (10 , (uint64_t )INT64_MAX + 1 )== 0 ); 55CHECK (mmq_ids_table_rows (UINT64_MAX ,0 )== 0 ); 56 57// synthetic layouts: the table is T*K rows + the margin, T tokens, K = 10 experts per token; late experts, empty tiles 58const int64_t K = 10 ; 59 std::mt19937_64 rng (5108 ); 60for (int iter = 0 ;iter < 2000 ;++ iter ) { 61const int64_t T = 1 + (int64_t ) (rng () % (iter < 1000 ?70 :4096 )); 62const int NE = 1 + (int ) (rng () %64 ); 63 std::vector < int64_t > cnt ((size_t )NE ,0 ); 64int64_t left = T * K ,widest = 0 ; 65// experts take at most one row per token; some are empty, the last ones often are 66for (int e = 0 ;e < NE && left > 0 ;++ e ) { 67const int64_t c = std::min < int64_t > ({(int64_t ) (rng () % ((uint64_t )T + 1 )),T ,left }); 68cnt [(size_t )e ]= c ; 69left -= c ; 70 } 71// whatever is left goes to experts that still have room (the rows must add up to T*K when they can) 72for (int e = 0 ;e < NE && left > 0 ;++ e ) { 73const int64_t room = T - cnt [(size_t )e ],c = std::min (room ,left ); 74cnt [(size_t )e ]+= c ; 75left -= c ; 76 } 77 std::vector < int64_t > bounds {0 }; 78int64_t total = 0 ; 79for (int64_t c :cnt ) {total += c ;bounds .push_back (total );widest = std::max (widest ,c ); } 80CHECK (widest <=T ); 81const int64_t table = (int64_t )mmq_ids_table_rows ((uint64_t )total , (uint64_t )T ); 82for (int J = kMmqTileColsStep ;J <=kMmqTileColsMax ;J += kMmqTileColsStep ) { 83// the launch uses max_rows = the widest expert here; the margin is sized for max_rows <= T 84CHECK (fixup_read_end (bounds ,widest ,J ) <=table ); 85CHECK (fixup_read_end (bounds ,T ,J ) <=table );// even a launch whose grid is sized for T 86CHECK (main_read_end (bounds ,J ) <=table ); 87 } 88 } 89 90// the exact table of before the fix does fail on the late-expert layout the PR found (the guard this test pins) 91 { 92const int64_t T = 100 ; 93 std::vector < int64_t > bounds {0 ,100 ,200 ,200 ,200 };// two real experts, then two empty late ones at row T*2 94const int64_t exact = 200 ; 95CHECK (fixup_read_end (bounds ,100 ,128 )> exact ); 96CHECK (fixup_read_end (bounds ,100 ,128 ) <= (int64_t )mmq_ids_table_rows (200 , (uint64_t )T )); 97 } 98 99if (failed ) { std::fprintf (stderr ,"%d check(s) failed\n" ,failed );return 1 ; } 100 std::puts ("mmq_ids_margin_test: ok" ); 101return 0 ; 102}