okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
8ac8ce32e
main
1// Strata. `strata-dsv41 --pack-info`: the pack as a description, before anything is loaded onto a GPU - the type map 2// (layer x role), the bytes an expert and a token, the tier sizes, and the tensors whose type has no device kernel (they 3// would go to the CPU lane, or stop the load). Host only, no device, no weights read: tensors.txt and ds4.txt are the 4// pack's whole description (tools/dsv41/pack_gguf.py). The same report is the first line of every benchmark log. 5#include "ds4_packinfo.hpp" 6#include "ds4_iq.cuh" // iq::supported / iq::name: the device-type list below is derived from the kernels, not copied 7 8#include <algorithm> 9#include <cstdio> 10#include <cstdlib> 11#include <cstring> 12#include <fstream> 13#include <map> 14#include <set> 15#include <string> 16#include <vector> 17 18#if defined(_MSC_VER ) 19#include <cstring> 20#define strtok_r strtok_s 21#endif 22namespace strata::ds4 { 23namespace { 24 25// The ggml block types the device kernels take (ds4_iq.cu's Ty<> / DS4_IQ_SWITCH + ds4_gguf.cu's MXFP4). A name here 26// and not in the pack's tensor list is fine; a tensor whose type is not here is the CPU lane's (ggml-cpu's row dots). 27// It was a copy of the supported() switch and fell behind when IQ1_S, IQ1_M and IQ4_NL got kernels (2026-10-23), so it is 28// now the switch itself, by type name. 29 30bool device_type (const std::string & t ) { 31static const std::set < std::string > ok = [] { 32 std::set < std::string > s = {"MXFP4" ,"TQK6" ,"TQK7" ,"TQK5X" ,"TQK4X" ,"TQK5T" ,"TQK4T" };// ds4_gguf.cu / trellis 33for (int ty = 0 ;ty < 64 ;++ ty ) { 34const char * n = gguf::iq::name (ty ); 35if (n && std::strcmp (n ,"?" )!= 0 && gguf::iq::supported (ty ))s .insert (n ); 36 } 37return s ; 38 }(); 39return ok .count (t )!= 0 ; 40} 41// The types the dense path reads as they are (ds4_gguf.cu: BF16 / F32 / MXFP8 rows, Q8_0 rows; ds4_iq's GEMV for the 42// K-quants is the same device support as the experts'). 43bool dense_type (const std::string & t ) { 44return t == "BF16" || t == "F32" || t == "F16" || t == "MXFP8" || t == "Q8_0" || device_type (t ); 45} 46 47struct Tensor { 48 std::string dtype ; 49double bytes = 0 ; 50 std::vector < long long > shape ; 51}; 52 53double gb (double b ) {return b /1e9 ; } 54 55}// namespace 56 57int pack_info (const std::string & dir , std::FILE * out ) { 58 std::map < std::string , std::string > kv ; 59 { 60 std::ifstream f (dir + "/ds4.txt" ); 61if (!f ) { 62 std::fprintf (out ,"pack-info: %s/ds4.txt: not found\n" ,dir .c_str ()); 63return 1 ; 64 } 65 std::string line ; 66while (std::getline (f ,line )) { 67const size_t sp = line .find (' ' ); 68if (sp != std::string::npos )kv [line .substr (0 ,sp )]= line .substr (sp + 1 ); 69 } 70 } 71auto num = [& ](const char * k ,long long d ) { 72auto it = kv .find (k ); 73return it == kv .end () ?d : std::atoll (it -> second .c_str ()); 74 }; 75const long long nl = num ("num_hidden_layers" ,0 ),ne = num ("n_routed_experts" ,0 ),topk = num ("num_experts_per_tok" ,0 ); 76const long long dim = num ("hidden_size" ,0 ),ff = num ("moe_intermediate_size" ,0 ),vocab = num ("vocab_size" ,0 ); 77 std::map < std::string ,Tensor > t ; 78 std::vector < std::string > files ; 79 std::map < int ,double > need ;// bytes the pack needs from each file (its largest tensor end) 80 { 81 std::FILE * f = std::fopen ((dir + "/tensors.txt" ).c_str (),"r" ); 82if (f == nullptr ) { 83 std::fprintf (out ,"pack-info: %s/tensors.txt: not found\n" ,dir .c_str ()); 84return 1 ; 85 } 86 std::vector < char > buf (1 <<16 ); 87while (std::fgets (buf .data (), (int )buf .size (),f )!= nullptr ) { 88char * s = buf .data (); 89if (s [0 ]== 'f' ) {// file <i> <name> 90char name [4096 ]; 91int i = 0 ; 92if (std::sscanf (s ,"file %d %4095s" ,& i ,name )== 2 ) { 93if ((int )files .size () <=i )files .resize ((size_t )i + 1 ); 94files [(size_t )i ]= name ; 95 } 96continue ; 97 } 98if (s [0 ]!= 't' )continue ; 99char * save = nullptr ; 100 ::strtok_r (s ," \n" ,& save ); 101const char * name = ::strtok_r (nullptr ," \n" ,& save ); 102const char * dt = ::strtok_r (nullptr ," \n" ,& save ); 103const char * fi = ::strtok_r (nullptr ," \n" ,& save ); 104const char * off = ::strtok_r (nullptr ," \n" ,& save ); 105const char * nb = ::strtok_r (nullptr ," \n" ,& save ); 106if (name == nullptr || dt == nullptr || fi == nullptr || off == nullptr || nb == nullptr )continue ; 107Tensor x ; 108x .dtype = dt ; 109x .bytes = (double ) std::strtoull (nb ,nullptr ,10 ); 110const int idx = std::atoi (fi ); 111need [idx ]= std::max (need [idx ], (double ) std::strtoull (off ,nullptr ,10 )+ x .bytes ); 112while (const char * d = ::strtok_r (nullptr ," \n" ,& save ))x .shape .push_back (std::atoll (d )); 113t [name ]= std::move (x ); 114 } 115 std::fclose (f ); 116 } 117if (t .empty ()) { 118 std::fprintf (out ,"pack-info: %s/tensors.txt: no tensors\n" ,dir .c_str ()); 119return 1 ; 120 } 121// the files the pack points at: they must be there, and at least as long as the largest tensor end says (the weights 122// live elsewhere; a pack whose model_dir was cleaned up still describes itself perfectly - this is the check) 123const std::string md = kv .count ("model_dir" ) ?kv ["model_dir" ] :"" ; 124int missing = 0 ; 125for (const auto & [i ,n ] :need ) { 126const std::string name = (size_t )i < files .size () ?files [(size_t )i ] :"" ; 127if (name .empty ()) { 128 std::fprintf (out ," file %d: no name in tensors.txt\n" ,i ); 129++ missing ; 130continue ; 131 } 132const std::string path = md + "/" + name ; 133 std::FILE * f = std::fopen (path .c_str (),"rb" ); 134if (f == nullptr ) { 135 std::fprintf (out ," MISSING %s (%.2f GB wanted)\n" ,path .c_str (),n /1e9 ); 136++ missing ; 137continue ; 138 } 139#if defined(_WIN32 ) 140_fseeki64 (f ,0 ,SEEK_END );// (long is 32 bits there: the weights are far past 2 GB) 141const long long sz = _ftelli64 (f ); 142#else 143 std::fseek (f ,0 ,SEEK_END ); 144const long long sz = std::ftell (f ); 145#endif 146 std::fclose (f ); 147if ((double )sz + 0.5 < n ) { 148 std::fprintf (out ," SHORT %s: %.2f GB there, %.2f GB wanted\n" ,path .c_str (), (double )sz /1e9 ,n /1e9 ); 149++ missing ; 150 } 151 } 152 153// ---- the expert map: layer -> role types and bytes an expert 154struct Role { 155 std::string type [3 ]; 156double bytes [3 ]= {0 ,0 ,0 }; 157bool mixed = false; 158int n = 0 ; 159 }; 160 std::vector < Role > lay ((size_t )nl ); 161 std::map < std::string ,long long > type_count ;// tensor count a type (the experts' 3 roles) 162double experts_bytes = 0 ; 163 std::set < std::string > no_kernel ; 164for (long long il = 0 ;il < nl ;++ il ) { 165Role & r = lay [(size_t )il ]; 166const std::string pre = "layers." + std::to_string (il )+ ".ffn.experts." ; 167for (long long e = 0 ;e < ne ;++ e ) { 168const char * role [3 ]= {"w1" ,"w3" ,"w2" }; 169for (int k = 0 ;k < 3 ;++ k ) { 170auto it = t .find (pre + std::to_string (e )+ "." + role [k ]+ ".weight" ); 171if (it == t .end ()) { 172 std::fprintf (out ,"pack-info: %s%s%lld.%s.weight: missing\n" ,pre .c_str (),"" ,e ,role [k ]); 173return 1 ; 174 } 175if (e == 0 ) { 176r .type [k ]= it -> second .dtype ; 177r .bytes [k ]= it -> second .bytes ; 178 }else if (it -> second .dtype != r .type [k ]|| it -> second .bytes != r .bytes [k ]) { 179r .mixed = true; 180 } 181if (e == 0 ) { 182++ type_count [it -> second .dtype ]; 183if (!device_type (it -> second .dtype ))no_kernel .insert ("layer " + std::to_string (il )+ " " + role [k ]+ " " + it -> second .dtype ); 184 } 185 } 186 } 187r .n = (int )ne ; 188experts_bytes += (double )ne * (r .bytes [0 ]+ r .bytes [1 ]+ r .bytes [2 ]); 189 } 190const double per_expert = nl > 0 ?experts_bytes / ((double )nl * (double )ne ) :0 ; 191// (a token picks `topk` experts a layer: sum over the layers of topk x one expert of that layer) 192double token_bytes = 0 ; 193for (long long il = 0 ;il < nl ;++ il )token_bytes += (double )topk * (lay [(size_t )il ].bytes [0 ]+ lay [(size_t )il ].bytes [1 ]+ lay [(size_t )il ].bytes [2 ]); 194const double expert_params = (double )nl * (double )ne * 3.0 * (double )ff * (double )dim ; 195 196// ---- the dense side: everything that is not an expert tensor 197double dense = 0 ,dense_embed = 0 ,dense_head = 0 ,dense_mtp = 0 ,dense_vision = 0 ,dense_engram = 0 ; 198 std::set < std::string > dense_types ; 199for (const auto & [name ,x ] :t ) { 200if (name .find (".ffn.experts." )!= std::string::npos && name .rfind ("layers." ,0 )== 0 )continue ; 201dense += x .bytes ; 202dense_types .insert (x .dtype ); 203if (name == "embed.weight" )dense_embed += x .bytes ; 204else if (name == "head.weight" )dense_head += x .bytes ; 205else if (name .rfind ("mtp." ,0 )== 0 )dense_mtp += x .bytes ; 206else if (name .find (".engram." )!= std::string::npos )dense_engram += x .bytes ; 207else if (name .rfind ("vision." ,0 )== 0 || name .rfind ("aligner." ,0 )== 0 )dense_vision += x .bytes ; 208if (!dense_type (x .dtype ))no_kernel .insert ("dense " + name + " " + x .dtype ); 209 } 210 211// ---- report 212 std::fprintf (out ,"pack-info %s\n" ,dir .c_str ()); 213 std::fprintf (out ," model_dir %s format %s %lld layers x %lld experts, %lld used; dim %lld, ff %lld, vocab %lld\n" , 214kv .count ("model_dir" ) ?kv ["model_dir" ].c_str () :"?" ,kv .count ("format" ) ?kv ["format" ].c_str () :"?" ,nl ,ne ,topk , 215dim ,ff ,vocab ); 216 std::fprintf (out ," layer gate+up down bytes/expert MB/token\n" ); 217for (long long il = 0 ;il < nl ;++ il ) { 218const Role & r = lay [(size_t )il ]; 219const double eb = r .bytes [0 ]+ r .bytes [1 ]+ r .bytes [2 ]; 220 std::fprintf (out ," %5lld %-9s %-9s %13.0f %10.2f%s\n" ,il ,r .type [0 ].c_str (),r .type [2 ].c_str (),eb , 221 (double )topk * eb /1e6 ,r .mixed ?" (mixed)" :"" ); 222 } 223 std::fprintf (out ," expert tensors by type:" ); 224for (const auto & [ty ,n ] :type_count ) std::fprintf (out ," %s %lld" ,ty .c_str (),n ); 225 std::fprintf (out ,"\n" ); 226 std::fprintf (out ," an expert %.2f MB, %lld picks a token: %.3f GB a token\n" ,per_expert /1e6 ,topk ,token_bytes /1e9 ); 227 std::fprintf (out ," all experts %.1f GB at %.2f bpw (%.0f G parameters), dense %.2f GB (embed %.2f, head %.2f, mtp %.2f, vision %.2f, engram %.2f)\n" , 228gb (experts_bytes ),expert_params > 0 ?experts_bytes * 8.0 /expert_params :0.0 ,expert_params /1e9 ,gb (dense ), 229gb (dense_embed ),gb (dense_head ),gb (dense_mtp ),gb (dense_vision ),gb (dense_engram )); 230 std::fprintf (out ," dense types:" ); 231for (const auto & d :dense_types ) std::fprintf (out ," %s" ,d .c_str ()); 232 std::fprintf (out ,"\n" ); 233// the geometry the engine needs at run time but that is easy to lose in a pack built by another tool: DSpark's block 234// size / noise token / markov rank (0 there means it drafts one token a run, i.e. it is off) and the Engram's layers 235 { 236auto kvn = [& ](const char * k ) { 237auto it = kv .find (k ); 238return it == kv .end () ?0 : std::atoll (it -> second .c_str ()); 239 }; 240const int n_mtp = (int ) std::count_if (t .begin (),t .end (), [](const auto & p ) {return p .first .rfind ("mtp." ,0 )== 0 ; }); 241const long long nl_eg = num ("engram_layer_ids" ,-1 ); 242 std::fprintf (out ," dspark: block_size %lld, noise_token_id %lld, markov_rank %lld, routed experts %lld of %lld%s\n" , 243num ("dspark_block_size" ,0 ),num ("dspark_noise_token_id" ,0 ),num ("dspark_markov_rank" ,0 ), 244num ("dspark_n_routed_experts" ,0 ),num ("dspark_num_experts_per_tok" ,0 ), 245num ("dspark_block_size" ,0 )== 0 ?" <- DSpark cannot draft (block size 0)" :"" ); 246 std::fprintf (out ," mtp tensors: %d%s\n" ,n_mtp ,n_mtp == 0 ?" <- no DSpark drafter in the pack" :"" ); 247auto it = kv .find ("engram_layer_ids" ); 248 std::fprintf (out ," engram layers: %s%s\n" ,it == kv .end () ?"(none)" :it -> second .c_str (), 249it == kv .end () ?" <- the Engram tables are not used" :"" ); 250 } 251if (no_kernel .empty ()) { 252 std::fprintf (out ," no GPU kernel: none (every tensor is a device type)\n" ); 253 }else { 254 std::fprintf (out ," no GPU kernel (%zu): the CPU lane takes them, they cannot go in the VRAM tier\n" ,no_kernel .size ()); 255int shown = 0 ; 256for (const std::string & s :no_kernel ) { 257 std::fprintf (out ," %s\n" ,s .c_str ()); 258if (++ shown >=40 ) { 259 std::fprintf (out ," ... %zu more\n" ,no_kernel .size ()- (size_t )shown ); 260break ; 261 } 262 } 263 } 264if (missing > 0 ) { 265 std::fprintf (out ," %d of %zu files missing or short: this pack cannot load (build it again, or point model_dir at " 266"the weights)\n" ,missing ,need .size ()); 267return 2 ; 268 } 269 std::fprintf (out ," files: %zu, all present and long enough\n" ,need .size ()); 270return 0 ; 271} 272 273}// namespace strata::ds4