char-fork/llama.cpp

git clone https://git.t4t.associates/char-fork/llama.cpp

HongHuangmodel : add Tencent Hy 4 (hy_v4) preview architecture support (#28127)49c0dc82b

char/aliceai-t5-moe
17.6 KiB506 linesraw
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 {
17    LLAMA_EXPERT_GATING_FUNC_TYPE_NONE           = 0,
18    LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX        = 1,
19    LLAMA_EXPERT_GATING_FUNC_TYPE_SIGMOID        = 2,
20    LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX_WEIGHT = 3, // applied to the router weights instead of the logits
21    LLAMA_EXPERT_GATING_FUNC_TYPE_SQRT_SOFTPLUS  = 4,
22};
23
24enum llama_swa_type {
25    LLAMA_SWA_TYPE_NONE      = 0,
26    LLAMA_SWA_TYPE_STANDARD  = 1,
27    LLAMA_SWA_TYPE_CHUNKED   = 2,
28    LLAMA_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 {
34    LLAMA_NON_CAUSAL_TYPE_ALL      = 0, // all layers non-causal, SWA still applied (gemma 3, qwen-vl, ...)
35    LLAMA_NON_CAUSAL_TYPE_SWA_ONLY = 1, // SWA layers non-causal, dense layers stay causal (gemma 4)
36    LLAMA_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 {
43    uint32_t n_embd;
44    uint32_t n_layer;
45};
46
47struct llama_hparams_convnext {
48    uint32_t n_embd;
49    uint32_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
56    bool vocab_only;
57    bool no_alloc;
58    bool rope_finetuned;
59    bool use_par_res;
60    bool swin_norm;
61    bool norm_before_residual = false;
62    bool norm_before_fc       = false;
63
64    uint32_t n_ctx_train; // context size the model was trained on
65    uint32_t n_embd;
66    uint32_t n_layer_all;
67    uint32_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.
71    int32_t  router_layer = -1;
72    uint32_t n_expert = 0;
73    uint32_t n_rel_attn_bkts = 0;
74
75    // TODO: this needs to be reworked
76    int32_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
79    uint32_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
80    uint32_t n_embd_head_v_full; // dimension of values (d_v) aka n_embd_head
81    uint32_t n_embd_head_k_swa;
82    uint32_t n_embd_head_v_swa;
83
84    // different RoPE dimensions for full_attention and SWA layers
85    uint32_t n_rot_full;
86    uint32_t n_rot_swa;
87
88    // note: deepseek2 using MLA converts into MQA with larger heads, then decompresses to MHA
89    uint32_t n_embd_head_k_mla_impl = 0;
90    uint32_t n_embd_head_v_mla_impl = 0;
91
92    // for WavTokenizer
93    struct llama_hparams_posnet   posnet;
94    struct llama_hparams_convnext convnext;
95
96    uint32_t n_shortconv_l_cache  = 0;
97
98    std::array<uint32_t, LLAMA_MAX_LAYERS> n_head_arr;
99    std::array<uint32_t, LLAMA_MAX_LAYERS> n_head_kv_arr;
100    std::array<uint32_t, LLAMA_MAX_LAYERS> n_ff_arr;
101
102    // per-layer expert feed-forward size
103    std::array<uint32_t, LLAMA_MAX_LAYERS> n_ff_exp_arr;
104    // per-layer top-k expert routing count
105    std::array<uint32_t, LLAMA_MAX_LAYERS> n_expert_used_arr;
106
107    uint32_t n_layer_dense_lead = 0;
108    uint32_t n_lora_q           = 0;
109    uint32_t n_lora_kv          = 0;
110    uint32_t n_ff_shexp         = 0;
111    uint32_t n_ff_chexp         = 0;
112    uint32_t n_expert_shared    = 0;
113    uint32_t n_norm_groups      = 0;
114    uint32_t n_expert_groups    = 0;
115    uint32_t n_group_used       = 0;
116    uint32_t n_group_experts    = 0;
117
118    // MLA + SWA (i.e. dots3note)
119    uint32_t n_lora_kv_swa           = 0;
120    uint32_t n_embd_head_k_mla_swa   = 0;
121    uint32_t n_embd_head_v_mla_swa   = 0;
122
123    float    expert_group_scale   = 0.05f;
124    float    expert_weights_scale = 0.0f;
125    bool     expert_weights_norm  = false;
126    uint32_t expert_gating_func   = LLAMA_EXPERT_GATING_FUNC_TYPE_NONE;
127    uint32_t moe_every_n_layers   = 0;
128    uint32_t moe_latent_size      = 0;
129
130    float f_norm_eps;
131    float f_norm_rms_eps;
132    float f_norm_group_eps;
133
134    float f_attn_logit_softcapping   = 50.0f;
135    float f_router_logit_softcapping = 30.0f;
136    float f_final_logit_softcapping  = 30.0f;
137
138    // for RWKV
139    uint32_t rescale_every_n_layers = 0;
140    uint32_t time_mix_extra_dim     = 0;
141    uint32_t time_decay_extra_dim   = 0;
142    uint32_t wkv_head_size          = 0;
143    uint32_t token_shift_count      = 2;
144    uint32_t n_lora_decay           = 0;
145    uint32_t n_lora_iclr            = 0;
146    uint32_t n_lora_value_res_mix   = 0;
147    uint32_t n_lora_gate            = 0;
148
149    float    rope_attn_factor = 1.0f;
150    float    rope_freq_base_train;
151    float    rope_freq_base_train_swa  = 10000.0f;
152    float    rope_freq_scale_train;
153    float    rope_freq_scale_train_swa = 1.0f;
154    float    rope_scaling_alpha        = 0.0f;  // NTK-aware alpha for XDRoPE
155
156    uint32_t n_ctx_orig_yarn;
157    float    rope_yarn_log_mul = 0.0f;
158
159    float    yarn_ext_factor  = -1.0f;
160    float    yarn_attn_factor =  1.0f;
161    float    yarn_beta_fast   = 32.0f;
162    float    yarn_beta_slow   =  1.0f;
163
164    std::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)
168    std::array<uint32_t, LLAMA_MAX_LAYERS> rope_pattern;
169
170    // Sliding Window Attention (SWA)
171    llama_swa_type swa_type = LLAMA_SWA_TYPE_NONE;
172    // the size of the sliding window (0 - no SWA)
173    uint32_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
177    llama_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
183    std::array<uint32_t, LLAMA_MAX_LAYERS> is_swa_impl;
184
185    // for hybrid state space models
186    std::array<uint32_t, LLAMA_MAX_LAYERS> is_recr_impl;
187
188    // for State Space Models
189    uint32_t ssm_d_conv  = 0;
190    uint32_t ssm_d_inner = 0;
191    uint32_t ssm_d_state = 0;
192    uint32_t ssm_dt_rank = 0;
193    uint32_t ssm_n_group = 0;
194
195    // for MiniMax-Text-01 linear attention
196    uint32_t n_embd_head_la = 0;
197
198    // for Kimi Linear KDA
199    uint32_t n_embd_head_kda = 0;
200    bool     kda_safe_gate = false;
201
202    // kimi-k3
203    uint32_t n_expert_latent      = 0;      // routed_expert_hidden_size (0 = experts run at n_embd)
204    uint32_t attn_res_block_size  = 0;      // 0 = no cross-layer attention residuals
205    float    kda_gate_lower_bound = -INFINITY;
206    float    situ_beta            = 1.0f;
207    float    situ_linear_beta     = 0.0f;   // 0 = no linear-beta transform on the up branch
208
209    bool ssm_dt_b_c_rms = false;
210
211    float f_clamp_kqv      = 0.0f;
212    float f_max_alibi_bias = 0.0f;
213    float f_logit_scale    = 0.0f;
214
215    // Additional scale factors (Granite/Granite MoE)
216    float f_residual_scale  = 0.0f;
217    float f_embedding_scale = 0.0f;
218    float f_attention_scale = 0.0f;
219
220    // grok-2
221    float    f_attn_out_scale = 0.0f;
222    uint32_t attn_temp_length = 0;
223
224    float    f_attn_value_scale = 0.0f;
225
226    bool causal_attn   = true;
227    bool use_alibi     = false;
228    bool attn_soft_cap = false;
229    bool use_kq_norm   = false;
230
231    // for Classifiers
232    uint32_t n_cls_out = 1;
233
234    // input embedding dimension (0 = use n_embd)
235    uint32_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
239    uint32_t n_embd_inp_enc_impl = 0;
240
241    // output embedding dimension (0 = use n_embd)
242    uint32_t n_embd_out_impl = 0;
243
244    uint32_t dflash_block_size       = 0;
245    uint32_t dflash_conv_kernel_size = 0;
246    uint32_t dflash_conv_group_size  = 0;
247    uint32_t dflash_selector_rank    = 0;
248    uint32_t dflash_selector_top_k   = 0;
249
250    // llama4 smallthinker
251    uint32_t n_moe_layer_step        = 0;
252    uint32_t n_no_rope_layer_step    = 4;
253    uint32_t n_attn_temp_floor_scale = 0;
254    float    f_attn_temp_scale       = 0.0f;
255    float    f_attn_temp_offset      = 0.0f; // offset position index
256
257    // gemma3n altup
258    uint32_t n_altup      = 4; // altup_num_inputs
259    uint32_t i_altup_act  = 0; // altup_active_idx
260    uint32_t laurel_rank  = 64;
261    uint32_t n_embd_altup = 256;
262
263    // needed for sentence-transformers dense layers
264    uint32_t dense_2_feat_in  = 0;  // in_features of the 2_Dense
265    uint32_t dense_2_feat_out = 0;  // out_features of the 2_Dense
266    uint32_t dense_3_feat_in  = 0;  // in_features of the 3_Dense
267    uint32_t dense_3_feat_out = 0;  // out_features of the 3_Dense
268
269    // xIELU
270    std::array<float, LLAMA_MAX_LAYERS> xielu_alpha_n;
271    std::array<float, LLAMA_MAX_LAYERS> xielu_alpha_p;
272    std::array<float, LLAMA_MAX_LAYERS> xielu_beta;
273    std::array<float, LLAMA_MAX_LAYERS> xielu_eps;
274
275    // DSA (deepseek sparse attention)
276    uint32_t indexer_n_head    = 0;
277    uint32_t indexer_head_size = 0;
278    uint32_t indexer_top_k     = 0;
279    // MSA
280    uint32_t indexer_block_size  = 0;
281    uint32_t indexer_local_blocks = 0;
282
283    // Indexer is "full" (1) or "shared" (0)
284    // Shared indexers reuse top-k from previous full layer
285    std::array<uint32_t, LLAMA_MAX_LAYERS> is_indexer_full_impl;
286
287    // DeepSeek-V4
288    uint32_t dsv4_o_group_count        = 0;
289    uint32_t dsv4_o_lora_rank          = 0;
290    uint32_t dsv4_hc_mult              = 0;
291    uint32_t dsv4_hc_sinkhorn_iters    = 0;
292    uint32_t dsv4_hash_layer_count     = 0;
293    float    dsv4_compress_rope_base   = 0.0f;
294    float    dsv4_hc_eps               = 0.0f;
295    std::array<uint32_t, LLAMA_MAX_LAYERS> dsv4_compress_ratios;
296
297    // 0 = full rank (DeepSeek-V4)
298    uint32_t hc_low_rank = 0;
299
300    // scale of the hyper-connection post gate (DeepSeek-V4 hardcodes 2.0)
301    float    hc_magnitude = 0.0f;
302
303    uint32_t ple_ngram_size      = 0;
304    uint32_t ple_heads_per_ngram = 0;
305    uint32_t ple_conv_kernel     = 0;
306    uint32_t ple_n_heads         = 0;   // (ngram_size - 1) * heads_per_ngram
307    uint32_t ple_head_dim        = 0;
308    uint32_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
310    uint32_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
312    std::bitset<LLAMA_MAX_LAYERS> is_ple_impl;
313    // the hash multipliers reach ~2e13 and have to stay 64-bit
314    std::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
316    std::array<uint32_t, LLAMA_MAX_PLE_HEADS>  ple_head_offsets;
317    std::array<uint32_t, LLAMA_MAX_PLE_HEADS>  ple_head_vocab_sizes;
318
319    bool is_ple(uint32_t il) const;
320
321    // PLE conv history rows: (kernel - 1) * ngram_size; 0 without a PLE module
322    uint32_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
331    uint32_t n_deepstack_layers = 0;
332
333    // deepstack layer array (Granite4 Vision)
334    // -1  => no deepstack
335    // >=0 => input embedding index for deepstack injection
336    std::array<int32_t, LLAMA_MAX_LAYERS> deepstack_mapping_arr;
337
338    // gemma4 per-layer embedding
339    uint32_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
343    llama_token dec_start_token_id = LLAMA_TOKEN_NULL;
344    uint32_t    dec_n_layer        = 0;
345
346    enum llama_pooling_type      pooling_type            = LLAMA_POOLING_TYPE_NONE;
347    enum llama_rope_type         rope_type               = LLAMA_ROPE_TYPE_NONE;
348    enum 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.
357    enum llm_ffn_op_type llm_ffn_op;
358
359    // Step35: optional per-layer clamps for (Swi)GLU
360    std::array<float, LLAMA_MAX_LAYERS> swiglu_clamp_exp; // clamping for expert FFN
361    std::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 ...
382    void set_swa_pattern(uint32_t n_pattern, bool dense_first = false);
383
384    // return true if one of the layers is SWA
385    bool is_swa_any() const;
386
387    bool is_swa(uint32_t il) const;
388
389    bool is_indexer_full(uint32_t il) const;
390
391    void set_recr_pattern(uint32_t n_pattern, bool dense_first = false);
392
393    // whether or not the given layer is recurrent (for hybrid models)
394    bool is_recr(uint32_t il) const;
395
396    uint32_t n_head(uint32_t il = 0) const;
397
398    uint32_t n_head_kv(uint32_t il = 0) const;
399
400    uint32_t n_ff(uint32_t il = 0) const;
401
402    uint32_t n_ff_exp(uint32_t il = 0) const;
403
404    uint32_t n_expert_used(uint32_t il = 0) const;
405
406    // return the maximum n_expert_used across all layers
407    uint32_t n_expert_used_max() const;
408
409    uint32_t n_gqa(uint32_t il = 0) const;
410
411    uint32_t n_rot(uint32_t il = 0) const;
412
413    // dimension of main + auxiliary input embeddings
414    uint32_t n_embd_inp() const;
415
416    // dimension of the encoder input embeddings
417    uint32_t n_embd_inp_enc() const;
418
419    // dimension of output embeddings
420    uint32_t n_embd_out() const;
421
422    // dimension of key/value embeddings for each head (per layer)
423    uint32_t n_embd_head_k(uint32_t il = 0) const;
424    uint32_t n_embd_head_v(uint32_t il = 0) const;
425
426    // dimension of key embeddings across all k-v heads
427    uint32_t n_embd_k_gqa(uint32_t il = 0) const;
428
429    // dimension of value embeddings across all k-v heads
430    uint32_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
433    bool is_n_embd_k_gqa_variable() const;
434    bool is_n_embd_v_gqa_variable() const;
435
436    // return the maximum n_embd_k_gqa/n_embd_v_gqa across all layers
437    uint32_t n_embd_k_gqa_max() const;
438    uint32_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
442    uint32_t n_embd_r() const;
443
444    // dimension of the recurrent state embeddings
445    uint32_t n_embd_s() const;
446
447    uint32_t n_pos_per_embd() const;
448
449    // note: currently only support if either all or none of the layers are MLA
450    bool is_mla() const;
451
452    uint32_t n_embd_head_k_mla() const;
453    uint32_t n_embd_head_v_mla() const;
454
455    bool has_kv(uint32_t il) const;
456
457    bool has_rope(uint32_t il) const;
458
459    // number of effective layers (excludes nextn layers)
460    uint32_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?
466    static bool is_masked_swa(uint32_t n_swa, llama_swa_type swa_type, llama_pos p0, llama_pos p1) {
467        assert(p0 >= 0 && p1 >= 0);
468
469        switch (swa_type) {
470            case LLAMA_SWA_TYPE_NONE:
471                {
472                } break;
473            case LLAMA_SWA_TYPE_STANDARD:
474                {
475                    if (p1 - p0 >= (int32_t) n_swa) {
476                        return true;
477                    }
478                } break;
479            case LLAMA_SWA_TYPE_CHUNKED:
480                {
481                    const llama_pos pos_chunk_start = (p1 / n_swa) * n_swa;
482
483                    if (p0 < pos_chunk_start) {
484                        return true;
485                    }
486                } break;
487            case LLAMA_SWA_TYPE_SYMMETRIC:
488                {
489                    const int32_t half_n_swa = (int32_t) n_swa / 2;
490                    const int32_t pos_diff = p1 - p0;
491
492                    // Mask if outside the symmetric window
493                    if (pos_diff < -half_n_swa || pos_diff > half_n_swa) {
494                        return true;
495                    }
496                } break;
497        }
498
499        return false;
500    }
501
502
503    bool use_mrope() const;
504};
505
506static_assert(std::is_trivially_copyable<llama_hparams>::value, "llama_hparams must be trivially copyable");