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_phi2::load_arch_hparams (llama_model_loader & ml ) { 4ml .get_key (LLM_KV_ATTENTION_LAYERNORM_EPS ,hparams .f_norm_eps ); 5 6switch (hparams .n_layer ()) { 7case 24 :type = LLM_TYPE_1B ;break ; 8case 32 :type = LLM_TYPE_3B ;break ; 9default :type = LLM_TYPE_UNKNOWN ; 10 } 11} 12 13void llama_model_phi2::load_arch_tensors (llama_model_loader & ) { 14LLAMA_LOAD_LOCALS ; 15 16tok_embd = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },0 ); 17 18// output 19output_norm = create_tensor (tn (LLM_TENSOR_OUTPUT_NORM ,"weight" ), {n_embd },0 ); 20output_norm_b = create_tensor (tn (LLM_TENSOR_OUTPUT_NORM ,"bias" ), {n_embd },0 ); 21output = create_tensor (tn (LLM_TENSOR_OUTPUT ,"weight" ), {n_embd ,n_vocab },0 ); 22output_b = create_tensor (tn (LLM_TENSOR_OUTPUT ,"bias" ), {n_vocab },0 ); 23 24for (int i = 0 ;i < n_layer ;++ i ) { 25auto & layer = layers [i ]; 26 27layer .attn_norm = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"weight" ,i ), {n_embd },0 ); 28layer .attn_norm_b = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"bias" ,i ), {n_embd },0 ); 29 30create_tensor_qkv (layer ,i ,n_embd ,n_embd ,n_embd_gqa ,n_embd_gqa ,0 ); 31 32layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,i ), {n_embd ,n_embd },0 ); 33layer .wo_b = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"bias" ,i ), {n_embd },0 ); 34 35layer .ffn_down = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"weight" ,i ), {n_ff ,n_embd },0 ); 36layer .ffn_down_b = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"bias" ,i ), {n_embd },0 ); 37 38layer .ffn_up = create_tensor (tn (LLM_TENSOR_FFN_UP ,"weight" ,i ), {n_embd ,n_ff },0 ); 39layer .ffn_up_b = create_tensor (tn (LLM_TENSOR_FFN_UP ,"bias" ,i ), {n_ff },0 ); 40 } 41} 42 43std::unique_ptr < llm_graph_context > llama_model_phi2::build_arch_graph (const llm_graph_params & params )const { 44return std::make_unique < graph > (* this ,params ); 45} 46 47llama_model_phi2::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 ()); 51 52ggml_tensor * cur ; 53ggml_tensor * attn_norm_output ; 54ggml_tensor * ffn_output ; 55ggml_tensor * inpL ; 56 57inpL = build_inp_embd (model .tok_embd ); 58 59// inp_pos - contains the positions 60ggml_tensor * inp_pos = build_inp_pos (); 61 62auto * inp_attn = build_attn_inp_kv (); 63 64ggml_tensor * inp_out_ids = build_inp_out_ids (); 65 66for (int il = 0 ;il < n_layer ;++ il ) { 67attn_norm_output = build_norm (inpL , 68model .layers [il ].attn_norm , 69model .layers [il ].attn_norm_b , 70LLM_NORM ,il ); 71cb (attn_norm_output ,"attn_norm" ,il ); 72 73// self-attention 74 { 75auto [Qcur ,Kcur ,Vcur ]= build_qkv (model .layers [il ],attn_norm_output , 76n_embd_head ,n_head ,n_head_kv ,il ); 77Qcur = ggml_rope_ext ( 78ctx0 ,Qcur ,inp_pos ,nullptr , 79n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 80ext_factor ,attn_factor ,beta_fast ,beta_slow 81 ); 82 83Kcur = ggml_rope_ext ( 84ctx0 ,Kcur ,inp_pos ,nullptr , 85n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 86ext_factor ,attn_factor ,beta_fast ,beta_slow 87 ); 88 89cb (Qcur ,"Qcur" ,il ); 90cb (Kcur ,"Kcur" ,il ); 91cb (Vcur ,"Vcur" ,il ); 92 93// with phi2, we scale the Q to avoid precision issues 94// ref: https://github.com/ml-explore/mlx-examples/blob/08e862336ade809bc37d1035f94b359e7d1a5152/phi2/phi2.py#L64-L66 95Qcur = ggml_scale (ctx0 ,Qcur ,1.0f /sqrtf (float (n_embd_head ))); 96 97cur = build_attn (inp_attn , 98model .layers [il ].wo ,model .layers [il ].wo_b ,model .layers [il ].wo_s , 99Qcur ,Kcur ,Vcur ,nullptr ,nullptr ,nullptr ,1.0f ,il ); 100 } 101if (il == n_layer - 1 && inp_out_ids ) { 102cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 103inpL = ggml_get_rows (ctx0 ,inpL ,inp_out_ids ); 104attn_norm_output = ggml_get_rows (ctx0 ,attn_norm_output ,inp_out_ids ); 105 } 106// FF 107 { 108ffn_output = build_ffn (attn_norm_output , 109model .layers [il ].ffn_up ,model .layers [il ].ffn_up_b ,NULL , 110NULL ,NULL ,NULL , 111model .layers [il ].ffn_down ,model .layers [il ].ffn_down_b ,NULL , 112NULL , 113LLM_FFN_GELU ,LLM_FFN_SEQ ,il ); 114cb (ffn_output ,"ffn_out" ,il ); 115 } 116cur = ggml_add (ctx0 ,cur ,ffn_output ); 117cur = ggml_add (ctx0 ,cur ,inpL ); 118 119cur = build_cvec (cur ,il ); 120cb (cur ,"l_out" ,il ); 121 122// input for next layer 123inpL = cur ; 124 } 125cur = build_norm (inpL , 126model .output_norm , 127model .output_norm_b , 128LLM_NORM ,-1 ); 129 130cb (cur ,"result_norm" ,-1 ); 131res -> t_embd = cur ; 132 133cur = build_lora_mm (model .output ,cur ,model .output_s ); 134cb (cur ,"result_output_no_bias" ,-1 ); 135 136cur = ggml_add (ctx0 ,cur ,model .output_b ); 137 138cb (cur ,"result_output" ,-1 ); 139res -> t_logits = cur ; 140 141ggml_build_forward_expand (gf ,cur ); 142}