char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
98a01afd9
char/aliceai-t5-moe
1#pragma once 2 3#include "llama-arch.h" 4#include "llama-batch.h" 5#include "llama-hparams.h" 6#include "llama-adapter.h" 7 8#include <cstdint> 9#include <cstdlib> 10#include <vector> 11#include <memory> 12#include <set> 13#include <functional> 14#include <map> 15 16struct ggml_cgraph ; 17struct ggml_context ; 18struct ggml_tensor ; 19 20struct llama_cparams ; 21struct llama_layer ; 22 23struct llama_memory_context_i ; 24 25class llama_kv_cache_context ; 26class llama_kv_cache_dsa_context ; 27class llama_kv_cache_dsa_iswa_context ; 28class llama_kv_cache_msa_context ; 29class llama_kv_cache_dsv4_raw_context ; 30class llama_kv_cache_dsv4_context ; 31class llama_kv_cache_iswa_context ; 32class llama_memory_recurrent_context ; 33class llama_memory_hybrid_context ; 34class llama_memory_hybrid_iswa_context ; 35 36// certain models (typically multi-modal) can produce different types of graphs 37enum llm_graph_type { 38LLM_GRAPH_TYPE_DEFAULT , 39LLM_GRAPH_TYPE_ENCODER , 40LLM_GRAPH_TYPE_DECODER , 41LLM_GRAPH_TYPE_DECODER_MTP , 42}; 43 44enum llm_fused_op { 45LLM_FUSED_OP_FLASH_ATTN , 46LLM_FUSED_OP_GDN_AR , 47LLM_FUSED_OP_GDN_CH , 48LLM_FUSED_OP_LIGHTNING_INDEXER , 49LLM_FUSED_OP_DSV4_HC_PRE , 50LLM_FUSED_OP_DSV4_HC_COMB , 51LLM_FUSED_OP_DSV4_HC_POST , 52}; 53 54enum llm_ffn_op_type :int { 55LLM_FFN_NONE = 0 ,// sentinel: unset; archs must assign before use 56LLM_FFN_SILU , 57LLM_FFN_GELU , 58LLM_FFN_RELU , 59LLM_FFN_RELU_SQR , 60LLM_FFN_SWIGLU , 61LLM_FFN_GEGLU , 62LLM_FFN_REGLU , 63LLM_FFN_SWIGLU_OAI_MOE , 64LLM_FFN_SITU ,// kimi-k3 65}; 66 67enum llm_ffn_gate_type { 68LLM_FFN_SEQ , 69LLM_FFN_PAR ,// ffn_gate is parallel to ffn_up 70}; 71 72enum llm_norm_type { 73LLM_NORM , 74LLM_NORM_RMS , 75LLM_NORM_GROUP , 76}; 77 78// TODO: tmp - need something better to pass the data from the encoder to the decoder 79struct llama_cross { 80// the output embeddings from the encoder as a ggml tensor 81// TODO: this needs more work to be correct, for now copy the embeddings data to host memory 82// ref: https://github.com/ggml-org/llama.cpp/pull/11213#discussion_r1969892524 83//ggml_tensor * t_embd = nullptr; 84 85int64_t n_embd = 0 ; 86int64_t n_enc = 0 ; 87 88// embeddings data copied to host memory (tmp) 89std ::vector < float > v_embd ; 90 91// needed to construct the cross-attention mask in the decoder 92std ::vector < std ::set < llama_seq_id >>seq_ids_enc ; 93}; 94 95struct llm_graph_params ; 96 97// 98// llm_graph_input 99// 100 101class llm_graph_input_i { 102public : 103llm_graph_input_i () { 104const char * LLAMA_GRAPH_INPUT_DEBUG = getenv ("LLAMA_GRAPH_INPUT_DEBUG" ); 105debug = LLAMA_GRAPH_INPUT_DEBUG ?atoi (LLAMA_GRAPH_INPUT_DEBUG ) :0 ; 106 } 107 108virtual ~llm_graph_input_i ()= default ; 109 110virtual void set_input (const llama_ubatch * ubatch )= 0 ; 111 112// return true if the resulting input tensors using the provided graph parameters would be 113// the same as the previous input tensors that we have currently stored in the object 114virtual bool can_reuse (const llm_graph_params & params ) { 115// returning false here by default will prevent from reusing the graph if the check 116// for the input type has not been implemented yet 117GGML_UNUSED (params ); 118return false; 119 } 120protected : 121// env: LLAMA_GRAPH_INPUT_DEBUG 122int debug = 0 ; 123}; 124 125using llm_graph_input_ptr = std ::unique_ptr < llm_graph_input_i > ; 126 127class llm_graph_input_embd :public llm_graph_input_i { 128public : 129llm_graph_input_embd (int64_t n_embd ) :n_embd (n_embd ) {} 130virtual ~llm_graph_input_embd ()= default ; 131 132void set_input (const llama_ubatch * ubatch )override ; 133 134bool can_reuse (const llm_graph_params & params )override ; 135 136ggml_tensor * tokens = nullptr ;// I32 [n_batch] 137ggml_tensor * embd = nullptr ;// F32 [n_embd, n_batch] 138 139const int64_t n_embd = 0 ; 140}; 141 142// similar to llm_graph_input_embd but with an additional hidden state input 143class llm_graph_input_embd_h :public llm_graph_input_i { 144public : 145llm_graph_input_embd_h (int64_t n_embd ) :n_embd (n_embd ) {} 146virtual ~llm_graph_input_embd_h ()= default ; 147 148void set_input (const llama_ubatch * ubatch )override ; 149 150bool can_reuse (const llm_graph_params & params )override ; 151 152ggml_tensor * tokens = nullptr ;// I32 [n_batch] 153ggml_tensor * embd = nullptr ;// F32 [n_embd, n_batch] 154ggml_tensor * h = nullptr ;// F32 [n_embd, n_batch] 155 156const int64_t n_embd = 0 ; 157}; 158 159class llm_graph_input_pos :public llm_graph_input_i { 160public : 161llm_graph_input_pos (uint32_t n_pos_per_embd ) :n_pos_per_embd (n_pos_per_embd ) {} 162virtual ~llm_graph_input_pos ()= default ; 163 164void set_input (const llama_ubatch * ubatch )override ; 165 166bool can_reuse (const llm_graph_params & params )override ; 167 168ggml_tensor * pos = nullptr ;// I32 [n_batch] 169 170const uint32_t n_pos_per_embd = 1 ; 171}; 172 173// temperature tuning, used by llama4 174class llm_graph_input_attn_temp :public llm_graph_input_i { 175public : 176llm_graph_input_attn_temp (uint32_t n_attn_temp_floor_scale ,float f_attn_temp_scale ,float f_attn_temp_offset ) 177 :n_attn_temp_floor_scale (n_attn_temp_floor_scale ),f_attn_temp_scale (f_attn_temp_scale ),f_attn_temp_offset (f_attn_temp_offset ) {} 178virtual ~llm_graph_input_attn_temp ()= default ; 179 180void set_input (const llama_ubatch * ubatch )override ; 181 182ggml_tensor * attn_scale = nullptr ;// F32 [n_batch] 183 184const uint32_t n_attn_temp_floor_scale ; 185const float f_attn_temp_scale ; 186const float f_attn_temp_offset ; 187}; 188 189class llm_graph_input_pos_bucket :public llm_graph_input_i { 190public : 191llm_graph_input_pos_bucket (const llama_hparams & hparams ) :hparams (hparams ) {} 192virtual ~llm_graph_input_pos_bucket ()= default ; 193 194void set_input (const llama_ubatch * ubatch )override ; 195 196ggml_tensor * pos_bucket = nullptr ;// I32 [n_batch, n_batch] 197 198const llama_hparams hparams ; 199}; 200 201class llm_graph_input_pos_bucket_kv :public llm_graph_input_i { 202public : 203llm_graph_input_pos_bucket_kv ( 204const llama_hparams & hparams , 205const llama_kv_cache_context * mctx ) :hparams (hparams ),mctx (mctx ) {} 206virtual ~llm_graph_input_pos_bucket_kv ()= default ; 207 208void set_input (const llama_ubatch * ubatch )override ; 209 210ggml_tensor * pos_bucket = nullptr ;// I32 [n_kv, n_batch] 211 212const llama_hparams hparams ; 213 214const llama_kv_cache_context * mctx ; 215}; 216 217class llm_graph_input_out_ids :public llm_graph_input_i { 218public : 219llm_graph_input_out_ids ( 220const llama_hparams & hparams , 221const llama_cparams & cparams , 222uint32_t n_outputs ) :hparams (hparams ),cparams (cparams ),n_outputs (n_outputs ) {} 223virtual ~llm_graph_input_out_ids ()= default ; 224 225void set_input (const llama_ubatch * ubatch )override ; 226 227bool can_reuse (const llm_graph_params & params )override ; 228 229ggml_tensor * out_ids ;// I32 [n_outputs] 230 231const llama_hparams hparams ; 232const llama_cparams cparams ; 233 234const uint32_t n_outputs ; 235}; 236 237class llm_graph_input_mean :public llm_graph_input_i { 238public : 239llm_graph_input_mean (const llama_cparams & cparams ) :cparams (cparams ) {} 240virtual ~llm_graph_input_mean ()= default ; 241 242void set_input (const llama_ubatch * ubatch )override ; 243 244ggml_tensor * mean ;// F32 [n_batch, n_batch] 245 246const llama_cparams cparams ; 247}; 248 249class llm_graph_input_cls :public llm_graph_input_i { 250public : 251llm_graph_input_cls (const llama_cparams & cparams ,const llm_arch arch ) :cparams (cparams ),arch (arch ) {} 252virtual ~llm_graph_input_cls ()= default ; 253 254void set_input (const llama_ubatch * ubatch )override ; 255 256ggml_tensor * cls ;// I32 [n_batch] 257 258const llama_cparams cparams ; 259const llm_arch arch ; 260}; 261 262class llm_graph_input_rs :public llm_graph_input_i { 263public : 264llm_graph_input_rs (const llama_memory_recurrent_context * mctx ) :mctx (mctx ) {} 265virtual ~llm_graph_input_rs ()= default ; 266 267void set_input (const llama_ubatch * ubatch )override ; 268 269bool can_reuse (const llm_graph_params & params )override ; 270 271ggml_tensor * s_copy ;// I32 [n_rs] 272 273// views of s_copy, computed once per graph 274// and shared across layers which use build_rs 275ggml_tensor * s_copy_main ;// I32 [n_seqs] 276ggml_tensor * s_copy_extra ;// I32 [n_rs - n_seqs] 277 278const llama_memory_recurrent_context * mctx ; 279 280// used in view offsets, need to match for valid graph reuse 281uint32_t head ; 282int32_t rs_z ; 283}; 284 285class llm_graph_input_cross_embd :public llm_graph_input_i { 286public : 287llm_graph_input_cross_embd ( 288const llama_cross * cross ) :cross (cross ) {} 289virtual ~llm_graph_input_cross_embd ()= default ; 290 291void set_input (const llama_ubatch * ubatch )override ; 292 293bool can_reuse (const llm_graph_params & params )override ; 294 295ggml_tensor * cross_embd ;// F32 [n_embd, n_outputs_enc] 296 297const llama_cross * cross ; 298}; 299 300class llm_graph_input_attn_no_cache :public llm_graph_input_i { 301public : 302llm_graph_input_attn_no_cache (const llama_hparams & hparams ,const llama_cparams & cparams ) : 303hparams (hparams ), 304cparams (cparams ) { 305 } 306 ~llm_graph_input_attn_no_cache ()= default ; 307 308void set_input (const llama_ubatch * ubatch )override ; 309 310ggml_tensor * get_kq_mask ()const {return self_kq_mask_cnv ; } 311ggml_tensor * get_kq_mask_swa ()const {return self_kq_mask_swa_cnv ; } 312 313// n_tokens == n_batch 314ggml_tensor * self_kq_mask = nullptr ;// F32/F16 [n_tokens, n_batch/n_stream, 1, n_stream] 315ggml_tensor * self_kq_mask_cnv = nullptr ;// [n_tokens, n_batch/n_stream, 1, n_stream] 316ggml_tensor * self_kq_mask_swa = nullptr ;// F32/F16 [n_tokens, n_batch/n_stream, 1, n_stream] 317ggml_tensor * self_kq_mask_swa_cnv = nullptr ;// [n_tokens, n_batch/n_stream, 1, n_stream] 318 319const llama_hparams hparams ; 320const llama_cparams cparams ; 321}; 322 323class llm_graph_input_attn_kv :public llm_graph_input_i { 324public : 325llm_graph_input_attn_kv ( 326const llama_hparams & hparams , 327const llama_cparams & cparams , 328const llama_kv_cache_context * mctx ) : 329hparams (hparams ), 330cparams (cparams ), 331mctx (mctx ) { 332 } 333 ~llm_graph_input_attn_kv ()= default ; 334 335void set_input (const llama_ubatch * ubatch )override ; 336 337bool can_reuse (const llm_graph_params & params )override ; 338 339ggml_tensor * get_k_idxs ()const {return self_k_idxs ; } 340ggml_tensor * get_v_idxs ()const {return self_v_idxs ; } 341 342ggml_tensor * get_kq_mask ()const {return self_kq_mask_cnv ; } 343 344ggml_tensor * self_k_idxs = nullptr ;// I64 [n_batch] 345ggml_tensor * self_v_idxs = nullptr ;// I64 [n_batch] or [n_batch*n_embd_v_gqa] 346 347ggml_tensor * self_kq_mask = nullptr ;// F32/F16 [n_kv, n_batch/n_stream, 1, n_stream] 348ggml_tensor * self_kq_mask_cnv = nullptr ;// [n_kv, n_batch/n_stream, 1, n_stream] 349 350// note: assumes v_rot^2 == I 351ggml_tensor * self_k_rot = nullptr ; 352ggml_tensor * self_v_rot = nullptr ; 353 354// note: these have to be copies because in order to be able to reuse a graph, its inputs 355// need to carry these parameters with them. otherwise, they can point to freed 356// llm_graph_params from a previous batch, causing stack-use-after-return 357const llama_hparams hparams ; 358const llama_cparams cparams ; 359 360const llama_kv_cache_context * mctx ; 361}; 362 363// V-less input for the KV cache 364// ref: https://github.com/ggml-org/llama.cpp/pull/19067 365class llm_graph_input_attn_k :public llm_graph_input_i { 366public : 367llm_graph_input_attn_k ( 368const llama_hparams & hparams , 369const llama_cparams & cparams , 370const llama_kv_cache_context * mctx ) : 371hparams (hparams ), 372cparams (cparams ), 373mctx (mctx ) { 374 } 375 ~llm_graph_input_attn_k ()= default ; 376 377void set_input (const llama_ubatch * ubatch )override ; 378 379bool can_reuse (const llm_graph_params & params )override ; 380 381// like can_reuse, but does not re-bind mctx 382bool can_reuse_impl (const llm_graph_params & params ); 383 384ggml_tensor * get_k_idxs ()const {return self_k_idxs ; } 385 386ggml_tensor * get_kq_mask ()const {return self_kq_mask_cnv ; } 387 388ggml_tensor * self_k_idxs = nullptr ;// I64 [n_batch] 389 390ggml_tensor * self_kq_mask = nullptr ;// F32/F16 [n_kv, n_batch/n_stream, 1, n_stream] 391ggml_tensor * self_kq_mask_cnv = nullptr ;// [n_kv, n_batch/n_stream, 1, n_stream] 392 393const llama_hparams hparams ; 394const llama_cparams cparams ; 395 396const llama_kv_cache_context * mctx ; 397}; 398 399class llm_graph_input_attn_k_dsa :public llm_graph_input_i { 400public : 401llm_graph_input_attn_k_dsa ( 402const llama_hparams & hparams , 403const llama_cparams & cparams , 404const llama_kv_cache_dsa_context * mctx ) : 405hparams (hparams ), 406cparams (cparams ), 407mctx (mctx ) { 408 } 409 ~llm_graph_input_attn_k_dsa ()= default ; 410 411void set_input (const llama_ubatch * ubatch )override ; 412 413bool can_reuse (const llm_graph_params & params )override ; 414 415// like can_reuse, but does not re-bind mctx 416bool can_reuse_impl (const llm_graph_params & params ); 417 418ggml_tensor * get_k_idxs_mla ()const {return self_k_idxs_mla ; } 419ggml_tensor * get_k_idxs_lid ()const {return self_k_idxs_lid ; } 420 421ggml_tensor * get_kq_mask_mla ()const {return self_kq_mask_mla_cnv ; } 422ggml_tensor * get_kq_mask_lid ()const {return self_kq_mask_lid ; } 423 424ggml_tensor * self_k_idxs_mla = nullptr ;// I64 [n_batch] 425ggml_tensor * self_k_idxs_lid = nullptr ;// I64 [n_batch] 426 427ggml_tensor * self_kq_mask_mla = nullptr ;// F32/F16 [n_kv, n_batch/n_stream, 1, n_stream] 428ggml_tensor * self_kq_mask_mla_cnv = nullptr ;// [n_kv, n_batch/n_stream, 1, n_stream] 429ggml_tensor * self_kq_mask_lid = nullptr ;// F32 [n_kv, n_batch/n_stream, 1, n_stream] 430ggml_tensor * self_kq_mask_lid_cnv = nullptr ;// [n_kv, n_batch/n_stream, 1, n_stream] 431 432ggml_tensor * self_k_rot_lid = nullptr ; 433 434const llama_hparams hparams ; 435const llama_cparams cparams ; 436 437const llama_kv_cache_dsa_context * mctx ; 438}; 439 440// DSA input (full-attention layers + indexer) with K-only input for the SWA layers 441class llm_graph_input_attn_k_dsa_iswa :public llm_graph_input_i { 442public : 443llm_graph_input_attn_k_dsa_iswa ( 444std ::unique_ptr < llm_graph_input_attn_k_dsa > inp_dsa , 445std ::unique_ptr < llm_graph_input_attn_k > inp_swa , 446const llama_kv_cache_dsa_iswa_context * mctx ) : 447inp_dsa (std ::move (inp_dsa )), 448inp_swa (std ::move (inp_swa )), 449mctx (mctx ) { 450 } 451 ~llm_graph_input_attn_k_dsa_iswa ()= default ; 452 453void set_input (const llama_ubatch * ubatch )override ; 454 455bool can_reuse (const llm_graph_params & params )override ; 456 457llm_graph_input_attn_k_dsa * get_dsa ()const {return inp_dsa .get (); } 458llm_graph_input_attn_k * get_swa ()const {return inp_swa .get (); } 459 460std ::unique_ptr < llm_graph_input_attn_k_dsa > inp_dsa ; 461std ::unique_ptr < llm_graph_input_attn_k > inp_swa ; 462 463const llama_kv_cache_dsa_iswa_context * mctx ; 464}; 465 466// standard K/V attention input against the base cache, plus destination indices for the indexer key cache 467class llm_graph_input_attn_kv_msa :public llm_graph_input_attn_kv { 468public : 469llm_graph_input_attn_kv_msa ( 470const llama_hparams & hparams , 471const llama_cparams & cparams , 472const llama_kv_cache_msa_context * mctx ); 473 ~llm_graph_input_attn_kv_msa ()= default ; 474 475void set_input (const llama_ubatch * ubatch )override ; 476 477bool can_reuse (const llm_graph_params & params )override ; 478 479ggml_tensor * get_k_idxs_idx ()const {return self_k_idxs_idx ; } 480 481ggml_tensor * self_k_idxs_idx = nullptr ;// I64 [n_batch] 482 483const llama_kv_cache_msa_context * mctx_msa ; 484}; 485 486class llm_graph_input_attn_kv_iswa :public llm_graph_input_i { 487public : 488llm_graph_input_attn_kv_iswa ( 489const llama_hparams & hparams , 490const llama_cparams & cparams , 491const llama_kv_cache_iswa_context * mctx ) : 492hparams (hparams ), 493cparams (cparams ), 494mctx (mctx ) { 495 } 496 ~llm_graph_input_attn_kv_iswa ()= default ; 497 498void set_input (const llama_ubatch * ubatch )override ; 499 500bool can_reuse (const llm_graph_params & params )override ; 501 502ggml_tensor * get_k_idxs ()const {return self_k_idxs ; } 503ggml_tensor * get_v_idxs ()const {return self_v_idxs ; } 504ggml_tensor * get_k_idxs_swa ()const {return self_k_idxs_swa ; } 505ggml_tensor * get_v_idxs_swa ()const {return self_v_idxs_swa ; } 506 507ggml_tensor * get_kq_mask ()const {return self_kq_mask_cnv ; } 508ggml_tensor * get_kq_mask_swa ()const {return self_kq_mask_swa_cnv ; } 509 510ggml_tensor * self_k_idxs = nullptr ;// I64 [n_batch] 511ggml_tensor * self_v_idxs = nullptr ;// I64 [n_batch] or [n_batch*n_embd_v_gqa] 512ggml_tensor * self_k_idxs_swa = nullptr ;// I64 [n_batch] 513ggml_tensor * self_v_idxs_swa = nullptr ;// I64 [n_batch] or [n_batch*n_embd_v_gqa] 514 515ggml_tensor * self_kq_mask = nullptr ;// F32/F16 [n_kv, n_batch/n_stream, 1, n_stream] 516ggml_tensor * self_kq_mask_cnv = nullptr ;// [n_kv, n_batch/n_stream, 1, n_stream] 517ggml_tensor * self_kq_mask_swa = nullptr ;// F32/F16 [n_kv, n_batch/n_stream, 1, n_stream] 518ggml_tensor * self_kq_mask_swa_cnv = nullptr ;// [n_kv, n_batch/n_stream, 1, n_stream] 519 520ggml_tensor * self_k_rot = nullptr ; 521ggml_tensor * self_v_rot = nullptr ; 522 523ggml_tensor * self_k_rot_swa = nullptr ; 524ggml_tensor * self_v_rot_swa = nullptr ; 525 526const llama_hparams hparams ; 527const llama_cparams cparams ; 528 529const llama_kv_cache_iswa_context * mctx ; 530}; 531 532class llm_graph_input_attn_k_iswa :public llm_graph_input_i { 533public : 534llm_graph_input_attn_k_iswa ( 535const llama_hparams & hparams , 536const llama_cparams & cparams , 537const llama_kv_cache_iswa_context * mctx ) : 538hparams (hparams ), 539cparams (cparams ), 540mctx (mctx ) { 541 } 542 ~llm_graph_input_attn_k_iswa ()= default ; 543 544void set_input (const llama_ubatch * ubatch )override ; 545 546bool can_reuse (const llm_graph_params & params )override ; 547 548ggml_tensor * get_k_idxs ()const {return self_k_idxs ; } 549ggml_tensor * get_k_idxs_swa ()const {return self_k_idxs_swa ; } 550 551ggml_tensor * get_kq_mask ()const {return self_kq_mask_cnv ; } 552ggml_tensor * get_kq_mask_swa ()const {return self_kq_mask_swa_cnv ; } 553 554ggml_tensor * self_k_idxs = nullptr ;// I64 [n_batch] 555ggml_tensor * self_k_idxs_swa = nullptr ;// I64 [n_batch] 556 557ggml_tensor * self_kq_mask = nullptr ;// F32/F16 [n_kv, n_batch/n_stream, 1, n_stream] 558ggml_tensor * self_kq_mask_cnv = nullptr ;// [n_kv, n_batch/n_stream, 1, n_stream] 559ggml_tensor * self_kq_mask_swa = nullptr ;// F32/F16 [n_kv, n_batch/n_stream, 1, n_stream] 560ggml_tensor * self_kq_mask_swa_cnv = nullptr ;// [n_kv, n_batch/n_stream, 1, n_stream] 561 562ggml_tensor * self_k_rot = nullptr ; 563ggml_tensor * self_k_rot_swa = nullptr ; 564 565const llama_hparams hparams ; 566const llama_cparams cparams ; 567 568const llama_kv_cache_iswa_context * mctx ; 569}; 570 571// DSV4 raw graph inputs are SWA-only, but their mask may be stream-shaped 572// so raw K can be concatenated with DSV4 compressed K in one attention op. 573class llm_graph_input_dsv4_raw { 574public : 575llm_graph_input_dsv4_raw ( 576const llama_cparams & cparams , 577const llama_kv_cache_dsv4_raw_context * mctx ) : 578cparams (cparams ), 579mctx (mctx ) { 580 } 581 582void set_input (const llama_ubatch * ubatch ); 583 584ggml_tensor * get_k_idxs ()const {return self_k_idxs ; } 585ggml_tensor * get_kq_mask ()const {return self_kq_mask_cnv ; } 586 587ggml_tensor * self_k_idxs = nullptr ;// I64 [n_batch] 588 589ggml_tensor * self_kq_mask = nullptr ;// F32/F16 [n_kv, n_batch/n_stream, 1, n_stream] 590ggml_tensor * self_kq_mask_cnv = nullptr ;// [n_kv, n_batch/n_stream, 1, n_stream] 591 592ggml_tensor * self_k_rot = nullptr ; 593 594const llama_cparams cparams ; 595 596const llama_kv_cache_dsv4_raw_context * mctx ; 597}; 598 599class llm_graph_input_dsv4 :public llm_graph_input_i { 600public : 601struct comp_input { 602ggml_tensor * state_pos = nullptr;// I32 [n_state] 603ggml_tensor * state_persist_src_idxs = nullptr;// I32 [n_state_persist] 604ggml_tensor * state_persist_dst_idxs = nullptr;// I32 [n_state_persist] 605ggml_tensor * state_restore_src_idxs = nullptr;// I32 [n_state_restore] 606ggml_tensor * state_restore_dst_idxs = nullptr;// I32 [n_state_restore] 607ggml_tensor * state_snapshot_src_idxs = nullptr;// I32 [n_state_snapshot] 608ggml_tensor * state_snapshot_dst_idxs = nullptr;// I32 [n_state_snapshot] 609ggml_tensor * state_read_idxs = nullptr;// I32 [ratio*n_state_write] 610ggml_tensor * state_write_idxs = nullptr;// I64 [n_state_write] 611ggml_tensor * state_write_pos = nullptr;// I32 [n_state_write] 612 613ggml_tensor * kq_mask = nullptr;// F32 [n_kv, n_batch/n_stream, 1, n_stream] 614 615ggml_tensor * k_rot = nullptr; 616 }; 617 618llm_graph_input_dsv4 ( 619const llama_cparams & cparams , 620std ::unique_ptr < llm_graph_input_dsv4_raw > inp_raw , 621const llama_kv_cache_dsv4_context * mctx ) : 622inp_raw (std ::move (inp_raw )), 623cparams (cparams ), 624mctx (mctx ) { 625 } 626 ~llm_graph_input_dsv4 ()= default ; 627 628void set_input (const llama_ubatch * ubatch )override ; 629 630bool can_reuse (const llm_graph_params & params )override ; 631 632llm_graph_input_dsv4_raw * get_raw ()const {return inp_raw .get (); } 633const comp_input & get_csa ()const {return inp_csa ; } 634const comp_input & get_hca ()const {return inp_hca ; } 635const comp_input & get_lid ()const {return inp_lid ; } 636 637std ::unique_ptr < llm_graph_input_dsv4_raw > inp_raw ; 638 639comp_input inp_csa ; 640comp_input inp_hca ; 641comp_input inp_lid ; 642 643const llama_cparams cparams ; 644 645const llama_kv_cache_dsv4_context * mctx ; 646}; 647 648class llm_graph_input_attn_cross :public llm_graph_input_i { 649public : 650llm_graph_input_attn_cross (const llama_cross * cross ) :cross (cross ) {} 651 ~llm_graph_input_attn_cross ()= default ; 652 653void set_input (const llama_ubatch * ubatch )override ; 654 655bool can_reuse (const llm_graph_params & params )override ; 656 657ggml_tensor * get_kq_mask_cross ()const {return cross_kq_mask_cnv ; } 658 659ggml_tensor * cross_kq_mask = nullptr ;// F32/F16 [n_outputs_enc, n_batch, 1, 1] 660ggml_tensor * cross_kq_mask_cnv = nullptr ;// F32/F16 [n_outputs_enc, n_batch, 1, 1] 661 662const llama_cross * cross = nullptr ; 663}; 664 665class llm_graph_input_mem_hybrid :public llm_graph_input_i { 666public : 667llm_graph_input_mem_hybrid ( 668const llama_cparams & cparams , 669std ::unique_ptr < llm_graph_input_attn_kv > inp_attn , 670std ::unique_ptr < llm_graph_input_rs > inp_rs , 671const llama_memory_hybrid_context * mctx ) : 672inp_attn (std ::move (inp_attn )), 673inp_rs (std ::move (inp_rs )), 674cparams (cparams ), 675mctx (mctx ) { } 676virtual ~llm_graph_input_mem_hybrid ()= default ; 677 678void set_input (const llama_ubatch * ubatch )override ; 679 680bool can_reuse (const llm_graph_params & params )override ; 681 682std ::unique_ptr < llm_graph_input_attn_kv > inp_attn ; 683std ::unique_ptr < llm_graph_input_rs > inp_rs ; 684 685llm_graph_input_attn_kv * get_attn ()const {return inp_attn .get (); } 686llm_graph_input_rs * get_recr ()const {return inp_rs .get (); } 687 688const llama_cparams cparams ; 689 690const llama_memory_hybrid_context * mctx ; 691}; 692 693class llm_graph_input_mem_hybrid_k :public llm_graph_input_i { 694public : 695llm_graph_input_mem_hybrid_k ( 696const llama_cparams & cparams , 697std ::unique_ptr < llm_graph_input_attn_k > inp_attn , 698std ::unique_ptr < llm_graph_input_rs > inp_rs , 699const llama_memory_hybrid_context * mctx ) : 700inp_attn (std ::move (inp_attn )), 701inp_rs (std ::move (inp_rs )), 702cparams (cparams ), 703mctx (mctx ) { } 704virtual ~llm_graph_input_mem_hybrid_k ()= default ; 705 706void set_input (const llama_ubatch * ubatch )override ; 707 708bool can_reuse (const llm_graph_params & params )override ; 709 710std ::unique_ptr < llm_graph_input_attn_k > inp_attn ; 711std ::unique_ptr < llm_graph_input_rs > inp_rs ; 712 713llm_graph_input_attn_k * get_attn ()const {return inp_attn .get (); } 714llm_graph_input_rs * get_recr ()const {return inp_rs .get (); } 715 716const llama_cparams cparams ; 717 718const llama_memory_hybrid_context * mctx ; 719}; 720 721class llm_graph_input_mem_hybrid_iswa :public llm_graph_input_i { 722public : 723llm_graph_input_mem_hybrid_iswa ( 724const llama_cparams & cparams , 725std ::unique_ptr < llm_graph_input_attn_kv_iswa > inp_attn , 726std ::unique_ptr < llm_graph_input_rs > inp_rs , 727const llama_memory_hybrid_iswa_context * mctx ) : 728inp_attn (std ::move (inp_attn )), 729inp_rs (std ::move (inp_rs )), 730cparams (cparams ), 731mctx (mctx ) { } 732virtual ~llm_graph_input_mem_hybrid_iswa ()= default ; 733 734void set_input (const llama_ubatch * ubatch )override ; 735 736bool can_reuse (const llm_graph_params & params )override ; 737 738std ::unique_ptr < llm_graph_input_attn_kv_iswa > inp_attn ; 739std ::unique_ptr < llm_graph_input_rs > inp_rs ; 740 741llm_graph_input_attn_kv_iswa * get_attn ()const {return inp_attn .get (); } 742llm_graph_input_rs * get_recr ()const {return inp_rs .get (); } 743 744const llama_cparams cparams ; 745 746const llama_memory_hybrid_iswa_context * mctx ; 747}; 748 749class llm_graph_input_sampling :public llm_graph_input_i { 750public : 751llm_graph_input_sampling (std ::map < llama_seq_id ,llama_sampler *> samplers ) : 752samplers (std ::move (samplers )) { } 753virtual ~llm_graph_input_sampling ()= default ; 754 755void set_input (const llama_ubatch * ubatch )override ; 756bool can_reuse (const llm_graph_params & params )override ; 757 758std ::map < llama_seq_id ,llama_sampler *> samplers ; 759}; 760 761// 762// llm_graph_result 763// 764 765// these objects deliver the result from the graph build process back to the llama_context 766// note that the input tensors created for the graph are referenced here - the goal is to be able to populate their 767// specific data, by calling the set_inputs() method 768// along with the input tensors, the object also provides commonly used outputs tensors, such as logits, embeddings, etc. 769// these are used by the llama_context to extact the relevant data, based on the compute parameters 770 771// callback that allows us to apply custom logic to each tensor (e.g. ggml-alloc, offloading, etc.) 772using llm_graph_cb = std ::function < void (const llama_ubatch & ubatch ,ggml_tensor * cur ,const char * name ,int il )> ; 773 774class llm_graph_result ; 775 776struct llm_graph_params { 777llm_arch arch = LLM_ARCH_UNKNOWN ; 778 779llama_hparams hparams ; 780llama_cparams cparams ; 781 782llama_ubatch ubatch ;// note: intentionally make a copy 783 784llm_graph_type gtype ; 785 786ggml_backend_sched_t sched ; 787ggml_backend_t backend_cpu ; 788 789const llama_adapter_cvec * cvec ; 790const llama_adapter_loras * loras ; 791const llama_memory_context_i * mctx ; 792const llama_cross * cross ; 793 794std ::map < llama_seq_id ,llama_sampler *> samplers ; 795 796static bool samplers_equal ( 797const std ::map < llama_seq_id ,llama_sampler *> & lhs , 798const std ::map < llama_seq_id ,llama_sampler *> & rhs ) { 799if (lhs .size ()!= rhs .size ()) { 800return false; 801 } 802for (const auto& [seq_id ,sampler ] :lhs ) { 803 autoit = rhs. find (seq_id); 804if (it == rhs. end () || it -> second != sampler) { 805return false; 806} 807} 808return true; 809} 810 811uint32_t n_outputs; 812 813llm_graph_cb cb; 814 815llm_graph_result * res; 816 817// return true if the "other" params would result in a graph with the same topology as with the current params 818// having the same topology allows us to reuse the graph in some cases 819bool allow_reuse ( const llm_graph_params & other) const { 820// first check the ubatch 821bool can_reuse_ubatch = 822ubatch. equal_seqs () == other. ubatch . equal_seqs () && 823ubatch. n_tokens == other. ubatch . n_tokens && 824ubatch. n_seq_tokens == other. ubatch . n_seq_tokens && 825ubatch. n_seqs == other. ubatch . n_seqs && 826ubatch. n_seqs_unq == other. ubatch . n_seqs_unq && 827( 828(!ubatch. token && !other. ubatch . token ) || 829(!ubatch. embd && !other. ubatch . embd ) || 830(ubatch. token && other. ubatch . token && ubatch. embd && other. ubatch . embd ) 831); 832 833// when we split the batch using "equal_seqs" we have to verify that the participating sequences are the same 834// the reason is because the set of attention streams would be different for different sequences 835if (can_reuse_ubatch && ubatch. equal_seqs ()) { 836if (!ubatch. data ) { 837// if the old ubatch does not own it's data, then we cannot guarantee that it is still alive, and 838// therefore we cannot perform the sequence id check. normally should never happen 839can_reuse_ubatch = false; 840} else { 841for ( uint32_t s = 0 ; s < ubatch. n_seqs_unq ; ++ s) { 842can_reuse_ubatch &= ubatch. seq_id_unq [s] == other. ubatch . seq_id_unq [s]; 843} 844} 845} 846 847if (!can_reuse_ubatch) { 848return false; 849} 850 851if (n_outputs != other. n_outputs ) { 852return false; 853} 854 855if (! samplers_equal (samplers, other. samplers )) { 856return false; 857} 858 859if (samplers. size () > 0 ) { 860if (!ubatch. data || !other. ubatch . data ) { 861return false; 862} 863 864// check that the outputs are the same for all samplers 865for ( uint32_t i = 0 ; i < ubatch. n_tokens ; ++ i) { 866if (ubatch. output [i] != other. ubatch . output [i] || 867ubatch. seq_id [i][ 0 ] != other. ubatch . seq_id [i][ 0 ]) { 868return false; 869} 870} 871} 872 873// TODO: https://github.com/ggml-org/llama.cpp/pull/24340#discussion_r3448035248 874if (cparams. nextn_layer_offset != other. cparams . nextn_layer_offset ) { 875return false; 876} 877 878return 879cparams. embeddings == other. cparams . embeddings && 880cparams. embeddings_nextn == other. cparams . embeddings_nextn && 881cparams. embeddings_nextn_masked == other. cparams . embeddings_nextn_masked && 882cparams. causal_attn == other. cparams . causal_attn && 883arch == other. arch && 884gtype == other. gtype && 885cvec == other. cvec && 886loras == other. loras && 887cross == other. cross ; 888} 889}; 890 891struct llm_graph_fused_node { 892llm_fused_op op ; 893ggml_tensor * tensor ; 894int il ; 895}; 896 897class llm_graph_result { 898public : 899llm_graph_result( int64_t max_nodes ); 900 901virtual ~ llm_graph_result () = default; 902 903ggml_tensor * get_inp_tokens () const { return t_inp_tokens; } 904ggml_tensor * get_logits () const { return t_logits; } 905ggml_tensor * get_embd () const { return t_embd; } 906ggml_tensor * get_embd_pooled () const { return t_embd_pooled; } 907ggml_tensor * get_h_nextn () const { return t_h_nextn; } 908 909ggml_tensor * get_layer_inp ( int il) const { return t_layer_inp[il]; } 910 911ggml_cgraph * get_gf () const { return gf; } 912ggml_context * get_ctx () const { return ctx_compute. get (); } 913 914int64_t get_max_nodes () const ; 915 916void reset (); 917 918void set_inputs ( const llama_ubatch * ubatch); 919void set_outputs ( const llm_graph_params & params); 920 921// try to update the existing graph result using the new graph parameters in order to reuse it 922// this can only be done if we determine that the resulting graph using the new graph parameters 923// would be identical to the existing graph. in that case, we simply have to update the memory 924// contexts of the input tensors of the graph and we can reuse it for another computation 925// return true if the graph was updated and can be reused 926bool can_reuse ( const llm_graph_params & params); 927 928llm_graph_input_i * add_input ( llm_graph_input_ptr input); 929 930void add_fused_node ( llm_graph_fused_node result); 931 932const std ::vector < llm_graph_fused_node > & get_fused_nodes () const { return fused_nodes; } 933 934void set_params ( const llm_graph_params & params); 935 936// important graph nodes 937ggml_tensor * t_inp_tokens = nullptr ; 938ggml_tensor * t_inp_embd = nullptr ; // [n_embd_inp, n_tokens] 939ggml_tensor * t_logits = nullptr ; 940ggml_tensor * t_embd = nullptr ; 941ggml_tensor * t_embd_pooled = nullptr ; 942ggml_tensor * t_h_nextn = nullptr ; // [n_embd, n_outputs] hidden state before final output norm 943 944std ::vector < ggml_tensor *> t_layer_inp; 945 946std ::vector < ggml_tensor *> t_sampled; 947std ::vector < ggml_tensor *> t_sampled_probs; 948std ::vector < ggml_tensor *> t_sampled_logits; 949std ::vector < ggml_tensor *> t_candidates; 950 951std ::vector < llm_graph_input_ptr > inputs; 952std ::vector < llm_graph_fused_node > fused_nodes; 953 954ggml_context_ptr ctx_compute; 955 956// memory buffers used to evaluate the model 957std ::vector < uint8_t > buf_compute_meta; 958 959ggml_cgraph * gf; 960 961int64_t max_nodes; 962 963private : 964// keep a copy of the previous graph parameters 965// we will use this to determine whether the graph can be reused by comparing them with the new parameters 966// note: these are updated after constructing the new graph 967llm_graph_params params; 968 969// env: LLAMA_GRAPH_RESULT_DEBUG 970int debug = 0 ; 971}; 972 973using llm_graph_result_ptr = std::unique_ptr < llm_graph_result > ; 974 975// 976// llm_graph_context 977// 978 979// used in build_rs to properly order writes and avoid unnecessary copies 980using llm_graph_get_rows_fn = std::function < ggml_tensor * ( ggml_context * , ggml_tensor * states, ggml_tensor * ids) > ; 981 982struct llm_graph_qkv { 983ggml_tensor * q ; // [n_embd_head, n_head, n_tokens] 984ggml_tensor * k ; // [n_embd_head, n_head_kv, n_tokens] 985ggml_tensor * v ; // [n_embd_head, n_head_kv, n_tokens] 986}; 987 988struct llm_graph_context { 989const llm_arch arch ; 990 991const llama_hparams & hparams ; 992const llama_cparams & cparams ; 993const llama_ubatch & ubatch ; 994 995const int64_t n_embd ; 996const int64_t n_layer ; 997const int64_t n_layer_nextn ; 998const int64_t n_rot ; 999const int64_t n_ctx ; // user-specified context size (can be different from n_ctx_train) 1000const int64_t n_head ; 1001const int64_t n_head_kv ; 1002const int64_t n_embd_head_k ; 1003const int64_t n_embd_k_gqa ; 1004const int64_t n_embd_head_v ; 1005const int64_t n_embd_v_gqa ; 1006const int64_t n_expert ; 1007const int64_t n_expert_used ; 1008 1009const float freq_base ; 1010const float freq_scale ; 1011const float ext_factor ; 1012const float attn_factor ; 1013const float beta_fast ; 1014const float beta_slow ; 1015const float norm_eps ; 1016const float norm_rms_eps ; 1017 1018const int64_t n_tokens ; 1019const int64_t n_outputs ; 1020const int32_t n_ctx_orig ; // yarn 1021 1022const enum llama_pooling_type pooling_type ; 1023const enum llama_rope_type rope_type ; 1024 1025ggml_backend_sched_t sched ; 1026 1027ggml_backend_t backend_cpu ; // TODO: needed by build_attn_mha, figure out a way to remove? 1028 1029const llama_adapter_cvec * cvec ; 1030const llama_adapter_loras * loras ; 1031const llama_memory_context_i * mctx ; 1032const llama_cross * cross ; 1033 1034std :: map < llama_seq_id , llama_sampler *> samplers; 1035 1036const llm_graph_cb & cb_func ; 1037 1038llm_graph_result * res ; 1039 1040ggml_context * ctx0 = nullptr; 1041ggml_cgraph * gf = nullptr; 1042 1043llm_graph_context( const llm_graph_params & params); 1044virtual ~ llm_graph_context () = default ; 1045 1046void cb ( ggml_tensor * cur, const char * name, int il) const; 1047 1048// 1049// common 1050// 1051 1052ggml_tensor * build_cvec ( 1053ggml_tensor * cur, 1054int il) const; 1055 1056// do mat_mul, while optionally apply lora and per-tensor scale 1057ggml_tensor * build_lora_mm ( 1058ggml_tensor * w, 1059ggml_tensor * cur, 1060ggml_tensor * w_s = nullptr) const; 1061 1062// do mat_mul_id, while optionally apply lora and per-expert scale 1063ggml_tensor * build_lora_mm_id ( 1064ggml_tensor * w, // ggml_tensor * as 1065ggml_tensor * cur, // ggml_tensor * b 1066ggml_tensor * ids, 1067ggml_tensor * w_s = nullptr) const; 1068 1069ggml_tensor * build_norm ( 1070ggml_tensor * cur, 1071ggml_tensor * mw, 1072ggml_tensor * mb, 1073llm_norm_type type, 1074int il) const; 1075 1076 1077// compute Q, K, V projections with optional bias and reshape 1078// supports both fused wqkv and separate wq/wk/wv paths 1079llm_graph_qkv build_qkv ( 1080const llama_layer & layer, 1081ggml_tensor * cur, 1082int64_t n_embd_head, 1083int64_t n_head, 1084int64_t n_head_kv, 1085int il) const; 1086 1087// Set reshape to false to return contiguous projections before clamp/reshape. 1088llm_graph_qkv build_qkv ( 1089const llama_layer & layer, 1090ggml_tensor * cur, 1091int64_t n_embd_head_q, 1092int64_t n_head_q, 1093int64_t n_embd_head_k, 1094int64_t n_head_k, 1095int64_t n_embd_head_v, 1096int64_t n_head_v, 1097int il, 1098bool reshape = true) const; 1099 1100ggml_tensor * build_ffn ( 1101ggml_tensor * cur, 1102ggml_tensor * up, 1103ggml_tensor * up_b, 1104ggml_tensor * up_s, 1105ggml_tensor * gate, 1106ggml_tensor * gate_b, 1107ggml_tensor * gate_s, 1108ggml_tensor * down, 1109ggml_tensor * down_b, 1110ggml_tensor * down_s, 1111ggml_tensor * act_scales, 1112llm_ffn_op_type type_op, 1113llm_ffn_gate_type type_gate, 1114int il) const; 1115 1116// build MoE FFN without bias tensors 1117ggml_tensor * build_moe_ffn ( 1118ggml_tensor * cur, 1119ggml_tensor * gate_inp, 1120ggml_tensor * up_exps, 1121ggml_tensor * gate_exps, 1122ggml_tensor * down_exps, 1123ggml_tensor * exp_probs_b, 1124int64_t n_expert, 1125int64_t n_expert_used, 1126llm_ffn_op_type type_op, 1127bool norm_w, 1128float w_scale, 1129llama_expert_gating_func_type gating_op, 1130int il, 1131ggml_tensor * probs_in = nullptr, 1132ggml_tensor * gate_up_exps = nullptr, 1133ggml_tensor * up_exps_s = nullptr, 1134ggml_tensor * gate_exps_s = nullptr, 1135ggml_tensor * down_exps_s = nullptr, 1136ggml_tensor * selected_experts_in = nullptr) const; 1137 1138ggml_tensor * build_moe_ffn ( 1139ggml_tensor * cur, 1140ggml_tensor * gate_inp, 1141ggml_tensor * gate_inp_b, 1142ggml_tensor * up_exps, 1143ggml_tensor * up_exps_b, 1144ggml_tensor * gate_exps, 1145ggml_tensor * gate_exps_b, 1146ggml_tensor * down_exps, 1147ggml_tensor * down_exps_b, 1148ggml_tensor * exp_probs_b, 1149int64_t n_expert, 1150int64_t n_expert_used, 1151llm_ffn_op_type type_op, 1152bool norm_w, 1153float w_scale, 1154llama_expert_gating_func_type gating_op, 1155int il, 1156ggml_tensor * probs_in = nullptr, 1157ggml_tensor * gate_up_exps = nullptr, 1158ggml_tensor * gate_up_exps_b = nullptr, 1159ggml_tensor * up_exps_s = nullptr, 1160ggml_tensor * gate_exps_s = nullptr, 1161ggml_tensor * down_exps_s = nullptr, 1162ggml_tensor * selected_experts_in = nullptr) const; 1163 1164// 1165// inputs 1166// 1167 1168ggml_tensor * build_inp_embd ( ggml_tensor * tok_embd) const; 1169ggml_tensor * build_inp_pos () const; 1170ggml_tensor * build_inp_attn_scale () const; 1171ggml_tensor * build_inp_out_ids () const; 1172ggml_tensor * build_inp_mean () const; 1173ggml_tensor * build_inp_cls () const; 1174 1175ggml_tensor * build_inp_cross_embd () const; 1176ggml_tensor * build_inp_pos_bucket_enc () const; 1177ggml_tensor * build_inp_pos_bucket_dec () const; 1178ggml_tensor * build_pos_bias ( ggml_tensor * pos_bucket, ggml_tensor * attn_rel_b) const; 1179 1180// 1181// attention 1182// 1183 1184ggml_tensor * build_attn_mha ( 1185ggml_tensor * q, // [n_embd_head_q, n_head_q, n_tokens] 1186ggml_tensor * k, // [n_embd_head_k, n_head_k, n_tokens] 1187ggml_tensor * v, // [n_embd_head_v, n_head_v, n_tokens] (v_trans = false) 1188ggml_tensor * kq_b, 1189ggml_tensor * kq_mask, 1190ggml_tensor * sinks, // [n_head_q] 1191ggml_tensor * v_mla, // [n_embd_head_v_mla, n_embd_head_v, n_head_v] 1192int64_t n_kv_max, 1193float kq_scale, 1194int il) const; 1195 1196llm_graph_input_attn_no_cache * build_attn_inp_no_cache () const; 1197 1198ggml_tensor * build_attn ( 1199llm_graph_input_attn_no_cache * inp, 1200ggml_tensor * wo, 1201ggml_tensor * wo_b, 1202ggml_tensor * wo_s, 1203ggml_tensor * q_cur, // [n_embd_head_q, n_head_q, n_tokens] 1204ggml_tensor * k_cur, // [n_embd_head_k, n_head_k, n_tokens] 1205ggml_tensor * v_cur, // [n_embd_head_v, n_head_v, n_tokens] 1206ggml_tensor * kq_b, 1207ggml_tensor * sinks, // [n_head_q] 1208ggml_tensor * v_mla, // [n_embd_head_v_mla, n_embd_head_v, n_head_v] 1209float kq_scale, 1210int il) const; 1211 1212llm_graph_input_attn_kv * build_attn_inp_kv () const; 1213 1214ggml_tensor * build_attn ( 1215llm_graph_input_attn_kv * inp, 1216ggml_tensor * wo, 1217ggml_tensor * wo_b, 1218ggml_tensor * wo_s, 1219ggml_tensor * q_cur, // [n_embd_head_q, n_head_q, n_tokens] 1220ggml_tensor * k_cur, // [n_embd_head_k, n_head_k, n_tokens] 1221ggml_tensor * v_cur, // [n_embd_head_v, n_head_v, n_tokens] 1222ggml_tensor * kq_b, 1223ggml_tensor * sinks, // [n_head_q] 1224ggml_tensor * v_mla, // [n_embd_head_v_mla, n_embd_head_v, n_head_v] // TODO: remove 1225float kq_scale, 1226int il) const; 1227 1228llm_graph_input_attn_k * build_attn_inp_k () const; 1229 1230ggml_tensor * build_attn ( 1231llm_graph_input_attn_k * inp, 1232ggml_tensor * wo, 1233ggml_tensor * wo_b, 1234ggml_tensor * wo_s, 1235ggml_tensor * q_cur, // [n_embd_head_q, n_head_q, n_tokens] 1236ggml_tensor * k_cur, // [n_embd_head_k, n_head_k, n_tokens] 1237ggml_tensor * v_cur, // [n_embd_head_v, n_head_v, n_tokens] 1238ggml_tensor * kq_b, 1239ggml_tensor * sinks, // [n_head_q] 1240ggml_tensor * v_mla, // [n_embd_head_v_mla, n_embd_head_v, n_head_v] 1241float kq_scale, 1242int il) const; 1243 1244llm_graph_input_attn_k_dsa * build_attn_inp_k_dsa () const; 1245 1246llm_graph_input_attn_k_dsa_iswa * build_attn_inp_k_dsa_iswa () const; 1247 1248llm_graph_input_attn_kv_msa * build_attn_inp_kv_msa ( bool msa_enabled) const; 1249 1250ggml_tensor * build_attn ( 1251llm_graph_input_attn_k_dsa * inp, 1252ggml_tensor * wo, 1253ggml_tensor * wo_b, 1254ggml_tensor * wo_s, 1255ggml_tensor * q_cur, // [n_embd_head_q, n_head_q, n_tokens] 1256ggml_tensor * k_cur, // [n_embd_head_k, n_head_k, n_tokens] 1257ggml_tensor * v_cur, // [n_embd_head_v, n_head_v, n_tokens] 1258ggml_tensor * kq_b, 1259ggml_tensor * sinks, // [n_head_q] 1260ggml_tensor * v_mla, // [n_embd_head_v_mla, n_embd_head_v, n_head_v] 1261ggml_tensor * top_k, // [n_indexer_top_k, n_tokens] 1262float kq_scale, 1263int il) const; 1264 1265llm_graph_input_attn_kv_iswa * build_attn_inp_kv_iswa () const; 1266 1267llm_graph_input_dsv4 * build_inp_dsv4 () const; 1268 1269// note: if k_cur or v_cur are not provided, they will not be stored in the memory 1270ggml_tensor * build_attn ( 1271llm_graph_input_attn_kv_iswa * inp, 1272ggml_tensor * wo, 1273ggml_tensor * wo_b, 1274ggml_tensor * wo_s, 1275ggml_tensor * q_cur, // [n_embd_head_q, n_head_q, n_tokens] 1276ggml_tensor * k_cur, // [n_embd_head_k, n_head_k, n_tokens] optional 1277ggml_tensor * v_cur, // [n_embd_head_v, n_head_v, n_tokens] optional 1278ggml_tensor * kq_b, 1279ggml_tensor * sinks, // [n_head_q] 1280ggml_tensor * v_mla, // [n_embd_head_v_mla, n_embd_head_v, n_head_v] 1281float kq_scale, 1282int il) const; 1283 1284llm_graph_input_attn_k_iswa * build_attn_inp_k_iswa () const; 1285 1286// note: if k_cur is not provided, it will not be stored in the memory 1287// note: the K cache is used as V (MLA-style attention) 1288ggml_tensor * build_attn ( 1289llm_graph_input_attn_k_iswa * inp, 1290ggml_tensor * wo, 1291ggml_tensor * wo_b, 1292ggml_tensor * wo_s, 1293ggml_tensor * q_cur, // [n_embd_head_q, n_head_q, n_tokens] 1294ggml_tensor * k_cur, // [n_embd_head_k, n_head_k, n_tokens] optional 1295ggml_tensor * v_cur, // [n_embd_head_v, n_head_v, n_tokens] optional 1296ggml_tensor * kq_b, 1297ggml_tensor * sinks, // [n_head_q] 1298ggml_tensor * v_mla, // [n_embd_head_v_mla, n_embd_head_v, n_head_v] 1299float kq_scale, 1300int il) const; 1301 1302llm_graph_input_attn_cross * build_attn_inp_cross () const; 1303 1304ggml_tensor * build_attn ( 1305llm_graph_input_attn_cross * inp, 1306ggml_tensor * wo, 1307ggml_tensor * wo_b, 1308ggml_tensor * wo_s, 1309ggml_tensor * q_cur, // [n_embd_head_q, n_head_q, n_tokens] 1310ggml_tensor * k_cur, // [n_embd_head_k, n_head_k, n_tokens] 1311ggml_tensor * v_cur, // [n_embd_head_v, n_head_v, n_tokens] 1312ggml_tensor * kq_b, 1313ggml_tensor * sinks, // [n_head_q] 1314ggml_tensor * v_mla, // [n_embd_head_v_mla, n_embd_head_v, n_head_v] 1315float kq_scale, 1316int il) const; 1317 1318// 1319// recurrent 1320// 1321 1322// TODO: move this implementation to llama_memory_recurrent. 1323// this is analogous to llama_kv_cache::cpy_k / cpy_v 1324// when moving, avoid passing `ggml_cgraph` - only pass `ggml_context`. would likely need to split the 1325// implementation in 2 separate methods. the goal is to avoid calling `ggml_build_forward_expand` in 1326// `llama_memory_recurrent` 1327ggml_tensor * build_rs ( 1328ggml_tensor * s, 1329ggml_tensor * state_copy_main, 1330ggml_tensor * state_copy_extra, 1331int32_t state_size, 1332int32_t n_seqs, 1333uint32_t n_rs, 1334uint32_t rs_head, 1335uint32_t rs_size, 1336int32_t rs_zero, 1337const llm_graph_get_rows_fn & get_state_rows = ggml_get_rows) const; 1338 1339llm_graph_input_rs * build_rs_inp () const; 1340 1341ggml_tensor * build_rs ( 1342llm_graph_input_rs * inp, 1343ggml_tensor * s, 1344int32_t state_size, 1345int32_t n_seqs, 1346const llm_graph_get_rows_fn & get_state_rows = ggml_get_rows) const; 1347 1348ggml_tensor * build_rwkv_token_shift_load ( 1349llm_graph_input_rs * inp, 1350const llama_ubatch & ubatch, 1351int il) const; 1352 1353ggml_tensor * build_rwkv_token_shift_store ( 1354ggml_tensor * token_shift, 1355const llama_ubatch & ubatch, 1356int il) const; 1357// 1358// hybrid 1359// 1360 1361llm_graph_input_mem_hybrid * build_inp_mem_hybrid () const; 1362llm_graph_input_mem_hybrid_k * build_inp_mem_hybrid_k () const; 1363 1364llm_graph_input_mem_hybrid_iswa * build_inp_mem_hybrid_iswa () const; 1365 1366// 1367// pooling 1368// 1369 1370void build_pooling ( 1371ggml_tensor * cls, 1372ggml_tensor * cls_b, 1373ggml_tensor * cls_out, 1374ggml_tensor * cls_out_b, 1375ggml_tensor * cls_norm) const; 1376 1377// 1378// sampling (backend sampling) 1379// 1380 1381void build_sampling () const; 1382 1383// 1384// dense (out) 1385// 1386 1387void build_dense_out ( 1388ggml_tensor * dense_2, 1389ggml_tensor * dense_2_b, 1390ggml_tensor * dense_3) const; 1391}; 1392 1393// TODO: better name 1394int32_t llama_relative_position_bucket ( llama_pos x, llama_pos y, uint64_t n_buckets, bool bidirectional );