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_mpt::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 , false); 6ml .get_key (LLM_KV_ATTENTION_MAX_ALIBI_BIAS ,hparams .f_max_alibi_bias , false); 7 8switch (hparams .n_layer ()) { 9case 32 :type = LLM_TYPE_7B ;break ; 10case 48 :type = LLM_TYPE_30B ;break ; 11default :type = LLM_TYPE_UNKNOWN ; 12 } 13} 14 15void llama_model_mpt::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 ); 19pos_embd = create_tensor (tn (LLM_TENSOR_POS_EMBD ,"weight" ), {n_embd ,n_ctx_train },TENSOR_NOT_REQUIRED ); 20 21// output 22output_norm = create_tensor (tn (LLM_TENSOR_OUTPUT_NORM ,"weight" ), {n_embd },0 ); 23output_norm_b = create_tensor (tn (LLM_TENSOR_OUTPUT_NORM ,"bias" ), {n_embd },TENSOR_NOT_REQUIRED ); 24 25output = create_tensor (tn (LLM_TENSOR_OUTPUT ,"weight" ), {n_embd ,n_vocab },TENSOR_NOT_REQUIRED ); 26if (!output ) { 27output = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },TENSOR_DUPLICATED );// needs to be on GPU 28 } 29 30for (int i = 0 ;i < n_layer ;++ i ) { 31auto & layer = layers [i ]; 32 33layer .attn_norm = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"weight" ,i ), {n_embd },0 ); 34layer .attn_norm_b = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"bias" ,i ), {n_embd },TENSOR_NOT_REQUIRED ); 35 36layer .wqkv = create_tensor (tn (LLM_TENSOR_ATTN_QKV ,"weight" ,i ), {n_embd ,n_embd + 2 * n_embd_gqa },0 ); 37layer .wqkv_b = create_tensor (tn (LLM_TENSOR_ATTN_QKV ,"bias" ,i ), {n_embd + 2 * n_embd_gqa },TENSOR_NOT_REQUIRED ); 38 39layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,i ), {n_embd ,n_embd },0 ); 40layer .wo_b = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"bias" ,i ), {n_embd },TENSOR_NOT_REQUIRED ); 41 42layer .ffn_norm = create_tensor (tn (LLM_TENSOR_FFN_NORM ,"weight" ,i ), {n_embd },0 ); 43layer .ffn_norm_b = create_tensor (tn (LLM_TENSOR_FFN_NORM ,"bias" ,i ), {n_embd },TENSOR_NOT_REQUIRED ); 44 45layer .ffn_down = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"weight" ,i ), {n_ff ,n_embd },0 ); 46layer .ffn_down_b = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"bias" ,i ), {n_embd },TENSOR_NOT_REQUIRED ); 47 48layer .ffn_up = create_tensor (tn (LLM_TENSOR_FFN_UP ,"weight" ,i ), {n_embd ,n_ff },0 ); 49layer .ffn_up_b = create_tensor (tn (LLM_TENSOR_FFN_UP ,"bias" ,i ), {n_ff },TENSOR_NOT_REQUIRED ); 50 51// FIXME test-llama-archs crashes if q_norm is created 52layer .attn_q_norm = create_tensor (tn (LLM_TENSOR_ATTN_Q_NORM ,"weight" ,i ), {n_embd },TENSOR_NOT_REQUIRED |TENSOR_SKIP_IF_VIRTUAL ); 53layer .attn_q_norm_b = create_tensor (tn (LLM_TENSOR_ATTN_Q_NORM ,"bias" ,i ), {n_embd },TENSOR_NOT_REQUIRED |TENSOR_SKIP_IF_VIRTUAL ); 54 55layer .attn_k_norm = create_tensor (tn (LLM_TENSOR_ATTN_K_NORM ,"weight" ,i ), {n_embd },TENSOR_NOT_REQUIRED ); 56layer .attn_k_norm_b = create_tensor (tn (LLM_TENSOR_ATTN_K_NORM ,"bias" ,i ), {n_embd },TENSOR_NOT_REQUIRED ); 57 58// AWQ ScaleActivation layer 59layer .ffn_act = create_tensor (tn (LLM_TENSOR_FFN_ACT ,"scales" ,i ), {n_ff },TENSOR_NOT_REQUIRED ); 60 } 61} 62 63std::unique_ptr < llm_graph_context > llama_model_mpt::build_arch_graph (const llm_graph_params & params )const { 64return std::make_unique < graph > (* this ,params ); 65} 66 67llama_model_mpt::graph::graph (const llama_model & model ,const llm_graph_params & params ) :llm_graph_context (params ) { 68const int64_t n_embd_head = hparams .n_embd_head_v (); 69 70GGML_ASSERT (n_embd_head == hparams .n_embd_head_k ()); 71 72ggml_tensor * cur ; 73ggml_tensor * pos ; 74ggml_tensor * inpL ; 75 76inpL = build_inp_embd (model .tok_embd ); 77 78auto * inp_attn = build_attn_inp_kv (); 79 80if (model .pos_embd ) { 81// inp_pos - contains the positions 82ggml_tensor * inp_pos = build_inp_pos (); 83pos = ggml_get_rows (ctx0 ,model .pos_embd ,inp_pos ); 84cb (pos ,"pos_embd" ,-1 ); 85 86inpL = ggml_add (ctx0 ,inpL ,pos ); 87cb (inpL ,"inpL" ,-1 ); 88 } 89 90ggml_tensor * inp_out_ids = build_inp_out_ids (); 91 92for (int il = 0 ;il < n_layer ;++ il ) { 93ggml_tensor * attn_norm ; 94 95attn_norm = build_norm (inpL ,model .layers [il ].attn_norm ,model .layers [il ].attn_norm_b ,LLM_NORM ,il ); 96cb (attn_norm ,"attn_norm" ,il ); 97 98// self-attention 99 { 100cur = attn_norm ; 101 102auto [Qcur ,Kcur ,Vcur ]= build_qkv (model .layers [il ],cur , 103n_embd_head ,n_head ,n_head_kv ,il ); 104 105// Q/K Layernorm 106if (model .layers [il ].attn_q_norm ) { 107Qcur = ggml_reshape_2d (ctx0 ,Qcur ,n_embd_head * n_head ,n_tokens ); 108Kcur = ggml_reshape_2d (ctx0 ,Kcur ,n_embd_head * n_head_kv ,n_tokens ); 109 110Qcur = build_norm (Qcur ,model .layers [il ].attn_q_norm ,model .layers [il ].attn_q_norm_b ,LLM_NORM ,il ); 111 112Kcur = build_norm (Kcur ,model .layers [il ].attn_k_norm ,model .layers [il ].attn_k_norm_b ,LLM_NORM ,il ); 113 114Qcur = ggml_reshape_3d (ctx0 ,Qcur ,n_embd_head ,n_head ,n_tokens ); 115Kcur = ggml_reshape_3d (ctx0 ,Kcur ,n_embd_head ,n_head_kv ,n_tokens ); 116 } 117 118cb (Qcur ,"Qcur" ,il ); 119cb (Kcur ,"Kcur" ,il ); 120cb (Vcur ,"Vcur" ,il ); 121 122cur = build_attn (inp_attn , 123model .layers [il ].wo ,model .layers [il ].wo_b ,model .layers [il ].wo_s , 124Qcur ,Kcur ,Vcur ,nullptr ,nullptr ,nullptr ,1.0f /sqrtf (float (n_embd_head )),il ); 125 } 126 127if (il == n_layer - 1 && inp_out_ids ) { 128cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 129inpL = ggml_get_rows (ctx0 ,inpL ,inp_out_ids ); 130 } 131 132// Add the input 133ggml_tensor * ffn_inp = ggml_add (ctx0 ,cur ,inpL ); 134cb (ffn_inp ,"ffn_inp" ,il ); 135 136// feed forward 137 { 138cur = build_norm (ffn_inp ,model .layers [il ].ffn_norm ,model .layers [il ].ffn_norm_b ,LLM_NORM ,il ); 139cb (cur ,"ffn_norm" ,il ); 140cur = build_ffn (cur , 141model .layers [il ].ffn_up ,model .layers [il ].ffn_up_b ,NULL , 142NULL ,NULL ,NULL , 143model .layers [il ].ffn_down ,model .layers [il ].ffn_down_b ,NULL , 144model .layers [il ].ffn_act ,LLM_FFN_GELU ,LLM_FFN_SEQ ,il ); 145cb (cur ,"ffn_out" ,il ); 146 } 147 148cur = ggml_add (ctx0 ,cur ,ffn_inp ); 149 150cur = build_cvec (cur ,il ); 151cb (cur ,"l_out" ,il ); 152 153// input for next layer 154inpL = cur ; 155 } 156 157cur = inpL ; 158 159cur = build_norm (cur ,model .output_norm ,model .output_norm_b ,LLM_NORM ,-1 ); 160 161cb (cur ,"result_norm" ,-1 ); 162res -> t_embd = cur ; 163 164cur = build_lora_mm (model .output ,cur ,model .output_s ); 165 166cb (cur ,"result_output" ,-1 ); 167res -> t_logits = cur ; 168 169ggml_build_forward_expand (gf ,cur ); 170}