okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
7f46672a4
main
1// metal/probe/window_probe.cpp - the Metal decode window on its own: Window::init and a few windows of one token, without the engine around it. 2// 3// window_probe <model-metal.gguf> [tokens N] [--ple <model.gguf>] (built with the same -DSTRATA_GEOMETRY_FILE as the model) 4// --requests F --logits-out G the gate-2 requests without the engine: every line of F is a token list (comma separated); each is read as a prompt 5// (the prompt chain from 32 tokens, windows of 8 below, as the engine reads it), its last token runs as a head window, and the logits of 6// that position (the vocabulary's floats) are appended to G. The judge (g3kl_mac.py --from-dump G) compares them with llama.cpp. 7// --session N carve the engine's session state for N cells of context (session_init, as generate.cpp does) before the window comes up 8// --ple F open F's PLE table as the engine does (direct reads, the I/O thread) and run the PLE block: the engine's first window 9// 10// A session with no PLE and no QSA state is enough for the window (the PLE block is skipped, the K/V live in the window's own buffers), so this is 11// the engine's process state - the model mapped and wrapped, every library compiled, every buffer allocated, the first pipelines created - for 12// a Metal fault that has to be looked at without an engine start. Not a correctness test: the tokens are whatever the model makes of them. 13#include "strata/core/session.hpp" 14#include "strata/core/geometry.hpp" 15#include "strata/core/layer.hpp" 16#include "strata/kernels/ngram.hpp" 17#include <cuda_runtime.h> 18 19#include <chrono> 20#include <fstream> 21#include <functional> 22#include <sstream> 23#include <cstdio> 24#include <cstdlib> 25#include <string> 26#include <vector> 27 28namespace strata::metal_engine { 29void set_model_shard (const char * shard1 ); 30void * window_create (); 31bool window_init (void * w ,const strata::core::ModelGeometry & g , strata::core::SessionState & ss ,int max_t ,int64_t max_cells ,int64_t * n_vocab , std::string & err ); 32bool window_run (void * w ,int T ,const int32_t * tokens ,int64_t pos0 ,bool head ,int32_t * out , std::string & err ); 33bool window_commit (void * w ,int n_keep , std::string & err ); 34bool window_copy_logits (void * w ,int t ,float * host ); 35void window_reset (const strata::core::SessionState * ss ); 36bool prompt_run (const int64_t * tokens ,int64_t n ,int64_t pos0 ,const float * const * embd_rows , 37const std::function < bool (const float * ,int64_t ,int64_t , std::string & )>& on_chunk , std::string & err ); 38}// namespace strata::metal_engine 39 40int main (int argc ,char ** argv ) { 41if (argc < 2 ) { std::fprintf (stderr ,"usage: window_probe <model-metal.gguf> [tokens N]\n" );return 2 ; } 42int n = argc > 2 && argv [2 ][0 ] >='0' && argv [2 ][0 ] <='9' ? std::atoi (argv [2 ]) :4 ; 43setenv ("STRATA_METAL_STREAM" ,"0" ,1 ); 44 strata::metal_engine::set_model_shard (argv [1 ]); 45 strata::core::SessionState ss ; 46 std::string err ; 47 strata::kernels::PleTable table ; 48static float dummy_f [16 ]= {}; 49static uint16_t dummy_h [16 ]= {}; 50static int32_t dummy_i [4 ]= {-1 ,-1 ,-1 ,-1 }; 51for (int i = 2 ;i + 1 < argc ;++ i ) 52if (std::string (argv [i ])== "--ple" ) { 53 strata::kernels::PleIoOptions pio ; 54if (!table .open (argv [i + 1 ],err ,pio )) { std::printf ("PLE table: %s\n" ,err .c_str ());return 5 ; } 55ss .ple .table = & table ;// (the window reads its weights from the model file; the rest of ready() is pointers it does not use) 56ss .ple .consts = strata::kernels::ple_artifact_consts (); 57ss .ple .hist = dummy_f ;ss .ple .emb_dev = dummy_f ;ss .ple .emb_host = dummy_f ;ss .ple .scratch = dummy_f ; 58ss .ple .token = dummy_i ;ss .ple .prev = dummy_i ; 59ss .ple .w .key_bf16 = dummy_h ;ss .ple .w .value_bf16 = dummy_h ;ss .ple .w .norm_key = dummy_f ;ss .ple .w .norm_query = dummy_f ; 60ss .ple .w .norm_conv = dummy_f ;ss .ple .w .conv1d_f16 = dummy_h ; 61 std::printf ("window_probe: PLE table %s open, %llu rows\n" ,argv [i + 1 ], (unsigned long long )table .rows ()); 62 } 63for (int i = 2 ;i + 1 < argc ;++ i ) 64if (std::string (argv [i ])== "--session" ) { 65const int64_t cells = std::atoll (argv [i + 1 ]); 66const int64_t K = strata::core::kCompiledGeometry .n_expert_used ; 67void * sbuf = nullptr ; 68const uint64_t bytes = strata::core::session_bytes (strata::core::kCompiledGeometry ,cells ,K ,0 ,-1 ); 69if (cudaMalloc (& sbuf ,bytes )!= cudaSuccess ) { std::printf ("session allocation of %.1f GB failed\n" ,bytes /1e9 );return 9 ; } 70if (strata::core::session_init (strata::core::kCompiledGeometry ,cells ,K ,sbuf ,ss ,0 ,-1 )== 0 ) { std::printf ("session_init failed\n" );return 9 ; } 71 std::printf ("window_probe: session state for %lld cells carved (%.2f GB)\n" , (long long )cells ,bytes /1e9 ); 72 } 73int64_t nv = 0 ; 74void * w = strata::metal_engine::window_create (); 75const auto t0 = std::chrono::steady_clock::now (); 76if (!strata::metal_engine::window_init (w , strata::core::kCompiledGeometry ,ss ,8 ,4096 ,& nv ,err )) { std::printf ("init failed: %s\n" ,err .c_str ());return 3 ; } 77 std::printf ("window_probe: init ok, vocabulary %lld, %.0f ms\n" , (long long )nv , std::chrono::duration < double , std::milli > (std::chrono::steady_clock::now ()- t0 ).count ()); 78 std::vector < int32_t > toks = {1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 }; 79int32_t out [8 ]= {}; 80for (int i = 0 ;i < n ;++ i ) { 81const auto t1 = std::chrono::steady_clock::now (); 82if (!strata::metal_engine::window_run (w ,1 ,& toks [(size_t )i %8 ],i , true,out ,err )|| !strata::metal_engine::window_commit (w ,1 ,err )) { 83 std::printf ("window %d failed: %s\n" ,i ,err .c_str ()); 84return 4 ; 85 } 86 std::printf ("window %d: token %d, %.1f ms\n" ,i ,out [0 ], std::chrono::duration < double , std::milli > (std::chrono::steady_clock::now ()- t1 ).count ()); 87 } 88 std::string reqf ,outf ; 89for (int i = 2 ;i + 1 < argc ;++ i ) { 90if (std::string (argv [i ])== "--requests" )reqf = argv [i + 1 ]; 91if (std::string (argv [i ])== "--logits-out" )outf = argv [i + 1 ]; 92 } 93if (!reqf .empty ()&& !outf .empty ()) { 94 std::ifstream in (reqf ); 95 std::ofstream outl (outf , std::ios::binary | std::ios::trunc ); 96 std::vector < float > lg ((size_t )nv ); 97 std::string line ; 98int nreq = 0 ; 99const auto t2 = std::chrono::steady_clock::now (); 100while (std::getline (in ,line )) { 101 std::vector < int64_t > ids ; 102 std::stringstream ss2 (line ); 103 std::string tok ; 104while (std::getline (ss2 ,tok ,',' ))if (!tok .empty ())ids .push_back (std::atoll (tok .c_str ())); 105if (ids .size ()< 2 )continue ; 106 strata::metal_engine::window_reset (& ss ); 107ss .ple_prev [0 ]= ss .ple_prev [1 ]= -1 ; 108const int64_t npre = (int64_t )ids .size ()- 1 ; 109if (!strata::metal_engine::prompt_run (ids .data (),npre ,0 ,nullptr ,nullptr ,err )) { std::printf ("request %d: prompt failed: %s\n" ,nreq ,err .c_str ());return 6 ; } 110int32_t last = (int32_t )ids .back (),o = 0 ; 111if (!strata::metal_engine::window_run (w ,1 ,& last ,npre , true,& o ,err )) { std::printf ("request %d: window failed: %s\n" ,nreq ,err .c_str ());return 7 ; } 112if (!strata::metal_engine::window_copy_logits (w ,0 ,lg .data ())) { std::printf ("request %d: no logits\n" ,nreq );return 8 ; } 113outl .write ((const char * )lg .data (), (std::streamsize )lg .size ()* 4 ); 114++ nreq ; 115 } 116 std::printf ("window_probe: %d requests, logits in %s, %.1f s\n" ,nreq ,outf .c_str (), std::chrono::duration < double > (std::chrono::steady_clock::now ()- t2 ).count ()); 117return 0 ; 118 } 119 std::printf ("window_probe: ok\n" ); 120return 0 ; 121}