char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
67d5978bb
char/aliceai-t5-moe
1#pragma once 2 3#include "llama-kv-cache.h" 4 5#include <vector> 6 7// llama_kv_cache_msa 8 9// uses two instances of llama_kv_cache, one for K/V tensors, and one for the MSA indexer tensors 10// both receive identical sequence operations and identical ubatches, so their cell layouts stay in synced. 11// the context also exposes per-ubatch pos - cell translation maps populated from llama_kv_cells via 12// llama_kv_cache::get_cells(), which the model graph uses to run MSA block selection in position space 13 14class llama_kv_cache_msa :public llama_memory_i { 15public : 16llama_kv_cache_msa ( 17const llama_model & model , 18ggml_type type_k , 19ggml_type type_v , 20bool v_trans , 21bool offload , 22bool unified , 23uint32_t kv_size , 24uint32_t n_seq_max , 25uint32_t n_pad , 26uint32_t n_swa , 27llama_swa_type swa_type , 28const layer_filter_cb & filter , 29const layer_filter_cb & filter_idx , 30const layer_reuse_cb & reuse ); 31 32 ~llama_kv_cache_msa ()= default ; 33 34// llama_memory_i 35 36llama_memory_context_ptr init_batch ( 37llama_batch_allocr & balloc , 38uint32_t n_ubatch , 39bool embd_all )override ; 40 41llama_memory_context_ptr init_full ()override ; 42 43llama_memory_context_ptr init_update ( llama_context * lctx, bool optimize) override; 44 45bool get_can_shift () const override; 46 47void clear ( bool data) override; 48 49bool seq_rm ( llama_seq_id seq_id, llama_pos p0, llama_pos p1) override; 50void seq_cp ( llama_seq_id seq_id_src, llama_seq_id seq_id_dst, llama_pos p0, llama_pos p1) override; 51void seq_keep ( llama_seq_id seq_id) override; 52void seq_add ( llama_seq_id seq_id, llama_pos p0, llama_pos p1, llama_pos shift) override; 53void seq_div ( llama_seq_id seq_id, llama_pos p0, llama_pos p1, int d) override; 54 55llama_pos seq_pos_min ( llama_seq_id seq_id) const override; 56llama_pos seq_pos_max ( llama_seq_id seq_id) const override; 57 58std::map < ggml_backend_buffer_type_t, size_t > memory_breakdown () const override; 59 60// state write/load 61 62void state_write ( llama_io_write_i & io, llama_seq_id seq_id = -1 , llama_state_seq_flags flags = 0 ) const override; 63void state_read ( llama_io_read_i & io, llama_seq_id seq_id = -1 , llama_state_seq_flags flags = 0 ) override; 64 65// llama_kv_cache_msa specific API 66 67llama_kv_cache * get_base () const; 68llama_kv_cache * get_idx () const; 69 70uint32_t get_n_pad () const { return n_pad; } 71uint32_t get_n_seq_max () const { return n_seq_max; } 72uint32_t get_n_swa () const { return n_swa; } 73llama_swa_type get_swa_type () const { return swa_type; } 74 75private : 76// keep the indexer KV cache hparams instance here as llama_kv_cache stores only a reference 77llama_hparams hparams_idx; 78 79const uint32_t n_stream = 1 ; 80const uint32_t n_seq_max = 1 ; 81const uint32_t n_pad = 1 ; 82 83const uint32_t n_swa = 0 ; 84const llama_swa_type swa_type = LLAMA_SWA_TYPE_NONE ; 85 86std ::unique_ptr < llama_kv_cache > kv_base; 87std ::unique_ptr < llama_kv_cache > kv_idx; 88}; 89 90class llama_kv_cache_msa_context : public llama_memory_context_i { 91public : 92using slot_info_vec_t = llama_kv_cache::slot_info_vec_t; 93 94// used for errors 95llama_kv_cache_msa_context( llama_memory_status status); 96 97// used to create a full-cache context 98llama_kv_cache_msa_context ( 99llama_kv_cache_msa * kv); 100 101// used to create an update context 102llama_kv_cache_msa_context ( 103llama_kv_cache_msa * kv, 104llama_context * lctx, 105bool optimize); 106 107// used to create a batch processing context from a batch 108llama_kv_cache_msa_context ( 109llama_kv_cache_msa * kv, 110slot_info_vec_t sinfos_base, 111slot_info_vec_t sinfos_idx, 112std::vector < llama_ubatch > ubatches); 113 114virtual ~ llama_kv_cache_msa_context (); 115 116// llama_memory_context_i 117 118bool next () override ; 119bool apply () override; 120 121llama_memory_status get_status () const override ; 122const llama_ubatch & get_ubatch () const override; 123 124// llama_kv_cache_msa_context specific API 125 126const llama_kv_cache_context * get_base () const; 127const llama_kv_cache_context * get_idx () const; 128 129// max position currently present in the cache plus one, padded MSA blocks are defined over token positions 130// so the block-selection tensors are sized by this value rather than by the number of cells 131uint32_t get_n_pos () const; 132 133// position <-> cell translation maps, populated from the base cache cells 134// the model graph relates cache contents to token positions only through these per ubatch inputs 135// value for empty or other-sequence cells is 0 so consumers must mask them 136void set_input_cell_pos ( ggml_tensor * dst, const llama_ubatch * ubatch, int32_t div) const; 137// positions without a cell map to cell 0, consumers must mask them assumes one sequence per stream 138void set_input_pos_slot ( ggml_tensor * dst, const llama_ubatch * ubatch) const; 139void set_input_pos_mask ( ggml_tensor * dst, const llama_ubatch * ubatch) const; 140 141private: 142llama_kv_cache_msa * kv; 143 144// the index of the next ubatch to process 145size_t i_next = 0 ; 146 147std ::vector < llama_ubatch > ubatches; 148 149const llama_memory_context_ptr ctx_base; 150const llama_memory_context_ptr ctx_idx; 151 152const llama_memory_status status ; 153};