okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
638533e3f
main
1// tests/core/native_dense_price_test.cpp - the layer split's pricing of a later stage's projections 2// (NativeDense::weight_bytes_for) against what NativeDense::load allocates for the same layers. 3// 4// With STRATA_HC_Q8=1 `load` also copies the GGUF's Q8_0 hyper-connection rows of its layers (and the final mixer's) to the 5// card; the search priced a stage without them (0.32 GiB of 2.2 GiB on the 48-layer UD-Q4_K_XL file, a 421 MiB WARNING on 6// every start of a two-card `--layer-split auto`). The test builds a synthetic GGUF with two layers of projections, their 7// Q8_0 hyper-connection rows and the final mixer, and checks for each stage range that the price equals what `load` took 8// (every matrix is under 1 MiB here, so both are exact payload sums) and that a stage's price counts its own layers' rows only. 9// Needs a CUDA device for the arena (exits 77 without one). 10#include "gguf_fixture.hpp" 11 12#include "strata/core/native_dense.hpp" 13#include "strata/core/weights.hpp" 14 15#include <cuda_runtime.h> 16 17#include <chrono> 18#include <cstdio> 19#include <cstdlib> 20#include <filesystem> 21#include <fstream> 22#include <set> 23#include <string> 24#include <vector> 25 26namespace fs= std::filesystem; 27using strata::core::NativeDense ; 28using strata::core::WeightTable ; 29 30namespace { 31int g_fail = 0 ; 32void check (bool ok ,const std::string & what ) { 33 std::printf (" %-96s %s\n" ,what .c_str (),ok ?"ok" :"FAIL" ); 34if (!ok )++ g_fail ; 35} 36 37struct TempDir { 38 fs::path path ; 39TempDir () { 40path = fs::temp_directory_path () / 41 ("strata-dense-price-test-" + std::to_string (std::chrono::steady_clock::now ().time_since_epoch ().count ())); 42 fs::create_directories (path ); 43 } 44 ~TempDir () { 45 std::error_code ignored ; 46 fs::remove_all (path ,ignored ); 47 } 48}; 49 50constexpr uint64_t NE0 = 256 ,NE1 = 4 ; 51constexpr uint64_t Q8_BYTES = NE0 /32 * 34 * NE1 ;// 1088 B: a Q8_0 matrix of 256 x 4 52 53std::vector < fixture::Kv > arch_keys () { 54return {fixture::str ("general.architecture" ,"qwen4exp" ), fixture::u32 ("qwen4exp.block_count" ,48 ), 55 fixture::u32 ("qwen4exp.embedding_length" ,2560 ), fixture::u32 ("qwen4exp.expert_count" ,512 ), 56 fixture::u32 ("qwen4exp.expert_used_count" ,10 ), fixture::u32 ("qwen4exp.attention.head_count" ,24 ), 57 fixture::u32 ("qwen4exp.attention.head_count_kv" ,2 )}; 58} 59 60const std::vector < std::string >& names () { 61static const std::vector < std::string > n = { 62"blk.0.attn_qkv.weight" ,"blk.1.attn_qkv.weight" ,"blk.0.hc_attn_down.weight" ,"blk.0.hc_attn_up.weight" , 63"blk.0.hc_ffn_down.weight" ,"blk.1.hc_attn_down.weight" ,"blk.1.hc_attn_up.weight" ,"output_hc_down.weight" , 64"blk.1.ple_key.weight" }; 65return n ; 66} 67 68// the projections and the PLE key are shape-only quantized rows (what iq_pack writes for a tensor the GGUF serves): no bytes in 69// the pool. The hyper-connection rows are real BF16 rows (kind 4, 256 x 4 x 2 = 2,048 B each), as in a real pack: on a layer 70// split generate.cpp does not skip them (add_hc_skip runs on one card only), and WeightTable::load refuses a shape-only row 71// that is not in its skip set. 72constexpr uint64_t HC_ROW_BYTES = NE0 * NE1 * 2 ; 73bool is_hc_row (const std::string & n ) {return n .find ("hc_" )!= std::string::npos ; } 74 75void write_pack (const fs::path & dir ) { 76 fs::create_directories (dir ); 77uint64_t hc_rows = 0 ; 78for (const auto & n :names ())hc_rows += is_hc_row (n ) ?1 :0 ; 79 std::string idx = "# align 256 pool " + std::to_string (hc_rows * HC_ROW_BYTES )+ " tensors " + std::to_string (names ().size ())+ "\n" ; 80uint64_t at = 0 ; 81for (const auto & n :names ()) { 82if (is_hc_row (n )) { 83const std::string off = std::to_string (at ),len = std::to_string (HC_ROW_BYTES ); 84idx += n + " 0 4 " + off + " " + len + " " + off + " " + len + " 256 4 0 0 0 0 0 0 0 0 0 0\n" ; 85at += HC_ROW_BYTES ; 86 }else { 87idx += n + " 0 0 0 0 0 0 256 4 8 0 32 0 0 0 0 0 0 0\n" ; 88 } 89 } 90 std::ofstream (dir /"index.txt" , std::ios::binary ) <<idx ; 91 std::vector < char > bytes ((size_t ) (hc_rows * HC_ROW_BYTES ?hc_rows * HC_ROW_BYTES :16 ),0 ); 92 std::ofstream (dir /"dense.bin" , std::ios::binary ).write (bytes .data (), (std::streamsize )bytes .size ()); 93} 94 95struct Stage { 96bool ok = false; 97 std::string err ; 98uint64_t priced = 0 ,loaded = 0 ,weights = 0 ; 99}; 100 101// generate.cpp's order for a later stage: the price from the headers and the table, then the load of its layers 102Stage run (const fs::path & pack ,const std::string & shard ,int64_t lb ,int64_t le ) { 103Stage s ; 104const std::vector < std::string > shards {shard }; 105 std::set < std::string > skip ; 106if (!NativeDense ::served_names (shards , true,skip ,s .err ))return s ; 107uint64_t pool = 0 ; 108if (!WeightTable ::pool_bytes (pack .string (),pool ,s .err ,& skip ))return s ; 109void * arena = nullptr ; 110if (cudaMalloc (& arena ,pool ?pool :256 )!= cudaSuccess ) {s .err = "cudaMalloc" ;return s ; } 111WeightTable wt ; 112NativeDense dense ; 113if (wt .load (pack .string (),arena ,pool ?pool :256 ,s .err ,& skip )&& 114NativeDense ::weight_bytes_for (shards ,wt , true,lb ,le ,s .priced ,s .err )) { 115NativeDense ::set_layer_range ((int )lb , (int )le ); 116s .ok = dense .load (shards ,wt ,s .err , true); 117s .loaded = dense .allocated_bytes (); 118s .weights = dense .weight_bytes (); 119 } 120dense .release (); 121NativeDense ::set_layer_range (-1 ,-1 ); 122cudaFree (arena ); 123return s ; 124} 125}// namespace 126 127int main () { 128int devices = 0 ; 129if (cudaGetDeviceCount (& devices )!= cudaSuccess || devices == 0 ) { 130 std::printf ("native_dense_price_test: no CUDA device, skipped\n" ); 131return 77 ; 132 } 133#if defined(_WIN32 ) 134_putenv_s ("STRATA_HC_Q8" ,"1" ); 135#else 136setenv ("STRATA_HC_Q8" ,"1" ,1 ); 137#endif 138TempDir tmp ; 139const fs::path pack = tmp .path /"pack" ; 140write_pack (pack ); 141const std::string shard = (tmp .path /"dense.gguf" ).string (); 142 std::vector < fixture::Tensor > tensors ; 143uint8_t seed = 1 ; 144for (const auto & n :names ())tensors .push_back ({n , {NE0 ,NE1 },8 ,seed ++ }); 145 fixture::write (shard ,arch_keys (),tensors ); 146 147 std::printf ("1. STRATA_HC_Q8=1: the price of a stage's projections is what load allocates\n" ); 148// qkv of the layer + the layer's hc rows + the final mixer and the PLE key (every stage loads both) 149const Stage s0 = run (pack ,shard ,0 ,1 );// blk.0: qkv + 3 hc rows; output_hc_down 150const Stage s1 = run (pack ,shard ,1 ,2 );// blk.1: qkv + 2 hc rows; output_hc_down 151const Stage s01 = run (pack ,shard ,0 ,2 ); 152check (s0 .ok && s1 .ok && s01 .ok ,"the three loads work " + s0 .err + s1 .err + s01 .err ); 153check (s0 .priced == (1 + 3 + 1 + 1 )* Q8_BYTES ,"layers [0, 1): qkv + three hc rows + output_hc_down + the PLE key (blk.1: another stage's layer) priced" ); 154check (s1 .priced == (1 + 2 + 1 + 1 )* Q8_BYTES ,"layers [1, 2): qkv + two hc rows + output_hc_down + the PLE key priced" ); 155check (s01 .priced == (2 + 5 + 1 + 1 )* Q8_BYTES ,"layers [0, 2): every row priced once" ); 156check (s0 .priced == s0 .loaded ,"layers [0, 1): priced == allocated by load (" + std::to_string (s0 .priced )+ " / " + std::to_string (s0 .loaded )+ ")" ); 157check (s1 .priced == s1 .loaded ,"layers [1, 2): priced == allocated by load (" + std::to_string (s1 .priced )+ " / " + std::to_string (s1 .loaded )+ ")" ); 158check (s01 .priced == s01 .loaded ,"layers [0, 2): priced == allocated by load (" + std::to_string (s01 .priced )+ " / " + std::to_string (s01 .loaded )+ ")" ); 159check (s0 .weights == s0 .loaded && s1 .weights == s1 .loaded ,"weight_bytes() counts the hc rows too" ); 160 161 std::printf (g_fail ?"native_dense_price_test: %d FAILED\n" :"native_dense_price_test: all passed\n" ,g_fail ); 162return g_fail ?1 :0 ; 163}