okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
7f46672a4
main
1// metal/src/engine/geo.h - the shape the Metal engine is compiled for, from strata::core::kCompiledGeometry (cmake -DSTRATA_GEOMETRY / 2// -DSTRATA_GEOMETRY_FILE, include/strata/core/geometry.hpp), and the #define prelude that hands it to the kernels. 3// 4// Host side: window.cpp, prompt_chain.inc and stream.cpp take their sizes from here instead of from literals (hidden 2560, 640 wide 5// experts, 48 layers, 24 query heads, ...). For Flash-Next's header every value below is the number it replaced. 6// 7// Kernel side: the MSL sources read their shape from GEO_* macros with Flash-Next's value as the default (`#ifndef GEO_QH`); the 8// prelude defines a macro only when this build's value differs from Flash-Next's, so a Flash-Next build hands the compiler exactly 9// the prelude it always did. Everything else in the kernels is run-time (the arguments carry n_embd, n_ff, hc_lr, the head counts of 10// the GDN) or a held-fixed constant of the family (state 128, head width 256, 4 hyper-connection streams, indexer 4 x 128). 11#pragma once 12 13#include "strata/core/geometry.hpp" 14 15#include <string> 16 17namespace strata_metal ::geo { 18 19inline constexprconst strata ::core ::ModelGeometry & G = strata ::core ::kCompiledGeometry ; 20 21inline constexprint N = (int )G .n_embd ;// hidden 22inline constexprint HC = (int )G .hc ;// hyper-connection streams (4) 23inline constexprint LR = (int )G .hc_lr ;// hyper-connection low rank 24inline constexprint NFF = (int )G .n_ff ;// routed (and shared) expert width 25inline constexprint TOPK = (int )G .n_expert_used ;// experts per token 26inline constexprint NEXP = (int )G .n_expert ;// routed experts per layer 27inline constexprint NL = (int )G .n_layers ;// trunk layers 28inline constexprint QI = (int )G .qsa_interval ;// every QI-th layer is full attention (QI - 1, 2 QI - 1, ...) 29inline constexprint NQSA = NL /QI ;// full-attention layers (12) 30inline constexprint NGDN = NL - NQSA ;// GDN layers (36) 31 32// GDN 33inline constexprint SS = (int )G .ssm_state_size ;// state width (128) 34inline constexprint HK = (int )G .ssm_k_heads ;// key heads (16) 35inline constexprint HV = (int )G .ssm_v_heads ;// value heads (48) 36inline constexprint CONV = (int )G .ssm_conv_channels ;// 2 * SS * HK + SS * HV = q|k|v channels (10240) 37inline constexprint ZV = (int )G .ssm_value_dim ;// SS * HV (6144): z, the readout y 38 39// full attention 40inline constexprint QH = (int )G .n_head ;// query heads (24) 41inline constexprint QKV = (int )G .n_head_kv ;// kv heads (2) 42inline constexprint QD = (int )G .head_dim ;// head width (256) 43inline constexprint IH = (int )G .idx_q_heads ;// indexer heads (4) 44inline constexprint ID = (int )G .idx_key_dim ;// indexer key width (128) 45inline constexprint BUD = (int )G .idx_top_k + 3 ;// selection budget in cells (2051): top_k + the tail block's 3 46inline constexprint QCH = 64 ;// cells per attention chunk (qsa.metal QCH) 47inline constexprint QCHUNKS = (BUD + QCH - 1 ) /QCH ;// 33 48inline constexprint QCHUNKS2 = (BUD + 31 ) /32 ;// 65: the same cells in chunks of 32 (qsa2.metal), the partials buffer's chunk count 49inline constexprint QPREP = QH + 2 * QKV + IH + 1 ;// qsa_prep threadgroups: q heads, k, v, indexer q, and the indexer-key group 50 51// derived sizes (floats unless said) 52inline constexprint HCD = HC * N ;// one residual row (hc_dim) = the PLE key width (10240) 53inline constexprint QFULL = QH * QD * 2 ;// attn_q: query and gate (12288) 54inline constexprint QOUT = QH * QD ;// the attention output row = attn_output's K (6144) 55inline constexprint KV = QKV * QD ;// attn_k / attn_v rows (512) 56inline constexprint IQR = IH * ID ;// indexer q_proj rows (512) 57inline constexprint GDN_FUSED = CONV + ZV + 2 * HV ;// the four GDN input projections as one row (16480) 58 59// the kernels' defaults are Flash-Next's: a macro is defined only for what this build changes 60inline std ::string prelude () { 61const strata ::core ::ModelGeometry d ; 62std ::string s ; 63 autodef = [& ](const char * name ,int64_t v ,int64_t dv ) { 64if (v != dv )s += std ::string ("#define ") + name + " " + std::to_string((long long) v) + " \n "; 65 }; 66def ("GEO_QH" ,G .n_head ,d .n_head ); 67def ("GEO_QKV" ,G .n_head_kv ,d .n_head_kv ); 68def ("GEO_BUD" ,G .idx_top_k + 3 ,d .idx_top_k + 3 ); 69def ("GEO_TOPK" ,G .n_expert_used ,d .n_expert_used ); 70def ("GEO_NJ" ,G .n_embd /256 ,d .n_embd /256 );// gr_norm2: elements per thread of one stream's row 71def ("GEO_GRU_NU" , (G .hc_lr /32 + 3 ) /4 , (d .hc_lr /32 + 3 ) /4 );// gr_up prefetch: units of 32 per lane (4 lanes a row) 72return s ; 73} 74 75}// namespace strata_metal::geo