char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
596a5795b
char/aliceai-t5-moe
1#pragma once 2 3#include "llama-kv-cache.h" 4#include "llama-kv-cache-iswa.h" 5 6#include <map> 7#include <memory> 8#include <unordered_map> 9#include <vector> 10 11class llama_dsv4_comp_state { 12public : 13using stream_copy_info = llama_kv_cache ::stream_copy_info ; 14 15stream_copy_info sc_info ; 16 17llama_dsv4_comp_state ( 18const llama_model & model , 19bool offload , 20bool unified , 21uint32_t n_seq_max , 22uint32_t ratio , 23uint32_t state_size , 24uint32_t n_embd_state , 25uint32_t n_rs_seq , 26const char * name , 27const llama_memory_i ::layer_filter_cb & filter ); 28 29void clear (llama_seq_id seq_id ,bool data ); 30void seq_cp (llama_seq_id seq_id_src ,llama_seq_id seq_id_dst ); 31void apply_copies (const stream_copy_info & sc_info )const ; 32 33uint32_t get_ratio ()const ; 34uint32_t get_state_size ()const ; 35uint32_t get_n_stream ()const ; 36uint32_t get_n_rs_seq ()const ; 37uint32_t get_n_rows ()const ; 38 39std ::map < ggml_backend_buffer_type_t ,size_t > memory_breakdown ()const ; 40 41void state_write (llama_io_write_i & io ,llama_seq_id seq_id ,llama_state_seq_flags flags ,const std ::vector < uint32_t > & rs_idx )const ; 42void state_read (llama_io_read_i & io ,llama_seq_id seq_id ,llama_state_seq_flags flags ); 43 44ggml_tensor * get_kv (ggml_context * ctx ,int32_t il )const ; 45ggml_tensor * get_score (ggml_context * ctx ,int32_t il )const ; 46ggml_tensor * get_kv_all (ggml_context * ctx ,int32_t il )const ; 47ggml_tensor * get_score_all (ggml_context * ctx ,int32_t il )const ; 48 49ggml_tensor * cpy_kv (ggml_context * ctx ,ggml_tensor * cur ,ggml_tensor * idxs ,int32_t il )const ; 50ggml_tensor * cpy_score (ggml_context * ctx ,ggml_tensor * cur ,ggml_tensor * idxs ,int32_t il )const ; 51 52private : 53struct layer { 54uint32_t il ; 55 56ggml_tensor * kv ; 57ggml_tensor * score ; 58 59std ::vector < ggml_tensor *> kv_stream ; 60std ::vector < ggml_tensor *> score_stream ; 61 }; 62 63const uint32_t ratio; 64const uint32_t state_size; 65const uint32_t n_embd_state; 66const uint32_t n_stream; 67const uint32_t n_rs_seq; 68 69std ::vector < std::pair < ggml_context_ptr, ggml_backend_buffer_ptr>> ctxs_bufs; 70 71std ::vector < layer > layers; 72 73std ::unordered_map < int32_t , int32_t > map_layer_ids; 74 75size_t total_size () const; 76}; 77 78// 79// llama_kv_cache_dsv4 80// 81 82// DSV4 uses a normal raw/SWA token cache plus compressed K-only block caches. 83// The compressed caches are storage only; DSV4-specific visibility and block 84// planning are handled by llama_kv_cache_dsv4_context / llm_graph_input_dsv4. 85// FIXME: currently the cache only supports non-unified mode even if unified flag is passed 86// FIXME: we currently conflate token_pos and buffer contents. See https://github.com/ggml-org/llama.cpp/pull/25521#discussion_r3558173819 87 88class llama_kv_cache_dsv4 : public llama_memory_i { 89public: 90llama_kv_cache_dsv4 ( 91const llama_model & model, 92ggml_type type_k, 93ggml_type type_v, 94bool v_trans, 95bool offload, 96bool swa_full, 97bool unified, 98uint32_t kv_size, 99uint32_t n_seq_max, 100uint32_t n_ubatch, 101uint32_t n_pad, 102uint32_t n_rs_seq, 103const layer_filter_cb & filter, 104const layer_reuse_cb & reuse); 105 106~llama_kv_cache_dsv4() = default ; 107 108// 109// llama_memory_i 110// 111 112llama_memory_context_ptr init_batch ( 113llama_batch_allocr & balloc, 114uint32_t n_ubatch, 115bool embd_all) override; 116 117llama_memory_context_ptr init_full () override; 118 119llama_memory_context_ptr init_update ( llama_context * lctx, bool optimize) override; 120 121bool get_can_shift () const override; 122 123void clear ( bool data) override; 124 125bool seq_rm ( llama_seq_id seq_id, llama_pos p0, llama_pos p1) override; 126void seq_cp ( llama_seq_id seq_id_src, llama_seq_id seq_id_dst, llama_pos p0, llama_pos p1) override; 127void seq_keep ( llama_seq_id seq_id) override; 128void seq_add ( llama_seq_id seq_id, llama_pos p0, llama_pos p1, llama_pos shift) override; 129void seq_div ( llama_seq_id seq_id, llama_pos p0, llama_pos p1, int d) override; 130 131llama_pos seq_pos_min ( llama_seq_id seq_id) const override; 132llama_pos seq_pos_max ( llama_seq_id seq_id) const override; 133 134std::map < ggml_backend_buffer_type_t, size_t > memory_breakdown () const override; 135 136void state_write ( llama_io_write_i & io, llama_seq_id seq_id = -1 , llama_state_seq_flags flags = 0 ) const override; 137void state_read ( llama_io_read_i & io, llama_seq_id seq_id = -1 , llama_state_seq_flags flags = 0 ) override; 138 139// 140// llama_kv_cache_dsv4 specific API 141// 142 143llama_kv_cache_iswa * get_raw () const; 144llama_kv_cache * get_csa () const; 145llama_kv_cache * get_hca () const; 146llama_kv_cache * get_lid () const; 147llama_dsv4_comp_state * get_csa_state () const; 148llama_dsv4_comp_state * get_hca_state () const; 149llama_dsv4_comp_state * get_lid_state () const; 150 151uint32_t get_n_rs_seq () const; 152const std ::vector < uint32_t > & get_rs_idx () const; 153void reset_rs_idx_for_ubatches ( const std ::vector < llama_ubatch > & ubatches); 154 155private : 156llama_hparams hparams_raw; 157llama_hparams hparams_csa; 158llama_hparams hparams_hca; 159llama_hparams hparams_lid; 160 161const uint32_t n_seq_max; 162const uint32_t n_rs_seq; 163 164std ::vector < uint32_t > rs_idx; 165 166std ::unique_ptr < llama_kv_cache_iswa > kv_raw; 167std ::unique_ptr < llama_kv_cache > kv_csa; 168std ::unique_ptr < llama_kv_cache > kv_hca; 169std ::unique_ptr < llama_kv_cache > kv_lid; 170std ::unique_ptr < llama_dsv4_comp_state > csa_state; 171std ::unique_ptr < llama_dsv4_comp_state > hca_state; 172std ::unique_ptr < llama_dsv4_comp_state > lid_state; 173 174void clear_compressed ( llama_seq_id seq_id, bool data); 175}; 176 177// DSV4 raw attention only uses the SWA half of kv_raw. The base half is kept 178// for generic ISWA bookkeeping, but it has no DSV4 layers to expose here. 179class llama_kv_cache_dsv4_raw_context : public llama_memory_context_i { 180public : 181using slot_info_vec_t = llama_kv_cache::slot_info_vec_t; 182 183llama_kv_cache_dsv4_raw_context (llama_kv_cache_iswa * kv); 184 185llama_kv_cache_dsv4_raw_context ( 186llama_kv_cache_iswa * kv, 187llama_context * lctx, 188bool optimize); 189 190llama_kv_cache_dsv4_raw_context ( 191llama_kv_cache_iswa * kv, 192slot_info_vec_t sinfos_base_write, 193slot_info_vec_t sinfos_swa_write, 194slot_info_vec_t sinfos_swa_read, 195std::vector < llama_ubatch > ubatches, 196std::vector < llama_ubatch > ubatches_write); 197 198bool next () override ; 199bool apply () override; 200 201llama_memory_status get_status () const override ; 202const llama_ubatch & get_ubatch () const override; 203 204uint32_t get_n_kv () const; 205uint32_t get_n_write () const; 206 207ggml_tensor * get_k ( ggml_context * ctx, int32_t il) const; 208ggml_tensor * cpy_k ( ggml_context * ctx, ggml_tensor * k_cur, ggml_tensor * k_idxs, int32_t il) const; 209 210ggml_tensor * build_input_k_idxs ( ggml_context * ctx, const llama_ubatch & ubatch) const; 211ggml_tensor * build_input_k_rot ( ggml_context * ctx) const; 212 213void set_input_k_idxs ( ggml_tensor * dst) const; 214void set_input_kq_mask ( ggml_tensor * dst, const llama_ubatch * ubatch, bool causal_attn) const; 215void set_input_k_rot ( ggml_tensor * dst) const; 216 217private: 218size_t i_next = 0 ; 219 220llama_kv_cache * kv_swa = nullptr ; 221 222slot_info_vec_t sinfos_write; 223slot_info_vec_t sinfos_read; 224std ::vector < llama_ubatch > ubatches; 225std ::vector < llama_ubatch > ubatches_write; 226 227const llama_memory_context_ptr ctx_base_mem; 228const llama_memory_context_ptr ctx_swa_mem; 229 230uint32_t n_kv = 0 ; 231 232const llama_memory_status status; 233}; 234 235// DSV4 compressed KV rows are graph outputs, not normal token KV writes. 236// Keep a small context that exposes K tensors without generic apply() semantics. 237class llama_kv_cache_dsv4_comp_context { 238public : 239using slot_info_vec_t = llama_kv_cache::slot_info_vec_t; 240 241llama_kv_cache_dsv4_comp_context (llama_kv_cache * kv); 242 243llama_kv_cache_dsv4_comp_context ( 244llama_kv_cache * kv, 245slot_info_vec_t sinfos, 246std::vector < llama_ubatch > ubatches); 247 248bool next (); 249 250uint32_t get_n_kv () const ; 251 252ggml_tensor * get_k ( ggml_context * ctx, int32_t il) const; 253ggml_tensor * cpy_k ( ggml_context * ctx, ggml_tensor * k_cur, ggml_tensor * k_idxs, int32_t il) const; 254 255ggml_tensor * build_input_k_rot ( ggml_context * ctx) const; 256void set_input_k_rot ( ggml_tensor * dst) const; 257 258private: 259llama_kv_cache * kv; 260 261size_t i_cur = 0 ; 262slot_info_vec_t sinfos; 263std ::vector < llama_ubatch > ubatches; 264 265uint32_t n_kv; 266}; 267 268class llama_kv_cache_dsv4_context : public llama_memory_context_i { 269public : 270using slot_info_vec_t = llama_kv_cache::slot_info_vec_t; 271using stream_copy_info = llama_kv_cache::stream_copy_info; 272 273struct comp_plan { 274// Per-ubatch recipe for updating compressor state, committing completed 275// compressed rows, and masking the compressed attention source. 276 277// APE row ids, i.e. pos % ratio, for the compressor-state updates. 278std :: vector < int32_t > state_pos ; 279 280// Current-ubatch source row ids and unique persistent-state 281// destination row ids for deterministic ring-state updates. 282std :: vector < int32_t > state_persist_src_idxs ; 283std :: vector < int32_t > state_persist_dst_idxs ; 284 285// Device-side rollback restore copies snapshot planes back to the 286// current compressor-state plane before the graph reads it. 287std :: vector < int32_t > state_restore_src_idxs ; 288std :: vector < int32_t > state_restore_dst_idxs ; 289 290// Device-side rollback snapshots copy rows from the graph-local 291// [persistent_state | current_ubatch_scratch] tensor into rollback 292// planes after the graph has computed current-token compressor state. 293std :: vector < int32_t > state_snapshot_src_idxs ; 294std :: vector < int32_t > state_snapshot_dst_idxs ; 295 296// Flattened source row ids used for state-backed commits. Source rows 297// index the graph-local [persistent_state | current_ubatch_scratch] 298// tensor. For overlapped compression the first half is previous rows 299// and the second half is current rows; a final synthetic zero/-inf row 300// may be addressed for the first block's previous half. 301std :: vector < int32_t > state_read_idxs ; 302 303// Final compressed-cache row ids written by state-backed commits. 304// A non-boundary CSA/LID decode step can target a masked scratch row. 305std :: vector < int64_t > state_write_idxs ; 306 307// RoPE positions for state-backed commits. 308std :: vector < int32_t > state_write_pos ; 309 310// Number of completed compressed rows visible for each query token. 311std :: vector < int32_t > n_visible ; 312 313// Number of streams used by the attention graph for this ubatch. 314int64_t n_stream = 1 ; 315 316// Graph-width for compressed rows. This can be larger than n_visible 317// so masked padding rows do not force a new graph at every CSA block. 318int64_t n_kv = 0 ; 319}; 320 321llama_kv_cache_dsv4_context( llama_memory_status status); 322 323llama_kv_cache_dsv4_context ( 324llama_kv_cache_dsv4 * kv); 325 326llama_kv_cache_dsv4_context ( 327llama_kv_cache_dsv4 * kv, 328llama_context * lctx, 329bool optimize, 330stream_copy_info sc_info_csa, 331stream_copy_info sc_info_hca, 332stream_copy_info sc_info_lid); 333 334llama_kv_cache_dsv4_context ( 335llama_kv_cache_dsv4 * kv, 336slot_info_vec_t sinfos_raw_base_write, 337slot_info_vec_t sinfos_raw_swa_write, 338slot_info_vec_t sinfos_raw_swa_read, 339std::vector < llama_ubatch > ubatches, 340std::vector < llama_ubatch > ubatches_raw); 341 342virtual ~ llama_kv_cache_dsv4_context (); 343 344// 345// llama_memory_context_i 346// 347 348bool next () override ; 349bool apply () override; 350 351llama_memory_status get_status () const override ; 352const llama_ubatch & get_ubatch () const override; 353 354// 355// llama_kv_cache_dsv4_context specific API 356// 357 358const llama_kv_cache_dsv4_raw_context * get_raw () const; 359const llama_kv_cache_dsv4_comp_context * get_csa () const; 360const llama_kv_cache_dsv4_comp_context * get_hca () const; 361const llama_kv_cache_dsv4_comp_context * get_lid () const; 362const llama_dsv4_comp_state * get_csa_state () const; 363const llama_dsv4_comp_state * get_hca_state () const; 364const llama_dsv4_comp_state * get_lid_state () const; 365 366const comp_plan & get_csa_plan () const; 367const comp_plan & get_hca_plan () const; 368const comp_plan & get_lid_plan () const; 369 370const comp_plan & get_csa_plan ( const llama_ubatch & ubatch) const; 371const comp_plan & get_hca_plan ( const llama_ubatch & ubatch) const; 372const comp_plan & get_lid_plan ( const llama_ubatch & ubatch) const; 373 374private: 375size_t i_next = 0 ; 376 377std ::vector < llama_ubatch > ubatches; 378 379std ::vector < comp_plan > plans_csa; 380std ::vector < comp_plan > plans_hca; 381std ::vector < comp_plan > plans_lid; 382 383const std ::unique_ptr < llama_kv_cache_dsv4_raw_context > ctx_raw; 384const llama_memory_context_ptr ctx_csa_mem; 385const llama_memory_context_ptr ctx_hca_mem; 386const llama_memory_context_ptr ctx_lid_mem; 387 388const std ::unique_ptr < llama_kv_cache_dsv4_comp_context > ctx_csa; 389const std ::unique_ptr < llama_kv_cache_dsv4_comp_context > ctx_hca; 390const std ::unique_ptr < llama_kv_cache_dsv4_comp_context > ctx_lid; 391 392llama_dsv4_comp_state * csa_state = nullptr ; 393llama_dsv4_comp_state * hca_state = nullptr ; 394llama_dsv4_comp_state * lid_state = nullptr ; 395 396stream_copy_info sc_info_csa; 397stream_copy_info sc_info_hca; 398stream_copy_info sc_info_lid; 399 400bool reserve_plans = false; 401mutable comp_plan reserve_plan_csa; 402mutable comp_plan reserve_plan_hca; 403mutable comp_plan reserve_plan_lid; 404 405const llama_memory_status status ; 406};