char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
49c0dc82b
char/aliceai-t5-moe
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 209bool ssm_dt_b_c_rms = false; 210 211float f_clamp_kqv = 0.0f ; 212float f_max_alibi_bias = 0.0f ; 213float f_logit_scale = 0.0f ; 214 215// Additional scale factors (Granite/Granite MoE) 216float f_residual_scale = 0.0f ; 217float f_embedding_scale = 0.0f ; 218float f_attention_scale = 0.0f ; 219 220// grok-2 221float f_attn_out_scale = 0.0f ; 222uint32_t attn_temp_length = 0 ; 223 224float f_attn_value_scale = 0.0f ; 225 226bool causal_attn = true; 227bool use_alibi = false; 228bool attn_soft_cap = false; 229bool use_kq_norm = false; 230 231// for Classifiers 232uint32_t n_cls_out = 1 ; 233 234// input embedding dimension (0 = use n_embd) 235uint32_t n_embd_inp_impl = 0 ; 236 237// encoder input embedding dimension (0 = use n_embd_inp()) 238// e.g. the eagle3 encoder fuses target_layers * target_hidden features 239uint32_t n_embd_inp_enc_impl = 0 ; 240 241// output embedding dimension (0 = use n_embd) 242uint32_t n_embd_out_impl = 0 ; 243 244uint32_t dflash_block_size = 0 ; 245uint32_t dflash_conv_kernel_size = 0 ; 246uint32_t dflash_conv_group_size = 0 ; 247uint32_t dflash_selector_rank = 0 ; 248uint32_t dflash_selector_top_k = 0 ; 249 250// llama4 smallthinker 251uint32_t n_moe_layer_step = 0 ; 252uint32_t n_no_rope_layer_step = 4 ; 253uint32_t n_attn_temp_floor_scale = 0 ; 254float f_attn_temp_scale = 0.0f ; 255float f_attn_temp_offset = 0.0f ;// offset position index 256 257// gemma3n altup 258uint32_t n_altup = 4 ;// altup_num_inputs 259uint32_t i_altup_act = 0 ;// altup_active_idx 260uint32_t laurel_rank = 64 ; 261uint32_t n_embd_altup = 256 ; 262 263// needed for sentence-transformers dense layers 264uint32_t dense_2_feat_in = 0 ;// in_features of the 2_Dense 265uint32_t dense_2_feat_out = 0 ;// out_features of the 2_Dense 266uint32_t dense_3_feat_in = 0 ;// in_features of the 3_Dense 267uint32_t dense_3_feat_out = 0 ;// out_features of the 3_Dense 268 269// xIELU 270std ::array < float ,LLAMA_MAX_LAYERS > xielu_alpha_n ; 271std ::array < float ,LLAMA_MAX_LAYERS > xielu_alpha_p ; 272std ::array < float ,LLAMA_MAX_LAYERS > xielu_beta ; 273std ::array < float ,LLAMA_MAX_LAYERS > xielu_eps ; 274 275// DSA (deepseek sparse attention) 276uint32_t indexer_n_head = 0 ; 277uint32_t indexer_head_size = 0 ; 278uint32_t indexer_top_k = 0 ; 279// MSA 280uint32_t indexer_block_size = 0 ; 281uint32_t indexer_local_blocks = 0 ; 282 283// Indexer is "full" (1) or "shared" (0) 284// Shared indexers reuse top-k from previous full layer 285std ::array < uint32_t ,LLAMA_MAX_LAYERS > is_indexer_full_impl ; 286 287// DeepSeek-V4 288uint32_t dsv4_o_group_count = 0 ; 289uint32_t dsv4_o_lora_rank = 0 ; 290uint32_t dsv4_hc_mult = 0 ; 291uint32_t dsv4_hc_sinkhorn_iters = 0 ; 292uint32_t dsv4_hash_layer_count = 0 ; 293float dsv4_compress_rope_base = 0.0f ; 294float dsv4_hc_eps = 0.0f ; 295std ::array < uint32_t ,LLAMA_MAX_LAYERS > dsv4_compress_ratios ; 296 297// 0 = full rank (DeepSeek-V4) 298uint32_t hc_low_rank = 0 ; 299 300// scale of the hyper-connection post gate (DeepSeek-V4 hardcodes 2.0) 301float hc_magnitude = 0.0f ; 302 303uint32_t ple_ngram_size = 0 ; 304uint32_t ple_heads_per_ngram = 0 ; 305uint32_t ple_conv_kernel = 0 ; 306uint32_t ple_n_heads = 0 ;// (ngram_size - 1) * heads_per_ngram 307uint32_t ple_head_dim = 0 ; 308uint32_t ple_eos_token_id = 0 ; 309// the id the PLE hash stands in at image positions; 0 makes the loader fall back to EOS 310uint32_t ple_image_token_id = 0 ; 311// the file lists PLE layer indices, so this is never a per-layer gguf array and can hold one bit per layer 312std ::bitset < LLAMA_MAX_LAYERS > is_ple_impl ; 313// the hash multipliers reach ~2e13 and have to stay 64-bit 314std ::array < uint64_t ,LLAMA_MAX_PLE_NGRAM > ple_layer_multipliers ; 315// head offsets and vocab sizes are token-space indices; the gather truncates them to int32 anyway 316std ::array < uint32_t ,LLAMA_MAX_PLE_HEADS > ple_head_offsets ; 317std ::array < uint32_t ,LLAMA_MAX_PLE_HEADS > ple_head_vocab_sizes ; 318 319bool is_ple (uint32_t il )const ; 320 321// PLE conv history rows: (kernel - 1) * ngram_size; 0 without a PLE module 322uint32_t ple_conv_state ()const ; 323 324// qwen3vl deepstack 325// When parsed from GGUF, this implies the first N layers consume the first 326// N deepstack embeddings. Use deepstack_mapping_arr if you need a more 327// complex mapping. If using deepstack_mapping_arr, also make sure to set 328// n_deepstack_layers to the number of unique deepstack layers so that 329// n_embd_imp is accurate (see granite.cpp). 330// TODO: can be expressed via the `new n_embd_inp_impl` and remove this param 331uint32_t n_deepstack_layers = 0 ; 332 333// deepstack layer array (Granite4 Vision) 334// -1 => no deepstack 335// >=0 => input embedding index for deepstack injection 336std ::array < int32_t ,LLAMA_MAX_LAYERS > deepstack_mapping_arr ; 337 338// gemma4 per-layer embedding 339uint32_t n_embd_per_layer = 0 ; 340 341// needed by encoder-decoder models (e.g. T5, FLAN-T5) 342// ref: https://github.com/ggml-org/llama.cpp/pull/8141 343llama_token dec_start_token_id = LLAMA_TOKEN_NULL ; 344uint32_t dec_n_layer = 0 ; 345 346enum llama_pooling_type pooling_type = LLAMA_POOLING_TYPE_NONE ; 347enum llama_rope_type rope_type = LLAMA_ROPE_TYPE_NONE ; 348enum llama_rope_scaling_type rope_scaling_type_train = LLAMA_ROPE_SCALING_TYPE_NONE ; 349 350 351// Resolved FFN gated activation flavor for archs that read 352// `<arch>.hidden_activation` from the GGUF (e.g. ModernBert derivatives). 353// Defaults to LLM_FFN_NONE (sentinel = 0); the mapping from the GGUF 354// string to a real op is done at hparam-load time via 355// llm_ffn_op_type_from_string() in llama-model.cpp, mirroring how 356// rope_scaling_type_train is handled. 357enum llm_ffn_op_type llm_ffn_op ; 358 359// Step35: optional per-layer clamps for (Swi)GLU 360std ::array < float ,LLAMA_MAX_LAYERS > swiglu_clamp_exp ;// clamping for expert FFN 361std ::array < float ,LLAMA_MAX_LAYERS > swiglu_clamp_shexp ;// shared expert 362 363// this value n_pattern means that every nth layer is dense (i.e. non-SWA) 364// dense_first means whether the pattern is start with a dense layer 365// note that if n_pattern == 0, all layers are SWA 366// if n_pattern == 1, all layers are dense 367// example 1: n_pattern = 3, dense_first = false 368// il == 0: swa 369// il == 1: swa 370// il == 2: dense 371// il == 3: swa 372// il == 4: swa 373// il == 5: dense 374// il == 6: swa 375// etc ... 376// example 2: n_pattern = 2, dense_first = true 377// il == 0: dense 378// il == 1: swa 379// il == 2: dense 380// il == 3: swa 381// etc ... 382void set_swa_pattern (uint32_t n_pattern ,bool dense_first = false); 383 384// return true if one of the layers is SWA 385bool is_swa_any ()const ; 386 387bool is_swa (uint32_t il )const ; 388 389bool is_indexer_full (uint32_t il )const ; 390 391void set_recr_pattern (uint32_t n_pattern ,bool dense_first = false); 392 393// whether or not the given layer is recurrent (for hybrid models) 394bool is_recr (uint32_t il )const ; 395 396uint32_t n_head (uint32_t il = 0 )const ; 397 398uint32_t n_head_kv (uint32_t il = 0 )const ; 399 400uint32_t n_ff (uint32_t il = 0 )const ; 401 402uint32_t n_ff_exp (uint32_t il = 0 )const ; 403 404uint32_t n_expert_used (uint32_t il = 0 )const ; 405 406// return the maximum n_expert_used across all layers 407uint32_t n_expert_used_max ()const ; 408 409uint32_t n_gqa (uint32_t il = 0 )const ; 410 411uint32_t n_rot (uint32_t il = 0 )const ; 412 413// dimension of main + auxiliary input embeddings 414uint32_t n_embd_inp ()const ; 415 416// dimension of the encoder input embeddings 417uint32_t n_embd_inp_enc ()const ; 418 419// dimension of output embeddings 420uint32_t n_embd_out ()const ; 421 422// dimension of key/value embeddings for each head (per layer) 423uint32_t n_embd_head_k (uint32_t il = 0 )const ; 424uint32_t n_embd_head_v (uint32_t il = 0 )const ; 425 426// dimension of key embeddings across all k-v heads 427uint32_t n_embd_k_gqa (uint32_t il = 0 )const ; 428 429// dimension of value embeddings across all k-v heads 430uint32_t n_embd_v_gqa (uint32_t il = 0 )const ; 431 432// true if any layer has a different n_embd_k_gqa/n_embd_v_gqa 433bool is_n_embd_k_gqa_variable ()const ; 434bool is_n_embd_v_gqa_variable ()const ; 435 436// return the maximum n_embd_k_gqa/n_embd_v_gqa across all layers 437uint32_t n_embd_k_gqa_max ()const ; 438uint32_t n_embd_v_gqa_max ()const ; 439 440// dimension of the rolling state embeddings 441// corresponds to Mamba's conv_states size or RWKV's token_shift states size 442uint32_t n_embd_r ()const ; 443 444// dimension of the recurrent state embeddings 445uint32_t n_embd_s ()const ; 446 447uint32_t n_pos_per_embd ()const ; 448 449// note: currently only support if either all or none of the layers are MLA 450bool is_mla ()const ; 451 452uint32_t n_embd_head_k_mla ()const ; 453uint32_t n_embd_head_v_mla ()const ; 454 455bool has_kv (uint32_t il )const ; 456 457bool has_rope (uint32_t il )const ; 458 459// number of effective layers (excludes nextn layers) 460uint32_t n_layer ()const ; 461 462// note that this function uses different SWA parameters from those in the hparams 463// note: inlined on purpose for performance reasons 464// TODO: think of a better place for this function 465// TODO: pack the SWA params in a struct? 466static bool is_masked_swa (uint32_t n_swa ,llama_swa_type swa_type ,llama_pos p0 ,llama_pos p1 ) { 467assert (p0 >=0 && p1 >=0 ); 468 469switch (swa_type ) { 470case LLAMA_SWA_TYPE_NONE : 471 { 472 }break ; 473case LLAMA_SWA_TYPE_STANDARD : 474 { 475if (p1 - p0 >= (int32_t )n_swa ) { 476return true; 477 } 478 }break ; 479case LLAMA_SWA_TYPE_CHUNKED : 480 { 481const llama_pos pos_chunk_start = (p1 /n_swa )* n_swa ; 482 483if (p0 < pos_chunk_start ) { 484return true; 485 } 486 }break ; 487case LLAMA_SWA_TYPE_SYMMETRIC : 488 { 489const int32_t half_n_swa = (int32_t )n_swa /2 ; 490const int32_t pos_diff = p1 - p0 ; 491 492// Mask if outside the symmetric window 493if (pos_diff < - half_n_swa || pos_diff > half_n_swa ) { 494return true; 495 } 496 }break ; 497 } 498 499return false; 500 } 501 502 503bool use_mrope ()const ; 504}; 505 506static_assert (std ::is_trivially_copyable < llama_hparams > ::value ,"llama_hparams must be trivially copyable" );