char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
d7e86430a
char/aliceai-t5-moe
1#include "models.h" 2 3void llama_model_granite_hybrid::load_arch_hparams (llama_model_loader & ml ) { 4ml .get_key (LLM_KV_ATTENTION_LAYERNORM_RMS_EPS ,hparams .f_norm_rms_eps ); 5ml .get_key (LLM_KV_LOGIT_SCALE ,hparams .f_logit_scale ,/* required */ false); 6ml .get_key (LLM_KV_RESIDUAL_SCALE ,hparams .f_residual_scale ,/* required */ false); 7ml .get_key (LLM_KV_EMBEDDING_SCALE ,hparams .f_embedding_scale ,/* required */ false); 8ml .get_key (LLM_KV_ATTENTION_SCALE ,hparams .f_attention_scale ,/* required */ false); 9 10ml .get_key (LLM_KV_SSM_CONV_KERNEL ,hparams .ssm_d_conv ); 11ml .get_key (LLM_KV_SSM_INNER_SIZE ,hparams .ssm_d_inner ); 12ml .get_key (LLM_KV_SSM_STATE_SIZE ,hparams .ssm_d_state ); 13ml .get_key (LLM_KV_SSM_TIME_STEP_RANK ,hparams .ssm_dt_rank ); 14ml .get_key (LLM_KV_SSM_GROUP_COUNT ,hparams .ssm_n_group ); 15 16// Granite uses rope_finetuned as a switch for rope, so default to true 17bool rope_finetuned = true; 18ml .get_key (LLM_KV_ROPE_SCALING_FINETUNED ,rope_finetuned , false); 19hparams .rope_finetuned = rope_finetuned ;// needed for round trip save 20 std::fill (hparams .rope_pattern .begin (),hparams .rope_pattern .end (),rope_finetuned ); 21 22// A layer is recurrent IFF the n_head_kv value is set to 0 23for (uint32_t i = 0 ;i < hparams .n_layer ();++ i ) { 24hparams .is_recr_impl [i ]= hparams .n_head_kv (i )== 0 ; 25 } 26 27ml .get_key (LLM_KV_ATTENTION_LAYERNORM_RMS_EPS ,hparams .f_norm_rms_eps ); 28 29switch (hparams .n_embd ) { 30case 768 :type = LLM_TYPE_350M ;break ; 31case 1536 :type = (hparams .n_ff ()== 512 ?LLM_TYPE_7B_A1B :LLM_TYPE_1B );break ; 32case 2048 :case 2560 :type = LLM_TYPE_3B ;break ; 33case 4096 :type = LLM_TYPE_32B_A9B ;break ; 34default :type = LLM_TYPE_UNKNOWN ; 35 } 36 37// For Granite MoE Shared 38ml .get_key (LLM_KV_EXPERT_SHARED_FEED_FORWARD_LENGTH ,hparams .n_ff_shexp ,/* required */ false); 39} 40 41void llama_model_granite_hybrid::load_arch_tensors (llama_model_loader & ) { 42LLAMA_LOAD_LOCALS ; 43 44// mamba2 Mixer SSM params 45// NOTE: int64_t for tensor dimensions 46const int64_t d_conv = hparams .ssm_d_conv ; 47const int64_t d_inner = hparams .ssm_d_inner ; 48const int64_t d_state = hparams .ssm_d_state ; 49const int64_t n_ssm_head = hparams .ssm_dt_rank ; 50const int64_t n_group = hparams .ssm_n_group ; 51const int64_t d_in_proj = 2 * d_inner + 2 * n_group * d_state + n_ssm_head ; 52 53// only an expansion factor of 2 is supported for now 54GGML_ASSERT (2 * n_embd == d_inner ); 55 56// embeddings 57tok_embd = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },0 ); 58 59// output 60 { 61output_norm = create_tensor (tn (LLM_TENSOR_OUTPUT_NORM ,"weight" ), {n_embd },0 ); 62output = create_tensor (tn (LLM_TENSOR_OUTPUT ,"weight" ), {n_embd ,n_vocab },TENSOR_NOT_REQUIRED ); 63// if output is NULL, init from the input tok embed, duplicated to allow offloading 64if (output == NULL ) { 65output = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },TENSOR_DUPLICATED ); 66 } 67 } 68 69for (int i = 0 ;i < n_layer ;++ i ) { 70auto & layer = layers [i ]; 71 72// norm 73layer .attn_norm = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"weight" ,i ), {n_embd },0 ); 74 75if (hparams .is_recr (i )) { 76// ssm layers 77layer .ssm_in = create_tensor (tn (LLM_TENSOR_SSM_IN ,"weight" ,i ), {n_embd ,d_in_proj },0 ); 78 79layer .ssm_conv1d = create_tensor (tn (LLM_TENSOR_SSM_CONV1D ,"weight" ,i ), {d_conv ,d_inner + 2 * n_group * d_state },0 ); 80layer .ssm_conv1d_b = create_tensor (tn (LLM_TENSOR_SSM_CONV1D ,"bias" ,i ), {d_inner + 2 * n_group * d_state },TENSOR_NOT_REQUIRED ); 81 82layer .ssm_dt_b = create_tensor (tn (LLM_TENSOR_SSM_DT ,"bias" ,i ), {n_ssm_head },0 ); 83 84// no "weight" suffix for these 85layer .ssm_a = create_tensor (tn (LLM_TENSOR_SSM_A ,i ), {1 ,n_ssm_head },0 ); 86layer .ssm_d = create_tensor (tn (LLM_TENSOR_SSM_D ,i ), {1 ,n_ssm_head },0 ); 87 88layer .ssm_norm = create_tensor (tn (LLM_TENSOR_SSM_NORM ,"weight" ,i ), {d_inner /n_group ,n_group },0 ); 89 90// out_proj 91layer .ssm_out = create_tensor (tn (LLM_TENSOR_SSM_OUT ,"weight" ,i ), {d_inner ,n_embd },0 ); 92 }else { 93// attention layers (with optional bias) 94const int64_t n_head_i = hparams .n_head (i ); 95const int64_t n_embd_k_gqa_i = hparams .n_embd_k_gqa (i ); 96const int64_t n_embd_v_gqa_i = hparams .n_embd_v_gqa (i ); 97create_tensor_qkv (layer ,i ,n_embd ,n_embd_head_k * n_head_i ,n_embd_k_gqa_i ,n_embd_v_gqa_i ,0 ); 98layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,i ), {n_embd_head_k * n_head_i ,n_embd },0 ); 99layer .wo_b = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"bias" ,i ), {n_embd },TENSOR_NOT_REQUIRED ); 100 } 101 102// feed forward (w/ optional biases) 103if (n_expert > 0 ) { 104// MoE FFN 105layer .ffn_norm = create_tensor (tn (LLM_TENSOR_FFN_NORM ,"weight" ,i ), {n_embd },0 ); 106layer .rope_freqs = create_tensor (tn (LLM_TENSOR_ROPE_FREQS ,"weight" ,i ), {n_rot /2 },TENSOR_NOT_REQUIRED | (i != 0 ?TENSOR_DUPLICATED :0 )); 107layer .ffn_gate_inp = create_tensor (tn (LLM_TENSOR_FFN_GATE_INP ,"weight" ,i ), {n_embd ,n_expert },0 ); 108layer .ffn_gate_exps = create_tensor (tn (LLM_TENSOR_FFN_GATE_EXPS ,"weight" ,i ), {n_embd ,n_ff ,n_expert },TENSOR_NOT_REQUIRED ); 109layer .ffn_down_exps = create_tensor (tn (LLM_TENSOR_FFN_DOWN_EXPS ,"weight" ,i ), {n_ff ,n_embd ,n_expert },0 ); 110layer .ffn_up_exps = create_tensor (tn (LLM_TENSOR_FFN_UP_EXPS ,"weight" ,i ), {n_embd ,n_ff ,n_expert },0 ); 111 112// For Granite MoE Shared 113if (hparams .n_ff_shexp > 0 ) { 114layer .ffn_gate_shexp = create_tensor (tn (LLM_TENSOR_FFN_GATE_SHEXP ,"weight" ,i ), {n_embd ,hparams .n_ff_shexp },0 ); 115layer .ffn_up_shexp = create_tensor (tn (LLM_TENSOR_FFN_UP_SHEXP ,"weight" ,i ), {n_embd ,hparams .n_ff_shexp },0 ); 116layer .ffn_down_shexp = create_tensor (tn (LLM_TENSOR_FFN_DOWN_SHEXP ,"weight" ,i ), {hparams .n_ff_shexp ,n_embd },0 ); 117 } 118 }else { 119layer .ffn_norm = create_tensor (tn (LLM_TENSOR_FFN_NORM ,"weight" ,i ), {n_embd },0 ); 120layer .rope_freqs = create_tensor (tn (LLM_TENSOR_ROPE_FREQS ,"weight" ,i ), {n_rot /2 },TENSOR_NOT_REQUIRED | (i != 0 ?TENSOR_DUPLICATED :0 )); 121layer .ffn_gate = create_tensor (tn (LLM_TENSOR_FFN_GATE ,"weight" ,i ), {n_embd ,n_ff },0 ); 122layer .ffn_down = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"weight" ,i ), {n_ff ,n_embd },0 ); 123layer .ffn_up = create_tensor (tn (LLM_TENSOR_FFN_UP ,"weight" ,i ), {n_embd ,n_ff },0 ); 124layer .ffn_gate_b = create_tensor (tn (LLM_TENSOR_FFN_GATE ,"bias" ,i ), {n_ff },TENSOR_NOT_REQUIRED ); 125layer .ffn_down_b = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"bias" ,i ), {n_embd },TENSOR_NOT_REQUIRED ); 126layer .ffn_up_b = create_tensor (tn (LLM_TENSOR_FFN_UP ,"bias" ,i ), {n_ff },TENSOR_NOT_REQUIRED ); 127 } 128 } 129} 130 131std::unique_ptr < llm_graph_context > llama_model_granite_hybrid::build_arch_graph (const llm_graph_params & params )const { 132return std::make_unique < graph > (* this ,params ); 133} 134 135llama_model_granite_hybrid::graph::graph (const llama_model & model ,const llm_graph_params & params ) : 136llm_build_mamba_base (params ) { 137const int64_t n_embd_head = hparams .n_embd_head_v (); 138GGML_ASSERT (n_embd_head == hparams .n_embd_head_k ()); 139 140ggml_tensor * cur ; 141ggml_tensor * inpL ; 142 143inpL = build_inp_embd (model .tok_embd ); 144 145auto * inp = build_inp_mem_hybrid (); 146 147ggml_tensor * inp_out_ids = build_inp_out_ids (); 148 149// Positional embeddings populated if rope enabled 150ggml_tensor * inp_pos = nullptr ; 151if (hparams .has_rope (0 )) { 152inp_pos = build_inp_pos (); 153 } 154 155for (int il = 0 ;il < n_layer ;++ il ) { 156struct ggml_tensor * inpSA = inpL ; 157 158// norm 159cur = build_norm (inpL ,model .layers [il ].attn_norm ,NULL ,LLM_NORM_RMS ,il ); 160cb (cur ,"attn_norm" ,il ); 161 162if (hparams .is_recr (il )) { 163// ssm layer // 164cur = build_mamba2_layer (inp -> get_recr (),cur ,model ,ubatch ,il ); 165 }else { 166// attention layer // 167cur = build_attention_layer (cur ,inp_pos ,inp -> get_attn (),model ,n_embd_head ,il ); 168 } 169 170if (il == n_layer - 1 && inp_out_ids ) { 171cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 172inpSA = ggml_get_rows (ctx0 ,inpSA ,inp_out_ids ); 173 } 174 175// ffn 176cur = build_layer_ffn (cur ,inpSA ,model ,il ); 177 178// input for next layer 179inpL = cur ; 180 } 181 182cur = inpL ; 183 184cur = build_norm (cur ,model .output_norm ,NULL ,LLM_NORM_RMS ,-1 ); 185 186cb (cur ,"result_norm" ,-1 ); 187res -> t_embd = cur ; 188 189// lm_head 190cur = build_lora_mm (model .output ,cur ,model .output_s ); 191 192// For Granite architectures - scale logits 193if (hparams .f_logit_scale ) { 194cur = ggml_scale (ctx0 ,cur ,1.0f /hparams .f_logit_scale ); 195 } 196cb (cur ,"result_output" ,-1 ); 197res -> t_logits = cur ; 198 199ggml_build_forward_expand (gf ,cur ); 200} 201 202ggml_tensor * llama_model_granite_hybrid::graph::build_attention_layer (ggml_tensor * cur , 203ggml_tensor * inp_pos , 204llm_graph_input_attn_kv * inp_attn , 205const llama_model & model , 206const int64_t n_embd_head , 207const int il ) { 208auto [Qcur ,Kcur ,Vcur ]= build_qkv (model .layers [il ],cur ,n_embd_head ,hparams .n_head (il ),hparams .n_head_kv (il ),il ); 209 210if (hparams .has_rope (il )) { 211ggml_tensor * rope_factors = model .get_rope_factors (cparams ,il ); 212Qcur = ggml_rope_ext (ctx0 ,Qcur ,inp_pos ,rope_factors ,n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 213ext_factor ,attn_factor ,beta_fast ,beta_slow ); 214 215Kcur = ggml_rope_ext (ctx0 ,Kcur ,inp_pos ,rope_factors ,n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 216ext_factor ,attn_factor ,beta_fast ,beta_slow ); 217 } 218 219cb (Qcur ,"Qcur" ,il ); 220cb (Kcur ,"Kcur" ,il ); 221cb (Vcur ,"Vcur" ,il ); 222 223const float kq_scale = 224hparams .f_attention_scale == 0.0f ?1.0f /sqrtf (float (n_embd_head )) :hparams .f_attention_scale ; 225cur = build_attn (inp_attn , 226model .layers [il ].wo ,model .layers [il ].wo_b ,model .layers [il ].wo_s , 227Qcur ,Kcur ,Vcur ,nullptr ,nullptr ,nullptr ,kq_scale ,il ); 228cb (cur ,"attn_out" ,il ); 229return cur ; 230} 231 232ggml_tensor * llama_model_granite_hybrid::graph::build_layer_ffn (ggml_tensor * cur , 233ggml_tensor * inpSA , 234const llama_model & model , 235const int il ) { 236// For Granite architectures - scale residual 237if (hparams .f_residual_scale ) { 238cur = ggml_scale (ctx0 ,cur ,hparams .f_residual_scale ); 239 } 240ggml_tensor * ffn_inp = ggml_add (ctx0 ,cur ,inpSA ); 241cb (ffn_inp ,"ffn_inp" ,il ); 242 243// feed-forward network (non-MoE) 244if (model .layers [il ].ffn_gate_inp == nullptr ) { 245cur = build_norm (ffn_inp ,model .layers [il ].ffn_norm ,NULL ,LLM_NORM_RMS ,il ); 246cb (cur ,"ffn_norm" ,il ); 247 248cur = build_ffn (cur , 249model .layers [il ].ffn_up ,model .layers [il ].ffn_up_b ,NULL , 250model .layers [il ].ffn_gate ,model .layers [il ].ffn_gate_b ,NULL , 251model .layers [il ].ffn_down ,model .layers [il ].ffn_down_b ,NULL , 252NULL ,LLM_FFN_SILU ,LLM_FFN_PAR ,il ); 253cb (cur ,"ffn_out" ,il ); 254 255 }else { 256// MoE branch 257cur = build_norm (ffn_inp ,model .layers [il ].ffn_norm ,NULL ,LLM_NORM_RMS ,il ); 258cb (cur ,"ffn_norm" ,il ); 259 260ggml_tensor * moe_out = 261build_moe_ffn (cur , 262model .layers [il ].ffn_gate_inp , 263model .layers [il ].ffn_up_exps , 264model .layers [il ].ffn_gate_exps , 265model .layers [il ].ffn_down_exps , 266nullptr , 267n_expert ,n_expert_used , 268LLM_FFN_SILU , true, 269hparams .expert_weights_scale , 270LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX , 271il ); 272cb (moe_out ,"ffn_moe_out" ,il ); 273 274// For Granite MoE Shared 275if (hparams .n_ff_shexp > 0 ) { 276ggml_tensor * ffn_shexp = 277build_ffn (cur , 278model .layers [il ].ffn_up_shexp ,NULL ,NULL , 279model .layers [il ].ffn_gate_shexp ,NULL ,NULL , 280model .layers [il ].ffn_down_shexp ,NULL ,NULL , 281NULL ,LLM_FFN_SILU ,LLM_FFN_PAR ,il ); 282cb (ffn_shexp ,"ffn_shexp" ,il ); 283 284cur = ggml_add (ctx0 ,moe_out ,ffn_shexp ); 285cb (cur ,"ffn_out" ,il ); 286 }else { 287cur = moe_out ; 288 } 289 } 290 291// For Granite architectures - scale residual 292if (hparams .f_residual_scale ) { 293cur = ggml_scale (ctx0 ,cur ,hparams .f_residual_scale ); 294 } 295cur = ggml_add (ctx0 ,cur ,ffn_inp ); 296cb (cur ,"ffn_out" ,il ); 297 298cur = build_cvec (cur ,il ); 299cb (cur ,"l_out" ,il ); 300 301return cur ; 302}