char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
7acb4e8cd
char/aliceai-t5-moe
1#include "models.h" 2 3void llama_model_apertus::load_arch_hparams (llama_model_loader & ml ) { 4ml .get_key (LLM_KV_ATTENTION_LAYERNORM_RMS_EPS ,hparams .f_norm_rms_eps ); 5 6ml .get_key_or_arr (LLM_KV_XIELU_ALPHA_N ,hparams .xielu_alpha_n ,hparams .n_layer ()); 7ml .get_key_or_arr (LLM_KV_XIELU_ALPHA_P ,hparams .xielu_alpha_p ,hparams .n_layer ()); 8ml .get_key_or_arr (LLM_KV_XIELU_BETA ,hparams .xielu_beta ,hparams .n_layer ()); 9ml .get_key_or_arr (LLM_KV_XIELU_EPS ,hparams .xielu_eps ,hparams .n_layer ()); 10 11switch (hparams .n_layer ()) { 12case 32 :type = LLM_TYPE_8B ;break ; 13default :type = LLM_TYPE_UNKNOWN ; 14 } 15} 16 17void llama_model_apertus::load_arch_tensors (llama_model_loader & ) { 18LLAMA_LOAD_LOCALS ; 19 20tok_embd = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },0 ); 21 22// output 23output_norm = create_tensor (tn (LLM_TENSOR_OUTPUT_NORM ,"weight" ), {n_embd },0 ); 24output = create_tensor (tn (LLM_TENSOR_OUTPUT ,"weight" ), {n_embd ,n_vocab },0 ); 25 26for (int i = 0 ;i < n_layer ;++ i ) { 27auto & layer = layers [i ]; 28 29layer .attn_norm = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"weight" ,i ), {n_embd },0 ); 30 31if (hparams .rope_scaling_type_train == LLAMA_ROPE_SCALING_TYPE_LONGROPE ) { 32layer .rope_long = create_tensor (tn (LLM_TENSOR_ROPE_FACTORS_LONG ,"weight" ,i ), {n_rot /2 },TENSOR_NOT_REQUIRED | (i != 0 ?TENSOR_DUPLICATED :0 )); 33layer .rope_short = create_tensor (tn (LLM_TENSOR_ROPE_FACTORS_SHORT ,"weight" ,i ), {n_rot /2 },TENSOR_NOT_REQUIRED | (i != 0 ?TENSOR_DUPLICATED :0 )); 34 }else { 35layer .rope_freqs = create_tensor (tn (LLM_TENSOR_ROPE_FREQS ,"weight" ,i ), {n_rot /2 },TENSOR_NOT_REQUIRED | (i != 0 ?TENSOR_DUPLICATED :0 )); 36 } 37 38create_tensor_qkv (layer ,i ,n_embd ,n_embd_head_k * n_head ,n_embd_gqa ,n_embd_gqa ,0 ); 39layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,i ), {n_embd_head_k * n_head ,n_embd },0 ); 40 41// optional bias tensors 42layer .wo_b = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"bias" ,i ), {n_embd },TENSOR_NOT_REQUIRED ); 43 44layer .ffn_norm = create_tensor (tn (LLM_TENSOR_FFN_NORM ,"weight" ,i ), {n_embd },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 48// Q and K layernorms for Apertus 49layer .attn_q_norm = create_tensor (tn (LLM_TENSOR_ATTN_Q_NORM ,"weight" ,i ), {n_embd_head_k },0 ); 50layer .attn_q_norm_b = create_tensor (tn (LLM_TENSOR_ATTN_Q_NORM ,"bias" ,i ), {n_embd_head_k },TENSOR_NOT_REQUIRED ); 51layer .attn_k_norm = create_tensor (tn (LLM_TENSOR_ATTN_K_NORM ,"weight" ,i ), {n_embd_head_k },0 ); 52layer .attn_k_norm_b = create_tensor (tn (LLM_TENSOR_ATTN_K_NORM ,"bias" ,i ), {n_embd_head_k },TENSOR_NOT_REQUIRED ); 53 } 54} 55 56std::unique_ptr < llm_graph_context > llama_model_apertus::build_arch_graph (const llm_graph_params & params )const { 57return std::make_unique < graph > (* this ,params ); 58} 59 60llama_model_apertus::graph::graph (const llama_model & model ,const llm_graph_params & params ) :llm_graph_context (params ) { 61const int64_t n_embd_head = hparams .n_embd_head_v (); 62 63GGML_ASSERT (n_embd_head == hparams .n_embd_head_k ()); 64GGML_ASSERT (n_embd_head == n_rot ); 65 66ggml_tensor * cur ; 67ggml_tensor * inpL ; 68 69inpL = build_inp_embd (model .tok_embd ); 70 71ggml_tensor * inp_pos = build_inp_pos (); 72auto * inp_attn = build_attn_inp_kv (); 73 74const float kq_scale = 75hparams .f_attention_scale == 0.0f ?1.0f /sqrtf (float (n_embd_head )) :hparams .f_attention_scale ; 76 77ggml_tensor * inp_out_ids = build_inp_out_ids (); 78 79for (int il = 0 ;il < n_layer ;++ il ) { 80ggml_tensor * inpSA = inpL ; 81 82cur = build_norm (inpL ,model .layers [il ].attn_norm ,nullptr ,LLM_NORM_RMS ,il ); 83cb (cur ,"attn_norm" ,il ); 84 85// self-attention 86 { 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 ,n_head ,n_head_kv ,il ); 92 93Qcur = build_norm (Qcur ,model .layers [il ].attn_q_norm ,NULL ,LLM_NORM_RMS ,il ); 94cb (Qcur ,"Qcur_normed" ,il ); 95 96Kcur = build_norm (Kcur ,model .layers [il ].attn_k_norm ,NULL ,LLM_NORM_RMS ,il ); 97cb (Kcur ,"Kcur_normed" ,il ); 98 99Qcur = ggml_rope_ext (ctx0 ,Qcur ,inp_pos ,rope_factors ,n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 100ext_factor ,attn_factor ,beta_fast ,beta_slow ); 101 102Kcur = ggml_rope_ext (ctx0 ,Kcur ,inp_pos ,rope_factors ,n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 103ext_factor ,attn_factor ,beta_fast ,beta_slow ); 104 105cb (Qcur ,"Qcur_pos" ,il ); 106cb (Kcur ,"Kcur_pos" ,il ); 107cb (Vcur ,"Vcur_pos" ,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 ,kq_scale ,il ); 112cb (cur ,"attn_out" ,il ); 113 } 114 115if (il == n_layer - 1 && inp_out_ids ) { 116cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 117inpSA = ggml_get_rows (ctx0 ,inpSA ,inp_out_ids ); 118 } 119 120ggml_tensor * ffn_inp = ggml_add (ctx0 ,cur ,inpSA ); 121cb (ffn_inp ,"ffn_inp" ,il ); 122 123// feed-forward network with xIELU activation 124 { 125cur = build_norm (ffn_inp ,model .layers [il ].ffn_norm ,nullptr ,LLM_NORM_RMS ,il ); 126cb (cur ,"ffn_norm" ,il ); 127 128// Up projection 129ggml_tensor * up = build_lora_mm (model .layers [il ].ffn_up ,cur ); 130cb (up ,"ffn_up" ,il ); 131 132float alpha_n_val = hparams .xielu_alpha_n [il ]; 133float alpha_p_val = hparams .xielu_alpha_p [il ]; 134float beta_val = hparams .xielu_beta [il ]; 135float eps_val = hparams .xielu_eps [il ]; 136 137// Apply xIELU activation 138ggml_tensor * activated = ggml_xielu (ctx0 ,up ,alpha_n_val ,alpha_p_val ,beta_val ,eps_val ); 139cb (activated ,"ffn_xielu" ,il ); 140 141// Down projection 142cur = build_lora_mm (model .layers [il ].ffn_down ,activated ); 143cb (cur ,"ffn_down" ,il ); 144 } 145 146cur = ggml_add (ctx0 ,cur ,ffn_inp ); 147cb (cur ,"ffn_out" ,il ); 148 149cur = build_cvec (cur ,il ); 150cb (cur ,"l_out" ,il ); 151 152// input for next layer 153inpL = cur ; 154 } 155 156cur = inpL ; 157 158cur = build_norm (cur ,model .output_norm ,nullptr ,LLM_NORM_RMS ,-1 ); 159 160cb (cur ,"result_norm" ,-1 ); 161res -> t_embd = cur ; 162 163// lm_head 164cur = build_lora_mm (model .output ,cur ,model .output_s ); 165 166cb (cur ,"result_output" ,-1 ); 167res -> t_logits = cur ; 168 169ggml_build_forward_expand (gf ,cur ); 170}