okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
01529aaa8
main
1// src/core/layout.cpp - the shape checks. See the header for why this exists. 2#include "strata/core/layout.hpp" 3 4#include <cstdio> 5 6namespace strata::core { 7namespace { 8 9/// A required 2-D shape. `ne0` is the CONTIGUOUS axis, matching the manifest and `s_gemv`'s convention 10/// (`y[o] = sum_i x[i]*W[i][o]`, row o contiguous of length ne0). 11struct Want2 { 12const char * suffix ; 13int64_t ne0 ; 14int64_t ne1 ; 15bool qsa_only ; 16bool gdn_only ; 17}; 18 19/// A required element count and ENGINE FORM for a 1-D tensor. 20/// 21/// `kind` is not decoration. The engine form of a 1-D tensor is whatever `WeightKind` the loader applied - 22/// `F32` copies 4 B/elem, `Bf16InF32` re-rounds to 2 - and a consumer that casts either one to `const float*` 23/// reads 2x its length. **THAT IS NOT HYPOTHETICAL: `ffn_gate_inp_shexp.weight` is BF16, so the arena holds 24/// 5120 B for 2560 elements, and `shared_expert` read it as f32. Every one of the 2560 MoE outputs came out 25/// non-finite** - a wrong answer loud enough to notice, which is the lucky version. Reading 5120 B past the 26/// end of a tensor inside a 4.5 GiB arena does not fault, so the same mistake on a tensor followed by 27/// plausible bytes would have produced plausible logits. 28struct Want1 { 29const char * suffix ; 30int64_t elements ; 31WeightKind kind ; 32bool qsa_only ; 33bool gdn_only ; 34}; 35 36bool fail (std::string & err ,const LayerView & v ,const char * suffix ,const char * what ,int64_t got , 37int64_t want ) { 38char buf [512 ]; 39 std::snprintf (buf ,sizeof buf ,"layer %lld: %s %s is %lld, the kernels require %lld" , 40 (long long )v .layer (),v .name (suffix ).c_str (),what , (long long )got , (long long )want ); 41err = buf ; 42return false; 43} 44 45bool check_one (const WeightTable & t ,const ModelGeometry & g ,int64_t layer , std::string & err ) { 46const LayerView v (t ,layer ); 47const bool qsa = is_qsa_layer (g ,layer ); 48 49// ---- the 2-D tensor set, per layer family 50const Want2 want2 []= { 51// gated residual, EVERY layer - `gr_read` takes w_down (hc_lr, hc_dim) and w_up (hc_lr, hc_dim) 52// after its own transpose, so the pack's orientation is [hc_dim, hc_lr] and [hc_lr, hc_dim]. 53 {"hc_attn_down.weight" ,g .hc_dim (),g .hc_lr , false, false}, 54 {"hc_attn_up.weight" ,g .hc_lr ,g .hc_dim (), false, false}, 55 {"hc_attn_inject.weight" ,g .hc_dim (),g .hc , false, false}, 56 {"hc_ffn_down.weight" ,g .hc_dim (),g .hc_lr , false, false}, 57 {"hc_ffn_up.weight" ,g .hc_lr ,g .hc_dim (), false, false}, 58 {"hc_ffn_inject.weight" ,g .hc_dim (),g .hc , false, false}, 59// MoE, EVERY layer 60 {"ffn_gate_inp.weight" ,g .n_embd ,g .n_expert , false, false}, 61 {"ffn_gate_shexp.weight" ,g .n_embd ,g .n_ff , false, false}, 62 {"ffn_up_shexp.weight" ,g .n_embd ,g .n_ff , false, false}, 63 {"ffn_down_shexp.weight" ,g .n_ff ,g .n_embd , false, false}, 64// GDN only 65 {"attn_qkv.weight" ,g .n_embd ,g .ssm_conv_channels , false, true}, 66 {"attn_gate.weight" ,g .n_embd ,g .ssm_value_dim , false, true}, 67 {"ssm_out.weight" ,g .ssm_value_dim ,g .n_embd , false, true}, 68 {"ssm_conv1d.weight" ,g .ssm_d_conv ,g .ssm_conv_channels , false, true}, 69 {"ssm_alpha.weight" ,g .n_embd ,g .ssm_v_heads , false, true}, 70 {"ssm_beta.weight" ,g .n_embd ,g .ssm_v_heads , false, true}, 71// QSA only 72 {"attn_q.weight" ,g .n_embd ,2 * g .n_head * g .head_dim , true, false}, 73 {"attn_k.weight" ,g .n_embd ,g .n_head_kv * g .head_dim , true, false}, 74 {"attn_v.weight" ,g .n_embd ,g .n_head_kv * g .head_dim , true, false}, 75 {"attn_output.weight" ,g .n_head * g .head_dim ,g .n_embd , true, false}, 76// THE INDEXER. `q_proj` is the QUERY count and `k_proj` is the KEY width, and they are different 77// numbers - conflating them is what made the planner's indexer term 4x too big in round 194. 78 {"indexer.q_proj.weight" ,g .n_embd ,g .idx_q_heads * g .idx_key_dim , true, false}, 79 {"indexer.k_proj.weight" ,g .n_embd ,g .idx_key_dim , true, false}, 80 }; 81for (const Want2 & w :want2 ) { 82if (w .qsa_only && !qsa )continue ; 83if (w .gdn_only && qsa )continue ; 84const WeightRef * r = v .get (w .suffix ); 85if (!r ) { 86err = "layer " + std::to_string (layer )+ ": missing " + v .name (w .suffix ); 87return false; 88 } 89if (r -> ne0 != w .ne0 )return fail (err ,v ,w .suffix ,"ne0" ,r -> ne0 ,w .ne0 ); 90if (r -> ne1 != w .ne1 )return fail (err ,v ,w .suffix ,"ne1" ,r -> ne1 ,w .ne1 ); 91 } 92 93// ---- the 1-D set. EVERY ONE OF THESE IS `F32` EXCEPT the shared expert's scalar gate, which is BF16 - 94// and that single exception is the one a reader would not guess, so it is written down rather than 95// inferred from the tensor count. 96const Want1 want1 []= { 97 {"hc_attn_norm.weight" ,g .hc_dim (),WeightKind ::F32 , false, false}, 98 {"hc_ffn_norm.weight" ,g .hc_dim (),WeightKind ::F32 , false, false}, 99 {"ffn_gate_inp_shexp.weight" ,g .n_embd ,WeightKind ::Bf16InF32 , false, false}, 100 {"ssm_a" ,g .ssm_v_heads ,WeightKind ::F32 , false, true}, 101 {"ssm_dt.bias" ,g .ssm_v_heads ,WeightKind ::F32 , false, true}, 102 {"ssm_norm.weight" ,g .ssm_state_size ,WeightKind ::F32 , false, true}, 103 {"attn_q_norm.weight" ,g .head_dim ,WeightKind ::F32 , true, false}, 104 {"attn_k_norm.weight" ,g .head_dim ,WeightKind ::F32 , true, false}, 105 {"indexer.q_norm.weight" ,g .idx_key_dim ,WeightKind ::F32 , true, false}, 106 {"indexer.k_norm.weight" ,g .idx_key_dim ,WeightKind ::F32 , true, false}, 107 }; 108for (const Want1 & w :want1 ) { 109if (w .qsa_only && !qsa )continue ; 110if (w .gdn_only && qsa )continue ; 111const WeightRef * r = v .get (w .suffix ); 112if (!r ) { 113err = "layer " + std::to_string (layer )+ ": missing " + v .name (w .suffix ); 114return false; 115 } 116if (r -> elements != w .elements )return fail (err ,v ,w .suffix ,"elements" ,r -> elements ,w .elements ); 117if (r -> kind != w .kind ) { 118// The byte count is what makes this a REAL check and not a label: a 4 B/elem tensor holds 119// `elements * 4`, a 2 B/elem one holds `elements * 2`, and a consumer reading it as the wrong one 120// walks off the end. 121const uint64_t want_bytes = (uint64_t )w .elements * 122 (w .kind == WeightKind ::Bf16InF32 ?2u :4u ); 123char buf [512 ]; 124 std::snprintf (buf ,sizeof buf , 125"layer %lld: %s is engine form %d (%llu B), the kernels read it as form %d (%llu B)" , 126 (long long )layer ,v .name (w .suffix ).c_str (), (int )r -> kind , 127 (unsigned long long )r -> bytes , (int )w .kind , (unsigned long long )want_bytes ); 128err = buf ; 129return false; 130 } 131 } 132return true; 133} 134 135}// namespace 136 137std::string LayerView ::name (const char * suffix )const { 138return "blk." + std::to_string (layer_ )+ "." + suffix ; 139} 140 141bool check_layer (const WeightTable & table ,const ModelGeometry & g ,int64_t layer , std::string & err ) { 142if (layer < 0 || layer >=g .n_layers ) { 143err = "layer " + std::to_string (layer )+ " is outside 0.." + std::to_string (g .n_layers - 1 ); 144return false; 145 } 146return check_one (table ,g ,layer ,err ); 147} 148 149bool check_all (const WeightTable & table ,const ModelGeometry & g , std::string & err ) { 150int64_t n_qsa = 0 ,n_gdn = 0 ; 151for (int64_t l = 0 ;l < g .n_layers ;++ l ) { 152if (!check_one (table ,g ,l ,err ))return false; 153if (is_qsa_layer (g ,l )) { 154++ n_qsa ; 155 }else { 156++ n_gdn ; 157 } 158 } 159// The split is derived twice over in `docs/semantics.md` - the interval and an explicit ratio array - 160// and both give 36 GDN and 12 QSA. Counting them here is the check that the LAYER TYPE PREDICATE and 161// the pack agree, which no per-tensor shape check can see. 162if (n_qsa != g .n_qsa_layers ()|| n_gdn != g .n_gdn_layers ()) { 163char buf [256 ]; 164 std::snprintf (buf ,sizeof buf ,"layer split is %lld QSA / %lld GDN, the geometry says %lld / %lld" , 165 (long long )n_qsa , (long long )n_gdn , (long long )g .n_qsa_layers (), 166 (long long )g .n_gdn_layers ()); 167err = buf ; 168return false; 169 } 170return true; 171} 172 173}// namespace strata::core