|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include "llama.h" |
| 4 | + |
| 5 | +#include <array> |
| 6 | + |
| 7 | +// bump if necessary |
| 8 | +#define LLAMA_MAX_LAYERS 512 |
| 9 | +#define LLAMA_MAX_EXPERTS 160 // DeepSeekV2 |
| 10 | + |
| 11 | +struct llama_hparams_posnet { |
| 12 | + uint32_t n_embd; |
| 13 | + uint32_t n_layer; |
| 14 | +}; |
| 15 | + |
| 16 | +struct llama_hparams_convnext { |
| 17 | + uint32_t n_embd; |
| 18 | + uint32_t n_layer; |
| 19 | +}; |
| 20 | + |
| 21 | +struct llama_hparams { |
| 22 | + bool vocab_only; |
| 23 | + bool rope_finetuned; |
| 24 | + bool use_par_res; |
| 25 | + bool swin_norm; |
| 26 | + |
| 27 | + uint32_t n_vocab = 0; |
| 28 | + uint32_t n_ctx_train; // context size the model was trained on |
| 29 | + uint32_t n_embd; |
| 30 | + uint32_t n_embd_features = 0; |
| 31 | + uint32_t n_layer; |
| 32 | + uint32_t n_rot; |
| 33 | + uint32_t n_swa = 0; // sliding window attention (SWA) |
| 34 | + uint32_t n_embd_head_k; // 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 |
| 35 | + uint32_t n_embd_head_v; // dimension of values (d_v) aka n_embd_head |
| 36 | + uint32_t n_expert = 0; |
| 37 | + uint32_t n_expert_used = 0; |
| 38 | + uint32_t n_vocab_type = 0; // for BERT-style token types |
| 39 | + uint32_t n_rel_attn_bkts = 0; |
| 40 | + |
| 41 | + // for WavTokenizer |
| 42 | + struct llama_hparams_posnet posnet; |
| 43 | + struct llama_hparams_convnext convnext; |
| 44 | + |
| 45 | + std::array<uint32_t, LLAMA_MAX_LAYERS> n_head_arr; |
| 46 | + std::array<uint32_t, LLAMA_MAX_LAYERS> n_head_kv_arr; |
| 47 | + std::array<uint32_t, LLAMA_MAX_LAYERS> n_ff_arr; |
| 48 | + |
| 49 | + uint32_t n_layer_dense_lead = 0; |
| 50 | + uint32_t n_lora_q = 0; |
| 51 | + uint32_t n_lora_kv = 0; |
| 52 | + uint32_t n_ff_exp = 0; |
| 53 | + uint32_t n_ff_shexp = 0; |
| 54 | + uint32_t n_expert_shared = 0; |
| 55 | + uint32_t n_norm_groups = 0; |
| 56 | + |
| 57 | + float expert_weights_scale = 0.0; |
| 58 | + |
| 59 | + float f_norm_eps; |
| 60 | + float f_norm_rms_eps; |
| 61 | + float f_norm_group_eps; |
| 62 | + |
| 63 | + float f_attn_logit_softcapping = 50.0f; |
| 64 | + float f_final_logit_softcapping = 30.0f; |
| 65 | + |
| 66 | + // for RWKV |
| 67 | + uint32_t rescale_every_n_layers = 0; |
| 68 | + uint32_t time_mix_extra_dim = 0; |
| 69 | + uint32_t time_decay_extra_dim = 0; |
| 70 | + uint32_t wkv_head_size = 0; |
| 71 | + |
| 72 | + float rope_attn_factor = 1.0f; |
| 73 | + float rope_freq_base_train; |
| 74 | + float rope_freq_scale_train; |
| 75 | + uint32_t n_ctx_orig_yarn; |
| 76 | + float rope_yarn_log_mul; |
| 77 | + int rope_sections[4]; // TODO: actually this should be std::array (I was wrong) |
| 78 | + |
| 79 | + // for State Space Models |
| 80 | + uint32_t ssm_d_conv = 0; |
| 81 | + uint32_t ssm_d_inner = 0; |
| 82 | + uint32_t ssm_d_state = 0; |
| 83 | + uint32_t ssm_dt_rank = 0; |
| 84 | + |
| 85 | + bool ssm_dt_b_c_rms = false; |
| 86 | + |
| 87 | + float f_clamp_kqv = 0.0f; |
| 88 | + float f_max_alibi_bias = 0.0f; |
| 89 | + float f_logit_scale = 0.0f; |
| 90 | + |
| 91 | + // Additional scale factors (Granite/Granite MoE) |
| 92 | + float f_residual_scale = 0.0f; |
| 93 | + float f_embedding_scale = 0.0f; |
| 94 | + float f_attention_scale = 0.0f; |
| 95 | + |
| 96 | + bool causal_attn = true; |
| 97 | + bool use_alibi = false; |
| 98 | + bool attn_soft_cap = false; |
| 99 | + |
| 100 | + // needed by encoder-decoder models (e.g. T5, FLAN-T5) |
| 101 | + // ref: https://github.com/ggerganov/llama.cpp/pull/8141 |
| 102 | + llama_token dec_start_token_id = LLAMA_TOKEN_NULL; |
| 103 | + |
| 104 | + enum llama_pooling_type pooling_type = LLAMA_POOLING_TYPE_NONE; |
| 105 | + enum llama_rope_type rope_type = LLAMA_ROPE_TYPE_NONE; |
| 106 | + enum llama_rope_scaling_type rope_scaling_type_train = LLAMA_ROPE_SCALING_TYPE_NONE; |
| 107 | + |
| 108 | + uint32_t n_head(uint32_t il = 0) const; |
| 109 | + |
| 110 | + uint32_t n_head_kv(uint32_t il = 0) const; |
| 111 | + |
| 112 | + uint32_t n_ff(uint32_t il = 0) const; |
| 113 | + |
| 114 | + uint32_t n_gqa(uint32_t il = 0) const; |
| 115 | + |
| 116 | + // dimension of key embeddings across all k-v heads |
| 117 | + uint32_t n_embd_k_gqa(uint32_t il = 0) const; |
| 118 | + |
| 119 | + // dimension of value embeddings across all k-v heads |
| 120 | + uint32_t n_embd_v_gqa(uint32_t il = 0) const; |
| 121 | + |
| 122 | + // dimension of the rolling state embeddings |
| 123 | + // corresponds to Mamba's conv_states size or RWKV's token_shift states size |
| 124 | + uint32_t n_embd_k_s() const; |
| 125 | + |
| 126 | + // dimension of the recurrent state embeddings |
| 127 | + uint32_t n_embd_v_s() const; |
| 128 | +}; |
| 129 | + |
| 130 | +static_assert(std::is_trivially_copyable<llama_hparams>::value, "llama_hparams must be trivially copyable"); |
| 131 | + |
0 commit comments