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_chatglm::load_arch_hparams (llama_model_loader & ml ) { 4ml .get_key (LLM_KV_ATTENTION_LAYERNORM_RMS_EPS ,hparams .f_norm_rms_eps ); 5 6switch (hparams .n_layer ()) { 7case 28 : { 8if (hparams .n_head (0 )== 16 ) { 9type = LLM_TYPE_1_5B ; 10 }else { 11type = LLM_TYPE_6B ; 12 } 13 }break ; 14case 40 : { 15if (hparams .n_head (0 )== 24 ) { 16type = LLM_TYPE_4B ; 17 }else { 18type = LLM_TYPE_9B ; 19 } 20 }break ; 21default :type = LLM_TYPE_UNKNOWN ; 22 } 23} 24 25void llama_model_chatglm::load_arch_tensors (llama_model_loader & ) { 26LLAMA_LOAD_LOCALS ; 27 28tok_embd = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },0 ); 29 30// output 31output_norm = create_tensor (tn (LLM_TENSOR_OUTPUT_NORM ,"weight" ), {n_embd },0 ); 32output = create_tensor (tn (LLM_TENSOR_OUTPUT ,"weight" ), {n_embd ,n_vocab },TENSOR_NOT_REQUIRED ); 33// if output is NULL, init from the input tok embed 34if (output == NULL ) { 35output = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },TENSOR_DUPLICATED ); 36 } 37 38for (int i = 0 ;i < n_layer ;++ i ) { 39auto & layer = layers [i ]; 40 41layer .attn_norm = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"weight" ,i ), {n_embd },0 ); 42create_tensor_qkv (layer ,i ,n_embd ,n_embd_head_k * n_head ,n_embd_k_gqa ,n_embd_v_gqa ,0 ); 43 44layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,i ), {n_embd ,n_embd },0 ); 45 46layer .ffn_norm = create_tensor (tn (LLM_TENSOR_FFN_NORM ,"weight" ,i ), {n_embd },0 ); 47 48layer .ffn_up = create_tensor (tn (LLM_TENSOR_FFN_UP ,"weight" ,i ), {n_embd ,n_ff * 2 },0 ); 49 50layer .ffn_down = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"weight" ,i ), {n_ff ,n_embd },0 ); 51 } 52} 53 54std::unique_ptr < llm_graph_context > llama_model_chatglm::build_arch_graph (const llm_graph_params & params )const { 55return std::make_unique < graph > (* this ,params ); 56} 57 58llama_model_chatglm::graph::graph (const llama_model & model ,const llm_graph_params & params ) :llm_graph_context (params ) { 59const int64_t n_embd_head = hparams .n_embd_head_v (); 60 61GGML_ASSERT (n_embd_head == hparams .n_embd_head_k ()); 62 63ggml_tensor * cur ; 64ggml_tensor * inpL ; 65 66inpL = build_inp_embd (model .tok_embd ); 67 68// inp_pos - contains the positions 69ggml_tensor * inp_pos = build_inp_pos (); 70 71auto * inp_attn = build_attn_inp_kv (); 72 73ggml_tensor * inp_out_ids = build_inp_out_ids (); 74 75for (int il = 0 ;il < n_layer ;++ il ) { 76ggml_tensor * inpSA = inpL ; 77 78cur = build_norm (inpL , 79model .layers [il ].attn_norm , 80NULL , 81LLM_NORM_RMS ,il ); 82cb (cur ,"attn_norm" ,il ); 83 84// self-attention 85 { 86auto [Qcur ,Kcur ,Vcur ]= build_qkv (model .layers [il ],cur , 87n_embd_head ,n_head ,n_head_kv ,il ); 88 89//printf("freq_base: %f freq_scale: %f ext_factor: %f attn_factor: %f\n", freq_base, freq_scale, ext_factor, attn_factor); 90Qcur = ggml_rope_ext ( 91ctx0 ,Qcur ,inp_pos ,nullptr , 92n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 93ext_factor ,attn_factor ,beta_fast ,beta_slow 94 ); 95 96Kcur = ggml_rope_ext ( 97ctx0 ,Kcur ,inp_pos ,nullptr , 98n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 99ext_factor ,attn_factor ,beta_fast ,beta_slow 100 ); 101 102cb (Qcur ,"Qcur" ,il ); 103cb (Kcur ,"Kcur" ,il ); 104cb (Vcur ,"Vcur" ,il ); 105 106cur = build_attn (inp_attn , 107model .layers [il ].wo ,NULL ,model .layers [il ].wo_s , 108Qcur ,Kcur ,Vcur ,nullptr ,nullptr ,nullptr ,1.0f /sqrtf (float (n_embd_head )),il ); 109 } 110 111if (il == n_layer - 1 && inp_out_ids ) { 112cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 113inpSA = ggml_get_rows (ctx0 ,inpSA ,inp_out_ids ); 114 } 115 116// Add the input 117ggml_tensor * ffn_inp = ggml_add (ctx0 ,cur ,inpSA ); 118cb (ffn_inp ,"ffn_inp" ,il ); 119 120// FF 121 { 122cur = build_norm (ffn_inp , 123model .layers [il ].ffn_norm , 124NULL , 125LLM_NORM_RMS ,il ); 126cb (cur ,"ffn_norm" ,il ); 127 128cur = build_ffn (cur , 129model .layers [il ].ffn_up ,NULL ,NULL , 130NULL ,NULL ,NULL , 131model .layers [il ].ffn_down ,NULL ,NULL , 132NULL , 133LLM_FFN_SWIGLU ,LLM_FFN_SEQ ,il ); 134cb (cur ,"ffn_out" ,il ); 135 136 } 137 138cur = ggml_add (ctx0 ,cur ,ffn_inp ); 139 140cur = build_cvec (cur ,il ); 141cb (cur ,"l_out" ,il ); 142 143// input for next layer 144inpL = cur ; 145 } 146 147cur = build_norm (inpL , 148model .output_norm , 149NULL , 150LLM_NORM_RMS ,-1 ); 151 152cb (cur ,"result_norm" ,-1 ); 153res -> t_embd = cur ; 154 155cur = build_lora_mm (model .output ,cur ,model .output_s ); 156 157cb (cur ,"result_output" ,-1 ); 158res -> t_logits = cur ; 159 160ggml_build_forward_expand (gf ,cur ); 161}