okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
b2de2641f
main
1#pragma once 2 3#include "llama.h" 4 5#include <array> 6#include <bitset> 7#include <cassert> 8#include <cmath> 9 10// bump if necessary 11#define LLAMA_MAX_LAYERS 512 12#define LLAMA_MAX_EXPERTS 1024 // Kimi K3 13#define LLAMA_MAX_PLE_NGRAM 8 // qwen4exp 14#define LLAMA_MAX_PLE_HEADS 64 // qwen4exp 15 16enum llama_expert_gating_func_type { 17LLAMA_EXPERT_GATING_FUNC_TYPE_NONE = 0 , 18LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX = 1 , 19LLAMA_EXPERT_GATING_FUNC_TYPE_SIGMOID = 2 , 20LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX_WEIGHT = 3 ,// applied to the router weights instead of the logits 21LLAMA_EXPERT_GATING_FUNC_TYPE_SQRT_SOFTPLUS = 4 , 22}; 23 24enum llama_swa_type { 25LLAMA_SWA_TYPE_NONE = 0 , 26LLAMA_SWA_TYPE_STANDARD = 1 , 27LLAMA_SWA_TYPE_CHUNKED = 2 , 28LLAMA_SWA_TYPE_SYMMETRIC = 3 , 29}; 30 31// how the non-causal mask should be constructed with llama_set_causal_attn(ctx, false) 32// (e.g. mtmd decoding image tokens) 33enum llama_non_causal_type { 34LLAMA_NON_CAUSAL_TYPE_ALL = 0 ,// all layers non-causal, SWA still applied (gemma 3, qwen-vl, ...) 35LLAMA_NON_CAUSAL_TYPE_SWA_ONLY = 1 ,// SWA layers non-causal, dense layers stay causal (gemma 4) 36LLAMA_NON_CAUSAL_TYPE_SWA_FULL = 2 ,// all layers non-causal, SWA not applied between tokens of the current ubatch (deepseek 4) 37}; 38 39// forward declaration; full definition in llama-graph.h 40enum llm_ffn_op_type :int ; 41 42struct llama_hparams_posnet { 43uint32_t n_embd ; 44uint32_t n_layer ; 45}; 46 47struct llama_hparams_convnext { 48uint32_t n_embd ; 49uint32_t n_layer ; 50}; 51 52struct llama_hparams { 53// note: use the `_impl` suffix to avoid name conflict between members and getters 54// for example: n_embd_out() vs n_embd_out_impl 55 56bool vocab_only ; 57bool no_alloc ; 58bool rope_finetuned ; 59bool use_par_res ; 60bool swin_norm ; 61bool norm_before_residual = false; 62bool norm_before_fc = false; 63 64uint32_t n_ctx_train ;// context size the model was trained on 65uint32_t n_embd ; 66uint32_t n_layer_all ; 67uint32_t n_layer_nextn = 0 ; 68 69// granite-switch: index of the single-head "router" KV layer that encodes 70// per-token adapter selection. -1 when the model has no such layer. 71int32_t router_layer = -1 ; 72uint32_t n_expert = 0 ; 73uint32_t n_rel_attn_bkts = 0 ; 74 75// TODO: this needs to be reworked 76int32_t n_layer_kv_from_start = -1 ;// if non-negative, the first n_layer_kv_from_start layers have KV cache 77 78// different head size for full_attention and SWA layers 79uint32_t n_embd_head_k_full ;// dimension of keys (d_k). d_q is assumed to be the same, but there are n_head q heads, and only n_head_kv k-v heads 80uint32_t n_embd_head_v_full ;// dimension of values (d_v) aka n_embd_head 81uint32_t n_embd_head_k_swa ; 82uint32_t n_embd_head_v_swa ; 83 84// different RoPE dimensions for full_attention and SWA layers 85uint32_t n_rot_full ; 86uint32_t n_rot_swa ; 87 88// note: deepseek2 using MLA converts into MQA with larger heads, then decompresses to MHA 89uint32_t n_embd_head_k_mla_impl = 0 ; 90uint32_t n_embd_head_v_mla_impl = 0 ; 91 92// for WavTokenizer 93struct llama_hparams_posnet posnet ; 94struct llama_hparams_convnext convnext ; 95 96uint32_t n_shortconv_l_cache = 0 ; 97 98std ::array < uint32_t ,LLAMA_MAX_LAYERS > n_head_arr ; 99std ::array < uint32_t ,LLAMA_MAX_LAYERS > n_head_kv_arr ; 100std ::array < uint32_t ,LLAMA_MAX_LAYERS > n_ff_arr ; 101 102// per-layer expert feed-forward size 103std ::array < uint32_t ,LLAMA_MAX_LAYERS > n_ff_exp_arr ; 104// per-layer top-k expert routing count 105std ::array < uint32_t ,LLAMA_MAX_LAYERS > n_expert_used_arr ; 106 107uint32_t n_layer_dense_lead = 0 ; 108uint32_t n_lora_q = 0 ; 109uint32_t n_lora_kv = 0 ; 110uint32_t n_ff_shexp = 0 ; 111uint32_t n_ff_chexp = 0 ; 112uint32_t n_expert_shared = 0 ; 113uint32_t n_norm_groups = 0 ; 114uint32_t n_expert_groups = 0 ; 115uint32_t n_group_used = 0 ; 116uint32_t n_group_experts = 0 ; 117 118// MLA + SWA (i.e. dots3note) 119uint32_t n_lora_kv_swa = 0 ; 120uint32_t n_embd_head_k_mla_swa = 0 ; 121uint32_t n_embd_head_v_mla_swa = 0 ; 122 123float expert_group_scale = 0.05f ; 124float expert_weights_scale = 0.0f ; 125bool expert_weights_norm = false; 126uint32_t expert_gating_func = LLAMA_EXPERT_GATING_FUNC_TYPE_NONE ; 127uint32_t moe_every_n_layers = 0 ; 128uint32_t moe_latent_size = 0 ; 129 130float f_norm_eps ; 131float f_norm_rms_eps ; 132float f_norm_group_eps ; 133 134float f_attn_logit_softcapping = 50.0f ; 135float f_router_logit_softcapping = 30.0f ; 136float f_final_logit_softcapping = 30.0f ; 137 138// for RWKV 139uint32_t rescale_every_n_layers = 0 ; 140uint32_t time_mix_extra_dim = 0 ; 141uint32_t time_decay_extra_dim = 0 ; 142uint32_t wkv_head_size = 0 ; 143uint32_t token_shift_count = 2 ; 144uint32_t n_lora_decay = 0 ; 145uint32_t n_lora_iclr = 0 ; 146uint32_t n_lora_value_res_mix = 0 ; 147uint32_t n_lora_gate = 0 ; 148 149float rope_attn_factor = 1.0f ; 150float rope_freq_base_train ; 151float rope_freq_base_train_swa = 10000.0f ; 152float rope_freq_scale_train ; 153float rope_freq_scale_train_swa = 1.0f ; 154float rope_scaling_alpha = 0.0f ;// NTK-aware alpha for XDRoPE 155 156uint32_t n_ctx_orig_yarn ; 157float rope_yarn_log_mul = 0.0f ; 158 159float yarn_ext_factor = -1.0f ; 160float yarn_attn_factor = 1.0f ; 161float yarn_beta_fast = 32.0f ; 162float yarn_beta_slow = 1.0f ; 163 164std ::array < int ,4 > rope_sections ; 165 166// Per-layer RoPE enable flags (1 = use RoPE, 0 = NoPE) 167// by default, all layers use RoPE (controlled by rope_finetuned) 168std ::array < uint32_t ,LLAMA_MAX_LAYERS > rope_pattern ; 169 170// Sliding Window Attention (SWA) 171llama_swa_type swa_type = LLAMA_SWA_TYPE_NONE ; 172// the size of the sliding window (0 - no SWA) 173uint32_t n_swa = 0 ; 174 175// see llama_non_causal_type 176// note: for SWA_FULL, older tokens (outside the current ubatch) are still window-clipped 177llama_non_causal_type non_causal_type = LLAMA_NON_CAUSAL_TYPE_ALL ; 178 179// if is_swa_impl[il] == 1, then layer il is SWA 180// if is_swa_impl[il] == 0, then layer il is dense (i.e. non-SWA) 181// by default, all layers are dense 182// note: using uint32_t type for compatibility reason 183std ::array < uint32_t ,LLAMA_MAX_LAYERS > is_swa_impl ; 184 185// for hybrid state space models 186std ::array < uint32_t ,LLAMA_MAX_LAYERS > is_recr_impl ; 187 188// for State Space Models 189uint32_t ssm_d_conv = 0 ; 190uint32_t ssm_d_inner = 0 ; 191uint32_t ssm_d_state = 0 ; 192uint32_t ssm_dt_rank = 0 ; 193uint32_t ssm_n_group = 0 ; 194 195// for MiniMax-Text-01 linear attention 196uint32_t n_embd_head_la = 0 ; 197 198// for Kimi Linear KDA 199uint32_t n_embd_head_kda = 0 ; 200bool kda_safe_gate = false; 201 202// kimi-k3 203uint32_t n_expert_latent = 0 ;// routed_expert_hidden_size (0 = experts run at n_embd) 204uint32_t attn_res_block_size = 0 ;// 0 = no cross-layer attention residuals 205float kda_gate_lower_bound = - INFINITY ; 206float situ_beta = 1.0f ; 207float situ_linear_beta = 0.0f ;// 0 = no linear-beta transform on the up branch 208 209// hrm-text (looped H/L stacks) 210uint32_t n_hrm_layers_per_stack = 0 ; 211uint32_t n_hrm_h_cycles = 0 ; 212uint32_t n_hrm_l_cycles = 0 ; 213bool hrm_prefix_lm = false; 214 215bool ssm_dt_b_c_rms = false; 216 217float f_clamp_kqv = 0.0f ; 218float f_max_alibi_bias = 0.0f ; 219float f_logit_scale = 0.0f ; 220 221// Additional scale factors (Granite/Granite MoE) 222float f_residual_scale = 0.0f ; 223float f_embedding_scale = 0.0f ; 224float f_attention_scale = 0.0f ; 225 226// grok-2 227float f_attn_out_scale = 0.0f ; 228uint32_t attn_temp_length = 0 ; 229 230float f_attn_value_scale = 0.0f ; 231 232bool causal_attn = true; 233bool use_alibi = false; 234bool attn_soft_cap = false; 235bool use_kq_norm = false; 236 237// for Classifiers 238uint32_t n_cls_out = 1 ; 239 240// input embedding dimension (0 = use n_embd) 241uint32_t n_embd_inp_impl = 0 ; 242 243// encoder input embedding dimension (0 = use n_embd_inp()) 244// e.g. the eagle3 encoder fuses target_layers * target_hidden features 245uint32_t n_embd_inp_enc_impl = 0 ; 246 247// output embedding dimension (0 = use n_embd) 248uint32_t n_embd_out_impl = 0 ; 249 250uint32_t dflash_block_size = 0 ; 251uint32_t dflash_conv_kernel_size = 0 ; 252uint32_t dflash_conv_group_size = 0 ; 253uint32_t dflash_selector_rank = 0 ; 254uint32_t dflash_selector_top_k = 0 ; 255 256// llama4 smallthinker 257uint32_t n_moe_layer_step = 0 ; 258uint32_t n_no_rope_layer_step = 4 ; 259uint32_t n_attn_temp_floor_scale = 0 ; 260float f_attn_temp_scale = 0.0f ; 261float f_attn_temp_offset = 0.0f ;// offset position index 262 263// gemma3n altup 264uint32_t n_altup = 4 ;// altup_num_inputs 265uint32_t i_altup_act = 0 ;// altup_active_idx 266uint32_t laurel_rank = 64 ; 267uint32_t n_embd_altup = 256 ; 268 269// needed for sentence-transformers dense layers 270uint32_t dense_2_feat_in = 0 ;// in_features of the 2_Dense 271uint32_t dense_2_feat_out = 0 ;// out_features of the 2_Dense 272uint32_t dense_3_feat_in = 0 ;// in_features of the 3_Dense 273uint32_t dense_3_feat_out = 0 ;// out_features of the 3_Dense 274 275// xIELU 276std ::array < float ,LLAMA_MAX_LAYERS > xielu_alpha_n ; 277std ::array < float ,LLAMA_MAX_LAYERS > xielu_alpha_p ; 278std ::array < float ,LLAMA_MAX_LAYERS > xielu_beta ; 279std ::array < float ,LLAMA_MAX_LAYERS > xielu_eps ; 280 281// DSA (deepseek sparse attention) 282uint32_t indexer_n_head = 0 ; 283uint32_t indexer_head_size = 0 ; 284uint32_t indexer_top_k = 0 ; 285// MSA 286uint32_t indexer_block_size = 0 ; 287uint32_t indexer_local_blocks = 0 ; 288 289// Indexer is "full" (1) or "shared" (0) 290// Shared indexers reuse top-k from previous full layer 291std ::array < uint32_t ,LLAMA_MAX_LAYERS > is_indexer_full_impl ; 292 293// DeepSeek-V4 294uint32_t dsv4_o_group_count = 0 ; 295uint32_t dsv4_o_lora_rank = 0 ; 296uint32_t dsv4_hc_mult = 0 ; 297uint32_t dsv4_hc_sinkhorn_iters = 0 ; 298uint32_t dsv4_hash_layer_count = 0 ; 299float dsv4_compress_rope_base = 0.0f ; 300float dsv4_hc_eps = 0.0f ; 301std ::array < uint32_t ,LLAMA_MAX_LAYERS > dsv4_compress_ratios ; 302 303// 0 = full rank (DeepSeek-V4) 304uint32_t hc_low_rank = 0 ; 305 306// scale of the hyper-connection post gate (DeepSeek-V4 hardcodes 2.0) 307float hc_magnitude = 0.0f ; 308 309uint32_t ple_ngram_size = 0 ; 310uint32_t ple_heads_per_ngram = 0 ; 311uint32_t ple_conv_kernel = 0 ; 312uint32_t ple_n_heads = 0 ;// (ngram_size - 1) * heads_per_ngram 313uint32_t ple_head_dim = 0 ; 314uint32_t ple_eos_token_id = 0 ; 315// the id the PLE hash stands in at image positions; 0 makes the loader fall back to EOS 316uint32_t ple_image_token_id = 0 ; 317// the file lists PLE layer indices, so this is never a per-layer gguf array and can hold one bit per layer 318std ::bitset < LLAMA_MAX_LAYERS > is_ple_impl ; 319// the hash multipliers reach ~2e13 and have to stay 64-bit 320std ::array < uint64_t ,LLAMA_MAX_PLE_NGRAM > ple_layer_multipliers ; 321// head offsets and vocab sizes are token-space indices; the gather truncates them to int32 anyway 322std ::array < uint32_t ,LLAMA_MAX_PLE_HEADS > ple_head_offsets ; 323std ::array < uint32_t ,LLAMA_MAX_PLE_HEADS > ple_head_vocab_sizes ; 324 325bool is_ple (uint32_t il )const ; 326 327// PLE conv history rows: (kernel - 1) * ngram_size; 0 without a PLE module 328uint32_t ple_conv_state ()const ; 329 330// qwen3vl deepstack 331// When parsed from GGUF, this implies the first N layers consume the first 332// N deepstack embeddings. Use deepstack_mapping_arr if you need a more 333// complex mapping. If using deepstack_mapping_arr, also make sure to set 334// n_deepstack_layers to the number of unique deepstack layers so that 335// n_embd_imp is accurate (see granite.cpp). 336// TODO: can be expressed via the `new n_embd_inp_impl` and remove this param 337uint32_t n_deepstack_layers = 0 ; 338 339// deepstack layer array (Granite4 Vision) 340// -1 => no deepstack 341// >=0 => input embedding index for deepstack injection 342std ::array < int32_t ,LLAMA_MAX_LAYERS > deepstack_mapping_arr ; 343 344// gemma4 per-layer embedding 345uint32_t n_embd_per_layer = 0 ; 346 347// needed by encoder-decoder models (e.g. T5, FLAN-T5) 348// ref: https://github.com/ggml-org/llama.cpp/pull/8141 349llama_token dec_start_token_id = LLAMA_TOKEN_NULL ; 350uint32_t dec_n_layer = 0 ; 351 352enum llama_pooling_type pooling_type = LLAMA_POOLING_TYPE_NONE ; 353enum llama_rope_type rope_type = LLAMA_ROPE_TYPE_NONE ; 354enum llama_rope_scaling_type rope_scaling_type_train = LLAMA_ROPE_SCALING_TYPE_NONE ; 355 356 357// Resolved FFN gated activation flavor for archs that read 358// `<arch>.hidden_activation` from the GGUF (e.g. ModernBert derivatives). 359// Defaults to LLM_FFN_NONE (sentinel = 0); the mapping from the GGUF 360// string to a real op is done at hparam-load time via 361// llm_ffn_op_type_from_string() in llama-model.cpp, mirroring how 362// rope_scaling_type_train is handled. 363enum llm_ffn_op_type llm_ffn_op ; 364 365// Step35: optional per-layer clamps for (Swi)GLU 366std ::array < float ,LLAMA_MAX_LAYERS > swiglu_clamp_exp ;// clamping for expert FFN 367std ::array < float ,LLAMA_MAX_LAYERS > swiglu_clamp_shexp ;// shared expert 368 369// this value n_pattern means that every nth layer is dense (i.e. non-SWA) 370// dense_first means whether the pattern is start with a dense layer 371// note that if n_pattern == 0, all layers are SWA 372// if n_pattern == 1, all layers are dense 373// example 1: n_pattern = 3, dense_first = false 374// il == 0: swa 375// il == 1: swa 376// il == 2: dense 377// il == 3: swa 378// il == 4: swa 379// il == 5: dense 380// il == 6: swa 381// etc ... 382// example 2: n_pattern = 2, dense_first = true 383// il == 0: dense 384// il == 1: swa 385// il == 2: dense 386// il == 3: swa 387// etc ... 388void set_swa_pattern (uint32_t n_pattern ,bool dense_first = false); 389 390// return true if one of the layers is SWA 391bool is_swa_any ()const ; 392 393bool is_swa (uint32_t il )const ; 394 395bool is_indexer_full (uint32_t il )const ; 396 397void set_recr_pattern (uint32_t n_pattern ,bool dense_first = false); 398 399// whether or not the given layer is recurrent (for hybrid models) 400bool is_recr (uint32_t il )const ; 401 402uint32_t n_head (uint32_t il = 0 )const ; 403 404uint32_t n_head_kv (uint32_t il = 0 )const ; 405 406uint32_t n_ff (uint32_t il = 0 )const ; 407 408uint32_t n_ff_exp (uint32_t il = 0 )const ; 409 410uint32_t n_expert_used (uint32_t il = 0 )const ; 411 412// return the maximum n_expert_used across all layers 413uint32_t n_expert_used_max ()const ; 414 415uint32_t n_gqa (uint32_t il = 0 )const ; 416 417uint32_t n_rot (uint32_t il = 0 )const ; 418 419// dimension of main + auxiliary input embeddings 420uint32_t n_embd_inp ()const ; 421 422// dimension of the encoder input embeddings 423uint32_t n_embd_inp_enc ()const ; 424 425// dimension of output embeddings 426uint32_t n_embd_out ()const ; 427 428// dimension of key/value embeddings for each head (per layer) 429uint32_t n_embd_head_k (uint32_t il = 0 )const ; 430uint32_t n_embd_head_v (uint32_t il = 0 )const ; 431 432// dimension of key embeddings across all k-v heads 433uint32_t n_embd_k_gqa (uint32_t il = 0 )const ; 434 435// dimension of value embeddings across all k-v heads 436uint32_t n_embd_v_gqa (uint32_t il = 0 )const ; 437 438// true if any layer has a different n_embd_k_gqa/n_embd_v_gqa 439bool is_n_embd_k_gqa_variable ()const ; 440bool is_n_embd_v_gqa_variable ()const ; 441 442// return the maximum n_embd_k_gqa/n_embd_v_gqa across all layers 443uint32_t n_embd_k_gqa_max ()const ; 444uint32_t n_embd_v_gqa_max ()const ; 445 446// dimension of the rolling state embeddings 447// corresponds to Mamba's conv_states size or RWKV's token_shift states size 448uint32_t n_embd_r ()const ; 449 450// dimension of the recurrent state embeddings 451uint32_t n_embd_s ()const ; 452 453uint32_t n_pos_per_embd ()const ; 454 455// note: currently only support if either all or none of the layers are MLA 456bool is_mla ()const ; 457 458uint32_t n_embd_head_k_mla ()const ; 459uint32_t n_embd_head_v_mla ()const ; 460 461bool has_kv (uint32_t il )const ; 462 463bool has_rope (uint32_t il )const ; 464 465// number of effective layers (excludes nextn layers) 466uint32_t n_layer ()const ; 467 468// note that this function uses different SWA parameters from those in the hparams 469// note: inlined on purpose for performance reasons 470// TODO: think of a better place for this function 471// TODO: pack the SWA params in a struct? 472static bool is_masked_swa (uint32_t n_swa ,llama_swa_type swa_type ,llama_pos p0 ,llama_pos p1 ) { 473assert (p0 >=0 && p1 >=0 ); 474 475switch (swa_type ) { 476case LLAMA_SWA_TYPE_NONE : 477 { 478 }break ; 479case LLAMA_SWA_TYPE_STANDARD : 480 { 481if (p1 - p0 >= (int32_t )n_swa ) { 482return true; 483 } 484 }break ; 485case LLAMA_SWA_TYPE_CHUNKED : 486 { 487const llama_pos pos_chunk_start = (p1 /n_swa )* n_swa ; 488 489if (p0 < pos_chunk_start ) { 490return true; 491 } 492 }break ; 493case LLAMA_SWA_TYPE_SYMMETRIC : 494 { 495const int32_t half_n_swa = (int32_t )n_swa /2 ; 496const int32_t pos_diff = p1 - p0 ; 497 498// Mask if outside the symmetric window 499if (pos_diff < - half_n_swa || pos_diff > half_n_swa ) { 500return true; 501 } 502 }break ; 503 } 504 505return false; 506 } 507 508 509bool use_mrope ()const ; 510}; 511 512static_assert (std ::is_trivially_copyable < llama_hparams > ::value ,"llama_hparams must be trivially copyable" );