char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
c61b98b87
char/aliceai-t5-moe
1#include "models.h" 2#include "../llama-memory-hybrid-iswa.h" 3#include "../llama-memory-hybrid.h" 4 5#include <algorithm> 6 7void llama_model_lfm2::load_arch_hparams (llama_model_loader & ml ) { 8ml .get_key (LLM_KV_SHORTCONV_L_CACHE ,hparams .n_shortconv_l_cache ); 9ml .get_key (LLM_KV_ATTENTION_LAYERNORM_RMS_EPS ,hparams .f_norm_rms_eps ); 10 11for (uint32_t il = 0 ;il < hparams .n_layer ();++ il ) { 12hparams .is_recr_impl [il ]= hparams .n_head_kv (il )== 0 ; 13 } 14 15hparams .n_layer_dense_lead = hparams .n_layer (); 16 17switch (hparams .n_ff ()) { 18case 2560 :type = LLM_TYPE_230M ;break ; 19case 4608 :type = LLM_TYPE_350M ;break ; 20case 6912 :type = LLM_TYPE_700M ;break ; 21case 8192 :type = LLM_TYPE_1_2B ;break ; 22case 10752 :type = LLM_TYPE_2_6B ;break ; 23default :type = LLM_TYPE_UNKNOWN ; 24 } 25 26if (const auto is_swa = ml .get_key (LLM_KV_ATTENTION_SLIDING_WINDOW ,hparams .n_swa , false);is_swa && hparams .n_swa > 0 ) { 27hparams .swa_type = LLAMA_SWA_TYPE_STANDARD ; 28for (uint32_t il = 0 ;il < hparams .n_layer ();++ il ) { 29hparams .is_swa_impl [il ]= !hparams .is_recr_impl [il ]; 30 } 31 } 32} 33 34void llama_model_lfm2::load_arch_tensors (llama_model_loader & ) { 35LLAMA_LOAD_LOCALS ; 36 37tok_embd = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },0 ); 38 39output_norm = create_tensor (tn (LLM_TENSOR_OUTPUT_NORM_LFM2 ,"weight" ), {n_embd },0 ); 40output = create_tensor (tn (LLM_TENSOR_OUTPUT ,"weight" ), {n_embd ,n_vocab },TENSOR_NOT_REQUIRED ); 41 42if (output == NULL ) { 43output = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },TENSOR_DUPLICATED ); 44 } 45 46for (int i = 0 ;i < n_layer ;++ i ) { 47auto & layer = layers [i ]; 48 49const bool is_moe_layer = i >=static_cast < int > (hparams .n_layer_dense_lead ); 50 51// ffn/moe is same for transformer and conv layers 52layer .ffn_norm = create_tensor (tn (LLM_TENSOR_FFN_NORM ,"weight" ,i ), {n_embd },0 ); 53if (is_moe_layer ) { 54GGML_ASSERT (n_expert && n_expert_used ); 55layer .ffn_gate_inp = create_tensor (tn (LLM_TENSOR_FFN_GATE_INP ,"weight" ,i ), {n_embd ,n_expert },0 ); 56layer .ffn_gate_exps = create_tensor (tn (LLM_TENSOR_FFN_GATE_EXPS ,"weight" ,i ), {n_embd ,hparams .n_ff_exp (),n_expert },0 ); 57layer .ffn_down_exps = create_tensor (tn (LLM_TENSOR_FFN_DOWN_EXPS ,"weight" ,i ), {hparams .n_ff_exp (),n_embd ,n_expert },0 ); 58layer .ffn_up_exps = create_tensor (tn (LLM_TENSOR_FFN_UP_EXPS ,"weight" ,i ), {n_embd ,hparams .n_ff_exp (),n_expert },0 ); 59layer .ffn_exp_probs_b = create_tensor (tn (LLM_TENSOR_FFN_EXP_PROBS_B ,"bias" ,i ), {n_expert },0 ); 60 }else {// dense 61layer .ffn_gate = create_tensor (tn (LLM_TENSOR_FFN_GATE ,"weight" ,i ), {n_embd ,n_ff },0 ); 62layer .ffn_down = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"weight" ,i ), {n_ff ,n_embd },0 ); 63layer .ffn_up = create_tensor (tn (LLM_TENSOR_FFN_UP ,"weight" ,i ), {n_embd ,n_ff },0 ); 64 } 65 66// for operator_norm 67layer .attn_norm = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"weight" ,i ), {n_embd },0 ); 68 69if (!hparams .is_recr (i )) { 70layer .attn_q_norm = create_tensor (tn (LLM_TENSOR_ATTN_Q_NORM ,"weight" ,i ), {n_embd_head_k },0 ); 71layer .attn_k_norm = create_tensor (tn (LLM_TENSOR_ATTN_K_NORM ,"weight" ,i ), {n_embd_head_k },0 ); 72GGML_ASSERT (n_embd_v_gqa == n_embd_k_gqa ); 73 74create_tensor_qkv (layer ,i ,n_embd ,n_embd ,hparams .n_embd_k_gqa (i ),hparams .n_embd_v_gqa (i ),0 ); 75 76layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,i ), {n_embd ,n_embd },0 ); 77 }else { 78layer .shortconv .conv = create_tensor (tn (LLM_TENSOR_SHORTCONV_CONV ,"weight" ,i ), {hparams .n_shortconv_l_cache ,n_embd },0 ); 79layer .shortconv .in_proj = create_tensor (tn (LLM_TENSOR_SHORTCONV_INPROJ ,"weight" ,i ), {n_embd ,3 * n_embd },0 ); 80layer .shortconv .out_proj = create_tensor (tn (LLM_TENSOR_SHORTCONV_OUTPROJ ,"weight" ,i ), {n_embd ,n_embd },0 ); 81 } 82 } 83 84// for LFM2-ColBert-350M 85dense_2_out_layers = create_tensor (tn (LLM_TENSOR_DENSE_2_OUT ,"weight" ), {n_embd ,hparams .n_embd_out ()},TENSOR_NOT_REQUIRED ); 86dense_2_out_layers_b = create_tensor (tn (LLM_TENSOR_DENSE_2_OUT ,"bias" ), {hparams .n_embd_out () },TENSOR_NOT_REQUIRED ); 87} 88 89std::unique_ptr < llm_graph_context > llama_model_lfm2::build_arch_graph (const llm_graph_params & params )const { 90if (hparams .swa_type == LLAMA_SWA_TYPE_STANDARD ) { 91return std::make_unique < graph < true>> (* this ,params ); 92 }else { 93return std::make_unique < graph < false>> (* this ,params ); 94 } 95} 96 97template < bool iswa > 98llama_model_lfm2::graph < iswa > ::graph (const llama_model & model ,const llm_graph_params & params ) : 99llm_graph_context (params ) { 100using inp_hybrid_type = std::conditional_t < iswa ,llm_graph_input_mem_hybrid_iswa ,llm_graph_input_mem_hybrid > ; 101using inp_attn_type = std::conditional_t < iswa ,llm_graph_input_attn_kv_iswa ,llm_graph_input_attn_kv > ; 102using mem_hybrid_ctx = std::conditional_t < iswa ,llama_memory_hybrid_iswa_context ,llama_memory_hybrid_context > ; 103 104// lambda helpers for readability 105auto build_dense_feed_forward = [& model ,this ](ggml_tensor * cur ,int il )-> ggml_tensor * { 106GGML_ASSERT (!model .layers [il ].ffn_up_b ); 107GGML_ASSERT (!model .layers [il ].ffn_gate_b ); 108GGML_ASSERT (!model .layers [il ].ffn_down_b ); 109return build_ffn (cur , 110model .layers [il ].ffn_up ,NULL ,NULL , 111model .layers [il ].ffn_gate ,NULL ,NULL , 112model .layers [il ].ffn_down ,NULL ,NULL , 113NULL ,LLM_FFN_SILU ,LLM_FFN_PAR ,il ); 114 }; 115auto build_moe_feed_forward = [& model ,this ](ggml_tensor * cur ,int il )-> ggml_tensor * { 116return build_moe_ffn (cur , 117model .layers [il ].ffn_gate_inp , 118model .layers [il ].ffn_up_exps , 119model .layers [il ].ffn_gate_exps , 120model .layers [il ].ffn_down_exps , 121model .layers [il ].ffn_exp_probs_b , 122n_expert ,n_expert_used , 123LLM_FFN_SILU , true, 124hparams .expert_weights_scale , 125static_cast < llama_expert_gating_func_type > (hparams .expert_gating_func ), 126il ); 127 }; 128auto build_attn_block = [& model ,this ](ggml_tensor * cur , 129ggml_tensor * inp_pos , 130inp_attn_type * inp_attn , 131int il )-> ggml_tensor * { 132GGML_ASSERT (hparams .n_embd_v_gqa (il )== hparams .n_embd_k_gqa (il )); 133const auto n_embd_head = hparams .n_embd_head_v (); 134const auto n_head_kv = hparams .n_head_kv (il ); 135 136auto [q ,k ,v ]= build_qkv (model .layers [il ],cur , 137n_embd_head ,n_head ,n_head_kv ,il ); 138 139// qk norm 140q = build_norm (q ,model .layers [il ].attn_q_norm ,NULL ,LLM_NORM_RMS ,il ); 141cb (q ,"model.layers.{}.self_attn.q_layernorm" ,il ); 142k = build_norm (k ,model .layers [il ].attn_k_norm ,NULL ,LLM_NORM_RMS ,il ); 143cb (k ,"model.layers.{}.self_attn.k_layernorm" ,il ); 144 145// RoPE 146q = ggml_rope_ext (ctx0 ,q ,inp_pos ,nullptr ,n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale ,ext_factor , 147attn_factor ,beta_fast ,beta_slow ); 148k = ggml_rope_ext (ctx0 ,k ,inp_pos ,nullptr ,n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale ,ext_factor , 149attn_factor ,beta_fast ,beta_slow ); 150 151cur = build_attn (inp_attn , 152model .layers [il ].wo ,NULL ,model .layers [il ].wo_s , 153q ,k ,v ,nullptr ,nullptr ,nullptr ,1.0f /sqrtf (float (n_embd_head )),il ); 154 155cb (cur ,"model.layers.{}.self_attn.out_proj" ,il ); 156 157return cur ; 158 }; 159auto build_shortconv_block = [& model ,this ](ggml_tensor * cur , 160llm_graph_input_rs * inp_recr , 161int il )-> ggml_tensor * { 162const auto * mctx_cur = static_cast < const mem_hybrid_ctx *> (mctx )-> get_recr (); 163const uint32_t kv_head = mctx_cur -> get_head (); 164const int64_t n_seq_tokens = ubatch .n_seq_tokens ; 165const int64_t n_seqs = ubatch .n_seqs ; 166GGML_ASSERT (n_seqs != 0 ); 167GGML_ASSERT (ubatch .equal_seqs ()); 168GGML_ASSERT (ubatch .n_tokens == n_seq_tokens * n_seqs ); 169 170GGML_ASSERT (hparams .n_shortconv_l_cache > 1 ); 171const uint32_t d_conv = hparams .n_shortconv_l_cache - 1 ; 172 173// {n_embd, n_tokens} => {n_embd, n_seq_tokens, n_seqs} 174cur = ggml_reshape_3d (ctx0 ,cur ,cur -> ne [0 ],n_seq_tokens ,n_seqs ); 175 176auto * bcx = build_lora_mm (model .layers [il ].shortconv .in_proj ,cur ); 177cb (bcx ,"model.layers.{}.conv.in_proj" ,il ); 178 179constexpr auto n_chunks = 3 ; 180GGML_ASSERT (bcx -> ne [0 ] %n_chunks == 0 ); 181const auto chunk_size = bcx -> ne [0 ] /n_chunks ; 182auto * b = ggml_view_3d (ctx0 ,bcx ,chunk_size ,bcx -> ne [1 ],bcx -> ne [2 ],bcx -> nb [1 ],bcx -> nb [2 ], 1830 * chunk_size * ggml_element_size (bcx )); 184auto * c = ggml_view_3d (ctx0 ,bcx ,chunk_size ,bcx -> ne [1 ],bcx -> ne [2 ],bcx -> nb [1 ],bcx -> nb [2 ], 1851 * chunk_size * ggml_element_size (bcx )); 186auto * x = ggml_view_3d (ctx0 ,bcx ,chunk_size ,bcx -> ne [1 ],bcx -> ne [2 ],bcx -> nb [1 ],bcx -> nb [2 ], 1872 * chunk_size * ggml_element_size (bcx )); 188 189auto * bx = ggml_transpose (ctx0 ,ggml_mul (ctx0 ,b ,x )); 190 191// read conv state 192auto * conv_state = mctx_cur -> get_r_l (il ); 193auto * conv_rs = build_rs (inp_recr ,conv_state ,hparams .n_embd_r (),n_seqs ); 194auto * conv = ggml_reshape_3d (ctx0 ,conv_rs ,d_conv ,hparams .n_embd ,n_seqs ); 195 196// causal prepends the state, non-causal pads symmetrically for a centered window 197if (hparams .causal_attn ) { 198bx = ggml_concat (ctx0 ,conv ,bx ,0 ); 199 }else { 200const int64_t pad = (hparams .n_shortconv_l_cache - 1 ) /2 ; 201auto * left = ggml_cont (ctx0 , 202ggml_view_3d (ctx0 ,conv ,pad ,hparams .n_embd ,n_seqs ,conv -> nb [1 ],conv -> nb [2 ], (d_conv - pad )* conv -> nb [0 ])); 203bx = ggml_pad_ext (ctx0 ,ggml_concat (ctx0 ,left ,bx ,0 ),0 ,pad ,0 ,0 ,0 ,0 ,0 ,0 ); 204 } 205GGML_ASSERT (bx -> ne [0 ]> conv -> ne [0 ]); 206 207// write conv states: slot 0 = the final state, slot s = the state s tokens back (partial rollback) 208const int64_t K = hparams .causal_attn && cparams .n_rs_seq > 0 ? (int64_t )cparams .n_rs_seq + 1 :1 ; 209const int64_t n_written = std::min < int64_t > (n_seq_tokens ,K ); 210const auto mem_size = mctx_cur -> get_size (); 211const size_t row_size = ggml_row_size (conv_state -> type , (int64_t )d_conv * n_embd ); 212 213for (int64_t slot = 0 ;slot < n_written ;++ slot ) { 214auto * conv_snap = ggml_view_3d (ctx0 ,bx ,d_conv ,bx -> ne [1 ],bx -> ne [2 ],bx -> nb [1 ],bx -> nb [2 ], 215 (bx -> ne [0 ]- d_conv - slot )* ggml_element_size (bx )); 216ggml_build_forward_expand (gf ,ggml_cpy (ctx0 ,conv_snap , 217ggml_view_2d (ctx0 ,conv_state , (int64_t )d_conv * n_embd ,n_seqs , 218conv_state -> nb [1 ], 219 ((size_t )slot * mem_size + kv_head )* row_size ))); 220 } 221 222auto * conv_kernel = model .layers [il ].shortconv .conv ; 223auto * conv_out = ggml_ssm_conv (ctx0 ,bx ,conv_kernel ); 224cb (conv_out ,"model.layers.{}.conv.conv" ,il ); 225 226auto * y = ggml_mul (ctx0 ,c ,conv_out ); 227y = build_lora_mm (model .layers [il ].shortconv .out_proj ,y ); 228cb (y ,"model.layers.{}.conv.out_proj" ,il ); 229// {n_embd, n_seq_tokens, n_seqs} => {n_embd, n_tokens} 230y = ggml_reshape_2d (ctx0 ,y ,y -> ne [0 ],n_seq_tokens * n_seqs ); 231 232return y ; 233 }; 234 235// actual graph construction starts here 236ggml_tensor * cur = build_inp_embd (model .tok_embd ); 237cb (cur ,"model.embed_tokens" ,-1 ); 238 239ggml_build_forward_expand (gf ,cur ); 240 241inp_hybrid_type * inp_hybrid = nullptr ; 242if constexpr (iswa ) { 243inp_hybrid = build_inp_mem_hybrid_iswa (); 244 }else { 245inp_hybrid = build_inp_mem_hybrid (); 246 } 247 248ggml_tensor * inp_pos = build_inp_pos (); 249ggml_tensor * inp_out_ids = build_inp_out_ids (); 250 251for (int il = 0 ;il < n_layer ;++ il ) { 252res -> t_layer_inp [il ]= cur ; 253 254const bool is_moe_layer = il >=static_cast < int > (hparams .n_layer_dense_lead ); 255 256auto * prev_cur = cur ; 257cur = build_norm (cur ,model .layers [il ].attn_norm ,NULL ,LLM_NORM_RMS ,il ); 258cb (cur ,"model.layers.{}.operator_norm" ,il ); 259 260cur = hparams .is_recr (il ) ?build_shortconv_block (cur ,inp_hybrid -> get_recr (),il ) : 261build_attn_block (cur ,inp_pos ,inp_hybrid -> get_attn (),il ); 262 263if (il == n_layer - 1 && inp_out_ids ) { 264cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 265prev_cur = ggml_get_rows (ctx0 ,prev_cur ,inp_out_ids ); 266 } 267 268cur = ggml_add (ctx0 ,prev_cur ,cur ); 269 270auto * ffn_norm_out = build_norm (cur ,model .layers [il ].ffn_norm ,NULL ,LLM_NORM_RMS ,il ); 271cb (ffn_norm_out ,"model.layers.{}.ffn_norm" ,il ); 272 273ggml_tensor * ffn_out = 274is_moe_layer ?build_moe_feed_forward (ffn_norm_out ,il ) :build_dense_feed_forward (ffn_norm_out ,il ); 275cb (ffn_norm_out ,"model.layers.{}.ffn_out" ,il ); 276 277cur = ggml_add (ctx0 ,cur ,ffn_out ); 278 279cur = build_cvec (cur ,il ); 280cb (cur ,"l_out" ,il ); 281 } 282 283cur = build_norm (cur ,model .output_norm ,NULL ,LLM_NORM_RMS ,-1 ); 284cb (cur ,"result_norm" ,-1 ); 285res -> t_embd = cur ; 286 287if (!cparams .embeddings ) { 288cur = build_lora_mm (model .output ,cur ,model .output_s ); 289cb (cur ,"result_output" ,-1 ); 290 291res -> t_logits = cur ; 292 } 293 294ggml_build_forward_expand (gf ,cur ); 295} 296 297// Explicit template instantiations 298template struct llama_model_lfm2::graph < true> ; 299template struct llama_model_lfm2:: graph < false > ;