okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
9ffd6c946
main
1// src/kernels/cpu/pool_stress.cpp - issue #29: the expert pool under the load a big-VRAM card gives it. 2// 3// With most experts resident on the GPU, many layers hand the pool nothing, the workers go to sleep in the middle 4// of a request, and the next tiny batch (one or two experts) is often finished by the host and the awake workers 5// before a sleeper has even woken. That late wake-up is what the park/claim protocol has to survive. This drives 6// hundreds of thousands of such batches - 0-4 jobs, run() and run_split(), with pauses long enough for the workers 7// to sleep - and checks that every batch returns, with every job's output written. A watchdog thread turns a 8// hang into a failure instead of a frozen test. 9// 10// pool_stress [seconds] (default 20) 11#include "strata/kernels/cpu/pool.hpp" 12#include "strata/kernels/cpu/expert.hpp" 13 14#include <atomic> 15#include <chrono> 16#include <cstdio> 17#include <cstdlib> 18#include <cstring> 19#include <random> 20#include <thread> 21#include <vector> 22 23namespace cpu= strata::kernels::cpu; 24 25int main (int argc ,char ** argv ) { 26const double seconds = argc > 1 ? std::atof (argv [1 ]) :20.0 ; 27const cpu::CpuFeatures feat = cpu::cpu_features (); 28if (!feat .usable ()) { 29 std::printf ("CPU lacks %s: pool_stress SKIPPED\n" ,feat .reason ()); 30return 0 ; 31 } 32constexpr int NJ = 4 ; 33 std::vector < std::vector < uint8_t >> blobs (NJ , std::vector < uint8_t > (cpu::BLOB ,0 )); 34 std::vector < float > x (cpu::H ,0.25f ); 35 cpu::ActQ act ; 36 cpu::act_quant_q8_1 (x .data (), cpu::H ,act ); 37 std::vector < float > out ((size_t )NJ * cpu::H ); 38 std::vector < cpu::ExpertJob > jobs (NJ ); 39for (int e = 0 ;e < NJ ;++ e ) { 40jobs [(size_t )e ].blob = blobs [(size_t )e ].data (); 41jobs [(size_t )e ].act = & act ; 42jobs [(size_t )e ].out = out .data ()+ (size_t )e * cpu::H ; 43jobs [(size_t )e ].slot = e ; 44 } 45 46 cpu::ExpertPool pool ; 47 std::printf ("pool_stress: %d workers + host, %.0f s\n" ,pool .workers (),seconds ); 48 49// the watchdog: a batch that does not return within 10 s is a hang 50 std::atomic < long long > beat {0 }; 51 std::atomic < bool > finished {false}; 52 std::thread dog ([& ] { 53long long last = -1 ; 54auto since = std::chrono::steady_clock::now (); 55while (!finished .load ()) { 56 std::this_thread::sleep_for (std::chrono::milliseconds (200 )); 57const long long b = beat .load (); 58const auto now = std::chrono::steady_clock::now (); 59if (b != last ) {last = b ;since = now ;continue ; } 60if (now - since > std::chrono::seconds (10 )) { 61 std::printf ("\npool_stress: HANG - batch %lld has not returned for 10 s\n" ,b ); 62 std::fflush (stdout ); 63 std::_Exit (3 ); 64 } 65 } 66 }); 67 68 std::mt19937 rng (29 ); 69const float SENTINEL = -1.2345e33f ; 70long long batches = 0 ,sleeps = 0 ,missed = 0 ; 71const auto t0 = std::chrono::steady_clock::now (); 72while (std::chrono::steady_clock::now ()- t0 < std::chrono::duration < double > (seconds )) { 73// now and then a gap long enough for every worker to go to sleep (the pool sleeps after 20 ms) 74if (rng () %64 == 0 ) { 75 std::this_thread::sleep_for (std::chrono::milliseconds (21 + (int ) (rng () %10 ))); 76++ sleeps ; 77 }else if (rng () %4 == 0 ) { 78 std::this_thread::sleep_for (std::chrono::microseconds (rng () %300 )); 79 } 80const int n = (int ) (rng () % (NJ + 1 )); 81for (int e = 0 ;e < n ;++ e ) std::fill (jobs [(size_t )e ].out ,jobs [(size_t )e ].out + cpu::H ,SENTINEL ); 82if (rng () %2 )pool .run (jobs .data (),n ); 83else pool .run_split (jobs .data (),n ); 84for (int e = 0 ;e < n ;++ e ) 85if (jobs [(size_t )e ].out [0 ]== SENTINEL || jobs [(size_t )e ].out [cpu::H - 1 ]== SENTINEL )++ missed ; 86beat .fetch_add (1 ); 87++ batches ; 88 } 89finished .store (true); 90dog .join (); 91 std::printf ("pool_stress: %lld batches (%lld after the workers slept), %lld jobs not run\n" ,batches ,sleeps , 92missed ); 93if (missed ) { 94 std::printf ("pool_stress: FAILED\n" ); 95return 1 ; 96 } 97 std::printf ("pool_stress OK\n" ); 98return 0 ; 99}