char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
5a6caa05f
char/aliceai-t5-moe
1#include "models.h" 2#include "llama-kv-cache-msa.h" 3#include <cmath> 4#include <vector> 5#include <cstdint> 6 7// MiniMax-M3: MiniMax-M2 style GQA (per-head QK-norm, partial rotary) with 8// DeepSeek-V3 leading-dense + routed/shared experts (sigmoid gating, routed scaling), 9// swigluoai activation, and MiniMax Sparse Attention (MSA). MTP is not in released model weights. 10// MSA blocks are defined over token positions. The graph translates between position space (block 11// selection) and cell space (K/V/indexer storage) via per-ubatch pos<->cell maps populated from llama_kv_cells 12 13void llama_model_minimax_m3::load_arch_hparams (llama_model_loader & ml ) { 14ml .get_key (LLM_KV_ATTENTION_LAYERNORM_RMS_EPS ,hparams .f_norm_rms_eps ); 15ml .get_key (LLM_KV_LEADING_DENSE_BLOCK_COUNT ,hparams .n_layer_dense_lead , false); 16ml .get_key_or_arr (LLM_KV_EXPERT_FEED_FORWARD_LENGTH ,hparams .n_ff_exp_arr ,hparams .n_layer_all ); 17ml .get_key (LLM_KV_EXPERT_SHARED_COUNT ,hparams .n_expert_shared ); 18ml .get_key (LLM_KV_EXPERT_WEIGHTS_SCALE ,hparams .expert_weights_scale , false); 19ml .get_key (LLM_KV_EXPERT_WEIGHTS_NORM ,hparams .expert_weights_norm , false); 20ml .get_key (LLM_KV_EXPERT_GATING_FUNC ,hparams .expert_gating_func ); 21ml .get_key (LLM_KV_ATTENTION_INDEXER_HEAD_COUNT ,hparams .indexer_n_head ); 22ml .get_key (LLM_KV_ATTENTION_INDEXER_KEY_LENGTH ,hparams .indexer_head_size ); 23ml .get_key (LLM_KV_ATTENTION_INDEXER_TOP_K ,hparams .indexer_top_k ); 24ml .get_key (LLM_KV_ATTENTION_INDEXER_BLOCK_SIZE ,hparams .indexer_block_size ); 25ml .get_key (LLM_KV_ATTENTION_INDEXER_LOCAL_BLOCKS ,hparams .indexer_local_blocks ); 26msa_p = { (int )hparams .indexer_block_size , (int )hparams .indexer_top_k , (int )hparams .indexer_local_blocks }; 27 28GGML_ASSERT (hparams .indexer_block_size > 0 );// avoid div by zero 29 30switch (hparams .n_layer ()) { 31case 60 :type = LLM_TYPE_428B_A23B ;break ; 32default :type = LLM_TYPE_UNKNOWN ; 33 } 34} 35 36void llama_model_minimax_m3::load_arch_tensors (llama_model_loader & ) { 37LLAMA_LOAD_LOCALS ; 38const int64_t n_expert_shared = hparams .n_expert_shared ; 39const int64_t n_ff_exp = hparams .n_ff_exp (); 40 41tok_embd = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },0 ); 42 43// output 44output_norm = create_tensor (tn (LLM_TENSOR_OUTPUT_NORM ,"weight" ), {n_embd },0 ); 45output = create_tensor (tn (LLM_TENSOR_OUTPUT ,"weight" ), {n_embd ,n_vocab },0 ); 46 47for (int i = 0 ;i < n_layer ;++ i ) { 48auto & layer = layers [i ]; 49 50create_tensor_qkv (layer ,i ,n_embd ,n_embd_head_k * n_head ,n_embd_gqa ,n_embd_gqa ,0 ); 51layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,i ), {n_embd_head_k * n_head ,n_embd },0 ); 52 53layer .attn_norm = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"weight" ,i ), {n_embd },0 ); 54// per-head QK-norm: a single head_dim vector applied to every head 55layer .attn_q_norm = create_tensor (tn (LLM_TENSOR_ATTN_Q_NORM ,"weight" ,i ), {n_embd_head_k },0 ); 56layer .attn_k_norm = create_tensor (tn (LLM_TENSOR_ATTN_K_NORM ,"weight" ,i ), {n_embd_head_k },0 ); 57 58layer .ffn_norm = create_tensor (tn (LLM_TENSOR_FFN_NORM ,"weight" ,i ), {n_embd },0 ); 59 60if (i < (int )hparams .n_layer_dense_lead ) { 61// leading dense layers 62layer .ffn_gate = create_tensor (tn (LLM_TENSOR_FFN_GATE ,"weight" ,i ), {n_embd ,n_ff },0 ); 63layer .ffn_down = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"weight" ,i ), {n_ff ,n_embd },0 ); 64layer .ffn_up = create_tensor (tn (LLM_TENSOR_FFN_UP ,"weight" ,i ), {n_embd ,n_ff },0 ); 65 }else { 66// routed experts 67layer .ffn_gate_inp = create_tensor (tn (LLM_TENSOR_FFN_GATE_INP ,"weight" ,i ), {n_embd ,n_expert },0 ); 68layer .ffn_exp_probs_b = create_tensor (tn (LLM_TENSOR_FFN_EXP_PROBS_B ,"bias" ,i ), {n_expert },0 ); 69layer .ffn_gate_exps = create_tensor (tn (LLM_TENSOR_FFN_GATE_EXPS ,"weight" ,i ), {n_embd ,n_ff_exp ,n_expert },0 ); 70layer .ffn_down_exps = create_tensor (tn (LLM_TENSOR_FFN_DOWN_EXPS ,"weight" ,i ), {n_ff_exp ,n_embd ,n_expert },0 ); 71layer .ffn_up_exps = create_tensor (tn (LLM_TENSOR_FFN_UP_EXPS ,"weight" ,i ), {n_embd ,n_ff_exp ,n_expert },0 ); 72 73// shared expert 74layer .ffn_gate_shexp = create_tensor (tn (LLM_TENSOR_FFN_GATE_SHEXP ,"weight" ,i ), {n_embd ,n_ff_exp * n_expert_shared },0 ); 75layer .ffn_down_shexp = create_tensor (tn (LLM_TENSOR_FFN_DOWN_SHEXP ,"weight" ,i ), {n_ff_exp * n_expert_shared ,n_embd },0 ); 76layer .ffn_up_shexp = create_tensor (tn (LLM_TENSOR_FFN_UP_SHEXP ,"weight" ,i ), {n_embd ,n_ff_exp * n_expert_shared },0 ); 77 78// indexer 79layer .index_q_proj = create_tensor (tn (LLM_TENSOR_INDEXER_Q_PROJ ,"weight" ,i ), {n_embd ,hparams .indexer_n_head * hparams .indexer_head_size },0 ); 80layer .index_k_proj = create_tensor (tn (LLM_TENSOR_INDEXER_K_PROJ ,"weight" ,i ), {n_embd ,hparams .indexer_head_size },0 ); 81layer .index_q_norm = create_tensor (tn (LLM_TENSOR_INDEXER_Q_NORM ,"weight" ,i ), {hparams .indexer_head_size },0 ); 82layer .index_k_norm = create_tensor (tn (LLM_TENSOR_INDEXER_K_NORM ,"weight" ,i ), {hparams .indexer_head_size },0 ); 83 } 84 } 85} 86 87std::unique_ptr < llm_graph_context > llama_model_minimax_m3::build_arch_graph (const llm_graph_params & params )const { 88return std::make_unique < graph > (* this ,params ); 89} 90 91class llm_graph_input_msa :public llm_graph_input_i { 92public : 93llm_graph_input_msa (const llama_kv_cache_msa_context * mctx ,int blk ,int local ) : 94mctx (mctx ),blk (blk ),local (local ) {} 95 96void set_input (const llama_ubatch * ubatch )override { 97if (pos_slot_i ) {mctx -> set_input_pos_slot (pos_slot_i ,ubatch ); } 98if (pos_slot_f ) {mctx -> set_input_pos_slot (pos_slot_f ,ubatch ); } 99if (cell_blk ) {mctx -> set_input_cell_pos (cell_blk ,ubatch ,blk ); } 100if (pos_mask ) {mctx -> set_input_pos_mask (pos_mask ,ubatch ); } 101 102// local-force bias over position blocks 103if (bias && ubatch -> pos ) { 104const int64_t n_tokens = ubatch -> n_tokens ; 105const int64_t nblk = bias -> ne [0 ]; 106 std::vector < float > data ((size_t )nblk * n_tokens ,0.0f ); 107for (int64_t i = 0 ;i < n_tokens ;++ i ) { 108const int64_t L = ubatch -> pos [i ] /blk ; 109for (int l = 0 ;l < local && L - l >=0 ;++ l ) { 110if (L - l < nblk ) { 111data [(size_t )i * nblk + (L - l )]= 1e30f ; 112 } 113 } 114 } 115ggml_backend_tensor_set (bias ,data .data (),0 ,data .size ()* sizeof (float )); 116 } 117 } 118 119// valid as long as the tensor dims still match the new ubatch/cache window and the 120// ubatch is in the same regime (decode graphs have pos_slot_f, batch graphs cell_blk) 121bool can_reuse (const llm_graph_params & params )override { 122const auto * mctx_new = static_cast < const llama_kv_cache_msa_context *> (params .mctx ); 123 124this -> mctx = mctx_new ; 125 126const int64_t n_ps = GGML_PAD ((int64_t )mctx_new -> get_n_pos (),blk ); 127const int64_t ns = params .cparams .kv_unified ?1 :params .ubatch .n_seqs_unq ; 128 129const bool decode = params .ubatch .n_tokens == ns ;// one token per stream 130 131bool res = true; 132 133res &=bias -> ne [0 ]* blk == n_ps ; 134res &=bias -> ne [1 ]== params .ubatch .n_tokens ; 135 136res &=pos_mask -> ne [0 ]== n_ps ; 137res &=pos_mask -> ne [1 ]== params .ubatch .n_tokens ; 138 139res &=pos_slot_i -> ne [0 ]== n_ps ; 140res &=pos_slot_i -> ne [1 ]== ns ; 141 142res &=decode == (pos_slot_f != nullptr ); 143res &=decode == (cell_blk == nullptr ); 144 145if (pos_slot_f ) { 146res &=pos_slot_f -> ne [0 ]== n_ps ; 147res &=pos_slot_f -> ne [1 ]== ns ; 148 } 149 150if (cell_blk ) { 151res &=cell_blk -> ne [0 ]== (int64_t )mctx_new -> get_base ()-> get_n_kv (); 152res &=cell_blk -> ne [1 ]== ns ; 153 } 154 155return res ; 156 } 157 158ggml_tensor * bias = nullptr ;// F32 [nblk, n_tokens] local-force bias (position blocks) 159ggml_tensor * pos_mask = nullptr ;// F32 [n_ps, n_tokens] 0/-inf visibility, by position 160ggml_tensor * pos_slot_i = nullptr ;// I32 [n_ps, ns] pos -> cell (get_rows index) 161ggml_tensor * pos_slot_f = nullptr ;// F32 [n_ps, ns] pos -> cell (gatherable values, decode) 162ggml_tensor * cell_blk = nullptr ;// I32 [n_kv, ns] cell -> position block (batch) 163 164const llama_kv_cache_msa_context * mctx ; 165 166int blk ; 167int local ; 168}; 169 170// One FA call for all GQA groups (and at multi-stream decode, all streams) by mapping them onto the FA sequence dim (ne[3]) 171ggml_tensor * llama_model_minimax_m3::graph::build_attn_msa_fa ( 172ggml_tensor * q_cur ,// [D, HQ, T] 173ggml_tensor * k ,// [D, n_keys, 1, C] 174ggml_tensor * v ,// [D, n_keys, 1, C] 175ggml_tensor * mask ,// [n_keys, R, 1, C] f16, contiguous 176int64_t Gp ,float kq_scale ,int il )const { 177 178const int64_t D = q_cur -> ne [0 ]; 179const int64_t HQ = q_cur -> ne [1 ]; 180const int64_t T = q_cur -> ne [2 ]; 181const int64_t C = k -> ne [3 ]; 182const int64_t R = HQ * T /(Gp * C ); 183GGML_ASSERT (Gp * C * R == HQ * T ); 184GGML_ASSERT (mask -> type == GGML_TYPE_F16 ); 185 186// [D, HQ, T] -> [D, Gp, C, R] -> [D, R, Gp, C] 187// batch (C=HKV, R=T): channel = group 188// decode (C=HKV*ns, R=1): channel = (group, stream), group innermost 189ggml_tensor * q = ggml_reshape_4d (ctx0 ,q_cur ,D ,Gp ,C ,R ); 190q = ggml_permute (ctx0 ,q ,0 ,2 ,3 ,1 ); 191 192ggml_tensor * o = ggml_flash_attn_ext (ctx0 ,q ,k ,v ,mask ,kq_scale , 193hparams .f_max_alibi_bias ,0.0f ); 194ggml_prec_set_acc (o ,GGML_PREC_F32 ); 195cb (o ,"msa_fattn" ,il ); 196 197// [D, Gp, R, C] -> [D, Gp, C, R] -> [n_embd, T] 198o = ggml_permute (ctx0 ,o ,0 ,1 ,3 ,2 ); 199if (!ggml_is_contiguous (o )) { 200o = ggml_cont (ctx0 ,o );// no-op layout at decode (R == 1), copy at batch 201 } 202return ggml_reshape_2d (ctx0 ,o ,D * HQ ,T ); 203} 204 205llama_model_minimax_m3::graph::graph (const llama_model & model ,const llm_graph_params & params ) :llm_graph_context (params ) { 206const int64_t n_embd_head = hparams .n_embd_head_v (); 207const auto & mm = static_cast < const llama_model_minimax_m3 &> (model ); 208 209GGML_ASSERT (n_embd_head == hparams .n_embd_head_k ()); 210// partial rotary: head_dim != n_rot, so don't assert n_embd_head == n_rot 211 212ggml_tensor * cur ; 213ggml_tensor * inpL ; 214 215inpL = build_inp_embd (model .tok_embd ); 216 217ggml_tensor * inp_pos = build_inp_pos (); 218 219// ========================================== 220// TODO: avoid such kind of complexity in the model graphs 221 222// MSA calls ggml_flash_attn_ext directly and assumes the non-transposed V layout that 223// llama.cpp only provides when flash attention is enabled. Block selection is anchored 224// to absolute KV cache slots, which equal positions only for append-only per-stream 225// caches either a single sequence, or multiple sequences with kv_unified == false (each 226// stream then has its own slot space). A unified cache with multiple sequences 227// interleaves slots and would silently break block anchoring so it falls back to dense. 228const bool fa_on = cparams .flash_attn ; 229const bool streams_ok = cparams .n_seq_max == 1 || !cparams .kv_unified ; 230const bool msa_enabled = fa_on && streams_ok ; 231 232auto * inp_attn = build_attn_inp_kv_msa (msa_enabled ); 233 234static bool warned_no_fa = false; 235if (!fa_on && !warned_no_fa ) { 236LLAMA_LOG_WARN ("%s: flash attention disabled; MSA requires it -> running DENSE attention " 237"(output may be degraded). Enable flash attention for MSA.\n" ,__func__ ); 238warned_no_fa = true; 239 } 240static bool warned_unified = false; 241if (fa_on && !streams_ok && !warned_unified ) { 242LLAMA_LOG_WARN ("%s: unified KV cache with n_seq_max > 1; MSA needs per-sequence streams " 243"-> running DENSE attention. Output may be degraded. Drop --kv-unified to enable MSA.\n" ,__func__ ); 244warned_unified = true; 245 } 246// ========================================== 247 248// hoisted per-graph MSA state (shared by every sparse layer) 249llm_graph_input_msa * msa = nullptr ; 250ggml_tensor * msa_kqm = nullptr ; 251ggml_tensor * msa_mf = nullptr ;// F32 copy of the FA mask for the final mask add 252int64_t n_kv = 0 ,n_ps = 0 ,nblk = 0 ,ns = 1 ,n_tps = 0 ; 253bool msa_decode = false;// gather (1 token per stream) vs mask 254const int blk = mm .msa_p .blk ; 255const int64_t Hd = hparams .indexer_n_head ;// one indexer head per GQA group 256 257if (msa_enabled ) { 258const auto * mctx_msa = static_cast < const llama_kv_cache_msa_context *> (mctx ); 259 260msa_kqm = inp_attn -> get_kq_mask (); 261n_kv = msa_kqm -> ne [0 ]; 262n_tps = msa_kqm -> ne [1 ];// tokens per stream 263ns = msa_kqm -> ne [3 ];// streams in this ubatch 264GGML_ASSERT (msa_kqm -> type == GGML_TYPE_F16 && "MSA requires the FA (f16) mask" ); 265GGML_ASSERT (n_tps * ns == n_tokens ); 266 267// the position axis covers every position currently in the cache and is padded to whole blocks 268n_ps = GGML_PAD ((int64_t )mctx_msa -> get_n_pos (),blk ); 269nblk = n_ps /blk ; 270msa_decode = n_tps == 1 ; 271 272auto inp = std::make_unique < llm_graph_input_msa > (mctx_msa ,blk ,mm .msa_p .local ); 273 274inp -> bias = ggml_new_tensor_2d (ctx0 ,GGML_TYPE_F32 ,nblk ,n_tokens );// stream-grouped tokens 275ggml_set_input (inp -> bias ); 276 277inp -> pos_mask = ggml_new_tensor_2d (ctx0 ,GGML_TYPE_F32 ,n_ps ,n_tokens ); 278ggml_set_input (inp -> pos_mask ); 279 280inp -> pos_slot_i = ggml_new_tensor_2d (ctx0 ,GGML_TYPE_I32 ,n_ps ,ns ); 281ggml_set_input (inp -> pos_slot_i ); 282 283if (msa_decode ) { 284inp -> pos_slot_f = ggml_new_tensor_2d (ctx0 ,GGML_TYPE_F32 ,n_ps ,ns ); 285ggml_set_input (inp -> pos_slot_f ); 286 }else { 287inp -> cell_blk = ggml_new_tensor_2d (ctx0 ,GGML_TYPE_I32 ,n_kv ,ns ); 288ggml_set_input (inp -> cell_blk ); 289 290msa_mf = ggml_cast (ctx0 ,msa_kqm ,GGML_TYPE_F32 ); 291 } 292 293msa = (llm_graph_input_msa * )res -> add_input (std::move (inp )); 294 } 295 296ggml_tensor * inp_out_ids = build_inp_out_ids (); 297 298for (int il = 0 ;il < n_layer ;++ il ) { 299ggml_tensor * inpSA = inpL ; 300 301// self-attention 302 { 303cur = build_norm (inpL ,model .layers [il ].attn_norm ,NULL ,LLM_NORM_RMS ,il ); 304cb (cur ,"attn_norm" ,il ); 305 306auto [Qcur ,Kcur ,Vcur ]= build_qkv (model .layers [il ],cur , 307n_embd_head ,n_head ,n_head_kv ,il ); 308 309// per-head QK RMSNorm (weights already include Gemma's +1) 310Qcur = build_norm (Qcur ,model .layers [il ].attn_q_norm ,NULL ,LLM_NORM_RMS ,il ); 311cb (Qcur ,"Qcur_normed" ,il ); 312Kcur = build_norm (Kcur ,model .layers [il ].attn_k_norm ,NULL ,LLM_NORM_RMS ,il ); 313cb (Kcur ,"Kcur_normed" ,il ); 314 315// partial rotary: only the first n_rot dims are rotated 316Qcur = ggml_rope_ext ( 317ctx0 ,Qcur ,inp_pos ,nullptr , 318n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 319ext_factor ,attn_factor ,beta_fast ,beta_slow ); 320Kcur = ggml_rope_ext ( 321ctx0 ,Kcur ,inp_pos ,nullptr , 322n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 323ext_factor ,attn_factor ,beta_fast ,beta_slow ); 324 325cb (Qcur ,"Qcur" ,il ); 326cb (Kcur ,"Kcur" ,il ); 327cb (Vcur ,"Vcur" ,il ); 328 329const bool is_sparse = msa_enabled && il >= (int )hparams .n_layer_dense_lead ; 330 331if (!is_sparse ) { 332cur = build_attn (inp_attn ,model .layers [il ].wo ,NULL ,model .layers [il ].wo_s , 333Qcur ,Kcur ,Vcur ,nullptr ,nullptr ,nullptr , 3341.0f /sqrtf (float (n_embd_head )),il ); 335 }else { 336const int64_t n_idx_dim = hparams .indexer_head_size ;// 128 337 338// Index Branch, project, norm, partial RoPE, cache 339ggml_tensor * iq = build_lora_mm (model .layers [il ].index_q_proj ,cur ); 340ggml_tensor * ik = build_lora_mm (model .layers [il ].index_k_proj ,cur ); 341iq = ggml_reshape_3d (ctx0 ,iq ,n_idx_dim ,Hd ,n_tokens ); 342ik = ggml_reshape_3d (ctx0 ,ik ,n_idx_dim ,1 ,n_tokens ); 343iq = build_norm (iq ,model .layers [il ].index_q_norm ,NULL ,LLM_NORM_RMS ,il );// +1 baked 344ik = build_norm (ik ,model .layers [il ].index_k_norm ,NULL ,LLM_NORM_RMS ,il ); 345iq = ggml_rope_ext (ctx0 ,iq ,inp_pos ,nullptr ,n_rot ,rope_type ,n_ctx_orig , 346freq_base ,freq_scale ,ext_factor ,attn_factor ,beta_fast ,beta_slow ); 347ik = ggml_rope_ext (ctx0 ,ik ,inp_pos ,nullptr ,n_rot ,rope_type ,n_ctx_orig , 348freq_base ,freq_scale ,ext_factor ,attn_factor ,beta_fast ,beta_slow ); 349 350const auto * mctx_msa_l = static_cast < const llama_kv_cache_msa_context *> (mctx ); 351const auto * mctx_cur = mctx_msa_l -> get_base (); 352const auto * mctx_idx = mctx_msa_l -> get_idx (); 353ggml_build_forward_expand (gf ,mctx_idx -> cpy_k (ctx0 ,ik ,inp_attn -> get_k_idxs_idx (),il )); 354ggml_tensor * ik_kv = mctx_idx -> get_k (ctx0 ,il ); 355 356if (inp_attn -> self_k_rot ) { 357Qcur = llama_mul_mat_hadamard (ctx0 ,Qcur ,inp_attn -> self_k_rot ); 358Kcur = llama_mul_mat_hadamard (ctx0 ,Kcur ,inp_attn -> self_k_rot ); 359 } 360if (inp_attn -> self_v_rot ) { 361Vcur = llama_mul_mat_hadamard (ctx0 ,Vcur ,inp_attn -> self_v_rot ); 362 } 363 364// Main branch: store K/V, take cache views 365ggml_build_forward_expand (gf ,Qcur ); 366ggml_build_forward_expand (gf ,Kcur ); 367ggml_build_forward_expand (gf ,Vcur ); 368ggml_build_forward_expand (gf ,mctx_cur -> cpy_k (ctx0 ,Kcur ,inp_attn -> get_k_idxs (),il )); 369ggml_build_forward_expand (gf ,mctx_cur -> cpy_v (ctx0 ,Vcur ,inp_attn -> get_v_idxs (),il )); 370ggml_tensor * k = mctx_cur -> get_k (ctx0 ,il ); 371ggml_tensor * v = mctx_cur -> get_v (ctx0 ,il ); 372GGML_ASSERT (!(v -> nb [1 ]> v -> nb [2 ])&& "MSA assumes v_trans=false (FA on)" ); 373 374const int64_t D = k -> ne [0 ]; 375const int64_t HKV = k -> ne [1 ]; 376const int64_t Gp = n_head /HKV ; 377GGML_ASSERT (HKV == Hd && "MSA: one indexer head per GQA group" ); 378GGML_ASSERT (k -> ne [3 ]== ns ); 379const int K = mm .msa_p .topk_blocks < (int )nblk ?mm .msa_p .topk_blocks : (int )nblk ; 380 381const float kq_scale = 1.0f /sqrtf (float (n_embd_head )); 382 383if (msa_decode ) { 384// decode: batched over streams top-k + gather, one grouped FA 385// gather the indexer keys through the pos -> cell map 386ggml_tensor * ik3 = ggml_view_3d (ctx0 ,ik_kv ,n_idx_dim ,n_kv ,ns , 387ik_kv -> nb [2 ],ik_kv -> nb [3 ],0 ); 388ggml_tensor * ikp = ggml_get_rows (ctx0 ,ik3 ,msa -> pos_slot_i );// [n_idx_dim, n_ps, ns] 389ggml_tensor * iq4 = ggml_reshape_4d (ctx0 ,iq ,n_idx_dim ,Hd ,1 ,ns ); 390ggml_tensor * sc = ggml_mul_mat (ctx0 , 391ggml_reshape_4d (ctx0 ,ikp ,n_idx_dim ,n_ps ,1 ,ns ),iq4 ); 392ggml_prec_set_acc (sc ,GGML_PREC_F32 ); 393// unmapped positions come out -inf, so they can never rank into the top-k 394sc = ggml_add_inplace (ctx0 ,sc , 395ggml_reshape_4d (ctx0 ,msa -> pos_mask ,n_ps ,1 ,1 ,ns )); 396ggml_tensor * bs = ggml_pool_2d (ctx0 ,sc ,GGML_OP_POOL_MAX ,blk ,1 ,blk ,1 ,0 ,0 ); 397cb (bs ,"msa_bs" ,il ); 398 399ggml_tensor * bsf = ggml_add (ctx0 ,bs , 400ggml_reshape_4d (ctx0 ,msa -> bias ,nblk ,1 ,1 ,ns )); 401ggml_tensor * idx = ggml_top_k (ctx0 ,bsf ,K );// position blocks 402 403// pos idx: tj[t,k,h,s] = blk*idx[k,h,s] + t (positions - mask gather) 404// cell idx: cs[t,k,h,s] = pos_slot[tj] (pos -> cell translation) 405// row idx: tr[t,k,h,s] = cs*HKV + h (per-stream K/V gather) 406ggml_tensor * a = ggml_scale (ctx0 ,ggml_cast (ctx0 ,idx ,GGML_TYPE_F32 ), (float )blk ); 407a = ggml_reshape_4d (ctx0 ,a ,1 ,K ,Hd ,ns ); 408ggml_tensor * tj = ggml_add (ctx0 , 409ggml_repeat_4d (ctx0 ,a ,blk ,K ,Hd ,ns ), 410ggml_reshape_3d (ctx0 ,ggml_arange (ctx0 ,0.0f , (float )blk ,1.0f ),blk ,1 ,1 )); 411 412ggml_tensor * tokj = ggml_cast (ctx0 ,ggml_reshape_2d (ctx0 ,tj , (int64_t )blk * K * Hd ,ns ),GGML_TYPE_I32 ); 413 414ggml_tensor * cs = ggml_get_rows (ctx0 , 415ggml_reshape_3d (ctx0 ,msa -> pos_slot_f ,1 ,n_ps ,ns ),tokj );// [1, blk*K*Hd, ns] 416cs = ggml_reshape_4d (ctx0 ,cs ,blk ,K ,Hd ,ns ); 417 418ggml_tensor * tr = ggml_add (ctx0 , 419ggml_scale (ctx0 ,cs , (float )HKV ), 420ggml_reshape_3d (ctx0 ,ggml_arange (ctx0 ,0.0f , (float )HKV ,1.0f ),1 ,1 ,Hd )); 421 422ggml_tensor * tokr = ggml_cast (ctx0 ,ggml_reshape_2d (ctx0 ,tr , (int64_t )blk * K * Hd ,ns ),GGML_TYPE_I32 ); 423 424ggml_tensor * k3 = ggml_view_3d (ctx0 ,k ,D ,HKV * n_kv ,ns ,k -> nb [1 ],k -> nb [3 ],0 ); 425ggml_tensor * v3 = ggml_view_3d (ctx0 ,v ,D ,HKV * n_kv ,ns ,v -> nb [1 ],v -> nb [3 ],0 ); 426ggml_tensor * mp = ggml_reshape_3d (ctx0 ,msa -> pos_mask ,1 ,n_ps ,ns ); 427 428ggml_tensor * kg = ggml_get_rows (ctx0 ,k3 ,tokr ); 429ggml_tensor * vg = ggml_get_rows (ctx0 ,v3 ,tokr ); 430ggml_tensor * mg = ggml_get_rows (ctx0 ,mp ,tokj ); 431 432// fold (group, stream) onto the FA channel dim 433const ggml_type kt = ggml_is_quantized (k -> type ) ?GGML_TYPE_F16 :k -> type ; 434const ggml_type vt = ggml_is_quantized (v -> type ) ?GGML_TYPE_F16 :v -> type ; 435ggml_tensor * kfa = ggml_reshape_4d (ctx0 ,kg ,D , (int64_t )blk * K ,1 ,Hd * ns ); 436ggml_tensor * vfa = ggml_reshape_4d (ctx0 ,vg ,D , (int64_t )blk * K ,1 ,Hd * ns ); 437if (kfa -> type != kt ) {kfa = ggml_cast (ctx0 ,kfa ,kt ); } 438if (vfa -> type != vt ) {vfa = ggml_cast (ctx0 ,vfa ,vt ); } 439// the FA mask must be F16 440ggml_tensor * mfa = ggml_cast (ctx0 ,ggml_reshape_4d (ctx0 ,mg , (int64_t )blk * K ,1 ,1 ,Hd * ns ),GGML_TYPE_F16 ); 441 442cur = build_attn_msa_fa (Qcur ,kfa ,vfa ,mfa ,Gp ,kq_scale ,il ); 443 }else { 444// batch: per-stream loop 445 std::vector < ggml_tensor *> outs (ns ); 446for (int64_t st = 0 ;st < ns ;++ st ) { 447ggml_tensor * iq_s = ggml_view_3d (ctx0 ,iq ,n_idx_dim ,Hd ,n_tps , 448iq -> nb [1 ],iq -> nb [2 ],st * n_tps * iq -> nb [2 ]); 449ggml_tensor * ik_s = ggml_view_2d (ctx0 ,ik_kv ,n_idx_dim ,n_kv , 450ik_kv -> nb [2 ],st * ik_kv -> nb [3 ]); 451ggml_tensor * psl_s = ggml_view_1d (ctx0 ,msa -> pos_slot_i ,n_ps , 452st * msa -> pos_slot_i -> nb [1 ]); 453ggml_tensor * pm_s = ggml_view_3d (ctx0 ,msa -> pos_mask ,n_ps ,1 ,n_tps , 454msa -> pos_mask -> nb [1 ],msa -> pos_mask -> nb [1 ],st * n_tps * msa -> pos_mask -> nb [1 ]); 455ggml_tensor * cb_s = ggml_view_1d (ctx0 ,msa -> cell_blk ,n_kv , 456st * msa -> cell_blk -> nb [1 ]); 457ggml_tensor * mf_s = ggml_view_3d (ctx0 ,msa_mf ,n_kv ,n_tps ,1 , 458msa_mf -> nb [1 ],msa_mf -> nb [3 ],st * msa_mf -> nb [3 ]); 459ggml_tensor * bias_s = ggml_view_3d (ctx0 ,msa -> bias ,nblk ,1 ,n_tps , 460msa -> bias -> nb [1 ],msa -> bias -> nb [1 ],st * n_tps * msa -> bias -> nb [1 ]); 461ggml_tensor * q_s = ggml_view_3d (ctx0 ,Qcur ,D ,n_head ,n_tps , 462Qcur -> nb [1 ],Qcur -> nb [2 ],st * n_tps * Qcur -> nb [2 ]); 463ggml_tensor * k_s = ggml_view_4d (ctx0 ,k ,D ,HKV ,n_kv ,1 , 464k -> nb [1 ],k -> nb [2 ],k -> nb [3 ],st * k -> nb [3 ]); 465ggml_tensor * v_s = ggml_view_4d (ctx0 ,v ,D ,HKV ,n_kv ,1 , 466v -> nb [1 ],v -> nb [2 ],v -> nb [3 ],st * v -> nb [3 ]); 467 468// block scores: the indexer keys are gathered through the pos -> cell map first 469// scores are unscaled, only the top-k ordering matters 470ggml_tensor * ikp = ggml_get_rows (ctx0 ,ik_s ,psl_s );// [n_idx_dim, n_ps] 471ggml_tensor * sc = ggml_mul_mat (ctx0 ,ikp , 472ggml_reshape_2d (ctx0 ,iq_s ,n_idx_dim ,Hd * n_tps )); 473// indexer scores run in F32 474ggml_prec_set_acc (sc ,GGML_PREC_F32 ); 475sc = ggml_reshape_3d (ctx0 ,sc ,n_ps ,Hd ,n_tps ); 476// unmapped positions (holes, padding, empty cells) come out -inf 477sc = ggml_add_inplace (ctx0 ,sc ,pm_s ); 478ggml_tensor * bs = ggml_pool_2d (ctx0 ,sc ,GGML_OP_POOL_MAX ,blk ,1 ,blk ,1 ,0 ,0 ); 479cb (bs ,"msa_bs" ,il ); 480 481// bias the scores so locally-forced blocks always rank first 482ggml_tensor * bsf = ggml_add (ctx0 ,bs ,bias_s );// [nblk, Hd, n_tps] 483cb (bsf ,"msa_bsf" ,il ); 484 485ggml_tensor * idx = ggml_top_k (ctx0 ,bsf ,K );// [K, Hd, n_tps] i32 486 487ggml_tensor * ninf = ggml_cast (ctx0 , 488ggml_scale_bias (ctx0 ,bias_s ,0.0f ,-1e30f ), 489GGML_TYPE_F16 );// [nblk, 1, n_tps] 490ninf = ggml_repeat_4d (ctx0 ,ninf ,nblk ,Hd ,n_tps ,1 ); 491ggml_tensor * zero = ggml_scale (ctx0 , 492ggml_cast (ctx0 ,idx ,GGML_TYPE_F32 ),0.0f ); 493ggml_tensor * bm = ggml_set_rows (ctx0 , 494ggml_reshape_3d (ctx0 ,ninf ,1 ,nblk ,Hd * n_tps ), 495ggml_reshape_3d (ctx0 ,zero ,1 ,K ,Hd * n_tps ), 496ggml_reshape_2d (ctx0 ,idx ,K ,Hd * n_tps )); 497bm = ggml_reshape_3d (ctx0 ,bm ,nblk ,Hd ,n_tps ); 498bm = ggml_cont (ctx0 ,ggml_permute (ctx0 ,bm ,0 ,2 ,1 ,3 ));// [nblk, n_tps, Hd] 499cb (bm ,"msa_block_mask" ,il ); 500 501// expand block -> cell granularity through the cell -> position block 502// map, then combine with the causal mask. empty cells are masked by the causal mask. 503ggml_tensor * bm2 = ggml_cont (ctx0 ,ggml_transpose (ctx0 , 504ggml_reshape_2d (ctx0 ,bm ,nblk ,n_tps * Hd )));// [n_tps*Hd, nblk] 505ggml_tensor * bmc = ggml_get_rows (ctx0 ,bm2 ,cb_s );// [n_tps*Hd, n_kv] F32 506ggml_tensor * bmx = ggml_cont (ctx0 ,ggml_transpose (ctx0 ,bmc )); 507bmx = ggml_reshape_3d (ctx0 ,bmx ,n_kv ,n_tps ,Hd ); 508ggml_tensor * mask4 = ggml_add_inplace (ctx0 ,bmx ,mf_s ); 509mask4 = ggml_cast (ctx0 , 510ggml_reshape_4d (ctx0 ,mask4 ,n_kv ,n_tps ,1 ,Hd ),GGML_TYPE_F16 ); 511cb (mask4 ,"msa_mask4" ,il ); 512 513// cache views with groups on ne[3]; 514ggml_tensor * kfa = ggml_permute (ctx0 ,k_s ,0 ,3 ,1 ,2 ); 515ggml_tensor * vfa = ggml_permute (ctx0 ,v_s ,0 ,3 ,1 ,2 ); 516 517outs [st ]= build_attn_msa_fa (q_s ,kfa ,vfa ,mask4 ,Gp ,kq_scale ,il ); 518 } 519cur = outs [0 ]; 520for (int64_t st = 1 ;st < ns ;++ st ) { 521cur = ggml_concat (ctx0 ,cur ,outs [st ],1 ); 522 } 523 } 524if (inp_attn -> self_v_rot ) { 525cur = llama_mul_mat_hadamard (ctx0 ,cur ,inp_attn -> self_v_rot ); 526 } 527cb (cur ,"kqv_out" ,il ); 528if (model .layers [il ].wo ) { 529cur = build_lora_mm (model .layers [il ].wo ,cur ,model .layers [il ].wo_s ); 530 } 531 } 532 } 533 534if (il == n_layer - 1 && inp_out_ids ) { 535cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 536inpSA = ggml_get_rows (ctx0 ,inpSA ,inp_out_ids ); 537 } 538 539ggml_tensor * ffn_inp = ggml_add (ctx0 ,cur ,inpSA ); 540cb (ffn_inp ,"ffn_inp" ,il ); 541 542cur = build_norm (ffn_inp ,model .layers [il ].ffn_norm ,NULL ,LLM_NORM_RMS ,il ); 543cb (cur ,"ffn_norm" ,il ); 544 545if ((uint32_t )il < hparams .n_layer_dense_lead ) { 546// leading dense FFN (swigluoai) 547cur = build_ffn (cur , 548model .layers [il ].ffn_up ,NULL ,NULL , 549model .layers [il ].ffn_gate ,NULL ,NULL , 550model .layers [il ].ffn_down ,NULL ,NULL , 551NULL , 552LLM_FFN_SWIGLU_OAI_MOE ,LLM_FFN_PAR ,il ); 553cb (cur ,"ffn_out" ,il ); 554 }else { 555// routed experts (swigluoai MoE) 556ggml_tensor * moe_out = build_moe_ffn (cur , 557model .layers [il ].ffn_gate_inp , 558model .layers [il ].ffn_up_exps , 559model .layers [il ].ffn_gate_exps , 560model .layers [il ].ffn_down_exps , 561model .layers [il ].ffn_exp_probs_b , 562n_expert ,n_expert_used , 563LLM_FFN_SWIGLU_OAI_MOE ,hparams .expert_weights_norm , 564hparams .expert_weights_scale , 565 (llama_expert_gating_func_type )hparams .expert_gating_func , 566il ); 567cb (moe_out ,"ffn_moe_out" ,il ); 568 569// shared expert (swigluoai) 570ggml_tensor * ffn_shexp = build_ffn (cur , 571model .layers [il ].ffn_up_shexp ,NULL ,NULL , 572model .layers [il ].ffn_gate_shexp ,NULL ,NULL , 573model .layers [il ].ffn_down_shexp ,NULL ,NULL , 574NULL , 575LLM_FFN_SWIGLU_OAI_MOE ,LLM_FFN_PAR ,il ); 576cb (ffn_shexp ,"ffn_shexp" ,il ); 577 578cur = ggml_add (ctx0 ,moe_out ,ffn_shexp ); 579cb (cur ,"ffn_out" ,il ); 580 } 581 582cur = ggml_add (ctx0 ,cur ,ffn_inp ); 583 584cur = build_cvec (cur ,il ); 585cb (cur ,"l_out" ,il ); 586 587// input for next layer 588inpL = cur ; 589 } 590 591cur = inpL ; 592 593cur = build_norm (cur ,model .output_norm ,NULL ,LLM_NORM_RMS ,-1 ); 594cb (cur ,"result_norm" ,-1 ); 595res -> t_embd = cur ; 596 597// lm_head 598cur = build_lora_mm (model .output ,cur ,model .output_s ); 599cb (cur ,"result_output" ,-1 ); 600res -> t_logits = cur ; 601 602ggml_build_forward_expand (gf ,cur ); 603}