char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
d344123fe
char/aliceai-t5-moe
1#include "models.h" 2 3void llama_model_bert::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 3 : 8type = LLM_TYPE_17M ;break ;// bge-micro 9case 6 : 10type = LLM_TYPE_22M ;break ;// MiniLM-L6 11case 12 : 12switch (hparams .n_embd ) { 13case 384 :type = LLM_TYPE_33M ;break ;// MiniLM-L12, bge-small 14case 768 :type = LLM_TYPE_109M ;break ;// bge-base 15default :type = LLM_TYPE_UNKNOWN ; 16 }break ; 17case 24 : 18type = LLM_TYPE_335M ;break ;// bge-large 19default :type = LLM_TYPE_UNKNOWN ; 20 } 21} 22 23void llama_model_bert::load_arch_tensors (llama_model_loader & ) { 24LLAMA_LOAD_LOCALS ; 25 26if (n_token_types == 0 ) { 27throw std::runtime_error (arch_name ()+ " model needs to define token type count" ); 28 } 29tok_embd = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },0 ); 30type_embd = create_tensor (tn (LLM_TENSOR_TOKEN_TYPES ,"weight" ), {n_embd ,n_token_types },TENSOR_NOT_REQUIRED ); 31 32pos_embd = create_tensor (tn (LLM_TENSOR_POS_EMBD ,"weight" ), {n_embd ,n_ctx_train },0 ); 33 34cls = create_tensor (tn (LLM_TENSOR_CLS ,"weight" ), {n_embd ,n_embd },TENSOR_NOT_REQUIRED ); 35cls_b = create_tensor (tn (LLM_TENSOR_CLS ,"bias" ), {n_embd },TENSOR_NOT_REQUIRED ); 36 37cls_out = create_tensor (tn (LLM_TENSOR_CLS_OUT ,"weight" ), {n_embd ,hparams .n_cls_out },TENSOR_NOT_REQUIRED ); 38cls_out_b = create_tensor (tn (LLM_TENSOR_CLS_OUT ,"bias" ), {hparams .n_cls_out },TENSOR_NOT_REQUIRED ); 39 40tok_norm = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD_NORM ,"weight" ,0 ), {n_embd },0 ); 41tok_norm_b = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD_NORM ,"bias" ,0 ), {n_embd },0 ); 42 43for (int i = 0 ;i < n_layer ;++ i ) { 44auto & layer = layers [i ]; 45 46create_tensor_qkv (layer ,i ,n_embd ,n_embd ,n_embd_gqa ,n_embd_gqa ,0 ); 47 48layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,i ), {n_embd ,n_embd },0 ); 49layer .wo_b = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"bias" ,i ), {n_embd },TENSOR_NOT_REQUIRED ); 50 51layer .attn_out_norm = create_tensor (tn (LLM_TENSOR_ATTN_OUT_NORM ,"weight" ,i ), {n_embd },0 ); 52layer .attn_out_norm_b = create_tensor (tn (LLM_TENSOR_ATTN_OUT_NORM ,"bias" ,i ), {n_embd },0 ); 53 54layer .ffn_up = create_tensor (tn (LLM_TENSOR_FFN_UP ,"weight" ,i ), {n_embd ,n_ff },0 ); 55layer .ffn_up_b = create_tensor (tn (LLM_TENSOR_FFN_UP ,"bias" ,i ), {n_ff },TENSOR_NOT_REQUIRED ); 56layer .ffn_down = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"weight" ,i ), {n_ff ,n_embd },0 ); 57layer .ffn_down_b = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"bias" ,i ), {n_embd },TENSOR_NOT_REQUIRED ); 58 59layer .layer_out_norm = create_tensor (tn (LLM_TENSOR_LAYER_OUT_NORM ,"weight" ,i ), {n_embd },0 ); 60layer .layer_out_norm_b = create_tensor (tn (LLM_TENSOR_LAYER_OUT_NORM ,"bias" ,i ), {n_embd },0 ); 61 } 62} 63 64std::unique_ptr < llm_graph_context > llama_model_bert::build_arch_graph (const llm_graph_params & params )const { 65return std::make_unique < graph > (* this ,params ); 66} 67 68llama_model_bert::graph::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 ; 75ggml_tensor * inp_pos = nullptr ; 76 77if (model .arch != LLM_ARCH_JINA_BERT_V2 ) { 78inp_pos = build_inp_pos (); 79 } 80 81// construct input embeddings (token, type, position) 82inpL = build_inp_embd (model .tok_embd ); 83 84// token types are hardcoded to zero ("Sentence A") 85if (model .type_embd ) { 86ggml_tensor * type_row0 = ggml_view_1d (ctx0 ,model .type_embd ,n_embd ,0 ); 87inpL = ggml_add (ctx0 ,inpL ,type_row0 ); 88 } 89if (model .arch == LLM_ARCH_BERT ) { 90inpL = ggml_add (ctx0 ,ggml_get_rows (ctx0 ,model .pos_embd ,inp_pos ),inpL ); 91 } 92cb (inpL ,"inp_embd" ,-1 ); 93 94// embed layer norm 95inpL = build_norm (inpL ,model .tok_norm ,model .tok_norm_b ,LLM_NORM ,0 ); 96cb (inpL ,"inp_norm" ,0 ); 97 98auto * inp_attn = build_attn_inp_no_cache (); 99 100ggml_tensor * inp_out_ids = build_inp_out_ids (); 101 102for (int il = 0 ;il < n_layer ;++ il ) { 103ggml_tensor * cur = inpL ; 104 105 { 106auto [Qcur ,Kcur ,Vcur ]= build_qkv (model .layers [il ],cur , 107n_embd_head ,n_head ,n_head_kv ,il ); 108 109if (model .layers [il ].attn_q_norm ) { 110Qcur = ggml_reshape_2d (ctx0 ,Qcur ,n_embd_head * n_head ,n_tokens ); 111 112Qcur = build_norm (Qcur ,model .layers [il ].attn_q_norm ,model .layers [il ].attn_q_norm_b ,LLM_NORM ,il ); 113 114Qcur = ggml_reshape_3d (ctx0 ,Qcur ,n_embd_head ,n_head ,n_tokens ); 115 } 116 117if (model .layers [il ].attn_k_norm ) { 118Kcur = ggml_reshape_2d (ctx0 ,Kcur ,n_embd_head * n_head_kv ,n_tokens ); 119 120Kcur = build_norm (Kcur ,model .layers [il ].attn_k_norm ,model .layers [il ].attn_k_norm_b ,LLM_NORM ,il ); 121 122Kcur = ggml_reshape_3d (ctx0 ,Kcur ,n_embd_head ,n_head_kv ,n_tokens ); 123 } 124 125// RoPE 126if (model .arch == LLM_ARCH_NOMIC_BERT || model .arch == LLM_ARCH_NOMIC_BERT_MOE || 127model .arch == LLM_ARCH_JINA_BERT_V3 ) { 128Qcur = ggml_rope_ext (ctx0 ,Qcur ,inp_pos ,nullptr ,n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 129ext_factor ,attn_factor ,beta_fast ,beta_slow ); 130 131Kcur = ggml_rope_ext (ctx0 ,Kcur ,inp_pos ,nullptr ,n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 132ext_factor ,attn_factor ,beta_fast ,beta_slow ); 133 } 134 135cb (Qcur ,"Qcur" ,il ); 136cb (Kcur ,"Kcur" ,il ); 137cb (Vcur ,"Vcur" ,il ); 138 139cur = build_attn (inp_attn , 140model .layers [il ].wo ,model .layers [il ].wo_b ,model .layers [il ].wo_s , 141Qcur ,Kcur ,Vcur ,nullptr ,nullptr ,nullptr ,1.0f /sqrtf (float (n_embd_head )),il ); 142cb (cur ,"kqv_out" ,il ); 143 } 144 145if (il == n_layer - 1 && inp_out_ids ) { 146cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 147inpL = ggml_get_rows (ctx0 ,inpL ,inp_out_ids ); 148 } 149 150// re-add the layer input 151cur = ggml_add (ctx0 ,cur ,inpL ); 152 153// attention layer norm 154cur = build_norm (cur ,model .layers [il ].attn_out_norm ,model .layers [il ].attn_out_norm_b ,LLM_NORM ,il ); 155 156if (model .layers [il ].attn_norm_2 != nullptr ) { 157cur = ggml_add (ctx0 ,cur ,inpL );// re-add the layer input 158cur = build_norm (cur ,model .layers [il ].attn_norm_2 ,model .layers [il ].attn_norm_2_b ,LLM_NORM ,il ); 159 } 160 161ggml_tensor * ffn_inp = cur ; 162cb (ffn_inp ,"ffn_inp" ,il ); 163 164// feed-forward network 165if (hparams .moe_every_n_layers > 0 && il %hparams .moe_every_n_layers == 1 ) { 166// MoE branch 167cur = build_moe_ffn (cur , 168model .layers [il ].ffn_gate_inp , 169model .layers [il ].ffn_up_exps , 170nullptr , 171model .layers [il ].ffn_down_exps , 172nullptr , 173hparams .n_expert ,hparams .n_expert_used (), 174LLM_FFN_GELU , false, 175hparams .expert_weights_scale , 176LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX , 177il ); 178cb (cur ,"ffn_moe_out" ,il ); 179 }else if (model .arch == LLM_ARCH_BERT || model .arch == LLM_ARCH_NOMIC_BERT_MOE || 180model .arch == LLM_ARCH_JINA_BERT_V3 ) { 181cur = build_ffn (cur , 182model .layers [il ].ffn_up ,model .layers [il ].ffn_up_b ,NULL , 183NULL ,NULL ,NULL , 184model .layers [il ].ffn_down ,model .layers [il ].ffn_down_b ,NULL ,NULL , 185LLM_FFN_GELU ,LLM_FFN_SEQ ,il ); 186cb (cur ,"ffn_out" ,il ); 187 }else if (model .arch == LLM_ARCH_JINA_BERT_V2 ) { 188const bool up_contains_gate = !model .layers [il ].ffn_gate && model .layers [il ].ffn_up -> ne [1 ]!= hparams .n_ff (); 189auto type_op = up_contains_gate ?LLM_FFN_GEGLU :LLM_FFN_GELU ; 190cur = build_ffn (cur , 191model .layers [il ].ffn_up ,model .layers [il ].ffn_up_b ,NULL , 192model .layers [il ].ffn_gate ,NULL ,NULL , 193model .layers [il ].ffn_down ,model .layers [il ].ffn_down_b ,NULL ,NULL , 194type_op ,LLM_FFN_PAR ,il ); 195cb (cur ,"ffn_out" ,il ); 196 }else { 197cur = build_ffn (cur , 198model .layers [il ].ffn_up ,NULL ,NULL , 199model .layers [il ].ffn_gate ,NULL ,NULL , 200model .layers [il ].ffn_down ,NULL ,NULL , 201NULL ,LLM_FFN_SILU ,LLM_FFN_PAR ,il ); 202cb (cur ,"ffn_out" ,il ); 203 } 204 205// attentions bypass the intermediate layer 206cur = ggml_add (ctx0 ,cur ,ffn_inp ); 207 208// output layer norm 209cur = build_norm (cur ,model .layers [il ].layer_out_norm ,model .layers [il ].layer_out_norm_b ,LLM_NORM ,il ); 210 211// input for next layer 212inpL = cur ; 213 } 214 215cur = inpL ; 216 217cb (cur ,"result_embd" ,-1 ); 218res -> t_embd = cur ; 219 220ggml_build_forward_expand (gf ,cur ); 221}