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_phi3::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 24 :type = LLM_TYPE_1B ;break ; 8case 32 :type = LLM_TYPE_3B ;break ; 9case 40 :type = LLM_TYPE_14B ;break ; 10default :type = LLM_TYPE_UNKNOWN ; 11 } 12 13const bool found_swa = ml .get_key (LLM_KV_ATTENTION_SLIDING_WINDOW ,hparams .n_swa , false); 14 15if (found_swa && hparams .n_swa > 0 ) { 16LLAMA_LOG_WARN ("%s: Phi SWA is currently disabled - results might be suboptimal for some models (see %s)\n" , 17__func__ ,"https://github.com/ggml-org/llama.cpp/pull/13676" ); 18 19// TODO: fix conversion scripts to correctly populate `n_swa` and `n_swa_pattern` 20hparams .swa_type = LLAMA_SWA_TYPE_NONE ; 21 22hparams .n_swa = 0 ; 23hparams .set_swa_pattern (1 ); 24 } 25} 26 27void llama_model_phi3::load_arch_tensors (llama_model_loader & ) { 28LLAMA_LOAD_LOCALS ; 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 ); 34output = create_tensor (tn (LLM_TENSOR_OUTPUT ,"weight" ), {n_embd ,n_vocab },TENSOR_NOT_REQUIRED ); 35 36// if output is NULL, init from the input tok embed 37if (output == NULL ) { 38output = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },TENSOR_DUPLICATED ); 39 } 40 41for (int i = 0 ;i < n_layer ;++ i ) { 42auto & layer = layers [i ]; 43 44layer .attn_norm = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"weight" ,i ), {n_embd },0 ); 45 46create_tensor_qkv (layer ,i ,n_embd ,n_embd ,n_embd_gqa ,n_embd_gqa ,TENSOR_NOT_REQUIRED ); 47layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,i ), {n_embd ,n_embd },0 ); 48 49layer .ffn_norm = create_tensor (tn (LLM_TENSOR_FFN_NORM ,"weight" ,i ), {n_embd },0 ); 50 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 ,2 * n_ff },0 ); 53 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 } 57} 58 59std::unique_ptr < llm_graph_context > llama_model_phi3::build_arch_graph (const llm_graph_params & params )const { 60if (hparams .swa_type != LLAMA_SWA_TYPE_NONE ) { 61return std::make_unique < graph < true>> (* this ,params ); 62 }else { 63return std::make_unique < graph < false>> (* this ,params ); 64 } 65} 66 67template < bool iswa > 68llama_model_phi3::graph < iswa > ::graph (const llama_model & model ,const llm_graph_params & params ) :llm_graph_context (params ) { 69const int64_t n_embd_head = hparams .n_embd_head_v (); 70 71GGML_ASSERT (n_embd_head == hparams .n_embd_head_k ()); 72 73ggml_tensor * cur ; 74ggml_tensor * inpL ; 75 76inpL = build_inp_embd (model .tok_embd ); 77 78// inp_pos - contains the positions 79ggml_tensor * inp_pos = build_inp_pos (); 80 81using inp_attn_type = std::conditional_t < iswa ,llm_graph_input_attn_kv_iswa ,llm_graph_input_attn_kv > ; 82inp_attn_type * inp_attn = nullptr ; 83 84if constexpr (iswa ) { 85inp_attn = build_attn_inp_kv_iswa (); 86 }else { 87inp_attn = build_attn_inp_kv (); 88 } 89ggml_tensor * inp_out_ids = build_inp_out_ids (); 90 91for (int il = 0 ;il < n_layer ;++ il ) { 92auto * residual = inpL ; 93 94// self-attention 95 { 96// rope freq factors for 128k context 97ggml_tensor * rope_factors = model .get_rope_factors (cparams ,il ); 98 99ggml_tensor * attn_norm_output = build_norm (inpL , 100model .layers [il ].attn_norm , 101model .layers [il ].attn_norm_b , 102LLM_NORM_RMS ,il ); 103cb (attn_norm_output ,"attn_norm" ,il ); 104 105auto [Qcur ,Kcur ,Vcur ]= build_qkv (model .layers [il ],attn_norm_output , 106n_embd_head ,n_head ,n_head_kv ,il ); 107Qcur = ggml_rope_ext ( 108ctx0 ,Qcur ,inp_pos ,rope_factors , 109n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 110ext_factor ,attn_factor ,beta_fast ,beta_slow 111 ); 112 113Kcur = ggml_rope_ext ( 114ctx0 ,Kcur ,inp_pos ,rope_factors , 115n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 116ext_factor ,attn_factor ,beta_fast ,beta_slow 117 ); 118 119cb (Qcur ,"Qcur" ,il ); 120cb (Kcur ,"Kcur" ,il ); 121cb (Vcur ,"Vcur" ,il ); 122 123Qcur = ggml_scale (ctx0 ,Qcur ,1.0f /sqrtf (float (n_embd_head ))); 124cb (Qcur ,"Qcur" ,il ); 125 126cur = build_attn (inp_attn , 127model .layers [il ].wo ,model .layers [il ].wo_b ,model .layers [il ].wo_s , 128Qcur ,Kcur ,Vcur ,nullptr ,nullptr ,nullptr ,1.0f ,il ); 129 } 130if (il == n_layer - 1 && inp_out_ids ) { 131cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 132residual = ggml_get_rows (ctx0 ,residual ,inp_out_ids ); 133 } 134cur = ggml_add (ctx0 ,cur ,residual ); 135residual = cur ; 136 137cur = build_norm (cur , 138model .layers [il ].ffn_norm ,model .layers [il ].ffn_norm_b , 139LLM_NORM_RMS ,il ); 140cb (cur ,"ffn_norm" ,il ); 141 142// feed-forward network 143if (model .layers [il ].ffn_gate_inp == nullptr ) { 144cur = build_ffn (cur , 145model .layers [il ].ffn_up ,NULL ,NULL , 146NULL ,NULL ,NULL , 147model .layers [il ].ffn_down ,NULL ,NULL , 148NULL , 149LLM_FFN_SWIGLU ,LLM_FFN_SEQ ,il ); 150cb (cur ,"ffn_out" ,il ); 151 }else { 152// MoE branch 153cur = build_moe_ffn (cur , 154model .layers [il ].ffn_gate_inp , 155model .layers [il ].ffn_up_exps , 156model .layers [il ].ffn_gate_exps , 157model .layers [il ].ffn_down_exps , 158nullptr , 159n_expert ,n_expert_used , 160LLM_FFN_SILU , true, 161hparams .expert_weights_scale , 162LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX , 163il ); 164cb (cur ,"ffn_moe_out" ,il ); 165 } 166cur = ggml_add (ctx0 ,residual ,cur ); 167 168cur = build_cvec (cur ,il ); 169cb (cur ,"l_out" ,il ); 170 171// input for next layer 172inpL = cur ; 173 } 174cur = build_norm (inpL , 175model .output_norm , 176model .output_norm_b , 177LLM_NORM_RMS ,-1 ); 178 179cb (cur ,"result_norm" ,-1 ); 180res -> t_embd = cur ; 181 182cur = build_lora_mm (model .output ,cur ,model .output_s ); 183 184if (model .output_b != nullptr ) { 185cb (cur ,"result_output_no_bias" ,-1 ); 186cur = ggml_add (ctx0 ,cur ,model .output_b ); 187 } 188cb (cur ,"result_output" ,-1 ); 189res -> t_logits = cur ; 190 191ggml_build_forward_expand (gf ,cur ); 192} 193 194// Explicit template instantiations 195template struct llama_model_phi3::graph < false> ; 196template struct llama_model_phi3:: graph < true > ;