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_refact::load_arch_hparams (llama_model_loader & ml ) { 4ml .get_key (LLM_KV_ATTENTION_LAYERNORM_RMS_EPS ,hparams .f_norm_rms_eps ); 5 6switch (hparams .n_layer ()) { 7case 32 :type = LLM_TYPE_1B ;break ; 8default :type = LLM_TYPE_UNKNOWN ; 9 } 10 11// TODO: become GGUF KV parameter 12hparams .f_max_alibi_bias = 8.0f ; 13} 14 15void llama_model_refact::load_arch_tensors (llama_model_loader & ) { 16LLAMA_LOAD_LOCALS ; 17 18tok_embd = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },0 ); 19 20// output 21output_norm = create_tensor (tn (LLM_TENSOR_OUTPUT_NORM ,"weight" ), {n_embd },0 ); 22output = create_tensor (tn (LLM_TENSOR_OUTPUT ,"weight" ), {n_embd ,n_vocab },TENSOR_NOT_REQUIRED ); 23 24// if output is NULL, init from the input tok embed 25if (output == NULL ) { 26output = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },TENSOR_DUPLICATED ); 27 } 28 29for (int i = 0 ;i < n_layer ;++ i ) { 30auto & layer = layers [i ]; 31 32layer .attn_norm = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"weight" ,i ), {n_embd },0 ); 33 34create_tensor_qkv (layer ,i ,n_embd ,n_embd_head_k * n_head ,n_embd_k_gqa ,n_embd_v_gqa ,0 ); 35layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,i ), {n_embd_head_k * n_head ,n_embd },0 ); 36 37// optional bias tensors 38layer .wo_b = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"bias" ,i ), {n_embd },TENSOR_NOT_REQUIRED ); 39 40layer .ffn_norm = create_tensor (tn (LLM_TENSOR_FFN_NORM ,"weight" ,i ), {n_embd },0 ); 41 42if (hparams .rope_scaling_type_train == LLAMA_ROPE_SCALING_TYPE_LONGROPE ) { 43layer .rope_long = create_tensor (tn (LLM_TENSOR_ROPE_FACTORS_LONG ,"weight" ,i ), {n_rot /2 },TENSOR_NOT_REQUIRED | (i != 0 ?TENSOR_DUPLICATED :0 )); 44layer .rope_short = create_tensor (tn (LLM_TENSOR_ROPE_FACTORS_SHORT ,"weight" ,i ), {n_rot /2 },TENSOR_NOT_REQUIRED | (i != 0 ?TENSOR_DUPLICATED :0 )); 45 } 46else { 47layer .rope_freqs = create_tensor (tn (LLM_TENSOR_ROPE_FREQS ,"weight" ,i ), {n_rot /2 },TENSOR_NOT_REQUIRED | (i != 0 ?TENSOR_DUPLICATED :0 )); 48 } 49 50if (n_expert == 0 ) { 51layer .ffn_gate = create_tensor (tn (LLM_TENSOR_FFN_GATE ,"weight" ,i ), {n_embd ,n_ff },0 ); 52layer .ffn_down = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"weight" ,i ), {n_ff ,n_embd },0 ); 53layer .ffn_up = create_tensor (tn (LLM_TENSOR_FFN_UP ,"weight" ,i ), {n_embd ,n_ff },0 ); 54 55// optional MLP bias 56layer .ffn_gate_b = create_tensor (tn (LLM_TENSOR_FFN_GATE ,"bias" ,i ), {n_ff },TENSOR_NOT_REQUIRED ); 57layer .ffn_down_b = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"bias" ,i ), {n_embd },TENSOR_NOT_REQUIRED ); 58layer .ffn_up_b = create_tensor (tn (LLM_TENSOR_FFN_UP ,"bias" ,i ), {n_ff },TENSOR_NOT_REQUIRED ); 59 }else { 60layer .ffn_gate_inp = create_tensor (tn (LLM_TENSOR_FFN_GATE_INP ,"weight" ,i ), {n_embd ,n_expert },0 ); 61layer .ffn_gate_exps = create_tensor (tn (LLM_TENSOR_FFN_GATE_EXPS ,"weight" ,i ), {n_embd ,n_ff ,n_expert },TENSOR_NOT_REQUIRED ); 62layer .ffn_down_exps = create_tensor (tn (LLM_TENSOR_FFN_DOWN_EXPS ,"weight" ,i ), {n_ff ,n_embd ,n_expert },0 ); 63layer .ffn_up_exps = create_tensor (tn (LLM_TENSOR_FFN_UP_EXPS ,"weight" ,i ), {n_embd ,n_ff ,n_expert },0 ); 64 65// For Granite MoE Shared 66if (hparams .n_ff_shexp > 0 ) { 67layer .ffn_gate_shexp = create_tensor (tn (LLM_TENSOR_FFN_GATE_SHEXP ,"weight" ,i ), {n_embd ,hparams .n_ff_shexp },0 ); 68layer .ffn_up_shexp = create_tensor (tn (LLM_TENSOR_FFN_UP_SHEXP ,"weight" ,i ), {n_embd ,hparams .n_ff_shexp },0 ); 69layer .ffn_down_shexp = create_tensor (tn (LLM_TENSOR_FFN_DOWN_SHEXP ,"weight" ,i ), {hparams .n_ff_shexp ,n_embd },0 ); 70 } 71 } 72 } 73} 74 75std::unique_ptr < llm_graph_context > llama_model_refact::build_arch_graph (const llm_graph_params & params )const { 76return std::make_unique < graph > (* this ,params ); 77} 78 79llama_model_refact::graph::graph (const llama_model & model ,const llm_graph_params & params ) :llm_graph_context (params ) { 80const int64_t n_embd_head = hparams .n_embd_head_v (); 81 82GGML_ASSERT (n_embd_head == hparams .n_embd_head_k ()); 83 84ggml_tensor * cur ; 85ggml_tensor * inpL ; 86 87inpL = build_inp_embd (model .tok_embd ); 88 89auto * inp_attn = build_attn_inp_kv (); 90 91ggml_tensor * inp_out_ids = build_inp_out_ids (); 92 93for (int il = 0 ;il < n_layer ;++ il ) { 94ggml_tensor * inpSA = inpL ; 95 96cur = build_norm (inpL , 97model .layers [il ].attn_norm ,NULL , 98LLM_NORM_RMS ,il ); 99cb (cur ,"attn_norm" ,il ); 100 101// self-attention 102 { 103auto [Qcur ,Kcur ,Vcur ]= build_qkv (model .layers [il ],cur , 104n_embd_head ,n_head ,n_head_kv ,il ); 105 106cb (Qcur ,"Qcur" ,il ); 107cb (Kcur ,"Kcur" ,il ); 108cb (Vcur ,"Vcur" ,il ); 109 110cur = build_attn (inp_attn , 111model .layers [il ].wo ,NULL ,model .layers [il ].wo_s , 112Qcur ,Kcur ,Vcur ,nullptr ,nullptr ,nullptr ,1.0f /sqrtf (float (n_embd_head )),il ); 113 } 114if (il == n_layer - 1 && inp_out_ids ) { 115cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 116inpSA = ggml_get_rows (ctx0 ,inpSA ,inp_out_ids ); 117 } 118ggml_tensor * ffn_inp = ggml_add (ctx0 ,cur ,inpSA ); 119cb (ffn_inp ,"ffn_inp" ,il ); 120 121// feed-forward network 122 { 123cur = build_norm (ffn_inp , 124model .layers [il ].ffn_norm ,NULL , 125LLM_NORM_RMS ,il ); 126cb (cur ,"ffn_norm" ,il ); 127 128cur = build_ffn (cur , 129model .layers [il ].ffn_up ,NULL ,NULL , 130model .layers [il ].ffn_gate ,NULL ,NULL , 131model .layers [il ].ffn_down ,NULL ,NULL , 132NULL , 133LLM_FFN_SILU ,LLM_FFN_PAR ,il ); 134cb (cur ,"ffn_out" ,il ); 135 } 136cur = ggml_add (ctx0 ,cur ,ffn_inp ); 137 138cur = build_cvec (cur ,il ); 139cb (cur ,"l_out" ,il ); 140 141// input for next layer 142inpL = cur ; 143 } 144cur = inpL ; 145 146cur = build_norm (cur , 147model .output_norm ,NULL , 148LLM_NORM_RMS ,-1 ); 149 150cb (cur ,"result_norm" ,-1 ); 151res -> t_embd = cur ; 152 153// lm_head 154cur = build_lora_mm (model .output ,cur ,model .output_s ); 155 156cb (cur ,"result_output" ,-1 ); 157res -> t_logits = cur ; 158 159ggml_build_forward_expand (gf ,cur ); 160}