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_bailingmoe::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_LEADING_DENSE_BLOCK_COUNT ,hparams .n_layer_dense_lead , false); 6ml .get_key_or_arr (LLM_KV_EXPERT_FEED_FORWARD_LENGTH ,hparams .n_ff_exp_arr ,hparams .n_layer_all ); 7ml .get_key (LLM_KV_EXPERT_SHARED_COUNT ,hparams .n_expert_shared ); 8ml .get_key (LLM_KV_EXPERT_WEIGHTS_SCALE ,hparams .expert_weights_scale , false); 9ml .get_key (LLM_KV_EXPERT_WEIGHTS_NORM ,hparams .expert_weights_norm , false); 10 11switch (hparams .n_layer ()) { 12case 28 :type = LLM_TYPE_16B ;break ; 13case 88 :type = LLM_TYPE_290B ;break ; 14default :type = LLM_TYPE_UNKNOWN ; 15 } 16} 17 18void llama_model_bailingmoe::load_arch_tensors (llama_model_loader & ) { 19LLAMA_LOAD_LOCALS ; 20const int64_t n_expert_shared = hparams .n_expert_shared ; 21 22const int64_t n_ff_exp = hparams .n_ff_exp (); 23 24tok_embd = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },0 ); 25 26// output 27output_norm = create_tensor (tn (LLM_TENSOR_OUTPUT_NORM ,"weight" ), {n_embd },0 ); 28output = create_tensor (tn (LLM_TENSOR_OUTPUT ,"weight" ), {n_embd ,n_vocab },0 ); 29 30for (int i = 0 ;i < n_layer ;++ i ) { 31auto & layer = layers [i ]; 32 33layer .attn_norm = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"weight" ,i ), {n_embd },0 ); 34 35create_tensor_qkv (layer ,i ,n_embd ,n_head * n_rot ,n_head_kv * n_rot ,n_head_kv * n_rot ,0 ); 36layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,i ), {n_head * n_rot ,n_embd },0 ); 37layer .ffn_norm = create_tensor (tn (LLM_TENSOR_FFN_NORM ,"weight" ,i ), {n_embd },0 ); 38 39layer .ffn_gate_inp = create_tensor (tn (LLM_TENSOR_FFN_GATE_INP ,"weight" ,i ), {n_embd ,n_expert },0 ); 40 41if (n_expert == 0 ) { 42throw std::runtime_error ("n_expert must be > 0" ); 43 } 44if (n_expert_used == 0 ) { 45throw std::runtime_error ("n_expert_used must be > 0" ); 46 } 47 48layer .ffn_gate_exps = create_tensor (tn (LLM_TENSOR_FFN_GATE_EXPS ,"weight" ,i ), {n_embd ,n_ff_exp ,n_expert },0 ); 49layer .ffn_down_exps = create_tensor (tn (LLM_TENSOR_FFN_DOWN_EXPS ,"weight" ,i ), {n_ff_exp ,n_embd ,n_expert },0 ); 50layer .ffn_up_exps = create_tensor (tn (LLM_TENSOR_FFN_UP_EXPS ,"weight" ,i ), {n_embd ,n_ff_exp ,n_expert },0 ); 51 52layer .ffn_gate_shexp = create_tensor (tn (LLM_TENSOR_FFN_GATE_SHEXP ,"weight" ,i ), {n_embd ,n_ff_exp * n_expert_shared },0 ); 53layer .ffn_down_shexp = create_tensor (tn (LLM_TENSOR_FFN_DOWN_SHEXP ,"weight" ,i ), {n_ff_exp * n_expert_shared ,n_embd },0 ); 54layer .ffn_up_shexp = create_tensor (tn (LLM_TENSOR_FFN_UP_SHEXP ,"weight" ,i ), {n_embd ,n_ff_exp * n_expert_shared },0 ); 55 } 56} 57 58std::unique_ptr < llm_graph_context > llama_model_bailingmoe::build_arch_graph (const llm_graph_params & params )const { 59return std::make_unique < graph > (* this ,params ); 60} 61 62llama_model_bailingmoe::graph::graph (const llama_model & model ,const llm_graph_params & params ) :llm_graph_context (params ) { 63ggml_tensor * cur ; 64ggml_tensor * inpL ; 65 66inpL = build_inp_embd (model .tok_embd ); 67 68// inp_pos - contains the positions 69ggml_tensor * inp_pos = build_inp_pos (); 70 71auto * inp_attn = build_attn_inp_kv (); 72 73ggml_tensor * inp_out_ids = build_inp_out_ids (); 74 75for (int il = 0 ;il < n_layer ;++ il ) { 76ggml_tensor * inpSA = inpL ; 77 78// norm 79cur = build_norm (inpL , 80model .layers [il ].attn_norm ,NULL , 81LLM_NORM_RMS ,il ); 82cb (cur ,"attn_norm" ,il ); 83 84// self-attention 85 { 86// rope freq factors for llama3; may return nullptr for llama2 and other models 87ggml_tensor * rope_factors = model .get_rope_factors (cparams ,il ); 88 89// compute Q and K and RoPE them 90auto [Qcur ,Kcur ,Vcur ]= build_qkv (model .layers [il ],cur , 91n_embd_head_k ,n_head ,n_head_kv ,il ); 92 93Qcur = ggml_rope_ext ( 94ctx0 ,Qcur ,inp_pos ,rope_factors , 95n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 96ext_factor ,attn_factor ,beta_fast ,beta_slow 97 ); 98 99Kcur = ggml_rope_ext ( 100ctx0 ,Kcur ,inp_pos ,rope_factors , 101n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 102ext_factor ,attn_factor ,beta_fast ,beta_slow 103 ); 104 105cb (Qcur ,"Qcur" ,il ); 106cb (Kcur ,"Kcur" ,il ); 107cb (Vcur ,"Vcur" ,il ); 108 109cur = build_attn (inp_attn , 110model .layers [il ].wo ,model .layers [il ].wo_b ,model .layers [il ].wo_s , 111Qcur ,Kcur ,Vcur ,nullptr ,nullptr ,nullptr ,1.0f /sqrtf (float (n_rot )),il ); 112 } 113 114if (il == n_layer - 1 && inp_out_ids ) { 115cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 116inpSA = ggml_get_rows (ctx0 ,inpSA ,inp_out_ids ); 117 } 118 119ggml_tensor * ffn_inp = ggml_add (ctx0 ,cur ,inpSA ); 120cb (ffn_inp ,"ffn_inp" ,il ); 121 122cur = build_norm (ffn_inp , 123model .layers [il ].ffn_norm ,NULL , 124LLM_NORM_RMS ,il ); 125cb (cur ,"ffn_norm" ,il ); 126 127ggml_tensor * moe_out = 128build_moe_ffn (cur , 129model .layers [il ].ffn_gate_inp , 130model .layers [il ].ffn_up_exps , 131model .layers [il ].ffn_gate_exps , 132model .layers [il ].ffn_down_exps , 133nullptr , 134n_expert ,n_expert_used , 135LLM_FFN_SILU ,hparams .expert_weights_norm , 136hparams .expert_weights_scale , 137LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX , 138il ); 139cb (moe_out ,"ffn_moe_out" ,il ); 140 141// FFN shared expert 142 { 143ggml_tensor * ffn_shexp = build_ffn (cur , 144model .layers [il ].ffn_up_shexp ,NULL ,NULL , 145model .layers [il ].ffn_gate_shexp ,NULL ,NULL , 146model .layers [il ].ffn_down_shexp ,NULL ,NULL , 147NULL , 148LLM_FFN_SILU ,LLM_FFN_PAR ,il ); 149cb (ffn_shexp ,"ffn_shexp" ,il ); 150 151cur = ggml_add (ctx0 ,moe_out ,ffn_shexp ); 152cb (cur ,"ffn_out" ,il ); 153 } 154 155cur = ggml_add (ctx0 ,cur ,ffn_inp ); 156 157cur = build_cvec (cur ,il ); 158cb (cur ,"l_out" ,il ); 159 160// input for next layer 161inpL = cur ; 162 } 163 164cur = inpL ; 165 166cur = build_norm (cur , 167model .output_norm ,NULL , 168LLM_NORM_RMS ,-1 ); 169 170cb (cur ,"result_norm" ,-1 ); 171res -> t_embd = cur ; 172 173// lm_head 174cur = build_lora_mm (model .output ,cur ,model .output_s ); 175 176cb (cur ,"result_output" ,-1 ); 177res -> t_logits = cur ; 178 179ggml_build_forward_expand (gf ,cur ); 180}