char-fork/llama.cpp

git clone https://git.t4t.associates/char-fork/llama.cpp

Daniel Hanmodel: add Qwen3.8-Flash-Next (qwen4exp) (#27742)6c84c7d5d

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