char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
873e5d8e3
char/aliceai-t5-moe
1#include "models.h" 2 3void llama_model_plm::load_arch_hparams (llama_model_loader & ml ) { 4ml .get_key (LLM_KV_ATTENTION_LAYERNORM_RMS_EPS ,hparams .f_norm_rms_eps ); 5ml .get_key (LLM_KV_ATTENTION_KV_LORA_RANK ,hparams .n_lora_kv ); 6 7switch (hparams .n_layer ()) { 8case 32 :type = LLM_TYPE_1_8B ;break ; 9default :type = LLM_TYPE_UNKNOWN ; 10 } 11} 12 13void llama_model_plm::load_arch_tensors (llama_model_loader & ) { 14LLAMA_LOAD_LOCALS ; 15 16const int64_t n_embd_head_qk_rope = hparams .n_rot (); 17const int64_t n_embd_head_qk_nope = hparams .n_embd_head_k ()- hparams .n_rot (); 18const int64_t kv_lora_rank = hparams .n_lora_kv ; 19 20tok_embd = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },0 ); 21 22// output 23output_norm = create_tensor (tn (LLM_TENSOR_OUTPUT_NORM ,"weight" ), {n_embd },0 ); 24// output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0); 25output = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },TENSOR_DUPLICATED ); 26 27for (int i = 0 ;i < n_layer ;++ i ) { 28auto & layer = layers [i ]; 29 30layer .attn_norm = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"weight" ,i ), {n_embd },0 ); 31 32layer .wq = create_tensor (tn (LLM_TENSOR_ATTN_Q ,"weight" ,i ), {n_embd ,n_embd_head_k * n_head },0 ); 33layer .wkv_a_mqa = create_tensor (tn (LLM_TENSOR_ATTN_KV_A_MQA ,"weight" ,i ), {n_embd ,kv_lora_rank + (n_embd_head_qk_rope )},0 ); 34layer .attn_kv_a_norm = create_tensor (tn (LLM_TENSOR_ATTN_KV_A_NORM ,"weight" ,i ), {kv_lora_rank },0 ); 35layer .wkv_b = create_tensor (tn (LLM_TENSOR_ATTN_KV_B ,"weight" ,i ), {kv_lora_rank ,n_head * (n_embd_head_qk_nope + n_embd_head_v )},0 ); 36layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,i ), {n_head * (n_embd_head_v ),n_embd },0 ); 37 38layer .ffn_norm = create_tensor (tn (LLM_TENSOR_FFN_NORM ,"weight" ,i ), {n_embd },0 ); 39layer .ffn_down = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"weight" ,i ), {n_ff ,n_embd },0 ); 40layer .ffn_up = create_tensor (tn (LLM_TENSOR_FFN_UP ,"weight" ,i ), {n_embd ,n_ff },0 ); 41 } 42} 43 44std::unique_ptr < llm_graph_context > llama_model_plm::build_arch_graph (const llm_graph_params & params )const { 45return std::make_unique < graph > (* this ,params ); 46} 47 48llama_model_plm::graph::graph (const llama_model & model ,const llm_graph_params & params ) :llm_graph_context (params ) { 49const float kq_scale = 1.0f /sqrtf (float (hparams .n_embd_head_k ())); 50 51const uint32_t n_embd_head_qk_rope = hparams .n_rot (); 52const uint32_t n_embd_head_qk_nope = hparams .n_embd_head_k ()- hparams .n_rot (); 53 54const uint32_t kv_lora_rank = hparams .n_lora_kv ; 55 56ggml_tensor * cur ; 57ggml_tensor * inpL ; 58 59// {n_embd, n_tokens} 60inpL = build_inp_embd (model .tok_embd ); 61 62// inp_pos - contains the positions 63ggml_tensor * inp_pos = build_inp_pos (); 64 65auto * inp_attn = build_attn_inp_kv (); 66 67ggml_tensor * inp_out_ids = build_inp_out_ids (); 68 69for (int il = 0 ;il < n_layer ;++ il ) { 70ggml_tensor * inpSA = inpL ; 71 72// norm 73cur = build_norm (inpL , 74model .layers [il ].attn_norm ,NULL , 75LLM_NORM_RMS ,il ); 76cb (cur ,"attn_norm" ,il ); 77 78// self_attention 79 { 80ggml_tensor * q = NULL ; 81q = ggml_mul_mat (ctx0 ,model .layers [il ].wq ,cur ); 82cb (q ,"q" ,il ); 83 84// {n_embd_head_k, n_head, n_tokens}, RoPE is applied to the trailing dims only 85q = ggml_reshape_3d (ctx0 ,q ,hparams .n_embd_head_k (),n_head ,n_tokens ); 86cb (q ,"q" ,il ); 87 88// {n_embd, kv_lora_rank + n_embd_head_qk_rope} * {n_embd, n_tokens} -> {kv_lora_rank + n_embd_head_qk_rope, n_tokens} 89ggml_tensor * kv_pe_compresseed = ggml_mul_mat (ctx0 ,model .layers [il ].wkv_a_mqa ,cur ); 90cb (kv_pe_compresseed ,"kv_pe_compresseed" ,il ); 91 92// split into {kv_lora_rank, n_tokens} 93ggml_tensor * kv_compressed = ggml_view_2d (ctx0 ,kv_pe_compresseed ,kv_lora_rank ,n_tokens , 94kv_pe_compresseed -> nb [1 ], 950 ); 96cb (kv_compressed ,"kv_compressed" ,il ); 97 98// and {n_embd_head_qk_rope, n_tokens} 99ggml_tensor * k_pe = ggml_view_3d (ctx0 ,kv_pe_compresseed ,n_embd_head_qk_rope ,1 ,n_tokens , 100kv_pe_compresseed -> nb [1 ], 101kv_pe_compresseed -> nb [1 ], 102ggml_row_size (kv_pe_compresseed -> type ,kv_lora_rank )); 103cb (k_pe ,"k_pe" ,il ); 104 105kv_compressed = build_norm (kv_compressed , 106model .layers [il ].attn_kv_a_norm ,NULL , 107LLM_NORM_RMS ,il ); 108cb (kv_compressed ,"kv_compressed" ,il ); 109 110// {kv_lora_rank, n_head * (n_embd_head_qk_nope + n_embd_head_v)} * {kv_lora_rank, n_tokens} -> {n_head * (n_embd_head_qk_nope + n_embd_head_v), n_tokens} 111ggml_tensor * kv = ggml_mul_mat (ctx0 ,model .layers [il ].wkv_b ,kv_compressed ); 112cb (kv ,"kv" ,il ); 113 114// split into {n_head * n_embd_head_qk_nope, n_tokens} 115ggml_tensor * k_nope = ggml_view_3d (ctx0 ,kv ,n_embd_head_qk_nope ,n_head ,n_tokens , 116ggml_row_size (kv -> type ,n_embd_head_qk_nope + hparams .n_embd_head_v ()), 117ggml_row_size (kv -> type ,n_head * (n_embd_head_qk_nope + hparams .n_embd_head_v ())), 1180 ); 119cb (k_nope ,"k_nope" ,il ); 120 121// and {n_head * n_embd_head_v, n_tokens} 122ggml_tensor * v_states = ggml_view_3d (ctx0 ,kv ,hparams .n_embd_head_v (),n_head ,n_tokens , 123ggml_row_size (kv -> type , (n_embd_head_qk_nope + hparams .n_embd_head_v ())), 124ggml_row_size (kv -> type , (n_embd_head_qk_nope + hparams .n_embd_head_v ())* n_head ), 125ggml_row_size (kv -> type , (n_embd_head_qk_nope ))); 126cb (v_states ,"v_states" ,il ); 127 128v_states = ggml_cont (ctx0 ,v_states ); 129cb (v_states ,"v_states" ,il ); 130 131v_states = ggml_view_2d (ctx0 ,v_states ,hparams .n_embd_head_v ()* n_head ,n_tokens , 132ggml_row_size (kv -> type ,hparams .n_embd_head_v ()* n_head ), 1330 ); 134cb (v_states ,"v_states" ,il ); 135 136q = ggml_rope_ext ( 137ctx0 ,q ,inp_pos ,nullptr , 138n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 139ext_factor ,attn_factor ,beta_fast ,beta_slow 140 ); 141q = ggml_rope_set_offset (q ,n_embd_head_qk_nope ); 142cb (q ,"q_rope" ,il ); 143 144// shared RoPE key 145k_pe = ggml_rope_ext ( 146ctx0 ,k_pe ,inp_pos ,nullptr , 147n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 148ext_factor ,attn_factor ,beta_fast ,beta_slow 149 ); 150cb (k_pe ,"k_pe" ,il ); 151 152ggml_tensor * q_states = q ; 153cb (q_states ,"q_states" ,il ); 154 155ggml_tensor * k_states = ggml_concat (ctx0 ,k_nope , 156ggml_repeat_4d (ctx0 ,k_pe ,n_embd_head_qk_rope ,n_head ,n_tokens ,1 ),0 ); 157cb (k_states ,"k_states" ,il ); 158 159cur = build_attn (inp_attn , 160model .layers [il ].wo ,NULL ,model .layers [il ].wo_s , 161q_states ,k_states ,v_states ,nullptr ,nullptr ,nullptr ,kq_scale ,il ); 162 } 163if (il == n_layer - 1 && inp_out_ids ) { 164cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 165inpSA = ggml_get_rows (ctx0 ,inpSA ,inp_out_ids ); 166 } 167ggml_tensor * ffn_inp = ggml_add (ctx0 ,cur ,inpSA ); 168cb (ffn_inp ,"ffn_inp" ,il ); 169 170cur = build_norm (ffn_inp , 171model .layers [il ].ffn_norm ,NULL , 172LLM_NORM_RMS ,il ); 173cb (cur ,"ffn_norm" ,il ); 174 175cur = build_ffn (cur , 176model .layers [il ].ffn_up ,NULL ,NULL , 177NULL ,NULL ,NULL , 178model .layers [il ].ffn_down ,NULL ,NULL , 179NULL , 180LLM_FFN_RELU_SQR ,LLM_FFN_SEQ ,il ); 181cb (cur ,"ffn_out" ,il ); 182 183cur = ggml_add (ctx0 ,cur ,ffn_inp ); 184 185cur = build_cvec (cur ,il ); 186cb (cur ,"l_out" ,il ); 187 188// input for next layer 189inpL = cur ; 190 } 191cur = inpL ; 192 193cur = build_norm (cur , 194model .output_norm ,NULL , 195LLM_NORM_RMS ,-1 ); 196 197cb (cur ,"result_norm" ,-1 ); 198res -> t_embd = cur ; 199 200cur = build_lora_mm (model .output ,cur ,model .output_s ); 201 202cb (cur ,"result_output" ,-1 ); 203res -> t_logits = cur ; 204 205ggml_build_forward_expand (gf ,cur ); 206}