okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
762daf658
main
1// src/core/load_main.cpp - `strata-load`: the expert arena load, measurable at any scale. 2// 3// P2.S1 gives this a target (<= 60 s cold, <= 10 s warm for 33.97 GB) and a method (8 threads, 16 MB chunks, 4// a checksum per layer). The size is a FLAG rather than hard-coded because the honest way to measure a 34 GB 5// load is to measure a smaller one first and project - and because pinning 34 GB evicts every page of page 6// cache on the machine, which is not something a test should do while other work is running. 7#include "strata/core/pinned.hpp" 8 9#include <cstdio> 10#include <cstdlib> 11#include <cstring> 12#include <string> 13 14int main (int argc ,char ** argv ) { 15 std::string path ; 16uint64_t layers = 2 ; 17int threads = 8 ; 18uint64_t chunk = 16ull <<20 ; 19uint64_t blob_bytes = 1382400 ;// architecture §3.2, asserted by tools/pack_layer.py 20uint64_t blobs_per_layer = 512 ; 21bool pin = true; 22bool stream = false; 23 24for (int i = 1 ;i < argc ;++ i ) { 25const std::string a = argv [i ]; 26auto val = [& ]()-> const char * { 27if (i + 1 >=argc ) { std::fprintf (stderr ,"%s needs a value\n" ,a .c_str ()); std::exit (2 ); } 28return argv [++ i ]; 29 }; 30if (a == "--file" )path = val (); 31else if (a == "--layers" )layers = std::strtoull (val (),nullptr ,10 ); 32else if (a == "--threads" )threads = std::atoi (val ()); 33else if (a == "--chunk-mb" )chunk = std::strtoull (val (),nullptr ,10 ) <<20 ; 34else if (a == "--no-pin" )pin = false; 35else if (a == "--stream" )stream = true; 36else if (a == "--help" || a == "-h" ) { 37 std::printf ("usage: strata-load --file experts.bin [--layers N] [--threads N] [--chunk-mb N]\n" 38" [--no-pin]\n" ); 39return 0 ; 40 }else { 41 std::fprintf (stderr ,"unknown argument: %s\n" ,a .c_str ()); 42return 2 ; 43 } 44 } 45if (path .empty ()) { std::fprintf (stderr ,"--file is required\n" );return 2 ; } 46 47const uint64_t bytes = layers * blobs_per_layer * blob_bytes ; 48 std::printf ("expert arena: %llu layers x %llu blobs x %llu B = %.3f GiB\n" , (unsigned long long )layers , 49 (unsigned long long )blobs_per_layer , (unsigned long long )blob_bytes , 50 (double )bytes / (1024.0 * 1024 * 1024 )); 51 52 strata::core::PinnedArena arena ; 53uint8_t * dst = nullptr ; 54if (pin ) { 55// constructed in place so the note from the constructor can be printed before the load 56new (& arena ) strata::core::PinnedArena (bytes ); 57 std::printf ("backing: %s\n" ,arena .note .c_str ()); 58if (!arena .valid ()) { std::fprintf (stderr ,"arena allocation failed\n" );return 1 ; } 59dst = arena .data (); 60 }else { 61dst = (uint8_t * ) std::malloc ((size_t )bytes ); 62if (!dst ) { std::fprintf (stderr ,"malloc failed\n" );return 1 ; } 63 std::printf ("backing: malloc, NOT pinned (--no-pin)\n" ); 64 } 65 66const strata::core::LoadStats st = 67 strata::core::load_experts (path ,dst ,blob_bytes ,blobs_per_layer ,layers ,threads ,chunk ); 68if (!st .ok ) { 69 std::fprintf (stderr ,"loading the experts failed: %s\n" ,st .error .empty () ?"unknown" :st .error .c_str ()); 70return 1 ; 71 } 72 73 std::printf ("loaded %.3f GiB in %.3f s with %d threads and %llu MiB chunks -> %.2f GiB/s\n" , 74 (double )st .bytes / (1024.0 * 1024 * 1024 ),st .seconds ,threads , 75 (unsigned long long ) (chunk >>20 ),st .gib_per_second ()); 76 std::printf ("per-layer checksums (FNV-1a 64):\n" ); 77for (size_t i = 0 ;i < st .layer_checksums .size ()&& i < 4 ;++ i ) { 78 std::printf (" layer %zu %016llx\n" ,i , (unsigned long long )st .layer_checksums [i ]); 79 } 80if (st .layer_checksums .size ()> 4 ) { 81 std::printf (" ... %zu more (all layers loaded)\n" ,st .layer_checksums .size ()- 4 ); 82 } 83if (stream ) { 84// The bus is the ceiling, so measure it at the granularity the engine actually uses. Printed as a 85// sweep rather than one number because "bandwidth" without a transfer size is not a specification. 86 std::printf ("\nexpert stream over the pinned arena (host -> device, one reused destination):\n" ); 87const uint64_t granularities []= {blob_bytes ,16 * blob_bytes ,512 * blob_bytes ,bytes }; 88const char * names []= {"1 blob (1.38 MB)" ,"16 blobs (22 MB)" ,"1 layer (707 MB)" ,"whole arena" }; 89for (int i = 0 ;i < 4 ;++ i ) { 90const uint64_t g = granularities [i ]; 91if (g > bytes )continue ; 92const int iters = (int ) (g >=bytes ?3 : (bytes /g >=8 ?8 :3 )); 93const strata::core::StreamStats ss = 94 strata::core::stream_bandwidth (dst , (bytes /g )* g ,g ,iters ); 95if (ss .seconds < 0 )continue ; 96 std::printf (" %-18s chunk %8.2f MB %6.2f GB/s (%.3f GiB in %.3f s)\n" ,names [i ], 97 (double )g /1e6 ,ss .gib_per_second ()* 1.073741824 , 98 (double )ss .bytes / (1024.0 * 1024 * 1024 ),ss .seconds ); 99 } 100// And what it means, using the design's own constants, stated with the assumption attached. 101const double best = 60.0 ;// GB/s, replaced below by the measured best if it is larger 102 (void )best ; 103 std::printf ("\nper-token expert stream at E = 663.6 MB and hit rate h (VRAM-resident fraction):\n" ); 104 std::printf (" %-8s %-14s %-14s %s\n" ,"h" ,"miss MB/token" ,"ms at 1 blob" ,"tok/s ceiling" ); 105const strata::core::StreamStats one = strata::core::stream_bandwidth (dst ,blob_bytes ,blob_bytes ,64 ); 106for (double h : {0.0 ,0.5 ,0.833 ,0.9 ,0.95 }) { 107const double miss_mb = (1.0 - h )* 663.6 ; 108const double ms = miss_mb /1000.0 / (one .gib_per_second ()* 1.073741824 )* 1000.0 ; 109 std::printf (" %-8.3f %-14.1f %-14.2f %.1f\n" ,h ,miss_mb ,ms ,ms > 0 ?1000.0 /ms :0.0 ); 110 } 111 std::printf (" h = 0.167 is the design's constant; h = 0.833 is its complement, shown because the\n" 112" plan's E = 663.6 MB is the FULL expert set per token, so which fraction is cached is\n" 113" the question the number hinges on.\n" ); 114 } 115 116// The projection is the number the phase's <= 60 s target is about, and it is printed as a projection 117// rather than as the result, because at --layers 2 this program has NOT loaded 33.97 GB. 118const double full = 33973862400.0 ; 119 std::printf ("projection for the full %.2f GiB at this rate: %.1f s\n" ,full / (1024.0 * 1024 * 1024 ), 120full / (1024.0 * 1024 * 1024 ) / (st .gib_per_second ()> 0 ?st .gib_per_second () :1e-9 )); 121return 0 ; 122}