char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
7acb4e8cd
char/aliceai-t5-moe
1#include "models.h" 2 3void llama_model_mistral3::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_ATTENTION_TEMPERATURE_SCALE ,hparams .f_attn_temp_scale , false); 6 7ml .get_key (LLM_KV_ROPE_SCALING_YARN_BETA_FAST ,hparams .yarn_beta_fast , false); 8ml .get_key (LLM_KV_ROPE_SCALING_YARN_BETA_SLOW ,hparams .yarn_beta_slow , false); 9ml .get_key (LLM_KV_ROPE_SCALING_YARN_LOG_MUL ,hparams .rope_yarn_log_mul , false); 10 11hparams .f_attn_temp_offset = 0.0f ; 12 13// TODO: maybe add n_attn_temp_floor_scale as a separate KV? 14if (hparams .f_attn_temp_scale != 0.0f ) { 15hparams .n_attn_temp_floor_scale = hparams .n_ctx_orig_yarn ; 16if (hparams .n_attn_temp_floor_scale == 0 ) { 17throw std::runtime_error ("invalid n_ctx_orig_yarn for attention temperature scaling" ); 18 } 19 } 20 21switch (hparams .n_layer ()) { 22case 26 :type = LLM_TYPE_3B ;break ; 23case 34 :type = LLM_TYPE_8B ;break ; 24case 40 :type = LLM_TYPE_14B ;break ; 25default :type = LLM_TYPE_UNKNOWN ; 26 } 27} 28 29void llama_model_mistral3::load_arch_tensors (llama_model_loader & ) { 30LLAMA_LOAD_LOCALS ; 31 32tok_embd = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },0 ); 33 34// output 35output_norm = create_tensor (tn (LLM_TENSOR_OUTPUT_NORM ,"weight" ), {n_embd },0 ); 36output = create_tensor (tn (LLM_TENSOR_OUTPUT ,"weight" ), {n_embd ,n_vocab },TENSOR_NOT_REQUIRED ); 37 38// if output is NULL, init from the input tok embed 39if (output == NULL ) { 40output = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },TENSOR_DUPLICATED ); 41 } 42 43for (int i = 0 ;i < n_layer ;++ i ) { 44auto & layer = layers [i ]; 45 46layer .attn_norm = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"weight" ,i ), {n_embd },0 ); 47 48create_tensor_qkv (layer ,i ,n_embd ,n_embd_head_k * n_head ,n_embd_k_gqa ,n_embd_v_gqa ,0 ); 49layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,i ), {n_embd_head_k * n_head ,n_embd },0 ); 50 51// optional bias tensors 52layer .wo_b = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"bias" ,i ), {n_embd },TENSOR_NOT_REQUIRED ); 53 54layer .ffn_norm = create_tensor (tn (LLM_TENSOR_FFN_NORM ,"weight" ,i ), {n_embd },0 ); 55 56if (hparams .rope_scaling_type_train == LLAMA_ROPE_SCALING_TYPE_LONGROPE ) { 57layer .rope_long = create_tensor (tn (LLM_TENSOR_ROPE_FACTORS_LONG ,"weight" ,i ), {n_rot /2 },TENSOR_NOT_REQUIRED | (i != 0 ?TENSOR_DUPLICATED :0 )); 58layer .rope_short = create_tensor (tn (LLM_TENSOR_ROPE_FACTORS_SHORT ,"weight" ,i ), {n_rot /2 },TENSOR_NOT_REQUIRED | (i != 0 ?TENSOR_DUPLICATED :0 )); 59 } 60else { 61layer .rope_freqs = create_tensor (tn (LLM_TENSOR_ROPE_FREQS ,"weight" ,i ), {n_rot /2 },TENSOR_NOT_REQUIRED | (i != 0 ?TENSOR_DUPLICATED :0 )); 62 } 63 64if (n_expert == 0 ) { 65layer .ffn_gate = create_tensor (tn (LLM_TENSOR_FFN_GATE ,"weight" ,i ), {n_embd ,n_ff },0 ); 66layer .ffn_down = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"weight" ,i ), {n_ff ,n_embd },0 ); 67layer .ffn_up = create_tensor (tn (LLM_TENSOR_FFN_UP ,"weight" ,i ), {n_embd ,n_ff },0 ); 68 69// optional MLP bias 70layer .ffn_gate_b = create_tensor (tn (LLM_TENSOR_FFN_GATE ,"bias" ,i ), {n_ff },TENSOR_NOT_REQUIRED ); 71layer .ffn_down_b = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"bias" ,i ), {n_embd },TENSOR_NOT_REQUIRED ); 72layer .ffn_up_b = create_tensor (tn (LLM_TENSOR_FFN_UP ,"bias" ,i ), {n_ff },TENSOR_NOT_REQUIRED ); 73 }else { 74layer .ffn_gate_inp = create_tensor (tn (LLM_TENSOR_FFN_GATE_INP ,"weight" ,i ), {n_embd ,n_expert },0 ); 75layer .ffn_gate_exps = create_tensor (tn (LLM_TENSOR_FFN_GATE_EXPS ,"weight" ,i ), {n_embd ,n_ff ,n_expert },TENSOR_NOT_REQUIRED ); 76layer .ffn_down_exps = create_tensor (tn (LLM_TENSOR_FFN_DOWN_EXPS ,"weight" ,i ), {n_ff ,n_embd ,n_expert },0 ); 77layer .ffn_up_exps = create_tensor (tn (LLM_TENSOR_FFN_UP_EXPS ,"weight" ,i ), {n_embd ,n_ff ,n_expert },0 ); 78 79// For Granite MoE Shared 80if (hparams .n_ff_shexp > 0 ) { 81layer .ffn_gate_shexp = create_tensor (tn (LLM_TENSOR_FFN_GATE_SHEXP ,"weight" ,i ), {n_embd ,hparams .n_ff_shexp },0 ); 82layer .ffn_up_shexp = create_tensor (tn (LLM_TENSOR_FFN_UP_SHEXP ,"weight" ,i ), {n_embd ,hparams .n_ff_shexp },0 ); 83layer .ffn_down_shexp = create_tensor (tn (LLM_TENSOR_FFN_DOWN_SHEXP ,"weight" ,i ), {hparams .n_ff_shexp ,n_embd },0 ); 84 } 85 } 86 } 87} 88 89std::unique_ptr < llm_graph_context > llama_model_mistral3::build_arch_graph (const llm_graph_params & params )const { 90return std::make_unique < graph > (* this ,params ); 91} 92 93llama_model_mistral3::graph::graph (const llama_model & model ,const llm_graph_params & params ) :llm_graph_context (params ) { 94const int64_t n_embd_head = hparams .n_embd_head_v (); 95 96GGML_ASSERT (n_embd_head == hparams .n_embd_head_k ()); 97GGML_ASSERT (n_embd_head == n_rot ); 98 99ggml_tensor * cur ; 100ggml_tensor * inpL ; 101 102inpL = build_inp_embd (model .tok_embd ); 103 104// inp_pos - contains the positions 105ggml_tensor * inp_pos = build_inp_pos (); 106 107// (optional) temperature tuning 108ggml_tensor * inp_attn_scale = nullptr ; 109if (hparams .f_attn_temp_scale != 0.0f ) { 110inp_attn_scale = build_inp_attn_scale (); 111 } 112 113auto * inp_attn = build_attn_inp_kv (); 114 115const float kq_scale = hparams .f_attention_scale == 0.0f ?1.0f /sqrtf (float (n_embd_head )) :hparams .f_attention_scale ; 116 117ggml_tensor * inp_out_ids = build_inp_out_ids (); 118 119for (int il = 0 ;il < n_layer ;++ il ) { 120ggml_tensor * inpSA = inpL ; 121 122// norm 123cur = build_norm (inpL , 124model .layers [il ].attn_norm ,NULL , 125LLM_NORM_RMS ,il ); 126cb (cur ,"attn_norm" ,il ); 127 128// self-attention 129 { 130// rope freq factors for llama3; may return nullptr for llama2 and other models 131ggml_tensor * rope_factors = model .get_rope_factors (cparams ,il ); 132 133// compute Q and K and RoPE them 134auto [Qcur ,Kcur ,Vcur ]= build_qkv (model .layers [il ],cur , 135n_embd_head ,n_head ,n_head_kv ,il ); 136 137Qcur = ggml_rope_ext ( 138ctx0 ,Qcur ,inp_pos ,rope_factors , 139n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 140ext_factor ,attn_factor ,beta_fast ,beta_slow 141 ); 142 143Kcur = ggml_rope_ext ( 144ctx0 ,Kcur ,inp_pos ,rope_factors , 145n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 146ext_factor ,attn_factor ,beta_fast ,beta_slow 147 ); 148 149cb (Qcur ,"Qcur" ,il ); 150cb (Kcur ,"Kcur" ,il ); 151cb (Vcur ,"Vcur" ,il ); 152 153if (inp_attn_scale ) { 154// apply llama 4 temperature scaling 155Qcur = ggml_mul (ctx0 ,Qcur ,inp_attn_scale ); 156cb (Qcur ,"Qcur_attn_temp_scaled" ,il ); 157 } 158 159cur = build_attn (inp_attn , 160model .layers [il ].wo ,model .layers [il ].wo_b ,model .layers [il ].wo_s , 161Qcur ,Kcur ,Vcur ,nullptr ,nullptr ,nullptr ,kq_scale ,il ); 162cb (cur ,"attn_out" ,il ); 163 } 164if (il == n_layer - 1 && inp_out_ids ) { 165cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 166inpSA = ggml_get_rows (ctx0 ,inpSA ,inp_out_ids ); 167 } 168ggml_tensor * ffn_inp = ggml_add (ctx0 ,cur ,inpSA ); 169cb (ffn_inp ,"ffn_inp" ,il ); 170 171// feed-forward network (non-MoE) 172if (model .layers [il ].ffn_gate_inp == nullptr ) { 173 174cur = build_norm (ffn_inp , 175model .layers [il ].ffn_norm ,NULL , 176LLM_NORM_RMS ,il ); 177cb (cur ,"ffn_norm" ,il ); 178 179cur = build_ffn (cur , 180model .layers [il ].ffn_up ,model .layers [il ].ffn_up_b ,model .layers [il ].ffn_up_s , 181model .layers [il ].ffn_gate ,model .layers [il ].ffn_gate_b ,model .layers [il ].ffn_gate_s , 182model .layers [il ].ffn_down ,model .layers [il ].ffn_down_b ,model .layers [il ].ffn_down_s , 183NULL , 184LLM_FFN_SILU ,LLM_FFN_PAR ,il ); 185cb (cur ,"ffn_out" ,il ); 186 }else { 187// MoE branch 188cur = build_norm (ffn_inp , 189model .layers [il ].ffn_norm ,NULL , 190LLM_NORM_RMS ,il ); 191cb (cur ,"ffn_norm" ,il ); 192 193cur = build_moe_ffn (cur , 194model .layers [il ].ffn_gate_inp , 195model .layers [il ].ffn_up_exps , 196model .layers [il ].ffn_gate_exps , 197model .layers [il ].ffn_down_exps , 198nullptr , 199n_expert ,n_expert_used , 200LLM_FFN_SILU , true, 201hparams .expert_weights_scale , 202LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX , 203il , 204nullptr ,nullptr , 205model .layers [il ].ffn_up_exps_s , 206model .layers [il ].ffn_gate_exps_s , 207model .layers [il ].ffn_down_exps_s ); 208cb (cur ,"ffn_moe_out" ,il ); 209 } 210cur = ggml_add (ctx0 ,cur ,ffn_inp ); 211cb (cur ,"ffn_out" ,il ); 212 213cur = build_cvec (cur ,il ); 214cb (cur ,"l_out" ,il ); 215 216// input for next layer 217inpL = cur ; 218 } 219cur = inpL ; 220 221cur = build_norm (cur , 222model .output_norm ,NULL , 223LLM_NORM_RMS ,-1 ); 224 225cb (cur ,"result_norm" ,-1 ); 226res -> t_embd = cur ; 227 228// lm_head 229cur = build_lora_mm (model .output ,cur ,model .output_s ); 230 231cb (cur ,"result_output" ,-1 ); 232res -> t_logits = cur ; 233 234ggml_build_forward_expand (gf ,cur ); 235}