okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
4cfeaa0f4
main
1// tests/core/model_family_test.cpp - the model families (NEW-MODELS-PLAN.md T0) on synthetic GGUFs: no model, no GPU. 2// 3// * the architecture guard accepts qwen4exp and qwen35moe and refuses anything else; the engine's guard refuses 4// qwen35moe with "not ... yet" (no engine path for Qwen3.6 before T1); 5// * geometry_from_gguf reads Qwen3.6-35B-A3B's real metadata values (hidden 2048, 40 + 1 MTP layers, 32 GDN V 6// heads, conv 8192, no hyper-connections, no indexer) and Flash-Next's as exactly the compiled defaults; 7// * the layout tables, on tiny files of either family: a complete file passes, a wrong shape, a missing tensor 8// and (qwen35moe) a tensor the table does not name are refused, naming the tensor. 9// 10// `model_family_test --real SHARD`: the same view of a real model (headers only): family, geometry, the kernels' 11// verdict and the layout check over every shard. 12#include "gguf_fixture.hpp" 13 14#include "strata/core/family_layout.hpp" 15 16#include <chrono> 17#include <cstdio> 18#include <filesystem> 19#include <string> 20#include <vector> 21 22namespace fs= std::filesystem; 23 24namespace { 25int g_fail = 0 ; 26void check (bool ok ,const std::string & what ) { 27 std::printf (" %-90s %s\n" ,what .c_str (),ok ?"ok" :"FAIL" ); 28if (!ok )++ g_fail ; 29} 30 31struct TempDir { 32 fs::path path ; 33TempDir () { 34path = fs::temp_directory_path () / 35 ("strata-model-family-test-" + std::to_string (std::chrono::steady_clock::now ().time_since_epoch ().count ())); 36 fs::create_directories (path ); 37 } 38 ~TempDir () { 39 std::error_code ignored ; 40 fs::remove_all (path ,ignored ); 41 } 42}; 43 44using fixture::Kv ; 45using fixture::Tensor ; 46const uint32_t F32 = 0 ; 47 48struct Dims { 49uint64_t block_count ,embd ,heads ,heads_kv ,key_len ,experts ,used ,ff ,ff_sh ,conv ,state ,groups ,v_heads , 50inner ,interval ,nextn ,hc ,hc_lr ,idx_heads ,idx_key ; 51}; 52// Qwen3.6-35B-A3B (unsloth/Qwen3.6-35B-A3B-MTP-GGUF, every quant) and Qwen3.8-Flash-Next, as their files say 53const Dims kQwen36 = {41 ,2048 ,16 ,2 ,256 ,256 ,8 ,512 ,512 ,4 ,128 ,16 ,32 ,4096 ,4 ,1 ,0 ,0 ,0 ,0 }; 54const Dims kFlashNext = {48 ,2560 ,24 ,2 ,256 ,512 ,10 ,640 ,640 ,4 ,128 ,16 ,48 ,6144 ,4 ,0 ,4 ,320 ,4 ,128 }; 55// tiny models of each family for the layout checks (every tensor F32, a few KB) 56const Dims kTiny36 = {5 ,64 ,2 ,1 ,16 ,4 ,2 ,32 ,32 ,4 ,8 ,2 ,4 ,32 ,4 ,1 ,0 ,0 ,0 ,0 }; 57const Dims kTinyFlash = {8 ,64 ,2 ,1 ,16 ,4 ,2 ,32 ,32 ,4 ,8 ,2 ,4 ,32 ,4 ,0 ,2 ,8 ,2 ,8 }; 58 59std::vector < Kv > metadata (const char * arch ,const Dims & d ,uint64_t vocab ) { 60const std::string p = std::string (arch )+ "." ; 61 std::vector < Kv > kv = {fixture::str ("general.architecture" ,arch ), 62 fixture::u32 (p + "block_count" ,d .block_count ), 63 fixture::u32 (p + "embedding_length" ,d .embd ), 64 fixture::u32 (p + "attention.head_count" ,d .heads ), 65 fixture::u32 (p + "attention.head_count_kv" ,d .heads_kv ), 66 fixture::u32 (p + "attention.key_length" ,d .key_len ), 67 fixture::u32 (p + "attention.value_length" ,d .key_len ), 68 fixture::u32 (p + "expert_count" ,d .experts ), 69 fixture::u32 (p + "expert_used_count" ,d .used ), 70 fixture::u32 (p + "expert_feed_forward_length" ,d .ff ), 71 fixture::u32 (p + "expert_shared_feed_forward_length" ,d .ff_sh ), 72 fixture::u32 (p + "ssm.conv_kernel" ,d .conv ), 73 fixture::u32 (p + "ssm.state_size" ,d .state ), 74 fixture::u32 (p + "ssm.group_count" ,d .groups ), 75 fixture::u32 (p + "ssm.time_step_rank" ,d .v_heads ), 76 fixture::u32 (p + "ssm.inner_size" ,d .inner ), 77 fixture::u32 (p + "full_attention_interval" ,d .interval )}; 78if (std::string (arch )== "qwen35moe" ) { 79kv .push_back (fixture::u32 (p + "nextn_predict_layers" ,d .nextn )); 80 }else { 81kv .push_back (fixture::u32 (p + "hyper_connection.count" ,d .hc )); 82kv .push_back (fixture::u32 (p + "hyper_connection.low_rank" ,d .hc_lr )); 83kv .push_back (fixture::u32 (p + "attention.indexer.head_count" ,d .idx_heads )); 84kv .push_back (fixture::u32 (p + "attention.indexer.key_length" ,d .idx_key )); 85 } 86 std::vector < std::string > tokens ; 87for (uint64_t i = 0 ;i < vocab ;++ i )tokens .push_back ("t" + std::to_string (i )); 88kv .push_back (fixture::str_array ("tokenizer.ggml.tokens" ,tokens )); 89return kv ; 90} 91 92/// The tensors the layout table of `g` names, as F32 fixtures. 93std::vector < Tensor > tensors_of (const strata::core::ModelGeometry & g ) { 94 std::vector < Tensor > ts ; 95uint8_t seed = 1 ; 96auto add = [& ](const std::string & name ,const std::vector < int64_t >& shape ) { 97 std::vector < uint64_t > s (shape .begin (),shape .end ()); 98ts .push_back ({name ,s ,F32 ,seed ++ }); 99 }; 100for (const auto & w : strata::core::global_tensors (g ))add (w .name ,w .shape ); 101for (int64_t l = 0 ;l < g .n_layers + g .n_mtp_layers ;++ l ) 102for (const auto & w : strata::core::layer_tensors (g ,l ))add ("blk." + std::to_string (l )+ "." + w .name ,w .shape ); 103return ts ; 104} 105 106bool geometry_of (const fs::path & p , strata::core::ModelGeometry & g ,int64_t & k , std::string & err ) { 107const strata::GgufFile f (p .string ()); 108return strata::core::geometry_from_gguf (f ,g ,k ,err ); 109} 110 111std::string layout_error (const fs::path & dir ,const char * arch ,const Dims & d ,uint64_t vocab , 112void (* mutate )(std::vector < Tensor >& )) { 113const fs::path meta = dir /"meta.gguf" ; 114 fixture::write (meta ,metadata (arch ,d ,vocab ), {}); 115 strata::core::ModelGeometry g ; 116int64_t k = 0 ; 117 std::string err ; 118if (std::string (arch )== "qwen35moe" ) { 119if (!geometry_of (meta ,g ,k ,err ))return "geometry: " + err ; 120 }else { 121// the qwen4exp guard pins Flash-Next's own block count and widths, so the tiny model's geometry is set here 122g .n_layers = (int64_t )d .block_count ; 123g .n_embd = (int64_t )d .embd ; 124g .n_head = (int64_t )d .heads ; 125g .n_head_kv = (int64_t )d .heads_kv ; 126g .head_dim = (int64_t )d .key_len ; 127g .n_expert = (int64_t )d .experts ; 128g .n_ff = (int64_t )d .ff ; 129g .n_ff_shexp = (int64_t )d .ff_sh ; 130g .ssm_d_conv = (int64_t )d .conv ; 131g .ssm_state_size = (int64_t )d .state ; 132g .ssm_k_heads = (int64_t )d .groups ; 133g .ssm_v_heads = (int64_t )d .v_heads ; 134g .ssm_value_dim = (int64_t )d .inner ; 135g .ssm_conv_channels = 2 * g .ssm_state_size * g .ssm_k_heads + g .ssm_value_dim ; 136g .hc = (int64_t )d .hc ; 137g .hc_lr = (int64_t )d .hc_lr ; 138g .idx_q_heads = (int64_t )d .idx_heads ; 139g .idx_key_dim = (int64_t )d .idx_key ; 140g .n_vocab = (int64_t )vocab ; 141 } 142 std::vector < Tensor > ts = tensors_of (g ); 143if (mutate )mutate (ts ); 144const fs::path model = dir / (std::string (arch )+ "-tiny.gguf" ); 145 fixture::write (model ,metadata (arch ,d ,vocab ),ts ); 146const strata::GgufModel m ({model .string ()}); 147size_t n = 0 ; 148if (!strata::core::check_gguf_layout (m ,g ,err ,& n ))return err ; 149return {}; 150} 151 152int real_model (const std::string & any ) { 153try { 154const strata::GgufModel m = strata::GgufModel ::open (any ); 155const std::string arch = strata::check_architecture (m .meta ()); 156 std::printf (" %zu shard(s); architecture guard: %s; engine guard: %s\n" ,m .size (), 157arch .empty () ?"ok" :arch .c_str (), 158 strata::check_engine_architecture (m .meta ()).empty () ?"runs" 159 : strata::check_engine_architecture (m .meta ()).c_str ()); 160if (!arch .empty ())return 1 ; 161 strata::core::ModelGeometry g ; 162int64_t k = 0 ; 163 std::string err ; 164if (!strata::core::geometry_from_gguf (m .meta (),g ,k ,err )) { 165 std::printf (" geometry: %s\n" ,err .c_str ()); 166return 1 ; 167 } 168 std::printf (" family %s: layers %lld (+%lld MTP), hidden %lld, attention %lld/%lld x %lld every %lld, GDN K %lld" 169" V %lld x %lld conv %lld (%lld), MoE %lld experts top-%lld FF %lld shared %lld, hc %lld, indexer" 170" %lld, vocab %lld\n" , 171 strata::model_family_title (g .family ), (long long )g .n_layers , (long long )g .n_mtp_layers , 172 (long long )g .n_embd , (long long )g .n_head , (long long )g .n_head_kv , (long long )g .head_dim , 173 (long long )g .qsa_interval , (long long )g .ssm_k_heads , (long long )g .ssm_v_heads , 174 (long long )g .ssm_state_size , (long long )g .ssm_conv_channels , (long long )g .ssm_d_conv , 175 (long long )g .n_expert , (long long )k , (long long )g .n_ff , (long long )g .n_ff_shexp , 176 (long long )g .hc , (long long )g .idx_q_heads , (long long )g .n_vocab ); 177const std::string mm = strata::core::compiled_geometry_mismatch (g ); 178 std::printf (" compiled kernels: %s\n" ,mm .empty () ?"match" :mm .c_str ()); 179size_t n = 0 ; 180const bool ok = strata::core::check_gguf_layout (m ,g ,err ,& n ); 181 std::printf (" layout: %s (%zu tensors checked)\n" ,ok ?"ok" :err .c_str (),n ); 182return ok ?0 :1 ; 183 }catch (const std::exception & e ) { 184 std::printf (" refused: %s\n" ,e .what ()); 185return 1 ; 186 } 187} 188}// namespace 189 190int main (int argc ,char ** argv ) { 191if (argc == 3 && std::string (argv [1 ])== "--real" )return real_model (argv [2 ]); 192 std::printf ("model_family_test\n" ); 193TempDir d ; 194 { 195// the guards 196const fs::path q36 = d .path /"q36.gguf" ,fn = d .path /"fn.gguf" ,other = d .path /"other.gguf" ; 197 fixture::write (q36 ,metadata ("qwen35moe" ,kQwen36 ,8 ), {}); 198 fixture::write (fn ,metadata ("qwen4exp" ,kFlashNext ,8 ), {}); 199 fixture::write (other , {fixture::str ("general.architecture" ,"llama" )}, {}); 200const strata::GgufFile a (q36 .string ()),b (fn .string ()),c (other .string ()); 201check (strata::model_family (a )== strata::ModelFamily ::Qwen35Moe && 202 strata::model_family (b )== strata::ModelFamily ::Qwen4Exp && 203 strata::model_family (c )== strata::ModelFamily ::Unknown , 204"model_family: qwen35moe, qwen4exp, unknown" ); 205check (strata::check_architecture (a ).empty (),"the architecture guard accepts qwen35moe" ); 206check (strata::check_architecture (b ).empty (),"the architecture guard accepts qwen4exp" ); 207check (strata::check_architecture (c ).find ("'llama'" )!= std::string::npos ,"and refuses llama, naming it" ); 208const std::string e = strata::check_engine_architecture (a ); 209check (e .find ("Qwen3.6-35B-A3B" )!= std::string::npos && e .find ("does not run it yet" )!= std::string::npos , 210"the engine guard refuses Qwen3.6: recognised, not supported yet" ); 211check (strata::check_engine_architecture (b ).empty (),"the engine guard runs qwen4exp" ); 212 std::vector < Kv > kv = metadata ("qwen35moe" ,kQwen36 ,8 ); 213for (auto it = kv .begin ();it != kv .end ();++ it ) 214if (it -> key == "qwen35moe.ssm.time_step_rank" ) {kv .erase (it );break ; } 215const fs::path miss = d .path /"miss.gguf" ; 216 fixture::write (miss ,kv , {}); 217check (strata::check_architecture (strata::GgufFile (miss .string ())).find ("ssm.time_step_rank" )!= std::string::npos , 218"a qwen35moe file without ssm.time_step_rank is refused, naming the key" ); 219 } 220 { 221// the geometry of the real models, from their metadata values 222const fs::path q36 = d .path /"q36g.gguf" ,fn = d .path /"fng.gguf" ; 223 fixture::write (q36 ,metadata ("qwen35moe" ,kQwen36 ,248320 ), {}); 224 fixture::write (fn ,metadata ("qwen4exp" ,kFlashNext ,248320 ), {}); 225 strata::core::ModelGeometry g ; 226int64_t k = 0 ; 227 std::string err ; 228const bool ok = geometry_of (q36 ,g ,k ,err ); 229check (ok ,"Qwen3.6 geometry reads" + (ok ? std::string () :": " + err )); 230check (g .family == strata::ModelFamily ::Qwen35Moe && g .n_layers == 40 && g .n_mtp_layers == 1 && g .n_embd == 2048 , 231"Qwen3.6: 40 trunk layers + 1 MTP block, hidden 2048" ); 232check (g .n_qsa_layers ()== 10 && g .n_gdn_layers ()== 30 && strata::core::layer_kind (g ,3 )== 233 strata::core::LayerKind ::Attention && strata::core::layer_kind (g ,40 )== strata::core::LayerKind ::Mtp , 234"Qwen3.6: 10 attention / 30 GDN layers, layer 40 is the MTP block" ); 235check (g .ssm_k_heads == 16 && g .ssm_v_heads == 32 && g .ssm_conv_channels == 8192 && g .ssm_value_dim == 4096 , 236"Qwen3.6 GDN: 16 K groups, 32 V heads, conv 8192, inner 4096" ); 237check (g .n_head == 16 && g .n_head_kv == 2 && g .head_dim == 256 && !g .has_indexer ()&& !g .has_hyper_connections (), 238"Qwen3.6 attention: 16 Q / 2 KV x 256, no indexer, no hyper-connections" ); 239check (g .n_expert == 256 && k == 8 && g .n_ff == 512 && g .n_ff_shexp == 512 && g .n_vocab == 248320 , 240"Qwen3.6 MoE: 256 experts top-8, FF 512, shared 512; vocab 248320" ); 241check (!strata::core::compiled_geometry_mismatch (g ).empty (),"Qwen3.6 does not match the compiled kernels" ); 242 strata::core::ModelGeometry f ; 243const bool okf = geometry_of (fn ,f ,k ,err ); 244const strata::core::ModelGeometry c ; 245check (okf && strata::core::compiled_geometry_mismatch (f ).empty ()&& k == 10 && f .n_expert == c .n_expert && 246f .ssm_conv_channels == c .ssm_conv_channels && f .n_vocab == c .n_vocab && f .hc_dim ()== c .hc_dim (), 247"Flash-Next's metadata gives exactly the compiled geometry" ); 248Dims coder = kFlashNext ; 249coder .experts = 256 ; 250const fs::path cp = d .path /"coder.gguf" ; 251 fixture::write (cp ,metadata ("qwen4exp" ,coder ,8 ), {}); 252check (geometry_of (cp ,f ,k ,err )&& f .n_expert == 256 && strata::core::compiled_geometry_mismatch (f ).empty (), 253"the Coder's 256 experts are a run-time size, not a kernel mismatch" ); 254Dims wrong = kQwen36 ; 255wrong .inner = 4000 ; 256const fs::path wp = d .path /"wrong.gguf" ; 257 fixture::write (wp ,metadata ("qwen35moe" ,wrong ,8 ), {}); 258check (!geometry_of (wp ,f ,k ,err )&& err .find ("ssm.inner_size" )!= std::string::npos , 259"an inconsistent ssm.inner_size is refused" ); 260 } 261 { 262// the layout tables 263check (layout_error (d .path ,"qwen35moe" ,kTiny36 ,16 ,nullptr ).empty (), 264"a complete qwen35moe file passes its layout (incl. the nextn MTP block)" ); 265const std::string ef = layout_error (d .path ,"qwen4exp" ,kTinyFlash ,16 ,nullptr ); 266check (ef .empty (),"a complete qwen4exp file passes its layout (hyper-connections, indexer)" + 267 (ef .empty () ? std::string () :": " + ef )); 268 std::string e = layout_error (d .path ,"qwen35moe" ,kTiny36 ,16 , [](std::vector < Tensor >& ts ) { 269for (auto & t :ts ) 270if (t .name == "blk.1.ssm_conv1d.weight" )t .shape [1 ]+= 1 ; 271 }); 272check (e .find ("blk.1.ssm_conv1d.weight" )!= std::string::npos && e .find ("wants" )!= std::string::npos , 273"a wrong shape is refused, naming the tensor and both shapes" ); 274e = layout_error (d .path ,"qwen35moe" ,kTiny36 ,16 , [](std::vector < Tensor >& ts ) { 275for (auto it = ts .begin ();it != ts .end ();++ it ) 276if (it -> name == "blk.4.nextn.eh_proj.weight" ) {ts .erase (it );break ; } 277 }); 278check (e == "missing blk.4.nextn.eh_proj.weight" ,"a missing MTP tensor is refused" ); 279e = layout_error (d .path ,"qwen35moe" ,kTiny36 ,16 , [](std::vector < Tensor >& ts ) { 280ts .push_back ({"blk.0.hc_attn_norm.weight" , {64 },F32 ,99 }); 281 }); 282check (e .find ("blk.0.hc_attn_norm.weight" )!= std::string::npos && e .find ("not in the" )!= std::string::npos , 283"a tensor the qwen35moe table does not name is refused" ); 284e = layout_error (d .path ,"qwen4exp" ,kTinyFlash ,16 , [](std::vector < Tensor >& ts ) { 285for (auto it = ts .begin ();it != ts .end ();++ it ) 286if (it -> name == "blk.3.indexer.k_proj.weight" ) {ts .erase (it );break ; } 287 }); 288check (e == "missing blk.3.indexer.k_proj.weight" ,"Flash-Next: a missing indexer tensor is refused (" + e + ")" ); 289 } 290 std::printf (g_fail ?"FAILED (%d)\n" :"ALL PASSED\n" ,g_fail ); 291return g_fail ?1 :0 ; 292}