okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
bd0e878f4
main
1// STRATA_PREFILL_TIMING mark coalescing (PR #4006): repeated marks in the running phase add nothing to the totals. 2// No GPU: the timeline is simulated, one fake clock tick per launch. 3#include <cstdio> 4#include <vector> 5 6#include "strata/prefill/timer_marks.hpp" 7 8using strata::prefill::timer_mark_repeats ; 9 10static int failed = 0 ; 11#define CHECK (x ) do { if (!(x)) { std::fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #x); ++failed; } } while (0) 12 13enum {kIdle ,kDequant ,kGemm ,kWaitCopy ,kPhases }; 14 15struct Mark {int phase ;double t ; }; 16 17// PfTimer::fold(): the gap between mark i and mark i+1 is charged to the phase of mark i. 18static std::vector < double > fold_totals (const std::vector < Mark >& marks ) { 19 std::vector < double > ms (kPhases ,0.0 ); 20for (size_t i = 0 ;i + 1 < marks .size ();++ i )ms [marks [i ].phase ]+= marks [i + 1 ].t - marks [i ].t ; 21return ms ; 22} 23 24// PfTimer::mark(): record unless the newest recorded mark is in the same phase. 25static std::vector < Mark > record (const std::vector < Mark >& calls ,size_t * skipped ) { 26 std::vector < Mark > out ; 27 std::vector < int > ph ; 28* skipped = 0 ; 29for (const Mark & m :calls ) { 30if (timer_mark_repeats (ph .data (),ph .size (),m .phase )) {++* skipped ;continue ; } 31out .push_back (m ); 32ph .push_back (m .phase ); 33 } 34return out ; 35} 36 37int main () { 38// the predicate 39const int ph []= {kGemm ,kDequant }; 40CHECK (!timer_mark_repeats (ph ,0 ,kDequant ));// nothing recorded yet: always record 41CHECK (timer_mark_repeats (ph ,2 ,kDequant ));// the newest mark is already dequant 42CHECK (!timer_mark_repeats (ph ,2 ,kGemm ));// an older mark's phase does not count, only the newest 43CHECK (!timer_mark_repeats (ph ,1 ,kDequant )); 44CHECK (timer_mark_repeats (ph ,1 ,kGemm )); 45 46// a compute() like timeline: gemm, then one dequant mark per expert (16 of them), then a copy wait, then dequant again 47 std::vector < Mark > calls ; 48double t = 0 ; 49calls .push_back ({kGemm ,t }); 50t += 5 ; 51for (int e = 0 ;e < 16 ;++ e ) {calls .push_back ({kDequant ,t });t += 0.5 + e * 0.01 ; } 52calls .push_back ({kWaitCopy ,t }); 53t += 3 ; 54for (int e = 0 ;e < 4 ;++ e ) {calls .push_back ({kDequant ,t });t += 1.0 ; } 55calls .push_back ({kIdle ,t });// the run's closing mark 56 57size_t skipped = 0 ; 58const std::vector < Mark > kept = record (calls ,& skipped ); 59const std::vector < double > full = fold_totals (calls ),coal = fold_totals (kept ); 60CHECK (skipped == 15 + 3 );// 18 records saved of 23 61CHECK (kept .size ()== 5 );// gemm, dequant, wait copy, dequant, idle 62for (int p = 0 ;p < kPhases ;++ p )CHECK (full [p ]- coal [p ]< 1e-9 && coal [p ]- full [p ]< 1e-9 );// the totals are the same 63 64// alternating phases: nothing is skipped 65 std::vector < Mark > alt ; 66for (int i = 0 ;i < 10 ;++ i )alt .push_back ({i %2 ?kGemm :kDequant , (double )i }); 67record (alt ,& skipped ); 68CHECK (skipped == 0 ); 69 70// the same phase for the whole run: one record plus the closing mark 71 std::vector < Mark > same ; 72for (int i = 0 ;i < 100 ;++ i )same .push_back ({kDequant , (double )i }); 73same .push_back ({kIdle ,100.0 }); 74const std::vector < Mark > k2 = record (same ,& skipped ); 75CHECK (skipped == 99 && k2 .size ()== 2 ); 76CHECK (fold_totals (k2 )[kDequant ]== fold_totals (same )[kDequant ]); 77 78// after a fold the timer keeps its last mark (used == 1): a repeat of that phase is still skipped 79const int kept_ph []= {kWaitCopy }; 80CHECK (timer_mark_repeats (kept_ph ,1 ,kWaitCopy )); 81 82if (failed ) { std::fprintf (stderr ,"%d check(s) failed\n" ,failed );return 1 ; } 83 std::puts ("timer_marks_test: ok" ); 84return 0 ; 85}