char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
465e49b9c
char/aliceai-t5-moe
1#include "models.h" 2 3void llama_model_deepseek2ocr::load_arch_hparams (llama_model_loader & ml ) { 4// similar to deepseek2, but without MLA 5ml .get_key (LLM_KV_ATTENTION_LAYERNORM_RMS_EPS ,hparams .f_norm_rms_eps ); 6ml .get_key (LLM_KV_LEADING_DENSE_BLOCK_COUNT ,hparams .n_layer_dense_lead , false); 7ml .get_key_or_arr (LLM_KV_EXPERT_FEED_FORWARD_LENGTH ,hparams .n_ff_exp_arr ,hparams .n_layer_all ); 8ml .get_key (LLM_KV_EXPERT_SHARED_COUNT ,hparams .n_expert_shared ); 9ml .get_key (LLM_KV_EXPERT_WEIGHTS_SCALE ,hparams .expert_weights_scale , false); 10ml .get_key (LLM_KV_EXPERT_WEIGHTS_NORM ,hparams .expert_weights_norm , false); 11ml .get_key (LLM_KV_EXPERT_GATING_FUNC ,hparams .expert_gating_func , false); 12 13if (hparams .expert_gating_func == LLAMA_EXPERT_GATING_FUNC_TYPE_NONE ) { 14hparams .expert_gating_func = LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX ; 15 } 16 17switch (hparams .n_layer ()) { 18case 12 :type = LLM_TYPE_3B ;break ; 19default :type = LLM_TYPE_UNKNOWN ; 20 } 21} 22 23void llama_model_deepseek2ocr::load_arch_tensors (llama_model_loader & ) { 24LLAMA_LOAD_LOCALS ; 25const int64_t n_expert_shared = hparams .n_expert_shared ; 26 27// similar to deepseek2, but without MLA 28const int64_t n_ff_exp = hparams .n_ff_exp (); 29 30tok_embd = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },0 ); 31 32// output 33output_norm = create_tensor (tn (LLM_TENSOR_OUTPUT_NORM ,"weight" ), {n_embd },0 ); 34// try to load output.weight, if not found, use token_embd (tied embeddings) 35output = create_tensor (tn (LLM_TENSOR_OUTPUT ,"weight" ), {n_embd ,n_vocab },TENSOR_NOT_REQUIRED ); 36if (!output ) { 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 43create_tensor_qkv (layer ,i ,n_embd ,n_embd ,n_embd ,n_embd ,0 ); 44layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,i ), {n_embd ,n_embd },0 ); 45 46// norm 47layer .ffn_norm = create_tensor (tn (LLM_TENSOR_FFN_NORM ,"weight" ,i ), {n_embd },0 ); 48layer .attn_norm = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"weight" ,i ), {n_embd },0 ); 49 50if (i < (int )hparams .n_layer_dense_lead ) { 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 ); 53layer .ffn_gate = create_tensor (tn (LLM_TENSOR_FFN_GATE ,"weight" ,i ), {n_embd ,n_ff },0 ); 54 }else { 55layer .ffn_gate_inp = create_tensor (tn (LLM_TENSOR_FFN_GATE_INP ,"weight" ,i ), {n_embd ,n_expert },0 ); 56layer .ffn_exp_probs_b = create_tensor (tn (LLM_TENSOR_FFN_EXP_PROBS_B ,"bias" ,i ), {n_expert },TENSOR_NOT_REQUIRED ); 57 58if (n_expert == 0 ) { 59throw std::runtime_error ("n_expert must be > 0" ); 60 } 61if (n_expert_used == 0 ) { 62throw std::runtime_error ("n_expert_used must be > 0" ); 63 } 64 65// MoE branch 66layer .ffn_down_exps = create_tensor (tn (LLM_TENSOR_FFN_DOWN_EXPS ,"weight" ,i ), {n_ff_exp ,n_embd ,n_expert },0 ); 67create_tensor_gate_up_exps (layer ,i ,n_embd ,n_ff_exp ,n_expert ,0 ); 68 69// Shared expert branch 70layer .ffn_gate_shexp = create_tensor (tn (LLM_TENSOR_FFN_GATE_SHEXP ,"weight" ,i ), {n_embd ,n_ff_exp * n_expert_shared },0 ); 71layer .ffn_down_shexp = create_tensor (tn (LLM_TENSOR_FFN_DOWN_SHEXP ,"weight" ,i ), {n_ff_exp * n_expert_shared ,n_embd },0 ); 72layer .ffn_up_shexp = create_tensor (tn (LLM_TENSOR_FFN_UP_SHEXP ,"weight" ,i ), {n_embd ,n_ff_exp * n_expert_shared },0 ); 73 } 74 } 75} 76 77std::unique_ptr < llm_graph_context > llama_model_deepseek2ocr::build_arch_graph (const llm_graph_params & params )const { 78return std::make_unique < graph > (* this ,params ); 79} 80