char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
c61b98b87
char/aliceai-t5-moe
1#include "models.h" 2 3void llama_model_llama4::load_arch_hparams (llama_model_loader & ml ) { 4ml .get_key (LLM_KV_ATTENTION_LAYERNORM_RMS_EPS ,hparams .f_norm_rms_eps ); 5ml .get_key_or_arr (LLM_KV_EXPERT_FEED_FORWARD_LENGTH ,hparams .n_ff_exp_arr ,hparams .n_layer_all ); 6ml .get_key (LLM_KV_INTERLEAVE_MOE_LAYER_STEP ,hparams .n_moe_layer_step ); 7 8const bool found_swa = ml .get_key (LLM_KV_ATTENTION_SLIDING_WINDOW ,hparams .n_swa , false); 9if (found_swa && hparams .n_swa == 0 ) { 10hparams .swa_type = LLAMA_SWA_TYPE_NONE ; 11hparams .n_no_rope_layer_step = hparams .n_layer ();// always use rope 12 }else { 13hparams .swa_type = LLAMA_SWA_TYPE_CHUNKED ; 14hparams .n_swa = 8192 ; 15hparams .n_attn_temp_floor_scale = 8192 ; 16hparams .f_attn_temp_scale = 0.1f ; 17hparams .f_attn_temp_offset = 1.0f ; 18 19uint32_t swa_period = 4 ;// pattern: 3 chunked - 1 full 20ml .get_key_or_arr (LLM_KV_ATTENTION_SLIDING_WINDOW_PATTERN ,swa_period , false); 21hparams .set_swa_pattern (swa_period ); 22 23hparams .rope_freq_base_train_swa = hparams .rope_freq_base_train ; 24hparams .rope_freq_scale_train_swa = hparams .rope_freq_scale_train ; 25ml .get_key (LLM_KV_ROPE_FREQ_BASE_SWA ,hparams .rope_freq_base_train_swa , false); 26 } 27 28switch (hparams .n_expert ) { 29case 0 : { 30// MobileLLM (no MoE) 31switch (hparams .n_embd ) { 32case 2048 :type = LLM_TYPE_140M ;break ; 33case 4096 :type = LLM_TYPE_360M ;break ; 34case 6144 :type = LLM_TYPE_950M ;break ; 35default :type = LLM_TYPE_UNKNOWN ; 36 } 37 }break ; 38case 16 :type = LLM_TYPE_17B_16E ;break ; 39case 128 :type = LLM_TYPE_17B_128E ;break ; 40default :type = LLM_TYPE_UNKNOWN ; 41 } 42 43hparams .use_kq_norm = type != LLM_TYPE_17B_128E ; 44} 45 46void llama_model_llama4::load_arch_tensors (llama_model_loader & ) { 47LLAMA_LOAD_LOCALS ; 48 49if (n_expert == 0 ) { 50throw std::runtime_error (arch_name ()+ " model cannot have zero experts" ); 51 } 52tok_embd = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },0 ); 53 54// output 55output_norm = create_tensor (tn (LLM_TENSOR_OUTPUT_NORM ,"weight" ), {n_embd },0 ); 56output = create_tensor (tn (LLM_TENSOR_OUTPUT ,"weight" ), {n_embd ,n_vocab },TENSOR_NOT_REQUIRED ); 57 58// if output is NULL, init from the input tok embed 59if (output == NULL ) { 60output = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },TENSOR_DUPLICATED ); 61 } 62 63for (int i = 0 ;i < n_layer ;++ i ) { 64const bool is_moe_layer = hparams .n_moe_layer_step > 0 && (i + 1 ) %hparams .n_moe_layer_step == 0 ; 65 66auto & layer = layers [i ]; 67 68layer .attn_norm = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"weight" ,i ), {n_embd },0 ); 69 70create_tensor_qkv (layer ,i ,n_embd ,n_embd_head_k * n_head ,n_embd_k_gqa ,n_embd_v_gqa ,0 ); 71layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,i ), {n_embd_head_k * n_head ,n_embd },0 ); 72 73layer .ffn_norm = create_tensor (tn (LLM_TENSOR_FFN_NORM ,"weight" ,i ), {n_embd },0 ); 74 75layer .rope_freqs = create_tensor (tn (LLM_TENSOR_ROPE_FREQS ,"weight" ,i ), {n_rot /2 },TENSOR_NOT_REQUIRED | (i != 0 ?TENSOR_DUPLICATED :0 )); 76 77if (is_moe_layer ) { 78const int64_t n_ff_exp = hparams .n_ff_exp (); 79 80layer .ffn_gate_inp = create_tensor (tn (LLM_TENSOR_FFN_GATE_INP ,"weight" ,i ), {n_embd ,n_expert },0 ); 81layer .ffn_gate_exps = create_tensor (tn (LLM_TENSOR_FFN_GATE_EXPS ,"weight" ,i ), {n_embd ,n_ff_exp ,n_expert },0 ); 82layer .ffn_down_exps = create_tensor (tn (LLM_TENSOR_FFN_DOWN_EXPS ,"weight" ,i ), {n_ff_exp ,n_embd ,n_expert },0 ); 83layer .ffn_up_exps = create_tensor (tn (LLM_TENSOR_FFN_UP_EXPS ,"weight" ,i ), {n_embd ,n_ff_exp ,n_expert },0 ); 84 85// Shared expert 86const int64_t n_ff_shexp = n_ff_exp ; 87layer .ffn_gate_shexp = create_tensor (tn (LLM_TENSOR_FFN_GATE_SHEXP ,"weight" ,i ), {n_embd ,n_ff_shexp },0 ); 88layer .ffn_down_shexp = create_tensor (tn (LLM_TENSOR_FFN_DOWN_SHEXP ,"weight" ,i ), {n_ff_shexp ,n_embd },0 ); 89layer .ffn_up_shexp = create_tensor (tn (LLM_TENSOR_FFN_UP_SHEXP ,"weight" ,i ), {n_embd ,n_ff_shexp },0 ); 90 }else { 91layer .ffn_gate = create_tensor (tn (LLM_TENSOR_FFN_GATE ,"weight" ,i ), {n_embd ,n_ff },0 ); 92layer .ffn_down = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"weight" ,i ), {n_ff ,n_embd },0 ); 93layer .ffn_up = create_tensor (tn (LLM_TENSOR_FFN_UP ,"weight" ,i ), {n_embd ,n_ff },0 ); 94 } 95 } 96} 97 98std::unique_ptr < llm_graph_context > llama_model_llama4::build_arch_graph (const llm_graph_params & params )const { 99if (hparams .swa_type == LLAMA_SWA_TYPE_NONE ) { 100return std::make_unique < graph < false>> (* this ,params ); 101 }else { 102return std::make_unique < graph < true>> (* this ,params ); 103 } 104} 105 106template < bool iswa > 107llama_model_llama4::graph < iswa > ::graph (const llama_model & model ,const llm_graph_params & params ) :llm_graph_context (params ) { 108const int64_t n_embd_head = hparams .n_embd_head_v (); 109 110GGML_ASSERT (n_embd_head == hparams .n_embd_head_k ()); 111GGML_ASSERT (n_embd_head == n_rot ); 112 113ggml_tensor * cur ; 114ggml_tensor * inpL ; 115 116inpL = build_inp_embd (model .tok_embd ); 117 118// inp_pos - contains the positions 119ggml_tensor * inp_pos = build_inp_pos (); 120 121// temperature tuning 122ggml_tensor * inp_attn_scale = nullptr ; 123inp_attn_scale = build_inp_attn_scale (); 124 125using inp_attn_type = std::conditional_t < iswa ,llm_graph_input_attn_kv_iswa ,llm_graph_input_attn_kv > ; 126inp_attn_type * inp_attn = nullptr ; 127 128if constexpr (iswa ) { 129inp_attn = build_attn_inp_kv_iswa (); 130 }else { 131inp_attn = build_attn_inp_kv (); 132 } 133 134const float kq_scale = hparams .f_attention_scale == 0.0f ?1.0f /sqrtf (float (n_embd_head )) :hparams .f_attention_scale ; 135 136ggml_tensor * inp_out_ids = build_inp_out_ids (); 137 138for (int il = 0 ;il < n_layer ;++ il ) { 139const float freq_base_l = model .get_rope_freq_base (cparams ,il ); 140const float freq_scale_l = model .get_rope_freq_scale (cparams ,il ); 141 142ggml_tensor * inpSA = inpL ; 143 144// This overlaps with SWA layers in current models, so get_rope_freq_base/scale may be superfluous 145const bool use_rope = hparams .n_no_rope_layer_step > 0 && 146 (il + 1 ) %hparams .n_no_rope_layer_step != 0 ; 147 148// norm 149cur = build_norm (inpL , 150model .layers [il ].attn_norm ,NULL , 151LLM_NORM_RMS ,il ); 152cb (cur ,"attn_norm" ,il ); 153 154// self-attention 155 { 156// rope freq factors for llama3; may return nullptr for llama2 and other models 157ggml_tensor * rope_factors = model .get_rope_factors (cparams ,il ); 158 159// compute Q and K and RoPE them 160auto [Qcur ,Kcur ,Vcur ]= build_qkv (model .layers [il ],cur , 161n_embd_head ,n_head ,n_head_kv ,il ); 162 163if (use_rope ) { 164Qcur = ggml_rope_ext ( 165ctx0 ,Qcur ,inp_pos ,rope_factors , 166n_rot ,rope_type ,n_ctx_orig ,freq_base_l ,freq_scale_l , 167ext_factor ,attn_factor ,beta_fast ,beta_slow 168 ); 169 170Kcur = ggml_rope_ext ( 171ctx0 ,Kcur ,inp_pos ,rope_factors , 172n_rot ,rope_type ,n_ctx_orig ,freq_base_l ,freq_scale_l , 173ext_factor ,attn_factor ,beta_fast ,beta_slow 174 ); 175 }else if (inp_attn_scale ) { 176Qcur = ggml_mul (ctx0 ,Qcur ,inp_attn_scale ); 177 } 178cb (Qcur ,"Qcur" ,il ); 179cb (Kcur ,"Kcur" ,il ); 180cb (Vcur ,"Vcur" ,il ); 181 182if (use_rope && hparams .use_kq_norm ) { 183// Llama4TextL2Norm 184Qcur = ggml_rms_norm (ctx0 ,Qcur ,hparams .f_norm_rms_eps ); 185Kcur = ggml_rms_norm (ctx0 ,Kcur ,hparams .f_norm_rms_eps ); 186cb (Qcur ,"Qcur_normed" ,il ); 187cb (Kcur ,"Kcur_normed" ,il ); 188 } 189cur = build_attn (inp_attn , 190model .layers [il ].wo ,model .layers [il ].wo_b ,model .layers [il ].wo_s , 191Qcur ,Kcur ,Vcur ,nullptr ,nullptr ,nullptr ,kq_scale ,il ); 192cb (cur ,"attn_out" ,il ); 193 } 194if (il == n_layer - 1 && inp_out_ids ) { 195cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 196inpSA = ggml_get_rows (ctx0 ,inpSA ,inp_out_ids ); 197 } 198ggml_tensor * ffn_inp = ggml_add (ctx0 ,cur ,inpSA ); 199cb (ffn_inp ,"ffn_inp" ,il ); 200 201// feed-forward network (non-MoE) 202if (model .layers [il ].ffn_gate_inp == nullptr ) { 203cur = build_norm (ffn_inp , 204model .layers [il ].ffn_norm ,NULL , 205LLM_NORM_RMS ,il ); 206cb (cur ,"ffn_norm" ,il ); 207 208cur = build_ffn (cur , 209model .layers [il ].ffn_up ,model .layers [il ].ffn_up_b ,NULL , 210model .layers [il ].ffn_gate ,model .layers [il ].ffn_gate_b ,NULL , 211model .layers [il ].ffn_down ,model .layers [il ].ffn_down_b ,NULL , 212NULL , 213LLM_FFN_SILU ,LLM_FFN_PAR ,il ); 214cb (cur ,"ffn_out" ,il ); 215 }else { 216ggml_tensor * ffn_inp_normed = build_norm (ffn_inp , 217model .layers [il ].ffn_norm ,NULL , 218LLM_NORM_RMS ,il ); 219cb (cur ,"ffn_norm" ,il ); 220 221ggml_tensor * moe_out = build_moe_ffn (ffn_inp_normed , 222model .layers [il ].ffn_gate_inp , 223model .layers [il ].ffn_up_exps , 224model .layers [il ].ffn_gate_exps , 225model .layers [il ].ffn_down_exps , 226nullptr , 227n_expert ,n_expert_used , 228LLM_FFN_SILU , false, 229hparams .expert_weights_scale , 230LLAMA_EXPERT_GATING_FUNC_TYPE_SIGMOID , 231il ); 232 233// Shared experts 234ggml_tensor * shexp_out = build_ffn (ffn_inp_normed , 235model .layers [il ].ffn_up_shexp ,NULL ,NULL , 236model .layers [il ].ffn_gate_shexp ,NULL ,NULL , 237model .layers [il ].ffn_down_shexp ,NULL ,NULL , 238NULL , 239LLM_FFN_SILU ,LLM_FFN_PAR ,il ); 240cb (shexp_out ,"ffn_moe_shexp" ,il ); 241 242cur = ggml_add (ctx0 ,moe_out ,shexp_out ); 243cb (cur ,"ffn_moe_out_merged" ,il ); 244 } 245cur = ggml_add (ctx0 ,cur ,ffn_inp ); 246cb (cur ,"ffn_out" ,il ); 247 248cur = build_cvec (cur ,il ); 249cb (cur ,"l_out" ,il ); 250 251// input for next layer 252inpL = cur ; 253 } 254cur = inpL ; 255 256cur = build_norm (cur , 257model .output_norm ,NULL , 258LLM_NORM_RMS ,-1 ); 259 260cb (cur ,"result_norm" ,-1 ); 261res -> t_embd = cur ; 262 263// lm_head 264cur = build_lora_mm (model .output ,cur ,model .output_s ); 265 266cb (cur ,"result_output" ,-1 ); 267res -> t_logits = cur ; 268 269ggml_build_forward_expand (gf ,cur ); 270} 271 272// Explicit template instantiations 273template struct llama_model_llama4::graph < false> ; 274template struct llama_model_llama4:: graph < true > ;