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_minicpm::load_arch_hparams (llama_model_loader & ml ) { 4// Backward-compatible defaults for older MiniCPM GGUFs 5hparams .f_embedding_scale = 12.0f ; 6hparams .f_residual_scale = 1.4f /sqrtf (float (hparams .n_layer ())); 7hparams .f_logit_scale = hparams .n_embd ? (256.0f /float (hparams .n_embd )) :1.0f ; 8 9ml .get_key (LLM_KV_ATTENTION_LAYERNORM_RMS_EPS ,hparams .f_norm_rms_eps ); 10 11// Optional KV reads, override defaults if present in newer GGUF exports 12ml .get_key (LLM_KV_EMBEDDING_SCALE ,hparams .f_embedding_scale ,/*required=*/ false); 13ml .get_key (LLM_KV_RESIDUAL_SCALE ,hparams .f_residual_scale ,/*required=*/ false); 14ml .get_key (LLM_KV_LOGIT_SCALE ,hparams .f_logit_scale ,/*required=*/ false); 15 16// MiniCPM uses rope by default, unlike Granite which uses it as a switch 17hparams .rope_finetuned = true; 18 19switch (hparams .n_layer ()) { 20case 52 :type = LLM_TYPE_1B ;break ; 21case 40 :type = LLM_TYPE_2B ;break ; 22default :type = LLM_TYPE_UNKNOWN ; 23 } 24} 25 26void llama_model_minicpm::load_arch_tensors (llama_model_loader & ) { 27LLAMA_LOAD_LOCALS ; 28 29tok_embd = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },0 ); 30 31// output 32output_norm = create_tensor (tn (LLM_TENSOR_OUTPUT_NORM ,"weight" ), {n_embd },0 ); 33output = create_tensor (tn (LLM_TENSOR_OUTPUT ,"weight" ), {n_embd ,n_vocab },TENSOR_NOT_REQUIRED ); 34 35// if output is NULL, init from the input tok embed 36if (output == NULL ) { 37output = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },TENSOR_DUPLICATED ); 38 } 39 40for (int i = 0 ;i < n_layer ;++ i ) { 41auto & layer = layers [i ]; 42 43layer .attn_norm = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"weight" ,i ), {n_embd },0 ); 44 45create_tensor_qkv (layer ,i ,n_embd ,n_embd_head_k * n_head ,n_embd_k_gqa ,n_embd_v_gqa ,0 ); 46layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,i ), {n_embd_head_k * n_head ,n_embd },0 ); 47 48// optional bias tensors 49layer .wo_b = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"bias" ,i ), {n_embd },TENSOR_NOT_REQUIRED ); 50 51layer .ffn_norm = create_tensor (tn (LLM_TENSOR_FFN_NORM ,"weight" ,i ), {n_embd },0 ); 52 53if (hparams .rope_scaling_type_train == LLAMA_ROPE_SCALING_TYPE_LONGROPE ) { 54layer .rope_long = create_tensor (tn (LLM_TENSOR_ROPE_FACTORS_LONG ,"weight" ,i ), {n_rot /2 },TENSOR_NOT_REQUIRED | (i != 0 ?TENSOR_DUPLICATED :0 )); 55layer .rope_short = create_tensor (tn (LLM_TENSOR_ROPE_FACTORS_SHORT ,"weight" ,i ), {n_rot /2 },TENSOR_NOT_REQUIRED | (i != 0 ?TENSOR_DUPLICATED :0 )); 56 } 57else { 58layer .rope_freqs = create_tensor (tn (LLM_TENSOR_ROPE_FREQS ,"weight" ,i ), {n_rot /2 },TENSOR_NOT_REQUIRED | (i != 0 ?TENSOR_DUPLICATED :0 )); 59 } 60 61if (n_expert == 0 ) { 62layer .ffn_gate = create_tensor (tn (LLM_TENSOR_FFN_GATE ,"weight" ,i ), {n_embd ,n_ff },0 ); 63layer .ffn_down = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"weight" ,i ), {n_ff ,n_embd },0 ); 64layer .ffn_up = create_tensor (tn (LLM_TENSOR_FFN_UP ,"weight" ,i ), {n_embd ,n_ff },0 ); 65 66// optional MLP bias 67layer .ffn_gate_b = create_tensor (tn (LLM_TENSOR_FFN_GATE ,"bias" ,i ), {n_ff },TENSOR_NOT_REQUIRED ); 68layer .ffn_down_b = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"bias" ,i ), {n_embd },TENSOR_NOT_REQUIRED ); 69layer .ffn_up_b = create_tensor (tn (LLM_TENSOR_FFN_UP ,"bias" ,i ), {n_ff },TENSOR_NOT_REQUIRED ); 70 }else { 71layer .ffn_gate_inp = create_tensor (tn (LLM_TENSOR_FFN_GATE_INP ,"weight" ,i ), {n_embd ,n_expert },0 ); 72layer .ffn_gate_exps = create_tensor (tn (LLM_TENSOR_FFN_GATE_EXPS ,"weight" ,i ), {n_embd ,n_ff ,n_expert },TENSOR_NOT_REQUIRED ); 73layer .ffn_down_exps = create_tensor (tn (LLM_TENSOR_FFN_DOWN_EXPS ,"weight" ,i ), {n_ff ,n_embd ,n_expert },0 ); 74layer .ffn_up_exps = create_tensor (tn (LLM_TENSOR_FFN_UP_EXPS ,"weight" ,i ), {n_embd ,n_ff ,n_expert },0 ); 75 76// For Granite MoE Shared 77if (hparams .n_ff_shexp > 0 ) { 78layer .ffn_gate_shexp = create_tensor (tn (LLM_TENSOR_FFN_GATE_SHEXP ,"weight" ,i ), {n_embd ,hparams .n_ff_shexp },0 ); 79layer .ffn_up_shexp = create_tensor (tn (LLM_TENSOR_FFN_UP_SHEXP ,"weight" ,i ), {n_embd ,hparams .n_ff_shexp },0 ); 80layer .ffn_down_shexp = create_tensor (tn (LLM_TENSOR_FFN_DOWN_SHEXP ,"weight" ,i ), {hparams .n_ff_shexp ,n_embd },0 ); 81 } 82 } 83 } 84} 85 86std::unique_ptr < llm_graph_context > llama_model_minicpm::build_arch_graph (const llm_graph_params & params )const { 87return std::make_unique < graph > (* this ,params ); 88} 89