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