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_minicpm3::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_Q_LORA_RANK ,hparams .n_lora_q ); 6ml .get_key (LLM_KV_ATTENTION_KV_LORA_RANK ,hparams .n_lora_kv ); 7 8switch (hparams .n_layer ()) { 9case 62 :type = LLM_TYPE_4B ;break ; 10default :type = LLM_TYPE_UNKNOWN ; 11 } 12} 13 14void llama_model_minicpm3::load_arch_tensors (llama_model_loader & ) { 15LLAMA_LOAD_LOCALS ; 16 17const int64_t n_embd_head_qk_rope = hparams .n_rot (); 18const int64_t n_embd_head_qk_nope = hparams .n_embd_head_k ()- hparams .n_rot (); 19 20const int64_t q_lora_rank = hparams .n_lora_q ; 21const int64_t kv_lora_rank = hparams .n_lora_kv ; 22tok_embd = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },0 ); 23 24// output 25output_norm = create_tensor (tn (LLM_TENSOR_OUTPUT_NORM ,"weight" ), {n_embd },0 ); 26output = create_tensor (tn (LLM_TENSOR_OUTPUT ,"weight" ), {n_embd ,n_vocab },TENSOR_NOT_REQUIRED ); 27 28// if output is NULL, init from the input tok embed 29if (output == NULL ) { 30output = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },TENSOR_DUPLICATED ); 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_q_a_norm = create_tensor (tn (LLM_TENSOR_ATTN_Q_A_NORM ,"weight" ,i ), {q_lora_rank },0 ); 38 39layer .attn_kv_a_norm = create_tensor (tn (LLM_TENSOR_ATTN_KV_A_NORM ,"weight" ,i ), {kv_lora_rank },0 ); 40 41layer .wq_a = create_tensor (tn (LLM_TENSOR_ATTN_Q_A ,"weight" ,i ), {n_embd ,q_lora_rank },0 ); 42layer .wq_b = create_tensor (tn (LLM_TENSOR_ATTN_Q_B ,"weight" ,i ), {q_lora_rank ,n_head * n_embd_head_k },0 ); 43 44layer .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 ); 45layer .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 ); 46layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,i ), {n_head * (n_embd_head_v ),n_embd },0 ); 47 48layer .ffn_norm = create_tensor (tn (LLM_TENSOR_FFN_NORM ,"weight" ,i ), {n_embd },0 ); 49 50layer .ffn_gate = create_tensor (tn (LLM_TENSOR_FFN_GATE ,"weight" ,i ), {n_embd ,n_ff },0 ); 51layer .ffn_down = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"weight" ,i ), {n_ff ,n_embd },0 ); 52layer .ffn_up = create_tensor (tn (LLM_TENSOR_FFN_UP ,"weight" ,i ), {n_embd ,n_ff },0 ); 53 54layer .rope_long = create_tensor (tn (LLM_TENSOR_ROPE_FACTORS_LONG ,"weight" ,i ), {n_embd_head_qk_rope /2 },TENSOR_NOT_REQUIRED | (i != 0 ?TENSOR_DUPLICATED :0 )); 55layer .rope_short = create_tensor (tn (LLM_TENSOR_ROPE_FACTORS_SHORT ,"weight" ,i ), {n_embd_head_qk_rope /2 },TENSOR_NOT_REQUIRED | (i != 0 ?TENSOR_DUPLICATED :0 )); 56 } 57} 58 59std::unique_ptr < llm_graph_context > llama_model_minicpm3::build_arch_graph (const llm_graph_params & params )const { 60return std::make_unique < graph > (* this ,params ); 61} 62 63llama_model_minicpm3::graph::graph (const llama_model & model ,const llm_graph_params & params ) :llm_graph_context (params ) { 64//TODO: if the model varies, these parameters need to be read from the model 65const int64_t n_embd_base = 256 ; 66const float scale_embd = 12.0f ; 67const float scale_depth = 1.4f ; 68const float kq_scale = 1.0f /sqrtf (float (hparams .n_embd_head_k ())); 69 70const uint32_t n_embd_head_qk_rope = hparams .n_rot (); 71const uint32_t n_embd_head_qk_nope = hparams .n_embd_head_k ()- hparams .n_rot (); 72 73const uint32_t kv_lora_rank = hparams .n_lora_kv ; 74 75ggml_tensor * cur ; 76ggml_tensor * inpL ; 77 78inpL = build_inp_embd (model .tok_embd ); 79 80// scale the input embeddings 81inpL = ggml_scale (ctx0 ,inpL ,scale_embd ); 82cb (inpL ,"inp_scaled" ,-1 ); 83 84// inp_pos - contains the positions 85ggml_tensor * inp_pos = build_inp_pos (); 86 87auto * inp_attn = build_attn_inp_kv (); 88 89ggml_tensor * inp_out_ids = build_inp_out_ids (); 90 91for (int il = 0 ;il < n_layer ;++ il ) { 92ggml_tensor * inpSA = inpL ; 93 94ggml_tensor * rope_factors = model .get_rope_factors (cparams ,il ); 95 96// norm 97cur = build_norm (inpL , 98model .layers [il ].attn_norm ,NULL , 99LLM_NORM_RMS ,il ); 100cb (cur ,"attn_norm" ,il ); 101 102// self_attention 103 { 104ggml_tensor * q = NULL ; 105// {n_embd, q_lora_rank} * {n_embd, n_tokens} -> {q_lora_rank, n_tokens} 106q = ggml_mul_mat (ctx0 ,model .layers [il ].wq_a ,cur ); 107cb (q ,"q" ,il ); 108 109q = build_norm (q , 110model .layers [il ].attn_q_a_norm ,NULL , 111LLM_NORM_RMS ,il ); 112cb (q ,"q" ,il ); 113 114// {q_lora_rank, n_head * hparams.n_embd_head_k()} * {q_lora_rank, n_tokens} -> {n_head * hparams.n_embd_head_k(), n_tokens} 115q = ggml_mul_mat (ctx0 ,model .layers [il ].wq_b ,q ); 116cb (q ,"q" ,il ); 117 118// {n_embd_head_k, n_head, n_tokens}, RoPE is applied to the trailing dims only 119q = ggml_reshape_3d (ctx0 ,q ,hparams .n_embd_head_k (),n_head ,n_tokens ); 120cb (q ,"q" ,il ); 121 122// {n_embd, kv_lora_rank + n_embd_head_qk_rope} * {n_embd, n_tokens} -> {kv_lora_rank + n_embd_head_qk_rope, n_tokens} 123ggml_tensor * kv_pe_compresseed = ggml_mul_mat (ctx0 ,model .layers [il ].wkv_a_mqa ,cur ); 124cb (kv_pe_compresseed ,"kv_pe_compresseed" ,il ); 125 126// split into {kv_lora_rank, n_tokens} 127ggml_tensor * kv_compressed = ggml_view_2d (ctx0 ,kv_pe_compresseed ,kv_lora_rank ,n_tokens , 128kv_pe_compresseed -> nb [1 ], 1290 ); 130cb (kv_compressed ,"kv_compressed" ,il ); 131 132// and {n_embd_head_qk_rope, n_tokens} 133ggml_tensor * k_pe = ggml_view_3d (ctx0 ,kv_pe_compresseed ,n_embd_head_qk_rope ,1 ,n_tokens , 134kv_pe_compresseed -> nb [1 ], 135kv_pe_compresseed -> nb [1 ], 136ggml_row_size (kv_pe_compresseed -> type ,kv_lora_rank )); 137cb (k_pe ,"k_pe" ,il ); 138 139kv_compressed = build_norm (kv_compressed , 140model .layers [il ].attn_kv_a_norm ,NULL , 141LLM_NORM_RMS ,il ); 142cb (kv_compressed ,"kv_compressed" ,il ); 143 144// {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} 145ggml_tensor * kv = ggml_mul_mat (ctx0 ,model .layers [il ].wkv_b ,kv_compressed ); 146cb (kv ,"kv" ,il ); 147 148// split into {n_head * n_embd_head_qk_nope, n_tokens} 149ggml_tensor * k_nope = ggml_view_3d (ctx0 ,kv ,n_embd_head_qk_nope ,n_head ,n_tokens , 150ggml_row_size (kv -> type ,n_embd_head_qk_nope + hparams .n_embd_head_v ()), 151ggml_row_size (kv -> type ,n_head * (n_embd_head_qk_nope + hparams .n_embd_head_v ())), 1520 ); 153cb (k_nope ,"k_nope" ,il ); 154 155// and {n_head * n_embd_head_v, n_tokens} 156ggml_tensor * v_states = ggml_view_3d (ctx0 ,kv ,hparams .n_embd_head_v (),n_head ,n_tokens , 157ggml_row_size (kv -> type , (n_embd_head_qk_nope + hparams .n_embd_head_v ())), 158ggml_row_size (kv -> type , (n_embd_head_qk_nope + hparams .n_embd_head_v ())* n_head ), 159ggml_row_size (kv -> type , (n_embd_head_qk_nope ))); 160cb (v_states ,"v_states" ,il ); 161 162v_states = ggml_cont (ctx0 ,v_states ); 163cb (v_states ,"v_states" ,il ); 164 165q = ggml_rope_ext ( 166ctx0 ,q ,inp_pos ,rope_factors , 167n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 168ext_factor ,attn_factor ,beta_fast ,beta_slow 169 ); 170q = ggml_rope_set_offset (q ,n_embd_head_qk_nope ); 171cb (q ,"q_rope" ,il ); 172 173// shared RoPE key 174k_pe = ggml_rope_ext ( 175ctx0 ,k_pe ,inp_pos ,rope_factors , 176n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 177ext_factor ,attn_factor ,beta_fast ,beta_slow 178 ); 179cb (k_pe ,"k_pe" ,il ); 180 181ggml_tensor * q_states = q ; 182cb (q_states ,"q_states" ,il ); 183 184ggml_tensor * k_states = ggml_concat (ctx0 ,k_nope , 185ggml_repeat_4d (ctx0 ,k_pe ,n_embd_head_qk_rope ,n_head ,n_tokens ,1 ),0 ); 186cb (k_states ,"k_states" ,il ); 187 188cur = build_attn (inp_attn , 189model .layers [il ].wo ,NULL ,model .layers [il ].wo_s , 190q_states ,k_states ,v_states ,nullptr ,nullptr ,nullptr ,kq_scale ,il ); 191 } 192if (il == n_layer - 1 && inp_out_ids ) { 193cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 194inpSA = ggml_get_rows (ctx0 ,inpSA ,inp_out_ids ); 195 } 196// scale_res - scale the hidden states for residual connection 197const float scale_res = scale_depth /sqrtf (float (n_layer ));// TODO: is this correct? 198cur = ggml_scale (ctx0 ,cur ,scale_res ); 199cb (cur ,"hidden_scaled" ,il ); 200 201ggml_tensor * ffn_inp = ggml_add (ctx0 ,cur ,inpSA ); 202cb (ffn_inp ,"ffn_inp" ,il ); 203 204// feed-forward network 205 { 206cur = build_norm (ffn_inp , 207model .layers [il ].ffn_norm ,NULL , 208LLM_NORM_RMS ,il ); 209cb (cur ,"ffn_norm" ,il ); 210 211cur = build_ffn (cur , 212model .layers [il ].ffn_up ,NULL ,NULL , 213model .layers [il ].ffn_gate ,NULL ,NULL , 214model .layers [il ].ffn_down ,NULL ,NULL , 215NULL , 216LLM_FFN_SILU ,LLM_FFN_PAR ,il ); 217cb (cur ,"ffn_out" ,il ); 218 } 219// scale the hidden states for residual connection 220cur = ggml_scale (ctx0 ,cur ,scale_res ); 221cb (cur ,"hidden_scaled_ffn" ,il ); 222 223cur = ggml_add (ctx0 ,cur ,ffn_inp ); 224 225cur = build_cvec (cur ,il ); 226cb (cur ,"l_out" ,il ); 227 228// input for next layer 229inpL = cur ; 230 } 231cur = inpL ; 232 233cur = build_norm (cur , 234model .output_norm ,NULL , 235LLM_NORM_RMS ,-1 ); 236 237cb (cur ,"result_norm" ,-1 ); 238res -> t_embd = cur ; 239 240// lm_head scaling 241const float scale_lmhead = float (n_embd_base )/float (n_embd ); 242cur = ggml_scale (ctx0 ,cur ,scale_lmhead ); 243cb (cur ,"lmhead_scaling" ,-1 ); 244 245// lm_head 246cur = build_lora_mm (model .output ,cur ,model .output_s ); 247 248cb (cur ,"result_output" ,-1 ); 249res -> t_logits = cur ; 250 251ggml_build_forward_expand (gf ,cur ); 252}