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