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_gptneox::load_arch_hparams (llama_model_loader & ml ) { 4ml .get_key (LLM_KV_ATTENTION_LAYERNORM_EPS ,hparams .f_norm_eps ); 5ml .get_key (LLM_KV_USE_PARALLEL_RESIDUAL ,hparams .use_par_res ); 6 7switch (hparams .n_layer ()) { 8case 6 : 9switch (hparams .n_ff ()) { 10case 512 :type = LLM_TYPE_14M ;break ; 11case 2048 :type = LLM_TYPE_70M ;break ; 12default :type = LLM_TYPE_UNKNOWN ; 13 }break ; 14case 12 : 15switch (hparams .n_ff ()) { 16case 3072 :type = LLM_TYPE_160M ;break ; 17default :type = LLM_TYPE_UNKNOWN ; 18 }break ; 19case 16 : 20switch (hparams .n_ff ()) { 21case 8192 :type = LLM_TYPE_1B ;break ; 22default :type = LLM_TYPE_UNKNOWN ; 23 }break ; 24case 24 : 25switch (hparams .n_ff ()) { 26case 4096 :type = LLM_TYPE_410M ;break ; 27case 8192 :type = LLM_TYPE_1_4B ;break ; 28default :type = LLM_TYPE_UNKNOWN ; 29 }break ; 30case 32 : 31switch (hparams .n_ff ()) { 32case 10240 :type = LLM_TYPE_2_8B ;break ; 33case 16384 :type = LLM_TYPE_6_9B ;break ; 34default :type = LLM_TYPE_UNKNOWN ; 35 }break ; 36case 36 : 37switch (hparams .n_ff ()) { 38case 20480 :type = LLM_TYPE_12B ;break ; 39default :type = LLM_TYPE_UNKNOWN ; 40 }break ; 41case 44 : 42switch (hparams .n_ff ()) { 43case 24576 :type = LLM_TYPE_20B ;break ; 44default :type = LLM_TYPE_UNKNOWN ; 45 }break ; 46default :type = LLM_TYPE_UNKNOWN ; 47 } 48} 49 50void llama_model_gptneox::load_arch_tensors (llama_model_loader & ) { 51LLAMA_LOAD_LOCALS ; 52 53tok_embd = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },0 ); 54 55// output 56output_norm = create_tensor (tn (LLM_TENSOR_OUTPUT_NORM ,"weight" ), {n_embd },0 ); 57output_norm_b = create_tensor (tn (LLM_TENSOR_OUTPUT_NORM ,"bias" ), {n_embd },0 ); 58output = create_tensor (tn (LLM_TENSOR_OUTPUT ,"weight" ), {n_embd ,n_vocab },0 ); 59 60for (int i = 0 ;i < n_layer ;++ i ) { 61auto & layer = layers [i ]; 62 63layer .attn_norm = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"weight" ,i ), {n_embd },0 ); 64layer .attn_norm_b = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"bias" ,i ), {n_embd },0 ); 65 66layer .wqkv = create_tensor (tn (LLM_TENSOR_ATTN_QKV ,"weight" ,i ), {n_embd ,n_embd + 2 * n_embd_gqa },0 ); 67layer .wqkv_b = create_tensor (tn (LLM_TENSOR_ATTN_QKV ,"bias" ,i ), {n_embd + 2 * n_embd_gqa },0 ); 68 69layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,i ), {n_embd ,n_embd },0 ); 70layer .wo_b = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"bias" ,i ), {n_embd },0 ); 71 72layer .ffn_norm = create_tensor (tn (LLM_TENSOR_FFN_NORM ,"weight" ,i ), {n_embd },0 ); 73layer .ffn_norm_b = create_tensor (tn (LLM_TENSOR_FFN_NORM ,"bias" ,i ), {n_embd },0 ); 74 75layer .ffn_down = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"weight" ,i ), {n_ff ,n_embd },0 ); 76layer .ffn_down_b = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"bias" ,i ), {n_embd },0 ); 77 78layer .ffn_up = create_tensor (tn (LLM_TENSOR_FFN_UP ,"weight" ,i ), {n_embd ,n_ff },0 ); 79layer .ffn_up_b = create_tensor (tn (LLM_TENSOR_FFN_UP ,"bias" ,i ), {n_ff },0 ); 80 } 81} 82 83std::unique_ptr < llm_graph_context > llama_model_gptneox::build_arch_graph (const llm_graph_params & params )const { 84return std::make_unique < graph > (* this ,params ); 85} 86 87llama_model_gptneox::graph::graph (const llama_model & model ,const llm_graph_params & params ) :llm_graph_context (params ) { 88const int64_t n_embd_head = hparams .n_embd_head_v (); 89 90GGML_ASSERT (n_embd_head == hparams .n_embd_head_k ()); 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 ) { 105cur = build_norm (inpL , 106model .layers [il ].attn_norm , 107model .layers [il ].attn_norm_b , 108LLM_NORM ,il ); 109cb (cur ,"attn_norm" ,il ); 110 111// self-attention 112 { 113auto [Qcur ,Kcur ,Vcur ]= build_qkv (model .layers [il ],cur , 114n_embd_head ,n_head ,n_head_kv ,il ); 115 116Qcur = ggml_rope_ext ( 117ctx0 ,Qcur ,inp_pos ,nullptr , 118n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 119ext_factor ,attn_factor ,beta_fast ,beta_slow 120 ); 121 122Kcur = ggml_rope_ext ( 123ctx0 ,Kcur ,inp_pos ,nullptr , 124n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 125ext_factor ,attn_factor ,beta_fast ,beta_slow 126 ); 127 128cb (Qcur ,"Qcur" ,il ); 129cb (Kcur ,"Kcur" ,il ); 130cb (Vcur ,"Vcur" ,il ); 131 132cur = build_attn (inp_attn , 133model .layers [il ].wo ,model .layers [il ].wo_b ,model .layers [il ].wo_s , 134Qcur ,Kcur ,Vcur ,nullptr ,nullptr ,nullptr ,1.0f /sqrtf (float (n_embd_head )),il ); 135 } 136 137if (il == n_layer - 1 && inp_out_ids ) { 138cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 139inpL = ggml_get_rows (ctx0 ,inpL ,inp_out_ids ); 140 } 141 142// ffn 143if (hparams .use_par_res ) { 144// attention and ffn are computed in parallel 145// x = x + attn(ln1(x)) + ffn(ln2(x)) 146 147ggml_tensor * attn_out = cur ; 148 149cur = build_norm (inpL , 150model .layers [il ].ffn_norm , 151model .layers [il ].ffn_norm_b , 152LLM_NORM ,il ); 153cb (cur ,"ffn_norm" ,il ); 154 155cur = build_ffn (cur , 156model .layers [il ].ffn_up ,model .layers [il ].ffn_up_b ,NULL , 157NULL ,NULL ,NULL , 158model .layers [il ].ffn_down ,model .layers [il ].ffn_down_b ,NULL , 159NULL , 160LLM_FFN_GELU ,LLM_FFN_SEQ ,il ); 161cb (cur ,"ffn_out" ,il ); 162 163cur = ggml_add (ctx0 ,cur ,inpL ); 164cb (cur ,"ffn_out" ,il ); 165 166cur = ggml_add (ctx0 ,cur ,attn_out ); 167 168cur = build_cvec (cur ,il ); 169cb (cur ,"l_out" ,il ); 170 171// input for next layer 172inpL = cur ; 173 }else { 174// attention and ffn are computed sequentially 175// x = x + attn(ln1(x)) 176// x = x + ffn(ln2(x)) 177 178ggml_tensor * ffn_inp = ggml_add (ctx0 ,cur ,inpL ); 179cb (ffn_inp ,"ffn_inp" ,il ); 180 181cur = build_norm (ffn_inp , 182model .layers [il ].ffn_norm , 183model .layers [il ].ffn_norm_b , 184LLM_NORM ,il ); 185cb (cur ,"ffn_norm" ,il ); 186 187cur = build_ffn (cur , 188model .layers [il ].ffn_up ,model .layers [il ].ffn_up_b ,NULL , 189NULL ,NULL ,NULL , 190model .layers [il ].ffn_down ,model .layers [il ].ffn_down_b ,NULL , 191NULL , 192LLM_FFN_GELU ,LLM_FFN_SEQ ,il ); 193cb (cur ,"ffn_out" ,il ); 194 195cur = ggml_add (ctx0 ,cur ,ffn_inp ); 196 197cur = build_cvec (cur ,il ); 198cb (cur ,"l_out" ,il ); 199 200// input for next layer 201inpL = cur ; 202 } 203 } 204 205cur = build_norm (inpL , 206model .output_norm , 207model .output_norm_b , 208LLM_NORM ,-1 ); 209 210cb (cur ,"result_norm" ,-1 ); 211res -> t_embd = cur ; 212 213cur = build_lora_mm (model .output ,cur ,model .output_s ); 214 215cb (cur ,"result_output" ,-1 ); 216res -> t_logits = cur ; 217 218ggml_build_forward_expand (gf ,cur ); 219}