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_grok::load_arch_hparams (llama_model_loader & ml ) { 4// defaults for old GGUFs 5hparams .yarn_beta_fast = 8.0f ; 6hparams .f_logit_scale = 0.5773502691896257f ; 7hparams .f_embedding_scale = 78.38367176906169f ; 8hparams .f_attn_out_scale = 0.08838834764831845f ; 9hparams .f_attn_logit_softcapping = 30.0f ; 10hparams .f_router_logit_softcapping = 30.0f ; 11// no final_logit_softcapping in grok-1 12hparams .f_final_logit_softcapping = 0.0f ; 13 14ml .get_key (LLM_KV_ATTENTION_LAYERNORM_RMS_EPS ,hparams .f_norm_rms_eps ); 15ml .get_key_or_arr (LLM_KV_EXPERT_FEED_FORWARD_LENGTH ,hparams .n_ff_exp_arr ,hparams .n_layer_all , false); 16ml .get_key (LLM_KV_LOGIT_SCALE ,hparams .f_logit_scale , false); 17ml .get_key (LLM_KV_EMBEDDING_SCALE ,hparams .f_embedding_scale , false); 18ml .get_key (LLM_KV_ATTENTION_OUTPUT_SCALE ,hparams .f_attn_out_scale , false); 19ml .get_key (LLM_KV_ATTN_LOGIT_SOFTCAPPING ,hparams .f_attn_logit_softcapping , false); 20ml .get_key (LLM_KV_ROUTER_LOGIT_SOFTCAPPING ,hparams .f_router_logit_softcapping , false); 21ml .get_key (LLM_KV_FINAL_LOGIT_SOFTCAPPING ,hparams .f_final_logit_softcapping , false); 22 23ml .get_key (LLM_KV_ATTENTION_TEMPERATURE_LENGTH ,hparams .attn_temp_length , false); 24ml .get_key (LLM_KV_ROPE_SCALING_YARN_EXT_FACTOR ,hparams .yarn_ext_factor , false); 25ml .get_key (LLM_KV_ROPE_SCALING_YARN_ATTN_FACTOR ,hparams .yarn_attn_factor , false); 26ml .get_key (LLM_KV_ROPE_SCALING_YARN_BETA_FAST ,hparams .yarn_beta_fast , false); 27ml .get_key (LLM_KV_ROPE_SCALING_YARN_BETA_SLOW ,hparams .yarn_beta_slow , false); 28 29switch (hparams .n_layer ()) { 30case 64 :type = LLM_TYPE_314B ;break ; 31default :type = LLM_TYPE_UNKNOWN ; 32 } 33} 34 35void llama_model_grok::load_arch_tensors (llama_model_loader & ) { 36LLAMA_LOAD_LOCALS ; 37 38if (n_expert == 0 ) { 39throw std::runtime_error (arch_name ()+ " model cannot have zero experts" ); 40 } 41 42tok_embd = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },0 ); 43 44// output 45output_norm = create_tensor (tn (LLM_TENSOR_OUTPUT_NORM ,"weight" ), {n_embd },0 ); 46output = create_tensor (tn (LLM_TENSOR_OUTPUT ,"weight" ), {n_embd ,n_vocab },TENSOR_NOT_REQUIRED ); 47 48// if output is NULL, init from the input tok embed 49if (output == NULL ) { 50output = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },TENSOR_DUPLICATED ); 51 } 52 53const int64_t n_ff_exp = hparams .n_ff_exp () ?hparams .n_ff_exp () :n_ff /* / n_expert_used*/ ;// grok-1 n_ff_exp == n_ff 54for (int i = 0 ;i < n_layer ;++ i ) { 55auto & layer = layers [i ]; 56 57layer .attn_norm = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"weight" ,i ), {n_embd },0 ); 58 59create_tensor_qkv (layer ,i ,n_embd ,n_embd ,n_embd_gqa ,n_embd_gqa ,0 ); 60layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,i ), {n_embd ,n_embd },0 ); 61 62layer .attn_out_norm = create_tensor (tn (LLM_TENSOR_ATTN_OUT_NORM ,"weight" ,i ), {n_embd },0 ); 63 64layer .ffn_norm = create_tensor (tn (LLM_TENSOR_FFN_NORM ,"weight" ,i ), {n_embd },0 ); 65 66layer .ffn_gate = create_tensor (tn (LLM_TENSOR_FFN_GATE ,"weight" ,i ), {n_embd ,n_ff },TENSOR_NOT_REQUIRED ); 67layer .ffn_down = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"weight" ,i ), {n_ff ,n_embd },TENSOR_NOT_REQUIRED ); 68layer .ffn_up = create_tensor (tn (LLM_TENSOR_FFN_UP ,"weight" ,i ), {n_embd ,n_ff },TENSOR_NOT_REQUIRED ); 69 70layer .ffn_gate_inp = create_tensor (tn (LLM_TENSOR_FFN_GATE_INP ,"weight" ,i ), {n_embd ,n_expert },0 ); 71layer .ffn_gate_exps = create_tensor (tn (LLM_TENSOR_FFN_GATE_EXPS ,"weight" ,i ), {n_embd ,n_ff_exp ,n_expert },TENSOR_NOT_REQUIRED ); 72layer .ffn_down_exps = create_tensor (tn (LLM_TENSOR_FFN_DOWN_EXPS ,"weight" ,i ), {n_ff_exp ,n_embd ,n_expert },0 ); 73layer .ffn_up_exps = create_tensor (tn (LLM_TENSOR_FFN_UP_EXPS ,"weight" ,i ), {n_embd ,n_ff_exp ,n_expert },0 ); 74 75layer .ffn_post_norm = create_tensor (tn (LLM_TENSOR_LAYER_OUT_NORM ,"weight" ,i ), {n_embd },TENSOR_NOT_REQUIRED ); 76if (!layer .ffn_post_norm ) { 77layer .ffn_post_norm = create_tensor (tn (LLM_TENSOR_FFN_POST_NORM ,"weight" ,i ), {n_embd },0 ); 78 } 79 } 80} 81 82std::unique_ptr < llm_graph_context > llama_model_grok::build_arch_graph (const llm_graph_params & params )const { 83return std::make_unique < graph > (* this ,params ); 84} 85 86llama_model_grok::graph::graph (const llama_model & model ,const llm_graph_params & params ) :llm_graph_context (params ) { 87const int64_t n_embd_head = hparams .n_embd_head_v (); 88 89GGML_ASSERT (n_embd_head == hparams .n_embd_head_k ()); 90GGML_ASSERT (n_embd_head == n_rot ); 91 92ggml_tensor * cur ; 93ggml_tensor * inpL ; 94 95inpL = build_inp_embd (model .tok_embd ); 96 97// inp_pos - contains the positions 98ggml_tensor * inp_pos = build_inp_pos (); 99 100auto * inp_attn = build_attn_inp_kv (); 101 102ggml_tensor * inp_out_ids = build_inp_out_ids (); 103 104for (int il = 0 ;il < n_layer ;++ il ) { 105ggml_tensor * inpSA = inpL ; 106 107// norm 108cur = build_norm (inpL , 109model .layers [il ].attn_norm ,NULL , 110LLM_NORM_RMS ,il ); 111cb (cur ,"attn_norm" ,il ); 112 113// self-attention 114 { 115// compute Q and K and RoPE them 116auto [Qcur ,Kcur ,Vcur ]= build_qkv (model .layers [il ],cur , 117n_embd_head ,n_head ,n_head_kv ,il ); 118 119Qcur = ggml_rope_ext ( 120ctx0 ,Qcur ,inp_pos ,nullptr , 121n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 122ext_factor ,attn_factor ,beta_fast ,beta_slow 123 ); 124 125Kcur = ggml_rope_ext ( 126ctx0 ,Kcur ,inp_pos ,nullptr , 127n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 128ext_factor ,attn_factor ,beta_fast ,beta_slow 129 ); 130 131cb (Qcur ,"Qcur" ,il ); 132cb (Kcur ,"Kcur" ,il ); 133cb (Vcur ,"Vcur" ,il ); 134 135cur = build_attn (inp_attn , 136model .layers [il ].wo ,model .layers [il ].wo_b ,model .layers [il ].wo_s , 137Qcur ,Kcur ,Vcur ,nullptr ,nullptr ,nullptr ,1.0f ,il ); 138 } 139if (il == n_layer - 1 && inp_out_ids ) { 140cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 141inpSA = ggml_get_rows (ctx0 ,inpSA ,inp_out_ids ); 142 } 143cur = build_norm (cur , 144model .layers [il ].attn_out_norm ,NULL , 145LLM_NORM_RMS ,il ); 146cb (cur ,"attn_out_norm" ,il ); 147 148ggml_tensor * ffn_inp = ggml_add (ctx0 ,cur ,inpSA ); 149cb (ffn_inp ,"ffn_inp" ,il ); 150 151// feed-forward network 152cur = build_norm (ffn_inp , 153model .layers [il ].ffn_norm ,NULL , 154LLM_NORM_RMS ,il ); 155cb (cur ,"ffn_norm" ,il ); 156 157// MoE branch 158ggml_tensor * moe_out = build_moe_ffn (cur , 159model .layers [il ].ffn_gate_inp , 160model .layers [il ].ffn_up_exps , 161model .layers [il ].ffn_gate_exps , 162model .layers [il ].ffn_down_exps , 163nullptr , 164n_expert ,n_expert_used , 165LLM_FFN_GELU , true, 166hparams .expert_weights_scale , 167LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX , 168il ); 169cb (moe_out ,"ffn_moe_out" ,il ); 170 171if (model .layers [il ].ffn_up ) { 172ggml_tensor * ffn_out = build_ffn (cur , 173model .layers [il ].ffn_up ,NULL ,NULL , 174model .layers [il ].ffn_gate ,NULL ,NULL , 175model .layers [il ].ffn_down ,NULL ,NULL , 176NULL , 177LLM_FFN_GELU ,LLM_FFN_PAR ,il ); 178cb (ffn_out ,"ffn_out" ,il ); 179 180cur = ggml_scale (ctx0 ,ggml_add (ctx0 ,ffn_out ,moe_out ), std::sqrt (2 ) /2 ); 181cb (cur ,"ffn_out" ,il ); 182 }else { 183cur = moe_out ; 184 } 185cur = build_norm (cur , 186model .layers [il ].ffn_post_norm ,NULL , 187LLM_NORM_RMS ,il ); 188cb (cur ,"ffn_post_norm" ,il ); 189 190cur = ggml_add (ctx0 ,cur ,ffn_inp ); 191cb (cur ,"ffn_out" ,il ); 192 193cur = build_cvec (cur ,il ); 194cb (cur ,"l_out" ,il ); 195 196// input for next layer 197inpL = cur ; 198 } 199cur = inpL ; 200 201cur = build_norm (cur , 202model .output_norm ,NULL , 203LLM_NORM_RMS ,-1 ); 204 205cb (cur ,"result_norm" ,-1 ); 206res -> t_embd = cur ; 207 208// lm_head 209cur = build_lora_mm (model .output ,cur ,model .output_s ); 210 211cur = ggml_scale (ctx0 ,cur ,hparams .f_logit_scale ); 212 213// final logit soft-capping 214if (hparams .f_final_logit_softcapping ) { 215cur = ggml_scale (ctx0 ,cur ,1.0f /hparams .f_final_logit_softcapping ); 216cur = ggml_tanh (ctx0 ,cur ); 217cur = ggml_scale (ctx0 ,cur ,hparams .f_final_logit_softcapping ); 218 } 219cb (cur ,"result_output" ,-1 ); 220res -> t_logits = cur ; 221 222ggml_build_forward_expand (gf ,cur ); 223}