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_ernie4_5::load_arch_hparams (llama_model_loader & ml ) { 4// paddleocr need mrope_section 5ml .get_key_or_arr (LLM_KV_ROPE_DIMENSION_SECTIONS ,hparams .rope_sections ,4 , false); 6 7ml .get_key (LLM_KV_ATTENTION_LAYERNORM_RMS_EPS ,hparams .f_norm_rms_eps ); 8if (arch == LLM_ARCH_ERNIE4_5_MOE ) { 9ml .get_key_or_arr (LLM_KV_EXPERT_FEED_FORWARD_LENGTH ,hparams .n_ff_exp_arr ,hparams .n_layer_all ); 10ml .get_key (LLM_KV_EXPERT_SHARED_FEED_FORWARD_LENGTH ,hparams .n_ff_shexp , false); 11ml .get_key (LLM_KV_INTERLEAVE_MOE_LAYER_STEP ,hparams .n_moe_layer_step ); 12ml .get_key (LLM_KV_LEADING_DENSE_BLOCK_COUNT ,hparams .n_layer_dense_lead , false); 13 } 14 15switch (hparams .n_layer ()) { 16case 18 :type = LLM_TYPE_0_3B ;break ; 17case 28 :type = LLM_TYPE_21B_A3B ;break ; 18case 54 :type = LLM_TYPE_300B_A47B ;break ; 19default :type = LLM_TYPE_UNKNOWN ; 20 } 21} 22 23void llama_model_ernie4_5::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_gqa ,n_embd_gqa ,0 ); 42layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,i ), {n_embd_head_k * n_head ,n_embd },0 ); 43 44// optional bias tensors 45layer .wo_b = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"bias" ,i ), {n_embd },TENSOR_NOT_REQUIRED ); 46 47layer .ffn_norm = create_tensor (tn (LLM_TENSOR_FFN_NORM ,"weight" ,i ), {n_embd },0 ); 48 49if (arch == LLM_ARCH_ERNIE4_5_MOE && static_cast < uint32_t > (i ) >=hparams .n_layer_dense_lead ) {// MoE layers 50int n_ff_exp = hparams .n_ff_exp (); 51 52layer .ffn_gate_inp = create_tensor (tn (LLM_TENSOR_FFN_GATE_INP ,"weight" ,i ), {n_embd ,n_expert },0 ); 53layer .ffn_exp_probs_b = create_tensor (tn (LLM_TENSOR_FFN_EXP_PROBS_B ,"bias" ,i ), {n_expert },TENSOR_NOT_REQUIRED ); 54layer .ffn_gate_exps = create_tensor (tn (LLM_TENSOR_FFN_GATE_EXPS ,"weight" ,i ), {n_embd ,n_ff_exp ,n_expert },TENSOR_NOT_REQUIRED ); 55layer .ffn_down_exps = create_tensor (tn (LLM_TENSOR_FFN_DOWN_EXPS ,"weight" ,i ), {n_ff_exp ,n_embd ,n_expert },0 ); 56layer .ffn_up_exps = create_tensor (tn (LLM_TENSOR_FFN_UP_EXPS ,"weight" ,i ), {n_embd ,n_ff_exp ,n_expert },0 ); 57 58// Shared expert (if present) 59if (hparams .n_ff_shexp > 0 ) { 60layer .ffn_gate_shexp = create_tensor (tn (LLM_TENSOR_FFN_GATE_SHEXP ,"weight" ,i ), {n_embd ,hparams .n_ff_shexp },0 ); 61layer .ffn_down_shexp = create_tensor (tn (LLM_TENSOR_FFN_DOWN_SHEXP ,"weight" ,i ), {hparams .n_ff_shexp ,n_embd },0 ); 62layer .ffn_up_shexp = create_tensor (tn (LLM_TENSOR_FFN_UP_SHEXP ,"weight" ,i ), {n_embd ,hparams .n_ff_shexp },0 ); 63 } 64 }else {// Dense layers 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 } 70} 71 72std::unique_ptr < llm_graph_context > llama_model_ernie4_5::build_arch_graph (const llm_graph_params & params )const { 73return std::make_unique < graph > (* this ,params ); 74} 75 76llama_model_ernie4_5::graph::graph (const llama_model & model ,const llm_graph_params & params ) : 77llm_graph_context (params ) { 78const int64_t n_embd_head = hparams .n_embd_head_v (); 79 80GGML_ASSERT (n_embd_head == hparams .n_embd_head_k ()); 81GGML_ASSERT (n_embd_head == n_rot ); 82 83ggml_tensor * cur ; 84ggml_tensor * inpL ; 85 86inpL = build_inp_embd (model .tok_embd ); 87 88// inp_pos - contains the positions 89ggml_tensor * inp_pos = build_inp_pos (); 90 91auto * inp_attn = build_attn_inp_kv (); 92 93ggml_tensor * inp_out_ids = build_inp_out_ids (); 94 95for (int il = 0 ;il < n_layer ;++ il ) { 96ggml_tensor * inpSA = inpL ; 97 98// norm 99 { 100cur = build_norm (inpL ,model .layers [il ].attn_norm ,NULL ,LLM_NORM_RMS ,il ); 101cb (cur ,"attn_norm" ,il ); 102 } 103// self-attention 104 { 105auto [Qcur ,Kcur ,Vcur ]= build_qkv (model .layers [il ],cur , 106n_embd_head ,n_head ,n_head_kv ,il ); 107 108Qcur = ggml_rope_ext (ctx0 ,Qcur ,inp_pos ,nullptr ,n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 109ext_factor ,attn_factor ,beta_fast ,beta_slow ); 110 111Kcur = ggml_rope_ext (ctx0 ,Kcur ,inp_pos ,nullptr ,n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 112ext_factor ,attn_factor ,beta_fast ,beta_slow ); 113 114cb (Qcur ,"Qcur" ,il ); 115cb (Kcur ,"Kcur" ,il ); 116cb (Vcur ,"Vcur" ,il ); 117 118cur = build_attn (inp_attn , 119model .layers [il ].wo ,NULL ,model .layers [il ].wo_s , 120Qcur ,Kcur ,Vcur ,nullptr ,nullptr ,nullptr ,1.0f /sqrtf (float (n_embd_head )),il ); 121 } 122if (il == n_layer - 1 ) { 123// skip computing output for unused tokens 124cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 125inpSA = ggml_get_rows (ctx0 ,inpSA ,inp_out_ids ); 126 } 127ggml_tensor * ffn_inp = ggml_add (ctx0 ,cur ,inpSA ); 128cb (ffn_inp ,"ffn_inp" ,il ); 129 130// feed-forward network 131 { 132cur = build_norm (ffn_inp ,model .layers [il ].ffn_norm ,NULL ,LLM_NORM_RMS ,il ); 133cb (cur ,"ffn_norm" ,il ); 134 135cur = build_ffn (cur , 136model .layers [il ].ffn_up ,NULL ,NULL , 137model .layers [il ].ffn_gate ,NULL ,NULL , 138model .layers [il ].ffn_down ,NULL ,NULL , 139NULL ,LLM_FFN_SILU ,LLM_FFN_PAR ,il ); 140cb (cur ,"ffn_out" ,il ); 141 } 142cur = ggml_add (ctx0 ,cur ,ffn_inp ); 143 144cur = build_cvec (cur ,il ); 145cb (cur ,"l_out" ,il ); 146 147// input for next layer 148inpL = cur ; 149 } 150cur = inpL ; 151 152cur = build_norm (cur ,model .output_norm ,NULL ,LLM_NORM_RMS ,-1 ); 153 154cb (cur ,"result_norm" ,-1 ); 155res -> t_embd = cur ; 156 157// lm_head 158cur = build_lora_mm (model .output ,cur ,model .output_s ); 159 160cb (cur ,"result_output" ,-1 ); 161res -> t_logits = cur ; 162 163ggml_build_forward_expand (gf ,cur ); 164}