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_bitnet::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 26 :type = LLM_TYPE_3B ;break ; 8default :type = LLM_TYPE_UNKNOWN ; 9 } 10} 11 12void llama_model_bitnet::load_arch_tensors (llama_model_loader & ) { 13LLAMA_LOAD_LOCALS ; 14 15tok_embd = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },0 ); 16 17// output 18output_norm = create_tensor (tn (LLM_TENSOR_OUTPUT_NORM ,"weight" ), {n_embd },0 ); 19 20for (int i = 0 ;i < n_layer ;++ i ) { 21auto & layer = layers [i ]; 22 23layer .attn_norm = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"weight" ,i ), {n_embd },0 ); 24layer .attn_sub_norm = create_tensor (tn (LLM_TENSOR_ATTN_SUB_NORM ,"weight" ,i ), {n_embd },0 ); 25 26layer .wq = create_tensor (tn (LLM_TENSOR_ATTN_Q ,"weight" ,i ), {n_embd ,n_embd },0 ); 27layer .wq_s = create_tensor (tn (LLM_TENSOR_ATTN_Q ,"scale" ,i ), {1 },TENSOR_NOT_REQUIRED ); 28layer .wk = create_tensor (tn (LLM_TENSOR_ATTN_K ,"weight" ,i ), {n_embd ,n_embd_gqa },0 ); 29layer .wk_s = create_tensor (tn (LLM_TENSOR_ATTN_K ,"scale" ,i ), {1 },TENSOR_NOT_REQUIRED ); 30layer .wv = create_tensor (tn (LLM_TENSOR_ATTN_V ,"weight" ,i ), {n_embd ,n_embd_gqa },0 ); 31layer .wv_s = create_tensor (tn (LLM_TENSOR_ATTN_V ,"scale" ,i ), {1 },TENSOR_NOT_REQUIRED ); 32layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,i ), {n_embd ,n_embd },0 ); 33layer .wo_s = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"scale" ,i ), {1 },TENSOR_NOT_REQUIRED ); 34 35layer .ffn_norm = create_tensor (tn (LLM_TENSOR_FFN_NORM ,"weight" ,i ), {n_embd },0 ); 36layer .ffn_sub_norm = create_tensor (tn (LLM_TENSOR_FFN_SUB_NORM ,"weight" ,i ), {n_ff },0 ); 37 38layer .ffn_gate = create_tensor (tn (LLM_TENSOR_FFN_GATE ,"weight" ,i ), {n_embd ,n_ff },0 ); 39layer .ffn_gate_s = create_tensor (tn (LLM_TENSOR_FFN_GATE ,"scale" ,i ), {1 },TENSOR_NOT_REQUIRED ); 40layer .ffn_down = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"weight" ,i ), {n_ff ,n_embd },0 ); 41layer .ffn_down_s = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"scale" ,i ), {1 },TENSOR_NOT_REQUIRED ); 42layer .ffn_up = create_tensor (tn (LLM_TENSOR_FFN_UP ,"weight" ,i ), {n_embd ,n_ff },0 ); 43layer .ffn_up_s = create_tensor (tn (LLM_TENSOR_FFN_UP ,"scale" ,i ), {1 },TENSOR_NOT_REQUIRED ); 44 } 45} 46 47std::unique_ptr < llm_graph_context > llama_model_bitnet::build_arch_graph (const llm_graph_params & params )const { 48return std::make_unique < graph > (* this ,params ); 49} 50 51llama_model_bitnet::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 ) { 69ggml_tensor * inpSA = inpL ; 70 71cur = build_norm (inpL , 72model .layers [il ].attn_norm ,NULL , 73LLM_NORM_RMS ,il ); 74cb (cur ,"attn_norm" ,il ); 75 76// self-attention 77 { 78auto [Qcur ,Kcur ,Vcur ]= build_qkv (model .layers [il ],cur , 79n_embd_head ,n_head ,n_head_kv ,il ); 80 81Qcur = ggml_rope_ext ( 82ctx0 ,Qcur ,inp_pos ,nullptr , 83n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 84ext_factor ,attn_factor ,beta_fast ,beta_slow 85 ); 86 87Kcur = ggml_rope_ext ( 88ctx0 ,Kcur ,inp_pos ,nullptr , 89n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 90ext_factor ,attn_factor ,beta_fast ,beta_slow 91 ); 92 93cb (Qcur ,"Qcur" ,il ); 94cb (Kcur ,"Kcur" ,il ); 95cb (Vcur ,"Vcur" ,il ); 96 97cur = build_attn (inp_attn , 98NULL ,NULL ,NULL , 99Qcur ,Kcur ,Vcur ,nullptr ,nullptr ,nullptr ,1.0f /sqrtf (float (n_embd_head )),il ); 100 101cur = build_norm (cur , 102model .layers [il ].attn_sub_norm ,NULL , 103LLM_NORM_RMS ,il ); 104cb (cur ,"attn_sub_norm" ,il ); 105 106cur = build_lora_mm (model .layers [il ].wo ,cur ,model .layers [il ].wo_s ); 107if (model .layers [il ].wo_b ) { 108cur = ggml_add (ctx0 ,cur ,model .layers [il ].wo_b ); 109 } 110cb (cur ,"attn_out" ,il ); 111 } 112 113if (il == n_layer - 1 && inp_out_ids ) { 114cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 115inpSA = ggml_get_rows (ctx0 ,inpSA ,inp_out_ids ); 116 } 117 118ggml_tensor * ffn_inp = ggml_add (ctx0 ,cur ,inpSA ); 119cb (ffn_inp ,"ffn_inp" ,il ); 120 121// feed-forward forward 122cur = build_norm (ffn_inp , 123model .layers [il ].ffn_norm ,NULL , 124LLM_NORM_RMS ,il ); 125cb (cur ,"ffn_norm" ,il ); 126 127cur = build_ffn (cur , 128model .layers [il ].ffn_up ,NULL ,model .layers [il ].ffn_up_s , 129model .layers [il ].ffn_gate ,NULL ,model .layers [il ].ffn_gate_s , 130NULL ,NULL ,NULL , 131NULL , 132LLM_FFN_SILU ,LLM_FFN_PAR ,il ); 133cb (cur ,"ffn_sub_out" ,il ); 134 135cur = build_norm (cur , 136model .layers [il ].ffn_sub_norm ,NULL , 137LLM_NORM_RMS ,il ); 138cb (cur ,"ffn_sub_norm" ,il ); 139 140cur = build_lora_mm (model .layers [il ].ffn_down ,cur ,model .layers [il ].ffn_down_s ); 141cb (cur ,"ffn_down" ,il ); 142 143cur = ggml_add (ctx0 ,cur ,ffn_inp ); 144cb (cur ,"l_out" ,il ); 145 146cur = build_cvec (cur ,il ); 147cb (cur ,"l_out" ,il ); 148 149// input for next layer 150inpL = cur ; 151 } 152 153cur = inpL ; 154 155cur = build_norm (cur , 156model .output_norm ,NULL , 157LLM_NORM_RMS ,-1 ); 158 159cb (cur ,"result_norm" ,-1 ); 160res -> t_embd = cur ; 161 162// lm_head 163// FIXME: do not use model.tok_embd directly, duplicate as model.output 164cur = build_lora_mm (model .tok_embd ,cur ); 165 166cb (cur ,"result_output" ,-1 ); 167res -> t_logits = cur ; 168 169ggml_build_forward_expand (gf ,cur ); 170}