char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
9a4843cf2
char/aliceai-t5-moe
1#include "llama-hparams.h" 2 3#include "ggml.h" 4 5#include <algorithm> 6#include <cassert> 7 8void llama_hparams::set_swa_pattern (uint32_t n_pattern ,bool dense_first ) { 9if (dense_first ) { 10for (uint32_t il = 0 ;il < n_layer ();++ il ) { 11is_swa_impl [il ]= n_pattern == 0 || (il %n_pattern != 0 ); 12 } 13 }else { 14for (uint32_t il = 0 ;il < n_layer ();++ il ) { 15is_swa_impl [il ]= n_pattern == 0 || (il %n_pattern < (n_pattern - 1 )); 16 } 17 } 18 19for (uint32_t il = n_layer ();il < n_layer_all ;++ il ) { 20is_swa_impl [il ]= false; 21 } 22} 23 24void llama_hparams::set_recr_pattern (uint32_t n_pattern ,bool dense_first ) { 25if (dense_first ) { 26for (uint32_t il = 0 ;il < n_layer ();++ il ) { 27is_recr_impl [il ]= n_pattern == 0 || (il %n_pattern != 0 ); 28 } 29 }else { 30for (uint32_t il = 0 ;il < n_layer ();++ il ) { 31is_recr_impl [il ]= n_pattern == 0 || (il %n_pattern < (n_pattern - 1 )); 32 } 33 } 34 35for (uint32_t il = n_layer ();il < n_layer_all ;++ il ) { 36is_recr_impl [il ]= false; 37 } 38} 39 40bool llama_hparams::is_swa_any ()const { 41for (uint32_t il = 0 ;il < n_layer_all ;++ il ) { 42if (is_swa_impl [il ]) { 43return true; 44 } 45 } 46 47return false; 48} 49 50uint32_t llama_hparams::n_head (uint32_t il )const { 51if (il < n_layer_all ) { 52return n_head_arr [il ]; 53 } 54 55GGML_ABORT ("fatal error" ); 56} 57 58uint32_t llama_hparams::n_head_kv (uint32_t il )const { 59if (il < n_layer_all ) { 60return n_head_kv_arr [il ]; 61 } 62 63GGML_ABORT ("fatal error" ); 64} 65 66uint32_t llama_hparams::n_ff (uint32_t il )const { 67if (il < n_layer_all ) { 68return n_ff_arr [il ]; 69 } 70 71GGML_ABORT ("fatal error" ); 72} 73 74uint32_t llama_hparams::n_ff_exp (uint32_t il )const { 75if (il < n_layer_all ) { 76return n_ff_exp_arr [il ]; 77 } 78 79GGML_ABORT ("fatal error" ); 80} 81 82uint32_t llama_hparams::n_expert_used (uint32_t il )const { 83if (il < n_layer_all ) { 84return n_expert_used_arr [il ]; 85 } 86 87GGML_ABORT ("fatal error" ); 88} 89 90uint32_t llama_hparams::n_expert_used_max ()const { 91uint32_t val = 0 ; 92for (uint32_t il = 0 ;il < n_layer_all ;++ il ) { 93val = std::max (val ,n_expert_used (il )); 94 } 95 96return val ; 97} 98 99uint32_t llama_hparams::n_gqa (uint32_t il )const { 100const uint32_t n_head = this -> n_head (il ); 101const uint32_t n_head_kv = this -> n_head_kv (il ); 102 103if (n_head_kv == 0 ) { 104return 0 ; 105 } 106 107return n_head /n_head_kv ; 108} 109 110uint32_t llama_hparams::n_rot (uint32_t il )const { 111if (il < n_layer_all ) { 112return is_swa (il ) ?n_rot_swa :n_rot_full ; 113 } 114 115GGML_ABORT ("fatal error" ); 116} 117 118uint32_t llama_hparams::n_embd_inp ()const { 119if (n_embd_inp_impl > 0 ) { 120return n_embd_inp_impl ; 121 } 122 123uint32_t n_embd_inp = n_embd ; 124 125if (n_deepstack_layers > 0 ) { 126n_embd_inp += n_embd * n_deepstack_layers ; 127 } 128 129return n_embd_inp ; 130} 131 132uint32_t llama_hparams::n_embd_inp_enc ()const { 133return n_embd_inp_enc_impl > 0 ?n_embd_inp_enc_impl :n_embd_inp (); 134} 135 136uint32_t llama_hparams::n_embd_out ()const { 137return n_embd_out_impl > 0 ?n_embd_out_impl :n_embd ; 138} 139 140uint32_t llama_hparams::n_embd_head_k (uint32_t il )const { 141if (il < n_layer_all ) { 142return is_swa (il ) ?n_embd_head_k_swa :n_embd_head_k_full ; 143 } 144 145GGML_ABORT ("fatal error" ); 146} 147 148uint32_t llama_hparams::n_embd_head_v (uint32_t il )const { 149if (il < n_layer_all ) { 150return is_swa (il ) ?n_embd_head_v_swa :n_embd_head_v_full ; 151 } 152 153GGML_ABORT ("fatal error" ); 154} 155 156uint32_t llama_hparams::n_embd_k_gqa (uint32_t il )const { 157const uint32_t n_head_kv = this -> n_head_kv (il ); 158 159return n_embd_head_k (il )* n_head_kv ; 160} 161 162uint32_t llama_hparams::n_embd_v_gqa (uint32_t il )const { 163const uint32_t n_head_kv = this -> n_head_kv (il ); 164 165return n_embd_head_v (il )* n_head_kv ; 166} 167 168bool llama_hparams::is_n_embd_k_gqa_variable ()const { 169const uint32_t val = n_embd_k_gqa (); 170for (uint32_t il = 0 ;il < n_layer_all ;++ il ) { 171if (val != n_embd_k_gqa (il )) { 172return true; 173 } 174 } 175 176return false; 177} 178 179bool llama_hparams::is_n_embd_v_gqa_variable ()const { 180const uint32_t val = n_embd_v_gqa (); 181for (uint32_t il = 0 ;il < n_layer_all ;++ il ) { 182if (val != n_embd_v_gqa (il )) { 183return true; 184 } 185 } 186 187return false; 188} 189 190uint32_t llama_hparams::n_embd_k_gqa_max ()const { 191uint32_t val = n_embd_k_gqa (); 192for (uint32_t il = 0 ;il < n_layer_all ;++ il ) { 193val = std::max (val ,n_embd_k_gqa (il )); 194 } 195 196return val ; 197} 198 199uint32_t llama_hparams::n_embd_v_gqa_max ()const { 200uint32_t val = n_embd_v_gqa (); 201for (uint32_t il = 0 ;il < n_layer_all ;++ il ) { 202val = std::max (val ,n_embd_v_gqa (il )); 203 } 204 205return val ; 206} 207 208uint32_t llama_hparams::n_embd_r ()const { 209if (wkv_head_size != 0 ) { 210// for RWKV models 211return token_shift_count * n_embd ; 212 } 213 214if (n_shortconv_l_cache != 0 ) { 215// for LFM2 models 216return n_embd * (n_shortconv_l_cache - 1 ); 217 } 218 219if (n_embd_head_kda != 0 ) { 220// for Kimi KDA layers 221// Conv state for Q, K, V: 3 * (d_conv - 1) * n_head * head_dim 222const uint32_t d_inner = n_head ()* n_embd_head_kda ;// 32 * 128 = 4096 223return 3 * (ssm_d_conv > 0 ?ssm_d_conv - 1 :3 )* d_inner ; 224 } 225 226// TODO: maybe support other convolution strides than 1 227// NOTE: since the first column of the conv_state is shifted out each time, it's not actually needed 228// Corresponds to Mamba's conv_states size 229const uint32_t n_conv = (ssm_d_conv > 0 ?ssm_d_conv - 1 :0 )* (ssm_d_inner + 2 * ssm_n_group * ssm_d_state ); 230 231// PLE conv history needs its own row: Meta splits cache_r_l by head, so a history packed behind the first is unaddressable 232// it lives in cache_ple_r_l instead, mirrored like the rest of the PLE module 233return n_conv ; 234} 235 236uint32_t llama_hparams::n_embd_s ()const { 237if (wkv_head_size != 0 ) { 238// corresponds to RWKV's wkv_states size 239return n_embd * wkv_head_size ; 240 } 241 242if (n_embd_head_kda != 0 ) { 243// for Kimi KDA layers 244// Full recurrent state: head_dim * head_dim * n_head 245// h tensor shape for delta attention: [head_dim, head_dim, n_head] 246return n_embd_head_kda * n_embd_head_kda * n_head ();// 128 * 128 * 32 = 524288 247 } 248 249if (n_embd_head_la != 0 ) { 250// for MiniMax-Text-01 linear attention layers 251// Full recurrent state: head_dim * head_dim * n_head 252// tensor shape for linear attention: [head_dim, head_dim, n_head] 253return n_embd_head_la * n_embd_head_la * n_head ();// 128 * 128 * 64 = 1048576 254 } 255 256// corresponds to Mamba's ssm_states size 257return ssm_d_state * ssm_d_inner ; 258} 259 260bool llama_hparams::is_recr (uint32_t il )const { 261if (il < n_layer_all ) { 262return is_recr_impl [il ]; 263 } 264 265GGML_ABORT ("%s: il (%u) out of bounds (n_layer_all: %u)\n" ,__func__ ,il ,n_layer_all ); 266} 267 268uint32_t llama_hparams::ple_conv_state ()const { 269if (ple_n_heads == 0 || ple_conv_kernel == 0 ) { 270return 0 ; 271 } 272 273// dilation equals the n-gram size, matching the reference module 274return (ple_conv_kernel - 1 )* ple_ngram_size * dsv4_hc_mult * n_embd ; 275} 276 277bool llama_hparams::is_ple (uint32_t il )const { 278if (il < n_layer_all ) { 279return is_ple_impl [il ]; 280 } 281 282GGML_ABORT ("%s: il (%u) out of bounds (n_layer_all: %u)\n" ,__func__ ,il ,n_layer_all ); 283} 284 285uint32_t llama_hparams::n_pos_per_embd ()const { 286return rope_type == LLAMA_ROPE_TYPE_MROPE || rope_type == LLAMA_ROPE_TYPE_IMROPE ?4 :1 ; 287} 288 289bool llama_hparams::is_swa (uint32_t il )const { 290if (il < n_layer_all ) { 291return is_swa_impl [il ]; 292 } 293 294GGML_ABORT ("%s: il (%u) out of bounds (n_layer_all: %u)\n" ,__func__ ,il ,n_layer_all ); 295} 296 297bool llama_hparams::is_mla ()const { 298assert ((n_embd_head_k_mla_impl == 0 && n_embd_head_v_mla_impl == 0 )|| 299 (n_embd_head_k_mla_impl != 0 && n_embd_head_v_mla_impl != 0 )); 300 301return n_embd_head_k_mla_impl != 0 && n_embd_head_v_mla_impl != 0 ; 302} 303 304bool llama_hparams::is_indexer_full (uint32_t il )const { 305if (il < n_layer ()) { 306return is_indexer_full_impl [il ]; 307 } 308 309GGML_ABORT ("%s: il (%u) out of bounds (n_layer: %u)\n" ,__func__ ,il ,n_layer ()); 310} 311 312uint32_t llama_hparams::n_embd_head_k_mla ()const { 313return is_mla () ?n_embd_head_k_mla_impl :n_embd_head_k (); 314} 315 316uint32_t llama_hparams::n_embd_head_v_mla ()const { 317return is_mla () ?n_embd_head_v_mla_impl :n_embd_head_v (); 318} 319 320bool llama_hparams::has_kv (uint32_t il )const { 321if (n_layer_kv_from_start >=0 ) { 322if (il < (uint32_t )n_layer_kv_from_start ) { 323return true; 324 } 325 326return false; 327 } 328 329// by default, all layers have kv 330return true; 331} 332 333bool llama_hparams::has_rope (uint32_t il )const { 334// the router layer stores adapter routing signal, not positional info, 335// so it must not be RoPE-shifted 336if (router_layer >=0 && (int32_t )il == router_layer ) { 337return false; 338 } 339 340if (il < n_layer_all ) { 341return rope_pattern [il ]!= 0 ; 342 } 343 344GGML_ABORT ("%s: il (%u) out of bounds (n_layer_all: %u)\n" ,__func__ ,il ,n_layer_all ); 345} 346 347uint32_t llama_hparams::n_layer ()const { 348return n_layer_all - n_layer_nextn ; 349} 350 351bool llama_hparams::use_mrope ()const { 352return rope_sections [0 ]> 0 && rope_sections [1 ]> 0 ; 353}