char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
563dec81c
char/aliceai-t5-moe
1#pragma once 2 3#include "llama-kv-cache.h" 4 5#include <vector> 6 7// 8// llama_kv_cache_dsa 9// 10 11// utilizes two instances of llama_kv_cache: 12// - the first instance is for caching key tensors of the model, 13// - the second instance is for caching lightning indexer key tensors 14 15class llama_kv_cache_dsa :public llama_memory_i { 16public : 17llama_kv_cache_dsa ( 18const llama_model & model , 19ggml_type type_k , 20ggml_type type_v , 21bool v_trans , 22bool offload , 23bool unified , 24uint32_t kv_size , 25uint32_t n_seq_max , 26uint32_t n_pad , 27uint32_t n_swa , 28llama_swa_type swa_type , 29const layer_filter_cb & filter_mla , 30const layer_filter_cb & filter_lid , 31const layer_reuse_cb & reuse ); 32 33 ~llama_kv_cache_dsa ()= default ; 34 35// 36// llama_memory_i 37// 38 39llama_memory_context_ptr init_batch ( 40llama_batch_allocr & balloc , 41uint32_t n_ubatch , 42bool embd_all )override ; 43 44llama_memory_context_ptr init_full ()override ; 45 46llama_memory_context_ptr init_update ( llama_context * lctx, bool optimize) override; 47 48bool get_can_shift () const override; 49 50void clear ( bool data) override; 51 52bool seq_rm ( llama_seq_id seq_id, llama_pos p0, llama_pos p1) override; 53void seq_cp ( llama_seq_id seq_id_src, llama_seq_id seq_id_dst, llama_pos p0, llama_pos p1) override; 54void seq_keep ( llama_seq_id seq_id) override; 55void seq_add ( llama_seq_id seq_id, llama_pos p0, llama_pos p1, llama_pos shift) override; 56void seq_div ( llama_seq_id seq_id, llama_pos p0, llama_pos p1, int d) override; 57 58llama_pos seq_pos_min ( llama_seq_id seq_id) const override; 59llama_pos seq_pos_max ( llama_seq_id seq_id) const override; 60 61std::map < ggml_backend_buffer_type_t, size_t > memory_breakdown () const override; 62 63// state write/load 64 65void state_write ( llama_io_write_i & io, llama_seq_id seq_id = -1 , llama_state_seq_flags flags = 0 ) const override; 66void state_read ( llama_io_read_i & io, llama_seq_id seq_id = -1 , llama_state_seq_flags flags = 0 ) override; 67 68// 69// llama_kv_cache_dsa specific API 70// 71 72llama_kv_cache * get_mla () const; 73llama_kv_cache * get_lid () const; 74 75private : 76// we keep indexer KV cache hparams instance here as llama_kv_cache stores only reference to it 77llama_hparams hparams_lid; 78const uint32_t n_stream = 1 ; 79 80std ::unique_ptr < llama_kv_cache > kv_mla; 81std ::unique_ptr < llama_kv_cache > kv_lid; 82}; 83 84class llama_kv_cache_dsa_context : public llama_memory_context_i { 85public : 86using slot_info_vec_t = llama_kv_cache::slot_info_vec_t; 87 88// used for errors 89llama_kv_cache_dsa_context( llama_memory_status status); 90 91// used to create a full-cache context 92llama_kv_cache_dsa_context ( 93llama_kv_cache_dsa * kv); 94 95// used to create an update context 96llama_kv_cache_dsa_context ( 97llama_kv_cache_dsa * kv, 98llama_context * lctx, 99bool optimize); 100 101// used to create a batch processing context from a batch 102llama_kv_cache_dsa_context ( 103llama_kv_cache_dsa * kv, 104slot_info_vec_t sinfos_base, 105slot_info_vec_t sinfos_ik, 106std::vector < llama_ubatch > ubatches); 107 108virtual ~ llama_kv_cache_dsa_context (); 109 110// 111// llama_memory_context_i 112// 113 114bool next () override ; 115bool apply () override; 116 117llama_memory_status get_status () const override ; 118const llama_ubatch & get_ubatch () const override; 119 120// 121// llama_kv_cache_dsa_context specific API 122// 123 124const llama_kv_cache_context * get_mla () const; 125const llama_kv_cache_context * get_lid () const; 126 127private : 128//llama_kv_cache_dsa * kv; 129 130// the index of the next ubatch to process 131size_t i_next = 0 ; 132 133std ::vector < llama_ubatch > ubatches; 134 135const llama_memory_context_ptr ctx_mla; 136const llama_memory_context_ptr ctx_lid; 137 138const llama_memory_status status ; 139};