char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
6c84c7d5d
char/aliceai-t5-moe
1#include "llama-memory-recurrent.h" 2 3#include "ggml-backend.h" 4#include "llama-impl.h" 5#include "llama-io.h" 6#include "llama-batch.h" 7#include "llama-model.h" 8 9#include <algorithm> 10#include <cassert> 11#include <cstring> 12#include <limits> 13#include <map> 14#include <stdexcept> 15 16// 17// llama_memory_recurrent 18// 19 20llama_memory_recurrent::llama_memory_recurrent ( 21const llama_model & model , 22ggml_type type_r , 23ggml_type type_s , 24bool offload , 25uint32_t mem_size , 26uint32_t n_seq_max , 27uint32_t n_rs_seq , 28const layer_filter_cb & filter ) :hparams (model .hparams ),n_seq_max (n_seq_max ) { 29const int32_t n_layer = hparams .n_layer (); 30 31head = 0 ; 32size = mem_size ; 33used = 0 ; 34 35this -> n_rs_seq = n_rs_seq ; 36rs_idx .assign (n_seq_max ,0 ); 37 38cells .clear (); 39cells .resize (mem_size ); 40 41// define a comparator for the buft -> ctx map to ensure that the order is well-defined: 42struct ggml_backend_buft_comparator { 43bool operator()(const ggml_backend_buffer_type_t & lhs ,const ggml_backend_buffer_type_t & rhs )const { 44return strcmp (ggml_backend_buft_name (lhs ),ggml_backend_buft_name (rhs ))< 0 ; 45 } 46 }; 47 std::map < ggml_backend_buffer_type_t ,ggml_context_ptr ,ggml_backend_buft_comparator > ctx_map ; 48 49// create a context for each buffer type 50auto ctx_for_buft = [& ](ggml_backend_buffer_type_t buft )-> ggml_context * { 51auto it = ctx_map .find (buft ); 52if (it == ctx_map .end ()) { 53ggml_init_params params = { 54// r and s per layer, plus the separate PLE conv row where the model has one 55/*.mem_size =*/ size_t ((hparams .ple_conv_state ()> 0 ?3u :2u )* n_layer * ggml_tensor_overhead ()), 56/*.mem_buffer =*/ NULL , 57/*.no_alloc =*/ true, 58 }; 59 60ggml_context * ctx = ggml_init (params ); 61if (!ctx ) { 62return nullptr ; 63 } 64 65ctx_map .emplace (buft ,ctx ); 66 67return ctx ; 68 } 69 70return it -> second .get (); 71 }; 72 73r_l .resize (n_layer ); 74s_l .resize (n_layer ); 75p_l .resize (n_layer ); 76 77for (int i = 0 ;i < n_layer ;i ++ ) { 78if (filter && !filter (i )) { 79LLAMA_LOG_DEBUG ("%s: layer %3d: skipped\n" ,__func__ ,i ); 80continue ; 81 } 82 83const char * dev_name = "CPU" ; 84 85ggml_backend_buffer_type_t buft = ggml_backend_cpu_buffer_type (); 86 87if (offload ) { 88auto * dev = model .dev_layer (i ); 89buft = ggml_backend_dev_buffer_type (dev ); 90 91dev_name = ggml_backend_dev_name (dev ); 92 } 93 94LLAMA_LOG_DEBUG ("%s, layer %3d: dev = %s\n" ,__func__ ,i ,dev_name ); 95 96ggml_context * ctx = ctx_for_buft (buft ); 97if (!ctx ) { 98throw std::runtime_error ("failed to create ggml context for rs cache" ); 99 } 100 101const uint32_t n_rows = mem_size * (1 + n_rs_seq ); 102ggml_tensor * r = ggml_new_tensor_2d (ctx ,type_r ,hparams .n_embd_r (),n_rows ); 103ggml_tensor * s = ggml_new_tensor_2d (ctx ,type_s ,hparams .n_embd_s (),n_rows ); 104ggml_format_name (r ,"cache_r_l%d" ,i ); 105ggml_format_name (s ,"cache_s_l%d" ,i ); 106r_l [i ]= r ; 107s_l [i ]= s ; 108 109// the PLE history needs its own row: Meta must mirror it while the delta-net conv state next door stays split 110if (hparams .ple_conv_state ()> 0 && hparams .is_ple (i )) { 111ggml_tensor * p = ggml_new_tensor_2d (ctx ,type_r ,hparams .ple_conv_state (),n_rows ); 112ggml_format_name (p ,"cache_ple_r_l%d" ,i ); 113p_l [i ]= p ; 114 } 115 } 116 117// allocate tensors and initialize the buffers to avoid NaNs in the padding 118for (auto & [buft ,ctx ] :ctx_map ) { 119ggml_backend_buffer_t buf = ggml_backend_alloc_ctx_tensors_from_buft (ctx .get (),buft ); 120if (!buf ) { 121throw std::runtime_error ("failed to allocate buffer for rs cache" ); 122 } 123ggml_backend_buffer_clear (buf ,0 ); 124LLAMA_LOG_INFO ("%s: %10s RS buffer size = %8.2f MiB\n" ,__func__ ,ggml_backend_buffer_name (buf ),ggml_backend_buffer_get_size (buf )/1024.0 /1024.0 ); 125ctxs_bufs .emplace_back (std::move (ctx ),buf ); 126 } 127 128 { 129const size_t memory_size_r = size_r_bytes (); 130const size_t memory_size_s = size_s_bytes (); 131const size_t memory_size_p = size_p_bytes (); 132 133LLAMA_LOG_INFO ("%s: size = %7.2f MiB (%6u cells, %3d layers, %2u seqs %2u rs_seq), R (%s): %7.2f MiB, S (%s): %7.2f MiB, P (%s): %7.2f MiB\n" ,__func__ , 134 (float )(memory_size_r + memory_size_s + memory_size_p ) / (1024.0f * 1024.0f ),mem_size ,n_layer ,n_seq_max ,n_rs_seq , 135ggml_type_name (type_r ), (float )memory_size_r / (1024.0f * 1024.0f ), 136ggml_type_name (type_s ), (float )memory_size_s / (1024.0f * 1024.0f ), 137ggml_type_name (type_r ), (float )memory_size_p / (1024.0f * 1024.0f )); 138 } 139} 140 141void llama_memory_recurrent::clear (bool data ) { 142for (int32_t i = 0 ;i < (int32_t )size ;++ i ) { 143cells [i ].pos = -1 ; 144cells [i ].seq_id .clear (); 145cells [i ].src = -1 ; 146cells [i ].tail = -1 ; 147 } 148 149head = 0 ; 150used = 0 ; 151 152if (data ) { 153for (auto & [_ ,buf ] :ctxs_bufs ) { 154ggml_backend_buffer_clear (buf .get (),0 ); 155 } 156 } 157 158 std::fill (rs_idx .begin (),rs_idx .end (),0 ); 159} 160 161bool llama_memory_recurrent::seq_rm (llama_seq_id seq_id ,llama_pos p0 ,llama_pos p1 ) { 162uint32_t new_head = size ; 163 164if (p0 < 0 ) { 165p0 = 0 ; 166 } 167 168if (p1 < 0 ) { 169p1 = std::numeric_limits < llama_pos > ::max (); 170 } 171 172if ((uint32_t )seq_id >=this -> n_seq_max ) { 173LLAMA_LOG_ERROR ("%s: invalid seq_id (%d) - larger than n_seq_max (%d)\n" ,__func__ ,seq_id ,this -> n_seq_max ); 174return false; 175 } 176 177const bool rm_all = p0 == 0 && p1 == std::numeric_limits < llama_pos > ::max (); 178if (rm_all ) { 179set_rs_idx (seq_id ,0 ); 180 } 181 182// models like Mamba or RWKV can't have a state partially erased at the end 183// of the sequence because their state isn't preserved for previous tokens 184if (seq_id >= (int64_t )size ) { 185// could be fatal 186return false; 187 } 188if (0 <=seq_id ) { 189int32_t & tail_id = cells [seq_id ].tail ; 190if (tail_id >=0 ) { 191auto & cell = cells [tail_id ]; 192 193// partial rollback via per-token snapshot index (bounded by n_rs_seq) 194if (0 < p0 && p0 <=cell .pos && p1 > cell .pos ) { 195const llama_pos rollback = cell .pos - (p0 - 1 ); 196// pending rollback is single-use 197const bool pending = rs_idx [seq_id ]!= 0 ; 198if (!pending && rollback >=1 && rollback <= (llama_pos )n_rs_seq ) { 199set_rs_idx (seq_id , (uint32_t )rollback ); 200cell .pos = p0 - 1 ; 201return true; 202 } 203return false; 204 } 205// invalidate tails which will be cleared 206if (p0 <=cell .pos && cell .pos < p1 ) { 207tail_id = -1 ; 208 } 209 } 210 }else { 211// seq_id is negative, then the range should include everything or nothing 212if (p0 != p1 && (p0 != 0 || p1 != std::numeric_limits < llama_pos > ::max ())) { 213//printf("[DEBUG] inside `llama_memory_recurrent::seq_rm`: `seq_id` is negative, so returning false\n"); 214return false; 215 } 216 } 217 218for (uint32_t i = 0 ;i < size ;++ i ) { 219if (cells [i ].pos >=p0 && cells [i ].pos < p1 ) { 220if (seq_id < 0 ) { 221cells [i ].seq_id .clear (); 222 }else if (cells [i ].has_seq_id (seq_id )) { 223cells [i ].seq_id .erase (seq_id ); 224 }else { 225continue ; 226 } 227if (cells [i ].is_empty ()) { 228// keep count of the number of used cells 229if (cells [i ].pos >=0 ) { 230used -- ; 231 } 232cells [i ].pos = -1 ; 233cells [i ].src = -1 ; 234if (new_head == size ) { 235new_head = i ; 236 } 237 } 238 } 239 } 240 241// If we freed up a slot, set head to it so searching can start there. 242if (new_head != size && new_head < head ) { 243head = new_head ; 244 } 245 246return true; 247} 248 249void llama_memory_recurrent::seq_cp (llama_seq_id seq_id_src ,llama_seq_id seq_id_dst ,llama_pos p0 ,llama_pos p1 ) { 250if (seq_id_src == seq_id_dst ) { 251return ; 252 } 253 254if (p0 < 0 ) { 255p0 = 0 ; 256 } 257 258if (p1 < 0 ) { 259p1 = std::numeric_limits < llama_pos > ::max (); 260 } 261 262if ((uint32_t )seq_id_dst < size && (uint32_t )seq_id_src < size ) { 263auto & tail_src = cells [seq_id_src ]; 264auto & tail_dst = cells [seq_id_dst ]; 265if (tail_dst .tail >=0 ) { 266// clear destination seq_id if it wasn't empty 267auto & cell_dst = cells [tail_dst .tail ]; 268 269cell_dst .seq_id .erase (seq_id_dst ); 270tail_dst .tail = -1 ; 271if (cell_dst .seq_id .empty ()) { 272cell_dst .pos = -1 ; 273cell_dst .src = -1 ; 274used -= 1 ; 275 } 276 } 277if (tail_src .tail >=0 ) { 278auto & cell_src = cells [tail_src .tail ]; 279 280cell_src .seq_id .insert (seq_id_dst ); 281tail_dst .tail = tail_src .tail ; 282 } 283 } 284} 285 286void llama_memory_recurrent::seq_keep (llama_seq_id seq_id ) { 287uint32_t new_head = size ; 288 289for (uint32_t i = 0 ;i < size ;++ i ) { 290if ((llama_seq_id )i != seq_id ) { 291cells [i ].tail = -1 ; 292 } 293 294if (!cells [i ].has_seq_id (seq_id )) { 295if (cells [i ].pos >=0 ) { 296used -- ; 297 } 298 299cells [i ].pos = -1 ; 300cells [i ].src = -1 ; 301cells [i ].seq_id .clear (); 302 303if (new_head == size ){ 304new_head = i ; 305 } 306 }else { 307cells [i ].seq_id .clear (); 308cells [i ].seq_id .insert (seq_id ); 309 } 310 } 311 312// If we freed up a slot, set head to it so searching can start there. 313if (new_head != size && new_head < head ) { 314head = new_head ; 315 } 316} 317 318void llama_memory_recurrent::seq_add (llama_seq_id seq_id ,llama_pos p0 ,llama_pos p1 ,llama_pos shift ) { 319if (shift == 0 ) { 320return ; 321 } 322 323if (p0 < 0 ) { 324p0 = 0 ; 325 } 326 327if (p1 < 0 ) { 328p1 = std::numeric_limits < llama_pos > ::max (); 329 } 330 331// If there is no range then return early to avoid looping over the 332if (p0 == p1 ) { 333return ; 334 } 335 336// for Mamba-like or RWKV models, only the pos needs to be shifted 337if (0 <=seq_id && seq_id < (int64_t )size ) { 338const int32_t tail_id = cells [seq_id ].tail ; 339if (tail_id >=0 ) { 340auto & cell = cells [tail_id ]; 341if (cell .has_seq_id (seq_id )&& p0 <=cell .pos && cell .pos < p1 ) { 342cell .pos += shift ; 343 } 344 } 345 } 346} 347 348void llama_memory_recurrent::seq_div (llama_seq_id seq_id ,llama_pos p0 ,llama_pos p1 ,int d ) { 349if (d == 1 ) { 350return ; 351 } 352 353if (p0 < 0 ) { 354p0 = 0 ; 355 } 356 357if (p1 < 0 ) { 358p1 = std::numeric_limits < llama_pos > ::max (); 359 } 360 361// If there is no range then return early to avoid looping over the cache. 362if (p0 == p1 ) { 363return ; 364 } 365 366// for Mamba-like or RWKV models, only the pos needs to be changed 367if (0 <=seq_id && seq_id < (int64_t )size ) { 368const int32_t tail_id = cells [seq_id ].tail ; 369if (tail_id >=0 ) { 370auto & cell = cells [tail_id ]; 371if (cell .has_seq_id (seq_id )&& p0 <=cell .pos && cell .pos < p1 ) { 372cell .pos /=d ; 373 } 374 } 375 } 376} 377 378llama_pos llama_memory_recurrent::seq_pos_min (llama_seq_id seq_id )const { 379llama_pos result = std::numeric_limits < llama_pos > ::max (); 380 381for (uint32_t i = 0 ;i < size ;++ i ) { 382if (cells [i ].has_seq_id (seq_id )) { 383result = std::min (result ,cells [i ].pos ); 384 } 385 } 386 387if (result == std::numeric_limits < llama_pos > ::max ()) { 388result = -1 ; 389 } 390 391return result ; 392} 393 394llama_pos llama_memory_recurrent::seq_pos_max (llama_seq_id seq_id )const { 395llama_pos result = -1 ; 396 397for (uint32_t i = 0 ;i < size ;++ i ) { 398if (cells [i ].has_seq_id (seq_id )) { 399result = std::max (result ,cells [i ].pos ); 400 } 401 } 402 403return result ; 404} 405 406void llama_memory_recurrent::set_rs_idx (llama_seq_id seq_id ,uint32_t idx ) { 407if (seq_id < 0 ) { 408 std::fill (rs_idx .begin (),rs_idx .end (),0 ); 409return ; 410 } 411 412assert (n_seq_max == rs_idx .size ()); 413 414GGML_ASSERT ((uint32_t )seq_id < n_seq_max ); 415GGML_ASSERT (idx <=n_rs_seq ); 416 417rs_idx [seq_id ]= idx ; 418} 419 420std::map < ggml_backend_buffer_type_t ,size_t > llama_memory_recurrent::memory_breakdown ()const { 421 std::map < ggml_backend_buffer_type_t ,size_t > ret ; 422for (const auto & [_ ,buf ] :ctxs_bufs ) { 423ret [ggml_backend_buffer_get_type (buf .get ())]+= ggml_backend_buffer_get_size (buf .get ()); 424 } 425return ret ; 426} 427 428llama_memory_context_ptr llama_memory_recurrent::init_batch (llama_batch_allocr & balloc ,uint32_t n_ubatch ,bool embd_all ) { 429do { 430balloc .split_reset (); 431 432 std::vector < llama_ubatch > ubatches ; 433while (true) { 434llama_ubatch ubatch ; 435 436if (embd_all ) { 437// if all tokens are output, split by sequence 438ubatch = balloc .split_seq (n_ubatch ); 439 }else { 440// TODO: non-sequential equal split can be done if using unified KV cache 441// for simplicity, we always use sequential equal split for now 442// [TAG_RECURRENT_ROLLBACK_SPLITS] 443// the trailing (1 + n_rs_seq) tokens of each seq must stay in the same ubatch 444// so that the rollback snapshots remain valid 445ubatch = balloc .split_equal (n_ubatch , true,n_rs_seq > 0 ?n_rs_seq + 1 :0 ); 446 } 447 448if (ubatch .n_tokens == 0 ) { 449break ; 450 } 451 452ubatches .push_back (std::move (ubatch ));// NOLINT 453 } 454 455if (balloc .get_n_used ()< balloc .get_n_tokens ()) { 456// failed to find a suitable split 457break ; 458 } 459 460if (!prepare (ubatches )) { 461break ; 462 } 463 464return std::make_unique < llama_memory_recurrent_context > (this , std::move (ubatches )); 465 }while (false); 466 467return std::make_unique < llama_memory_recurrent_context > (LLAMA_MEMORY_STATUS_FAILED_PREPARE ); 468} 469 470llama_memory_context_ptr llama_memory_recurrent::init_full () { 471return std::make_unique < llama_memory_recurrent_context > (this ); 472} 473 474llama_memory_context_ptr llama_memory_recurrent::init_update (llama_context * lctx ,bool optimize ) { 475GGML_UNUSED (lctx ); 476GGML_UNUSED (optimize ); 477 478return std::make_unique < llama_memory_recurrent_context > (LLAMA_MEMORY_STATUS_NO_UPDATE ); 479} 480 481bool llama_memory_recurrent::prepare (const std::vector < llama_ubatch > & ubatches ) { 482// simply remember the full state because it is very small for this type of cache 483// TODO: optimize 484auto org_cells = cells ; 485auto org_used = used ; 486auto org_head = head ; 487 488bool success = true; 489 490for (const auto & ubatch :ubatches ) { 491if (!find_slot (ubatch )) { 492success = false; 493break ; 494 } 495 } 496 497// restore the original state 498cells = std::move (org_cells ); 499used = org_used ; 500head = org_head ; 501 502return success ; 503} 504 505bool llama_memory_recurrent::find_slot (const llama_ubatch & ubatch ) { 506const uint32_t n_seq_tokens = ubatch .n_seq_tokens ; 507const uint32_t n_seqs = ubatch .n_seqs ; 508 509// if we have enough unused cells before the current head -> 510// better to start searching from the beginning of the cache, hoping to fill it 511if (head > used + 2 * n_seqs ) { 512head = 0 ; 513 } 514 515// For recurrent state architectures (like Mamba or RWKV), 516// each cache cell can store the state for a whole sequence. 517// A slot should be always be contiguous. 518 519// can only process batches with an equal number of new tokens in each sequence 520GGML_ASSERT (ubatch .equal_seqs ()); 521 522int32_t min = size - 1 ; 523int32_t max = 0 ; 524 525// everything should fit if all seq_ids are smaller than the max 526for (uint32_t s = 0 ;s < n_seqs ;++ s ) { 527const uint32_t i = s * n_seq_tokens ;// first token of sequence set s 528const uint32_t n_seq_id = ubatch .n_seq_id [i ]; 529 530for (uint32_t j = 0 ;j < n_seq_id ;++ j ) { 531const llama_seq_id seq_id = ubatch .seq_id [i ][j ]; 532 533if (seq_id < 0 || (uint32_t )seq_id >=size ) { 534// too big seq_id 535// TODO: would it be possible to resize the cache instead? 536LLAMA_LOG_ERROR ("%s: seq_id=%d >= n_seq_max=%u Try using a bigger --parallel value\n" ,__func__ ,seq_id ,n_seq_max ); 537return false; 538 } 539if (j > 0 ) { 540auto & seq = cells [seq_id ]; 541if (seq .tail >=0 ) { 542auto & cell = cells [seq .tail ]; 543// clear cells from seq_ids that become shared 544// (should not normally happen, but let's handle it anyway) 545cell .seq_id .erase (seq_id ); 546seq .tail = -1 ; 547if (cell .seq_id .empty ()) { 548cell .pos = -1 ; 549cell .src = -1 ; 550used -= 1 ; 551 } 552 } 553 } 554 } 555 } 556 557#ifndef NDEBUG 558 { 559 std::vector < int32_t > tails_verif ; 560tails_verif .assign (size ,-1 ); 561for (uint32_t i = 0 ;i < size ;++ i ) { 562auto & cell = cells [i ]; 563for (llama_seq_id seq_id :cell .seq_id ) { 564if (tails_verif [seq_id ]!= -1 ) { 565LLAMA_LOG_ERROR ("%s: duplicate tail for seq_id %d in cell %d and %d\n" ,__func__ ,seq_id ,i ,tails_verif [seq_id ]); 566 } 567tails_verif [seq_id ]= i ; 568 } 569 } 570for (uint32_t i = 0 ;i < size ;++ i ) { 571if (tails_verif [i ]!= cells [i ].tail ) { 572LLAMA_LOG_ERROR ("%s: wrong tail for seq_id %d, (%d instead of %d)\n" ,__func__ ,i ,cells [i ].tail ,tails_verif [i ]); 573 } 574 } 575 } 576#endif 577 578// find next empty cell 579uint32_t next_empty_cell = head ; 580 581for (uint32_t i = 0 ;i < size ;++ i ) { 582if (next_empty_cell >=size ) {next_empty_cell -= size ; } 583auto & cell = cells [next_empty_cell ]; 584if (cell .is_empty ()) {break ; } 585next_empty_cell += 1 ; 586 } 587 588// find usable cell range 589for (uint32_t s = 0 ;s < n_seqs ;++ s ) { 590const uint32_t i = s * n_seq_tokens ; 591const llama_seq_id seq_id = ubatch .seq_id [i ][0 ]; 592auto & seq_meta = cells [seq_id ]; 593bool has_cell = false; 594if (seq_meta .tail >=0 ) { 595auto & cell = cells [seq_meta .tail ]; 596GGML_ASSERT (cell .has_seq_id (seq_id )); 597// does this seq_id "own" the cell? 598if (cell .seq_id .size ()== 1 ) {has_cell = true; } 599 } 600if (!has_cell ) { 601auto & empty_cell = cells [next_empty_cell ]; 602GGML_ASSERT (empty_cell .is_empty ()); 603// copy old tail into the empty cell 604if (seq_meta .tail >=0 ) { 605auto & orig_cell = cells [seq_meta .tail ]; 606empty_cell .pos = orig_cell .pos ; 607empty_cell .src = orig_cell .src ; 608orig_cell .seq_id .erase (seq_id ); 609empty_cell .seq_id .insert (seq_id );// will be overwritten 610GGML_ASSERT (!orig_cell .is_empty ());// has at least one remaining seq_id 611 } 612seq_meta .tail = next_empty_cell ; 613// find next empty cell 614if (s + 1 < n_seqs ) { 615for (uint32_t j = 0 ;j < size ;++ j ) { 616next_empty_cell += 1 ; 617if (next_empty_cell >=size ) {next_empty_cell -= size ; } 618auto & cell = cells [next_empty_cell ]; 619if (cell .is_empty ()) {break ; } 620 } 621 } 622 } 623if (min > seq_meta .tail ) {min = seq_meta .tail ; } 624if (max < seq_meta .tail ) {max = seq_meta .tail ; } 625 } 626 627// gather and re-order 628for (uint32_t s = 0 ;s < n_seqs ;++ s ) { 629const uint32_t i = s * n_seq_tokens ; 630const int32_t dst_id = s + min ; 631const int32_t src_id = cells [ubatch .seq_id [i ][0 ]].tail ; 632if (dst_id != src_id ) { 633auto & dst_cell = cells [dst_id ]; 634auto & src_cell = cells [src_id ]; 635 636 std::swap (dst_cell .pos ,src_cell .pos ); 637 std::swap (dst_cell .src ,src_cell .src ); 638 std::swap (dst_cell .seq_id ,src_cell .seq_id ); 639 640// swap tails 641for (uint32_t j = 0 ;j < size ;++ j ) { 642int32_t & tail = cells [j ].tail ; 643if (tail == src_id ) { 644tail = dst_id ; 645 }else if (tail == dst_id ) { 646tail = src_id ; 647 } 648 } 649 } 650 } 651 652// update the pos of the used seqs 653for (uint32_t s = 0 ;s < n_seqs ;++ s ) { 654const uint32_t i = s * n_seq_tokens ; 655const llama_pos last_pos = ubatch .pos [i + n_seq_tokens - 1 ]; 656const int32_t cell_id = s + min ; 657auto & cell = cells [cell_id ]; 658 659if (cell .pos >=0 && last_pos != cell .pos + (llama_pos )n_seq_tokens ) { 660// What should happen when the pos backtracks or skips a value? 661// Clearing the state mid-batch would require special-casing which isn't done. 662LLAMA_LOG_WARN ("%s: non-consecutive token position %d after %d for sequence %d with %u new tokens\n" , 663__func__ ,last_pos ,cell .pos ,ubatch .seq_id [i ][0 ],n_seq_tokens ); 664 } 665cell .pos = last_pos ; 666cell .seq_id .clear (); 667for (int32_t j = 0 ;j < ubatch .n_seq_id [i ];++ j ) { 668const llama_seq_id seq_id = ubatch .seq_id [i ][j ]; 669cell .seq_id .insert (seq_id ); 670cells [seq_id ].tail = cell_id ; 671 } 672 } 673 674// Find first cell without src refs, to use as the zero-ed state 675 { 676// TODO: bake-in src refcounts in the cell metadata 677 std::vector < int32_t > refcounts (size ,0 ); 678for (size_t i = 0 ;i < size ;++ i ) { 679const int32_t src = cells [i ].src ; 680if (src >=0 ) { 681refcounts [src ]+= 1 ; 682 } 683 } 684 685rs_z = -1 ; 686for (int i = min ;i <=max ;++ i ) { 687if (refcounts [i ]== 0 ) { 688rs_z = i ; 689break ; 690 } 691 } 692 693for (int i = min ;i <=max ;++ i ) { 694if (cells [i ].src < 0 ) { 695GGML_ASSERT (rs_z >=0 ); 696cells [i ].src0 = rs_z ; 697 }else { 698// Stage the source ids for all used cells to allow correct seq_* behavior 699// and still make these values available when setting the inputs 700cells [i ].src0 = cells [i ].src ; 701 } 702cells [i ].src = i ;// avoid moving or clearing twice 703 } 704 } 705 706// allow getting the range of used cells, from head to head + n 707head = min ; 708n = max - min + 1 ; 709used = std::count_if (cells .begin (),cells .end (), 710 [](const mem_cell & cell ){return !cell .is_empty (); }); 711 712// sanity check 713return n >=n_seqs ; 714} 715 716bool llama_memory_recurrent::get_can_shift ()const { 717// shifting the pos is trivial for recurrent models 718return true; 719} 720 721size_t llama_memory_recurrent::total_size ()const { 722size_t size = 0 ; 723for (const auto & [_ ,buf ] :ctxs_bufs ) { 724size += ggml_backend_buffer_get_size (buf .get ()); 725 } 726 727return size ; 728} 729 730size_t llama_memory_recurrent::size_r_bytes ()const { 731size_t size_r_bytes = 0 ; 732 733for (const auto & r :r_l ) { 734if (r != nullptr ) { 735size_r_bytes += ggml_nbytes (r ); 736 } 737 } 738 739return size_r_bytes ; 740} 741 742size_t llama_memory_recurrent::size_s_bytes ()const { 743size_t size_s_bytes = 0 ; 744 745for (const auto & s :s_l ) { 746if (s != nullptr ) { 747size_s_bytes += ggml_nbytes (s ); 748 } 749 } 750 751return size_s_bytes ; 752} 753 754size_t llama_memory_recurrent::size_p_bytes ()const { 755size_t size_p_bytes = 0 ; 756 757for (const auto & p :p_l ) { 758if (p != nullptr ) { 759size_p_bytes += ggml_nbytes (p ); 760 } 761 } 762 763return size_p_bytes ; 764} 765 766void llama_memory_recurrent::state_write (llama_io_write_i & io ,llama_seq_id seq_id ,llama_state_seq_flags flags )const { 767GGML_UNUSED (flags ); 768 769 std::vector < std::pair < uint32_t ,uint32_t >> cell_ranges ;// ranges, from inclusive, to exclusive 770 std::vector < std::pair < uint32_t ,uint32_t >> cell_ranges_data ;// logical source row ranges 771uint32_t cell_count = 0 ; 772 773// Count the number of cells with the specified seq_id 774// Find all the ranges of cells with this seq id (or all, when -1) 775uint32_t cell_range_begin = size ; 776for (uint32_t i = 0 ;i < size ;++ i ) { 777const auto & cell = cells [i ]; 778// TODO: fix incosistent handling of `seq_id < 0` and `seq_id == -1` in the codebase [TAG_LLAMA_SEQ_ID_NEG] 779if ((seq_id == -1 && !cell .is_empty ())|| cell .has_seq_id (seq_id )) { 780++ cell_count ; 781uint32_t rs_idx_cur = 0 ; 782 783if (n_rs_seq != 0 ) { 784if (seq_id != -1 ) { 785GGML_ASSERT (seq_id >=0 && (size_t )seq_id < rs_idx .size ()); 786rs_idx_cur = rs_idx [seq_id ]; 787 }else { 788bool has_rs_idx = false; 789for (const llama_seq_id cell_seq_id :cell .seq_id ) { 790GGML_ASSERT (cell_seq_id >=0 && (size_t )cell_seq_id < rs_idx .size ()); 791 792const uint32_t seq_rs_idx = rs_idx [cell_seq_id ]; 793if (!has_rs_idx ) { 794rs_idx_cur = seq_rs_idx ; 795has_rs_idx = true; 796 }else if (rs_idx_cur != seq_rs_idx ) { 797GGML_ABORT ("cannot write shared recurrent state with different rollback indices" ); 798 } 799 } 800 } 801 } 802 803const uint32_t cell_id = rs_idx_cur * size + (cell .src >=0 ?cell .src : (int32_t )i ); 804if (cell_ranges_data .empty ()|| cell_ranges_data .back ().second != cell_id ) { 805cell_ranges_data .emplace_back (cell_id ,cell_id + 1 ); 806 }else { 807cell_ranges_data .back ().second ++ ; 808 } 809 810if (cell_range_begin == size ) { 811cell_range_begin = i ; 812 } 813 }else { 814if (cell_range_begin != size ) { 815cell_ranges .emplace_back (cell_range_begin ,i ); 816cell_range_begin = size ; 817 } 818 } 819 } 820if (cell_range_begin != size ) { 821cell_ranges .emplace_back (cell_range_begin ,size ); 822 } 823 824if ((flags & LLAMA_STATE_SEQ_FLAGS_ON_DEVICE )&& cell_ranges .size ()> 1 ) { 825GGML_ABORT ("cannot save/load multiple ranges of cells to/from device memory\n" ); 826 } 827 828// DEBUG CHECK: Sum of cell counts in ranges should equal the total cell count 829uint32_t cell_count_check = 0 ; 830for (const auto & range :cell_ranges ) { 831cell_count_check += range .second - range .first ; 832 } 833GGML_ASSERT (cell_count == cell_count_check ); 834 835cell_count_check = 0 ; 836for (const auto & range :cell_ranges_data ) { 837cell_count_check += range .second - range .first ; 838 } 839GGML_ASSERT (cell_count == cell_count_check ); 840 841io .write (& cell_count ,sizeof (cell_count )); 842 843state_write_meta (io ,cell_ranges ,seq_id ); 844state_write_data (io ,cell_ranges_data ); 845} 846 847void llama_memory_recurrent::state_read (llama_io_read_i & io ,llama_seq_id seq_id ,llama_state_seq_flags flags ) { 848GGML_UNUSED (flags ); 849 850uint32_t cell_count ; 851io .read (& cell_count ,sizeof (cell_count )); 852 853bool res = true; 854 855res = res && state_read_meta (io ,cell_count ,seq_id ); 856 857try { 858res = res && state_read_data (io ,cell_count ); 859 }catch (...) { 860res = false; 861 } 862 863if (!res ) { 864// TODO: fix incosistent handling of `seq_id < 0` and `seq_id == -1` in the codebase [TAG_LLAMA_SEQ_ID_NEG] 865if (seq_id == -1 ) { 866clear (true); 867 }else { 868seq_rm (seq_id ,-1 ,-1 ); 869 } 870throw std::runtime_error ("failed to restore kv cache" ); 871 } 872 873if (n_rs_seq != 0 ) { 874set_rs_idx (seq_id ,0 ); 875 } 876} 877 878void llama_memory_recurrent::state_write_meta (llama_io_write_i & io ,const std::vector < std::pair < uint32_t ,uint32_t >> & cell_ranges ,llama_seq_id seq_id )const { 879for (const auto & range :cell_ranges ) { 880for (uint32_t i = range .first ;i < range .second ;++ i ) { 881const auto & cell = cells [i ]; 882const llama_pos pos = cell .pos ; 883const uint32_t n_seq_id = seq_id == -1 ?cell .seq_id .size () :0 ; 884 885io .write (& pos ,sizeof (pos )); 886io .write (& n_seq_id ,sizeof (n_seq_id )); 887 888if (n_seq_id ) { 889for (auto seq_id :cell .seq_id ) { 890io .write (& seq_id ,sizeof (seq_id )); 891 } 892 } 893 } 894 } 895} 896 897void llama_memory_recurrent::state_write_data (llama_io_write_i & io ,const std::vector < std::pair < uint32_t ,uint32_t >> & cell_ranges )const { 898const uint32_t s_trans = 0 ; 899const uint32_t n_layer = hparams .n_layer (); 900 901io .write (& s_trans ,sizeof (s_trans )); 902io .write (& n_layer ,sizeof (n_layer )); 903 904// Iterate and write all the R tensors first, each row is a cell 905// Get whole range at a time 906for (uint32_t il = 0 ;il < n_layer ;++ il ) { 907// skip null layers (read_data will handle this by checking "r_l" and "s_l" for null) 908if (r_l [il ]== nullptr )continue ; 909 910// Write R tensor type 911const int32_t r_type_i = (int32_t )r_l [il ]-> type ; 912io .write (& r_type_i ,sizeof (r_type_i )); 913 914// Write row size of R tensor 915const uint64_t r_size_row = ggml_row_size (r_l [il ]-> type ,hparams .n_embd_r ()); 916io .write (& r_size_row ,sizeof (r_size_row )); 917 918// Write each logical cell row range. With pending recurrent rollback, 919// the logical current state may live in a rollback snapshot plane. 920for (const auto & range :cell_ranges ) { 921const size_t range_size = range .second - range .first ; 922const size_t buf_size = range_size * r_size_row ; 923io .write_tensor (r_l [il ],range .first * r_size_row ,buf_size ); 924 } 925 926// the PLE conv history is a second recurrent row, so it has to travel with the first 927if (p_l [il ]!= nullptr ) { 928const uint64_t p_size_row = ggml_row_size (p_l [il ]-> type ,hparams .ple_conv_state ()); 929io .write (& p_size_row ,sizeof (p_size_row )); 930 931for (const auto & range :cell_ranges ) { 932const size_t range_size = range .second - range .first ; 933io .write_tensor (p_l [il ],range .first * p_size_row ,range_size * p_size_row ); 934 } 935 } 936 } 937 938if (!s_trans ) { 939for (uint32_t il = 0 ;il < n_layer ;++ il ) { 940// skip null layers (read_data will handle this by checking "r_l" and "s_l" for null) 941if (s_l [il ]== nullptr )continue ; 942 943// Write S tensor type 944const int32_t s_type_i = (int32_t )s_l [il ]-> type ; 945io .write (& s_type_i ,sizeof (s_type_i )); 946 947// Write row size of S tensor 948const uint64_t s_size_row = ggml_row_size (s_l [il ]-> type ,hparams .n_embd_s ()); 949io .write (& s_size_row ,sizeof (s_size_row )); 950 951// Write each logical cell row range. With pending recurrent rollback, 952// the logical current state may live in a rollback snapshot plane. 953for (const auto & range :cell_ranges ) { 954const size_t range_size = range .second - range .first ; 955const size_t buf_size = range_size * s_size_row ; 956io .write_tensor (s_l [il ],range .first * s_size_row ,buf_size ); 957 } 958 } 959 }else { 960// When S tensor is transposed, we also need the element size and get the element ranges from each row 961const uint32_t mem_size = size ; 962for (uint32_t il = 0 ;il < n_layer ;++ il ) { 963// skip null layers (read_data will handle this by checking "r_l" and "s_l" for null) 964if (s_l [il ]== nullptr )continue ; 965 966const uint32_t n_embd_s = hparams .n_embd_s (); 967 968// Write S tensor type 969const int32_t s_type_i = (int32_t )s_l [il ]-> type ; 970io .write (& s_type_i ,sizeof (s_type_i )); 971 972// Write element size 973const uint32_t s_size_el = ggml_type_size (s_l [il ]-> type ); 974io .write (& s_size_el ,sizeof (s_size_el )); 975 976// Write GQA embedding size 977io .write (& n_embd_s ,sizeof (n_embd_s )); 978 979// For each row, we get the element values of each logical cell 980for (uint32_t j = 0 ;j < n_embd_s ;++ j ) { 981for (const auto & range :cell_ranges ) { 982const size_t range_size = range .second - range .first ; 983const size_t src_offset = (range .first + j * mem_size )* s_size_el ; 984const size_t buf_size = range_size * s_size_el ; 985io .write_tensor (s_l [il ],src_offset ,buf_size ); 986 } 987 } 988 } 989 } 990} 991 992bool llama_memory_recurrent::state_read_meta (llama_io_read_i & io ,uint32_t cell_count ,llama_seq_id dest_seq_id ) { 993if (dest_seq_id != -1 ) { 994// single sequence 995seq_rm (dest_seq_id ,-1 ,-1 ); 996 997if (cell_count == 0 ) { 998return true; 999 } 1000 1001llama_batch_allocr balloc (hparams .n_pos_per_embd ()); 1002 1003llama_ubatch ubatch = balloc .ubatch_reserve (cell_count ,1 ); 1004 1005for (uint32_t i = 0 ;i < cell_count ;++ i ) { 1006llama_pos pos ; 1007uint32_t n_seq_id ; 1008 1009io .read (& pos ,sizeof (pos )); 1010io .read (& n_seq_id ,sizeof (n_seq_id )); 1011 1012if (n_seq_id != 0 ) { 1013LLAMA_LOG_ERROR ("%s: invalid seq_id-agnostic kv cell\n" ,__func__ ); 1014return false; 1015 } 1016 1017ubatch .pos [i ]= pos ; 1018 } 1019ubatch .n_seq_id [0 ]= 1 ; 1020ubatch .seq_id [0 ]= & dest_seq_id ; 1021 1022if (!find_slot (ubatch )) { 1023LLAMA_LOG_ERROR ("%s: failed to find available cells in kv cache\n" ,__func__ ); 1024return false; 1025 } 1026 1027// DEBUG CHECK: kv.head should be our first cell, kv.head + cell_count - 1 should be our last cell (verify seq_id and pos values) 1028// Assume that this is one contiguous block of cells 1029GGML_ASSERT (head + cell_count <=size ); 1030GGML_ASSERT (cells [head ].pos == ubatch .pos [0 ]); 1031GGML_ASSERT (cells [head + cell_count - 1 ].pos == ubatch .pos [cell_count - 1 ]); 1032GGML_ASSERT (cells [head ].has_seq_id (dest_seq_id )); 1033GGML_ASSERT (cells [head + cell_count - 1 ].has_seq_id (dest_seq_id )); 1034 }else { 1035// whole KV cache restore 1036 1037if (cell_count > size ) { 1038LLAMA_LOG_ERROR ("%s: not enough cells in kv cache\n" ,__func__ ); 1039return false; 1040 } 1041 1042clear (true); 1043 1044for (uint32_t i = 0 ;i < cell_count ;++ i ) { 1045auto & cell = cells [i ]; 1046 1047llama_pos pos ; 1048uint32_t n_seq_id ; 1049 1050io .read (& pos ,sizeof (pos )); 1051io .read (& n_seq_id ,sizeof (n_seq_id )); 1052 1053cell .pos = pos ; 1054 1055for (uint32_t j = 0 ;j < n_seq_id ;++ j ) { 1056llama_seq_id seq_id ; 1057io .read (& seq_id ,sizeof (seq_id )); 1058 1059if (seq_id < 0 || (uint32_t )seq_id >=this -> n_seq_max ) { 1060LLAMA_LOG_ERROR ("%s: invalid seq_id, %d is out of range [0, %u)\n" ,__func__ ,seq_id ,this -> n_seq_max ); 1061return false; 1062 } 1063 1064cell .seq_id .insert (seq_id ); 1065 1066int32_t & tail = cells [seq_id ].tail ; 1067if (tail != -1 ) { 1068LLAMA_LOG_ERROR ("%s: duplicate tail for seq_id %d in cell %d and %d\n" ,__func__ ,seq_id ,i ,tail ); 1069return false; 1070 } 1071tail = i ; 1072 } 1073 } 1074 1075head = 0 ; 1076used = cell_count ; 1077 } 1078 1079for (uint32_t i = 0 ;i < cell_count ;++ i ) { 1080uint32_t cell_id = head + i ; 1081// make sure the recurrent states will keep their restored state 1082cells [cell_id ].src = cell_id ; 1083 } 1084 1085return true; 1086} 1087 1088bool llama_memory_recurrent::state_read_data (llama_io_read_i & io ,uint32_t cell_count ) { 1089uint32_t s_trans ; 1090uint32_t n_layer ; 1091io .read (& s_trans ,sizeof (s_trans )); 1092io .read (& n_layer ,sizeof (n_layer )); 1093 1094if (n_layer != hparams .n_layer ()) { 1095LLAMA_LOG_ERROR ("%s: mismatched layer count (%u instead of %u)\n" ,__func__ ,n_layer ,hparams .n_layer ()); 1096return false; 1097 } 1098if (cell_count > size ) { 1099LLAMA_LOG_ERROR ("%s: not enough cells in kv cache to restore state (%u > %u)\n" ,__func__ ,cell_count ,size ); 1100return false; 1101 } 1102if (false!= (bool )s_trans ) { 1103LLAMA_LOG_ERROR ("%s: incompatible s transposition\n" ,__func__ ); 1104return false; 1105 } 1106 1107// For each layer, read the keys for each cell, one row is one cell, read as one contiguous block 1108for (uint32_t il = 0 ;il < n_layer ;++ il ) { 1109// skip null layers 1110if (r_l [il ]== nullptr )continue ; 1111 1112// Read type of key 1113int32_t r_type_i_ref ; 1114io .read (& r_type_i_ref ,sizeof (r_type_i_ref )); 1115const int32_t r_type_i = (int32_t )r_l [il ]-> type ; 1116if (r_type_i != r_type_i_ref ) { 1117LLAMA_LOG_ERROR ("%s: mismatched r type (%d != %d, layer %d)\n" ,__func__ ,r_type_i ,r_type_i_ref ,il ); 1118return false; 1119 } 1120 1121// Read row size of key 1122uint64_t r_size_row_ref ; 1123io .read (& r_size_row_ref ,sizeof (r_size_row_ref )); 1124const size_t r_size_row = ggml_row_size (r_l [il ]-> type ,hparams .n_embd_r ()); 1125if (r_size_row != r_size_row_ref ) { 1126LLAMA_LOG_ERROR ("%s: mismatched r row size (%zu != %zu, layer %d)\n" ,__func__ ,r_size_row , (size_t )r_size_row_ref ,il ); 1127return false; 1128 } 1129 1130if (cell_count ) { 1131// Read and set the keys for the whole cell range 1132io .read_tensor (r_l [il ],head * r_size_row ,cell_count * r_size_row ); 1133 } 1134 1135if (p_l [il ]!= nullptr ) { 1136uint64_t p_size_row_ref ; 1137io .read (& p_size_row_ref ,sizeof (p_size_row_ref )); 1138const size_t p_size_row = ggml_row_size (p_l [il ]-> type ,hparams .ple_conv_state ()); 1139if (p_size_row != p_size_row_ref ) { 1140LLAMA_LOG_ERROR ("%s: mismatched ple row size (%zu != %zu, layer %d)\n" ,__func__ ,p_size_row , (size_t )p_size_row_ref ,il ); 1141return false; 1142 } 1143 1144if (cell_count ) { 1145io .read_tensor (p_l [il ],head * p_size_row ,cell_count * p_size_row ); 1146 } 1147 } 1148 } 1149 1150if (!s_trans ) { 1151for (uint32_t il = 0 ;il < n_layer ;++ il ) { 1152// skip null layers 1153if (s_l [il ]== nullptr )continue ; 1154 1155// Read type of value 1156int32_t s_type_i_ref ; 1157io .read (& s_type_i_ref ,sizeof (s_type_i_ref )); 1158const int32_t s_type_i = (int32_t )s_l [il ]-> type ; 1159 1160if (s_type_i != s_type_i_ref ) { 1161LLAMA_LOG_ERROR ("%s: mismatched s type (%d != %d, layer %d)\n" ,__func__ ,s_type_i ,s_type_i_ref ,il ); 1162return false; 1163 } 1164 1165// Read row size of value 1166uint64_t s_size_row_ref ; 1167io .read (& s_size_row_ref ,sizeof (s_size_row_ref )); 1168const size_t s_size_row = ggml_row_size (s_l [il ]-> type ,hparams .n_embd_s ()); 1169if (s_size_row != s_size_row_ref ) { 1170LLAMA_LOG_ERROR ("%s: mismatched s row size (%zu != %zu, layer %d)\n" ,__func__ ,s_size_row , (size_t )s_size_row_ref ,il ); 1171return false; 1172 } 1173 1174if (cell_count ) { 1175// Read and set the values for the whole cell range 1176io .read_tensor (s_l [il ],head * s_size_row ,cell_count * s_size_row ); 1177 } 1178 } 1179 }else { 1180// For each layer, read the values for each cell (transposed) 1181for (uint32_t il = 0 ;il < n_layer ;++ il ) { 1182// skip null layers 1183if (s_l [il ]== nullptr )continue ; 1184 1185const uint32_t n_embd_s = hparams .n_embd_s (); 1186 1187// Read type of value 1188int32_t s_type_i_ref ; 1189io .read (& s_type_i_ref ,sizeof (s_type_i_ref )); 1190const int32_t s_type_i = (int32_t )s_l [il ]-> type ; 1191if (s_type_i != s_type_i_ref ) { 1192LLAMA_LOG_ERROR ("%s: mismatched s type (%d != %d, layer %d)\n" ,__func__ ,s_type_i ,s_type_i_ref ,il ); 1193return false; 1194 } 1195 1196// Read element size of value 1197uint32_t s_size_el_ref ; 1198io .read (& s_size_el_ref ,sizeof (s_size_el_ref )); 1199const size_t s_size_el = ggml_type_size (s_l [il ]-> type ); 1200if (s_size_el != s_size_el_ref ) { 1201LLAMA_LOG_ERROR ("%s: mismatched s element size (%zu != %zu, layer %d)\n" ,__func__ ,s_size_el , (size_t )s_size_el_ref ,il ); 1202return false; 1203 } 1204 1205// Read state embedding size 1206uint32_t n_embd_s_ref ; 1207io .read (& n_embd_s_ref ,sizeof (n_embd_s_ref )); 1208if (n_embd_s != n_embd_s_ref ) { 1209LLAMA_LOG_ERROR ("%s: mismatched s embedding size (%u != %u, layer %d)\n" ,__func__ ,n_embd_s ,n_embd_s_ref ,il ); 1210return false; 1211 } 1212 1213if (cell_count ) { 1214// For each row in the transposed matrix, read the values for the whole cell range 1215for (uint32_t j = 0 ;j < n_embd_s ;++ j ) { 1216const size_t dst_offset = (head + j * size )* s_size_el ; 1217io .read_tensor (s_l [il ],dst_offset ,cell_count * s_size_el ); 1218 } 1219 } 1220 } 1221 } 1222 1223return true; 1224} 1225 1226// 1227// llama_memory_recurrent_context 1228// 1229 1230llama_memory_recurrent_context::llama_memory_recurrent_context (llama_memory_status status ) :status (status ) {} 1231 1232llama_memory_recurrent_context::llama_memory_recurrent_context ( 1233llama_memory_recurrent * mem ) :status (LLAMA_MEMORY_STATUS_SUCCESS ),mem (mem ),is_full (true) { 1234} 1235 1236llama_memory_recurrent_context::llama_memory_recurrent_context ( 1237llama_memory_recurrent * mem , 1238 std::vector < llama_ubatch > ubatches ) :status (LLAMA_MEMORY_STATUS_SUCCESS ),mem (mem ),ubatches (std::move (ubatches )) {} 1239 1240llama_memory_recurrent_context::~llama_memory_recurrent_context ()= default ; 1241 1242bool llama_memory_recurrent_context::next () { 1243assert (status == LLAMA_MEMORY_STATUS_SUCCESS ); 1244 1245if (++ i_next >=ubatches .size ()) { 1246return false; 1247 } 1248 1249return true; 1250} 1251 1252bool llama_memory_recurrent_context::apply () { 1253assert (!llama_memory_status_is_fail (status )); 1254 1255// no ubatches -> this is an update 1256if (ubatches .empty ()) { 1257// recurrent cache never performs updates 1258assert (status == LLAMA_MEMORY_STATUS_NO_UPDATE ); 1259 1260return true; 1261 } 1262 1263mem -> find_slot (ubatches [i_next ]); 1264 1265return true; 1266} 1267 1268llama_memory_status llama_memory_recurrent_context::get_status ()const { 1269return status ; 1270} 1271 1272const llama_ubatch & llama_memory_recurrent_context::get_ubatch ()const { 1273assert (status == LLAMA_MEMORY_STATUS_SUCCESS ); 1274 1275return ubatches [i_next ]; 1276} 1277 1278uint32_t llama_memory_recurrent_context::get_n_rs ()const { 1279return is_full ?mem -> size :mem -> n ; 1280} 1281 1282uint32_t llama_memory_recurrent_context::get_head ()const { 1283return is_full ?0 :mem -> head ; 1284} 1285 1286int32_t llama_memory_recurrent_context::get_rs_z ()const { 1287return is_full ?0 :mem -> rs_z ; 1288} 1289 1290uint32_t llama_memory_recurrent_context::get_size ()const { 1291return mem -> size ; 1292} 1293 1294ggml_tensor * llama_memory_recurrent_context::get_r_l (int32_t il )const { 1295return mem -> r_l [il ]; 1296} 1297 1298ggml_tensor * llama_memory_recurrent_context::get_s_l (int32_t il )const { 1299return mem -> s_l [il ]; 1300} 1301 1302ggml_tensor * llama_memory_recurrent_context::get_p_l (int32_t il )const { 1303return mem -> p_l [il ]; 1304} 1305 1306int32_t llama_memory_recurrent_context::s_copy (int i )const { 1307const uint32_t cell_idx = i + mem -> head ; 1308const int32_t src0 = mem -> cells [cell_idx ].src0 ; 1309 1310if (mem -> n_rs_seq == 0 ) { 1311return src0 ; 1312 } 1313 1314uint32_t idx = 0 ; 1315if (!mem -> cells [cell_idx ].seq_id .empty ()) { 1316const llama_seq_id seq = * mem -> cells [cell_idx ].seq_id .begin (); 1317if (seq >=0 && (size_t )seq < mem -> rs_idx .size ()) { 1318idx = mem -> rs_idx [seq ]; 1319// reset rollback idx 1320mem -> rs_idx [seq ]= 0 ; 1321 } 1322 } 1323return (int32_t )(idx * mem -> size )+ src0 ; 1324}