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_stablelm::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 32 :type = LLM_TYPE_3B ;break ; 9case 40 :type = LLM_TYPE_12B ;break ; 10default :type = LLM_TYPE_UNKNOWN ; 11 } 12} 13 14void llama_model_stablelm::load_arch_tensors (llama_model_loader & ) { 15LLAMA_LOAD_LOCALS ; 16 17tok_embd = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },0 ); 18 19// output 20output_norm_b = create_tensor (tn (LLM_TENSOR_OUTPUT_NORM ,"bias" ), {n_embd },0 ); 21output_norm = create_tensor (tn (LLM_TENSOR_OUTPUT_NORM ,"weight" ), {n_embd },0 ); 22output = create_tensor (tn (LLM_TENSOR_OUTPUT ,"weight" ), {n_embd ,n_vocab },0 ); 23 24for (int i = 0 ;i < n_layer ;++ i ) { 25auto & layer = layers [i ]; 26 27layer .attn_norm = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"weight" ,i ), {n_embd },0 ); 28layer .attn_norm_b = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"bias" ,i ), {n_embd },0 ); 29 30create_tensor_qkv (layer ,i ,n_embd ,n_embd ,n_embd_gqa ,n_embd_gqa ,0 ); 31layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,i ), {n_embd ,n_embd },0 ); 32 33// optional q and k layernorms, present in StableLM 2 12B 34layer .attn_q_norm = create_tensor (tn (LLM_TENSOR_ATTN_Q_NORM ,"weight" ,i ), {n_embd_head_k ,n_head },TENSOR_NOT_REQUIRED ); 35layer .attn_k_norm = create_tensor (tn (LLM_TENSOR_ATTN_K_NORM ,"weight" ,i ), {n_embd_head_k ,n_head_kv },TENSOR_NOT_REQUIRED ); 36 37// optional FFN norm, not present in StableLM 2 12B which uses parallel residual 38layer .ffn_norm = create_tensor (tn (LLM_TENSOR_FFN_NORM ,"weight" ,i ), {n_embd },TENSOR_NOT_REQUIRED ); 39layer .ffn_norm_b = create_tensor (tn (LLM_TENSOR_FFN_NORM ,"bias" ,i ), {n_embd },TENSOR_NOT_REQUIRED ); 40 41layer .ffn_gate = create_tensor (tn (LLM_TENSOR_FFN_GATE ,"weight" ,i ), {n_embd ,n_ff },0 ); 42layer .ffn_down = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"weight" ,i ), {n_ff ,n_embd },0 ); 43layer .ffn_up = create_tensor (tn (LLM_TENSOR_FFN_UP ,"weight" ,i ), {n_embd ,n_ff },0 ); 44 } 45} 46 47std::unique_ptr < llm_graph_context > llama_model_stablelm::build_arch_graph (const llm_graph_params & params )const { 48return std::make_unique < graph > (* this ,params ); 49} 50 51llama_model_stablelm::graph::graph (const llama_model & model ,const llm_graph_params & params ) :llm_graph_context (params ) { 52const int64_t n_embd_head = hparams .n_embd_head_v (); 53 54GGML_ASSERT (n_embd_head == hparams .n_embd_head_k ()); 55 56ggml_tensor * cur ; 57ggml_tensor * inpL ; 58 59inpL = build_inp_embd (model .tok_embd ); 60 61// inp_pos - contains the positions 62ggml_tensor * inp_pos = build_inp_pos (); 63 64auto * inp_attn = build_attn_inp_kv (); 65 66ggml_tensor * inp_out_ids = build_inp_out_ids (); 67 68for (int il = 0 ;il < n_layer ;++ il ) { 69// norm 70cur = build_norm (inpL , 71model .layers [il ].attn_norm , 72model .layers [il ].attn_norm_b , 73LLM_NORM ,il ); 74cb (cur ,"attn_norm" ,il ); 75 76ggml_tensor * inpSA = cur ; 77 78// self-attention 79 { 80// compute Q and K and RoPE them 81auto [Qcur ,Kcur ,Vcur ]= build_qkv (model .layers [il ],cur , 82n_embd_head ,n_head ,n_head_kv ,il ); 83 84if (model .layers [il ].attn_q_norm ) { 85Qcur = build_norm (Qcur , 86model .layers [il ].attn_q_norm , 87NULL , 88LLM_NORM ,il ); 89cb (Qcur ,"Qcur" ,il ); 90 } 91if (model .layers [il ].attn_k_norm ) { 92Kcur = build_norm (Kcur , 93model .layers [il ].attn_k_norm , 94NULL , 95LLM_NORM ,il ); 96cb (Kcur ,"Kcur" ,il ); 97 } 98 99Qcur = ggml_rope_ext ( 100ctx0 ,Qcur ,inp_pos ,nullptr , 101n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 102ext_factor ,attn_factor ,beta_fast ,beta_slow 103 ); 104 105Kcur = ggml_rope_ext ( 106ctx0 ,Kcur ,inp_pos ,nullptr , 107n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 108ext_factor ,attn_factor ,beta_fast ,beta_slow 109 ); 110 111cb (Qcur ,"Qcur" ,il ); 112cb (Kcur ,"Kcur" ,il ); 113cb (Vcur ,"Vcur" ,il ); 114 115cur = build_attn (inp_attn , 116model .layers [il ].wo ,NULL ,model .layers [il ].wo_s , 117Qcur ,Kcur ,Vcur ,nullptr ,nullptr ,nullptr ,1.0f /sqrtf (float (n_embd_head )),il ); 118 } 119if (il == n_layer - 1 && inp_out_ids ) { 120cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 121inpL = ggml_get_rows (ctx0 ,inpL ,inp_out_ids ); 122inpSA = ggml_get_rows (ctx0 ,inpSA ,inp_out_ids ); 123 } 124ggml_tensor * ffn_inp = ggml_add (ctx0 ,cur ,inpL ); 125cb (ffn_inp ,"ffn_inp" ,il ); 126 127// feed-forward network 128 { 129if (model .layers [il ].ffn_norm ) { 130cur = build_norm (ffn_inp , 131model .layers [il ].ffn_norm , 132model .layers [il ].ffn_norm_b , 133LLM_NORM ,il ); 134cb (cur ,"ffn_norm" ,il ); 135 }else { 136// parallel residual 137cur = inpSA ; 138 } 139cur = build_ffn (cur , 140model .layers [il ].ffn_up ,NULL ,NULL , 141model .layers [il ].ffn_gate ,NULL ,NULL , 142model .layers [il ].ffn_down ,NULL ,NULL , 143NULL , 144LLM_FFN_SILU ,LLM_FFN_PAR ,il ); 145cb (cur ,"ffn_out" ,il ); 146 } 147cur = ggml_add (ctx0 ,cur ,ffn_inp ); 148 149cur = build_cvec (cur ,il ); 150cb (cur ,"l_out" ,il ); 151 152// input for next layer 153inpL = cur ; 154 } 155cur = inpL ; 156 157cur = build_norm (cur , 158model .output_norm , 159model .output_norm_b , 160LLM_NORM ,-1 ); 161 162cb (cur ,"result_norm" ,-1 ); 163res -> t_embd = cur ; 164 165// lm_head 166cur = build_lora_mm (model .output ,cur ,model .output_s ); 167 168cb (cur ,"result_output" ,-1 ); 169res -> t_logits = cur ; 170 171ggml_build_forward_expand (gf ,cur ); 172}