okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
bd0e878f4
main
1// P0.1 (lite): the run manifest. Off by default and free when off; the same decisions give the same text whatever order 2// they were recorded in; a later record of a path replaces the earlier; the file is written whole and only when something 3// changed. No GPU. run_manifest_test [--out FILE] (FILE keeps the last document for a JSON parser to check) 4#include <cstdio> 5#include <cstdlib> 6#include <cstring> 7#include <filesystem> 8#include <fstream> 9#include <sstream> 10#include <string> 11 12#include "strata/core/run_manifest.hpp" 13 14using strata::core::manifest::Manifest ; 15 16static int failed = 0 ; 17#define CHECK (x ) do { if (!(x)) { std::fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #x); ++failed; } } while (0) 18 19static std::string slurp (const std::string & p ) { 20 std::ifstream f (p , std::ios::binary ); 21 std::stringstream s ; 22s <<f .rdbuf (); 23return s .str (); 24} 25 26static void run_a (Manifest & m ) { 27m .engine ("kind" ,"native-single" ); 28m .engine ("backend" ,"cuda" ); 29m .budget ("expert_cache_mib" ,1500 ); 30m .budget ("vram_reserve_mib" ,960 ); 31m .path ("prefill" ,"mmq" , true,"tile found for IQ3_S gate/up and IQ4_NL down" ,"layers 0-47" ); 32m .path ("prefill" ,"fused-native" , false,"MMQ covers these formats" ); 33m .path ("decode" ,"route-tail-skip" , true,"serve default" ); 34m .tensors ("dense" ,"Q8_0" ,3 ,3000 ); 35m .tensors ("dense" ,"F32" ,10 ,400 ); 36m .tensors ("dense" ,"Q8_0" ,2 ,2000 );// accumulates 37m .tensors ("head" ,"Q6_K" ,1 ,123456 ); 38} 39static void run_b (Manifest & m ) {// the same decisions, recorded in another order, with one superseded record 40m .tensors ("head" ,"Q6_K" ,1 ,123456 ); 41m .tensors ("dense" ,"F32" ,10 ,400 ); 42m .path ("decode" ,"route-tail-skip" , true,"serve default" ); 43m .path ("prefill" ,"fused-native" , true,"provisional" ); 44m .path ("prefill" ,"fused-native" , false,"MMQ covers these formats" );// replaces the line above 45m .budget ("vram_reserve_mib" ,960 ); 46m .engine ("backend" ,"cuda" ); 47m .tensors ("dense" ,"Q8_0" ,5 ,5000 ); 48m .path ("prefill" ,"mmq" , true,"tile found for IQ3_S gate/up and IQ4_NL down" ,"layers 0-47" ); 49m .budget ("expert_cache_mib" ,1500 ); 50m .engine ("kind" ,"native-single" ); 51} 52 53int main (int argc ,char ** argv ) { 54 std::string keep ; 55for (int i = 1 ;i + 1 < argc ;++ i ) 56if (!std::strcmp (argv [i ],"--out" ))keep = argv [i + 1 ]; 57const std::filesystem::path dir = std::filesystem::temp_directory_path () /"strata_manifest_test" ; 58 std::filesystem::create_directories (dir ); 59const std::string f1 = (dir /"a.json" ).string (),f2 = (dir /"b.json" ).string (); 60 std::filesystem::remove (f1 ); 61 std::filesystem::remove (f2 ); 62 63// off: nothing recorded, nothing written, no file 64 { 65Manifest off ; 66CHECK (!off .enabled ()); 67run_a (off ); 68CHECK (!off .write ("ready" )&& !off .flush_if_dirty ("ready" )); 69CHECK (off .json ("x" ).find ("\"kind\"" )== std::string::npos ); 70 } 71 72// two matched runs: the same text, in either recording order 73Manifest a (f1 ),b (f2 ); 74CHECK (a .enabled ()&& a .file ()== f1 ); 75run_a (a ); 76run_b (b ); 77CHECK (a .json ("ready" )== b .json ("ready" )); 78CHECK (a .json ("ready" )!= a .json ("done" ));// the stage is the one field that is meant to differ 79CHECK (a .write ("ready" )&& b .write ("ready" )); 80CHECK (slurp (f1 )== slurp (f2 )&& !slurp (f1 ).empty ()); 81CHECK (!std::filesystem::exists (f1 + ".tmp" ));// renamed over, no leftover 82 83// keys are sorted and the structure is there 84const std::string doc = slurp (f1 ); 85CHECK (doc .find ("\"manifest_version\": 1" )!= std::string::npos ); 86CHECK (doc .find ("\"selected\": false, \"reason\": \"MMQ covers these formats\"" )!= std::string::npos ); 87CHECK (doc .find ("\"Q8_0\": {\"count\": 5, \"bytes\": 5000}" )!= std::string::npos ); 88CHECK (doc .find ("\"backend\"" )< doc .find ("\"kind\"" )); 89CHECK (doc .find ("\"decode\"" )< doc .find ("\"prefill\"" )); 90CHECK (doc .find ("\"provisional\"" )== std::string::npos ); 91 92// dirty tracking: no rewrite when nothing new, one when something is 93CHECK (!a .flush_if_dirty ("request" )); 94a .path ("decode" ,"route-tail-skip" , true,"serve default" );// the same record again 95CHECK (!a .flush_if_dirty ("request" )); 96a .path ("decode" ,"route-tail-skip" , false,"STRATA_ROUTE_TAIL_SKIP=0" ); 97CHECK (a .flush_if_dirty ("request" )); 98CHECK (slurp (f1 ).find ("STRATA_ROUTE_TAIL_SKIP=0" )!= std::string::npos ); 99CHECK (slurp (f1 ).find ("\"stage\": \"request\"" )!= std::string::npos ); 100CHECK (!a .flush_if_dirty ("request" )); 101a .tensors ("dense" ,"F32" ,1 ,4 ); 102CHECK (a .flush_if_dirty ("request" )); 103 104// an empty manifest is a valid document, and text is escaped 105Manifest e ((dir /"e.json" ).string ()); 106CHECK (e .write ("ready" )); 107CHECK (slurp ((dir /"e.json" ).string ()).find ("\"paths\": []," )!= std::string::npos ); 108e .path ("x" ,"quote\"back\\slash" , true,"line1\nline2\ttab \x01" ); 109const std::string et = e .json ("ready" ); 110CHECK (et .find ("quote\\\"back\\\\slash" )!= std::string::npos ); 111CHECK (et .find ("line1\\nline2\\ttab \\u0001" )!= std::string::npos ); 112 113// an unwritable target does not throw, returns false, and the run goes on 114Manifest bad ((dir /"no_such_dir" /"m.json" ).string ()); 115bad .path ("x" ,"y" , true,"z" ); 116CHECK (!bad .write ("ready" )); 117 118if (!keep .empty ()) { 119 std::ofstream k (keep , std::ios::binary ); 120k <<e .json ("ready" ); 121 std::ofstream k2 (keep + ".run" , std::ios::binary ); 122k2 <<a .json ("ready" ); 123 } 124 std::filesystem::remove_all (dir ); 125if (failed ) { std::fprintf (stderr ,"%d check(s) failed\n" ,failed );return 1 ; } 126 std::puts ("run_manifest_test: ok" ); 127return 0 ; 128}