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_starcoder::load_arch_hparams (llama_model_loader & ml ) { 4ml .get_key (LLM_KV_ATTENTION_LAYERNORM_EPS ,hparams .f_norm_eps ); 5 6switch (hparams .n_layer ()) { 7case 24 :type = LLM_TYPE_1B ;break ; 8case 36 :type = LLM_TYPE_3B ;break ; 9case 42 :type = LLM_TYPE_7B ;break ; 10case 40 :type = LLM_TYPE_15B ;break ; 11default :type = LLM_TYPE_UNKNOWN ; 12 } 13} 14 15void llama_model_starcoder::load_arch_tensors (llama_model_loader & ) { 16LLAMA_LOAD_LOCALS ; 17 18tok_embd = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },0 ); 19pos_embd = create_tensor (tn (LLM_TENSOR_POS_EMBD ,"weight" ), {n_embd ,n_ctx_train },0 ); 20 21// output 22 { 23output_norm = create_tensor (tn (LLM_TENSOR_OUTPUT_NORM ,"weight" ), {n_embd },0 ); 24output_norm_b = create_tensor (tn (LLM_TENSOR_OUTPUT_NORM ,"bias" ), {n_embd },0 ); 25output = create_tensor (tn (LLM_TENSOR_OUTPUT ,"weight" ), {n_embd ,n_vocab },TENSOR_NOT_REQUIRED ); 26if (!output ) { 27// needs to be on GPU 28output = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },TENSOR_DUPLICATED ); 29 } 30 31 } 32 33for (int i = 0 ;i < n_layer ;++ i ) { 34auto & layer = layers [i ]; 35 36layer .attn_norm = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"weight" ,i ), {n_embd },0 ); 37layer .attn_norm_b = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"bias" ,i ), {n_embd },0 ); 38 39layer .wqkv = create_tensor (tn (LLM_TENSOR_ATTN_QKV ,"weight" ,i ), {n_embd ,n_embd + 2 * n_embd_gqa },0 ); 40layer .wqkv_b = create_tensor (tn (LLM_TENSOR_ATTN_QKV ,"bias" ,i ), {n_embd + 2 * n_embd_gqa },0 ); 41 42layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,i ), {n_embd ,n_embd },0 ); 43layer .wo_b = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"bias" ,i ), {n_embd },0 ); 44 45layer .ffn_norm = create_tensor (tn (LLM_TENSOR_FFN_NORM ,"weight" ,i ), {n_embd },0 ); 46layer .ffn_norm_b = create_tensor (tn (LLM_TENSOR_FFN_NORM ,"bias" ,i ), {n_embd },0 ); 47 48layer .ffn_down = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"weight" ,i ), {n_ff ,n_embd },0 ); 49layer .ffn_down_b = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"bias" ,i ), {n_embd },0 ); 50 51layer .ffn_up = create_tensor (tn (LLM_TENSOR_FFN_UP ,"weight" ,i ), {n_embd ,n_ff },0 ); 52layer .ffn_up_b = create_tensor (tn (LLM_TENSOR_FFN_UP ,"bias" ,i ), {n_ff },0 ); 53 } 54} 55 56std::unique_ptr < llm_graph_context > llama_model_starcoder::build_arch_graph (const llm_graph_params & params )const { 57return std::make_unique < graph > (* this ,params ); 58} 59 60llama_model_starcoder::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 ()); 64 65ggml_tensor * cur ; 66ggml_tensor * inpL ; 67 68inpL = build_inp_embd (model .tok_embd ); 69 70// inp_pos - contains the positions 71ggml_tensor * inp_pos = build_inp_pos (); 72 73auto * inp_attn = build_attn_inp_kv (); 74 75ggml_tensor * pos = ggml_get_rows (ctx0 ,model .pos_embd ,inp_pos ); 76cb (pos ,"pos_embd" ,-1 ); 77 78inpL = ggml_add (ctx0 ,inpL ,pos ); 79cb (inpL ,"inpL" ,-1 ); 80 81ggml_tensor * inp_out_ids = build_inp_out_ids (); 82 83for (int il = 0 ;il < n_layer ;++ il ) { 84cur = build_norm (inpL , 85model .layers [il ].attn_norm , 86model .layers [il ].attn_norm_b , 87LLM_NORM ,il ); 88cb (cur ,"attn_norm" ,il ); 89 90// self-attention 91 { 92auto [Qcur ,Kcur ,Vcur ]= build_qkv (model .layers [il ],cur , 93n_embd_head ,n_head ,n_head_kv ,il ); 94 95cur = build_attn (inp_attn , 96model .layers [il ].wo ,model .layers [il ].wo_b ,model .layers [il ].wo_s , 97Qcur ,Kcur ,Vcur ,nullptr ,nullptr ,nullptr ,1.0f /sqrtf (float (n_embd_head )),il ); 98 } 99if (il == n_layer - 1 && inp_out_ids ) { 100cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 101inpL = ggml_get_rows (ctx0 ,inpL ,inp_out_ids ); 102 } 103// add the input 104ggml_tensor * ffn_inp = ggml_add (ctx0 ,cur ,inpL ); 105cb (ffn_inp ,"ffn_inp" ,il ); 106 107// FF 108 { 109cur = build_norm (ffn_inp , 110model .layers [il ].ffn_norm , 111model .layers [il ].ffn_norm_b , 112LLM_NORM ,il ); 113cb (cur ,"ffn_norm" ,il ); 114 115cur = build_ffn (cur , 116model .layers [il ].ffn_up ,model .layers [il ].ffn_up_b ,NULL , 117NULL ,NULL ,NULL , 118model .layers [il ].ffn_down ,model .layers [il ].ffn_down_b ,NULL , 119NULL , 120LLM_FFN_GELU ,LLM_FFN_SEQ ,il ); 121cb (cur ,"ffn_out" ,il ); 122 } 123cur = ggml_add (ctx0 ,cur ,ffn_inp ); 124 125cur = build_cvec (cur ,il ); 126cb (cur ,"l_out" ,il ); 127 128// input for next layer 129inpL = cur ; 130 } 131cur = build_norm (inpL , 132model .output_norm , 133model .output_norm_b , 134LLM_NORM ,-1 ); 135 136cb (cur ,"result_norm" ,-1 ); 137res -> t_embd = cur ; 138 139cur = build_lora_mm (model .output ,cur ,model .output_s ); 140 141cb (cur ,"result_output" ,-1 ); 142res -> t_logits = cur ; 143 144ggml_build_forward_expand (gf ,cur ); 145}