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_dbrx::load_arch_hparams (llama_model_loader & ml ) { 4ml .get_key (LLM_KV_ATTENTION_LAYERNORM_EPS ,hparams .f_norm_eps ); 5ml .get_key (LLM_KV_ATTENTION_CLAMP_KQV ,hparams .f_clamp_kqv ); 6 7switch (hparams .n_layer ()) { 8case 40 :type = LLM_TYPE_16x12B ;break ; 9default :type = LLM_TYPE_UNKNOWN ; 10 } 11} 12 13void llama_model_dbrx::load_arch_tensors (llama_model_loader & ) { 14LLAMA_LOAD_LOCALS ; 15 16if (n_expert == 0 ) { 17throw std::runtime_error ("DBRX model cannot have zero experts" ); 18 } 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 ); 24output = create_tensor (tn (LLM_TENSOR_OUTPUT ,"weight" ), {n_embd ,n_vocab },0 ); 25 26for (int i = 0 ;i < n_layer ;++ i ) { 27auto & layer = layers [i ]; 28 29layer .attn_norm = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"weight" ,i ), {n_embd },0 ); 30 31layer .wqkv = create_tensor (tn (LLM_TENSOR_ATTN_QKV ,"weight" ,i ), {n_embd ,n_embd + 2 * n_embd_gqa },0 ); 32layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,i ), {n_embd ,n_embd },0 ); 33 34layer .attn_out_norm = create_tensor (tn (LLM_TENSOR_ATTN_OUT_NORM ,"weight" ,i ), {n_embd },0 ); 35 36layer .ffn_gate_inp = create_tensor (tn (LLM_TENSOR_FFN_GATE_INP ,"weight" ,i ), {n_embd ,n_expert },0 ); 37layer .ffn_gate_exps = create_tensor (tn (LLM_TENSOR_FFN_GATE_EXPS ,"weight" ,i ), {n_embd ,n_ff ,n_expert },0 ); 38layer .ffn_down_exps = create_tensor (tn (LLM_TENSOR_FFN_DOWN_EXPS ,"weight" ,i ), {n_ff ,n_embd ,n_expert },0 ); 39layer .ffn_up_exps = create_tensor (tn (LLM_TENSOR_FFN_UP_EXPS ,"weight" ,i ), {n_embd ,n_ff ,n_expert },0 ); 40 } 41} 42 43std::unique_ptr < llm_graph_context > llama_model_dbrx::build_arch_graph (const llm_graph_params & params )const { 44return std::make_unique < graph > (* this ,params ); 45} 46 47llama_model_dbrx::graph::graph (const llama_model & model ,const llm_graph_params & params ) :llm_graph_context (params ) { 48const int64_t n_embd_head = hparams .n_embd_head_v (); 49 50GGML_ASSERT (n_embd_head == hparams .n_embd_head_k ()); 51GGML_ASSERT (n_embd_head == n_rot ); 52 53ggml_tensor * cur ; 54ggml_tensor * inpL ; 55 56inpL = build_inp_embd (model .tok_embd ); 57 58// inp_pos - contains the positions 59ggml_tensor * inp_pos = build_inp_pos (); 60 61auto * inp_attn = build_attn_inp_kv (); 62 63ggml_tensor * inp_out_ids = build_inp_out_ids (); 64 65for (int il = 0 ;il < n_layer ;++ il ) { 66ggml_tensor * inpSA = inpL ; 67 68// norm 69cur = build_norm (inpL , 70model .layers [il ].attn_norm ,NULL , 71LLM_NORM ,il ); 72cb (cur ,"attn_norm" ,il ); 73 74// self-attention 75 { 76auto [Qcur ,Kcur ,Vcur ]= build_qkv (model .layers [il ],cur , 77n_embd_head ,n_head ,n_head_kv ,il ); 78 79Qcur = ggml_rope_ext ( 80ctx0 ,Qcur ,inp_pos ,nullptr , 81n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 82ext_factor ,attn_factor ,beta_fast ,beta_slow 83 ); 84 85Kcur = ggml_rope_ext ( 86ctx0 ,Kcur ,inp_pos ,nullptr , 87n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 88ext_factor ,attn_factor ,beta_fast ,beta_slow 89 ); 90 91cb (Qcur ,"Qcur" ,il ); 92cb (Kcur ,"Kcur" ,il ); 93cb (Vcur ,"Vcur" ,il ); 94 95cur = build_attn (inp_attn , 96model .layers [il ].wo ,NULL ,model .layers [il ].wo_s , 97Qcur ,Kcur ,Vcur ,nullptr ,nullptr ,nullptr ,1.0f /sqrtf (float (n_embd_head )),il ); 98 } 99 100if (il == n_layer - 1 && inp_out_ids ) { 101cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 102inpSA = ggml_get_rows (ctx0 ,inpSA ,inp_out_ids ); 103 } 104 105ggml_tensor * ffn_inp = ggml_add (ctx0 ,cur ,inpSA ); 106cb (ffn_inp ,"ffn_inp" ,il ); 107 108// feed-forward network 109// MoE branch 110cur = build_norm (ffn_inp , 111model .layers [il ].attn_out_norm ,NULL , 112LLM_NORM ,il ); 113cb (cur ,"attn_out_norm" ,il ); 114 115cur = build_moe_ffn (cur , 116model .layers [il ].ffn_gate_inp , 117model .layers [il ].ffn_up_exps , 118model .layers [il ].ffn_gate_exps , 119model .layers [il ].ffn_down_exps , 120nullptr , 121n_expert ,n_expert_used , 122LLM_FFN_SILU , true, 123hparams .expert_weights_scale , 124LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX , 125il ); 126cb (cur ,"ffn_moe_out" ,il ); 127 128cur = ggml_add (ctx0 ,cur ,ffn_inp ); 129cb (cur ,"ffn_out" ,il ); 130 131cur = build_cvec (cur ,il ); 132cb (cur ,"l_out" ,il ); 133 134// input for next layer 135inpL = cur ; 136 } 137 138cur = inpL ; 139 140cur = build_norm (cur , 141model .output_norm ,NULL , 142LLM_NORM ,-1 ); 143 144cb (cur ,"result_norm" ,-1 ); 145res -> t_embd = cur ; 146 147// lm_head 148cur = build_lora_mm (model .output ,cur ,model .output_s ); 149 150cb (cur ,"result_output" ,-1 ); 151res -> t_logits = cur ; 152 153ggml_build_forward_expand (gf ,cur ); 154}