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_modern_bert::load_arch_hparams (llama_model_loader & ml ) { 4const bool found_swa = ml .get_key (LLM_KV_ATTENTION_SLIDING_WINDOW ,hparams .n_swa , false); 5if (found_swa && hparams .n_swa > 0 ) { 6hparams .swa_type = LLAMA_SWA_TYPE_SYMMETRIC ; 7ml .get_key (LLM_KV_ROPE_FREQ_BASE_SWA ,hparams .rope_freq_base_train_swa , false); 8uint32_t swa_period = 3 ; 9ml .get_key_or_arr (LLM_KV_ATTENTION_SLIDING_WINDOW_PATTERN ,swa_period , false); 10hparams .set_swa_pattern (swa_period , true); 11 }else { 12hparams .swa_type = LLAMA_SWA_TYPE_NONE ; 13 } 14 15ml .get_key (LLM_KV_ATTENTION_LAYERNORM_EPS ,hparams .f_norm_eps ); 16 17// Some ModernBert derivatives (e.g. IBM Granite Embedding 97m R2) use 18// SiLU/SwiGLU in the FFN instead of the default GELU/GeGLU. 19hparams .llm_ffn_op = LLM_FFN_GEGLU ; 20 std::string hidden_act ; 21if (ml .get_key (LLM_KV_HIDDEN_ACT ,hidden_act , false)) { 22hparams .llm_ffn_op = llm_ffn_op_type_from_string (hidden_act ,LLM_FFN_GEGLU ); 23 } 24 25switch (hparams .n_layer ()) { 26case 12 : 27type = LLM_TYPE_47M ;break ;// granite-embedding-small 28case 22 : 29type = LLM_TYPE_149M ;break ;// modern-bert-base 30case 28 : 31type = LLM_TYPE_395M ;break ;// modern-bert-large 32default :type = LLM_TYPE_UNKNOWN ; 33 } 34} 35 36void llama_model_modern_bert::load_arch_tensors (llama_model_loader & ) { 37LLAMA_LOAD_LOCALS ; 38 39tok_embd = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },0 ); 40tok_norm = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD_NORM ,"weight" ,0 ), {n_embd },0 ); 41 42output_norm = create_tensor (tn (LLM_TENSOR_OUTPUT_NORM ,"weight" ), {n_embd },0 ); 43 44for (int i = 0 ;i < n_layer ;++ i ) { 45auto & layer = layers [i ]; 46 47if (i != 0 ) { 48layer .attn_norm = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"weight" ,i ), {n_embd },0 ); 49 }else { 50// layer 0 uses identity 51layer .attn_norm = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"weight" ,i ), {n_embd },TENSOR_NOT_REQUIRED ); 52 } 53 54 55layer .wqkv = create_tensor (tn (LLM_TENSOR_ATTN_QKV ,"weight" ,i ), {n_embd ,3 * n_embd },0 ); 56layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,i ), {n_embd ,n_embd },0 ); 57 58layer .ffn_up = create_tensor (tn (LLM_TENSOR_FFN_UP ,"weight" ,i ), {n_embd ,2 * n_ff },0 ); 59layer .ffn_down = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"weight" ,i ), {n_ff ,n_embd },0 ); 60layer .ffn_norm = create_tensor (tn (LLM_TENSOR_FFN_NORM ,"weight" ,i ), {n_embd },0 ); 61 } 62 63cls_out = create_tensor (tn (LLM_TENSOR_CLS_OUT ,"weight" ), {n_embd ,hparams .n_cls_out },TENSOR_NOT_REQUIRED ); 64cls_out_b = create_tensor (tn (LLM_TENSOR_CLS_OUT ,"bias" ), {hparams .n_cls_out },TENSOR_NOT_REQUIRED ); 65cls = create_tensor (tn (LLM_TENSOR_CLS ,"weight" ), {n_embd ,n_embd },TENSOR_NOT_REQUIRED ); 66cls_norm = create_tensor (tn (LLM_TENSOR_CLS_NORM ,"weight" ), {n_embd },TENSOR_NOT_REQUIRED ); 67 68} 69 70std::unique_ptr < llm_graph_context > llama_model_modern_bert::build_arch_graph (const llm_graph_params & params )const { 71return std::make_unique < graph > (* this ,params ); 72} 73 74llama_model_modern_bert::graph::graph (const llama_model & model ,const llm_graph_params & params ) :llm_graph_context (params ) { 75const int64_t n_embd_head = hparams .n_embd_head_v (); 76 77GGML_ASSERT (n_embd_head == hparams .n_embd_head_k ()); 78 79ggml_tensor * cur ; 80ggml_tensor * inpL ; 81ggml_tensor * inp_pos = build_inp_pos (); 82 83// construct input embeddings (token, type, position) 84inpL = build_inp_embd (model .tok_embd ); 85cb (inpL ,"inp_embd" ,-1 ); 86 87// embed layer norm 88inpL = build_norm (inpL ,model .tok_norm ,nullptr ,LLM_NORM ,0 ); 89cb (inpL ,"inp_norm" ,0 ); 90 91ggml_tensor * inp_out_ids = build_inp_out_ids (); 92 93auto * inp_attn = build_attn_inp_no_cache (); 94 95for (int il = 0 ;il < n_layer ;++ il ) { 96const float freq_base_l = model .get_rope_freq_base (cparams ,il ); 97const float freq_scale_l = model .get_rope_freq_scale (cparams ,il ); 98 99cur = inpL ; 100 101// attention layer norm 102if (model .layers [il ].attn_norm ) { 103cur = build_norm (inpL , 104model .layers [il ].attn_norm ,NULL , 105LLM_NORM ,il ); 106cb (cur ,"attn_norm" ,il ); 107 } 108 109// self attention 110auto [Qcur ,Kcur ,Vcur ]= build_qkv (model .layers [il ],cur , 111n_embd_head ,n_head ,n_head_kv ,il ); 112 113// RoPE 114Qcur = ggml_rope_ext ( 115ctx0 ,Qcur ,inp_pos ,nullptr , 116n_rot ,rope_type ,n_ctx_orig ,freq_base_l ,freq_scale_l , 117ext_factor ,attn_factor ,beta_fast ,beta_slow 118 ); 119 120Kcur = ggml_rope_ext ( 121ctx0 ,Kcur ,inp_pos ,nullptr , 122n_rot ,rope_type ,n_ctx_orig ,freq_base_l ,freq_scale_l , 123ext_factor ,attn_factor ,beta_fast ,beta_slow 124 ); 125 126cb (Qcur ,"Qcur" ,il ); 127cb (Kcur ,"Kcur" ,il ); 128cb (Vcur ,"Vcur" ,il ); 129 130cur = build_attn (inp_attn , 131model .layers [il ].wo ,nullptr ,model .layers [il ].wo_s , 132Qcur ,Kcur ,Vcur ,nullptr ,nullptr ,nullptr ,1.0f /sqrtf (float (n_embd_head )),il ); 133cb (cur ,"kqv_out" ,il ); 134 135if (il == n_layer - 1 && inp_out_ids ) { 136cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 137inpL = ggml_get_rows (ctx0 ,inpL ,inp_out_ids ); 138 } 139 140// re-add the layer input 141ggml_tensor * ffn_inp = ggml_add (ctx0 ,cur ,inpL ); 142cb (ffn_inp ,"ffn_inp" ,il ); 143 144// attention layer norm 145cur = build_norm (ffn_inp , 146model .layers [il ].ffn_norm ,NULL , 147LLM_NORM ,il ); 148cb (cur ,"ffn_norm" ,il ); 149 150cur = build_ffn (cur , 151model .layers [il ].ffn_up ,NULL ,NULL , 152NULL ,NULL ,NULL , 153model .layers [il ].ffn_down ,NULL ,NULL , 154NULL , 155hparams .llm_ffn_op , 156LLM_FFN_SEQ ,il ); 157 158// attentions bypass the intermediate layer 159cur = ggml_add (ctx0 ,cur ,ffn_inp ); 160 161// input for next layer 162inpL = cur ; 163 } 164 165cur = inpL ; 166 167cur = build_norm (cur , 168model .output_norm ,NULL , 169LLM_NORM ,-1 ); 170cb (cur ,"final_norm_out" ,-1 ); 171 172res -> t_embd = cur ; 173ggml_build_forward_expand (gf ,cur ); 174}