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-memory.h" 6 7#include <map> 8#include <set> 9#include <vector> 10 11// 12// llama_memory_recurrent 13// 14 15// TODO: extract the cache state used for graph computation into llama_memory_recurrent_context_i 16// see the implementation of llama_kv_cache_context_i for an example how to do it 17class llama_memory_recurrent :public llama_memory_i { 18public : 19llama_memory_recurrent ( 20const llama_model & model , 21ggml_type type_r , 22ggml_type type_s , 23bool offload , 24uint32_t mem_size , 25uint32_t n_seq_max , 26uint32_t n_rs_seq , 27const layer_filter_cb & filter ); 28 29 ~llama_memory_recurrent ()= default ; 30 31// 32// llama_memory_i 33// 34 35llama_memory_context_ptr init_batch ( 36llama_batch_allocr & balloc , 37uint32_t n_ubatch , 38bool embd_all )override ; 39 40llama_memory_context_ptr init_full ()override ; 41 42llama_memory_context_ptr init_update ( llama_context * lctx, bool optimize) override; 43 44void clear ( bool data) override; 45 46bool seq_rm ( llama_seq_id seq_id, llama_pos p0, llama_pos p1) override; 47void seq_cp ( llama_seq_id seq_id_src, llama_seq_id seq_id_dst, llama_pos p0, llama_pos p1) override; 48void seq_keep ( llama_seq_id seq_id) override; 49void seq_add ( llama_seq_id seq_id, llama_pos p0, llama_pos p1, llama_pos shift) override; 50void seq_div ( llama_seq_id seq_id, llama_pos p0, llama_pos p1, int d) override; 51 52llama_pos seq_pos_min ( llama_seq_id seq_id) const override; 53llama_pos seq_pos_max ( llama_seq_id seq_id) const override; 54 55std::map < ggml_backend_buffer_type_t, size_t > memory_breakdown () const override; 56 57bool prepare ( const std ::vector < llama_ubatch > & ubatches); 58 59// find a contiguous slot of memory cells and emplace the ubatch there 60bool find_slot ( const llama_ubatch & ubatch); 61 62bool get_can_shift () const override; 63 64// state write/load 65 66void state_write ( llama_io_write_i & io, llama_seq_id seq_id = -1 , llama_state_seq_flags flags = 0 ) const override; 67void state_read ( llama_io_read_i & io, llama_seq_id seq_id = -1 , llama_state_seq_flags flags = 0 ) override; 68 69uint32_t head = 0 ; // the location where the batch will be placed in the cache (see find_slot()) 70uint32_t size = 0 ; // total number of cells, shared across all sequences 71uint32_t used = 0 ; // used cells (i.e. at least one seq_id) 72 73// number of recurrent-state snapshots per seq for rollback; tensors are widened to (1 + n_rs_seq) groups 74uint32_t n_rs_seq = 0 ; 75 76// per-seq rollback index 77std ::vector < uint32_t > rs_idx; 78 79void set_rs_idx ( llama_seq_id seq_id, uint32_t idx); 80 81// computed before each graph build 82uint32_t n = 0 ; 83 84// first zero-ed state 85int32_t rs_z = -1 ; 86 87// TODO: optimize for recurrent state needs 88struct mem_cell { 89llama_pos pos = -1 ; 90int32_t src = -1 ; // used to know where states should be copied from 91int32_t src0 = -1 ; // like src, but only used when setting the inputs (allowing to copy once) 92int32_t tail = -1 ; 93 94std :: set < llama_seq_id > seq_id ; 95 96bool has_seq_id ( const llama_seq_id & id) const { 97return seq_id. find ( id ) != seq_id . end (); 98} 99 100bool is_empty () const { 101return seq_id. empty (); 102} 103 104bool is_same_seq ( const mem_cell & other) const { 105return seq_id == other. seq_id ; 106} 107}; 108 109std ::vector < mem_cell > cells; 110 111// per layer 112std ::vector < ggml_tensor *> r_l; 113std ::vector < ggml_tensor *> s_l; 114// a second conv history that must stay replicated across devices, so it cannot share the r row 115std ::vector < ggml_tensor *> p_l; 116 117private : 118//const llama_model & model; 119const llama_hparams & hparams; 120 121const uint32_t n_seq_max = 1 ; 122 123// ggml contexts for the KV cache along with the allocated backend buffers: 124std ::vector < std::pair < ggml_context_ptr, ggml_backend_buffer_ptr>> ctxs_bufs; 125 126size_t total_size () const ; 127 128size_t size_r_bytes () const; 129size_t size_s_bytes () const; 130size_t size_p_bytes () const; 131 132void state_write_meta ( llama_io_write_i & io, const std ::vector < std::pair < uint32_t , uint32_t >> & cell_ranges, llama_seq_id seq_id = -1 ) const ; 133void state_write_data ( llama_io_write_i & io, const std ::vector < std::pair < uint32_t , uint32_t >> & cell_ranges) const; 134 135bool state_read_meta ( llama_io_read_i & io, uint32_t cell_count, llama_seq_id dest_seq_id = -1 ); 136bool state_read_data ( llama_io_read_i & io, uint32_t cell_count); 137}; 138 139class llama_memory_recurrent_context : public llama_memory_context_i { 140public : 141// used for errors 142llama_memory_recurrent_context( llama_memory_status status); 143 144// used to create a full-cache or update context 145llama_memory_recurrent_context ( 146llama_memory_recurrent * mem); 147 148// used to create a batch processing context from a batch 149llama_memory_recurrent_context ( 150llama_memory_recurrent * mem, 151std::vector < llama_ubatch > ubatches); 152 153virtual ~ llama_memory_recurrent_context (); 154 155// 156// llama_memory_context_i 157// 158 159bool next () override ; 160bool apply () override; 161 162llama_memory_status get_status () const override ; 163const llama_ubatch & get_ubatch () const override; 164 165// 166// llama_memory_recurrent_context specific API 167// 168 169uint32_t get_n_rs () const; 170uint32_t get_head () const; 171int32_t get_rs_z () const; 172uint32_t get_size () const; 173 174ggml_tensor * get_r_l ( int32_t il) const; 175ggml_tensor * get_s_l ( int32_t il) const; 176ggml_tensor * get_p_l ( int32_t il) const; 177 178int32_t s_copy ( int i) const; 179 180private: 181const llama_memory_status status; 182 183llama_memory_recurrent * mem; 184 185size_t i_next = 0 ; 186 187std ::vector < llama_ubatch > ubatches; 188 189// 190// data needed for building the compute graph for the current ubatch: 191// TODO: extract all the state like `head` and `n` here 192// 193 194const bool is_full = false; 195};