char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
6c84c7d5d
char/aliceai-t5-moe
1#pragma once 2 3#include "llama-batch.h" 4#include "llama-graph.h" 5#include "llama-kv-cells.h" 6#include "llama-memory.h" 7 8#include <unordered_map> 9#include <vector> 10 11struct llama_cparams ; 12struct llama_hparams ; 13struct llama_model ; 14struct llama_context ; 15 16// 17// llama_kv_cache 18// 19 20class llama_kv_cache :public llama_memory_i { 21public : 22struct stream_copy_info { 23bool empty ()const { 24assert (ssrc .size ()== sdst .size ()); 25return ssrc .empty (); 26 } 27 28std ::vector < uint32_t > ssrc ; 29std ::vector < uint32_t > sdst ; 30 }; 31 32// for each ubatch, create a slot_info that contains information about where the ubatch should be inserted in the 33// KV cells. for example, cell indices for each token, such that: token[i] -> goes to cells[idxs[i]] 34struct slot_info { 35// data for ggml_set_rows 36using idx_vec_t = std ::vector < uint32_t > ; 37 38// number of streams: ns = s1 - s0 + 1 39uint32_t s0 ; 40uint32_t s1 ; 41 42std ::vector < llama_seq_id > strm ;// [ns] 43std ::vector < idx_vec_t > idxs ;// [ns] 44 45uint32_t head ()const { 46GGML_ASSERT (idxs .size ()== 1 ); 47GGML_ASSERT (!idxs [0 ].empty ()); 48 49return idxs [0 ][0 ]; 50 } 51 52void resize (size_t n ) { 53strm .resize (n ); 54idxs .resize (n ); 55 } 56 57size_t size ()const { 58GGML_ASSERT (idxs .size ()== strm .size ()); 59GGML_ASSERT (!idxs .empty ()); 60 61return idxs [0 ].size (); 62 } 63 64size_t n_stream ()const { 65return strm .size (); 66 } 67 68bool empty ()const { 69return idxs .empty (); 70 } 71 72void clear () { 73idxs .clear (); 74 } 75 76// check if indices are contiguous starting from head() 77bool is_contiguous ()const { 78if (idxs .empty ()|| idxs [0 ].empty ()) { 79return true; 80 } 81if (idxs .size ()> 1 ) { 82return false; 83 } 84const uint32_t h = idxs [0 ][0 ]; 85for (size_t i = 0 ;i < idxs [0 ].size ();++ i ) { 86if (idxs [0 ][i ]!= h + i ) { 87return false; 88 } 89 } 90return true; 91 } 92 }; 93 94using slot_info_vec_t = std ::vector < slot_info > ; 95 96// TODO: refactor the memory instances to not depend on `llama_model` 97// instead pass all necessary info (e.g. hparams, dev layers, arch, etc.) directly 98// likely through `struct llama_memory_params` 99llama_kv_cache ( 100const llama_model & model , 101const llama_hparams & hparams , 102ggml_type type_k , 103ggml_type type_v , 104bool v_trans , 105bool offload , 106bool unified , 107uint32_t kv_size , 108uint32_t n_seq_max , 109uint32_t n_pad , 110uint32_t n_swa , 111llama_swa_type swa_type , 112llama_memory_t mem_other , 113const layer_filter_cb & filter , 114const layer_reuse_cb & reuse , 115const layer_share_cb & share , 116// a model can hold more than one cache, so the tensor names have to stay unique 117const char * name_tag = "" ); 118 119 ~llama_kv_cache ()= default ; 120 121// 122// llama_memory_i 123// 124 125llama_memory_context_ptr init_batch ( 126llama_batch_allocr & balloc , 127uint32_t n_ubatch , 128bool embd_all )override ; 129 130llama_memory_context_ptr init_full ()override ; 131 132llama_memory_context_ptr init_update ( llama_context * lctx, bool optimize) override; 133 134bool get_can_shift () const override; 135 136void clear ( bool data) override; 137 138bool seq_rm ( llama_seq_id seq_id, llama_pos p0, llama_pos p1) override; 139void seq_cp ( llama_seq_id seq_id_src, llama_seq_id seq_id_dst, llama_pos p0, llama_pos p1) override; 140void seq_keep ( llama_seq_id seq_id) override; 141void seq_add ( llama_seq_id seq_id, llama_pos p0, llama_pos p1, llama_pos shift) override; 142void seq_div ( llama_seq_id seq_id, llama_pos p0, llama_pos p1, int d) override; 143 144llama_pos seq_pos_min ( llama_seq_id seq_id) const override; 145llama_pos seq_pos_max ( llama_seq_id seq_id) const override; 146 147std::map < ggml_backend_buffer_type_t, size_t > memory_breakdown () const override; 148 149// state write/load 150 151void state_write ( llama_io_write_i & io, llama_seq_id seq_id = -1 , llama_state_seq_flags flags = 0 ) const override; 152void state_read ( llama_io_read_i & io, llama_seq_id seq_id = -1 , llama_state_seq_flags flags = 0 ) override; 153 154// 155// llama_kv_cache specific API 156// 157 158uint32_t get_size () const; 159uint32_t get_n_stream () const; 160 161bool get_has_shift () const; 162 163ggml_type type_k () const; 164ggml_type type_v () const; 165 166std::vector < uint32_t > get_layer_ids () const; 167ggml_tensor * get_k_storage ( int32_t il) const; 168 169const llama_kv_cells & get_cells ( llama_seq_id seq_id) const; 170 171// state_read, plus the cells the restored tokens were placed in 172// a cache that mirrors another one (the qwen4exp indexer) must not search for its own cells: two searches agree only by luck 173// sinfos_out: if set, filled with the layout used; a stream with no cells leaves an empty entry 174// sinfos_in : if set, the layout to use instead of searching. one entry per stream, cell count must match the blob 175void state_read_sinfo ( 176llama_io_read_i & io, 177llama_seq_id seq_id, 178llama_state_seq_flags flags, 179slot_info_vec_t * sinfos_out, 180const slot_info_vec_t * sinfos_in); 181 182// 183// graph_build API 184// 185 186uint32_t get_n_kv ( const slot_info & sinfo) const; 187 188// get views of the current state of the cache 189ggml_tensor * get_k ( ggml_context * ctx, int32_t il, uint32_t n_kv, const slot_info & sinfo) const; 190ggml_tensor * get_v ( ggml_context * ctx, int32_t il, uint32_t n_kv, const slot_info & sinfo) const; 191 192// store k_cur and v_cur in the cache based on the provided head location 193ggml_tensor * cpy_k ( ggml_context * ctx, ggml_tensor * k_cur, ggml_tensor * k_idxs, int32_t il, const slot_info & sinfo) const; 194ggml_tensor * cpy_v ( ggml_context * ctx, ggml_tensor * v_cur, ggml_tensor * v_idxs, int32_t il, const slot_info & sinfo) const; 195 196// 197// preparation API 198// 199 200// find places for the provided ubatches in the cache, returns the slot infos 201// return empty vector on failure 202slot_info_vec_t prepare ( const std ::vector < llama_ubatch > & ubatches); 203 204bool update ( llama_context * lctx, bool do_shift, const stream_copy_info & sc_info); 205 206// find a slot of kv cells that can hold the ubatch 207// if cont == true, then the slot must be continuous 208// return empty slot_info on failure 209slot_info find_slot ( const llama_ubatch & ubatch, bool cont) const; 210 211// emplace the ubatch context into slot: [sinfo.idxs[0...ubatch.n_tokens - 1]] 212void apply_ubatch ( const slot_info & sinfo, const llama_ubatch & ubatch); 213 214// 215// input API 216// 217 218ggml_tensor * build_input_k_idxs ( ggml_context * ctx, const llama_ubatch & ubatch) const; 219ggml_tensor * build_input_v_idxs ( ggml_context * ctx, const llama_ubatch & ubatch) const; 220 221ggml_tensor * build_input_k_rot ( ggml_context * ctx) const; 222ggml_tensor * build_input_v_rot ( ggml_context * ctx) const; 223 224void set_input_k_idxs ( ggml_tensor * dst, const llama_ubatch * ubatch, const slot_info & sinfo) const; 225void set_input_v_idxs ( ggml_tensor * dst, const llama_ubatch * ubatch, const slot_info & sinfo) const; 226 227void set_input_k_shift ( ggml_tensor * dst) const; 228 229void set_input_kq_mask ( ggml_tensor * dst, const llama_ubatch * ubatch, bool causal_attn) const; 230void set_input_pos_bucket ( ggml_tensor * dst, const llama_ubatch * ubatch) const; 231 232void set_input_k_rot ( ggml_tensor * dst) const; 233void set_input_v_rot ( ggml_tensor * dst) const; 234 235// true if llama_kv_cell_ext holds information that has to survive a state save/restore 236bool has_cell_ext () const; 237 238// for every token of the ubatch, the ids of the n tokens that precede it in its sequence 239// example for M-RoPE image case: tokens A B X X X C, where X is a 3-token image at pos 2 spanning positions 2..4: 240// tok: A B X X X C 241// pos: 0 1 2 2 2 5 242// prev, n=2: A -> [NULL, NULL], B -> [NULL, A], 3rd X -> [X, X], C -> [X, X] 243// note: used by n-gram input embeddings 244void get_prev_tokens ( const llama_ubatch & ubatch, uint32_t n, std ::vector < llama_token > & res) const; 245 246private: 247const llama_model & model; 248const llama_hparams & hparams; 249 250struct kv_layer { 251// layer index in the model 252// note: can be different from the layer index in the KV cache 253uint32_t il ; 254 255ggml_tensor * k ; 256ggml_tensor * v ; 257 258std :: vector < ggml_tensor *> k_stream ; 259std :: vector < ggml_tensor *> v_stream ; 260}; 261 262bool v_trans = true; // the value tensor is transposed 263 264const uint32_t n_seq_max = 1 ; 265const uint32_t n_stream = 1 ; 266 267// required padding 268const uint32_t n_pad = 1 ; 269 270// SWA 271const uint32_t n_swa = 0 ; 272 273// env: LLAMA_ATTN_ROT_DISABLE 274bool attn_rot_k = false; 275bool attn_rot_v = false; 276 277// if all layers participating in the cache have constant head size, the value is stored here 278// otherwise the value is -1 279int32_t n_embd_head_k_all = 0 ; 280int32_t n_embd_head_v_all = 0 ; 281 282// pre-computed hadamard martrices 283std ::unordered_map < int64_t , std::vector < float >> attn_rot_hadamard; 284 285// env: LLAMA_KV_CACHE_DEBUG 286int debug = 0 ; 287 288// this is the SWA type of the cache - not to be confused with the model SWA type 289const llama_swa_type swa_type = LLAMA_SWA_TYPE_NONE ; 290 291// ggml contexts for the KV cache along with the allocated backend buffers: 292std ::vector < std::pair < ggml_context_ptr, ggml_backend_buffer_ptr>> ctxs_bufs; 293 294// the current index from where we start searching for a free slot in the ring buffer of KV cells (see find_slot()) 295// note: this is not part of the KV state and it's only used to speed-up the find_slot() method 296std ::vector < uint32_t > v_heads; 297 298// TODO: temporary until we refactor to be able to share the same cells between 2 kv caches [TAG_KV_CACHE_SHARE_CELLS] 299llama_kv_cache * other; 300 301std ::shared_ptr < llama_kv_cells_vec > v_cells_impl; 302 303llama_kv_cells_vec & v_cells; 304 305// maps from a sequence id to a stream id 306std ::vector < uint32_t > seq_to_stream; 307 308// pending stream copies that will be applied during the next update 309stream_copy_info sc_info; 310 311std ::vector < kv_layer > layers; 312 313// model layer id -> KV cache layer id 314std ::unordered_map < int32_t , int32_t > map_layer_ids; 315 316size_t total_size () const; 317 318size_t size_k_bytes () const; 319size_t size_v_bytes () const; 320 321ggml_tensor * build_rope_shift ( 322const llama_cparams & cparams, 323ggml_context * ctx, 324ggml_tensor * cur, 325ggml_tensor * shift, 326ggml_tensor * rot, 327ggml_tensor * factors, 328float freq_base, 329float freq_scale, 330uint32_t il) const; 331 332ggml_cgraph * build_graph_shift ( 333llm_graph_result * res, 334llama_context * lctx) const; 335 336struct cell_ranges_t { 337uint32_t strm ; 338 339std :: vector < std ::pair < uint32_t, uint32_t >> data; // ranges, from inclusive, to exclusive 340}; 341 342void state_write_meta ( llama_io_write_i & io, const cell_ranges_t & cr, llama_seq_id seq_id = -1 ) const ; 343void state_write_data ( llama_io_write_i & io, const cell_ranges_t & cr) const; 344 345// sinfo_in, when set, replaces the find_slot call: the cells are given by the caller 346bool state_read_meta ( llama_io_read_i & io, uint32_t strm, uint32_t cell_count, slot_info & sinfo, llama_seq_id dest_seq_id = -1 , const slot_info * sinfo_in = nullptr ); 347bool state_read_data ( llama_io_read_i & io, uint32_t strm, uint32_t cell_count, const slot_info & sinfo); 348}; 349 350class llama_kv_cache_context : public llama_memory_context_i { 351public : 352// some shorthands 353using slot_info_vec_t = llama_kv_cache::slot_info_vec_t; 354using stream_copy_info = llama_kv_cache::stream_copy_info; 355 356// used for errors 357llama_kv_cache_context( llama_memory_status status); 358 359// used to create a full-cache context 360llama_kv_cache_context ( 361llama_kv_cache * kv); 362 363// used to create an update context 364llama_kv_cache_context ( 365llama_kv_cache * kv, 366llama_context * lctx, 367bool do_shift, 368stream_copy_info sc_info); 369 370// used to create a batch processing context from a batch 371llama_kv_cache_context ( 372llama_kv_cache * kv, 373slot_info_vec_t sinfos, 374std::vector < llama_ubatch > ubatches); 375 376virtual ~ llama_kv_cache_context (); 377 378// 379// llama_memory_context_i 380// 381 382bool next () override ; 383bool apply () override; 384 385llama_memory_status get_status () const override ; 386const llama_ubatch & get_ubatch () const override; 387 388// 389// llama_kv_cache_context specific API 390// 391 392uint32_t get_n_kv () const; 393 394ggml_type type_k () const; 395ggml_type type_v () const; 396 397// get views of the current state of the cache 398ggml_tensor * get_k ( ggml_context * ctx, int32_t il) const; 399ggml_tensor * get_v ( ggml_context * ctx, int32_t il) const; 400 401// store k_cur and v_cur in the cache based on the provided head location 402// note: the heads in k_cur and v_cur should be laid out contiguously in memory 403// - k_cur [n_embd_head_k, n_head_k, n_tokens] 404// - k_idxs [n_tokens] 405// - v_cur [n_embd_head_v, n_head_v, n_tokens] 406// - v_idxs [n_tokens] or [n_tokens*n_embd_v_gqa] depending if V cache is transposed 407ggml_tensor * cpy_k ( ggml_context * ctx, ggml_tensor * k_cur, ggml_tensor * k_idxs, int32_t il) const; 408ggml_tensor * cpy_v ( ggml_context * ctx, ggml_tensor * v_cur, ggml_tensor * v_idxs, int32_t il) const; 409 410// create destination indices for each head of the current batch for where it would be written in the KV cache 411// the indices address the global KV cache (not per stream) - this is not relevant for the user of this API, but 412// helps understand the implementation logic of cpy_k and cpy_v 413ggml_tensor * build_input_k_idxs ( ggml_context * ctx, const llama_ubatch & ubatch) const; 414ggml_tensor * build_input_v_idxs ( ggml_context * ctx, const llama_ubatch & ubatch) const; 415 416ggml_tensor * build_input_k_rot ( ggml_context * ctx) const; 417ggml_tensor * build_input_v_rot ( ggml_context * ctx) const; 418 419void set_input_k_idxs ( ggml_tensor * dst, const llama_ubatch * ubatch) const; 420void set_input_v_idxs ( ggml_tensor * dst, const llama_ubatch * ubatch) const; 421 422void set_input_k_shift ( ggml_tensor * dst) const; 423void set_input_kq_mask ( ggml_tensor * dst, const llama_ubatch * ubatch, bool causal_attn) const; 424void set_input_pos_bucket ( ggml_tensor * dst, const llama_ubatch * ubatch) const; 425 426void set_input_k_rot ( ggml_tensor * dst) const; 427void set_input_v_rot ( ggml_tensor * dst) const; 428 429// see llama_kv_cache::get_prev_tokens() 430void get_prev_tokens ( const llama_ubatch & ubatch, uint32_t n, std ::vector < llama_token > & res) const; 431 432private: 433llama_memory_status status; 434 435llama_kv_cache * kv; 436llama_context * lctx; 437 438// 439// update context 440// 441 442bool do_shift = false; 443 444stream_copy_info sc_info; 445 446// 447// batch processing context 448// 449 450// the index of the cur ubatch to process 451size_t i_cur = 0 ; 452 453slot_info_vec_t sinfos; 454 455std ::vector < llama_ubatch > ubatches; 456 457// 458// data needed for building the compute graph for the current ubatch: 459// 460 461// a heuristic, to avoid attending the full cache if it is not yet utilized 462// as the cache gets filled, the benefit from this heuristic disappears 463int32_t n_kv ; 464};