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_afmoe::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_GATING_FUNC ,hparams .expert_gating_func , false); 9ml .get_key (LLM_KV_EXPERT_WEIGHTS_SCALE ,hparams .expert_weights_scale , false); 10ml .get_key (LLM_KV_EXPERT_WEIGHTS_NORM ,hparams .expert_weights_norm , false); 11ml .get_key (LLM_KV_ATTENTION_SLIDING_WINDOW ,hparams .n_swa , false); 12 13// Set up interleaved sliding window attention (ISWA) 14// Pattern: 3 sliding - 1 full (global_attn_every_n_layers = 4) 15if (hparams .n_swa > 0 ) { 16hparams .swa_type = LLAMA_SWA_TYPE_STANDARD ; 17uint32_t swa_period = 4 ; 18ml .get_key_or_arr (LLM_KV_ATTENTION_SLIDING_WINDOW_PATTERN ,swa_period , false); 19hparams .set_swa_pattern (swa_period ); 20 21hparams .rope_freq_base_train_swa = hparams .rope_freq_base_train ; 22hparams .rope_freq_scale_train_swa = hparams .rope_freq_scale_train ; 23ml .get_key (LLM_KV_ROPE_FREQ_BASE_SWA ,hparams .rope_freq_base_train_swa , false); 24 }else { 25hparams .swa_type = LLAMA_SWA_TYPE_NONE ; 26 } 27 28// Default to sigmoid if not set 29if (hparams .expert_gating_func == LLAMA_EXPERT_GATING_FUNC_TYPE_NONE ) { 30hparams .expert_gating_func = LLAMA_EXPERT_GATING_FUNC_TYPE_SIGMOID ; 31 } 32 33switch (hparams .n_layer ()) { 34case 56 :type = LLM_TYPE_6B ;break ; 35case 32 :type = LLM_TYPE_26B ;break ; 36default :type = LLM_TYPE_UNKNOWN ; 37 } 38} 39 40void llama_model_afmoe::load_arch_tensors (llama_model_loader & ) { 41LLAMA_LOAD_LOCALS ; 42const int64_t n_expert_shared = hparams .n_expert_shared ; 43 44tok_embd = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },0 ); 45 46// output 47output_norm = create_tensor (tn (LLM_TENSOR_OUTPUT_NORM ,"weight" ), {n_embd },0 ); 48output = create_tensor (tn (LLM_TENSOR_OUTPUT ,"weight" ), {n_embd ,n_vocab },TENSOR_NOT_REQUIRED ); 49 50// if output is NULL, init from the input tok embed 51if (output == NULL ) { 52output = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },TENSOR_DUPLICATED ); 53 } 54 55const int64_t n_ff_exp = hparams .n_ff_exp (); 56 57for (int i = 0 ;i < n_layer ;++ i ) { 58auto & layer = layers [i ]; 59 60// dual attention normalization 61layer .attn_norm = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"weight" ,i ), {n_embd },0 ); 62layer .attn_post_norm = create_tensor (tn (LLM_TENSOR_ATTN_POST_NORM ,"weight" ,i ), {n_embd },0 ); 63 64// attention projections 65create_tensor_qkv (layer ,i ,n_embd ,n_embd_head_k * n_head ,n_embd_k_gqa ,n_embd_v_gqa ,0 ); 66layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,i ), {n_embd_head_k * n_head ,n_embd },0 ); 67 68// Q/K normalization 69layer .attn_q_norm = create_tensor (tn (LLM_TENSOR_ATTN_Q_NORM ,"weight" ,i ), {n_embd_head_k },0 ); 70layer .attn_k_norm = create_tensor (tn (LLM_TENSOR_ATTN_K_NORM ,"weight" ,i ), {n_embd_head_k },0 ); 71 72// attention gating 73layer .wqkv_gate = create_tensor (tn (LLM_TENSOR_ATTN_GATE ,"weight" ,i ), {n_embd ,n_embd_head_k * n_head },0 ); 74 75// dual ffn normalization 76layer .ffn_norm = create_tensor (tn (LLM_TENSOR_FFN_NORM ,"weight" ,i ), {n_embd },0 ); 77layer .ffn_post_norm = create_tensor (tn (LLM_TENSOR_FFN_POST_NORM ,"weight" ,i ), {n_embd },0 ); 78 79if (static_cast < uint32_t > (i ) >=hparams .n_layer_dense_lead ) { 80// MoE layers 81layer .ffn_gate_inp = create_tensor (tn (LLM_TENSOR_FFN_GATE_INP ,"weight" ,i ), {n_embd ,n_expert },0 ); 82layer .ffn_exp_probs_b = create_tensor (tn (LLM_TENSOR_FFN_EXP_PROBS_B ,"bias" ,i ), {n_expert },0 ); 83 84// grouped expert weights 85layer .ffn_gate_exps = create_tensor (tn (LLM_TENSOR_FFN_GATE_EXPS ,"weight" ,i ), {n_embd ,n_ff_exp ,n_expert },0 ); 86layer .ffn_down_exps = create_tensor (tn (LLM_TENSOR_FFN_DOWN_EXPS ,"weight" ,i ), {n_ff_exp ,n_embd ,n_expert },0 ); 87layer .ffn_up_exps = create_tensor (tn (LLM_TENSOR_FFN_UP_EXPS ,"weight" ,i ), {n_embd ,n_ff_exp ,n_expert },0 ); 88 89// shared expert 90if (n_expert_shared > 0 ) { 91const int64_t n_ff_shexp = n_ff_exp * n_expert_shared ; 92layer .ffn_gate_shexp = create_tensor (tn (LLM_TENSOR_FFN_GATE_SHEXP ,"weight" ,i ), {n_embd ,n_ff_shexp },0 ); 93layer .ffn_down_shexp = create_tensor (tn (LLM_TENSOR_FFN_DOWN_SHEXP ,"weight" ,i ), {n_ff_shexp ,n_embd },0 ); 94layer .ffn_up_shexp = create_tensor (tn (LLM_TENSOR_FFN_UP_SHEXP ,"weight" ,i ), {n_embd ,n_ff_shexp },0 ); 95 } 96 }else { 97// Dense layers 98layer .ffn_gate = create_tensor (tn (LLM_TENSOR_FFN_GATE ,"weight" ,i ), {n_embd ,n_ff },0 ); 99layer .ffn_down = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"weight" ,i ), {n_ff ,n_embd },0 ); 100layer .ffn_up = create_tensor (tn (LLM_TENSOR_FFN_UP ,"weight" ,i ), {n_embd ,n_ff },0 ); 101 } 102 } 103} 104 105std::unique_ptr < llm_graph_context > llama_model_afmoe::build_arch_graph (const llm_graph_params & params )const { 106return std::make_unique < graph > (* this ,params ); 107} 108 109llama_model_afmoe::graph::graph (const llama_model & model ,const llm_graph_params & params ) :llm_graph_context (params ) { 110const int64_t n_embd_head = hparams .n_embd_head_v (); 111GGML_ASSERT (n_embd_head == hparams .n_embd_head_k ()); 112 113ggml_tensor * cur ; 114ggml_tensor * inpL ; 115 116inpL = build_inp_embd (model .tok_embd ); 117 118// MuP scaling: embeddings * sqrt(hidden_size) 119// mup_enabled = true, hidden_size = 1024, scale = 32.0 120inpL = ggml_scale (ctx0 ,inpL ,sqrtf (float (n_embd ))); 121cb (inpL ,"inp_embd_scaled" ,-1 ); 122 123// inp_pos - contains the positions 124ggml_tensor * inp_pos = build_inp_pos (); 125auto * inp_attn = build_attn_inp_kv_iswa (); 126ggml_tensor * inp_out_ids = build_inp_out_ids (); 127 128const float kq_scale = 1.0f /sqrtf (float (n_embd_head )); 129 130for (int il = 0 ;il < n_layer ;++ il ) { 131const float freq_base_l = model .get_rope_freq_base (cparams ,il ); 132const float freq_scale_l = model .get_rope_freq_scale (cparams ,il ); 133 134ggml_tensor * inpSA = inpL ; 135 136// This overlaps with SWA layers in current models, so get_rope_freq_base/scale may be superfluous 137const bool use_rope = hparams .n_no_rope_layer_step > 0 && 138 (il + 1 ) %hparams .n_no_rope_layer_step != 0 ; 139 140// dual attention normalization (pre) 141cur = build_norm (inpL , 142model .layers [il ].attn_norm ,NULL , 143LLM_NORM_RMS ,il ); 144cb (cur ,"attn_norm" ,il ); 145 146// self-attention 147 { 148ggml_tensor * attn_inp = cur ;// save input for gate computation 149 150auto [Qcur ,Kcur ,Vcur ]= build_qkv (model .layers [il ],cur , 151n_embd_head ,n_head ,n_head_kv ,il ); 152 153// compute gate from input 154ggml_tensor * gate = build_lora_mm (model .layers [il ].wqkv_gate ,attn_inp ); 155cb (gate ,"attn_gate_proj" ,il ); 156 157// Q/K normalization 158Qcur = build_norm (Qcur ,model .layers [il ].attn_q_norm ,NULL ,LLM_NORM_RMS ,il ); 159Kcur = build_norm (Kcur ,model .layers [il ].attn_k_norm ,NULL ,LLM_NORM_RMS ,il ); 160cb (Qcur ,"Qcur_normed" ,il ); 161cb (Kcur ,"Kcur_normed" ,il ); 162 163if (use_rope ) { 164Qcur = ggml_rope_ext ( 165ctx0 ,Qcur ,inp_pos ,nullptr , 166n_rot ,rope_type ,n_ctx_orig ,freq_base_l ,freq_scale_l , 167ext_factor ,attn_factor ,beta_fast ,beta_slow ); 168cb (Qcur ,"Qcur_rope" ,il ); 169 170Kcur = ggml_rope_ext ( 171ctx0 ,Kcur ,inp_pos ,nullptr , 172n_rot ,rope_type ,n_ctx_orig ,freq_base_l ,freq_scale_l , 173ext_factor ,attn_factor ,beta_fast ,beta_slow ); 174cb (Kcur ,"Kcur_rope" ,il ); 175 } 176 177cur = build_attn (inp_attn , 178NULL ,NULL ,NULL ,// wo will be applied after gating 179Qcur ,Kcur ,Vcur ,nullptr ,nullptr ,nullptr ,kq_scale ,il ); 180cb (cur ,"attn_out" ,il ); 181 182// attention gating: attn_out * sigmoid(gate) BEFORE o_proj 183gate = ggml_sigmoid (ctx0 ,gate ); 184cb (gate ,"attn_gate_sig" ,il ); 185cur = ggml_mul (ctx0 ,cur ,gate ); 186cb (cur ,"attn_gated" ,il ); 187 188// now apply output projection 189cur = build_lora_mm (model .layers [il ].wo ,cur ,model .layers [il ].wo_s ); 190cb (cur ,"attn_o_proj" ,il ); 191 } 192 193// dual attention normalization (post) 194cur = build_norm (cur , 195model .layers [il ].attn_post_norm ,NULL , 196LLM_NORM_RMS ,il ); 197cb (cur ,"attn_post_norm" ,il ); 198 199if (il == n_layer - 1 && inp_out_ids ) { 200cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 201inpSA = ggml_get_rows (ctx0 ,inpSA ,inp_out_ids ); 202 } 203 204ggml_tensor * ffn_inp = ggml_add (ctx0 ,cur ,inpSA ); 205cb (ffn_inp ,"ffn_inp" ,il ); 206 207// dual ffn normalization (pre) 208cur = build_norm (ffn_inp , 209model .layers [il ].ffn_norm ,NULL , 210LLM_NORM_RMS ,il ); 211cb (cur ,"ffn_norm" ,il ); 212 213// MoE or dense FFN 214if ((uint32_t )il >=hparams .n_layer_dense_lead ) { 215// MoE layer with sigmoid routing, normalization, and scaling 216ggml_tensor * moe_out = build_moe_ffn (cur , 217model .layers [il ].ffn_gate_inp , 218model .layers [il ].ffn_up_exps , 219model .layers [il ].ffn_gate_exps , 220model .layers [il ].ffn_down_exps , 221model .layers [il ].ffn_exp_probs_b , 222n_expert ,n_expert_used , 223LLM_FFN_SILU , 224hparams .expert_weights_norm ,// norm_w (route_norm=True) 225hparams .expert_weights_scale ,// w_scale (route_scale=2.826) 226 (llama_expert_gating_func_type )hparams .expert_gating_func , 227il ); 228cb (moe_out ,"ffn_moe_out" ,il ); 229 230// shared expert 231if (hparams .n_expert_shared > 0 ) { 232ggml_tensor * ffn_shexp = build_ffn (cur , 233model .layers [il ].ffn_up_shexp ,NULL ,NULL , 234model .layers [il ].ffn_gate_shexp ,NULL ,NULL , 235model .layers [il ].ffn_down_shexp ,NULL ,NULL , 236NULL , 237LLM_FFN_SILU ,LLM_FFN_PAR ,il ); 238cb (ffn_shexp ,"ffn_shexp" ,il ); 239 240cur = ggml_add (ctx0 ,moe_out ,ffn_shexp ); 241cb (cur ,"ffn_out" ,il ); 242 }else { 243cur = moe_out ; 244 } 245 }else { 246// dense layer 247cur = build_ffn (cur , 248model .layers [il ].ffn_up ,NULL ,NULL , 249model .layers [il ].ffn_gate ,NULL ,NULL , 250model .layers [il ].ffn_down ,NULL ,NULL , 251NULL , 252LLM_FFN_SILU ,LLM_FFN_PAR ,il ); 253cb (cur ,"ffn_out" ,il ); 254 } 255 256// dual ffn normalization (post) 257cur = build_norm (cur , 258model .layers [il ].ffn_post_norm ,NULL , 259LLM_NORM_RMS ,il ); 260cb (cur ,"ffn_post_norm" ,il ); 261 262cur = ggml_add (ctx0 ,cur ,ffn_inp ); 263cur = build_cvec (cur ,il ); 264cb (cur ,"l_out" ,il ); 265 266// input for next layer 267inpL = cur ; 268 } 269 270cur = inpL ; 271 272cur = build_norm (cur , 273model .output_norm ,NULL , 274LLM_NORM_RMS ,-1 ); 275cb (cur ,"result_norm" ,-1 ); 276 277res -> t_embd = cur ; 278 279// lm_head 280cur = build_lora_mm (model .output ,cur ,model .output_s ); 281cb (cur ,"result_output" ,-1 ); 282res -> t_logits = cur ; 283 284ggml_build_forward_expand (gf ,cur ); 285}