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