okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
74d12d663
main
1// tests/core/gguf_geometry_test.cpp - the model's geometry from its GGUF metadata (strata::read_geometry) and the 2// check against the shape the kernels are compiled for (strata::geometry_unsupported, check_architecture), on 3// synthetic GGUFs: no model, no GPU. 4// 5// The full key set is the one Unsloth's Qwen3.8-Flash-Next UD-IQ4_XS carries (read from its first shard). Then 6// the cases: a file with only the six keys the guard has always required, a pruned expert count, keys that 7// describe one thing twice and disagree, and a shape the kernels are not compiled for. 8// 9// `gguf_geometry_test --real SHARD1`: the geometry a real model's metadata shard declares, and the verdict. 10#include "gguf_fixture.hpp" 11 12#include <chrono> 13#include <cstdio> 14#include <filesystem> 15#include <functional> 16#include <string> 17#include <vector> 18 19namespace fs= std::filesystem; 20using strata::core::ModelGeometry ; 21 22namespace { 23int g_fail = 0 ; 24void check (bool ok ,const std::string & what ) { 25 std::printf (" %-90s %s\n" ,what .c_str (),ok ?"ok" :"FAIL" ); 26if (!ok )++ g_fail ; 27} 28 29struct TempDir { 30 fs::path path ; 31TempDir () { 32path = fs::temp_directory_path () / 33 ("strata-gguf-geometry-test-" + std::to_string (std::chrono::steady_clock::now ().time_since_epoch ().count ())); 34 fs::create_directories (path ); 35 } 36 ~TempDir () { 37 std::error_code ignored ; 38 fs::remove_all (path ,ignored ); 39 } 40}; 41 42std::vector < uint64_t > ratios (int64_t layers ,int64_t interval ,uint64_t block ) { 43 std::vector < uint64_t > r ((size_t )layers ,0 ); 44for (int64_t l = 0 ;l < layers ;++ l ) 45if (l %interval == interval - 1 )r [(size_t )l ]= block ; 46return r ; 47} 48 49/// Qwen3.8-Flash-Next's metadata as its GGUF carries it (the PLE and rope-section keys the reader does not use 50/// are left out), with a vocabulary of `vocab` tokens. 51std::vector < fixture::Kv > full_keys (size_t vocab = 1000 ) { 52using namespace fixture ; 53const std::string p = "qwen4exp." ; 54 std::vector < std::string > tokens (vocab ); 55for (size_t i = 0 ;i < vocab ;++ i )tokens [i ]= "t" + std::to_string (i ); 56return {str ("general.architecture" ,"qwen4exp" ), 57u32 (p + "block_count" ,48 ), 58u32 (p + "context_length" ,262144 ), 59u32 (p + "embedding_length" ,2560 ), 60u32 (p + "attention.head_count" ,24 ), 61u32 (p + "attention.head_count_kv" ,2 ), 62f32 (p + "rope.freq_base" ,1e7f ), 63f32 (p + "attention.layer_norm_rms_epsilon" ,1e-6f ), 64u32 (p + "expert_count" ,512 ), 65u32 (p + "expert_used_count" ,10 ), 66u32 (p + "attention.key_length" ,256 ), 67u32 (p + "attention.value_length" ,256 ), 68u32 (p + "expert_feed_forward_length" ,640 ), 69u32 (p + "expert_shared_feed_forward_length" ,640 ), 70u32 (p + "ssm.conv_kernel" ,4 ), 71u32 (p + "ssm.state_size" ,128 ), 72u32 (p + "ssm.group_count" ,16 ), 73u32 (p + "ssm.time_step_rank" ,48 ), 74u32 (p + "ssm.inner_size" ,6144 ), 75u32 (p + "full_attention_interval" ,4 ), 76u32 (p + "rope.dimension_count" ,64 ), 77u32 (p + "hyper_connection.count" ,4 ), 78u32 (p + "hyper_connection.low_rank" ,320 ), 79u32 (p + "attention.indexer.head_count" ,4 ), 80u32 (p + "attention.indexer.key_length" ,128 ), 81u32 (p + "attention.indexer.top_k" ,2048 ), 82u32s (p + "attention.compress_ratios" ,ratios (48 ,4 ,4 )), 83str ("tokenizer.ggml.model" ,"gpt2" ), 84strs ("tokenizer.ggml.tokens" ,tokens )}; 85} 86 87/// `kv` with `key` replaced by `v` (or removed, when `v` is null). 88std::vector < fixture::Kv > with (std::vector < fixture::Kv > kv ,const std::string & key ,const fixture::Kv * v ) { 89for (auto it = kv .begin ();it != kv .end ();++ it ) 90if (it -> key == key ) { 91if (v )* it = * v ; 92else kv .erase (it ); 93return kv ; 94 } 95if (v )kv .push_back (* v ); 96return kv ; 97} 98 99struct Result { 100 std::string read_err ,unsupported ,guard ; 101ModelGeometry g ; 102}; 103Result run (const fs::path & dir ,const std::vector < fixture::Kv >& kv ) { 104static int n = 0 ; 105const fs::path p = dir / ("m" + std::to_string (n ++ )+ ".gguf" ); 106 fixture::write (p ,kv , {}); 107Result r ; 108const strata::GgufFile f (p .string ()); 109r .read_err = strata::read_geometry (f ,r .g ); 110if (r .read_err .empty ())r .unsupported = strata::geometry_unsupported (r .g ); 111r .guard = strata::check_architecture (f ); 112return r ; 113} 114bool has (const std::string & s ,const char * part ) {return s .find (part )!= std::string::npos ; } 115 116bool same_shape (const ModelGeometry & a ,const ModelGeometry & b ) { 117return a .n_embd == b .n_embd && a .n_layers == b .n_layers && a .qsa_interval == b .qsa_interval && 118a .n_vocab == b .n_vocab && a .ssm_state_size == b .ssm_state_size && a .ssm_k_heads == b .ssm_k_heads && 119a .ssm_v_heads == b .ssm_v_heads && a .ssm_d_conv == b .ssm_d_conv && 120a .ssm_conv_channels == b .ssm_conv_channels && a .ssm_value_dim == b .ssm_value_dim && a .n_head == b .n_head && 121a .n_head_kv == b .n_head_kv && a .head_dim == b .head_dim && a .n_rot == b .n_rot && 122a .idx_q_heads == b .idx_q_heads && a .idx_key_dim == b .idx_key_dim && a .idx_block == b .idx_block && 123a .idx_top_k == b .idx_top_k && a .hc == b .hc && a .hc_lr == b .hc_lr && a .n_expert == b .n_expert && 124a .n_expert_used == b .n_expert_used && a .n_ff == b .n_ff ; 125} 126 127int real_model (const std::string & shard ) { 128try { 129const strata::GgufFile f (shard ); 130ModelGeometry g ; 131const std::string err = strata::read_geometry (f ,g ); 132if (!err .empty ()) { 133 std::printf (" refused: %s\n" ,err .c_str ()); 134return 1 ; 135 } 136 std::printf (" n_embd %lld, layers %lld (attention every %lld), vocab %lld, rms_eps %g\n" , (long long )g .n_embd , 137 (long long )g .n_layers , (long long )g .qsa_interval , (long long )g .n_vocab ,g .rms_eps ); 138 std::printf (" GDN: state %lld, k heads %lld, v heads %lld, conv %lld (%lld channels, values %lld)\n" , 139 (long long )g .ssm_state_size , (long long )g .ssm_k_heads , (long long )g .ssm_v_heads , 140 (long long )g .ssm_d_conv , (long long )g .ssm_conv_channels , (long long )g .ssm_value_dim ); 141 std::printf (" QSA: heads %lld/%lld x %lld, rope %lld, indexer %lld x %lld, block %lld, top-k %lld\n" , 142 (long long )g .n_head , (long long )g .n_head_kv , (long long )g .head_dim , (long long )g .n_rot , 143 (long long )g .idx_q_heads , (long long )g .idx_key_dim , (long long )g .idx_block , 144 (long long )g .idx_top_k ); 145 std::printf (" HC %lld (rank %lld); MoE %lld experts, %lld per token, ff %lld\n" , (long long )g .hc , 146 (long long )g .hc_lr , (long long )g .n_expert , (long long )g .n_expert_used , (long long )g .n_ff ); 147const std::string why = strata::geometry_unsupported (g ); 148 std::printf (" the compiled kernels: %s\n" ,why .empty () ?"can run it" :why .c_str ()); 149return why .empty () ?0 :1 ; 150 }catch (const std::exception & e ) { 151 std::printf (" refused: %s\n" ,e .what ()); 152return 1 ; 153 } 154} 155}// namespace 156 157int main (int argc ,char ** argv ) { 158if (argc == 3 && std::string (argv [1 ])== "--real" )return real_model (argv [2 ]); 159 std::printf ("gguf_geometry_test\n" ); 160TempDir d ; 161const std::string p = "qwen4exp." ; 162 { 163const Result r = run (d .path ,full_keys ()); 164ModelGeometry want = strata::core::kCompiledGeometry ; 165want .n_vocab = 1000 ; 166check (r .read_err .empty (),"the full key set reads" + (r .read_err .empty () ?"" :": " + r .read_err )); 167check (same_shape (r .g ,want )," and gives the compiled shape (vocabulary from tokenizer.ggml.tokens)" ); 168check (r .g .ssm_conv_channels == 10240 ," conv channels derived: 2 x 128 x 16 + 6144 = 10240" ); 169check (r .unsupported .empty ()&& r .guard .empty ()," which the kernels run, and the guard passes" ); 170 } 171 { 172// the six keys tests/core/native_dense_ple_key_test.cpp writes: everything else is the compiled value 173const std::vector < fixture::Kv > six = { 174 fixture::str ("general.architecture" ,"qwen4exp" ), fixture::u32 (p + "block_count" ,48 ), 175 fixture::u32 (p + "embedding_length" ,2560 ), fixture::u32 (p + "expert_count" ,512 ), 176 fixture::u32 (p + "expert_used_count" ,10 ), fixture::u32 (p + "attention.head_count" ,24 ), 177 fixture::u32 (p + "attention.head_count_kv" ,2 )}; 178const Result r = run (d .path ,six ); 179check (r .read_err .empty ()&& same_shape (r .g , strata::core::kCompiledGeometry )&& r .guard .empty (), 180"only the six required keys: the rest keeps the compiled values" ); 181for (const char * key : {"block_count" ,"embedding_length" ,"expert_count" ,"expert_used_count" , 182"attention.head_count" ,"attention.head_count_kv" }) { 183const Result m = run (d .path ,with (six ,p + key ,nullptr )); 184check (has (m .read_err ,"missing" )&& has (m .read_err ,key )&& m .guard == m .read_err , 185 std::string ("a file without " )+ key + " is refused, naming it" ); 186 } 187 } 188 { 189// GSQ-RCO Coder: fewer experts, read from the file and run by the same kernels 190const fixture::Kv e = fixture::u32 (p + "expert_count" ,384 ),k = fixture::u32 (p + "expert_used_count" ,8 ); 191const Result r = run (d .path ,with (with (full_keys (),e .key ,& e ),k .key ,& k )); 192check (r .read_err .empty ()&& r .g .n_expert == 384 && r .g .n_expert_used == 8 && r .guard .empty (), 193"a pruned variant: 384 experts, 8 per token, read and accepted" ); 194const fixture::Kv k65 = fixture::u32 (p + "expert_used_count" ,65 ); 195check (has (run (d .path ,with (full_keys (),k65 .key ,& k65 )).read_err ,"expert_used_count = 65" ), 196"65 experts per token is refused (the router takes 1..64)" ); 197const fixture::Kv k0 = fixture::u32 (p + "expert_used_count" ,0 ); 198check (has (run (d .path ,with (full_keys (),k0 .key ,& k0 )).read_err ,"must be positive" ), 199"0 experts per token is refused" ); 200 } 201 { 202// keys that describe one thing twice must agree 203const fixture::Kv vl = fixture::u32 (p + "attention.value_length" ,128 ); 204check (has (run (d .path ,with (full_keys (),vl .key ,& vl )).read_err ,"attention.value_length = 128" ), 205"a value head width other than the key head width is refused" ); 206const fixture::Kv sh = fixture::u32 (p + "expert_shared_feed_forward_length" ,1280 ); 207check (has (run (d .path ,with (full_keys (),sh .key ,& sh )).read_err ,"expert_shared_feed_forward_length = 1280" ), 208"a shared expert wider than the routed ones is refused" ); 209const fixture::Kv in = fixture::u32 (p + "ssm.inner_size" ,4096 ); 210check (has (run (d .path ,with (full_keys (),in .key ,& in )).read_err ,"square heads" ), 211"ssm.inner_size other than state_size x time_step_rank is refused" ); 212 std::vector < uint64_t > shifted = ratios (48 ,4 ,4 ); 213shifted [3 ]= 0 ; 214shifted [2 ]= 4 ; 215const fixture::Kv cr = fixture::u32s (p + "attention.compress_ratios" ,shifted ); 216check (has (run (d .path ,with (full_keys (),cr .key ,& cr )).read_err ,"compress_ratios[2] = 4" ), 217"compress_ratios that disagree with full_attention_interval are refused at the first layer" ); 218const fixture::Kv short_cr = fixture::u32s (p + "attention.compress_ratios" ,ratios (40 ,4 ,4 )); 219check (has (run (d .path ,with (full_keys (),short_cr .key ,& short_cr )).read_err ,"one entry for each of the 48" ), 220"compress_ratios of another length are refused" ); 221 std::vector < uint64_t > mixed = ratios (48 ,4 ,4 ); 222mixed [7 ]= 8 ; 223const fixture::Kv mx = fixture::u32s (p + "attention.compress_ratios" ,mixed ); 224check (has (run (d .path ,with (full_keys (),mx .key ,& mx )).read_err ,"compress_ratios[7] = 8" ), 225"two pooling blocks in one model are refused" ); 226// #2396: 48 zeros are a converter's placeholder (orcarouter's Uncensored GGUFs): read as absent, the compiled block stays 227const fixture::Kv zeros = fixture::u32s (p + "attention.compress_ratios" , std::vector < uint64_t > (48 ,0 )); 228const Result z = run (d .path ,with (full_keys (),zeros .key ,& zeros )); 229check (z .read_err .empty ()&& z .guard .empty ()&& z .g .idx_block == 4 ,"an all-zero compress_ratios list is treated as absent" ); 230const fixture::Kv zshort = fixture::u32s (p + "attention.compress_ratios" , std::vector < uint64_t > (40 ,0 )); 231check (has (run (d .path ,with (full_keys (),zshort .key ,& zshort )).read_err ,"one entry for each of the 48" ), 232"an all-zero list of another length is still refused" ); 233 std::vector < uint64_t > one_set (48 ,0 ); 234one_set [3 ]= 4 ;// a real (but partial) list: layer 7 would be full attention with a 0 235const fixture::Kv partial = fixture::u32s (p + "attention.compress_ratios" ,one_set ); 236check (has (run (d .path ,with (full_keys (),partial .key ,& partial )).read_err ,"compress_ratios[7] = 0" ), 237"a list with one non-zero entry is checked in full" ); 238const fixture::Kv fl = fixture::f32 (p + "block_count" ,48.0f ); 239check (has (run (d .path ,with (full_keys (),fl .key ,& fl )).read_err ,"is not an integer" ), 240"a float where a count belongs is refused" ); 241 } 242 { 243// a consistent file of another shape reads, and the kernels refuse it naming the key 244const fixture::Kv bc = fixture::u32 (p + "block_count" ,40 ); 245const fixture::Kv cr = fixture::u32s (p + "attention.compress_ratios" ,ratios (40 ,4 ,4 )); 246const Result r = run (d .path ,with (with (full_keys (),bc .key ,& bc ),cr .key ,& cr )); 247check (r .read_err .empty ()&& r .g .n_layers == 40 && r .g .n_qsa_layers ()== 10 ,"40 layers read: 10 QSA, 30 GDN" ); 248check (r .unsupported == "qwen4exp.block_count = 40, the kernels are compiled for 48" && r .guard == r .unsupported , 249" and refused: " + r .unsupported ); 250const fixture::Kv hd = fixture::u32 (p + "attention.key_length" ,128 ),vd = fixture::u32 (p + "attention.value_length" ,128 ); 251const Result h = run (d .path ,with (with (full_keys (),hd .key ,& hd ),vd .key ,& vd )); 252check (h .read_err .empty ()&& h .g .head_dim == 128 && has (h .guard ,"attention.key_length = 128" ), 253"head width 128 reads and is refused: " + h .guard ); 254const fixture::Kv tk = fixture::u32 (p + "attention.indexer.top_k" ,1024 ); 255check (has (run (d .path ,with (full_keys (),tk .key ,& tk )).guard ,"attention.indexer.top_k = 1024" ), 256"an indexer budget of 1024 is refused" ); 257const fixture::Kv eps = fixture::f32 (p + "attention.layer_norm_rms_epsilon" ,1e-5f ); 258check (has (run (d .path ,with (full_keys (),eps .key ,& eps )).guard ,"layer_norm_rms_epsilon = 1e-05" ), 259"an rms epsilon of 1e-5 is refused" ); 260const fixture::Kv vocab = fixture::u32 (p + "embedding_length" ,2560 ); 261check (run (d .path ,with (full_keys (151936 ),vocab .key ,& vocab )).guard .empty (), 262"another vocabulary size is accepted (the head and embedding take their tensors' shapes)" ); 263const fixture::Kv arch = fixture::str ("general.architecture" ,"glm5next" ); 264const Result a = run (d .path ,with (full_keys (),arch .key ,& arch )); 265check (a .read_err == "architecture is 'glm5next', this engine requires 'qwen4exp' or 'edge0'" && a .guard == a .read_err , 266"another architecture is refused by name, by the reader and the guard" ); 267 } 268 { 269// Qwen3.8-27B's own keys (qwen35, dense): the reader names the architecture, not the first qwen4exp key it 270// misses (which is what generate printed before the reader checked the name) 271const std::vector < fixture::Kv > dense = { 272 fixture::str ("general.architecture" ,"qwen35" ), fixture::u32 ("qwen35.block_count" ,65 ), 273 fixture::u32 ("qwen35.embedding_length" ,5120 ), fixture::u32 ("qwen35.feed_forward_length" ,17408 ), 274 fixture::u32 ("qwen35.attention.head_count" ,24 ), fixture::u32 ("qwen35.attention.head_count_kv" ,4 )}; 275const Result r = run (d .path ,dense ); 276check (r .read_err == "architecture is 'qwen35', this engine requires 'qwen4exp' or 'edge0'" , 277"a dense qwen35 file is refused by its architecture: " + r .read_err ); 278const Result none = run (d .path ,with (dense ,"general.architecture" ,nullptr )); 279check (none .read_err == "missing general.architecture" ,"a file without general.architecture says so" ); 280 } 281 std::printf (g_fail ?"gguf_geometry_test: %d FAILED\n" :"gguf_geometry_test: all passed\n" ,g_fail ); 282return g_fail ?1 :0 ; 283}