char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
98a01afd9
char/aliceai-t5-moe
1#include "llama-graph.h" 2 3#include "llama-impl.h" 4#include "llama-model.h" 5#include "llama-batch.h" 6#include "llama-cparams.h" 7#include "llama-sampler.h" 8 9#include "llama-kv-cache.h" 10#include "llama-kv-cache-iswa.h" 11#include "llama-kv-cache-dsa.h" 12#include "llama-kv-cache-dsa-iswa.h" 13#include "llama-kv-cache-msa.h" 14#include "llama-kv-cache-dsv4.h" 15#include "llama-memory-hybrid.h" 16#include "llama-memory-hybrid-iswa.h" 17#include "llama-memory-recurrent.h" 18 19#include <cassert> 20#include <cmath> 21#include <cstring> 22#include <numeric> 23#include <sstream> 24#include <string> 25#include <unordered_set> 26 27// dedup helpers 28 29static ggml_tensor * build_attn_inp_kq_mask ( 30ggml_context * ctx , 31const llama_kv_cache_context * mctx , 32const llama_ubatch & ubatch , 33const llama_cparams & cparams ) { 34const auto n_kv = mctx -> get_n_kv (); 35const auto n_tokens = ubatch .n_tokens ; 36const auto n_stream = cparams .kv_unified ?1 :ubatch .n_seqs_unq ; 37 38// flash attention requires an f16 mask 39const auto type = cparams .flash_attn ?GGML_TYPE_F16 :GGML_TYPE_F32 ; 40 41ggml_tensor * res = ggml_new_tensor_4d (ctx ,type ,n_kv ,n_tokens /n_stream ,1 ,n_stream ); 42ggml_set_input (res ); 43ggml_set_name (res ,"attn_inp_kq_mask" ); 44 45return res ; 46} 47 48static bool can_reuse_kq_mask ( 49ggml_tensor * kq_mask , 50const llama_kv_cache_context * mctx , 51const llama_ubatch & ubatch , 52const llama_cparams & cparams ) { 53const auto n_kv = mctx -> get_n_kv (); 54const auto n_tokens = ubatch .n_tokens ; 55const auto n_stream = cparams .kv_unified ?1 :ubatch .n_seqs_unq ; 56 57bool res = true; 58 59res &= (kq_mask -> ne [0 ]== n_kv ); 60res &= (kq_mask -> ne [1 ]== n_tokens /n_stream ); 61res &= (kq_mask -> ne [2 ]== 1 ); 62res &= (kq_mask -> ne [3 ]== n_stream ); 63 64return res ; 65} 66 67// impl 68 69void llm_graph_input_embd::set_input (const llama_ubatch * ubatch ) { 70if (ubatch -> token ) { 71const int64_t n_tokens = ubatch -> n_tokens ; 72 73ggml_backend_tensor_set (tokens ,ubatch -> token ,0 ,n_tokens * ggml_element_size (tokens )); 74 } 75 76if (ubatch -> embd ) { 77GGML_ASSERT (n_embd == embd -> ne [0 ]); 78 79const int64_t n_tokens = ubatch -> n_tokens ; 80 81ggml_backend_tensor_set (embd ,ubatch -> embd ,0 ,n_tokens * n_embd * ggml_element_size (embd )); 82 } 83} 84 85bool llm_graph_input_embd::can_reuse (const llm_graph_params & params ) { 86bool res = true; 87 88res &= (!params .ubatch .token )|| (tokens && tokens -> ne [0 ]== params .ubatch .n_tokens ); 89res &= (!params .ubatch .embd )|| (embd && embd -> ne [1 ]== params .ubatch .n_tokens ); 90 91return res ; 92} 93 94void llm_graph_input_embd_h::set_input (const llama_ubatch * ubatch ) { 95const int64_t n_tokens = ubatch -> n_tokens ; 96 97if (ubatch -> token ) { 98ggml_backend_tensor_set (tokens ,ubatch -> token ,0 ,n_tokens * ggml_element_size (tokens )); 99 }else { 100// note: mtmd embedding input goes through here 101GGML_ASSERT (ubatch -> embd ); 102GGML_ASSERT (n_embd == embd -> ne [0 ]); 103 104ggml_backend_tensor_set (embd ,ubatch -> embd ,0 ,n_tokens * n_embd * ggml_element_size (h )); 105 } 106 107// TODO: extend llama_ubatch to differentiate between token embeddings and hidden states 108// for now, we assume that the hidden state is always provided as an embedding 109// ref: https://github.com/ggml-org/llama.cpp/pull/23643 110if (ubatch -> embd ) { 111GGML_ASSERT (n_embd == h -> ne [0 ]); 112 113ggml_backend_tensor_set (h ,ubatch -> embd ,0 ,n_tokens * n_embd * ggml_element_size (h )); 114 } 115} 116 117bool llm_graph_input_embd_h::can_reuse (const llm_graph_params & params ) { 118bool res = true; 119 120res &= (!params .ubatch .token )|| (tokens && tokens -> ne [0 ]== params .ubatch .n_tokens ); 121res &= (!params .ubatch .embd )|| (embd && embd -> ne [1 ]== params .ubatch .n_tokens ); 122res &= (!params .ubatch .embd )|| (h && h -> ne [1 ]== params .ubatch .n_tokens ); 123 124return res ; 125} 126 127void llm_graph_input_pos::set_input (const llama_ubatch * ubatch ) { 128if (ubatch -> pos && pos ) { 129const int64_t n_tokens = ubatch -> n_tokens ; 130 131if (ubatch -> token && n_pos_per_embd == 4 ) { 132// in case we're using M-RoPE with text tokens, convert the 1D positions to 4D 133// the 3 first dims are the same, and 4th dim is all 0 134 std::vector < llama_pos > pos_data (n_tokens * n_pos_per_embd ); 135// copy the first dimension 136for (int i = 0 ;i < n_tokens ;++ i ) { 137pos_data [i ]= ubatch -> pos [i ]; 138pos_data [n_tokens + i ]= ubatch -> pos [i ]; 139pos_data [2 * n_tokens + i ]= ubatch -> pos [i ]; 140pos_data [3 * n_tokens + i ]= 0 ;// 4th dim is 0 141 } 142ggml_backend_tensor_set (pos ,pos_data .data (),0 ,pos_data .size ()* ggml_element_size (pos )); 143 }else { 144ggml_backend_tensor_set (pos ,ubatch -> pos ,0 ,n_tokens * n_pos_per_embd * ggml_element_size (pos )); 145 } 146 } 147} 148 149bool llm_graph_input_pos::can_reuse (const llm_graph_params & params ) { 150bool res = true; 151 152res &=pos -> ne [0 ]== params .ubatch .n_tokens * n_pos_per_embd ; 153 154return res ; 155} 156 157void llm_graph_input_attn_temp::set_input (const llama_ubatch * ubatch ) { 158if (ubatch -> pos && attn_scale ) { 159const int64_t n_tokens = ubatch -> n_tokens ; 160 161GGML_ASSERT (f_attn_temp_scale != 0.0f ); 162GGML_ASSERT (n_attn_temp_floor_scale != 0 ); 163 164 std::vector < float > attn_scale_data (n_tokens ,0.0f ); 165for (int i = 0 ;i < n_tokens ;++ i ) { 166const float pos = ubatch -> pos [i ]; 167attn_scale_data [i ]= std::log ( 168 std::floor ((pos + f_attn_temp_offset ) /n_attn_temp_floor_scale )+ 1.0 169 )* f_attn_temp_scale + 1.0 ; 170 } 171 172ggml_backend_tensor_set (attn_scale ,attn_scale_data .data (),0 ,n_tokens * ggml_element_size (attn_scale )); 173 } 174} 175 176void llm_graph_input_pos_bucket::set_input (const llama_ubatch * ubatch ) { 177if (pos_bucket ) { 178const int64_t n_tokens = ubatch -> n_tokens ; 179 180GGML_ASSERT (ggml_backend_buffer_is_host (pos_bucket -> buffer )); 181GGML_ASSERT (!ubatch -> equal_seqs ());// TODO: use ubatch->n_seqs instead of failing 182 183int32_t * data = (int32_t * )pos_bucket -> data ; 184 185for (int j = 0 ;j < n_tokens ;++ j ) { 186for (int i = 0 ;i < n_tokens ;++ i ) { 187data [j * n_tokens + i ]= llama_relative_position_bucket (ubatch -> pos [i ],ubatch -> pos [j ],hparams .n_rel_attn_bkts , true); 188 } 189 } 190 } 191} 192 193void llm_graph_input_pos_bucket_kv::set_input (const llama_ubatch * ubatch ) { 194if (pos_bucket ) { 195mctx -> set_input_pos_bucket (pos_bucket ,ubatch ); 196 } 197} 198 199void llm_graph_input_out_ids::set_input (const llama_ubatch * ubatch ) { 200GGML_ASSERT (out_ids ); 201 202const int64_t n_tokens = ubatch -> n_tokens ; 203 204GGML_ASSERT (ggml_backend_buffer_is_host (out_ids -> buffer )); 205int32_t * data = (int32_t * )out_ids -> data ; 206 207if (n_outputs == n_tokens ) { 208for (int i = 0 ;i < n_tokens ;++ i ) { 209data [i ]= i ; 210 } 211 212return ; 213 } 214 215GGML_ASSERT (ubatch -> output ); 216 217int n_outputs = 0 ; 218 219for (int i = 0 ;i < n_tokens ;++ i ) { 220if (ubatch -> output [i ]) { 221data [n_outputs ++ ]= i ; 222 } 223 } 224} 225 226bool llm_graph_input_out_ids::can_reuse (const llm_graph_params & params ) { 227bool res = true; 228 229res &=n_outputs == params .n_outputs ; 230 231return res ; 232} 233 234void llm_graph_input_mean::set_input (const llama_ubatch * ubatch ) { 235if (cparams .embeddings && 236 (cparams .pooling_type == LLAMA_POOLING_TYPE_MEAN || 237cparams .pooling_type == LLAMA_POOLING_TYPE_RANK )) { 238 239const int64_t n_tokens = ubatch -> n_tokens ; 240const int64_t n_seq_tokens = ubatch -> n_seq_tokens ; 241const int64_t n_seqs_unq = ubatch -> n_seqs_unq ; 242 243GGML_ASSERT (mean ); 244GGML_ASSERT (ggml_backend_buffer_is_host (mean -> buffer )); 245 246float * data = (float * )mean -> data ; 247memset (mean -> data ,0 ,n_tokens * n_seqs_unq * ggml_element_size (mean )); 248 249 std::vector < uint64_t > sums (n_seqs_unq ,0 ); 250for (int i = 0 ;i < n_tokens ;i += n_seq_tokens ) { 251for (int s = 0 ;s < ubatch -> n_seq_id [i ];++ s ) { 252const llama_seq_id seq_id = ubatch -> seq_id [i ][s ]; 253const int32_t seq_idx = ubatch -> seq_idx [seq_id ]; 254 255sums [seq_idx ]+= ubatch -> n_seq_tokens ; 256 } 257 } 258 259 std::vector < float > div (n_seqs_unq ,0.0f ); 260for (int s = 0 ;s < n_seqs_unq ;++ s ) { 261const uint64_t sum = sums [s ]; 262if (sum > 0 ) { 263div [s ]= 1.0f /float (sum ); 264 } 265 } 266 267for (int i = 0 ;i < n_tokens ;i += n_seq_tokens ) { 268for (int s = 0 ;s < ubatch -> n_seq_id [i ];++ s ) { 269const llama_seq_id seq_id = ubatch -> seq_id [i ][s ]; 270const int32_t seq_idx = ubatch -> seq_idx [seq_id ]; 271 272for (int j = 0 ;j < n_seq_tokens ;++ j ) { 273data [seq_idx * n_tokens + i + j ]= div [seq_idx ]; 274 } 275 } 276 } 277 } 278} 279 280void llm_graph_input_cls::set_input (const llama_ubatch * ubatch ) { 281const int64_t n_tokens = ubatch -> n_tokens ; 282const int64_t n_seqs_unq = ubatch -> n_seqs_unq ; 283 284if (cparams .embeddings && ( 285cparams .pooling_type == LLAMA_POOLING_TYPE_CLS || 286cparams .pooling_type == LLAMA_POOLING_TYPE_RANK || 287cparams .pooling_type == LLAMA_POOLING_TYPE_LAST 288 )) { 289GGML_ASSERT (cls ); 290GGML_ASSERT (ggml_backend_buffer_is_host (cls -> buffer )); 291 292uint32_t * data = (uint32_t * )cls -> data ; 293memset (cls -> data ,0 ,n_seqs_unq * ggml_element_size (cls )); 294 295 std::vector < int > target_pos (n_seqs_unq ,-1 ); 296 std::vector < int > target_row (n_seqs_unq ,-1 ); 297 298const bool last = ( 299cparams .pooling_type == LLAMA_POOLING_TYPE_LAST || 300 (cparams .pooling_type == LLAMA_POOLING_TYPE_RANK && (arch == LLM_ARCH_QWEN3 || arch == LLM_ARCH_QWEN3VL ))// qwen3 reranking & embedding models use last token 301 ); 302 303for (int i = 0 ;i < n_tokens ;++ i ) { 304const llama_pos pos = ubatch -> pos [i ]; 305 306for (int s = 0 ;s < ubatch -> n_seq_id [i ];++ s ) { 307const llama_seq_id seq_id = ubatch -> seq_id [i ][s ]; 308const int32_t seq_idx = ubatch -> seq_idx [seq_id ]; 309 310if ( 311 (target_pos [seq_idx ]== -1 )|| 312 (last && pos >=target_pos [seq_idx ])|| 313 (!last && pos < target_pos [seq_idx ]) 314 ) { 315target_pos [seq_idx ]= pos ; 316target_row [seq_idx ]= i ; 317 } 318 } 319 } 320 321for (int s = 0 ;s < n_seqs_unq ;++ s ) { 322if (target_row [s ] >=0 ) { 323data [s ]= target_row [s ]; 324 } 325 } 326 } 327} 328 329void llm_graph_input_rs::set_input (const llama_ubatch * ubatch ) { 330GGML_UNUSED (ubatch ); 331 332const int64_t n_rs = mctx -> get_n_rs (); 333 334if (s_copy ) { 335GGML_ASSERT (ggml_backend_buffer_is_host (s_copy -> buffer )); 336int32_t * data = (int32_t * )s_copy -> data ; 337 338// assuming copy destinations ALWAYS happen ONLY on the cells between head and head+n 339for (uint32_t i = 0 ;i < n_rs ;++ i ) { 340data [i ]= mctx -> s_copy (i ); 341 } 342 } 343} 344 345bool llm_graph_input_rs::can_reuse (const llm_graph_params & params ) { 346const auto * mctx = static_cast < const llama_memory_recurrent_context *> (params .mctx ); 347 348this -> mctx = mctx ; 349 350bool res = true; 351 352res &=s_copy -> ne [0 ]== mctx -> get_n_rs (); 353 354res &=s_copy_main -> ne [0 ]== params .ubatch .n_seqs ; 355res &=s_copy_extra -> ne [0 ]== mctx -> get_n_rs ()- params .ubatch .n_seqs ; 356 357res &=head == mctx -> get_head (); 358res &=rs_z == mctx -> get_rs_z (); 359 360return res ; 361} 362 363void llm_graph_input_cross_embd::set_input (const llama_ubatch * ubatch ) { 364GGML_UNUSED (ubatch ); 365 366if (cross_embd && !cross -> v_embd .empty ()) { 367assert (cross_embd -> type == GGML_TYPE_F32 ); 368 369ggml_backend_tensor_set (cross_embd ,cross -> v_embd .data (),0 ,ggml_nbytes (cross_embd )); 370 } 371} 372 373bool llm_graph_input_cross_embd::can_reuse (const llm_graph_params & params ) { 374return cross && cross == params .cross && !cross -> v_embd .empty ()&& 375cross_embd -> ne [0 ]== cross -> n_embd && cross_embd -> ne [1 ]== cross -> n_enc ; 376} 377 378template < typename T > 379static void print_mask (const T * data ,int64_t n_tokens ,int64_t n_kv ,int64_t n_swa ,llama_swa_type swa_type ) { 380LLAMA_LOG_DEBUG ("%s: === Attention mask ===\n" ,__func__ ); 381const char * swa_type_str = "unknown" ; 382 383switch (swa_type ) { 384case LLAMA_SWA_TYPE_NONE :swa_type_str = "LLAMA_SWA_TYPE_NONE" ;break ; 385case LLAMA_SWA_TYPE_STANDARD :swa_type_str = "LLAMA_SWA_TYPE_STANDARD" ;break ; 386case LLAMA_SWA_TYPE_CHUNKED :swa_type_str = "LLAMA_SWA_TYPE_CHUNKED" ;break ; 387case LLAMA_SWA_TYPE_SYMMETRIC :swa_type_str = "LLAMA_SWA_TYPE_SYMMETRIC" ;break ; 388 }; 389 390LLAMA_LOG_DEBUG ("%s: n_swa : %d, n_kv: %d, swa_type: %s\n" ,__func__ , (int )n_swa , (int )n_kv ,swa_type_str ); 391LLAMA_LOG_DEBUG ("%s: '0' = can attend, '∞' = masked\n" ,__func__ ); 392LLAMA_LOG_DEBUG ("%s: Rows = query tokens, Columns = key/value tokens\n\n" ,__func__ ); 393 394LLAMA_LOG_DEBUG (" " ); 395for (int j = 0 ;j < std::min ((int64_t )20 ,n_kv );++ j ) { 396LLAMA_LOG_DEBUG ("%2d" ,j ); 397 } 398LLAMA_LOG_DEBUG ("\n" ); 399 400for (int i = 0 ;i < std::min ((int64_t )20 ,n_tokens );++ i ) { 401LLAMA_LOG_DEBUG (" %2d " ,i ); 402for (int j = 0 ;j < std::min ((int64_t )20 ,n_kv );++ j ) { 403float val = llama_cast < float > (data [i * n_kv + j ]); 404if (val == - INFINITY ) { 405LLAMA_LOG_DEBUG (" ∞" ); 406 }else { 407LLAMA_LOG_DEBUG (" 0" ); 408 } 409 } 410LLAMA_LOG_DEBUG ("\n" ); 411 } 412} 413 414void llm_graph_input_attn_no_cache::set_input (const llama_ubatch * ubatch ) { 415const int64_t n_kv = ubatch -> n_tokens ; 416const int64_t n_tokens = ubatch -> n_tokens ; 417 418const auto fill_mask = [& ](auto * data ,int64_t ne ,int n_swa ,llama_swa_type swa_type ) { 419using T = std::remove_reference_t < decltype(* data )> ; 420 std::fill (data ,data + ne ,llama_cast < T > (- INFINITY )); 421 422for (int i1 = 0 ;i1 < n_tokens ;++ i1 ) { 423const llama_seq_id s1 = ubatch -> seq_id [i1 ][0 ]; 424const llama_pos p1 = ubatch -> pos [i1 ]; 425 426const uint64_t idst = i1 * n_kv ; 427 428for (int i0 = 0 ;i0 < n_tokens ;++ i0 ) { 429const llama_seq_id s0 = ubatch -> seq_id [i0 ][0 ]; 430const llama_pos p0 = ubatch -> pos [i0 ]; 431 432// mask different sequences 433if (s0 != s1 ) { 434continue ; 435 } 436 437// mask future tokens 438if (cparams .causal_attn && p0 > p1 ) { 439continue ; 440 } 441 442// apply SWA if any 443if (llama_hparams::is_masked_swa (n_swa ,swa_type ,p0 ,p1 )) { 444continue ; 445 } 446 447data [idst + i0 ]= llama_cast < T > (hparams .use_alibi ?- std::abs (p0 - p1 ) :0.0f ); 448 } 449 } 450 451if (debug ) { 452print_mask (data ,n_tokens ,n_kv ,n_swa ,swa_type ); 453 } 454 }; 455 456GGML_ASSERT (self_kq_mask ); 457GGML_ASSERT (ggml_backend_buffer_is_host (self_kq_mask -> buffer )); 458if (self_kq_mask -> type == GGML_TYPE_F16 ) { 459fill_mask ((ggml_fp16_t * )self_kq_mask -> data ,ggml_nelements (self_kq_mask ),0 ,LLAMA_SWA_TYPE_NONE ); 460 }else { 461fill_mask ((float * )self_kq_mask -> data ,ggml_nelements (self_kq_mask ),0 ,LLAMA_SWA_TYPE_NONE ); 462 } 463 464if (hparams .swa_type != LLAMA_SWA_TYPE_NONE ) { 465GGML_ASSERT (self_kq_mask_swa ); 466GGML_ASSERT (ggml_backend_buffer_is_host (self_kq_mask_swa -> buffer )); 467if (self_kq_mask_swa -> type == GGML_TYPE_F16 ) { 468fill_mask ((ggml_fp16_t * )self_kq_mask_swa -> data ,ggml_nelements (self_kq_mask_swa ),hparams .n_swa ,hparams .swa_type ); 469 }else { 470fill_mask ((float * )self_kq_mask_swa -> data ,ggml_nelements (self_kq_mask_swa ),hparams .n_swa ,hparams .swa_type ); 471 } 472 } 473} 474 475void llm_graph_input_attn_kv::set_input (const llama_ubatch * ubatch ) { 476mctx -> set_input_k_idxs (self_k_idxs ,ubatch ); 477mctx -> set_input_v_idxs (self_v_idxs ,ubatch ); 478 479// the mask is left unallocated when the graph only stores K/V without attending 480// (e.g. DFlash's KV-injection pass) 481if (self_kq_mask && self_kq_mask -> buffer ) { 482mctx -> set_input_kq_mask (self_kq_mask ,ubatch ,cparams .causal_attn ); 483 } 484 485if (self_k_rot && self_k_rot -> buffer ) { 486mctx -> set_input_k_rot (self_k_rot ); 487 } 488 489if (self_v_rot && self_v_rot -> buffer ) { 490mctx -> set_input_v_rot (self_v_rot ); 491 } 492} 493 494bool llm_graph_input_attn_kv::can_reuse (const llm_graph_params & params ) { 495const auto * mctx = static_cast < const llama_kv_cache_context *> (params .mctx ); 496 497this -> mctx = mctx ; 498 499bool res = true; 500 501res &=self_k_idxs -> ne [0 ]== params .ubatch .n_tokens ; 502//res &= self_v_idxs->ne[0] == params.ubatch.n_tokens; // TODO: need to move this to the unified cache and check there 503 504res &=can_reuse_kq_mask (self_kq_mask ,mctx ,params .ubatch ,params .cparams ); 505 506return res ; 507} 508 509void llm_graph_input_attn_k::set_input (const llama_ubatch * ubatch ) { 510mctx -> set_input_k_idxs (self_k_idxs ,ubatch ); 511 512mctx -> set_input_kq_mask (self_kq_mask ,ubatch ,cparams .causal_attn ); 513} 514 515bool llm_graph_input_attn_k::can_reuse (const llm_graph_params & params ) { 516mctx = static_cast < const llama_kv_cache_context *> (params .mctx ); 517 518return can_reuse_impl (params ); 519} 520 521bool llm_graph_input_attn_k::can_reuse_impl (const llm_graph_params & params ) { 522bool res = true; 523 524res &=self_k_idxs -> ne [0 ]== params .ubatch .n_tokens ; 525 526res &=can_reuse_kq_mask (self_kq_mask ,mctx ,params .ubatch ,params .cparams ); 527 528return res ; 529} 530 531llm_graph_input_attn_kv_msa::llm_graph_input_attn_kv_msa ( 532const llama_hparams & hparams , 533const llama_cparams & cparams , 534const llama_kv_cache_msa_context * mctx ) : 535llm_graph_input_attn_kv (hparams ,cparams ,mctx -> get_base ()), 536mctx_msa (mctx ) { 537} 538 539void llm_graph_input_attn_kv_msa::set_input (const llama_ubatch * ubatch ) { 540 llm_graph_input_attn_kv::set_input (ubatch ); 541 542if (self_k_idxs_idx ) { 543mctx_msa -> get_idx ()-> set_input_k_idxs (self_k_idxs_idx ,ubatch ); 544 } 545} 546 547bool llm_graph_input_attn_kv_msa::can_reuse (const llm_graph_params & params ) { 548mctx_msa = static_cast < const llama_kv_cache_msa_context *> (params .mctx ); 549 550// the parent class operates on the base cache context 551this -> mctx = mctx_msa -> get_base (); 552 553bool res = true; 554 555res &=self_k_idxs -> ne [0 ]== params .ubatch .n_tokens ; 556if (self_k_idxs_idx ) { 557res &=self_k_idxs_idx -> ne [0 ]== params .ubatch .n_tokens ; 558 } 559 560res &=can_reuse_kq_mask (self_kq_mask ,this -> mctx ,params .ubatch ,params .cparams ); 561 562return res ; 563} 564 565void llm_graph_input_attn_k_dsa::set_input (const llama_ubatch * ubatch ) { 566mctx -> get_mla ()-> set_input_k_idxs (self_k_idxs_mla ,ubatch ); 567 568mctx -> get_mla ()-> set_input_kq_mask (self_kq_mask_mla ,ubatch ,cparams .causal_attn ); 569 570mctx -> get_lid ()-> set_input_k_idxs (self_k_idxs_lid ,ubatch ); 571 572mctx -> get_lid ()-> set_input_kq_mask (self_kq_mask_lid ,ubatch ,cparams .causal_attn ); 573 574// left unallocated when the indexer does not use the rotation 575if (self_k_rot_lid && self_k_rot_lid -> buffer ) { 576mctx -> get_lid ()-> set_input_k_rot (self_k_rot_lid ); 577 } 578} 579 580bool llm_graph_input_attn_k_dsa::can_reuse (const llm_graph_params & params ) { 581mctx = static_cast < const llama_kv_cache_dsa_context *> (params .mctx ); 582 583return can_reuse_impl (params ); 584} 585 586bool llm_graph_input_attn_k_dsa::can_reuse_impl (const llm_graph_params & params ) { 587bool res = true; 588 589res &=self_k_idxs_mla -> ne [0 ]== params .ubatch .n_tokens ; 590res &=self_k_idxs_lid -> ne [0 ]== params .ubatch .n_tokens ; 591 592res &=can_reuse_kq_mask (self_kq_mask_mla ,mctx -> get_mla (),params .ubatch ,params .cparams ); 593res &=can_reuse_kq_mask (self_kq_mask_lid ,mctx -> get_lid (),params .ubatch ,params .cparams ); 594 595return res ; 596} 597 598void llm_graph_input_attn_k_dsa_iswa::set_input (const llama_ubatch * ubatch ) { 599inp_dsa -> set_input (ubatch ); 600inp_swa -> set_input (ubatch ); 601} 602 603bool llm_graph_input_attn_k_dsa_iswa::can_reuse (const llm_graph_params & params ) { 604mctx = static_cast < const llama_kv_cache_dsa_iswa_context *> (params .mctx ); 605 606inp_dsa -> mctx = mctx -> get_dsa (); 607inp_swa -> mctx = mctx -> get_swa (); 608 609bool res = true; 610 611res &=inp_dsa -> can_reuse_impl (params ); 612res &=inp_swa -> can_reuse_impl (params ); 613 614return res ; 615} 616 617void llm_graph_input_attn_kv_iswa::set_input (const llama_ubatch * ubatch ) { 618// base tensors may not be allocated if there are no non-SWA attention layers 619if (self_k_idxs && self_k_idxs -> buffer ) { 620mctx -> get_base ()-> set_input_k_idxs (self_k_idxs ,ubatch ); 621if (self_v_idxs ) { 622mctx -> get_base ()-> set_input_v_idxs (self_v_idxs ,ubatch ); 623 } 624 } 625 626// the kq mask guards on its own buffer: shared cells leave idxs unbacked while the mask stays live 627if (self_kq_mask && self_kq_mask -> buffer ) { 628mctx -> get_base ()-> set_input_kq_mask (self_kq_mask ,ubatch ,cparams .causal_attn ); 629 } 630 631// swa tensors may not be allocated if there are no SWA attention layers 632if (self_k_idxs_swa && self_k_idxs_swa -> buffer ) { 633mctx -> get_swa ()-> set_input_k_idxs (self_k_idxs_swa ,ubatch ); 634if (self_v_idxs_swa ) { 635mctx -> get_swa ()-> set_input_v_idxs (self_v_idxs_swa ,ubatch ); 636 } 637 } 638 639if (self_kq_mask_swa && self_kq_mask_swa -> buffer ) { 640mctx -> get_swa ()-> set_input_kq_mask (self_kq_mask_swa ,ubatch ,cparams .causal_attn ); 641 } 642 643if (self_k_rot && self_k_rot -> buffer ) { 644mctx -> get_base ()-> set_input_k_rot (self_k_rot ); 645 } 646 647if (self_v_rot && self_v_rot -> buffer ) { 648mctx -> get_base ()-> set_input_v_rot (self_v_rot ); 649 } 650 651if (self_k_rot_swa && self_k_rot_swa -> buffer ) { 652mctx -> get_swa ()-> set_input_k_rot (self_k_rot_swa ); 653 } 654 655if (self_v_rot_swa && self_v_rot_swa -> buffer ) { 656mctx -> get_swa ()-> set_input_v_rot (self_v_rot_swa ); 657 } 658} 659 660bool llm_graph_input_attn_kv_iswa::can_reuse (const llm_graph_params & params ) { 661const auto * mctx = static_cast < const llama_kv_cache_iswa_context *> (params .mctx ); 662 663this -> mctx = mctx ; 664 665bool res = true; 666 667// base tensors may not be allocated if there are no non-SWA attention layers 668if (self_k_idxs && self_k_idxs -> buffer ) { 669res &=self_k_idxs -> ne [0 ]== params .ubatch .n_tokens ; 670//res &= self_v_idxs->ne[0] == params.ubatch.n_tokens; // TODO: need to move this to the unified cache and check there 671 } 672 673if (self_kq_mask && self_kq_mask -> buffer ) { 674res &=can_reuse_kq_mask (self_kq_mask ,mctx -> get_base (),params .ubatch ,params .cparams ); 675 } 676 677// swa tensors may not be allocated if there are no SWA attention layers 678if (self_k_idxs_swa && self_k_idxs_swa -> buffer ) { 679res &=self_k_idxs_swa -> ne [0 ]== params .ubatch .n_tokens ; 680//res &= self_v_idxs_swa->ne[0] == params.ubatch.n_tokens; // TODO: need to move this to the unified cache and check there 681 } 682 683if (self_kq_mask_swa && self_kq_mask_swa -> buffer ) { 684res &=can_reuse_kq_mask (self_kq_mask_swa ,mctx -> get_swa (),params .ubatch ,params .cparams ); 685 } 686 687return res ; 688} 689 690void llm_graph_input_attn_k_iswa::set_input (const llama_ubatch * ubatch ) { 691// base tensors may not be allocated if there are no non-SWA attention layers 692if (self_k_idxs && self_k_idxs -> buffer ) { 693mctx -> get_base ()-> set_input_k_idxs (self_k_idxs ,ubatch ); 694 } 695 696// the kq mask guards on its own buffer: shared cells leave idxs unbacked while the mask stays live 697if (self_kq_mask && self_kq_mask -> buffer ) { 698mctx -> get_base ()-> set_input_kq_mask (self_kq_mask ,ubatch ,cparams .causal_attn ); 699 } 700 701// swa tensors may not be allocated if there are no SWA attention layers 702if (self_k_idxs_swa && self_k_idxs_swa -> buffer ) { 703mctx -> get_swa ()-> set_input_k_idxs (self_k_idxs_swa ,ubatch ); 704 } 705 706if (self_kq_mask_swa && self_kq_mask_swa -> buffer ) { 707mctx -> get_swa ()-> set_input_kq_mask (self_kq_mask_swa ,ubatch ,cparams .causal_attn ); 708 } 709 710if (self_k_rot && self_k_rot -> buffer ) { 711mctx -> get_base ()-> set_input_k_rot (self_k_rot ); 712 } 713 714if (self_k_rot_swa && self_k_rot_swa -> buffer ) { 715mctx -> get_swa ()-> set_input_k_rot (self_k_rot_swa ); 716 } 717} 718 719bool llm_graph_input_attn_k_iswa::can_reuse (const llm_graph_params & params ) { 720const auto * mctx = static_cast < const llama_kv_cache_iswa_context *> (params .mctx ); 721 722this -> mctx = mctx ; 723 724bool res = true; 725 726// base tensors may not be allocated if there are no non-SWA attention layers 727if (self_k_idxs && self_k_idxs -> buffer ) { 728res &=self_k_idxs -> ne [0 ]== params .ubatch .n_tokens ; 729 } 730 731if (self_kq_mask && self_kq_mask -> buffer ) { 732res &=can_reuse_kq_mask (self_kq_mask ,mctx -> get_base (),params .ubatch ,params .cparams ); 733 } 734 735// swa tensors may not be allocated if there are no SWA attention layers 736if (self_k_idxs_swa && self_k_idxs_swa -> buffer ) { 737res &=self_k_idxs_swa -> ne [0 ]== params .ubatch .n_tokens ; 738 } 739 740if (self_kq_mask_swa && self_kq_mask_swa -> buffer ) { 741res &=can_reuse_kq_mask (self_kq_mask_swa ,mctx -> get_swa (),params .ubatch ,params .cparams ); 742 } 743 744return res ; 745} 746 747static void dsv4_set_i64 (ggml_tensor * dst ,const std::vector < int64_t > & src ) { 748if (!dst || !dst -> buffer ) { 749return ; 750 } 751 752GGML_ASSERT (dst -> ne [0 ]== (int64_t )src .size ()); 753ggml_backend_tensor_set (dst ,src .data (),0 ,src .size ()* ggml_element_size (dst )); 754} 755 756static void dsv4_set_i32 (ggml_tensor * dst ,const std::vector < int32_t > & src ) { 757if (!dst || !dst -> buffer ) { 758return ; 759 } 760 761GGML_ASSERT (dst -> ne [0 ]== (int64_t )src .size ()); 762ggml_backend_tensor_set (dst ,src .data (),0 ,src .size ()* ggml_element_size (dst )); 763} 764 765static void dsv4_set_kq_mask ( 766ggml_tensor * dst , 767const llama_kv_cache_dsv4_context::comp_plan & plan , 768uint32_t n_tokens , 769int64_t n_stream ) { 770if (!dst || !dst -> buffer ) { 771return ; 772 } 773 774GGML_ASSERT (dst -> type == GGML_TYPE_F32 || dst -> type == GGML_TYPE_F16 ); 775GGML_ASSERT (n_stream > 0 ); 776GGML_ASSERT (n_tokens %n_stream == 0 ); 777GGML_ASSERT (dst -> ne [0 ]== plan .n_kv ); 778GGML_ASSERT (dst -> ne [1 ]== (int64_t )n_tokens /n_stream ); 779GGML_ASSERT (dst -> ne [2 ]== 1 ); 780GGML_ASSERT (dst -> ne [3 ]== n_stream ); 781GGML_ASSERT ((int64_t )plan .n_visible .size ()== (int64_t )n_tokens ); 782GGML_ASSERT (ggml_backend_buffer_is_host (dst -> buffer )); 783 784if (dst -> type == GGML_TYPE_F32 ) { 785float * data = (float * )dst -> data ; 786 787for (int64_t i = 0 ;i < (int64_t )n_tokens ;++ i ) { 788const int32_t n_visible = plan .n_visible [i ]; 789 790for (int64_t j = 0 ;j < dst -> ne [0 ];++ j ) { 791data [i * dst -> ne [0 ]+ j ]= j < n_visible ?0.0f :- INFINITY ; 792 } 793 } 794 }else if (dst -> type == GGML_TYPE_F16 ) { 795ggml_fp16_t * data = (ggml_fp16_t * )dst -> data ; 796const ggml_fp16_t fp16_ninf = llama_cast < ggml_fp16_t > (- INFINITY ); 797const ggml_fp16_t fp16_zero = llama_cast < ggml_fp16_t > (0.0f ); 798 799for (int64_t i = 0 ;i < (int64_t )n_tokens ;++ i ) { 800const int32_t n_visible = plan .n_visible [i ]; 801 802for (int64_t j = 0 ;j < dst -> ne [0 ];++ j ) { 803data [i * dst -> ne [0 ]+ j ]= j < n_visible ?fp16_zero :fp16_ninf ; 804 } 805 } 806 } 807} 808 809static ggml_tensor * dsv4_build_raw_kq_mask ( 810ggml_context * ctx , 811const llama_kv_cache_dsv4_raw_context * mctx , 812const llama_ubatch & ubatch , 813const llama_cparams & cparams , 814int64_t n_stream ) { 815const auto n_kv = mctx -> get_n_kv (); 816const auto n_tokens = ubatch .n_tokens ; 817 818GGML_ASSERT (n_stream > 0 ); 819GGML_ASSERT (n_tokens %n_stream == 0 ); 820 821const auto type = cparams .flash_attn ?GGML_TYPE_F16 :GGML_TYPE_F32 ; 822 823ggml_tensor * res = ggml_new_tensor_4d (ctx ,type ,n_kv ,n_tokens /n_stream ,1 ,n_stream ); 824ggml_set_input (res ); 825ggml_set_name (res ,"attn_inp_kq_mask" ); 826 827return res ; 828} 829 830static bool dsv4_can_reuse_raw_kq_mask ( 831ggml_tensor * kq_mask , 832const llama_kv_cache_dsv4_raw_context * mctx , 833const llama_ubatch & ubatch , 834int64_t n_stream ) { 835const auto n_kv = mctx -> get_n_kv (); 836const auto n_tokens = ubatch .n_tokens ; 837 838GGML_ASSERT (n_stream > 0 ); 839 840bool res = true; 841 842res &= (kq_mask -> ne [0 ]== n_kv ); 843res &= (kq_mask -> ne [1 ]== n_tokens /n_stream ); 844res &= (kq_mask -> ne [2 ]== 1 ); 845res &= (kq_mask -> ne [3 ]== n_stream ); 846 847return res ; 848} 849 850static std::string dsv4_plan_positions (const std::vector < int32_t > & values ) { 851 std::ostringstream ss ; 852ss <<"[" ; 853for (size_t i = 0 ;i < values .size ();++ i ) { 854if (i > 0 ) { 855ss <<", " ; 856 } 857ss <<values [i ]; 858 } 859ss <<"]" ; 860return ss .str (); 861} 862 863static bool dsv4_compress_debug () { 864static const bool debug = []() { 865const char * env = getenv ("LLAMA_DSV4_COMPRESS_DEBUG" ); 866return env && atoi (env )> 0 ; 867 }(); 868 869return debug ; 870} 871 872static void dsv4_set_comp_inputs ( 873const llm_graph_input_dsv4::comp_input & inp , 874const llama_kv_cache_dsv4_context::comp_plan & plan , 875const char * name , 876bool debug , 877uint32_t n_tokens , 878int64_t n_stream ) { 879dsv4_set_i32 (inp .state_pos ,plan .state_pos ); 880dsv4_set_i32 (inp .state_persist_src_idxs ,plan .state_persist_src_idxs ); 881dsv4_set_i32 (inp .state_persist_dst_idxs ,plan .state_persist_dst_idxs ); 882dsv4_set_i32 (inp .state_restore_src_idxs ,plan .state_restore_src_idxs ); 883dsv4_set_i32 (inp .state_restore_dst_idxs ,plan .state_restore_dst_idxs ); 884dsv4_set_i32 (inp .state_snapshot_src_idxs ,plan .state_snapshot_src_idxs ); 885dsv4_set_i32 (inp .state_snapshot_dst_idxs ,plan .state_snapshot_dst_idxs ); 886dsv4_set_i32 (inp .state_read_idxs ,plan .state_read_idxs ); 887dsv4_set_i64 (inp .state_write_idxs ,plan .state_write_idxs ); 888dsv4_set_i32 (inp .state_write_pos ,plan .state_write_pos ); 889dsv4_set_kq_mask (inp .kq_mask ,plan ,n_tokens ,n_stream ); 890 891if (debug || dsv4_compress_debug ()) { 892LLAMA_LOG_INFO ("%s: %s n_tokens=%u, n_stream=%d, state_persist_dst=%s, state_write_pos=%s\n" , 893__func__ ,name ,n_tokens , (int )n_stream , 894dsv4_plan_positions (plan .state_persist_dst_idxs ).c_str (), 895dsv4_plan_positions (plan .state_write_pos ).c_str ()); 896 } 897} 898 899static bool dsv4_can_reuse_tensor_1d (ggml_tensor * t ,int64_t ne0 ) { 900return (t == nullptr && ne0 == 0 )|| (t != nullptr && t -> ne [0 ]== ne0 ); 901} 902 903static bool dsv4_can_reuse_kq_mask ( 904ggml_tensor * t , 905const llama_kv_cache_dsv4_context::comp_plan & plan , 906uint32_t n_tokens , 907int64_t n_stream ) { 908if (plan .n_kv == 0 ) { 909return t == nullptr ; 910 } 911 912GGML_ASSERT (n_stream > 0 ); 913 914return t != nullptr && 915t -> ne [0 ]== plan .n_kv && 916t -> ne [1 ]== (int64_t )n_tokens /n_stream && 917t -> ne [2 ]== 1 && 918t -> ne [3 ]== n_stream ; 919} 920 921static bool dsv4_can_reuse_comp_input ( 922const llm_graph_input_dsv4::comp_input & inp , 923const llama_kv_cache_dsv4_context::comp_plan & plan , 924uint32_t n_tokens , 925int64_t n_stream ) { 926bool res = true; 927res &=dsv4_can_reuse_tensor_1d (inp .state_pos ,plan .state_pos .size ()); 928res &=dsv4_can_reuse_tensor_1d (inp .state_persist_src_idxs ,plan .state_persist_src_idxs .size ()); 929res &=dsv4_can_reuse_tensor_1d (inp .state_persist_dst_idxs ,plan .state_persist_dst_idxs .size ()); 930res &=dsv4_can_reuse_tensor_1d (inp .state_restore_src_idxs ,plan .state_restore_src_idxs .size ()); 931res &=dsv4_can_reuse_tensor_1d (inp .state_restore_dst_idxs ,plan .state_restore_dst_idxs .size ()); 932res &=dsv4_can_reuse_tensor_1d (inp .state_snapshot_src_idxs ,plan .state_snapshot_src_idxs .size ()); 933res &=dsv4_can_reuse_tensor_1d (inp .state_snapshot_dst_idxs ,plan .state_snapshot_dst_idxs .size ()); 934res &=dsv4_can_reuse_tensor_1d (inp .state_read_idxs ,plan .state_read_idxs .size ()); 935res &=dsv4_can_reuse_tensor_1d (inp .state_write_idxs ,plan .state_write_idxs .size ()); 936res &=dsv4_can_reuse_tensor_1d (inp .state_write_pos ,plan .state_write_pos .size ()); 937res &=dsv4_can_reuse_kq_mask (inp .kq_mask ,plan ,n_tokens ,n_stream ); 938 939return res ; 940} 941 942static ggml_tensor * dsv4_build_input_1d ( 943ggml_context * ctx , 944ggml_type type , 945int64_t ne0 , 946const std::string & name ) { 947if (ne0 == 0 ) { 948return nullptr ; 949 } 950 951ggml_tensor * res = ggml_new_tensor_1d (ctx ,type ,ne0 ); 952ggml_set_input (res ); 953ggml_set_name (res ,name .c_str ()); 954 955return res ; 956} 957 958static void dsv4_build_comp_inputs ( 959ggml_context * ctx , 960 llm_graph_input_dsv4::comp_input & inp , 961const llama_kv_cache_dsv4_context::comp_plan & plan , 962const char * name , 963const llama_cparams & cparams , 964int64_t n_stream ) { 965inp .state_pos = dsv4_build_input_1d (ctx ,GGML_TYPE_I32 ,plan .state_pos .size (), std::string ("dsv4_" )+ name + "_state_pos" ); 966inp .state_persist_src_idxs = dsv4_build_input_1d (ctx ,GGML_TYPE_I32 ,plan .state_persist_src_idxs .size (), std::string ("dsv4_" )+ name + "_state_persist_src_idxs" ); 967inp .state_persist_dst_idxs = dsv4_build_input_1d (ctx ,GGML_TYPE_I32 ,plan .state_persist_dst_idxs .size (), std::string ("dsv4_" )+ name + "_state_persist_dst_idxs" ); 968inp .state_restore_src_idxs = dsv4_build_input_1d (ctx ,GGML_TYPE_I32 ,plan .state_restore_src_idxs .size (), std::string ("dsv4_" )+ name + "_state_restore_src_idxs" ); 969inp .state_restore_dst_idxs = dsv4_build_input_1d (ctx ,GGML_TYPE_I32 ,plan .state_restore_dst_idxs .size (), std::string ("dsv4_" )+ name + "_state_restore_dst_idxs" ); 970inp .state_snapshot_src_idxs = dsv4_build_input_1d (ctx ,GGML_TYPE_I32 ,plan .state_snapshot_src_idxs .size (), std::string ("dsv4_" )+ name + "_state_snapshot_src_idxs" ); 971inp .state_snapshot_dst_idxs = dsv4_build_input_1d (ctx ,GGML_TYPE_I32 ,plan .state_snapshot_dst_idxs .size (), std::string ("dsv4_" )+ name + "_state_snapshot_dst_idxs" ); 972inp .state_read_idxs = dsv4_build_input_1d (ctx ,GGML_TYPE_I32 ,plan .state_read_idxs .size (), std::string ("dsv4_" )+ name + "_state_read_idxs" ); 973inp .state_write_idxs = dsv4_build_input_1d (ctx ,GGML_TYPE_I64 ,plan .state_write_idxs .size (), std::string ("dsv4_" )+ name + "_state_write_idxs" ); 974inp .state_write_pos = dsv4_build_input_1d (ctx ,GGML_TYPE_I32 ,plan .state_write_pos .size (), std::string ("dsv4_" )+ name + "_state_write_pos" ); 975 976if (plan .n_kv > 0 ) { 977const int64_t n_tokens = (int64_t )plan .n_visible .size (); 978 979GGML_ASSERT (n_stream > 0 ); 980GGML_ASSERT (n_tokens %n_stream == 0 ); 981 982inp .kq_mask = ggml_new_tensor_4d (ctx , (strcmp (name ,"lid" )!= 0 && cparams .flash_attn )|| (strcmp (name ,"lid" )== 0 && cparams .fused_lid ) ?GGML_TYPE_F16 :GGML_TYPE_F32 ,plan .n_kv ,n_tokens /n_stream ,1 ,n_stream ); 983ggml_set_input (inp .kq_mask ); 984ggml_set_name (inp .kq_mask , (std::string ("dsv4_" )+ name + "_kq_mask" ).c_str ()); 985 } 986} 987 988void llm_graph_input_dsv4_raw::set_input (const llama_ubatch * ubatch ) { 989if (self_k_idxs && self_k_idxs -> buffer ) { 990mctx -> set_input_k_idxs (self_k_idxs ); 991 } 992 993if (self_kq_mask && self_kq_mask -> buffer ) { 994mctx -> set_input_kq_mask (self_kq_mask ,ubatch ,cparams .causal_attn ); 995 } 996 997if (self_k_rot ) { 998mctx -> set_input_k_rot (self_k_rot ); 999 } 1000} 1001 1002void llm_graph_input_dsv4::set_input (const llama_ubatch * ubatch ) { 1003const auto & plan_csa = mctx -> get_csa_plan (* ubatch ); 1004const auto & plan_hca = mctx -> get_hca_plan (* ubatch ); 1005const auto & plan_lid = mctx -> get_lid_plan (* ubatch ); 1006const int64_t n_stream = plan_csa .n_stream ; 1007 1008inp_raw -> mctx = mctx -> get_raw (); 1009inp_raw -> set_input (ubatch ); 1010 1011dsv4_set_comp_inputs (inp_csa ,plan_csa ,"csa" ,debug > 0 ,ubatch -> n_tokens ,n_stream ); 1012dsv4_set_comp_inputs (inp_hca ,plan_hca ,"hca" ,debug > 0 ,ubatch -> n_tokens ,n_stream ); 1013dsv4_set_comp_inputs (inp_lid ,plan_lid ,"lid" ,debug > 0 ,ubatch -> n_tokens ,n_stream ); 1014 1015if (inp_csa .k_rot && inp_csa .k_rot -> buffer ) { 1016mctx -> get_csa ()-> set_input_k_rot (inp_csa .k_rot ); 1017 } 1018 1019if (inp_hca .k_rot && inp_hca .k_rot -> buffer ) { 1020mctx -> get_hca ()-> set_input_k_rot (inp_hca .k_rot ); 1021 } 1022 1023if (inp_lid .k_rot && inp_lid .k_rot -> buffer ) { 1024mctx -> get_lid ()-> set_input_k_rot (inp_lid .k_rot ); 1025 } 1026} 1027 1028bool llm_graph_input_dsv4::can_reuse (const llm_graph_params & params ) { 1029const auto * mctx = static_cast < const llama_kv_cache_dsv4_context *> (params .mctx ); 1030 1031this -> mctx = mctx ; 1032inp_raw -> mctx = mctx -> get_raw (); 1033 1034bool res = true; 1035 1036const auto & plan_csa = mctx -> get_csa_plan (params .ubatch ); 1037const auto & plan_hca = mctx -> get_hca_plan (params .ubatch ); 1038const auto & plan_lid = mctx -> get_lid_plan (params .ubatch ); 1039const int64_t n_stream = plan_csa .n_stream ; 1040 1041const auto * raw_ctx = mctx -> get_raw (); 1042inp_raw -> mctx = raw_ctx ; 1043 1044if (inp_raw -> self_k_idxs && inp_raw -> self_k_idxs -> buffer ) { 1045res &=inp_raw -> self_k_idxs -> ne [0 ]== raw_ctx -> get_n_write (); 1046 } 1047if (inp_raw -> self_kq_mask && inp_raw -> self_kq_mask -> buffer ) { 1048res &=dsv4_can_reuse_raw_kq_mask (inp_raw -> self_kq_mask ,raw_ctx ,params .ubatch ,n_stream ); 1049 } 1050 1051res &=dsv4_can_reuse_comp_input (inp_csa ,plan_csa ,params .ubatch .n_tokens ,n_stream ); 1052res &=dsv4_can_reuse_comp_input (inp_hca ,plan_hca ,params .ubatch .n_tokens ,n_stream ); 1053res &=dsv4_can_reuse_comp_input (inp_lid ,plan_lid ,params .ubatch .n_tokens ,n_stream ); 1054 1055return res ; 1056} 1057 1058void llm_graph_input_attn_cross::set_input (const llama_ubatch * ubatch ) { 1059GGML_ASSERT (cross_kq_mask ); 1060 1061const int64_t n_enc = cross_kq_mask -> ne [0 ]; 1062const int64_t n_tokens = ubatch -> n_tokens ; 1063 1064GGML_ASSERT (ggml_backend_buffer_is_host (cross_kq_mask -> buffer )); 1065GGML_ASSERT (!ubatch -> equal_seqs ());// TODO: use ubatch->n_seqs instead of failing 1066 1067const auto fill_mask = [& ](auto * data ) { 1068using T = std::remove_reference_t < decltype(* data )> ; 1069for (int i = 0 ;i < n_tokens ;++ i ) { 1070GGML_ASSERT (!cross -> seq_ids_enc .empty ()&& "llama_encode must be called first" ); 1071for (int j = 0 ;j < n_enc ;++ j ) { 1072float f = - INFINITY ; 1073 1074for (int s = 0 ;s < ubatch -> n_seq_id [i ];++ s ) { 1075const llama_seq_id seq_id = ubatch -> seq_id [i ][s ]; 1076 1077if (cross -> seq_ids_enc [j ].find (seq_id )!= cross -> seq_ids_enc [j ].end ()) { 1078f = 0.0f ; 1079 } 1080 } 1081 1082data [i * n_enc + j ]= llama_cast < T > (f ); 1083 } 1084 } 1085 }; 1086 1087if (cross_kq_mask -> type == GGML_TYPE_F16 ) { 1088fill_mask ((ggml_fp16_t * )cross_kq_mask -> data ); 1089 }else { 1090fill_mask ((float * )cross_kq_mask -> data ); 1091 } 1092} 1093 1094bool llm_graph_input_attn_cross::can_reuse (const llm_graph_params & params ) { 1095return cross && cross == params .cross && !cross -> v_embd .empty ()&& 1096cross_kq_mask -> ne [0 ]== cross -> n_enc && cross_kq_mask -> ne [1 ]== params .ubatch .n_tokens && 1097cross_kq_mask -> type == (params .cparams .flash_attn ?GGML_TYPE_F16 :GGML_TYPE_F32 ); 1098} 1099 1100void llm_graph_input_mem_hybrid::set_input (const llama_ubatch * ubatch ) { 1101mctx -> get_attn ()-> set_input_k_idxs (inp_attn -> self_k_idxs ,ubatch ); 1102mctx -> get_attn ()-> set_input_v_idxs (inp_attn -> self_v_idxs ,ubatch ); 1103 1104mctx -> get_attn ()-> set_input_kq_mask (inp_attn -> self_kq_mask ,ubatch ,cparams .causal_attn ); 1105 1106if (inp_attn -> self_k_rot ) { 1107mctx -> get_attn ()-> set_input_k_rot (inp_attn -> self_k_rot ); 1108 } 1109 1110if (inp_attn -> self_v_rot ) { 1111mctx -> get_attn ()-> set_input_v_rot (inp_attn -> self_v_rot ); 1112 } 1113 1114const int64_t n_rs = mctx -> get_recr ()-> get_n_rs (); 1115 1116if (inp_rs -> s_copy ) { 1117GGML_ASSERT (ggml_backend_buffer_is_host (inp_rs -> s_copy -> buffer )); 1118int32_t * data = (int32_t * )inp_rs -> s_copy -> data ; 1119 1120// assuming copy destinations ALWAYS happen ONLY on the cells between head and head+n 1121for (uint32_t i = 0 ;i < n_rs ;++ i ) { 1122data [i ]= mctx -> get_recr ()-> s_copy (i ); 1123 } 1124 } 1125} 1126 1127bool llm_graph_input_mem_hybrid::can_reuse (const llm_graph_params & params ) { 1128const auto * mctx = static_cast < const llama_memory_hybrid_context *> (params .mctx ); 1129 1130this -> mctx = mctx ; 1131 1132bool res = true; 1133 1134res &=inp_attn -> self_k_idxs -> ne [0 ]== params .ubatch .n_tokens ; 1135//res &= inp_attn->self_v_idxs->ne[0] == params.ubatch.n_tokens; // TODO: need to move this to the unified cache and check there 1136 1137res &=can_reuse_kq_mask (inp_attn -> self_kq_mask ,mctx -> get_attn (),params .ubatch ,params .cparams ); 1138 1139res &=inp_rs -> s_copy -> ne [0 ]== mctx -> get_recr ()-> get_n_rs (); 1140 1141res &=inp_rs -> s_copy_main -> ne [0 ]== params .ubatch .n_seqs ; 1142res &=inp_rs -> s_copy_extra -> ne [0 ]== mctx -> get_recr ()-> get_n_rs ()- params .ubatch .n_seqs ; 1143 1144res &=inp_rs -> head == mctx -> get_recr ()-> get_head (); 1145res &=inp_rs -> rs_z == mctx -> get_recr ()-> get_rs_z (); 1146 1147return res ; 1148} 1149 1150// TODO: Hybrid input classes are a bit redundant. 1151// Instead of creating a hybrid input, the graph can simply create 2 separate inputs. 1152// Refactoring is required in the future. 1153void llm_graph_input_mem_hybrid_k::set_input (const llama_ubatch * ubatch ) { 1154mctx -> get_attn ()-> set_input_k_idxs (inp_attn -> self_k_idxs ,ubatch ); 1155 1156mctx -> get_attn ()-> set_input_kq_mask (inp_attn -> self_kq_mask ,ubatch ,cparams .causal_attn ); 1157 1158const int64_t n_rs = mctx -> get_recr ()-> get_n_rs (); 1159 1160if (inp_rs -> s_copy ) { 1161GGML_ASSERT (ggml_backend_buffer_is_host (inp_rs -> s_copy -> buffer )); 1162int32_t * data = (int32_t * )inp_rs -> s_copy -> data ; 1163 1164// assuming copy destinations ALWAYS happen ONLY on the cells between head and head+n 1165for (uint32_t i = 0 ;i < n_rs ;++ i ) { 1166data [i ]= mctx -> get_recr ()-> s_copy (i ); 1167 } 1168 } 1169} 1170 1171bool llm_graph_input_mem_hybrid_k::can_reuse (const llm_graph_params & params ) { 1172const auto * mctx = static_cast < const llama_memory_hybrid_context *> (params .mctx ); 1173 1174this -> mctx = mctx ; 1175 1176bool res = true; 1177 1178res &=inp_attn -> self_k_idxs -> ne [0 ]== params .ubatch .n_tokens ; 1179 1180res &=can_reuse_kq_mask (inp_attn -> self_kq_mask ,mctx -> get_attn (),params .ubatch ,params .cparams ); 1181 1182res &=inp_rs -> s_copy -> ne [0 ]== mctx -> get_recr ()-> get_n_rs (); 1183 1184res &=inp_rs -> s_copy_main -> ne [0 ]== params .ubatch .n_seqs ; 1185res &=inp_rs -> s_copy_extra -> ne [0 ]== mctx -> get_recr ()-> get_n_rs ()- params .ubatch .n_seqs ; 1186 1187res &=inp_rs -> head == mctx -> get_recr ()-> get_head (); 1188res &=inp_rs -> rs_z == mctx -> get_recr ()-> get_rs_z (); 1189 1190return res ; 1191} 1192 1193void llm_graph_input_mem_hybrid_iswa::set_input (const llama_ubatch * ubatch ) { 1194const auto * attn_ctx = mctx -> get_attn (); 1195 1196// base tensors may not be allocated if there are no non-SWA attention layers 1197if (inp_attn -> self_k_idxs && inp_attn -> self_k_idxs -> buffer ) { 1198attn_ctx -> get_base ()-> set_input_k_idxs (inp_attn -> self_k_idxs ,ubatch ); 1199attn_ctx -> get_base ()-> set_input_v_idxs (inp_attn -> self_v_idxs ,ubatch ); 1200 } 1201 1202if (inp_attn -> self_kq_mask && inp_attn -> self_kq_mask -> buffer ) { 1203attn_ctx -> get_base ()-> set_input_kq_mask (inp_attn -> self_kq_mask ,ubatch ,cparams .causal_attn ); 1204 } 1205 1206// swa tensors may not be allocated if there are no SWA attention layers 1207if (inp_attn -> self_k_idxs_swa && inp_attn -> self_k_idxs_swa -> buffer ) { 1208attn_ctx -> get_swa ()-> set_input_k_idxs (inp_attn -> self_k_idxs_swa ,ubatch ); 1209attn_ctx -> get_swa ()-> set_input_v_idxs (inp_attn -> self_v_idxs_swa ,ubatch ); 1210 } 1211 1212if (inp_attn -> self_kq_mask_swa && inp_attn -> self_kq_mask_swa -> buffer ) { 1213attn_ctx -> get_swa ()-> set_input_kq_mask (inp_attn -> self_kq_mask_swa ,ubatch ,cparams .causal_attn ); 1214 } 1215 1216if (inp_attn -> self_k_rot ) { 1217attn_ctx -> get_base ()-> set_input_k_rot (inp_attn -> self_k_rot ); 1218 } 1219 1220if (inp_attn -> self_v_rot ) { 1221attn_ctx -> get_base ()-> set_input_v_rot (inp_attn -> self_v_rot ); 1222 } 1223 1224if (inp_attn -> self_k_rot_swa ) { 1225attn_ctx -> get_swa ()-> set_input_k_rot (inp_attn -> self_k_rot_swa ); 1226 } 1227 1228if (inp_attn -> self_v_rot_swa ) { 1229attn_ctx -> get_swa ()-> set_input_v_rot (inp_attn -> self_v_rot_swa ); 1230 } 1231 1232const int64_t n_rs = mctx -> get_recr ()-> get_n_rs (); 1233 1234if (inp_rs -> s_copy ) { 1235GGML_ASSERT (ggml_backend_buffer_is_host (inp_rs -> s_copy -> buffer )); 1236int32_t * data = (int32_t * )inp_rs -> s_copy -> data ; 1237 1238// assuming copy destinations ALWAYS happen ONLY on the cells between head and head+n 1239for (uint32_t i = 0 ;i < n_rs ;++ i ) { 1240data [i ]= mctx -> get_recr ()-> s_copy (i ); 1241 } 1242 } 1243} 1244 1245bool llm_graph_input_mem_hybrid_iswa::can_reuse (const llm_graph_params & params ) { 1246const auto * mctx = static_cast < const llama_memory_hybrid_iswa_context *> (params .mctx ); 1247 1248this -> mctx = mctx ; 1249 1250bool res = true; 1251 1252const auto * attn_ctx = mctx -> get_attn (); 1253 1254// base tensors may not be allocated if there are no non-SWA attention layers 1255if (inp_attn -> self_k_idxs && inp_attn -> self_k_idxs -> buffer ) { 1256res &=inp_attn -> self_k_idxs -> ne [0 ]== params .ubatch .n_tokens ; 1257//res &= inp_attn->self_v_idxs->ne[0] == params.ubatch.n_tokens; // TODO: need to move this to the unified cache and check there 1258 } 1259 1260res &=can_reuse_kq_mask (inp_attn -> self_kq_mask ,attn_ctx -> get_base (),params .ubatch ,params .cparams ); 1261 1262// swa tensors may not be allocated if there are no SWA attention layers 1263if (inp_attn -> self_k_idxs_swa && inp_attn -> self_k_idxs_swa -> buffer ) { 1264res &=inp_attn -> self_k_idxs_swa -> ne [0 ]== params .ubatch .n_tokens ; 1265//res &= inp_attn->self_v_idxs_swa->ne[0] == params.ubatch.n_tokens; // TODO: need to move this to the unified cache and check there 1266 } 1267 1268res &=can_reuse_kq_mask (inp_attn -> self_kq_mask_swa ,attn_ctx -> get_swa (),params .ubatch ,params .cparams ); 1269 1270res &=inp_rs -> s_copy -> ne [0 ]== mctx -> get_recr ()-> get_n_rs (); 1271 1272res &=inp_rs -> s_copy_main -> ne [0 ]== params .ubatch .n_seqs ; 1273res &=inp_rs -> s_copy_extra -> ne [0 ]== mctx -> get_recr ()-> get_n_rs ()- params .ubatch .n_seqs ; 1274 1275res &=inp_rs -> head == mctx -> get_recr ()-> get_head (); 1276res &=inp_rs -> rs_z == mctx -> get_recr ()-> get_rs_z (); 1277 1278return res ; 1279} 1280 1281void llm_graph_input_sampling::set_input (const llama_ubatch * ubatch ) { 1282// set the inputs only for the active samplers in the current ubatch 1283 std::unordered_set < llama_seq_id > active_samplers ; 1284for (uint32_t i = 0 ;i < ubatch -> n_tokens ;i ++ ) { 1285if (ubatch -> output [i ]) { 1286llama_seq_id seq_id = ubatch -> seq_id [i ][0 ]; 1287active_samplers .insert (seq_id ); 1288 } 1289 } 1290 1291for (auto seq_id :active_samplers ) { 1292if (samplers .find (seq_id )== samplers .end ()) { 1293continue ; 1294 } 1295 1296auto & sampler = samplers [seq_id ]; 1297 1298if (sampler -> iface -> backend_set_input ) { 1299sampler -> iface -> backend_set_input (sampler ); 1300 } 1301 } 1302} 1303 1304bool llm_graph_input_sampling::can_reuse (const llm_graph_params & params ) { 1305if (samplers .size ()!= params .samplers .size ()) { 1306return false; 1307 } 1308 1309for (const auto & [seq_id ,sampler ] :params .samplers ) { 1310if (samplers [seq_id ]!= sampler ) { 1311return false; 1312 } 1313 } 1314 1315return true; 1316} 1317 1318// 1319// llm_graph_result 1320// 1321 1322llm_graph_result::llm_graph_result (int64_t max_nodes ) :max_nodes (max_nodes ) { 1323reset (); 1324 1325const char * LLAMA_GRAPH_RESULT_DEBUG = getenv ("LLAMA_GRAPH_RESULT_DEBUG" ); 1326debug = LLAMA_GRAPH_RESULT_DEBUG ?atoi (LLAMA_GRAPH_RESULT_DEBUG ) :0 ; 1327} 1328 1329int64_t llm_graph_result::get_max_nodes ()const { 1330return max_nodes ; 1331} 1332 1333void llm_graph_result::reset () { 1334t_inp_tokens = nullptr ; 1335t_inp_embd = nullptr ; 1336t_logits = nullptr ; 1337t_embd = nullptr ; 1338t_embd_pooled = nullptr ; 1339t_h_nextn = nullptr ; 1340 1341t_layer_inp .resize (LLAMA_MAX_LAYERS + 1 ); 1342 std::fill (t_layer_inp .begin (),t_layer_inp .end (),nullptr ); 1343 1344t_sampled .clear (); 1345t_sampled_probs .clear (); 1346t_sampled_logits .clear (); 1347t_candidates .clear (); 1348 1349params = {}; 1350 1351inputs .clear (); 1352fused_nodes .clear (); 1353 1354buf_compute_meta .resize (ggml_tensor_overhead ()* max_nodes + ggml_graph_overhead_custom (max_nodes , false)); 1355 1356ggml_init_params params = { 1357/*.mem_size =*/ buf_compute_meta .size (), 1358/*.mem_buffer =*/ buf_compute_meta .data (), 1359/*.no_alloc =*/ true, 1360 }; 1361 1362ctx_compute .reset (ggml_init (params )); 1363 1364gf = ggml_new_graph_custom (ctx_compute .get (),max_nodes , false); 1365} 1366 1367void llm_graph_result::set_inputs (const llama_ubatch * ubatch ) { 1368for (auto & input :inputs ) { 1369input -> set_input (ubatch ); 1370 } 1371} 1372 1373void llm_graph_result::set_outputs (const llm_graph_params & params ) { 1374if (t_logits != nullptr ) { 1375ggml_set_output (t_logits ); 1376 } 1377if (t_embd != nullptr ) { 1378ggml_set_output (t_embd ); 1379 } 1380if (t_embd_pooled != nullptr ) { 1381ggml_set_output (t_embd_pooled ); 1382 } 1383if (t_h_nextn != nullptr ) { 1384ggml_set_output (t_h_nextn ); 1385 } 1386 { 1387const auto & embeddings_layer_inp = params .cparams .embeddings_layer_inp ; 1388for (size_t il = 0 ;il < embeddings_layer_inp .size ();++ il ) { 1389if (embeddings_layer_inp [il ]) { 1390GGML_ASSERT (t_layer_inp [il ]!= nullptr && "layer input tensor is null" ); 1391ggml_set_output (t_layer_inp [il ]); 1392 } 1393 } 1394 } 1395for (auto * tensor :t_sampled ) { 1396if (tensor != nullptr ) { 1397ggml_set_output (tensor ); 1398 } 1399 } 1400for (auto * tensor :t_sampled_probs ) { 1401if (tensor != nullptr ) { 1402ggml_set_output (tensor ); 1403 } 1404 } 1405for (auto * tensor :t_sampled_logits ) { 1406if (tensor != nullptr ) { 1407ggml_set_output (tensor ); 1408 } 1409 } 1410for (auto * tensor :t_candidates ) { 1411if (tensor != nullptr ) { 1412ggml_set_output (tensor ); 1413 } 1414 } 1415} 1416 1417bool llm_graph_result::can_reuse (const llm_graph_params & params ) { 1418if (!this -> params .allow_reuse (params )) { 1419if (debug > 1 ) { 1420LLAMA_LOG_DEBUG ("%s: cannot reuse graph due to incompatible graph parameters\n" ,__func__ ); 1421 } 1422 1423return false; 1424 } 1425 1426if (debug > 1 ) { 1427LLAMA_LOG_DEBUG ("%s: checking compatibility of %d inputs:\n" ,__func__ , (int )inputs .size ()); 1428 } 1429 1430bool res = true; 1431 1432for (auto & input :inputs ) { 1433const bool cur = input -> can_reuse (params ); 1434 1435if (debug > 1 ) { 1436LLAMA_LOG_DEBUG ("%s: can_reuse = %d\n" ,"placeholder" ,cur ); 1437 } 1438 1439res = res && cur ; 1440 } 1441 1442if (debug > 0 ) { 1443LLAMA_LOG_DEBUG ("%s: can reuse graph = %d\n" ,__func__ ,res ); 1444 } 1445 1446return res ; 1447} 1448 1449llm_graph_input_i * llm_graph_result::add_input (llm_graph_input_ptr input ) { 1450inputs .emplace_back (std::move (input )); 1451return inputs .back ().get (); 1452} 1453 1454void llm_graph_result::add_fused_node (llm_graph_fused_node result ) { 1455fused_nodes .push_back (result ); 1456} 1457 1458void llm_graph_result::set_params (const llm_graph_params & params ) { 1459this -> params = params ; 1460} 1461 1462// 1463// llm_graph_context 1464// 1465 1466llm_graph_context::llm_graph_context (const llm_graph_params & params ) : 1467arch (params .arch ), 1468hparams (params .hparams ), 1469cparams (params .cparams ), 1470ubatch (params .ubatch ), 1471n_embd (hparams .n_embd ), 1472n_layer (hparams .n_layer ()), 1473n_layer_nextn (hparams .n_layer_nextn ), 1474n_rot (hparams .n_rot ()), 1475n_ctx (cparams .n_ctx ), 1476n_head (hparams .n_head ()), 1477n_head_kv (hparams .n_head_kv ()), 1478n_embd_head_k (hparams .n_embd_head_k ()), 1479n_embd_k_gqa (hparams .n_embd_k_gqa ()), 1480n_embd_head_v (hparams .n_embd_head_v ()), 1481n_embd_v_gqa (hparams .n_embd_v_gqa ()), 1482n_expert (hparams .n_expert ), 1483n_expert_used (cparams .warmup ?hparams .n_expert :hparams .n_expert_used ()), 1484freq_base (cparams .rope_freq_base ), 1485freq_scale (cparams .rope_freq_scale ), 1486ext_factor (cparams .yarn_ext_factor ), 1487attn_factor (cparams .yarn_attn_factor ), 1488beta_fast (cparams .yarn_beta_fast ), 1489beta_slow (cparams .yarn_beta_slow ), 1490norm_eps (hparams .f_norm_eps ), 1491norm_rms_eps (hparams .f_norm_rms_eps ), 1492n_tokens (ubatch .n_tokens ), 1493n_outputs (params .n_outputs ), 1494n_ctx_orig (cparams .n_ctx_orig_yarn ), 1495pooling_type (cparams .pooling_type ), 1496rope_type (hparams .rope_type ), 1497sched (params .sched ), 1498backend_cpu (params .backend_cpu ), 1499cvec (params .cvec ), 1500loras (params .loras ), 1501mctx (params .mctx ), 1502cross (params .cross ), 1503samplers (params .samplers ), 1504cb_func (params .cb ), 1505res (params .res ), 1506ctx0 (res -> get_ctx ()), 1507gf (res -> get_gf ()) { 1508res -> set_params (params ); 1509 } 1510 1511void llm_graph_context::cb (ggml_tensor * cur ,const char * name ,int il )const { 1512if (cb_func ) { 1513cb_func (ubatch ,cur ,name ,il ); 1514 } 1515} 1516 1517 1518 1519ggml_tensor * llm_graph_context::build_cvec ( 1520ggml_tensor * cur , 1521int il )const { 1522return cvec -> apply_to (ctx0 ,cur ,il ); 1523} 1524 1525ggml_tensor * llm_graph_context::build_lora_mm ( 1526ggml_tensor * w , 1527ggml_tensor * cur , 1528ggml_tensor * w_s )const { 1529ggml_tensor * res = ggml_mul_mat (ctx0 ,w ,cur ); 1530 1531if (w_s ) { 1532res = ggml_mul (ctx0 ,res ,w_s ); 1533 } 1534 1535for (const auto & lora :* loras ) { 1536llama_adapter_lora_weight * lw = lora .first -> get_weight (w ); 1537if (lw == nullptr ) { 1538continue ; 1539 } 1540 1541const float adapter_scale = lora .second ; 1542const float scale = lw -> get_scale (lora .first -> alpha ,adapter_scale ); 1543 1544ggml_tensor * ab_cur = ggml_mul_mat ( 1545ctx0 ,lw -> b , 1546ggml_mul_mat (ctx0 ,lw -> a ,cur ) 1547 ); 1548 1549ab_cur = ggml_scale (ctx0 ,ab_cur ,scale ); 1550res = ggml_add (ctx0 ,res ,ab_cur ); 1551 } 1552 1553return res ; 1554} 1555 1556ggml_tensor * llm_graph_context::build_lora_mm_id ( 1557ggml_tensor * w ,// ggml_tensor * as 1558ggml_tensor * cur ,// ggml_tensor * b 1559ggml_tensor * ids , 1560ggml_tensor * w_s )const { 1561ggml_tensor * res = ggml_mul_mat_id (ctx0 ,w ,cur ,ids ); 1562 1563if (w_s ) { 1564const int64_t n_expert = w_s -> ne [0 ]; 1565const int64_t n_tokens = cur -> ne [2 ]; 1566ggml_tensor * s = ggml_reshape_3d (ctx0 ,w_s ,1 ,n_expert ,1 ); 1567s = ggml_repeat_4d (ctx0 ,s ,1 ,n_expert ,n_tokens ,1 ); 1568s = ggml_get_rows (ctx0 ,s ,ids ); 1569res = ggml_mul (ctx0 ,res ,s ); 1570 } 1571for (const auto & lora :* loras ) { 1572llama_adapter_lora_weight * lw = lora .first -> get_weight (w ); 1573if (lw == nullptr ) { 1574continue ; 1575 } 1576 1577const float alpha = lora .first -> alpha ; 1578const float rank = (float )lw -> b -> ne [0 ]; 1579const float scale = alpha ?lora .second * alpha /rank :lora .second ; 1580 1581ggml_tensor * ab_cur = ggml_mul_mat_id ( 1582ctx0 ,lw -> b , 1583ggml_mul_mat_id (ctx0 ,lw -> a ,cur ,ids ), 1584ids 1585 ); 1586 1587ab_cur = ggml_scale (ctx0 ,ab_cur ,scale ); 1588res = ggml_add (ctx0 ,res ,ab_cur ); 1589 } 1590 1591return res ; 1592} 1593 1594ggml_tensor * llm_graph_context::build_norm ( 1595ggml_tensor * cur , 1596ggml_tensor * mw , 1597ggml_tensor * mb , 1598llm_norm_type type , 1599int il )const { 1600switch (type ) { 1601case LLM_NORM :cur = ggml_norm (ctx0 ,cur ,hparams .f_norm_eps );break ; 1602case LLM_NORM_RMS :cur = ggml_rms_norm (ctx0 ,cur ,hparams .f_norm_rms_eps );break ; 1603case LLM_NORM_GROUP : 1604 { 1605cur = ggml_reshape_3d (ctx0 ,cur ,cur -> ne [0 ],1 ,cur -> ne [1 ]); 1606cur = ggml_group_norm (ctx0 ,cur ,hparams .n_norm_groups ,hparams .f_norm_group_eps ); 1607cur = ggml_reshape_2d (ctx0 ,cur ,cur -> ne [0 ],cur -> ne [2 ]); 1608 }break ; 1609 } 1610 1611if (mw || mb ) { 1612cb (cur ,"norm" ,il ); 1613 } 1614 1615if (mw ) { 1616cur = ggml_mul (ctx0 ,cur ,mw ); 1617if (mb ) { 1618cb (cur ,"norm_w" ,il ); 1619 } 1620 } 1621 1622if (mb ) { 1623cur = ggml_add (ctx0 ,cur ,mb ); 1624 } 1625 1626return cur ; 1627} 1628 1629 1630llm_graph_qkv llm_graph_context::build_qkv ( 1631const llama_layer & layer , 1632ggml_tensor * cur , 1633int64_t n_embd_head , 1634int64_t n_head , 1635int64_t n_head_kv , 1636int il )const { 1637return build_qkv (layer ,cur , 1638n_embd_head ,n_head , 1639n_embd_head ,n_head_kv , 1640n_embd_head ,n_head_kv , 1641il ); 1642} 1643 1644llm_graph_qkv llm_graph_context::build_qkv ( 1645const llama_layer & layer , 1646ggml_tensor * cur , 1647int64_t n_embd_head_q , 1648int64_t n_head_q , 1649int64_t n_embd_head_k , 1650int64_t n_head_k , 1651int64_t n_embd_head_v , 1652int64_t n_head_v , 1653int il , 1654bool reshape )const { 1655const int64_t n_embd_q = n_embd_head_q * n_head_q ; 1656const int64_t n_embd_k = n_embd_head_k * n_head_k ; 1657 1658ggml_tensor * Qcur ,* Kcur ,* Vcur ; 1659 1660if (layer .wqkv ) { 1661// fused QKV path 1662ggml_tensor * qkv = build_lora_mm (layer .wqkv ,cur ,layer .wqkv_s ); 1663cb (qkv ,"wqkv" ,il ); 1664if (layer .wqkv_b ) { 1665qkv = ggml_add (ctx0 ,qkv ,layer .wqkv_b ); 1666cb (qkv ,"wqkv_b" ,il ); 1667 }else if (layer .wq_b && layer .wk_b && layer .wv_b ) { 1668// Fused weights may coexist with separate Q/K/V biases in legacy or custom GGUFs. 1669ggml_tensor * qkv_b = ggml_concat (ctx0 ,ggml_concat (ctx0 ,layer .wq_b ,layer .wk_b ,0 ),layer .wv_b ,0 ); 1670qkv = ggml_add (ctx0 ,qkv ,qkv_b ); 1671cb (qkv ,"wqkv_b" ,il ); 1672 } 1673if (reshape && hparams .f_clamp_kqv > 0.0f ) { 1674qkv = ggml_clamp (ctx0 ,qkv ,- hparams .f_clamp_kqv ,hparams .f_clamp_kqv ); 1675cb (qkv ,"wqkv_clamped" ,il ); 1676 } 1677if (reshape ) { 1678Qcur = ggml_view_3d (ctx0 ,qkv ,n_embd_head_q ,n_head_q ,n_tokens , 1679ggml_row_size (qkv -> type ,n_embd_head_q ),qkv -> nb [1 ],0 ); 1680Kcur = ggml_view_3d (ctx0 ,qkv ,n_embd_head_k ,n_head_k ,n_tokens , 1681ggml_row_size (qkv -> type ,n_embd_head_k ),qkv -> nb [1 ], 1682ggml_row_size (qkv -> type ,n_embd_q )); 1683Vcur = ggml_view_3d (ctx0 ,qkv ,n_embd_head_v ,n_head_v ,n_tokens , 1684ggml_row_size (qkv -> type ,n_embd_head_v ),qkv -> nb [1 ], 1685ggml_row_size (qkv -> type ,n_embd_q + n_embd_k )); 1686 }else { 1687Qcur = ggml_view_2d (ctx0 ,qkv ,n_embd_q ,n_tokens ,qkv -> nb [1 ],0 ); 1688Kcur = ggml_view_2d (ctx0 ,qkv ,n_embd_k ,n_tokens ,qkv -> nb [1 ], 1689ggml_row_size (qkv -> type ,n_embd_q )); 1690Vcur = ggml_view_2d (ctx0 ,qkv ,n_embd_head_v * n_head_v ,n_tokens ,qkv -> nb [1 ], 1691ggml_row_size (qkv -> type ,n_embd_q + n_embd_k )); 1692 } 1693if (!reshape ) { 1694Qcur = ggml_cont (ctx0 ,Qcur ); 1695Kcur = ggml_cont (ctx0 ,Kcur ); 1696Vcur = ggml_cont (ctx0 ,Vcur ); 1697 } 1698 }else { 1699// separate Q/K/V path 1700Qcur = build_lora_mm (layer .wq ,cur ,layer .wq_s ); 1701if (reshape ) { 1702cb (Qcur ,"Qcur" ,il ); 1703 } 1704if (layer .wq_b ) { 1705Qcur = ggml_add (ctx0 ,Qcur ,layer .wq_b ); 1706if (reshape ) { 1707cb (Qcur ,"Qcur" ,il ); 1708 } 1709 } 1710if (reshape && hparams .f_clamp_kqv > 0.0f ) { 1711Qcur = ggml_clamp (ctx0 ,Qcur ,- hparams .f_clamp_kqv ,hparams .f_clamp_kqv ); 1712cb (Qcur ,"Qcur_clamped" ,il ); 1713 } 1714Kcur = build_lora_mm (layer .wk ,cur ,layer .wk_s ); 1715if (reshape ) { 1716cb (Kcur ,"Kcur" ,il ); 1717 } 1718if (layer .wk_b ) { 1719Kcur = ggml_add (ctx0 ,Kcur ,layer .wk_b ); 1720if (reshape ) { 1721cb (Kcur ,"Kcur" ,il ); 1722 } 1723 } 1724if (reshape && hparams .f_clamp_kqv > 0.0f ) { 1725Kcur = ggml_clamp (ctx0 ,Kcur ,- hparams .f_clamp_kqv ,hparams .f_clamp_kqv ); 1726cb (Kcur ,"Kcur_clamped" ,il ); 1727 } 1728Vcur = build_lora_mm (layer .wv ,cur ,layer .wv_s ); 1729if (reshape ) { 1730cb (Vcur ,"Vcur" ,il ); 1731 } 1732if (layer .wv_b ) { 1733Vcur = ggml_add (ctx0 ,Vcur ,layer .wv_b ); 1734if (reshape ) { 1735cb (Vcur ,"Vcur" ,il ); 1736 } 1737 } 1738if (reshape && hparams .f_clamp_kqv > 0.0f ) { 1739Vcur = ggml_clamp (ctx0 ,Vcur ,- hparams .f_clamp_kqv ,hparams .f_clamp_kqv ); 1740cb (Vcur ,"Vcur_clamped" ,il ); 1741 } 1742if (reshape ) { 1743Qcur = ggml_reshape_3d (ctx0 ,Qcur ,n_embd_head_q ,n_head_q ,n_tokens ); 1744Kcur = ggml_reshape_3d (ctx0 ,Kcur ,n_embd_head_k ,n_head_k ,n_tokens ); 1745Vcur = ggml_reshape_3d (ctx0 ,Vcur ,n_embd_head_v ,n_head_v ,n_tokens ); 1746 } 1747 } 1748 1749if (reshape ) { 1750cb (Qcur ,"Qcur" ,il ); 1751cb (Kcur ,"Kcur" ,il ); 1752cb (Vcur ,"Vcur" ,il ); 1753 } 1754 1755return {Qcur ,Kcur ,Vcur }; 1756} 1757 1758 1759ggml_tensor * llm_graph_context::build_ffn ( 1760ggml_tensor * cur , 1761ggml_tensor * up , 1762ggml_tensor * up_b , 1763ggml_tensor * up_s , 1764ggml_tensor * gate , 1765ggml_tensor * gate_b , 1766ggml_tensor * gate_s , 1767ggml_tensor * down , 1768ggml_tensor * down_b , 1769ggml_tensor * down_s , 1770ggml_tensor * act_scales , 1771llm_ffn_op_type type_op , 1772llm_ffn_gate_type type_gate , 1773int il )const { 1774// NVFP4 support is currently restricted to 1775// 1) LORA absence (*_s would be applied after LORA residual, which is incorrect) 1776// 2) bias absense (*_s would be applied after bias addition, which is incorrect) 1777// TODO: disambiguate LLM-architectural scales (which use *_s) from NVFP4 scale_2 (which also uses *_s currently) 1778auto has_lora = [this ](ggml_tensor * w ) { 1779if (!w ) { 1780return false; 1781 } 1782for (const auto & lora :* loras ) { 1783if (lora .first -> get_weight (w )!= nullptr ) { 1784return true; 1785 } 1786 } 1787return false; 1788 }; 1789 1790GGML_ASSERT (!up_s || !up_b || !up || up -> type != GGML_TYPE_NVFP4 ); 1791GGML_ASSERT (!gate_s || !gate_b || !gate || gate -> type != GGML_TYPE_NVFP4 ); 1792GGML_ASSERT (!down_s || !down_b || !down || down -> type != GGML_TYPE_NVFP4 ); 1793GGML_ASSERT (!up_s || !up || up -> type != GGML_TYPE_NVFP4 || !has_lora (up )); 1794GGML_ASSERT (!gate_s || !gate || gate -> type != GGML_TYPE_NVFP4 || !has_lora (gate )); 1795GGML_ASSERT (!down_s || !down || down -> type != GGML_TYPE_NVFP4 || !has_lora (down )); 1796 1797ggml_tensor * tmp = up ?build_lora_mm (up ,cur ) :cur ; 1798cb (tmp ,"ffn_up" ,il ); 1799 1800if (up_b ) { 1801tmp = ggml_add (ctx0 ,tmp ,up_b ); 1802cb (tmp ,"ffn_up_b" ,il ); 1803 } 1804 1805if (up_s ) { 1806tmp = ggml_mul (ctx0 ,tmp ,up_s ); 1807cb (tmp ,"ffn_up_s" ,il ); 1808 } 1809 1810if (gate ) { 1811switch (type_gate ) { 1812case LLM_FFN_SEQ : 1813 { 1814cur = build_lora_mm (gate ,tmp ); 1815cb (cur ,"ffn_gate" ,il ); 1816 }break ; 1817case LLM_FFN_PAR : 1818 { 1819cur = build_lora_mm (gate ,cur ); 1820cb (cur ,"ffn_gate" ,il ); 1821 }break ; 1822 } 1823 1824if (gate_b ) { 1825cur = ggml_add (ctx0 ,cur ,gate_b ); 1826cb (cur ,"ffn_gate_b" ,il ); 1827 } 1828 1829if (gate_s ) { 1830cur = ggml_mul (ctx0 ,cur ,gate_s ); 1831cb (cur ,"ffn_gate_s" ,il ); 1832 } 1833 1834 }else { 1835cur = tmp ; 1836 } 1837 1838switch (type_op ) { 1839case LLM_FFN_SILU : 1840if (gate && type_gate == LLM_FFN_PAR ) { 1841if (il >=0 ) { 1842const float limit = hparams .swiglu_clamp_shexp [il ]; 1843constexpr float eps = 1e-6f ; 1844if (limit > eps ) { 1845if (arch == LLM_ARCH_DEEPSEEK4 || (arch == LLM_ARCH_DFLASH && hparams .dsv4_hc_mult > 0 )) { 1846cur = ggml_swiglu_clamp (ctx0 ,cur ,tmp ,limit ); 1847 }else { 1848tmp = ggml_clamp (ctx0 ,tmp ,- limit ,limit ); 1849cb (tmp ,"ffn_up_clamped" ,il ); 1850ggml_tensor * gate_act = ggml_silu (ctx0 ,cur ); 1851cb (gate_act ,"ffn_silu" ,il ); 1852gate_act = ggml_clamp (ctx0 ,gate_act ,- INFINITY ,limit ); 1853cb (gate_act ,"ffn_silu_clamped" ,il ); 1854cur = ggml_mul (ctx0 ,gate_act ,tmp ); 1855 } 1856cb (cur ,"ffn_swiglu_limited" ,il ); 1857type_gate = LLM_FFN_SEQ ; 1858break ; 1859 } 1860 } 1861 1862cur = ggml_swiglu_split (ctx0 ,cur ,tmp ); 1863cb (cur ,"ffn_swiglu" ,il ); 1864type_gate = LLM_FFN_SEQ ; 1865 }else { 1866cur = ggml_silu (ctx0 ,cur ); 1867cb (cur ,"ffn_silu" ,il ); 1868 }break ; 1869case LLM_FFN_GELU : 1870if (gate && type_gate == LLM_FFN_PAR ) { 1871cur = ggml_geglu_split (ctx0 ,cur ,tmp ); 1872cb (cur ,"ffn_geglu" ,il ); 1873type_gate = LLM_FFN_SEQ ; 1874 }else { 1875cur = ggml_gelu (ctx0 ,cur ); 1876cb (cur ,"ffn_gelu" ,il ); 1877if (act_scales != NULL ) { 1878cur = ggml_div (ctx0 ,cur ,act_scales ); 1879cb (cur ,"ffn_act" ,il ); 1880 } 1881 }break ; 1882case LLM_FFN_RELU : 1883if (gate && type_gate == LLM_FFN_PAR ) { 1884cur = ggml_reglu_split (ctx0 ,cur ,tmp ); 1885cb (cur ,"ffn_reglu" ,il ); 1886type_gate = LLM_FFN_SEQ ; 1887 }else { 1888cur = ggml_relu (ctx0 ,cur ); 1889cb (cur ,"ffn_relu" ,il ); 1890 }break ; 1891case LLM_FFN_RELU_SQR : 1892 { 1893cur = ggml_relu (ctx0 ,cur ); 1894cb (cur ,"ffn_relu" ,il ); 1895 1896cur = ggml_sqr (ctx0 ,cur ); 1897cb (cur ,"ffn_sqr(relu)" ,il ); 1898 }break ; 1899case LLM_FFN_SWIGLU : 1900 { 1901cur = ggml_swiglu (ctx0 ,cur ); 1902cb (cur ,"ffn_swiglu" ,il ); 1903 }break ; 1904case LLM_FFN_SWIGLU_OAI_MOE : 1905if (gate && type_gate == LLM_FFN_PAR ) { 1906// same alpha/limit constants as gpt-oss 1907const float alpha = 1.702f ; 1908const float limit = 7.0f ; 1909cur = ggml_swiglu_oai (ctx0 ,cur ,tmp ,alpha ,limit ); 1910cb (cur ,"ffn_swiglu_oai" ,il ); 1911type_gate = LLM_FFN_SEQ ; 1912 }else { 1913GGML_ABORT ("LLM_FFN_SWIGLU_OAI_MOE requires a parallel gate" ); 1914 }break ; 1915case LLM_FFN_GEGLU : 1916 { 1917cur = ggml_geglu (ctx0 ,cur ); 1918cb (cur ,"ffn_geglu" ,il ); 1919 }break ; 1920case LLM_FFN_REGLU : 1921 { 1922cur = ggml_reglu (ctx0 ,cur ); 1923cb (cur ,"ffn_reglu" ,il ); 1924 }break ; 1925case LLM_FFN_SITU : 1926GGML_ABORT ("not yet supported" ); 1927default : 1928GGML_ABORT ("fatal error" ); 1929 } 1930 1931if (gate && type_gate == LLM_FFN_PAR ) { 1932cur = ggml_mul (ctx0 ,cur ,tmp ); 1933cb (cur ,"ffn_gate_par" ,il ); 1934 } 1935 1936if (down ) { 1937cur = build_lora_mm (down ,cur ); 1938if (arch == LLM_ARCH_GLM4 || arch == LLM_ARCH_GLM4_MOE || arch == LLM_ARCH_JAIS2 ) { 1939// GLM4, GLM4_MOE, and JAIS2 seem to have numerical issues with half-precision accumulators 1940ggml_prec_set_acc (cur ,GGML_PREC_F32 ); 1941 } 1942 } 1943 1944if (down_b ) { 1945cb (cur ,"ffn_down" ,il ); 1946 } 1947 1948if (down_b ) { 1949cur = ggml_add (ctx0 ,cur ,down_b ); 1950 } 1951 1952if (down_s ) { 1953cur = ggml_mul (ctx0 ,cur ,down_s ); 1954cb (cur ,"ffn_down_s" ,il ); 1955 } 1956 1957return cur ; 1958} 1959 1960ggml_tensor * llm_graph_context::build_moe_ffn ( 1961ggml_tensor * cur , 1962ggml_tensor * gate_inp , 1963ggml_tensor * up_exps , 1964ggml_tensor * gate_exps , 1965ggml_tensor * down_exps , 1966ggml_tensor * exp_probs_b , 1967int64_t n_expert , 1968int64_t n_expert_used , 1969llm_ffn_op_type type_op , 1970bool norm_w , 1971float w_scale , 1972llama_expert_gating_func_type gating_op , 1973int il , 1974ggml_tensor * probs_in , 1975ggml_tensor * gate_up_exps , 1976ggml_tensor * up_exps_s , 1977ggml_tensor * gate_exps_s , 1978ggml_tensor * down_exps_s , 1979ggml_tensor * selected_experts_in )const { 1980return build_moe_ffn ( 1981cur , 1982gate_inp ,/* gate_inp_b */ nullptr , 1983up_exps ,/* up_exps_b */ nullptr , 1984gate_exps ,/* gate_exps_b */ nullptr , 1985down_exps ,/* down_exps_b */ nullptr , 1986exp_probs_b , 1987n_expert , 1988n_expert_used , 1989type_op , 1990norm_w , 1991w_scale , 1992gating_op , 1993il , 1994probs_in , 1995gate_up_exps , 1996/* gate_up_exps_b */ nullptr , 1997up_exps_s , 1998gate_exps_s , 1999down_exps_s , 2000selected_experts_in 2001 ); 2002} 2003 2004ggml_tensor * llm_graph_context::build_moe_ffn ( 2005ggml_tensor * cur , 2006ggml_tensor * gate_inp , 2007ggml_tensor * gate_inp_b , 2008ggml_tensor * up_exps , 2009ggml_tensor * up_exps_b , 2010ggml_tensor * gate_exps , 2011ggml_tensor * gate_exps_b , 2012ggml_tensor * down_exps , 2013ggml_tensor * down_exps_b , 2014ggml_tensor * exp_probs_b , 2015int64_t n_expert , 2016int64_t n_expert_used , 2017llm_ffn_op_type type_op , 2018bool norm_w , 2019float w_scale , 2020llama_expert_gating_func_type gating_op , 2021int il , 2022ggml_tensor * probs_in , 2023ggml_tensor * gate_up_exps , 2024ggml_tensor * gate_up_exps_b , 2025ggml_tensor * up_exps_s , 2026ggml_tensor * gate_exps_s , 2027ggml_tensor * down_exps_s , 2028ggml_tensor * selected_experts_in )const { 2029const int64_t n_embd = cur -> ne [0 ]; 2030const int64_t n_tokens = cur -> ne [1 ]; 2031const bool weight_before_ffn = arch == LLM_ARCH_LLAMA4 ;// for llama4, we apply the sigmoid-ed weights before the FFN 2032 2033ggml_tensor * logits = nullptr ; 2034 2035if (probs_in == nullptr ) { 2036logits = build_lora_mm (gate_inp ,cur );// [n_expert, n_tokens] 2037if (gating_op == LLAMA_EXPERT_GATING_FUNC_TYPE_SQRT_SOFTPLUS ) { 2038ggml_prec_set_acc (logits ,GGML_PREC_F32 ); 2039 } 2040cb (logits ,"ffn_moe_logits" ,il ); 2041 }else { 2042logits = probs_in ; 2043 } 2044 2045if (gate_inp_b ) { 2046logits = ggml_add (ctx0 ,logits ,gate_inp_b ); 2047cb (logits ,"ffn_moe_logits_biased" ,il ); 2048 } 2049 2050ggml_tensor * probs = nullptr ; 2051switch (gating_op ) { 2052case LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX : 2053 { 2054probs = ggml_soft_max (ctx0 ,logits );// [n_expert, n_tokens] 2055 }break ; 2056case LLAMA_EXPERT_GATING_FUNC_TYPE_SIGMOID : 2057 { 2058probs = ggml_sigmoid (ctx0 ,logits );// [n_expert, n_tokens] 2059 }break ; 2060case LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX_WEIGHT : 2061 { 2062probs = logits ;// [n_expert, n_tokens] 2063 }break ; 2064case LLAMA_EXPERT_GATING_FUNC_TYPE_SQRT_SOFTPLUS : 2065 { 2066probs = ggml_sqrt (ctx0 ,ggml_softplus (ctx0 ,logits ));// [n_expert, n_tokens] 2067 }break ; 2068default : 2069GGML_ABORT ("fatal error" ); 2070 } 2071cb (probs ,"ffn_moe_probs" ,il ); 2072 2073// add experts selection bias - introduced in DeepSeek V3 2074// leave probs unbiased as it's later used to get expert weights 2075ggml_tensor * selection_probs = probs ; 2076if (exp_probs_b != nullptr ) { 2077selection_probs = ggml_add (ctx0 ,probs ,exp_probs_b ); 2078cb (selection_probs ,"ffn_moe_probs_biased" ,il ); 2079 } 2080 2081// llama4 doesn't have exp_probs_b, and sigmoid is only used after top_k 2082// see: https://github.com/meta-llama/llama-models/blob/699a02993512fb36936b1b0741e13c06790bcf98/models/llama4/moe.py#L183-L198 2083if (arch == LLM_ARCH_LLAMA4 ) { 2084selection_probs = logits ; 2085 } 2086 2087if (arch == LLM_ARCH_GROVEMOE ) { 2088selection_probs = ggml_sigmoid (ctx0 ,logits );// [n_expert, n_tokens] 2089cb (selection_probs ,"ffn_moe_probs_biased" ,il ); 2090 } 2091 2092// select top n_group_used expert groups 2093// https://huggingface.co/deepseek-ai/DeepSeek-V3/blob/e815299b0bcbac849fa540c768ef21845365c9eb/modeling_deepseek.py#L440-L457 2094if (hparams .n_expert_groups > 1 && n_tokens > 0 ) { 2095const int64_t n_exp_per_group = n_expert /hparams .n_expert_groups ; 2096 2097// organize experts into n_expert_groups 2098ggml_tensor * selection_groups = ggml_reshape_3d (ctx0 ,selection_probs ,n_exp_per_group ,hparams .n_expert_groups ,n_tokens );// [n_exp_per_group, n_expert_groups, n_tokens] 2099 2100ggml_tensor * group_scores = ggml_argsort_top_k (ctx0 ,selection_groups ,2 );// [2, n_expert_groups, n_tokens] 2101group_scores = ggml_get_rows (ctx0 ,ggml_reshape_4d (ctx0 ,selection_groups ,1 ,selection_groups -> ne [0 ],selection_groups -> ne [1 ],selection_groups -> ne [2 ]),group_scores );// [1, 2, n_expert_groups, n_tokens] 2102 2103// get top n_group_used expert groups 2104group_scores = ggml_sum_rows (ctx0 ,ggml_reshape_3d (ctx0 ,group_scores ,group_scores -> ne [1 ],group_scores -> ne [2 ],group_scores -> ne [3 ]));// [1, n_expert_groups, n_tokens] 2105group_scores = ggml_reshape_2d (ctx0 ,group_scores ,group_scores -> ne [1 ],group_scores -> ne [2 ]);// [n_expert_groups, n_tokens] 2106 2107ggml_tensor * expert_groups = ggml_argsort_top_k (ctx0 ,group_scores ,hparams .n_group_used );// [n_group_used, n_tokens] 2108cb (expert_groups ,"ffn_moe_group_topk" ,il ); 2109 2110// mask out the other groups 2111selection_probs = ggml_get_rows (ctx0 ,selection_groups ,expert_groups );// [n_exp_per_group, n_group_used, n_tokens] 2112selection_probs = ggml_set_rows (ctx0 ,ggml_fill (ctx0 ,selection_groups ,- INFINITY ),selection_probs ,expert_groups );// [n_exp_per_group, n_expert_groups, n_tokens] 2113selection_probs = ggml_reshape_2d (ctx0 ,selection_probs ,n_expert ,n_tokens );// [n_expert, n_tokens] 2114cb (selection_probs ,"ffn_moe_probs_masked" ,il ); 2115 } 2116 2117// select experts 2118ggml_tensor * selected_experts = selected_experts_in ; 2119if (selected_experts == nullptr ) { 2120selected_experts = ggml_argsort_top_k (ctx0 ,selection_probs ,n_expert_used );// [n_expert_used, n_tokens] 2121cb (selected_experts -> src [0 ],"ffn_moe_argsort" ,il ); 2122 } 2123cb (selected_experts ,"ffn_moe_topk" ,il ); 2124 2125if (arch == LLM_ARCH_GROVEMOE && n_expert != hparams .n_expert ) { 2126// TODO: Use scalar div instead when/if implemented 2127ggml_tensor * f_sel = ggml_cast (ctx0 ,selected_experts ,GGML_TYPE_F32 ); 2128selected_experts = ggml_cast (ctx0 ,ggml_scale (ctx0 ,f_sel ,1.0f /float (hparams .n_group_experts )),GGML_TYPE_I32 ); 2129probs = ggml_reshape_3d (ctx0 ,probs ,1 ,hparams .n_expert ,n_tokens ); 2130 }else { 2131probs = ggml_reshape_3d (ctx0 ,probs ,1 ,n_expert ,n_tokens ); 2132 } 2133 2134ggml_tensor * weights = ggml_get_rows (ctx0 ,probs ,selected_experts );// [1, n_expert_used, n_tokens] 2135cb (weights ,"ffn_moe_weights" ,il ); 2136 2137 2138if (gating_op == LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX_WEIGHT ) { 2139weights = ggml_reshape_2d (ctx0 ,weights ,n_expert_used ,n_tokens ); 2140weights = ggml_soft_max (ctx0 ,weights );// [n_expert_used, n_tokens] 2141weights = ggml_reshape_3d (ctx0 ,weights ,1 ,n_expert_used ,n_tokens ); 2142cb (weights ,"ffn_moe_weights_softmax" ,il ); 2143 } 2144 2145if (norm_w ) { 2146weights = ggml_reshape_2d (ctx0 ,weights ,n_expert_used ,n_tokens ); 2147 2148ggml_tensor * weights_sum = ggml_sum_rows (ctx0 ,weights );// [1, n_tokens] 2149cb (weights_sum ,"ffn_moe_weights_sum" ,il ); 2150 2151// Avoid division by zero, clamp to smallest number representable by F16 2152weights_sum = ggml_clamp (ctx0 ,weights_sum ,6.103515625e-5 ,INFINITY ); 2153cb (weights_sum ,"ffn_moe_weights_sum_clamped" ,il ); 2154 2155weights = ggml_div (ctx0 ,weights ,weights_sum );// [n_expert_used, n_tokens] 2156cb (weights ,"ffn_moe_weights_norm" ,il ); 2157 2158weights = ggml_reshape_3d (ctx0 ,weights ,1 ,n_expert_used ,n_tokens ); 2159 } 2160if (w_scale != 0.0f && w_scale != 1.0f ) { 2161weights = ggml_scale (ctx0 ,weights ,w_scale ); 2162cb (weights ,"ffn_moe_weights_scaled" ,il ); 2163 } 2164 2165//call early so that topk-moe can be used 2166ggml_build_forward_expand (gf ,weights ); 2167 2168cur = ggml_reshape_3d (ctx0 ,cur ,n_embd ,1 ,n_tokens ); 2169 2170if (weight_before_ffn ) { 2171// repeat cur to [n_embd, n_expert_used, n_tokens] 2172ggml_tensor * repeated = ggml_repeat_4d (ctx0 ,cur ,n_embd ,n_expert_used ,n_tokens ,1 ); 2173cur = ggml_mul (ctx0 ,repeated ,weights ); 2174cb (cur ,"ffn_moe_weighted" ,il ); 2175 } 2176 2177ggml_tensor * up = nullptr ; 2178ggml_tensor * experts = nullptr ; 2179 2180if (gate_up_exps ) { 2181// merged gate_up path: one mul_mat_id, then split into gate and up views 2182ggml_tensor * gate_up = build_lora_mm_id (gate_up_exps ,cur ,selected_experts ,up_exps_s );// [n_ff*2, n_expert_used, n_tokens] 2183cb (gate_up ,"ffn_moe_gate_up" ,il ); 2184 2185if (up_exps_s ) { 2186cb (gate_up ,"ffn_moe_gate_up_scaled" ,il ); 2187 } 2188 2189if (gate_up_exps_b ) { 2190gate_up = ggml_add_id (ctx0 ,gate_up ,gate_up_exps_b ,selected_experts ); 2191cb (gate_up ,"ffn_moe_gate_up_biased" ,il ); 2192 } 2193 2194const int64_t n_ff = gate_up -> ne [0 ] /2 ; 2195cur = ggml_view_3d (ctx0 ,gate_up ,n_ff ,gate_up -> ne [1 ],gate_up -> ne [2 ],gate_up -> nb [1 ],gate_up -> nb [2 ],0 ); 2196cb (cur ,"ffn_moe_gate" ,il ); 2197up = ggml_view_3d (ctx0 ,gate_up ,n_ff ,gate_up -> ne [1 ],gate_up -> ne [2 ],gate_up -> nb [1 ],gate_up -> nb [2 ],n_ff * gate_up -> nb [0 ]); 2198cb (up ,"ffn_moe_up" ,il ); 2199 }else { 2200// separate gate and up path 2201up = build_lora_mm_id (up_exps ,cur ,selected_experts ,up_exps_s );// [n_ff, n_expert_used, n_tokens] 2202cb (up ,"ffn_moe_up" ,il ); 2203 2204if (up_exps_s ) { 2205cb (up ,"ffn_moe_up_scaled" ,il ); 2206 } 2207 2208if (up_exps_b ) { 2209up = ggml_add_id (ctx0 ,up ,up_exps_b ,selected_experts ); 2210cb (up ,"ffn_moe_up_biased" ,il ); 2211 } 2212 2213if (gate_exps ) { 2214cur = build_lora_mm_id (gate_exps ,cur ,selected_experts ,gate_exps_s );// [n_ff, n_expert_used, n_tokens] 2215cb (cur ,"ffn_moe_gate" ,il ); 2216 }else { 2217cur = up ; 2218 } 2219 2220if (gate_exps_s ) { 2221cb (cur ,"ffn_moe_gate_scaled" ,il ); 2222 } 2223 2224if (gate_exps_b ) { 2225cur = ggml_add_id (ctx0 ,cur ,gate_exps_b ,selected_experts ); 2226cb (cur ,"ffn_moe_gate_biased" ,il ); 2227 } 2228 } 2229 2230const bool has_gate = gate_exps || gate_up_exps ; 2231 2232switch (type_op ) { 2233case LLM_FFN_SILU : 2234if (gate_exps ) { 2235if (il >=0 ) { 2236const float limit = hparams .swiglu_clamp_exp [il ]; 2237constexpr float eps = 1e-6f ; 2238if (limit > eps ) { 2239if (arch == LLM_ARCH_MAPLE || arch == LLM_ARCH_DEEPSEEK4 || (arch == LLM_ARCH_DFLASH && hparams .dsv4_hc_mult > 0 )|| arch == LLM_ARCH_HY_V4 ) { 2240cur = ggml_swiglu_clamp (ctx0 ,cur ,up ,limit ); 2241 }else { 2242up = ggml_clamp (ctx0 ,up ,- limit ,limit ); 2243cb (up ,"ffn_moe_up_clamped" ,il ); 2244ggml_tensor * gate_act = ggml_silu (ctx0 ,cur ); 2245cb (gate_act ,"ffn_moe_silu" ,il ); 2246gate_act = ggml_clamp (ctx0 ,gate_act ,- INFINITY ,limit ); 2247cb (gate_act ,"ffn_moe_silu_clamped" ,il ); 2248cur = ggml_mul (ctx0 ,gate_act ,up ); 2249 } 2250cb (cur ,"ffn_moe_swiglu_limited" ,il ); 2251break ; 2252 } 2253 } 2254 } 2255 2256if (has_gate ) { 2257cur = ggml_swiglu_split (ctx0 ,cur ,up ); 2258cb (cur ,"ffn_moe_swiglu" ,il ); 2259 }else { 2260cur = ggml_silu (ctx0 ,cur ); 2261cb (cur ,"ffn_moe_silu" ,il ); 2262 }break ; 2263case LLM_FFN_SITU : 2264 { 2265// situ(gate, up) = beta*tanh(gate/beta)*sigmoid(gate) * lb*tanh(up/lb) 2266GGML_ASSERT (has_gate ); 2267const float beta = hparams .situ_beta ; 2268const float lb = hparams .situ_linear_beta ; 2269 2270ggml_tensor * act = ggml_scale (ctx0 ,ggml_tanh (ctx0 ,ggml_scale (ctx0 ,cur ,1.0f /beta )),beta ); 2271act = ggml_mul (ctx0 ,act ,ggml_sigmoid (ctx0 ,cur )); 2272if (lb > 0.0f ) { 2273up = ggml_scale (ctx0 ,ggml_tanh (ctx0 ,ggml_scale (ctx0 ,up ,1.0f /lb )),lb ); 2274 } 2275cur = ggml_mul (ctx0 ,act ,up ); 2276cb (cur ,"ffn_moe_situ" ,il ); 2277 }break ; 2278case LLM_FFN_GELU : 2279if (has_gate ) { 2280cur = ggml_geglu_split (ctx0 ,cur ,up ); 2281cb (cur ,"ffn_moe_geglu" ,il ); 2282 }else { 2283cur = ggml_gelu (ctx0 ,cur ); 2284cb (cur ,"ffn_moe_gelu" ,il ); 2285 }break ; 2286case LLM_FFN_SWIGLU_OAI_MOE : 2287 { 2288// TODO: move to hparams? 2289constexpr float alpha = 1.702f ; 2290constexpr float limit = 7.0f ; 2291cur = ggml_swiglu_oai (ctx0 ,cur ,up ,alpha ,limit ); 2292cb (cur ,"ffn_moe_swiglu_oai" ,il ); 2293 }break ; 2294case LLM_FFN_RELU : 2295if (has_gate ) { 2296cur = ggml_reglu_split (ctx0 ,cur ,up ); 2297cb (cur ,"ffn_moe_reglu" ,il ); 2298 }else { 2299cur = ggml_relu (ctx0 ,cur ); 2300cb (cur ,"ffn_moe_relu" ,il ); 2301 }break ; 2302case LLM_FFN_RELU_SQR : 2303if (has_gate ) { 2304// TODO: add support for gated squared relu 2305GGML_ABORT ("fatal error: gated squared relu not implemented" ); 2306 }else { 2307cur = ggml_relu (ctx0 ,cur ); 2308cur = ggml_sqr (ctx0 ,cur ); 2309cb (cur ,"ffn_moe_relu_sqr" ,il ); 2310 }break ; 2311default : 2312GGML_ABORT ("fatal error" ); 2313 } 2314 2315experts = build_lora_mm_id (down_exps ,cur ,selected_experts ,down_exps_s );// [n_embd, n_expert_used, n_tokens] 2316cb (experts ,"ffn_moe_down" ,il ); 2317 2318if (down_exps_s ) { 2319cb (experts ,"ffn_moe_down_scaled" ,il ); 2320 } 2321 2322if (down_exps_b ) { 2323experts = ggml_add_id (ctx0 ,experts ,down_exps_b ,selected_experts ); 2324cb (experts ,"ffn_moe_down_biased" ,il ); 2325 } 2326 2327if (!weight_before_ffn ) { 2328experts = ggml_mul (ctx0 ,experts ,weights ); 2329cb (experts ,"ffn_moe_weighted" ,il ); 2330 } 2331 2332ggml_build_forward_expand (gf ,experts ); 2333 2334ggml_tensor * cur_experts [LLAMA_MAX_EXPERTS ]= {nullptr }; 2335 2336assert (n_expert_used > 0 ); 2337 2338// order the views before the adds 2339// Use per-layer n_expert_used to bound the graph even during warmup (avoids 2340// the large-add-nodes issue for uniform arches; for Puzzle the per-layer 2341// value is correct). ref: https://github.com/ggml-org/llama.cpp/pull/14753 2342const uint32_t n_expert_used_il = hparams .n_expert_used (il ); 2343for (uint32_t i = 0 ;i < n_expert_used_il ;++ i ) { 2344cur_experts [i ]= ggml_view_2d (ctx0 ,experts ,n_embd ,n_tokens ,experts -> nb [2 ],i * experts -> nb [1 ]); 2345 2346ggml_build_forward_expand (gf ,cur_experts [i ]); 2347 } 2348 2349// aggregate experts 2350ggml_tensor * moe_out = cur_experts [0 ]; 2351 2352for (uint32_t i = 1 ;i < n_expert_used_il ;++ i ) { 2353moe_out = ggml_add (ctx0 ,moe_out ,cur_experts [i ]); 2354 2355ggml_build_forward_expand (gf ,moe_out ); 2356 } 2357 2358if (n_expert_used_il == 1 ) { 2359// avoid returning a non-contiguous tensor 2360moe_out = ggml_cont (ctx0 ,moe_out ); 2361 } 2362 2363cb (moe_out ,"ffn_moe_out" ,il ); 2364 2365return moe_out ; 2366} 2367 2368// input embeddings with optional lora 2369ggml_tensor * llm_graph_context::build_inp_embd (ggml_tensor * tok_embd )const { 2370const int64_t n_embd_inp = hparams .n_embd_inp (); 2371const int64_t n_embd = hparams .n_embd ; 2372 2373assert (n_embd_inp >=n_embd ); 2374 2375auto inp = std::make_unique < llm_graph_input_embd > (n_embd_inp ); 2376 2377inp -> tokens = ggml_new_tensor_1d (ctx0 ,GGML_TYPE_I32 ,ubatch .n_tokens ); 2378cb (inp -> tokens ,"inp_tokens" ,-1 ); 2379ggml_set_input (inp -> tokens ); 2380res -> t_inp_tokens = inp -> tokens ; 2381 2382inp -> embd = ggml_new_tensor_2d (ctx0 ,GGML_TYPE_F32 ,n_embd_inp ,ubatch .n_tokens ); 2383cb (inp -> embd ,"inp_embd" ,-1 ); 2384ggml_set_input (inp -> embd ); 2385 2386// select one of the 2 inputs, based on the batch contents 2387// ref: https://github.com/ggml-org/llama.cpp/pull/18550 2388 std::array < ggml_tensor * ,2 > inps ; 2389 2390// token embeddings path (ubatch.token != nullptr) 2391 { 2392auto & cur = inps [0 ]; 2393 2394cur = ggml_get_rows (ctx0 ,tok_embd ,inp -> tokens ); 2395 2396// apply lora for embedding tokens if needed 2397for (const auto & lora :* loras ) { 2398llama_adapter_lora_weight * lw = lora .first -> get_weight (tok_embd ); 2399if (lw == nullptr ) { 2400continue ; 2401 } 2402 2403const float adapter_scale = lora .second ; 2404const float scale = lw -> get_scale (lora .first -> alpha ,adapter_scale ); 2405 2406ggml_tensor * inpL_delta = ggml_scale (ctx0 ,ggml_mul_mat ( 2407ctx0 ,lw -> b ,// non-transposed lora_b 2408ggml_get_rows (ctx0 ,lw -> a ,inp -> tokens ) 2409 ),scale ); 2410 2411cur = ggml_add (ctx0 ,cur ,inpL_delta ); 2412 } 2413 2414if (n_embd_inp != n_embd ) { 2415cur = ggml_pad (ctx0 ,cur ,hparams .n_embd_inp ()- n_embd ,0 ,0 ,0 ); 2416 } 2417 } 2418 2419// vector embeddings path (ubatch.embd != nullptr) 2420 { 2421auto & cur = inps [1 ]; 2422 2423cur = inp -> embd ; 2424 } 2425 2426assert (ggml_are_same_shape (inps [0 ],inps [1 ])); 2427assert (ggml_are_same_stride (inps [0 ],inps [1 ])); 2428 2429ggml_tensor * cur = ggml_build_forward_select (gf ,inps .data (),inps .size (),ubatch .token ?0 :1 ); 2430 2431if (n_embd_inp != n_embd ) { 2432cur = ggml_view_2d (ctx0 ,cur ,n_embd ,n_tokens ,cur -> nb [1 ],0 ); 2433 } 2434 2435res -> t_inp_embd = cur ; 2436 2437// For Granite architecture 2438// NOTE: For deepstack models, only apply scale to token inputs (ie text-only input). 2439// Raw embeddings are assumed to be multimodal inputs that should not be scaled. 2440if (hparams .f_embedding_scale != 0.0f && (ubatch .token || hparams .n_deepstack_layers == 0 )) { 2441if (!ggml_is_contiguous (cur )) { 2442cur = ggml_cont (ctx0 ,cur ); 2443 } 2444cur = ggml_scale (ctx0 ,cur ,hparams .f_embedding_scale ); 2445 } 2446 2447cb (cur ,"embd" ,-1 ); 2448 2449res -> add_input (std::move (inp )); 2450 2451// make sure the produced embeddings are immediately materialized in the ggml graph 2452// ref: https://github.com/ggml-org/llama.cpp/pull/18599 2453ggml_build_forward_expand (gf ,cur ); 2454 2455return cur ; 2456} 2457 2458ggml_tensor * llm_graph_context::build_inp_pos ()const { 2459auto inp = std::make_unique < llm_graph_input_pos > (hparams .n_pos_per_embd ()); 2460 2461auto & cur = inp -> pos ; 2462 2463cur = ggml_new_tensor_1d (ctx0 ,GGML_TYPE_I32 , (int64_t )n_tokens * hparams .n_pos_per_embd ()); 2464ggml_set_input (cur ); 2465 2466res -> add_input (std::move (inp )); 2467 2468return cur ; 2469} 2470 2471ggml_tensor * llm_graph_context::build_inp_attn_scale ()const { 2472auto inp = std::make_unique < llm_graph_input_attn_temp > (hparams .n_attn_temp_floor_scale ,hparams .f_attn_temp_scale ,hparams .f_attn_temp_offset ); 2473 2474auto & cur = inp -> attn_scale ; 2475 2476// this need to be 1x1xN for broadcasting 2477cur = ggml_new_tensor_3d (ctx0 ,GGML_TYPE_F32 ,1 ,1 ,n_tokens ); 2478ggml_set_input (cur ); 2479ggml_set_name (cur ,"attn_scale" ); 2480 2481res -> add_input (std::move (inp )); 2482 2483return cur ; 2484} 2485 2486ggml_tensor * llm_graph_context::build_inp_out_ids ()const { 2487// note: when all tokens are output, we could skip this optimization to spare the ggml_get_rows() calls, 2488// but this would make the graph topology depend on the number of output tokens, which can interfere with 2489// features that require constant topology such as pipeline parallelism 2490// ref: https://github.com/ggml-org/llama.cpp/pull/14275#issuecomment-2987424471 2491//if (n_outputs < n_tokens) { 2492// return nullptr; 2493//} 2494 2495auto inp = std::make_unique < llm_graph_input_out_ids > (hparams ,cparams ,n_outputs ); 2496 2497auto & cur = inp -> out_ids ; 2498 2499cur = ggml_new_tensor_1d (ctx0 ,GGML_TYPE_I32 ,n_outputs ); 2500ggml_set_input (cur ); 2501 2502res -> add_input (std::move (inp )); 2503 2504return cur ; 2505} 2506 2507ggml_tensor * llm_graph_context::build_inp_mean ()const { 2508auto inp = std::make_unique < llm_graph_input_mean > (cparams ); 2509 2510auto & cur = inp -> mean ; 2511 2512cur = ggml_new_tensor_2d (ctx0 ,GGML_TYPE_F32 ,n_tokens ,ubatch .n_seqs_unq ); 2513ggml_set_input (cur ); 2514 2515res -> add_input (std::move (inp )); 2516 2517return cur ; 2518} 2519 2520ggml_tensor * llm_graph_context::build_inp_cls ()const { 2521auto inp = std::make_unique < llm_graph_input_cls > (cparams ,arch ); 2522 2523auto & cur = inp -> cls ; 2524 2525cur = ggml_new_tensor_1d (ctx0 ,GGML_TYPE_I32 ,ubatch .n_seqs_unq ); 2526ggml_set_input (cur ); 2527 2528res -> add_input (std::move (inp )); 2529 2530return cur ; 2531} 2532 2533ggml_tensor * llm_graph_context::build_inp_cross_embd ()const { 2534auto inp = std::make_unique < llm_graph_input_cross_embd > (cross ); 2535 2536auto & cur = inp -> cross_embd ; 2537 2538// if we have the output embeddings from the encoder, use them directly 2539// TODO: needs more work to be correct, for now just use the tensor shape 2540//if (cross->t_embd) { 2541// cur = ggml_view_tensor(ctx0, cross->t_embd); 2542 2543// return cur; 2544//} 2545 2546const auto n_embd = !cross -> v_embd .empty () ?cross -> n_embd :hparams .n_embd_inp (); 2547// Encoding must fit in one ubatch; do not reserve the full training context. 2548const auto n_enc = !cross -> v_embd .empty () ?cross -> n_enc :cparams .n_ubatch ; 2549 2550cur = ggml_new_tensor_2d (ctx0 ,GGML_TYPE_F32 ,n_embd ,n_enc ); 2551ggml_set_input (cur ); 2552 2553res -> add_input (std::move (inp )); 2554 2555return cur ; 2556} 2557 2558ggml_tensor * llm_graph_context::build_inp_pos_bucket_enc ()const { 2559auto inp = std::make_unique < llm_graph_input_pos_bucket > (hparams ); 2560 2561auto & cur = inp -> pos_bucket ; 2562 2563cur = ggml_new_tensor_2d (ctx0 ,GGML_TYPE_I32 ,n_tokens ,n_tokens ); 2564ggml_set_input (cur ); 2565 2566res -> add_input (std::move (inp )); 2567 2568return cur ; 2569} 2570 2571ggml_tensor * llm_graph_context::build_inp_pos_bucket_dec ()const { 2572const auto * mctx_cur = static_cast < const llama_kv_cache_context *> (mctx ); 2573 2574auto inp = std::make_unique < llm_graph_input_pos_bucket_kv > (hparams ,mctx_cur ); 2575 2576const auto n_kv = mctx_cur -> get_n_kv (); 2577 2578auto & cur = inp -> pos_bucket ; 2579 2580cur = ggml_new_tensor_2d (ctx0 ,GGML_TYPE_I32 ,n_kv ,n_tokens ); 2581ggml_set_input (cur ); 2582 2583res -> add_input (std::move (inp )); 2584 2585return cur ; 2586} 2587 2588ggml_tensor * llm_graph_context::build_pos_bias (ggml_tensor * pos_bucket ,ggml_tensor * attn_rel_b )const { 2589ggml_tensor * pos_bucket_1d = ggml_reshape_1d (ctx0 ,pos_bucket ,pos_bucket -> ne [0 ]* pos_bucket -> ne [1 ]); 2590cb (pos_bucket_1d ,"pos_bucket_1d" ,-1 ); 2591 2592ggml_tensor * pos_bias = ggml_get_rows (ctx0 ,attn_rel_b ,pos_bucket_1d ); 2593 2594pos_bias = ggml_reshape_3d (ctx0 ,pos_bias ,pos_bias -> ne [0 ],pos_bucket -> ne [0 ],pos_bucket -> ne [1 ]); 2595pos_bias = ggml_permute (ctx0 ,pos_bias ,2 ,0 ,1 ,3 ); 2596pos_bias = ggml_cont (ctx0 ,pos_bias ); 2597 2598cb (pos_bias ,"pos_bias" ,-1 ); 2599 2600return pos_bias ; 2601} 2602 2603ggml_tensor * llm_graph_context::build_attn_mha ( 2604ggml_tensor * q , 2605ggml_tensor * k , 2606ggml_tensor * v , 2607ggml_tensor * kq_b , 2608ggml_tensor * kq_mask , 2609ggml_tensor * sinks , 2610ggml_tensor * v_mla , 2611int64_t n_kv_max , 2612float kq_scale , 2613int il )const { 2614const bool v_trans = v -> nb [1 ]> v -> nb [2 ]; 2615 2616// split the batch into streams if needed 2617const auto n_stream = k -> ne [3 ]; 2618 2619q = ggml_view_4d (ctx0 ,q ,q -> ne [0 ],q -> ne [1 ],q -> ne [2 ]/n_stream ,n_stream ,q -> nb [1 ],q -> nb [2 ],q -> nb [3 ]/n_stream ,0 ); 2620 2621q = ggml_permute (ctx0 ,q ,0 ,2 ,1 ,3 ); 2622k = ggml_permute (ctx0 ,k ,0 ,2 ,1 ,3 ); 2623v = ggml_permute (ctx0 ,v ,0 ,2 ,1 ,3 ); 2624 2625ggml_tensor * cur ; 2626 2627const bool use_flash_attn = cparams .flash_attn && kq_b == nullptr ; 2628if (use_flash_attn ) { 2629GGML_ASSERT (kq_b == nullptr && "Flash attention does not support KQ bias yet" ); 2630 2631if (v_trans ) { 2632v = ggml_transpose (ctx0 ,v ); 2633 } 2634 2635// this can happen when KV cache is not used (e.g. an embedding model with non-causal attn) 2636if (k -> type == GGML_TYPE_F32 ) { 2637k = ggml_cast (ctx0 ,k ,GGML_TYPE_F16 ); 2638 } 2639 2640if (v -> type == GGML_TYPE_F32 ) { 2641v = ggml_cast (ctx0 ,v ,GGML_TYPE_F16 ); 2642 } 2643 2644cur = ggml_flash_attn_ext (ctx0 ,q ,k ,v ,kq_mask ,kq_scale ,hparams .f_max_alibi_bias , 2645hparams .attn_soft_cap ?hparams .f_attn_logit_softcapping :0.0f ); 2646res -> add_fused_node ({LLM_FUSED_OP_FLASH_ATTN ,cur ,il }); 2647 2648ggml_flash_attn_ext_add_sinks (cur ,sinks ); 2649GGML_ASSERT (n_kv_max >=0 && n_kv_max <=INT32_MAX ); 2650ggml_flash_attn_ext_set_n_kv_max (cur ,static_cast < int32_t > (n_kv_max )); 2651ggml_prec_set_acc (cur ,GGML_PREC_F32 ); 2652 2653if (v_mla ) { 2654#if 0 2655// v_mla can be applied as a matrix-vector multiplication with broadcasting across dimension 3 == n_tokens. 2656// However, the code is optimized for dimensions 0 and 1 being large, so this is inefficient. 2657cur = ggml_reshape_4d (ctx0 ,cur ,v_mla -> ne [0 ],1 ,n_head ,n_tokens ); 2658cur = ggml_mul_mat (ctx0 ,v_mla ,cur ); 2659#else 2660// It's preferable to do the calculation as a matrix-matrix multiplication with n_tokens in dimension 1. 2661// The permutations are noops and only change how the tensor data is interpreted. 2662cur = ggml_permute (ctx0 ,cur ,0 ,2 ,1 ,3 ); 2663cur = ggml_mul_mat (ctx0 ,v_mla ,cur ); 2664cb (cur ,"fattn_mla" ,il ); 2665cur = ggml_permute (ctx0 ,cur ,0 ,2 ,1 ,3 ); 2666cur = ggml_cont (ctx0 ,cur );// Needed because ggml_reshape_2d expects contiguous inputs. 2667#endif 2668 } 2669 2670cur = ggml_reshape_2d (ctx0 ,cur ,cur -> ne [0 ]* cur -> ne [1 ],cur -> ne [2 ]* cur -> ne [3 ]); 2671 }else { 2672ggml_tensor * kq = ggml_mul_mat (ctx0 ,k ,q ); 2673cb (kq ,"kq" ,il ); 2674 2675// note: this op tends to require high floating point range 2676// while for some models F16 is enough, for others it is not, so we default to F32 here 2677ggml_prec_set_acc (kq ,GGML_PREC_F32 ); 2678 2679if (arch == LLM_ARCH_GROK ) { 2680// need to do the following: 2681// multiply by attn_output_multiplier 2682// and then : 2683// kq = 30 * tanh(kq / 30) 2684// before the softmax below 2685 2686kq = ggml_tanh (ctx0 ,ggml_scale (ctx0 ,kq ,hparams .f_attn_out_scale /hparams .f_attn_logit_softcapping )); 2687cb (kq ,"kq_tanh" ,il ); 2688kq = ggml_scale (ctx0 ,kq ,hparams .f_attn_logit_softcapping ); 2689cb (kq ,"kq_scaled" ,il ); 2690 } 2691 2692if (hparams .attn_soft_cap ) { 2693kq = ggml_scale (ctx0 ,kq ,1.0f /hparams .f_attn_logit_softcapping ); 2694cb (kq ,"kq_scaled_1" ,il ); 2695kq = ggml_tanh (ctx0 ,kq ); 2696cb (kq ,"kq_tanh" ,il ); 2697kq = ggml_scale (ctx0 ,kq ,hparams .f_attn_logit_softcapping ); 2698cb (kq ,"kq_scaled_2" ,il ); 2699 } 2700 2701if (kq_b ) { 2702kq = ggml_add (ctx0 ,kq ,kq_b ); 2703cb (kq ,"kq_plus_kq_b" ,il ); 2704 } 2705 2706kq = ggml_soft_max_ext (ctx0 ,kq ,kq_mask ,kq_scale ,hparams .f_max_alibi_bias ); 2707ggml_soft_max_add_sinks (kq ,sinks ); 2708cb (kq ,"kq_soft_max" ,il ); 2709 2710if (!v_trans ) { 2711// note: avoid this branch 2712v = ggml_cont (ctx0 ,ggml_transpose (ctx0 ,v )); 2713cb (v ,"v_cont" ,il ); 2714 } 2715 2716ggml_tensor * kqv = ggml_mul_mat (ctx0 ,v ,kq ); 2717cb (kqv ,"kqv" ,il ); 2718 2719// for MLA with the absorption optimization, we need to "decompress" from MQA back to MHA 2720if (v_mla ) { 2721kqv = ggml_mul_mat (ctx0 ,v_mla ,kqv ); 2722cb (kqv ,"kqv_mla" ,il ); 2723 } 2724 2725cur = ggml_permute (ctx0 ,kqv ,0 ,2 ,1 ,3 ); 2726 2727// recombine streams 2728cur = ggml_cont_2d (ctx0 ,cur ,cur -> ne [0 ]* cur -> ne [1 ],cur -> ne [2 ]* cur -> ne [3 ]); 2729 2730if (!cparams .offload_kqv ) { 2731// all nodes between the KV store and the attention output are run on the CPU 2732ggml_backend_sched_set_tensor_backend (sched ,cur ,backend_cpu ); 2733 } 2734 } 2735 2736ggml_build_forward_expand (gf ,cur ); 2737 2738return cur ; 2739} 2740 2741llm_graph_input_attn_no_cache * llm_graph_context::build_attn_inp_no_cache ()const { 2742auto inp = std::make_unique < llm_graph_input_attn_no_cache > (hparams ,cparams ); 2743 2744// flash attention requires an f16 mask 2745const auto type_mask = cparams .flash_attn ?GGML_TYPE_F16 :GGML_TYPE_F32 ; 2746 2747// note: there is no KV cache, so the number of KV values is equal to the number of tokens in the batch 2748inp -> self_kq_mask = ggml_new_tensor_4d (ctx0 ,type_mask ,n_tokens ,n_tokens ,1 ,1 ); 2749ggml_set_input (inp -> self_kq_mask ); 2750 2751inp -> self_kq_mask_cnv = inp -> self_kq_mask ; 2752 2753if (hparams .swa_type != LLAMA_SWA_TYPE_NONE ) { 2754inp -> self_kq_mask_swa = ggml_new_tensor_4d (ctx0 ,type_mask ,n_tokens ,n_tokens ,1 ,1 ); 2755ggml_set_input (inp -> self_kq_mask_swa ); 2756 2757inp -> self_kq_mask_swa_cnv = inp -> self_kq_mask_swa ; 2758 }else { 2759inp -> self_kq_mask_swa = nullptr ; 2760inp -> self_kq_mask_swa_cnv = nullptr ; 2761 } 2762 2763return (llm_graph_input_attn_no_cache * )res -> add_input (std::move (inp )); 2764} 2765 2766ggml_tensor * llm_graph_context::build_attn ( 2767llm_graph_input_attn_no_cache * inp , 2768ggml_tensor * wo , 2769ggml_tensor * wo_b , 2770ggml_tensor * wo_s , 2771ggml_tensor * q_cur , 2772ggml_tensor * k_cur , 2773ggml_tensor * v_cur , 2774ggml_tensor * kq_b , 2775ggml_tensor * sinks , 2776ggml_tensor * v_mla , 2777float kq_scale , 2778int il )const { 2779GGML_UNUSED (n_tokens ); 2780 2781// these nodes are added to the graph together so that they are not reordered 2782// by doing so, the number of splits in the graph is reduced 2783ggml_build_forward_expand (gf ,q_cur ); 2784ggml_build_forward_expand (gf ,k_cur ); 2785ggml_build_forward_expand (gf ,v_cur ); 2786 2787const bool is_swa = hparams .is_swa (il ); 2788 2789const auto & kq_mask = is_swa ?inp -> get_kq_mask_swa () :inp -> get_kq_mask (); 2790 2791// [TAG_NO_CACHE_PAD] 2792// TODO: if ubatch.equal_seqs() == true, we can split the three tensors below into ubatch.n_seqs_unq streams 2793// but it might not be worth it: https://github.com/ggml-org/llama.cpp/pull/15636 2794//assert(!ubatch.equal_seqs() || (k_cur->ne[3] == 1 && k_cur->ne[3] == ubatch.n_seqs_unq)); 2795 2796ggml_tensor * q = q_cur ; 2797ggml_tensor * k = k_cur ; 2798ggml_tensor * v = v_cur ; 2799 2800ggml_tensor * cur = build_attn_mha (q ,k ,v ,kq_b ,kq_mask ,sinks ,v_mla ,0 ,kq_scale ,il ); 2801cb (cur ,"kqv_out" ,il ); 2802 2803if (wo ) { 2804cur = build_lora_mm (wo ,cur ,wo_s ); 2805 } 2806 2807if (wo_b ) { 2808//cb(cur, "kqv_wo", il); 2809 } 2810 2811if (wo_b ) { 2812cur = ggml_add (ctx0 ,cur ,wo_b ); 2813 } 2814 2815return cur ; 2816} 2817 2818static std::unique_ptr < llm_graph_input_attn_kv > build_attn_inp_kv_impl ( 2819ggml_context * ctx0 , 2820const llama_ubatch & ubatch , 2821const llama_hparams & hparams , 2822const llama_cparams & cparams , 2823const llama_kv_cache_context * mctx_cur ) { 2824 2825auto inp = std::make_unique < llm_graph_input_attn_kv > (hparams ,cparams ,mctx_cur ); 2826 2827 { 2828GGML_ASSERT (hparams .swa_type == LLAMA_SWA_TYPE_NONE && "Use llama_kv_cache_iswa for SWA" ); 2829 2830inp -> self_k_idxs = mctx_cur -> build_input_k_idxs (ctx0 ,ubatch ); 2831inp -> self_v_idxs = mctx_cur -> build_input_v_idxs (ctx0 ,ubatch ); 2832 2833inp -> self_kq_mask = build_attn_inp_kq_mask (ctx0 ,mctx_cur ,ubatch ,cparams ); 2834inp -> self_kq_mask_cnv = inp -> self_kq_mask ; 2835 } 2836 2837inp -> self_k_rot = mctx_cur -> build_input_k_rot (ctx0 ); 2838inp -> self_v_rot = mctx_cur -> build_input_v_rot (ctx0 ); 2839 2840return inp ; 2841} 2842 2843llm_graph_input_attn_kv * llm_graph_context::build_attn_inp_kv ()const { 2844const auto * mctx_cur = static_cast < const llama_kv_cache_context *> (mctx ); 2845 2846auto inp = build_attn_inp_kv_impl (ctx0 ,ubatch ,hparams ,cparams ,mctx_cur ); 2847 2848return (llm_graph_input_attn_kv * )res -> add_input (std::move (inp )); 2849} 2850 2851ggml_tensor * llm_graph_context::build_attn ( 2852llm_graph_input_attn_kv * inp , 2853ggml_tensor * wo , 2854ggml_tensor * wo_b , 2855ggml_tensor * wo_s , 2856ggml_tensor * q_cur , 2857ggml_tensor * k_cur , 2858ggml_tensor * v_cur , 2859ggml_tensor * kq_b , 2860ggml_tensor * sinks , 2861ggml_tensor * v_mla ,// TODO: remove 2862float kq_scale , 2863int il )const { 2864GGML_ASSERT (v_mla == nullptr ); 2865 2866if (inp -> self_k_rot ) { 2867q_cur = llama_mul_mat_hadamard (ctx0 ,q_cur ,inp -> self_k_rot ); 2868k_cur = llama_mul_mat_hadamard (ctx0 ,k_cur ,inp -> self_k_rot ); 2869 } 2870 2871if (inp -> self_v_rot ) { 2872v_cur = llama_mul_mat_hadamard (ctx0 ,v_cur ,inp -> self_v_rot ); 2873 } 2874 2875// these nodes are added to the graph together so that they are not reordered 2876// by doing so, the number of splits in the graph is reduced 2877// expand k later to enable rope fusion which directly writes into k-v cache 2878ggml_build_forward_expand (gf ,q_cur ); 2879ggml_build_forward_expand (gf ,v_cur ); 2880ggml_build_forward_expand (gf ,k_cur ); 2881 2882const auto * mctx_cur = inp -> mctx ; 2883 2884// store to KV cache 2885 { 2886const auto & k_idxs = inp -> get_k_idxs (); 2887const auto & v_idxs = inp -> get_v_idxs (); 2888 2889ggml_build_forward_expand (gf ,mctx_cur -> cpy_k (ctx0 ,k_cur ,k_idxs ,il )); 2890ggml_build_forward_expand (gf ,mctx_cur -> cpy_v (ctx0 ,v_cur ,v_idxs ,il )); 2891 } 2892 2893ggml_tensor * kq_mask = inp -> get_kq_mask (); 2894 2895ggml_tensor * q = q_cur ; 2896ggml_tensor * k = mctx_cur -> get_k (ctx0 ,il ); 2897ggml_tensor * v = mctx_cur -> get_v (ctx0 ,il ); 2898 2899ggml_tensor * cur = build_attn_mha (q ,k ,v ,kq_b ,kq_mask ,sinks ,v_mla ,0 ,kq_scale ,il ); 2900cb (cur ,"kqv_out" ,il ); 2901 2902if (inp -> self_v_rot ) { 2903cur = llama_mul_mat_hadamard (ctx0 ,cur ,inp -> self_v_rot ); 2904 } 2905 2906if (wo ) { 2907if (arch == LLM_ARCH_GLM4 || arch == LLM_ARCH_GLM4_MOE || arch == LLM_ARCH_JAIS2 ) { 2908// GLM4, GLM4_MOE, and JAIS2 seem to have numerical issues with half-precision accumulators 2909cur = build_lora_mm (wo ,cur ); 2910ggml_prec_set_acc (cur ,GGML_PREC_F32 ); 2911if (wo_s ) { 2912cur = ggml_mul (ctx0 ,cur ,wo_s ); 2913 } 2914 }else { 2915cur = build_lora_mm (wo ,cur ,wo_s ); 2916 } 2917 } 2918 2919if (wo_b ) { 2920cur = ggml_add (ctx0 ,cur ,wo_b ); 2921 } 2922 2923return cur ; 2924} 2925 2926static std::unique_ptr < llm_graph_input_attn_k > build_attn_inp_k_impl ( 2927ggml_context * ctx0 , 2928const llama_ubatch & ubatch , 2929const llama_hparams & hparams , 2930const llama_cparams & cparams , 2931const llama_kv_cache_context * mctx_cur ) { 2932 2933auto inp = std::make_unique < llm_graph_input_attn_k > (hparams ,cparams ,mctx_cur ); 2934 2935 { 2936GGML_ASSERT (hparams .swa_type == LLAMA_SWA_TYPE_NONE && "Use llama_kv_cache_iswa for SWA" ); 2937 2938inp -> self_k_idxs = mctx_cur -> build_input_k_idxs (ctx0 ,ubatch ); 2939 2940inp -> self_kq_mask = build_attn_inp_kq_mask (ctx0 ,mctx_cur ,ubatch ,cparams ); 2941inp -> self_kq_mask_cnv = inp -> self_kq_mask ; 2942 } 2943 2944return inp ; 2945} 2946 2947llm_graph_input_attn_k * llm_graph_context::build_attn_inp_k ()const { 2948const auto * mctx_cur = static_cast < const llama_kv_cache_context *> (mctx ); 2949 2950auto inp = build_attn_inp_k_impl (ctx0 ,ubatch ,hparams ,cparams ,mctx_cur ); 2951 2952return (llm_graph_input_attn_k * )res -> add_input (std::move (inp )); 2953} 2954 2955ggml_tensor * llm_graph_context::build_attn ( 2956llm_graph_input_attn_k * inp , 2957ggml_tensor * wo , 2958ggml_tensor * wo_b , 2959ggml_tensor * wo_s , 2960ggml_tensor * q_cur , 2961ggml_tensor * k_cur , 2962ggml_tensor * v_cur , 2963ggml_tensor * kq_b , 2964ggml_tensor * sinks , 2965ggml_tensor * v_mla , 2966float kq_scale , 2967int il )const { 2968// these nodes are added to the graph together so that they are not reordered 2969// by doing so, the number of splits in the graph is reduced 2970// expand k later to enable rope fusion which directly writes into k-v cache 2971ggml_build_forward_expand (gf ,q_cur ); 2972ggml_build_forward_expand (gf ,v_cur ); 2973ggml_build_forward_expand (gf ,k_cur ); 2974 2975const auto * mctx_cur = inp -> mctx ; 2976 2977// store to KV cache 2978 { 2979const auto & k_idxs = inp -> get_k_idxs (); 2980 2981ggml_build_forward_expand (gf ,mctx_cur -> cpy_k (ctx0 ,k_cur ,k_idxs ,il )); 2982 } 2983 2984const auto & kq_mask = inp -> get_kq_mask (); 2985 2986ggml_tensor * q = q_cur ; 2987ggml_tensor * k = mctx_cur -> get_k (ctx0 ,il ); 2988ggml_tensor * v = ggml_view_4d (ctx0 ,k ,v_cur -> ne [0 ],k -> ne [1 ],k -> ne [2 ],k -> ne [3 ],k -> nb [1 ],k -> nb [2 ],k -> nb [3 ],0 ); 2989 2990ggml_tensor * cur = build_attn_mha (q ,k ,v ,kq_b ,kq_mask ,sinks ,v_mla ,0 ,kq_scale ,il ); 2991cb (cur ,"kqv_out" ,il ); 2992 2993if (wo ) { 2994if (arch == LLM_ARCH_GLM4 || arch == LLM_ARCH_GLM4_MOE ) { 2995// GLM4 and GLM4_MOE seem to have numerical issues with half-precision accumulators 2996cur = build_lora_mm (wo ,cur ); 2997ggml_prec_set_acc (cur ,GGML_PREC_F32 ); 2998if (wo_s ) { 2999cur = ggml_mul (ctx0 ,cur ,wo_s ); 3000 } 3001 }else { 3002cur = build_lora_mm (wo ,cur ,wo_s ); 3003 } 3004 } 3005 3006if (wo_b ) { 3007cur = ggml_add (ctx0 ,cur ,wo_b ); 3008 } 3009 3010return cur ; 3011} 3012 3013ggml_tensor * llm_graph_context::build_attn ( 3014llm_graph_input_attn_k_dsa * inp , 3015ggml_tensor * wo , 3016ggml_tensor * wo_b , 3017ggml_tensor * wo_s , 3018ggml_tensor * q_cur , 3019ggml_tensor * k_cur , 3020ggml_tensor * v_cur , 3021ggml_tensor * kq_b , 3022ggml_tensor * sinks , 3023ggml_tensor * v_mla , 3024ggml_tensor * top_k , 3025float kq_scale , 3026int il )const { 3027// these nodes are added to the graph together so that they are not reordered 3028// by doing so, the number of splits in the graph is reduced 3029// expand k later to enable rope fusion which directly writes into k-v cache 3030ggml_build_forward_expand (gf ,q_cur ); 3031ggml_build_forward_expand (gf ,v_cur ); 3032ggml_build_forward_expand (gf ,k_cur ); 3033 3034const auto * mctx_cur = inp -> mctx -> get_mla (); 3035 3036// store to KV cache 3037 { 3038const auto & k_idxs = inp -> get_k_idxs_mla (); 3039 3040ggml_build_forward_expand (gf ,mctx_cur -> cpy_k (ctx0 ,k_cur ,k_idxs ,il )); 3041 } 3042 3043const auto & kq_mask = inp -> get_kq_mask_mla (); 3044 3045// prepare new kq mask - starts filled with -INFINITY 3046ggml_tensor * kq_mask_all = ggml_fill (ctx0 ,kq_mask ,- INFINITY ); 3047 3048// reshape KQ mask into tensor with rows of size 1: 3049// [n_kv, n_batch, 1, n_stream] -> [1, n_kv, n_batch, n_stream] 3050kq_mask_all = ggml_view_4d (ctx0 ,kq_mask_all ,1 ,kq_mask_all -> ne [0 ],kq_mask_all -> ne [1 ],kq_mask_all -> ne [3 ],kq_mask_all -> nb [0 ],kq_mask_all -> nb [1 ],kq_mask_all -> nb [2 ],0 ); 3051 3052// reshape top_k indices: [n_top_k, n_batch, 1, n_stream] -> [n_top_k, n_batch, n_stream, 1] 3053ggml_tensor * top_k_3d = ggml_view_4d (ctx0 ,top_k ,top_k -> ne [0 ],top_k -> ne [1 ],top_k -> ne [3 ],1 ,top_k -> nb [1 ],top_k -> nb [2 ],top_k -> ne [3 ]* top_k -> nb [3 ],0 ); 3054 3055// prepare zero-filled tensor with rows of size 1: [1, n_top_k, n_batch, n_stream] 3056// this will be our source of zero values for unmasking top k mask elements 3057ggml_tensor * zeros = ggml_new_tensor_4d (ctx0 ,GGML_TYPE_F32 ,1 ,top_k_3d -> ne [0 ],top_k_3d -> ne [1 ],top_k_3d -> ne [2 ]); 3058zeros = ggml_fill (ctx0 ,zeros ,0.0f ); 3059 3060// modify KQ mask by unmasking elements that are in top_k indices 3061// ggml_set_rows([1, n_kv, n_batch, n_stream], [1, n_top_k, n_batch, n_stream], [n_top_k, n_batch, n_stream, 1]) 3062ggml_tensor * kq_mask_top_k = ggml_set_rows (ctx0 ,kq_mask_all ,zeros ,top_k_3d ); 3063 3064// reshape to restore the original shape of KQ mask: 3065// [1, n_kv, n_batch, n_stream] -> [n_kv, n_batch, 1, n_stream] 3066kq_mask_top_k = ggml_view_4d (ctx0 ,kq_mask_top_k ,kq_mask_top_k -> ne [1 ],kq_mask_top_k -> ne [2 ],1 ,kq_mask_top_k -> ne [3 ],kq_mask_top_k -> nb [2 ],kq_mask_top_k -> nb [3 ],kq_mask_top_k -> nb [3 ],0 ); 3067 3068// combine with the original kq mask 3069kq_mask_top_k = ggml_add (ctx0 ,kq_mask_top_k ,kq_mask ); 3070 3071ggml_tensor * q = q_cur ; 3072ggml_tensor * k = mctx_cur -> get_k (ctx0 ,il ); 3073ggml_tensor * v = ggml_view_4d (ctx0 ,k ,v_cur -> ne [0 ],k -> ne [1 ],k -> ne [2 ],k -> ne [3 ],k -> nb [1 ],k -> nb [2 ],k -> nb [3 ],0 ); 3074 3075ggml_tensor * cur = build_attn_mha (q ,k ,v ,kq_b ,kq_mask_top_k ,sinks ,v_mla ,top_k -> ne [0 ],kq_scale ,il ); 3076cb (cur ,"kqv_out" ,il ); 3077 3078if (wo ) { 3079cur = build_lora_mm (wo ,cur ,wo_s ); 3080 } 3081 3082if (wo_b ) { 3083cur = ggml_add (ctx0 ,cur ,wo_b ); 3084 } 3085 3086return cur ; 3087} 3088 3089ggml_tensor * llm_graph_context::build_attn ( 3090llm_graph_input_attn_kv_iswa * inp , 3091ggml_tensor * wo , 3092ggml_tensor * wo_b , 3093ggml_tensor * wo_s , 3094ggml_tensor * q_cur , 3095ggml_tensor * k_cur , 3096ggml_tensor * v_cur , 3097ggml_tensor * kq_b , 3098ggml_tensor * sinks , 3099ggml_tensor * v_mla , 3100float kq_scale , 3101int il )const { 3102const bool is_swa = hparams .is_swa (il ); 3103 3104auto * k_rot = is_swa ?inp -> self_k_rot_swa :inp -> self_k_rot ; 3105auto * v_rot = is_swa ?inp -> self_v_rot_swa :inp -> self_v_rot ; 3106 3107if (k_rot ) { 3108q_cur = llama_mul_mat_hadamard (ctx0 ,q_cur ,k_rot ); 3109if (k_cur ) { 3110k_cur = llama_mul_mat_hadamard (ctx0 ,k_cur ,k_rot ); 3111 } 3112 } 3113if (v_rot ) { 3114if (v_cur ) { 3115v_cur = llama_mul_mat_hadamard (ctx0 ,v_cur ,v_rot ); 3116 } 3117 } 3118 3119// these nodes are added to the graph together so that they are not reordered 3120// by doing so, the number of splits in the graph is reduced 3121ggml_build_forward_expand (gf ,q_cur ); 3122 3123if (k_cur ) { 3124ggml_build_forward_expand (gf ,k_cur ); 3125 } 3126 3127if (v_cur ) { 3128ggml_build_forward_expand (gf ,v_cur ); 3129 } 3130 3131const auto * mctx_iswa = inp -> mctx ; 3132 3133const auto * mctx_cur = is_swa ?mctx_iswa -> get_swa () :mctx_iswa -> get_base (); 3134 3135// optionally store to KV cache 3136if (k_cur ) { 3137const auto & k_idxs = is_swa ?inp -> get_k_idxs_swa () :inp -> get_k_idxs (); 3138 3139ggml_build_forward_expand (gf ,mctx_cur -> cpy_k (ctx0 ,k_cur ,k_idxs ,il )); 3140 } 3141 3142if (v_cur ) { 3143const auto & v_idxs = is_swa ?inp -> get_v_idxs_swa () :inp -> get_v_idxs (); 3144 3145ggml_build_forward_expand (gf ,mctx_cur -> cpy_v (ctx0 ,v_cur ,v_idxs ,il )); 3146 } 3147 3148const auto & kq_mask = is_swa ?inp -> get_kq_mask_swa () :inp -> get_kq_mask (); 3149 3150ggml_tensor * q = q_cur ; 3151ggml_tensor * k = mctx_cur -> get_k (ctx0 ,il ); 3152ggml_tensor * v = mctx_cur -> get_v (ctx0 ,il ); 3153 3154ggml_tensor * cur = build_attn_mha (q ,k ,v ,kq_b ,kq_mask ,sinks ,v_mla ,0 ,kq_scale ,il ); 3155cb (cur ,"kqv_out" ,il ); 3156 3157if (v_rot ) { 3158cur = llama_mul_mat_hadamard (ctx0 ,cur ,v_rot ); 3159 } 3160 3161if (wo ) { 3162cur = build_lora_mm (wo ,cur ,wo_s ); 3163 } 3164 3165if (wo_b ) { 3166//cb(cur, "kqv_wo", il); 3167 } 3168 3169if (wo_b ) { 3170cur = ggml_add (ctx0 ,cur ,wo_b ); 3171 } 3172 3173return cur ; 3174} 3175 3176ggml_tensor * llm_graph_context::build_attn ( 3177llm_graph_input_attn_k_iswa * inp , 3178ggml_tensor * wo , 3179ggml_tensor * wo_b , 3180ggml_tensor * wo_s , 3181ggml_tensor * q_cur , 3182ggml_tensor * k_cur , 3183ggml_tensor * v_cur , 3184ggml_tensor * kq_b , 3185ggml_tensor * sinks , 3186ggml_tensor * v_mla , 3187float kq_scale , 3188int il )const { 3189const bool is_swa = hparams .is_swa (il ); 3190 3191auto * k_rot = is_swa ?inp -> self_k_rot_swa :inp -> self_k_rot ; 3192 3193if (k_rot ) { 3194q_cur = llama_mul_mat_hadamard (ctx0 ,q_cur ,k_rot ); 3195if (k_cur ) { 3196k_cur = llama_mul_mat_hadamard (ctx0 ,k_cur ,k_rot ); 3197 } 3198 } 3199 3200// these nodes are added to the graph together so that they are not reordered 3201// by doing so, the number of splits in the graph is reduced 3202ggml_build_forward_expand (gf ,q_cur ); 3203 3204if (k_cur ) { 3205ggml_build_forward_expand (gf ,k_cur ); 3206 } 3207 3208const auto * mctx_iswa = inp -> mctx ; 3209const auto * mctx_cur = is_swa ?mctx_iswa -> get_swa () :mctx_iswa -> get_base (); 3210 3211// optionally store to KV cache 3212if (k_cur ) { 3213const auto & k_idxs = is_swa ?inp -> get_k_idxs_swa () :inp -> get_k_idxs (); 3214 3215ggml_build_forward_expand (gf ,mctx_cur -> cpy_k (ctx0 ,k_cur ,k_idxs ,il )); 3216 } 3217 3218const auto & kq_mask = is_swa ?inp -> get_kq_mask_swa () :inp -> get_kq_mask (); 3219 3220// MLA-style attention: the cached K is used as V 3221ggml_tensor * q = q_cur ; 3222ggml_tensor * k = mctx_cur -> get_k (ctx0 ,il ); 3223ggml_tensor * v = ggml_view_4d (ctx0 ,k ,v_cur -> ne [0 ],k -> ne [1 ],k -> ne [2 ],k -> ne [3 ],k -> nb [1 ],k -> nb [2 ],k -> nb [3 ],0 ); 3224 3225ggml_tensor * cur = build_attn_mha (q ,k ,v ,kq_b ,kq_mask ,sinks ,v_mla ,0 ,kq_scale ,il ); 3226cb (cur ,"kqv_out" ,il ); 3227 3228if (k_rot ) { 3229cur = llama_mul_mat_hadamard (ctx0 ,cur ,k_rot ); 3230 } 3231 3232if (wo ) { 3233cur = build_lora_mm (wo ,cur ,wo_s ); 3234 } 3235 3236if (wo_b ) { 3237cur = ggml_add (ctx0 ,cur ,wo_b ); 3238 } 3239 3240return cur ; 3241} 3242 3243llm_graph_input_attn_cross * llm_graph_context::build_attn_inp_cross ()const { 3244auto inp = std::make_unique < llm_graph_input_attn_cross > (cross ); 3245 3246const int32_t n_enc = !cross -> v_embd .empty () ?cross -> n_enc :cparams .n_ubatch ; 3247 3248// flash attention requires an f16 mask 3249const auto type_mask = cparams .flash_attn ?GGML_TYPE_F16 :GGML_TYPE_F32 ; 3250 3251inp -> cross_kq_mask = ggml_new_tensor_4d (ctx0 ,type_mask ,n_enc ,n_tokens ,1 ,1 ); 3252ggml_set_input (inp -> cross_kq_mask ); 3253 3254inp -> cross_kq_mask_cnv = inp -> cross_kq_mask ; 3255 3256return (llm_graph_input_attn_cross * )res -> add_input (std::move (inp )); 3257} 3258 3259ggml_tensor * llm_graph_context::build_attn ( 3260llm_graph_input_attn_cross * inp , 3261ggml_tensor * wo , 3262ggml_tensor * wo_b , 3263ggml_tensor * wo_s , 3264ggml_tensor * q_cur , 3265ggml_tensor * k_cur , 3266ggml_tensor * v_cur , 3267ggml_tensor * kq_b , 3268ggml_tensor * sinks , 3269ggml_tensor * v_mla , 3270float kq_scale , 3271int il )const { 3272// these nodes are added to the graph together so that they are not reordered 3273// by doing so, the number of splits in the graph is reduced 3274ggml_build_forward_expand (gf ,q_cur ); 3275ggml_build_forward_expand (gf ,k_cur ); 3276ggml_build_forward_expand (gf ,v_cur ); 3277 3278const auto & kq_mask = inp -> get_kq_mask_cross (); 3279 3280ggml_tensor * q = q_cur ; 3281ggml_tensor * k = k_cur ; 3282ggml_tensor * v = v_cur ; 3283 3284ggml_tensor * cur = build_attn_mha (q ,k ,v ,kq_b ,kq_mask ,sinks ,v_mla ,0 ,kq_scale ,il ); 3285cb (cur ,"kqv_out" ,il ); 3286 3287if (wo ) { 3288cur = build_lora_mm (wo ,cur ,wo_s ); 3289 } 3290 3291if (wo_b ) { 3292//cb(cur, "kqv_wo", il); 3293 } 3294 3295if (wo_b ) { 3296cur = ggml_add (ctx0 ,cur ,wo_b ); 3297 } 3298 3299return cur ; 3300} 3301 3302static std::unique_ptr < llm_graph_input_attn_k_dsa > build_attn_inp_k_dsa_impl ( 3303ggml_context * ctx0 , 3304const llama_ubatch & ubatch , 3305const llama_hparams & hparams , 3306const llama_cparams & cparams , 3307const llama_kv_cache_dsa_context * mctx_cur ) { 3308 3309auto inp = std::make_unique < llm_graph_input_attn_k_dsa > (hparams ,cparams ,mctx_cur ); 3310 3311 { 3312inp -> self_k_idxs_mla = mctx_cur -> get_mla ()-> build_input_k_idxs (ctx0 ,ubatch ); 3313 3314inp -> self_kq_mask_mla = build_attn_inp_kq_mask (ctx0 ,mctx_cur -> get_mla (),ubatch ,cparams ); 3315inp -> self_kq_mask_mla_cnv = inp -> self_kq_mask_mla ; 3316 } 3317 3318 { 3319inp -> self_k_idxs_lid = mctx_cur -> get_lid ()-> build_input_k_idxs (ctx0 ,ubatch ); 3320 3321// ensure that mask type matches fused lightning indexer use (requires f16 mask) 3322auto cparams_copy = cparams ; 3323cparams_copy .flash_attn = cparams .fused_lid ; 3324 3325inp -> self_kq_mask_lid = build_attn_inp_kq_mask (ctx0 ,mctx_cur -> get_lid (),ubatch ,cparams_copy ); 3326inp -> self_kq_mask_lid_cnv = inp -> self_kq_mask_lid ; 3327 3328inp -> self_k_rot_lid = mctx_cur -> get_lid ()-> build_input_k_rot (ctx0 ); 3329 } 3330 3331return inp ; 3332} 3333 3334llm_graph_input_attn_k_dsa * llm_graph_context::build_attn_inp_k_dsa ()const { 3335const auto * mctx_cur = static_cast < const llama_kv_cache_dsa_context *> (mctx ); 3336 3337auto inp = build_attn_inp_k_dsa_impl (ctx0 ,ubatch ,hparams ,cparams ,mctx_cur ); 3338 3339return (llm_graph_input_attn_k_dsa * )res -> add_input (std::move (inp )); 3340} 3341 3342llm_graph_input_attn_k_dsa_iswa * llm_graph_context::build_attn_inp_k_dsa_iswa ()const { 3343const auto * mctx_cur = static_cast < const llama_kv_cache_dsa_iswa_context *> (mctx ); 3344 3345auto inp_dsa = build_attn_inp_k_dsa_impl (ctx0 ,ubatch ,hparams ,cparams ,mctx_cur -> get_dsa ()); 3346 3347// build_attn_inp_k_impl rejects SWA caches, so construct the input directly 3348auto inp_swa = std::make_unique < llm_graph_input_attn_k > (hparams ,cparams ,mctx_cur -> get_swa ()); 3349 3350inp_swa -> self_k_idxs = mctx_cur -> get_swa ()-> build_input_k_idxs (ctx0 ,ubatch ); 3351 3352inp_swa -> self_kq_mask = build_attn_inp_kq_mask (ctx0 ,mctx_cur -> get_swa (),ubatch ,cparams ); 3353inp_swa -> self_kq_mask_cnv = inp_swa -> self_kq_mask ; 3354 3355auto inp = std::make_unique < llm_graph_input_attn_k_dsa_iswa > (std::move (inp_dsa ), std::move (inp_swa ),mctx_cur ); 3356 3357return (llm_graph_input_attn_k_dsa_iswa * )res -> add_input (std::move (inp )); 3358} 3359 3360llm_graph_input_attn_kv_msa * llm_graph_context::build_attn_inp_kv_msa (bool msa_enabled )const { 3361const auto * mctx_cur = static_cast < const llama_kv_cache_msa_context *> (mctx ); 3362 3363auto inp = std::make_unique < llm_graph_input_attn_kv_msa > (hparams ,cparams ,mctx_cur ); 3364 3365const auto * mctx_base = mctx_cur -> get_base (); 3366const auto * mctx_idx = mctx_cur -> get_idx (); 3367 3368 { 3369GGML_ASSERT (hparams .swa_type == LLAMA_SWA_TYPE_NONE && "Use llama_kv_cache_iswa for SWA" ); 3370 3371inp -> self_k_idxs = mctx_base -> build_input_k_idxs (ctx0 ,ubatch ); 3372inp -> self_v_idxs = mctx_base -> build_input_v_idxs (ctx0 ,ubatch ); 3373 3374inp -> self_kq_mask = build_attn_inp_kq_mask (ctx0 ,mctx_base ,ubatch ,cparams ); 3375inp -> self_kq_mask_cnv = inp -> self_kq_mask ; 3376 } 3377 3378inp -> self_k_rot = mctx_base -> build_input_k_rot (ctx0 ); 3379inp -> self_v_rot = mctx_base -> build_input_v_rot (ctx0 ); 3380 3381if (msa_enabled ) { 3382inp -> self_k_idxs_idx = mctx_idx -> build_input_k_idxs (ctx0 ,ubatch ); 3383 } 3384 3385return (llm_graph_input_attn_kv_msa * )res -> add_input (std::move (inp )); 3386} 3387 3388// TODO: maybe separate the inner implementation into a separate function 3389// like with the non-sliding window equivalent 3390// once sliding-window hybrid caches are a thing. 3391llm_graph_input_attn_kv_iswa * llm_graph_context::build_attn_inp_kv_iswa ()const { 3392const auto * mctx_cur = static_cast < const llama_kv_cache_iswa_context *> (mctx ); 3393 3394auto inp = std::make_unique < llm_graph_input_attn_kv_iswa > (hparams ,cparams ,mctx_cur ); 3395 3396 { 3397inp -> self_k_idxs = mctx_cur -> get_base ()-> build_input_k_idxs (ctx0 ,ubatch ); 3398inp -> self_v_idxs = mctx_cur -> get_base ()-> build_input_v_idxs (ctx0 ,ubatch ); 3399 3400inp -> self_kq_mask = build_attn_inp_kq_mask (ctx0 ,mctx_cur -> get_base (),ubatch ,cparams ); 3401inp -> self_kq_mask_cnv = inp -> self_kq_mask ; 3402 } 3403 3404 { 3405GGML_ASSERT (hparams .swa_type != LLAMA_SWA_TYPE_NONE && "Use llama_kv_cache for non-SWA" ); 3406 3407inp -> self_k_idxs_swa = mctx_cur -> get_swa ()-> build_input_k_idxs (ctx0 ,ubatch ); 3408inp -> self_v_idxs_swa = mctx_cur -> get_swa ()-> build_input_v_idxs (ctx0 ,ubatch ); 3409 3410inp -> self_kq_mask_swa = build_attn_inp_kq_mask (ctx0 ,mctx_cur -> get_swa (),ubatch ,cparams ); 3411inp -> self_kq_mask_swa_cnv = inp -> self_kq_mask_swa ; 3412 } 3413 3414inp -> self_k_rot = mctx_cur -> get_base ()-> build_input_k_rot (ctx0 ); 3415inp -> self_v_rot = mctx_cur -> get_base ()-> build_input_v_rot (ctx0 ); 3416 3417inp -> self_k_rot_swa = mctx_cur -> get_swa ()-> build_input_k_rot (ctx0 ); 3418inp -> self_v_rot_swa = mctx_cur -> get_swa ()-> build_input_v_rot (ctx0 ); 3419 3420return (llm_graph_input_attn_kv_iswa * )res -> add_input (std::move (inp )); 3421} 3422 3423llm_graph_input_attn_k_iswa * llm_graph_context::build_attn_inp_k_iswa ()const { 3424const auto * mctx_cur = static_cast < const llama_kv_cache_iswa_context *> (mctx ); 3425 3426auto inp = std::make_unique < llm_graph_input_attn_k_iswa > (hparams ,cparams ,mctx_cur ); 3427 3428 { 3429inp -> self_k_idxs = mctx_cur -> get_base ()-> build_input_k_idxs (ctx0 ,ubatch ); 3430 3431inp -> self_kq_mask = build_attn_inp_kq_mask (ctx0 ,mctx_cur -> get_base (),ubatch ,cparams ); 3432inp -> self_kq_mask_cnv = inp -> self_kq_mask ; 3433 } 3434 3435 { 3436GGML_ASSERT (hparams .swa_type != LLAMA_SWA_TYPE_NONE && "Use llama_kv_cache for non-SWA" ); 3437 3438inp -> self_k_idxs_swa = mctx_cur -> get_swa ()-> build_input_k_idxs (ctx0 ,ubatch ); 3439 3440inp -> self_kq_mask_swa = build_attn_inp_kq_mask (ctx0 ,mctx_cur -> get_swa (),ubatch ,cparams ); 3441inp -> self_kq_mask_swa_cnv = inp -> self_kq_mask_swa ; 3442 } 3443 3444inp -> self_k_rot = mctx_cur -> get_base ()-> build_input_k_rot (ctx0 ); 3445 3446inp -> self_k_rot_swa = mctx_cur -> get_swa ()-> build_input_k_rot (ctx0 ); 3447 3448return (llm_graph_input_attn_k_iswa * )res -> add_input (std::move (inp )); 3449} 3450 3451llm_graph_input_dsv4 * llm_graph_context::build_inp_dsv4 ()const { 3452const auto * mctx_cur = static_cast < const llama_kv_cache_dsv4_context *> (mctx ); 3453const auto * raw_ctx = mctx_cur -> get_raw (); 3454 3455auto inp_raw = std::make_unique < llm_graph_input_dsv4_raw > (cparams ,raw_ctx ); 3456 3457const int64_t n_stream = mctx_cur -> get_csa_plan (ubatch ).n_stream ; 3458 3459GGML_ASSERT (hparams .swa_type != LLAMA_SWA_TYPE_NONE && "DSV4 expects SWA raw cache" ); 3460 3461inp_raw -> self_k_idxs = raw_ctx -> build_input_k_idxs (ctx0 ,ubatch ); 3462inp_raw -> self_kq_mask = dsv4_build_raw_kq_mask (ctx0 ,raw_ctx ,ubatch ,cparams ,n_stream ); 3463inp_raw -> self_kq_mask_cnv = inp_raw -> self_kq_mask ; 3464 3465inp_raw -> self_k_rot = raw_ctx -> build_input_k_rot (ctx0 ); 3466auto inp = std::make_unique < llm_graph_input_dsv4 > (cparams , std::move (inp_raw ),mctx_cur ); 3467 3468dsv4_build_comp_inputs (ctx0 ,inp -> inp_csa ,mctx_cur -> get_csa_plan (ubatch ),"csa" ,cparams ,n_stream ); 3469dsv4_build_comp_inputs (ctx0 ,inp -> inp_hca ,mctx_cur -> get_hca_plan (ubatch ),"hca" ,cparams ,n_stream ); 3470dsv4_build_comp_inputs (ctx0 ,inp -> inp_lid ,mctx_cur -> get_lid_plan (ubatch ),"lid" ,cparams ,n_stream ); 3471inp -> inp_csa .k_rot = mctx_cur -> get_csa ()-> build_input_k_rot (ctx0 ); 3472inp -> inp_hca .k_rot = mctx_cur -> get_hca ()-> build_input_k_rot (ctx0 ); 3473inp -> inp_lid .k_rot = mctx_cur -> get_lid ()-> build_input_k_rot (ctx0 ); 3474 3475return (llm_graph_input_dsv4 * )res -> add_input (std::move (inp )); 3476} 3477 3478ggml_tensor * llm_graph_context::build_rs ( 3479ggml_tensor * s , 3480ggml_tensor * state_copy_main , 3481ggml_tensor * state_copy_extra , 3482int32_t state_size , 3483int32_t n_seqs , 3484uint32_t n_rs , 3485uint32_t rs_head , 3486uint32_t rs_size , 3487int32_t rs_zero , 3488const llm_graph_get_rows_fn & get_state_rows )const { 3489 3490GGML_UNUSED (rs_size ); 3491ggml_tensor * states = ggml_reshape_2d (ctx0 ,s ,state_size ,s -> ne [1 ]); 3492 3493// Clear a single state which will then be copied to the other cleared states. 3494// Note that this is a no-op when the view is zero-sized. 3495ggml_tensor * state_zero = ggml_view_1d (ctx0 ,states ,state_size * (rs_zero >=0 ),rs_zero * states -> nb [1 ]* (rs_zero >=0 )); 3496ggml_build_forward_expand (gf ,ggml_scale_inplace (ctx0 ,state_zero ,0 )); 3497 3498// copy states 3499// NOTE: assuming the copy destinations are ALL contained between rs_head and rs_head + n_rs 3500// {state_size, rs_size} -> {state_size, n_seqs} 3501ggml_tensor * output_states = get_state_rows (ctx0 ,states ,state_copy_main ); 3502ggml_build_forward_expand (gf ,output_states ); 3503 3504// copy extra states which won't be changed further (between n_seqs and n_rs) 3505ggml_tensor * states_extra = ggml_get_rows (ctx0 ,states ,state_copy_extra ); 3506ggml_build_forward_expand (gf , 3507ggml_cpy (ctx0 , 3508states_extra , 3509ggml_view_2d (ctx0 ,s ,state_size , (n_rs - n_seqs ),s -> nb [1 ], (rs_head + n_seqs )* s -> nb [1 ]))); 3510 3511return output_states ; 3512} 3513 3514static std::unique_ptr < llm_graph_input_rs > build_rs_inp_impl ( 3515ggml_context * ctx0 , 3516const llama_ubatch & ubatch , 3517const llama_memory_recurrent_context * mctx_cur ) { 3518 3519auto inp = std::make_unique < llm_graph_input_rs > (mctx_cur ); 3520 3521const int64_t n_rs = mctx_cur -> get_n_rs (); 3522const int64_t n_seqs = ubatch .n_seqs ; 3523 3524inp -> s_copy = ggml_new_tensor_1d (ctx0 ,GGML_TYPE_I32 ,n_rs ); 3525ggml_set_input (inp -> s_copy ); 3526 3527inp -> s_copy_main = ggml_view_1d (ctx0 ,inp -> s_copy ,n_seqs ,0 ); 3528inp -> s_copy_extra = ggml_view_1d (ctx0 ,inp -> s_copy ,n_rs - n_seqs ,n_seqs * inp -> s_copy -> nb [0 ]); 3529 3530inp -> head = mctx_cur -> get_head (); 3531inp -> rs_z = mctx_cur -> get_rs_z (); 3532 3533return inp ; 3534} 3535 3536llm_graph_input_rs * llm_graph_context::build_rs_inp ()const { 3537const auto * mctx_cur = static_cast < const llama_memory_recurrent_context *> (mctx ); 3538 3539auto inp = build_rs_inp_impl (ctx0 ,ubatch ,mctx_cur ); 3540 3541return (llm_graph_input_rs * )res -> add_input (std::move (inp )); 3542} 3543 3544ggml_tensor * llm_graph_context::build_rs ( 3545llm_graph_input_rs * inp , 3546ggml_tensor * s , 3547int32_t state_size , 3548int32_t n_seqs , 3549const llm_graph_get_rows_fn & get_state_rows )const { 3550const auto * kv_state = inp -> mctx ; 3551 3552return build_rs (s ,inp -> s_copy_main ,inp -> s_copy_extra ,state_size ,n_seqs , 3553kv_state -> get_n_rs (),kv_state -> get_head (),kv_state -> get_size (),kv_state -> get_rs_z (), 3554get_state_rows ); 3555} 3556 3557ggml_tensor * llm_graph_context::build_rwkv_token_shift_load ( 3558llm_graph_input_rs * inp , 3559const llama_ubatch & ubatch , 3560int il )const { 3561const auto * mctx_cur = static_cast < const llama_memory_recurrent_context *> (mctx ); 3562 3563const auto token_shift_count = hparams .token_shift_count ; 3564 3565const int64_t n_seqs = ubatch .n_seqs ; 3566 3567ggml_tensor * token_shift_all = mctx_cur -> get_r_l (il ); 3568 3569ggml_tensor * token_shift = build_rs ( 3570inp ,token_shift_all , 3571hparams .n_embd_r (),n_seqs ); 3572 3573token_shift = ggml_reshape_3d (ctx0 ,token_shift ,hparams .n_embd ,token_shift_count ,n_seqs ); 3574 3575return token_shift ; 3576} 3577 3578ggml_tensor * llm_graph_context::build_rwkv_token_shift_store ( 3579ggml_tensor * token_shift , 3580const llama_ubatch & ubatch , 3581int il )const { 3582const auto * mctx_cur = static_cast < const llama_memory_recurrent_context *> (mctx ); 3583 3584const auto token_shift_count = hparams .token_shift_count ; 3585const auto n_embd = hparams .n_embd ; 3586 3587const int64_t n_seqs = ubatch .n_seqs ; 3588 3589const auto kv_head = mctx_cur -> get_head (); 3590 3591return ggml_cpy ( 3592ctx0 , 3593ggml_view_1d (ctx0 ,token_shift ,n_embd * n_seqs * token_shift_count ,0 ), 3594ggml_view_1d (ctx0 ,mctx_cur -> get_r_l (il ),hparams .n_embd_r ()* n_seqs ,hparams .n_embd_r ()* kv_head * ggml_element_size (mctx_cur -> get_r_l (il ))) 3595 ); 3596} 3597 3598llm_graph_input_mem_hybrid * llm_graph_context::build_inp_mem_hybrid ()const { 3599const auto * mctx_cur = static_cast < const llama_memory_hybrid_context *> (mctx ); 3600 3601auto inp_rs = build_rs_inp_impl (ctx0 ,ubatch ,mctx_cur -> get_recr ()); 3602auto inp_attn = build_attn_inp_kv_impl (ctx0 ,ubatch ,hparams ,cparams ,mctx_cur -> get_attn ()); 3603 3604auto inp = std::make_unique < llm_graph_input_mem_hybrid > (cparams , std::move (inp_attn ), std::move (inp_rs ),mctx_cur ); 3605 3606return (llm_graph_input_mem_hybrid * )res -> add_input (std::move (inp )); 3607} 3608 3609llm_graph_input_mem_hybrid_k * llm_graph_context::build_inp_mem_hybrid_k ()const { 3610const auto * mctx_cur = static_cast < const llama_memory_hybrid_context *> (mctx ); 3611 3612auto inp_rs = build_rs_inp_impl (ctx0 ,ubatch ,mctx_cur -> get_recr ()); 3613auto inp_attn = build_attn_inp_k_impl (ctx0 ,ubatch ,hparams ,cparams ,mctx_cur -> get_attn ()); 3614 3615auto inp = std::make_unique < llm_graph_input_mem_hybrid_k > (cparams , std::move (inp_attn ), std::move (inp_rs ),mctx_cur ); 3616 3617return (llm_graph_input_mem_hybrid_k * )res -> add_input (std::move (inp )); 3618} 3619 3620llm_graph_input_mem_hybrid_iswa * llm_graph_context::build_inp_mem_hybrid_iswa ()const { 3621const auto * mctx_cur = static_cast < const llama_memory_hybrid_iswa_context *> (mctx ); 3622 3623auto inp_rs = build_rs_inp_impl (ctx0 ,ubatch ,mctx_cur -> get_recr ()); 3624 3625// build iswa attention input 3626const auto * attn_ctx = mctx_cur -> get_attn (); 3627 3628auto inp_attn = std::make_unique < llm_graph_input_attn_kv_iswa > (hparams ,cparams ,attn_ctx ); 3629 3630 { 3631inp_attn -> self_k_idxs = attn_ctx -> get_base ()-> build_input_k_idxs (ctx0 ,ubatch ); 3632inp_attn -> self_v_idxs = attn_ctx -> get_base ()-> build_input_v_idxs (ctx0 ,ubatch ); 3633 3634inp_attn -> self_kq_mask = build_attn_inp_kq_mask (ctx0 ,attn_ctx -> get_base (),ubatch ,cparams ); 3635inp_attn -> self_kq_mask_cnv = inp_attn -> self_kq_mask ; 3636 } 3637 3638 { 3639inp_attn -> self_k_idxs_swa = attn_ctx -> get_swa ()-> build_input_k_idxs (ctx0 ,ubatch ); 3640inp_attn -> self_v_idxs_swa = attn_ctx -> get_swa ()-> build_input_v_idxs (ctx0 ,ubatch ); 3641 3642inp_attn -> self_kq_mask_swa = build_attn_inp_kq_mask (ctx0 ,attn_ctx -> get_swa (),ubatch ,cparams ); 3643inp_attn -> self_kq_mask_swa_cnv = inp_attn -> self_kq_mask_swa ; 3644 } 3645 3646auto inp = std::make_unique < llm_graph_input_mem_hybrid_iswa > (cparams , std::move (inp_attn ), std::move (inp_rs ),mctx_cur ); 3647 3648return (llm_graph_input_mem_hybrid_iswa * )res -> add_input (std::move (inp )); 3649} 3650 3651void llm_graph_context::build_dense_out ( 3652ggml_tensor * dense_2 , 3653ggml_tensor * dense_2_b , 3654ggml_tensor * dense_3 )const { 3655if (!cparams .embeddings || !(dense_2 || dense_2_b || dense_3 )) { 3656return ; 3657 } 3658ggml_tensor * cur = res -> t_embd_pooled != nullptr ?res -> t_embd_pooled :res -> t_embd ; 3659GGML_ASSERT (cur != nullptr && "missing t_embd_pooled/t_embd" ); 3660 3661if (dense_2 ) { 3662cur = ggml_mul_mat (ctx0 ,dense_2 ,cur ); 3663 } 3664if (dense_2_b ) { 3665cur = ggml_add (ctx0 ,cur ,dense_2_b ); 3666 } 3667if (dense_3 ) { 3668cur = ggml_mul_mat (ctx0 ,dense_3 ,cur ); 3669 } 3670cb (cur ,"result_embd_pooled" ,-1 ); 3671res -> t_embd_pooled = cur ; 3672ggml_build_forward_expand (gf ,cur ); 3673} 3674 3675 3676void llm_graph_context::build_pooling ( 3677ggml_tensor * cls , 3678ggml_tensor * cls_b , 3679ggml_tensor * cls_out , 3680ggml_tensor * cls_out_b , 3681ggml_tensor * cls_norm )const { 3682if (!cparams .embeddings ) { 3683return ; 3684 } 3685 3686ggml_tensor * inp = res -> t_embd ; 3687 3688//// find result_norm tensor for input 3689//for (int i = ggml_graph_n_nodes(gf) - 1; i >= 0; --i) { 3690// inp = ggml_graph_node(gf, i); 3691// if (strcmp(inp->name, "result_norm") == 0 || strcmp(inp->name, "result_embd") == 0) { 3692// break; 3693// } 3694 3695// inp = nullptr; 3696//} 3697 3698GGML_ASSERT (inp != nullptr && "missing result_norm/result_embd tensor" ); 3699 3700ggml_tensor * cur ; 3701 3702switch (pooling_type ) { 3703case LLAMA_POOLING_TYPE_NONE : 3704 { 3705cur = inp ; 3706 }break ; 3707case LLAMA_POOLING_TYPE_MEAN : 3708 { 3709ggml_tensor * inp_mean = build_inp_mean (); 3710cur = ggml_mul_mat (ctx0 ,ggml_cont (ctx0 ,ggml_transpose (ctx0 ,inp )),inp_mean ); 3711 }break ; 3712case LLAMA_POOLING_TYPE_CLS : 3713case LLAMA_POOLING_TYPE_LAST : 3714 { 3715ggml_tensor * inp_cls = build_inp_cls (); 3716cur = ggml_get_rows (ctx0 ,inp ,inp_cls ); 3717 }break ; 3718case LLAMA_POOLING_TYPE_RANK : 3719 { 3720if (arch == LLM_ARCH_MODERN_BERT ) { 3721// modern bert gte reranker builds mean first then applies prediction head and classifier 3722// https://github.com/huggingface/transformers/blob/main/src/transformers/models/modernbert/modular_modernbert.py#L1404-1411 3723ggml_tensor * inp_mean = build_inp_mean (); 3724cur = ggml_mul_mat (ctx0 ,ggml_cont (ctx0 ,ggml_transpose (ctx0 ,inp )),inp_mean ); 3725 }else { 3726ggml_tensor * inp_cls = build_inp_cls (); 3727cur = ggml_get_rows (ctx0 ,inp ,inp_cls ); 3728 } 3729 3730// classification head 3731// https://github.com/huggingface/transformers/blob/5af7d41e49bbfc8319f462eb45253dcb3863dfb7/src/transformers/models/roberta/modeling_roberta.py#L1566 3732if (cls ) { 3733cur = ggml_mul_mat (ctx0 ,cls ,cur ); 3734if (cls_b ) { 3735cur = ggml_add (ctx0 ,cur ,cls_b ); 3736 } 3737if (arch == LLM_ARCH_MODERN_BERT ) { 3738cur = ggml_gelu (ctx0 ,cur ); 3739 }else { 3740cur = ggml_tanh (ctx0 ,cur ); 3741 } 3742if (cls_norm ) { 3743// head norm 3744cur = build_norm (cur ,cls_norm ,NULL ,LLM_NORM ,-1 ); 3745 } 3746 } 3747 3748// some models don't have `cls_out`, for example: https://huggingface.co/jinaai/jina-reranker-v1-tiny-en 3749// https://huggingface.co/jinaai/jina-reranker-v1-tiny-en/blob/cb5347e43979c3084a890e3f99491952603ae1b7/modeling_bert.py#L884-L896 3750// Single layer classification head (direct projection) 3751// https://github.com/huggingface/transformers/blob/f4fc42216cd56ab6b68270bf80d811614d8d59e4/src/transformers/models/bert/modeling_bert.py#L1476 3752if (cls_out ) { 3753cur = ggml_mul_mat (ctx0 ,cls_out ,cur ); 3754if (cls_out_b ) { 3755cur = ggml_add (ctx0 ,cur ,cls_out_b ); 3756 } 3757 } 3758 3759// softmax for qwen3 reranker 3760if (arch == LLM_ARCH_QWEN3 || arch == LLM_ARCH_QWEN3VL ) { 3761cur = ggml_soft_max (ctx0 ,cur ); 3762 } 3763 }break ; 3764default : 3765 { 3766GGML_ABORT ("unknown pooling type" ); 3767 } 3768 } 3769 3770cb (cur ,"result_embd_pooled" ,-1 ); 3771res -> t_embd_pooled = cur ; 3772 3773ggml_build_forward_expand (gf ,cur ); 3774} 3775 3776void llm_graph_context::build_sampling ()const { 3777if (samplers .empty ()|| !res -> t_logits ) { 3778return ; 3779 } 3780 3781 std::array < ggml_tensor * ,2 > outs ; 3782outs [0 ]= res -> t_logits ; 3783 3784auto inp_sampling = std::make_unique < llm_graph_input_sampling > (samplers ); 3785res -> add_input (std::move (inp_sampling )); 3786 3787 std::map < llama_seq_id , std::vector < uint32_t >> sampling_rows ; 3788uint32_t n_rows = 0 ; 3789for (uint32_t i = 0 ;i < ubatch .n_tokens ;++ i ) { 3790if (ubatch .output [i ]) { 3791sampling_rows [ubatch .seq_id [i ][0 ]].push_back (n_rows ++ ); 3792 } 3793 } 3794 3795res -> t_sampled .resize (n_rows ,nullptr ); 3796res -> t_sampled_probs .resize (n_rows ,nullptr ); 3797res -> t_sampled_logits .resize (n_rows ,nullptr ); 3798res -> t_candidates .resize (n_rows ,nullptr ); 3799 3800// res->t_logits will contain logits for all tokens that want the logits calculated (logits=1 or output=1) 3801GGML_ASSERT (res -> t_logits != nullptr && "missing t_logits tensor" ); 3802 3803// add a dummy row to keep the single-output graph static regardless of active samplers 3804// multi-output graphs can still vary with the number of output rows 3805ggml_tensor * logits_t = ggml_pad (ctx0 ,res -> t_logits ,0 ,1 ,0 ,0 ); 3806 3807for (const auto & entry :samplers ) { 3808if (entry .second -> iface -> backend_reset ) { 3809entry .second -> iface -> backend_reset (entry .second ); 3810 } 3811 } 3812 3813static const std::vector < uint32_t > dummy_row = {0 }; 3814 3815for (const auto & [seq_id ,sampler ] :samplers ) { 3816const auto it = sampling_rows .find (seq_id ); 3817 3818// inactive samplers always work on the first row 3819const bool active = it != sampling_rows .end (); 3820const auto & rows = active ?it -> second :dummy_row ; 3821const int i_out = active ?1 :0 ; 3822 3823for (uint32_t i = 0 ;i < rows .size ();++ i ) { 3824ggml_tensor * logits_seq = ggml_view_1d (ctx0 ,logits_t ,logits_t -> ne [0 ],rows [i ]* logits_t -> nb [1 ]); 3825ggml_format_name (logits_seq ,"logits_seq_%d_%u" ,seq_id ,i ); 3826 3827struct llama_sampler_data data = { 3828/*.logits =*/ logits_seq , 3829/*.probs =*/ nullptr , 3830/*.sampled =*/ nullptr , 3831/*.candidates =*/ nullptr , 3832 }; 3833 3834assert (sampler -> iface -> backend_apply ); 3835sampler -> iface -> backend_apply (sampler ,ctx0 ,gf ,& data ); 3836 3837if (data .sampled != nullptr ) { 3838if (active ) { 3839res -> t_sampled [rows [i ]]= data .sampled ; 3840 } 3841outs [1 ]= data .sampled ; 3842ggml_build_forward_select (gf ,outs .data (),outs .size (),i_out ); 3843 } 3844 3845if (data .probs != nullptr ) { 3846if (active ) { 3847res -> t_sampled_probs [rows [i ]]= data .probs ; 3848 } 3849outs [1 ]= data .probs ; 3850ggml_build_forward_select (gf ,outs .data (),outs .size (),i_out ); 3851 } 3852 3853if (data .logits != nullptr ) { 3854if (active ) { 3855res -> t_sampled_logits [rows [i ]]= data .logits ; 3856 } 3857outs [1 ]= data .logits ; 3858ggml_build_forward_select (gf ,outs .data (),outs .size (),i_out ); 3859 } 3860 3861if (data .candidates != nullptr ) { 3862if (active ) { 3863res -> t_candidates [rows [i ]]= data .candidates ; 3864 } 3865outs [1 ]= data .candidates ; 3866ggml_build_forward_select (gf ,outs .data (),outs .size (),i_out ); 3867 } 3868 } 3869 } 3870 3871// TODO: Call backend_accept after all samplers have been applied. 3872/* 3873for (const auto & [seq_id, sampler] : samplers) { 3874const auto it = sampling_rows.find(seq_id); 3875if (it == sampling_rows.end()) { 3876continue; 3877} 3878 3879for (uint32_t row : it->second) { 3880ggml_tensor * selected_token = res->t_sampled[row]; 3881if (selected_token != nullptr && sampler->iface->backend_accept) { 3882sampler->iface->backend_accept(sampler, ctx0, gf, selected_token); 3883} 3884} 3885} 3886*/ 3887} 3888 3889int32_t llama_relative_position_bucket (llama_pos x ,llama_pos y ,uint64_t n_buckets ,bool bidirectional ) { 3890// TODO move to hparams if a T5 variant appears that uses a different value 3891const int64_t max_distance = 128 ; 3892 3893if (bidirectional ) { 3894n_buckets >>=1 ; 3895 } 3896 3897const int64_t max_exact = n_buckets >>1 ; 3898 3899int32_t relative_position = x - y ; 3900int32_t relative_bucket = 0 ; 3901 3902if (bidirectional ) { 3903relative_bucket += (relative_position > 0 )* n_buckets ; 3904relative_position = std::abs (relative_position ); 3905 }else { 3906relative_position = - std::min < int32_t > (relative_position ,0 ); 3907 } 3908 3909int32_t relative_position_if_large = floorf (max_exact + logf (1.0 * relative_position /max_exact )* (n_buckets - max_exact ) /log (1.0 * max_distance /max_exact )); 3910relative_position_if_large = std::min < int32_t > (relative_position_if_large ,n_buckets - 1 ); 3911relative_bucket += (relative_position < max_exact ?relative_position :relative_position_if_large ); 3912 3913return relative_bucket ; 3914}