okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
6ca60e9a0
main
1// The conversation copy streams and events are pooled across threads (#3350, #3470): short-lived threads (the layer 2// split's later stages run each prompt chunk on a fresh std::async thread) must not leave a stream and an event behind 3// each. Synthetic 4 KiB copies, no model; needs a CUDA device (skips, exit 77, without one). 4#include "conversation_copy.hpp" 5 6#include <atomic> 7#include <cstdio> 8#include <cstdlib> 9#include <thread> 10#include <vector> 11 12using namespace strata::core::conversation_detail ; 13 14namespace { 15int checks = 0 ; 16void check (bool ok ,const char * label ) { 17++ checks ; 18if (!ok ) { std::fprintf (stderr ,"FAIL: %s\n" ,label ); std::exit (1 ); } 19} 20 21// one thread's work on device `dev`: a queued copy, the ordering event, a synchronous copy 22bool one_thread (int dev ,void * d ,void * h ,size_t bytes ) { 23if (cudaSetDevice (dev )!= cudaSuccess )return false; 24if (copy_nonblocking (d ,h ,bytes , true)!= cudaSuccess )return false; 25if (order_after_queued ()!= cudaSuccess )return false; 26if (copy_nonblocking (h ,d ,bytes , false)!= cudaSuccess )return false; 27return cudaDeviceSynchronize ()== cudaSuccess ; 28} 29}// namespace 30 31int main () { 32int n_dev = 0 ; 33if (cudaGetDeviceCount (& n_dev )!= cudaSuccess || n_dev < 1 ) { 34 (void )cudaGetLastError (); 35 std::printf ("conversation_copy_streams_test: no CUDA device, skipped\n" ); 36return 77 ; 37 } 38constexpr size_t bytes = 4096 ; 39void * d = nullptr ; 40check (cudaMalloc (& d ,bytes )== cudaSuccess ,"device buffer" ); 41 std::vector < char > h (bytes ,7 ); 42 43// 0. #2409: what the first copy stream and event of a card cost, on a context that is warm (the buffer above is 44// allocated, and a first copy has loaded the copy path's code on a thread that is then discarded) 45 { 46bool fresh_ready = true; 47 std::thread probe ([& ] {fresh_ready = copy_stream_ready (0 ); }); 48probe .join (); 49check (!fresh_ready ,"before any copy no thread has a stream for the card (copy_stream_ready is false)" ); 50cudaDeviceSynchronize (); 51size_t free0 = 0 ,free1 = 0 ,tot = 0 ; 52check (cudaMemGetInfo (& free0 ,& tot )== cudaSuccess ,"free memory before the first stream" ); 53bool ok = false,ready_after = false; 54 std::thread t ([& ] { 55ok = one_thread (0 ,d ,h .data (),bytes ); 56ready_after = copy_stream_ready (0 ); 57 }); 58t .join (); 59check (ok && ready_after ,"the first copy creates the stream, and the thread then has it" ); 60check (cudaMemGetInfo (& free1 ,& tot )== cudaSuccess ,"free memory after the first stream" ); 61const long long used = (long long )free0 - (long long )free1 ; 62 std::printf ("conversation_copy_streams_test: the first copy stream + event of the card took %lld KiB of device memory\n" , 63used /1024 ); 64// the restore's guard (program/conv_cache.hpp kRestoreFirstUseMib = 64) is 4x this bound 65check (used <= (16ll <<20 ),"a card's first copy stream and event take under 16 MiB" ); 66bool pooled = false; 67 std::thread t2 ([& ] {pooled = copy_stream_ready (0 ); }); 68t2 .join (); 69check (pooled ,"a finished thread's stream is in the pool: the next thread needs no new one" ); 70 } 71 72// 1. 200 threads, one after the other (a chunk per std::async thread): the handles are reused, not created per thread 73const long s0 = copy_streams_created (),e0 = copy_events_created (); 74for (int i = 0 ;i < 200 ;++ i ) { 75bool ok = false; 76 std::thread t ([& ] {ok = one_thread (0 ,d ,h .data (),bytes ); }); 77t .join (); 78check (ok ,"a short-lived thread's copies succeed" ); 79 } 80check (copy_streams_created ()- s0 <=1 ,"200 sequential threads create at most one stream" ); 81check (copy_events_created ()- e0 <=1 ,"200 sequential threads create at most one event" ); 82 83// 2. threads at the same time each have their own (the number is bounded by the concurrency) 84const long s1 = copy_streams_created (); 85 std::atomic < int > fails {0 }; 86for (int round = 0 ;round < 10 ;++ round ) { 87 std::vector < std::thread > ts ; 88for (int k = 0 ;k < 6 ;++ k ) 89ts .emplace_back ([& ] {for (int j = 0 ;j < 5 ;++ j )if (!one_thread (0 ,d ,h .data (),bytes ))++ fails ; }); 90for (auto & t :ts )t .join (); 91 } 92check (fails .load ()== 0 ,"concurrent threads' copies succeed" ); 93check (copy_streams_created ()- s1 <=6 ,"ten rounds of 6 concurrent threads create at most 6 more streams" ); 94 95// 3. a second device has its own 96if (n_dev > 1 ) { 97const long s2 = copy_streams_created (); 98for (int i = 0 ;i < 50 ;++ i ) { 99bool ok = false; 100 std::thread t ([& ] { 101ok = cudaSetDevice (1 )== cudaSuccess ; 102void * d1 = nullptr ; 103ok = ok && cudaMalloc (& d1 ,bytes )== cudaSuccess && one_thread (1 ,d1 ,h .data (),bytes ); 104if (d1 )cudaFree (d1 ); 105 }); 106t .join (); 107check (ok ,"device 1: a short-lived thread's copies succeed" ); 108 } 109check (copy_streams_created ()- s2 <=1 ,"device 1: 50 sequential threads create at most one stream" ); 110 } 111cudaFree (d ); 112 std::printf ("conversation_copy_streams_test: %d checks passed (%ld streams, %ld events created in all)\n" ,checks , 113copy_streams_created (),copy_events_created ()); 114return 0 ; 115}