char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
42928bc14
char/aliceai-t5-moe
1#include "models.h" 2 3void llama_model_hunyuan_vl::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_ROPE_DIMENSION_SECTIONS ,hparams .rope_sections ,4 , false); 6 7// XDRoPE / NTK-aware scaling: base = rope_theta * alpha^(dim / (dim - 2)) 8if (hparams .rope_scaling_alpha > 0.0f ) { 9const int dim = hparams .n_embd_head_k (); 10hparams .rope_freq_base_train = hparams .rope_freq_base_train 11* powf (hparams .rope_scaling_alpha , (float )dim / (float )(dim - 2 )); 12 } 13 14switch (hparams .n_embd ) { 15case 1024 :type = LLM_TYPE_0_5B ;break ; 16case 2048 :type = LLM_TYPE_1_8B ;break ; 17case 3072 :type = LLM_TYPE_4B ;break ; 18case 4096 :type = LLM_TYPE_7B ;break ; 19default :type = LLM_TYPE_UNKNOWN ; 20 } 21} 22 23void llama_model_hunyuan_vl::load_arch_tensors (llama_model_loader & ) { 24LLAMA_LOAD_LOCALS ; 25 26tok_embd = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },0 ); 27 28// output 29output_norm = create_tensor (tn (LLM_TENSOR_OUTPUT_NORM ,"weight" ), {n_embd },0 ); 30output = create_tensor (tn (LLM_TENSOR_OUTPUT ,"weight" ), {n_embd ,n_vocab },TENSOR_NOT_REQUIRED ); 31// if output is NULL, init from the input tok embed 32if (output == NULL ) { 33output = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },TENSOR_DUPLICATED ); 34 } 35 36for (int i = 0 ;i < n_layer ;++ i ) { 37auto & layer = layers [i ]; 38 39layer .attn_norm = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"weight" ,i ), {n_embd },0 ); 40 41create_tensor_qkv (layer ,i ,n_embd ,n_embd_head_k * n_head ,n_embd_k_gqa ,n_embd_v_gqa ,0 ); 42layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,i ), {n_embd_head_k * n_head ,n_embd },0 ); 43 44layer .attn_k_norm = create_tensor (tn (LLM_TENSOR_ATTN_K_NORM ,"weight" ,i ), {n_embd_head_k },0 ); 45layer .attn_q_norm = create_tensor (tn (LLM_TENSOR_ATTN_Q_NORM ,"weight" ,i ), {n_embd_head_k },0 ); 46 47layer .ffn_norm = create_tensor (tn (LLM_TENSOR_FFN_NORM ,"weight" ,i ), {n_embd },0 ); 48 49layer .ffn_gate = create_tensor (tn (LLM_TENSOR_FFN_GATE ,"weight" ,i ), {n_embd ,n_ff },0 ); 50layer .ffn_down = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"weight" ,i ), {n_ff ,n_embd },0 ); 51layer .ffn_up = create_tensor (tn (LLM_TENSOR_FFN_UP ,"weight" ,i ), {n_embd ,n_ff },0 ); 52 53 } 54} 55 56std::unique_ptr < llm_graph_context > llama_model_hunyuan_vl::build_arch_graph (const llm_graph_params & params )const { 57return std::make_unique < graph > (* this ,params ); 58} 59 60llama_model_hunyuan_vl::graph::graph (const llama_model & model ,const llm_graph_params & params ) :llm_graph_context (params ) { 61const int64_t n_embd_head = hparams .n_embd_head_v (); 62 63GGML_ASSERT (n_embd_head == hparams .n_embd_head_k ()); 64GGML_ASSERT (n_embd_head == n_rot ); 65 66const bool use_mrope = hparams .use_mrope (); 67 68int sections [4 ]; 69 std::copy (std::begin (hparams .rope_sections ), std::begin (hparams .rope_sections )+ 4 ,sections ); 70 71ggml_tensor * cur ; 72ggml_tensor * inpL ; 73 74inpL = build_inp_embd (model .tok_embd ); 75 76// inp_pos - contains the positions 77ggml_tensor * inp_pos = build_inp_pos (); 78 79auto * inp_attn = build_attn_inp_kv (); 80 81const float kq_scale = 1.0f /sqrtf (float (n_embd_head )); 82 83ggml_tensor * inp_out_ids = build_inp_out_ids (); 84 85for (int il = 0 ;il < n_layer ;++ il ) { 86ggml_tensor * inpSA = inpL ; 87 88// norm 89cur = build_norm (inpL , 90model .layers [il ].attn_norm ,NULL , 91LLM_NORM_RMS ,il ); 92cb (cur ,"attn_norm" ,il ); 93// self-attention 94 { 95// rope freq factors for llama3; may return nullptr for llama2 and other models 96ggml_tensor * rope_factors = model .get_rope_factors (cparams ,il ); 97 98// compute Q and K and RoPE them 99auto [Qcur ,Kcur ,Vcur ]= build_qkv (model .layers [il ],cur , 100n_embd_head ,n_head ,n_head_kv ,il ); 101 102if (use_mrope ) { 103Qcur = ggml_rope_multi ( 104ctx0 ,Qcur ,inp_pos ,rope_factors , 105n_rot ,sections ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 106ext_factor ,attn_factor ,beta_fast ,beta_slow 107 ); 108 109Kcur = ggml_rope_multi ( 110ctx0 ,Kcur ,inp_pos ,rope_factors , 111n_rot ,sections ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 112ext_factor ,attn_factor ,beta_fast ,beta_slow 113 ); 114 }else { 115Qcur = ggml_rope_ext ( 116ctx0 ,Qcur ,inp_pos ,rope_factors , 117n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 118ext_factor ,attn_factor ,beta_fast ,beta_slow 119 ); 120 121Kcur = ggml_rope_ext ( 122ctx0 ,Kcur ,inp_pos ,rope_factors , 123n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 124ext_factor ,attn_factor ,beta_fast ,beta_slow 125 ); 126 } 127 128cb (Qcur ,"Qcur" ,il ); 129cb (Kcur ,"Kcur" ,il ); 130cb (Vcur ,"Vcur" ,il ); 131 132Kcur = build_norm (Kcur , 133model .layers [il ].attn_k_norm ,nullptr , 134LLM_NORM_RMS ,il ); 135cb (Kcur ,"Kcur_norm" ,il ); 136 137Qcur = build_norm (Qcur , 138model .layers [il ].attn_q_norm ,nullptr , 139LLM_NORM_RMS ,il ); 140cb (Qcur ,"Qcur_norm" ,il ); 141 142cur = build_attn (inp_attn , 143model .layers [il ].wo ,model .layers [il ].wo_b ,model .layers [il ].wo_s , 144Qcur ,Kcur ,Vcur ,nullptr ,nullptr ,nullptr ,kq_scale ,il ); 145cb (cur ,"attn_out" ,il ); 146 } 147if (il == n_layer - 1 && inp_out_ids ) { 148cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 149inpSA = ggml_get_rows (ctx0 ,inpSA ,inp_out_ids ); 150 } 151ggml_tensor * ffn_inp = ggml_add (ctx0 ,cur ,inpSA ); 152cb (ffn_inp ,"ffn_inp" ,il ); 153 154cur = build_norm (ffn_inp , 155model .layers [il ].ffn_norm ,NULL , 156LLM_NORM_RMS ,il ); 157cb (cur ,"ffn_norm" ,il ); 158// feed-forward network (non-MoE) 159ggml_tensor * cur_mlp = build_ffn (cur , 160model .layers [il ].ffn_up ,NULL ,NULL , 161model .layers [il ].ffn_gate ,NULL ,NULL , 162model .layers [il ].ffn_down ,NULL ,NULL , 163NULL , 164LLM_FFN_SILU ,LLM_FFN_PAR ,il ); 165cb (cur_mlp ,"ffn_out" ,il ); 166 167cur = ggml_add (ctx0 ,cur_mlp ,ffn_inp ); 168 169cur = build_cvec (cur ,il ); 170cb (cur ,"l_out" ,il ); 171 172// input for next layer 173inpL = cur ; 174 } 175cur = inpL ; 176 177cur = build_norm (cur , 178model .output_norm ,NULL , 179LLM_NORM_RMS ,-1 ); 180 181cb (cur ,"result_norm" ,-1 ); 182res -> t_embd = cur ; 183// lm_head 184cur = build_lora_mm (model .output ,cur ,model .output_s ); 185cb (cur ,"result_output" ,-1 ); 186res -> t_logits = cur ; 187 188ggml_build_forward_expand (gf ,cur ); 189}