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