char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
563dec81c
char/aliceai-t5-moe
1#include "llama-kv-cache-dsa.h" 2 3#include "llama-impl.h" 4#include "llama-batch.h" 5#include "llama-model.h" 6 7#include <algorithm> 8#include <cassert> 9 10// 11// llama_kv_cache_dsa 12// 13 14llama_kv_cache_dsa::llama_kv_cache_dsa ( 15const llama_model & model , 16ggml_type type_k , 17ggml_type type_v , 18bool v_trans , 19bool offload , 20bool unified , 21uint32_t kv_size , 22uint32_t n_seq_max , 23uint32_t n_pad , 24uint32_t n_swa , 25llama_swa_type swa_type , 26const layer_filter_cb & filter_mla , 27const layer_filter_cb & filter_lid , 28const layer_reuse_cb & reuse ) : 29hparams_lid (model .hparams ),n_stream (unified ?1 :n_seq_max ) { 30 31LLAMA_LOG_INFO ("%s: creating main KV cache, size = %u cells\n" ,__func__ ,kv_size ); 32 33kv_mla = std::make_unique < llama_kv_cache > ( 34model ,model .hparams ,type_k ,type_v , 35v_trans ,offload ,unified ,kv_size ,n_seq_max ,n_pad , 36n_swa ,swa_type ,nullptr ,filter_mla ,reuse ,nullptr ); 37 38// we use llama_kv_cache for caching indexer keys 39// by hand-tweaking some hparams we fool it to create 40// indexer key cache tensors with correct dimensions 41// https://github.com/ggml-org/llama.cpp/pull/21149#discussion_r3015940823 42 43// DSA lightning indexer uses MQA with single key head 44 std::fill (hparams_lid .n_head_kv_arr .begin (),hparams_lid .n_head_kv_arr .end (),1 ); 45hparams_lid .n_embd_head_k_full = model .hparams .indexer_head_size ; 46hparams_lid .rope_type = LLAMA_ROPE_TYPE_NEOX ; 47 48LLAMA_LOG_INFO ("%s: creating indexer KV cache, size = %u cells\n" ,__func__ ,kv_size ); 49 50kv_lid = std::make_unique < llama_kv_cache > ( 51model ,hparams_lid ,type_k ,type_v , 52v_trans ,offload ,unified ,kv_size ,n_seq_max ,n_pad , 53n_swa ,swa_type ,nullptr ,filter_lid ,reuse ,nullptr ); 54} 55 56void llama_kv_cache_dsa::clear (bool data ) { 57kv_mla -> clear (data ); 58kv_lid -> clear (data ); 59} 60 61bool llama_kv_cache_dsa::seq_rm (llama_seq_id seq_id ,llama_pos p0 ,llama_pos p1 ) { 62bool res = true; 63 64res = res & kv_mla -> seq_rm (seq_id ,p0 ,p1 ); 65res = res & kv_lid -> seq_rm (seq_id ,p0 ,p1 ); 66 67return res ; 68} 69 70void llama_kv_cache_dsa::seq_cp (llama_seq_id seq_id_src ,llama_seq_id seq_id_dst ,llama_pos p0 ,llama_pos p1 ) { 71kv_mla -> seq_cp (seq_id_src ,seq_id_dst ,p0 ,p1 ); 72kv_lid -> seq_cp (seq_id_src ,seq_id_dst ,p0 ,p1 ); 73} 74 75void llama_kv_cache_dsa::seq_keep (llama_seq_id seq_id ) { 76kv_mla -> seq_keep (seq_id ); 77kv_lid -> seq_keep (seq_id ); 78} 79 80void llama_kv_cache_dsa::seq_add (llama_seq_id seq_id ,llama_pos p0 ,llama_pos p1 ,llama_pos shift ) { 81kv_mla -> seq_add (seq_id ,p0 ,p1 ,shift ); 82kv_lid -> seq_add (seq_id ,p0 ,p1 ,shift ); 83} 84 85void llama_kv_cache_dsa::seq_div (llama_seq_id seq_id ,llama_pos p0 ,llama_pos p1 ,int d ) { 86kv_mla -> seq_div (seq_id ,p0 ,p1 ,d ); 87kv_lid -> seq_div (seq_id ,p0 ,p1 ,d ); 88} 89 90llama_pos llama_kv_cache_dsa::seq_pos_min (llama_seq_id seq_id )const { 91return kv_mla -> seq_pos_min (seq_id ); 92} 93 94llama_pos llama_kv_cache_dsa::seq_pos_max (llama_seq_id seq_id )const { 95return kv_mla -> seq_pos_max (seq_id ); 96} 97 98std::map < ggml_backend_buffer_type_t ,size_t > llama_kv_cache_dsa::memory_breakdown ()const { 99 std::map < ggml_backend_buffer_type_t ,size_t > mb = kv_mla -> memory_breakdown (); 100for (const auto & buft_size :kv_lid -> memory_breakdown ()) { 101mb [buft_size .first ]+= buft_size .second ; 102 } 103return mb ; 104} 105 106llama_memory_context_ptr llama_kv_cache_dsa::init_batch ( 107llama_batch_allocr & balloc , 108uint32_t n_ubatch , 109bool embd_all ) { 110GGML_UNUSED (embd_all ); 111 112do { 113balloc .split_reset (); 114 115 std::vector < llama_ubatch > ubatches ; 116while (true) { 117auto ubatch = n_stream == 1 ?balloc .split_simple (n_ubatch ) :balloc .split_equal (n_ubatch , true,0 ); 118 119if (ubatch .n_tokens == 0 ) { 120break ; 121 } 122 123ubatches .push_back (std::move (ubatch ));// NOLINT 124 } 125 126if (balloc .get_n_used ()< balloc .get_n_tokens ()) { 127// failed to find a suitable split 128break ; 129 } 130 131auto sinfos_mla = kv_mla -> prepare (ubatches ); 132if (sinfos_mla .empty ()) { 133break ; 134 } 135 136auto sinfos_lid = kv_lid -> prepare (ubatches ); 137if (sinfos_lid .empty ()) { 138break ; 139 } 140 141assert (sinfos_mla .size ()== sinfos_lid .size ()); 142 143return std::make_unique < llama_kv_cache_dsa_context > ( 144this , std::move (sinfos_mla ), std::move (sinfos_lid ), std::move (ubatches )); 145 }while (false); 146 147return std::make_unique < llama_kv_cache_dsa_context > (LLAMA_MEMORY_STATUS_FAILED_PREPARE ); 148} 149 150llama_memory_context_ptr llama_kv_cache_dsa::init_full () { 151return std::make_unique < llama_kv_cache_dsa_context > (this ); 152} 153 154llama_memory_context_ptr llama_kv_cache_dsa::init_update (llama_context * lctx ,bool optimize ) { 155return std::make_unique < llama_kv_cache_dsa_context > (this ,lctx ,optimize ); 156} 157 158bool llama_kv_cache_dsa::get_can_shift ()const { 159return kv_mla -> get_can_shift ()&& 160kv_lid -> get_can_shift ()&& 161kv_mla -> get_size ()== kv_lid -> get_size (); 162} 163 164void llama_kv_cache_dsa::state_write (llama_io_write_i & io ,llama_seq_id seq_id ,llama_state_seq_flags flags )const { 165kv_mla -> state_write (io ,seq_id ,flags ); 166kv_lid -> state_write (io ,seq_id ,flags ); 167} 168 169void llama_kv_cache_dsa::state_read (llama_io_read_i & io ,llama_seq_id seq_id ,llama_state_seq_flags flags ) { 170kv_mla -> state_read (io ,seq_id ,flags ); 171kv_lid -> state_read (io ,seq_id ,flags ); 172} 173 174llama_kv_cache * llama_kv_cache_dsa::get_mla ()const { 175return kv_mla .get (); 176} 177 178llama_kv_cache * llama_kv_cache_dsa::get_lid ()const { 179return kv_lid .get (); 180} 181 182// 183// llama_kv_cache_dsa_context 184// 185 186llama_kv_cache_dsa_context::llama_kv_cache_dsa_context (llama_memory_status status ) :status (status ) {} 187 188llama_kv_cache_dsa_context::llama_kv_cache_dsa_context ( 189llama_kv_cache_dsa * kv ) : 190ctx_mla (kv -> get_mla ()-> init_full ()), 191ctx_lid (kv -> get_lid ()-> init_full ()), 192status (llama_memory_status_combine (ctx_mla -> get_status (),ctx_lid -> get_status ())) { 193} 194 195llama_kv_cache_dsa_context::llama_kv_cache_dsa_context ( 196llama_kv_cache_dsa * kv , 197llama_context * lctx , 198bool optimize ) : 199ctx_mla (kv -> get_mla ()-> init_update (lctx ,optimize )), 200ctx_lid (kv -> get_lid ()-> init_update (lctx ,optimize )), 201status (llama_memory_status_combine (ctx_mla -> get_status (),ctx_lid -> get_status ())) { 202} 203 204llama_kv_cache_dsa_context::llama_kv_cache_dsa_context ( 205llama_kv_cache_dsa * kv , 206slot_info_vec_t sinfos_mla , 207slot_info_vec_t sinfos_lid , 208 std::vector < llama_ubatch > ubatches ) : 209ubatches (std::move (ubatches )), 210// note: here we copy the ubatches. not sure if this is ideal 211ctx_mla (new llama_kv_cache_context (kv -> get_mla (), std::move (sinfos_mla ),this -> ubatches )), 212ctx_lid (new llama_kv_cache_context (kv -> get_lid (), std::move (sinfos_lid ),this -> ubatches )), 213status (llama_memory_status_combine (ctx_mla -> get_status (),ctx_lid -> get_status ())) { 214} 215 216llama_kv_cache_dsa_context:: ~llama_kv_cache_dsa_context ()= default ; 217 218bool llama_kv_cache_dsa_context::next () { 219assert (status == LLAMA_MEMORY_STATUS_SUCCESS ); 220 221ctx_mla -> next (); 222ctx_lid -> next (); 223 224if (++ i_next >=ubatches .size ()) { 225return false; 226 } 227 228return true; 229} 230 231bool llama_kv_cache_dsa_context::apply () { 232assert (!llama_memory_status_is_fail (status )); 233 234bool res = true; 235 236res = res & ctx_mla -> apply (); 237res = res & ctx_lid -> apply (); 238 239return res ; 240} 241 242llama_memory_status llama_kv_cache_dsa_context::get_status ()const { 243return status ; 244} 245 246const llama_ubatch & llama_kv_cache_dsa_context::get_ubatch ()const { 247assert (status == LLAMA_MEMORY_STATUS_SUCCESS ); 248 249return ubatches [i_next ]; 250} 251 252const llama_kv_cache_context * llama_kv_cache_dsa_context::get_mla ()const { 253assert (status == LLAMA_MEMORY_STATUS_SUCCESS ); 254 255return static_cast < const llama_kv_cache_context *> (ctx_mla .get ()); 256} 257 258const llama_kv_cache_context * llama_kv_cache_dsa_context::get_lid ()const { 259assert (status == LLAMA_MEMORY_STATUS_SUCCESS ); 260 261return static_cast < const llama_kv_cache_context *> (ctx_lid .get ()); 262}