char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
c61b98b87
char/aliceai-t5-moe
1#include "models.h" 2#include "../llama-memory-hybrid-iswa.h" 3#include "../llama-memory-hybrid.h" 4 5void llama_model_lfm2moe::load_arch_hparams (llama_model_loader & ml ) { 6ml .get_key (LLM_KV_SHORTCONV_L_CACHE ,hparams .n_shortconv_l_cache ); 7ml .get_key (LLM_KV_ATTENTION_LAYERNORM_RMS_EPS ,hparams .f_norm_rms_eps ); 8ml .get_key (LLM_KV_LEADING_DENSE_BLOCK_COUNT ,hparams .n_layer_dense_lead , false); 9ml .get_key_or_arr (LLM_KV_EXPERT_FEED_FORWARD_LENGTH ,hparams .n_ff_exp_arr ,hparams .n_layer_all ); 10ml .get_key (LLM_KV_EXPERT_GATING_FUNC ,hparams .expert_gating_func ); 11 12for (uint32_t il = 0 ;il < hparams .n_layer ();++ il ) { 13hparams .is_recr_impl [il ]= hparams .n_head_kv (il )== 0 ; 14 } 15 16switch (hparams .n_layer ()) { 17case 24 :type = LLM_TYPE_8B_A1B ;break ; 18case 40 :type = LLM_TYPE_24B_A2B ;break ; 19default :type = LLM_TYPE_UNKNOWN ; 20 } 21} 22 23void llama_model_lfm2moe::load_arch_tensors (llama_model_loader & ) { 24LLAMA_LOAD_LOCALS ; 25 26tok_embd = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },0 ); 27 28output_norm = create_tensor (tn (LLM_TENSOR_OUTPUT_NORM_LFM2 ,"weight" ), {n_embd },0 ); 29output = create_tensor (tn (LLM_TENSOR_OUTPUT ,"weight" ), {n_embd ,n_vocab },TENSOR_NOT_REQUIRED ); 30 31if (output == NULL ) { 32output = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },TENSOR_DUPLICATED ); 33 } 34 35for (int i = 0 ;i < n_layer ;++ i ) { 36auto & layer = layers [i ]; 37 38const bool is_moe_layer = i >=static_cast < int > (hparams .n_layer_dense_lead ); 39 40// ffn/moe is same for transformer and conv layers 41layer .ffn_norm = create_tensor (tn (LLM_TENSOR_FFN_NORM ,"weight" ,i ), {n_embd },0 ); 42if (is_moe_layer ) { 43GGML_ASSERT (n_expert && n_expert_used ); 44layer .ffn_gate_inp = create_tensor (tn (LLM_TENSOR_FFN_GATE_INP ,"weight" ,i ), {n_embd ,n_expert },0 ); 45layer .ffn_gate_exps = create_tensor (tn (LLM_TENSOR_FFN_GATE_EXPS ,"weight" ,i ), {n_embd ,hparams .n_ff_exp (),n_expert },0 ); 46layer .ffn_down_exps = create_tensor (tn (LLM_TENSOR_FFN_DOWN_EXPS ,"weight" ,i ), {hparams .n_ff_exp (),n_embd ,n_expert },0 ); 47layer .ffn_up_exps = create_tensor (tn (LLM_TENSOR_FFN_UP_EXPS ,"weight" ,i ), {n_embd ,hparams .n_ff_exp (),n_expert },0 ); 48layer .ffn_exp_probs_b = create_tensor (tn (LLM_TENSOR_FFN_EXP_PROBS_B ,"bias" ,i ), {n_expert },0 ); 49 }else {// dense 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 } 54 55// for operator_norm 56layer .attn_norm = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"weight" ,i ), {n_embd },0 ); 57 58if (!hparams .is_recr (i )) { 59layer .attn_q_norm = create_tensor (tn (LLM_TENSOR_ATTN_Q_NORM ,"weight" ,i ), {n_embd_head_k },0 ); 60layer .attn_k_norm = create_tensor (tn (LLM_TENSOR_ATTN_K_NORM ,"weight" ,i ), {n_embd_head_k },0 ); 61GGML_ASSERT (n_embd_v_gqa == n_embd_k_gqa ); 62 63create_tensor_qkv (layer ,i ,n_embd ,n_embd ,hparams .n_embd_k_gqa (i ),hparams .n_embd_v_gqa (i ),0 ); 64 65layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,i ), {n_embd ,n_embd },0 ); 66 }else { 67layer .shortconv .conv = create_tensor (tn (LLM_TENSOR_SHORTCONV_CONV ,"weight" ,i ), {hparams .n_shortconv_l_cache ,n_embd },0 ); 68layer .shortconv .in_proj = create_tensor (tn (LLM_TENSOR_SHORTCONV_INPROJ ,"weight" ,i ), {n_embd ,3 * n_embd },0 ); 69layer .shortconv .out_proj = create_tensor (tn (LLM_TENSOR_SHORTCONV_OUTPROJ ,"weight" ,i ), {n_embd ,n_embd },0 ); 70 } 71 } 72 73// for LFM2-ColBert-350M 74dense_2_out_layers = create_tensor (tn (LLM_TENSOR_DENSE_2_OUT ,"weight" ), {n_embd ,hparams .n_embd_out ()},TENSOR_NOT_REQUIRED ); 75dense_2_out_layers_b = create_tensor (tn (LLM_TENSOR_DENSE_2_OUT ,"bias" ), {hparams .n_embd_out () },TENSOR_NOT_REQUIRED ); 76} 77 78std::unique_ptr < llm_graph_context > llama_model_lfm2moe::build_arch_graph (const llm_graph_params & params )const { 79if (hparams .swa_type == LLAMA_SWA_TYPE_STANDARD ) { 80return std::make_unique < graph < true>> (* this ,params ); 81 }else { 82return std::make_unique < graph < false>> (* this ,params ); 83 } 84} 85