okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
7f46672a4
main
1// metal/tests/msl_dump.cpp - compile every kernel library the engine compiles (engine/msl_libs.h), without starting an engine. 2// 3// msl_dump [--kq] [--f16] [--engine-msl DIR] STRATA_METAL_MSL_DUMP=<dir> msl_dump writes the text of each library 4// 5// The engine builds its libraries from the same lists (msl_libs.h), so this is "the MSL text the engine compiles": with the dump 6// directory set, mtl_rt.mm writes each library's full string (prelude, <metal_stdlib>, ggml-common, the kernel files) and an index; 7// tools/msl_text_check.py compares two dumps. It also proves that the libraries compile at the geometry this binary was built for 8// (-DSTRATA_GEOMETRY / -DSTRATA_GEOMETRY_FILE): a compile error ends the run with the compiler's message. 9// --kq the prelude of a model with K-quant / IQ down experts (kq_down_prelude) 10// --f16 STRATA_METAL_F16 11// --wrap F map and wrap the model file F as the engine does before it compiles (a process holding a model of that size) 12// --touch read every page of the wrapped file (the process holds the model resident) 13// --map F map a second file too (the PLE file of the engine) 14// --pipes also build the pipeline of a few kernels of the core library (xprep, moe_route, moe_combine_sh2, gr_norm2): the GPU compiler, not only the front end 15#include "gguf_lite.h" 16#include "mtl_rt.h" 17#include "engine/msl_libs.h" 18 19#include <algorithm> 20#include <chrono> 21#include <cstdio> 22#include <cstring> 23#include <string> 24#include <sys/mman.h> 25 26int main (int argc ,char ** argv ) { 27bool kq = false,f16 = false,pipes = false; 28 std::string edir = "../src/engine/msl" ,wrapf ,mapf ; 29bool touch = false; 30for (int i = 1 ;i < argc ;++ i ) { 31if (!std::strcmp (argv [i ],"--kq" ))kq = true; 32else if (!std::strcmp (argv [i ],"--f16" ))f16 = true; 33else if (!std::strcmp (argv [i ],"--pipes" ))pipes = true; 34else if (!std::strcmp (argv [i ],"--wrap" )&& i + 1 < argc )wrapf = argv [++ i ]; 35else if (!std::strcmp (argv [i ],"--map" )&& i + 1 < argc )mapf = argv [++ i ]; 36else if (!std::strcmp (argv [i ],"--touch" ))touch = true; 37else if (!std::strcmp (argv [i ],"--engine-msl" )&& i + 1 < argc )edir = argv [++ i ]; 38 } 39using namespace strata_metal ; 40const std::string kqp = kq ?"#define STRATA_KQ_DOWN 1\n" :"" ; 41const std::pair < const char * ,MslLib > libs []= { 42 {"core" ,lib_core (f16 ,kqp )}, {"gdn" ,lib_gdn ()}, 43 {"qsa" ,lib_qsa ()}, {"sample" ,lib_sample ()}, 44 {"ple" ,lib_ple (edir )}, {"prompt" ,lib_prompt (kqp )}, 45 {"prompt_gdn" ,lib_prompt_gdn ()}, {"prompt_qsa" ,lib_prompt_qsa ()}, 46 {"mtp" ,lib_mtp (edir )}, {"mtp_chain" ,lib_mtp_chain (edir )}, 47 {"dflash2" ,lib_dflash2 (edir )}, {"coop_probe" ,lib_coop_probe ()}, 48 }; 49 gguf::File gf ,gf2 ; 50 mtl::Buffer W ; 51 std::string err ; 52// (the files are mapped before the device exists, as Window::init does) 53if (!wrapf .empty ()&& !gf .open (wrapf ,err )) { std::printf ("cannot open %s: %s\n" ,wrapf .c_str (),err .c_str ());return 2 ; } 54if (!mapf .empty ()&& !gf2 .open (mapf ,err )) { std::printf ("cannot open %s: %s\n" ,mapf .c_str (),err .c_str ());return 2 ; } 55 mtl::Context cx ; 56if (!wrapf .empty ()) {// as Window::init: the file wrapped (chunks overlapping by the largest tensor when it is bigger than one buffer) 57size_t span = 0 ; 58for (const auto & kv :gf .tensors ) 59if (kv .first != "per_layer_token_embd.weight" )span = std::max < size_t > (span ,kv .second .bytes ); 60W = cx .wrap (gf .map ,gf .map_bytes ,span ); 61madvise (gf .map ,gf .map_bytes ,MADV_WILLNEED );// (as Window::init) 62if (touch ) {// read every page: the process then holds the model resident 63unsigned sink = 0 ; 64for (size_t o = 0 ;o < gf .map_bytes ;o += 16384 )sink += ((volatile unsigned char * )gf .map )[o ]; 65 std::printf ("read every page of the file (%u)\n" ,sink ); 66 } 67 std::printf ("wrapped %s: %.1f GB, handle %s\n" ,wrapf .c_str (),gf .file_bytes /1e9 ,W .handle ?"ok" :"NULL" ); 68 } 69int bad = 0 ,core_id = -1 ; 70for (const auto & [name ,L ] :libs ) { 71const auto t0 = std::chrono::steady_clock::now (); 72 std::string why ; 73const int id = cx .compile_try (L .files ,L .prelude , false,& why ); 74const double ms = std::chrono::duration < double , std::milli > (std::chrono::steady_clock::now ()- t0 ).count (); 75if (id < 0 ) { 76 std::printf ("%-11s FAIL (%.0f ms): %s\n" ,name ,ms ,why .c_str ()); 77++ bad ; 78 }else { 79 std::printf ("%-11s ok (%.0f ms)\n" ,name ,ms ); 80if (std::string (name )== "core" )core_id = id ; 81 } 82if (pipes && std::string (name )== "ple" && core_id >=0 ) {// (as the engine: the five decode-window libraries compiled, then the first pipeline) 83for (const char * fn : {"xprep" ,"moe_route" ,"moe_combine_sh2" ,"gr_norm2" }) { 84const auto t1 = std::chrono::steady_clock::now (); 85 mtl::Pipeline pl = cx .pipeline (core_id ,fn , mtl::Constants ()); 86 std::printf (" pipeline %-16s ok (%.0f ms, max threads %d)\n" ,fn , 87 std::chrono::duration < double , std::milli > (std::chrono::steady_clock::now ()- t1 ).count (),pl .max_threads ); 88 } 89 } 90 } 91 std::printf ("%s\n" ,bad ?"msl_dump: FAILED" :"msl_dump: all libraries compile" ); 92return bad ?1 :0 ; 93}