char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
4988f6e86
char/aliceai-t5-moe
1#include "models.h" 2 3void llama_model_cohere2::load_arch_hparams (llama_model_loader & ml ) { 4hparams .swa_type = LLAMA_SWA_TYPE_STANDARD ; 5uint32_t swa_period = 4 ; 6ml .get_key_or_arr (LLM_KV_ATTENTION_SLIDING_WINDOW_PATTERN ,swa_period , false); 7hparams .set_swa_pattern (swa_period ); 8 9hparams .rope_freq_base_train_swa = hparams .rope_freq_base_train ; 10hparams .rope_freq_scale_train_swa = hparams .rope_freq_scale_train ; 11 12ml .get_key (LLM_KV_ROPE_FREQ_BASE_SWA ,hparams .rope_freq_base_train_swa , false); 13ml .get_key (LLM_KV_ATTENTION_SLIDING_WINDOW ,hparams .n_swa ); 14ml .get_key (LLM_KV_LOGIT_SCALE ,hparams .f_logit_scale ); 15ml .get_key (LLM_KV_ATTENTION_LAYERNORM_EPS ,hparams .f_norm_eps ); 16 17switch (hparams .n_layer ()) { 18case 32 :type = LLM_TYPE_8B ;break ; 19default :type = LLM_TYPE_UNKNOWN ; 20 } 21} 22 23void llama_model_cohere2::load_arch_tensors (llama_model_loader & ) { 24LLAMA_LOAD_LOCALS ; 25 26tok_embd = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },0 ); 27 28// output 29output_norm = create_tensor (tn (LLM_TENSOR_OUTPUT_NORM ,"weight" ), {n_embd },0 ); 30// init output from the input tok embed 31output = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab }, 32TENSOR_DUPLICATED ); 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 ); 41 42layer .ffn_gate = create_tensor (tn (LLM_TENSOR_FFN_GATE ,"weight" ,i ), {n_embd ,n_ff },0 ); 43layer .ffn_down = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"weight" ,i ), {n_ff ,n_embd },0 ); 44layer .ffn_up = create_tensor (tn (LLM_TENSOR_FFN_UP ,"weight" ,i ), {n_embd ,n_ff },0 ); 45 } 46} 47 48std::unique_ptr < llm_graph_context > llama_model_cohere2::build_arch_graph (const llm_graph_params & params )const { 49return std::make_unique < graph > (* this ,params ); 50} 51 52llama_model_cohere2::graph::graph (const llama_model & model ,const llm_graph_params & params ) :llm_graph_context (params ) { 53const int64_t n_embd_head = hparams .n_embd_head_v (); 54 55GGML_ASSERT (n_embd_head == hparams .n_embd_head_k ()); 56 57const float f_logit_scale = hparams .f_logit_scale ; 58 59ggml_tensor * cur ; 60ggml_tensor * inpL ; 61 62inpL = build_inp_embd (model .tok_embd ); 63 64// inp_pos - contains the positions 65ggml_tensor * inp_pos = build_inp_pos (); 66 67auto * inp_attn = build_attn_inp_kv_iswa (); 68 69ggml_tensor * inp_out_ids = build_inp_out_ids (); 70 71for (int il = 0 ;il < n_layer ;++ il ) { 72const bool is_swa = hparams .is_swa (il ); 73// UNUSED: 74// const float freq_base_l = model.get_rope_freq_base (cparams, il); 75// const float freq_scale_l = model.get_rope_freq_scale(cparams, il); 76 77// norm 78cur = build_norm (inpL ,model .layers [il ].attn_norm ,NULL ,LLM_NORM ,il ); 79cb (cur ,"attn_norm" ,il ); 80ggml_tensor * ffn_inp = cur ; 81 82// self-attention 83 { 84// rope freq factors for 128k context 85ggml_tensor * rope_factors = model .get_rope_factors (cparams ,il ); 86 87// compute Q and K and RoPE them 88auto [Qcur ,Kcur ,Vcur ]= build_qkv (model .layers [il ],cur , 89n_embd_head ,n_head ,n_head_kv ,il ); 90 91if (is_swa ) { 92Qcur = ggml_rope_ext ( 93ctx0 ,Qcur ,inp_pos ,rope_factors , 94n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 95ext_factor ,attn_factor ,beta_fast ,beta_slow 96 ); 97 98Kcur = ggml_rope_ext ( 99ctx0 ,Kcur ,inp_pos ,rope_factors , 100n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 101ext_factor ,attn_factor ,beta_fast ,beta_slow 102 ); 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_embd_head )),il ); 112 } 113 114if (il == n_layer - 1 && inp_out_ids ) { 115cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 116inpL = ggml_get_rows (ctx0 ,inpL ,inp_out_ids ); 117ffn_inp = ggml_get_rows (ctx0 ,ffn_inp ,inp_out_ids ); 118 } 119 120ggml_tensor * attn_out = cur ; 121 122// feed-forward network 123 { 124cur = build_ffn (ffn_inp , 125model .layers [il ].ffn_up ,NULL ,model .layers [il ].ffn_up_s , 126model .layers [il ].ffn_gate ,NULL ,model .layers [il ].ffn_gate_s , 127model .layers [il ].ffn_down ,NULL ,model .layers [il ].ffn_down_s , 128NULL ,LLM_FFN_SILU ,LLM_FFN_PAR ,il ); 129cb (cur ,"ffn_out" ,il ); 130 } 131 132// add together residual + FFN + self-attention 133cur = ggml_add (ctx0 ,cur ,inpL ); 134cur = ggml_add (ctx0 ,cur ,attn_out ); 135 136cur = build_cvec (cur ,il ); 137cb (cur ,"l_out" ,il ); 138 139// input for next layer 140inpL = cur ; 141 } 142 143cur = inpL ; 144 145cur = build_norm (cur ,model .output_norm ,NULL ,LLM_NORM ,-1 ); 146 147cb (cur ,"result_norm" ,-1 ); 148res -> t_embd = cur ; 149 150// lm_head 151cur = build_lora_mm (model .output ,cur ,model .output_s ); 152 153if (f_logit_scale ) { 154cur = ggml_scale (ctx0 ,cur ,f_logit_scale ); 155 } 156 157cb (cur ,"result_output" ,-1 ); 158res -> t_logits = cur ; 159 160ggml_build_forward_expand (gf ,cur ); 161}