Brunobkr/llama.cpp_AlgMor24_github
ΩFFFΣLLIa • llama.cpp • AlgMor24 ██████╗ ███████╗███████╗███████╗██╗ ██╗ ██╗ █████╗ ██╔═══██╗██╔════╝██╔════╝██╔════╝██║ ██║ ██║██╔══██╗ ██║ ██║█████╗ █████╗ █████╗ ██║ ██║ ██║███████║ ██║ ██║██╔══╝ ██╔══╝ ██╔══╝ ██║ ██║ ██║██╔══██║ ╚██████╔╝██║ ██║ ███████╗███████╗███████╗██║██║ ██║ ╚═════╝ ╚═╝ ╚═╝ ╚══════╝╚══════╝╚══════╝╚═╝╚═╝ ╚═╝ High-Performance LLM / VLM Inference & Autonomous Agentic Ecosystem… See the full description on the dataset page: https://huggingface.co/datasets/Brunobkr/llama.cpp_AlgMor24_github.
03k
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_recurrent18//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 type50 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 /*.mem_size =*/ size_t(2u*n_layer*ggml_tensor_overhead()),55 /*.mem_buffer =*/ NULL,56 /*.no_alloc =*/ true,57 };58 59 ggml_context * ctx = ggml_init(params);60 if (!ctx) {61 return nullptr;62 }63 64 ctx_map.emplace(buft, ctx);65 66 return ctx;67 }68 69 return it->second.get();70 };71 72 r_l.resize(n_layer);73 s_l.resize(n_layer);74 75 for (int i = 0; i < n_layer; i++) {76 if (filter && !filter(i)) {77 LLAMA_LOG_DEBUG("%s: layer %3d: skipped\n", __func__, i);78 continue;79 }80 81 const char * dev_name = "CPU";82 83 ggml_backend_buffer_type_t buft = ggml_backend_cpu_buffer_type();84 85 if (offload) {86 auto * dev = model.dev_layer(i);87 buft = ggml_backend_dev_buffer_type(dev);88 89 dev_name = ggml_backend_dev_name(dev);90 }91 92 LLAMA_LOG_DEBUG("%s, layer %3d: dev = %s\n", __func__, i, dev_name);93 94 ggml_context * ctx = ctx_for_buft(buft);95 if (!ctx) {96 throw std::runtime_error("failed to create ggml context for rs cache");97 }98 99 const uint32_t n_rows = mem_size * (1 + n_rs_seq);100 ggml_tensor * r = ggml_new_tensor_2d(ctx, type_r, hparams.n_embd_r(), n_rows);101 ggml_tensor * s = ggml_new_tensor_2d(ctx, type_s, hparams.n_embd_s(), n_rows);102 ggml_format_name(r, "cache_r_l%d", i);103 ggml_format_name(s, "cache_s_l%d", i);104 r_l[i] = r;105 s_l[i] = s;106 }107 108 // allocate tensors and initialize the buffers to avoid NaNs in the padding109 for (auto & [buft, ctx] : ctx_map) {110 ggml_backend_buffer_t buf = ggml_backend_alloc_ctx_tensors_from_buft(ctx.get(), buft);111 if (!buf) {112 throw std::runtime_error("failed to allocate buffer for rs cache");113 }114 ggml_backend_buffer_clear(buf, 0);115 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);116 ctxs_bufs.emplace_back(std::move(ctx), buf);117 }118 119 {120 const size_t memory_size_r = size_r_bytes();121 const size_t memory_size_s = size_s_bytes();122 123 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\n", __func__,124 (float)(memory_size_r + memory_size_s) / (1024.0f * 1024.0f), mem_size, n_layer, n_seq_max, n_rs_seq,125 ggml_type_name(type_r), (float)memory_size_r / (1024.0f * 1024.0f),126 ggml_type_name(type_s), (float)memory_size_s / (1024.0f * 1024.0f));127 }128}129 130void llama_memory_recurrent::clear(bool data) {131 for (int32_t i = 0; i < (int32_t) size; ++i) {132 cells[i].pos = -1;133 cells[i].seq_id.clear();134 cells[i].src = -1;135 cells[i].tail = -1;136 }137 138 head = 0;139 used = 0;140 141 if (data) {142 for (auto & [_, buf] : ctxs_bufs) {143 ggml_backend_buffer_clear(buf.get(), 0);144 }145 }146 147 std::fill(rs_idx.begin(), rs_idx.end(), 0);148}149 150bool llama_memory_recurrent::seq_rm(llama_seq_id seq_id, llama_pos p0, llama_pos p1) {151 uint32_t new_head = size;152 153 if (p0 < 0) {154 p0 = 0;155 }156 157 if (p1 < 0) {158 p1 = std::numeric_limits<llama_pos>::max();159 }160 161 const bool rm_all = p0 == 0 && p1 == std::numeric_limits<llama_pos>::max();162 if (rm_all) {163 if (seq_id >= 0) {164 set_rs_idx(seq_id, 0);165 } else {166 std::fill(rs_idx.begin(), rs_idx.end(), 0);167 }168 }169 170 // models like Mamba or RWKV can't have a state partially erased at the end171 // of the sequence because their state isn't preserved for previous tokens172 if (seq_id >= (int64_t) size) {173 // could be fatal174 return false;175 }176 if (0 <= seq_id) {177 int32_t & tail_id = cells[seq_id].tail;178 if (tail_id >= 0) {179 auto & cell = cells[tail_id];180 181 // partial rollback via per-token snapshot index (bounded by n_rs_seq)182 if (0 < p0 && p0 <= cell.pos && p1 > cell.pos) {183 const llama_pos rollback = cell.pos - (p0 - 1);184 if (rollback >= 1 && rollback <= (llama_pos) n_rs_seq) {185 set_rs_idx(seq_id, (uint32_t) rollback);186 cell.pos = p0 - 1;187 return true;188 }189 return false;190 }191 // invalidate tails which will be cleared192 if (p0 <= cell.pos && cell.pos < p1) {193 tail_id = -1;194 }195 }196 } else {197 // seq_id is negative, then the range should include everything or nothing198 if (p0 != p1 && (p0 != 0 || p1 != std::numeric_limits<llama_pos>::max())) {199 //printf("[DEBUG] inside `llama_memory_recurrent::seq_rm`: `seq_id` is negative, so returning false\n");200 return false;201 }202 }203 204 for (uint32_t i = 0; i < size; ++i) {205 if (cells[i].pos >= p0 && cells[i].pos < p1) {206 if (seq_id < 0) {207 cells[i].seq_id.clear();208 } else if (cells[i].has_seq_id(seq_id)) {209 cells[i].seq_id.erase(seq_id);210 } else {211 continue;212 }213 if (cells[i].is_empty()) {214 // keep count of the number of used cells215 if (cells[i].pos >= 0) {216 used--;217 }218 cells[i].pos = -1;219 cells[i].src = -1;220 if (new_head == size) {221 new_head = i;222 }223 }224 }225 }226 227 // If we freed up a slot, set head to it so searching can start there.228 if (new_head != size && new_head < head) {229 head = new_head;230 }231 232 return true;233}234 235void llama_memory_recurrent::seq_cp(llama_seq_id seq_id_src, llama_seq_id seq_id_dst, llama_pos p0, llama_pos p1) {236 if (seq_id_src == seq_id_dst) {237 return;238 }239 240 if (p0 < 0) {241 p0 = 0;242 }243 244 if (p1 < 0) {245 p1 = std::numeric_limits<llama_pos>::max();246 }247 248 if ((uint32_t) seq_id_dst < size && (uint32_t) seq_id_src < size) {249 auto & tail_src = cells[seq_id_src];250 auto & tail_dst = cells[seq_id_dst];251 if (tail_dst.tail >= 0) {252 // clear destination seq_id if it wasn't empty253 auto & cell_dst = cells[tail_dst.tail];254 255 cell_dst.seq_id.erase(seq_id_dst);256 tail_dst.tail = -1;257 if (cell_dst.seq_id.empty()) {258 cell_dst.pos = -1;259 cell_dst.src = -1;260 used -= 1;261 }262 }263 if (tail_src.tail >= 0) {264 auto & cell_src = cells[tail_src.tail];265 266 cell_src.seq_id.insert(seq_id_dst);267 tail_dst.tail = tail_src.tail;268 }269 }270}271 272void llama_memory_recurrent::seq_keep(llama_seq_id seq_id) {273 uint32_t new_head = size;274 275 for (uint32_t i = 0; i < size; ++i) {276 if ((llama_seq_id) i != seq_id) {277 cells[i].tail = -1;278 }279 280 if (!cells[i].has_seq_id(seq_id)) {281 if (cells[i].pos >= 0) {282 used--;283 }284 285 cells[i].pos = -1;286 cells[i].src = -1;287 cells[i].seq_id.clear();288 289 if (new_head == size){290 new_head = i;291 }292 } else {293 cells[i].seq_id.clear();294 cells[i].seq_id.insert(seq_id);295 }296 }297 298 // If we freed up a slot, set head to it so searching can start there.299 if (new_head != size && new_head < head) {300 head = new_head;301 }302}303 304void llama_memory_recurrent::seq_add(llama_seq_id seq_id, llama_pos p0, llama_pos p1, llama_pos shift) {305 if (shift == 0) {306 return;307 }308 309 if (p0 < 0) {310 p0 = 0;311 }312 313 if (p1 < 0) {314 p1 = std::numeric_limits<llama_pos>::max();315 }316 317 // If there is no range then return early to avoid looping over the318 if (p0 == p1) {319 return;320 }321 322 // for Mamba-like or RWKV models, only the pos needs to be shifted323 if (0 <= seq_id && seq_id < (int64_t) size) {324 const int32_t tail_id = cells[seq_id].tail;325 if (tail_id >= 0) {326 auto & cell = cells[tail_id];327 if (cell.has_seq_id(seq_id) && p0 <= cell.pos && cell.pos < p1) {328 cell.pos += shift;329 }330 }331 }332}333 334void llama_memory_recurrent::seq_div(llama_seq_id seq_id, llama_pos p0, llama_pos p1, int d) {335 if (d == 1) {336 return;337 }338 339 if (p0 < 0) {340 p0 = 0;341 }342 343 if (p1 < 0) {344 p1 = std::numeric_limits<llama_pos>::max();345 }346 347 // If there is no range then return early to avoid looping over the cache.348 if (p0 == p1) {349 return;350 }351 352 // for Mamba-like or RWKV models, only the pos needs to be changed353 if (0 <= seq_id && seq_id < (int64_t) size) {354 const int32_t tail_id = cells[seq_id].tail;355 if (tail_id >= 0) {356 auto & cell = cells[tail_id];357 if (cell.has_seq_id(seq_id) && p0 <= cell.pos && cell.pos < p1) {358 cell.pos /= d;359 }360 }361 }362}363 364llama_pos llama_memory_recurrent::seq_pos_min(llama_seq_id seq_id) const {365 llama_pos result = std::numeric_limits<llama_pos>::max();366 367 for (uint32_t i = 0; i < size; ++i) {368 if (cells[i].has_seq_id(seq_id)) {369 result = std::min(result, cells[i].pos);370 }371 }372 373 if (result == std::numeric_limits<llama_pos>::max()) {374 result = -1;375 }376 377 return result;378}379 380llama_pos llama_memory_recurrent::seq_pos_max(llama_seq_id seq_id) const {381 llama_pos result = -1;382 383 for (uint32_t i = 0; i < size; ++i) {384 if (cells[i].has_seq_id(seq_id)) {385 result = std::max(result, cells[i].pos);386 }387 }388 389 return result;390}391 392void llama_memory_recurrent::set_rs_idx(llama_seq_id seq_id, uint32_t idx) {393 if (seq_id < 0 || (size_t) seq_id >= rs_idx.size()) {394 return;395 }396 rs_idx[seq_id] = (idx > n_rs_seq) ? n_rs_seq : idx;397}398 399std::map<ggml_backend_buffer_type_t, size_t> llama_memory_recurrent::memory_breakdown() const {400 std::map<ggml_backend_buffer_type_t, size_t> ret;401 for (const auto & [_, buf] : ctxs_bufs) {402 ret[ggml_backend_buffer_get_type(buf.get())] += ggml_backend_buffer_get_size(buf.get());403 }404 return ret;405}406 407llama_memory_context_ptr llama_memory_recurrent::init_batch(llama_batch_allocr & balloc, uint32_t n_ubatch, bool embd_all) {408 do {409 balloc.split_reset();410 411 std::vector<llama_ubatch> ubatches;412 while (true) {413 llama_ubatch ubatch;414 415 if (embd_all) {416 // if all tokens are output, split by sequence417 ubatch = balloc.split_seq(n_ubatch);418 } else {419 // TODO: non-sequential equal split can be done if using unified KV cache420 // for simplicity, we always use sequential equal split for now421 // [TAG_RECURRENT_ROLLBACK_SPLITS]422 // the trailing (1 + n_rs_seq) tokens of each seq must stay in the same ubatch423 // so that the rollback snapshots remain valid424 ubatch = balloc.split_equal(n_ubatch, true, n_rs_seq > 0 ? n_rs_seq + 1 : 0);425 }426 427 if (ubatch.n_tokens == 0) {428 break;429 }430 431 ubatches.push_back(std::move(ubatch)); // NOLINT432 }433 434 if (balloc.get_n_used() < balloc.get_n_tokens()) {435 // failed to find a suitable split436 break;437 }438 439 if (!prepare(ubatches)) {440 break;441 }442 443 return std::make_unique<llama_memory_recurrent_context>(this, std::move(ubatches));444 } while (false);445 446 return std::make_unique<llama_memory_recurrent_context>(LLAMA_MEMORY_STATUS_FAILED_PREPARE);447}448 449llama_memory_context_ptr llama_memory_recurrent::init_full() {450 return std::make_unique<llama_memory_recurrent_context>(this);451}452 453llama_memory_context_ptr llama_memory_recurrent::init_update(llama_context * lctx, bool optimize) {454 GGML_UNUSED(lctx);455 GGML_UNUSED(optimize);456 457 return std::make_unique<llama_memory_recurrent_context>(LLAMA_MEMORY_STATUS_NO_UPDATE);458}459 460bool llama_memory_recurrent::prepare(const std::vector<llama_ubatch> & ubatches) {461 // simply remember the full state because it is very small for this type of cache462 // TODO: optimize463 auto org_cells = cells;464 auto org_used = used;465 auto org_head = head;466 467 bool success = true;468 469 for (const auto & ubatch : ubatches) {470 if (!find_slot(ubatch)) {471 success = false;472 break;473 }474 }475 476 // restore the original state477 cells = std::move(org_cells);478 used = org_used;479 head = org_head;480 481 return success;482}483 484bool llama_memory_recurrent::find_slot(const llama_ubatch & ubatch) {485 const uint32_t n_seq_tokens = ubatch.n_seq_tokens;486 const uint32_t n_seqs = ubatch.n_seqs;487 488 // if we have enough unused cells before the current head ->489 // better to start searching from the beginning of the cache, hoping to fill it490 if (head > used + 2*n_seqs) {491 head = 0;492 }493 494 // For recurrent state architectures (like Mamba or RWKV),495 // each cache cell can store the state for a whole sequence.496 // A slot should be always be contiguous.497 498 // can only process batches with an equal number of new tokens in each sequence499 GGML_ASSERT(ubatch.equal_seqs());500 501 int32_t min = size - 1;502 int32_t max = 0;503 504 // everything should fit if all seq_ids are smaller than the max505 for (uint32_t s = 0; s < n_seqs; ++s) {506 const uint32_t i = s*n_seq_tokens; // first token of sequence set s507 const uint32_t n_seq_id = ubatch.n_seq_id[i];508 509 for (uint32_t j = 0; j < n_seq_id; ++j) {510 const llama_seq_id seq_id = ubatch.seq_id[i][j];511 512 if (seq_id < 0 || (uint32_t) seq_id >= size) {513 // too big seq_id514 // TODO: would it be possible to resize the cache instead?515 LLAMA_LOG_ERROR("%s: seq_id=%d >= n_seq_max=%u Try using a bigger --parallel value\n", __func__, seq_id, n_seq_max);516 return false;517 }518 if (j > 0) {519 auto & seq = cells[seq_id];520 if (seq.tail >= 0) {521 auto & cell = cells[seq.tail];522 // clear cells from seq_ids that become shared523 // (should not normally happen, but let's handle it anyway)524 cell.seq_id.erase(seq_id);525 seq.tail = -1;526 if (cell.seq_id.empty()) {527 cell.pos = -1;528 cell.src = -1;529 used -= 1;530 }531 }532 }533 }534 }535 536#ifndef NDEBUG537 {538 std::vector<int32_t> tails_verif;539 tails_verif.assign(size, -1);540 for (uint32_t i = 0; i < size; ++i) {541 auto & cell = cells[i];542 for (llama_seq_id seq_id : cell.seq_id) {543 if (tails_verif[seq_id] != -1) {544 LLAMA_LOG_ERROR("%s: duplicate tail for seq_id %d in cell %d and %d\n", __func__, seq_id, i, tails_verif[seq_id]);545 }546 tails_verif[seq_id] = i;547 }548 }549 for (uint32_t i = 0; i < size; ++i) {550 if (tails_verif[i] != cells[i].tail) {551 LLAMA_LOG_ERROR("%s: wrong tail for seq_id %d, (%d instead of %d)\n", __func__, i, cells[i].tail, tails_verif[i]);552 }553 }554 }555#endif556 557 // find next empty cell558 uint32_t next_empty_cell = head;559 560 for (uint32_t i = 0; i < size; ++i) {561 if (next_empty_cell >= size) { next_empty_cell -= size; }562 auto & cell = cells[next_empty_cell];563 if (cell.is_empty()) { break; }564 next_empty_cell += 1;565 }566 567 // find usable cell range568 for (uint32_t s = 0; s < n_seqs; ++s) {569 const uint32_t i = s*n_seq_tokens;570 const llama_seq_id seq_id = ubatch.seq_id[i][0];571 auto & seq_meta = cells[seq_id];572 bool has_cell = false;573 if (seq_meta.tail >= 0) {574 auto & cell = cells[seq_meta.tail];575 GGML_ASSERT(cell.has_seq_id(seq_id));576 // does this seq_id "own" the cell?577 if (cell.seq_id.size() == 1) { has_cell = true; }578 }579 if (!has_cell) {580 auto & empty_cell = cells[next_empty_cell];581 GGML_ASSERT(empty_cell.is_empty());582 // copy old tail into the empty cell583 if (seq_meta.tail >= 0) {584 auto & orig_cell = cells[seq_meta.tail];585 empty_cell.pos = orig_cell.pos;586 empty_cell.src = orig_cell.src;587 orig_cell.seq_id.erase(seq_id);588 empty_cell.seq_id.insert(seq_id); // will be overwritten589 GGML_ASSERT(!orig_cell.is_empty()); // has at least one remaining seq_id590 }591 seq_meta.tail = next_empty_cell;592 // find next empty cell593 if (s + 1 < n_seqs) {594 for (uint32_t j = 0; j < size; ++j) {595 next_empty_cell += 1;596 if (next_empty_cell >= size) { next_empty_cell -= size; }597 auto & cell = cells[next_empty_cell];598 if (cell.is_empty()) { break; }599 }600 }601 }602 if (min > seq_meta.tail) { min = seq_meta.tail; }603 if (max < seq_meta.tail) { max = seq_meta.tail; }604 }605 606 // gather and re-order607 for (uint32_t s = 0; s < n_seqs; ++s) {608 const uint32_t i = s*n_seq_tokens;609 const int32_t dst_id = s + min;610 const int32_t src_id = cells[ubatch.seq_id[i][0]].tail;611 if (dst_id != src_id) {612 auto & dst_cell = cells[dst_id];613 auto & src_cell = cells[src_id];614 615 std::swap(dst_cell.pos, src_cell.pos);616 std::swap(dst_cell.src, src_cell.src);617 std::swap(dst_cell.seq_id, src_cell.seq_id);618 619 // swap tails620 for (uint32_t j = 0; j < size; ++j) {621 int32_t & tail = cells[j].tail;622 if (tail == src_id) {623 tail = dst_id;624 } else if (tail == dst_id) {625 tail = src_id;626 }627 }628 }629 }630 631 // update the pos of the used seqs632 for (uint32_t s = 0; s < n_seqs; ++s) {633 const uint32_t i = s*n_seq_tokens;634 const llama_pos last_pos = ubatch.pos[i + n_seq_tokens - 1];635 const int32_t cell_id = s + min;636 auto & cell = cells[cell_id];637 638 if (cell.pos >= 0 && last_pos != cell.pos + (llama_pos) n_seq_tokens) {639 // What should happen when the pos backtracks or skips a value?640 // Clearing the state mid-batch would require special-casing which isn't done.641 LLAMA_LOG_WARN("%s: non-consecutive token position %d after %d for sequence %d with %u new tokens\n",642 __func__, last_pos, cell.pos, ubatch.seq_id[i][0], n_seq_tokens);643 }644 cell.pos = last_pos;645 cell.seq_id.clear();646 for (int32_t j = 0; j < ubatch.n_seq_id[i]; ++j) {647 const llama_seq_id seq_id = ubatch.seq_id[i][j];648 cell.seq_id.insert(seq_id);649 cells[seq_id].tail = cell_id;650 }651 }652 653 // Find first cell without src refs, to use as the zero-ed state654 {655 // TODO: bake-in src refcounts in the cell metadata656 std::vector<int32_t> refcounts(size, 0);657 for (size_t i = 0; i < size; ++i) {658 const int32_t src = cells[i].src;659 if (src >= 0) {660 refcounts[src] += 1;661 }662 }663 664 rs_z = -1;665 for (int i = min; i <= max; ++i) {666 if (refcounts[i] == 0) {667 rs_z = i;668 break;669 }670 }671 672 for (int i = min; i <= max; ++i) {673 if (cells[i].src < 0) {674 GGML_ASSERT(rs_z >= 0);675 cells[i].src0 = rs_z;676 } else {677 // Stage the source ids for all used cells to allow correct seq_* behavior678 // and still make these values available when setting the inputs679 cells[i].src0 = cells[i].src;680 }681 cells[i].src = i; // avoid moving or clearing twice682 }683 }684 685 // allow getting the range of used cells, from head to head + n686 head = min;687 n = max - min + 1;688 used = std::count_if(cells.begin(), cells.end(),689 [](const mem_cell & cell){ return !cell.is_empty(); });690 691 // sanity check692 return n >= n_seqs;693}694 695bool llama_memory_recurrent::get_can_shift() const {696 // shifting the pos is trivial for recurrent models697 return true;698}699 700size_t llama_memory_recurrent::total_size() const {701 size_t size = 0;702 for (const auto & [_, buf] : ctxs_bufs) {703 size += ggml_backend_buffer_get_size(buf.get());704 }705 706 return size;707}708 709size_t llama_memory_recurrent::size_r_bytes() const {710 size_t size_r_bytes = 0;711 712 for (const auto & r : r_l) {713 if (r != nullptr) {714 size_r_bytes += ggml_nbytes(r);715 }716 }717 718 return size_r_bytes;719}720 721size_t llama_memory_recurrent::size_s_bytes() const {722 size_t size_s_bytes = 0;723 724 for (const auto & s : s_l) {725 if (s != nullptr) {726 size_s_bytes += ggml_nbytes(s);727 }728 }729 730 return size_s_bytes;731}732 733void llama_memory_recurrent::state_write(llama_io_write_i & io, llama_seq_id seq_id, llama_state_seq_flags flags) const {734 GGML_UNUSED(flags);735 736 std::vector<std::pair<uint32_t, uint32_t>> cell_ranges; // ranges, from inclusive, to exclusive737 std::vector<std::pair<uint32_t, uint32_t>> cell_ranges_data; // logical source row ranges738 uint32_t cell_count = 0;739 740 // Count the number of cells with the specified seq_id741 // Find all the ranges of cells with this seq id (or all, when -1)742 uint32_t cell_range_begin = size;743 for (uint32_t i = 0; i < size; ++i) {744 const auto & cell = cells[i];745 if ((seq_id == -1 && !cell.is_empty()) || cell.has_seq_id(seq_id)) {746 ++cell_count;747 uint32_t rs_idx_cur = 0;748 749 if (n_rs_seq != 0) {750 if (seq_id != -1) {751 GGML_ASSERT(seq_id >= 0 && (size_t) seq_id < rs_idx.size());752 rs_idx_cur = rs_idx[seq_id];753 } else {754 bool has_rs_idx = false;755 for (const llama_seq_id cell_seq_id : cell.seq_id) {756 GGML_ASSERT(cell_seq_id >= 0 && (size_t) cell_seq_id < rs_idx.size());757 758 const uint32_t seq_rs_idx = rs_idx[cell_seq_id];759 if (!has_rs_idx) {760 rs_idx_cur = seq_rs_idx;761 has_rs_idx = true;762 } else if (rs_idx_cur != seq_rs_idx) {763 GGML_ABORT("cannot write shared recurrent state with different rollback indices");764 }765 }766 }767 }768 769 const uint32_t cell_id = rs_idx_cur * size + (cell.src >= 0 ? cell.src : (int32_t) i);770 if (cell_ranges_data.empty() || cell_ranges_data.back().second != cell_id) {771 cell_ranges_data.emplace_back(cell_id, cell_id + 1);772 } else {773 cell_ranges_data.back().second++;774 }775 776 if (cell_range_begin == size) {777 cell_range_begin = i;778 }779 } else {780 if (cell_range_begin != size) {781 cell_ranges.emplace_back(cell_range_begin, i);782 cell_range_begin = size;783 }784 }785 }786 if (cell_range_begin != size) {787 cell_ranges.emplace_back(cell_range_begin, size);788 }789 790 if ((flags & LLAMA_STATE_SEQ_FLAGS_ON_DEVICE) && cell_ranges.size() > 1) {791 GGML_ABORT("cannot save/load multiple ranges of cells to/from device memory\n");792 }793 794 // DEBUG CHECK: Sum of cell counts in ranges should equal the total cell count795 uint32_t cell_count_check = 0;796 for (const auto & range : cell_ranges) {797 cell_count_check += range.second - range.first;798 }799 GGML_ASSERT(cell_count == cell_count_check);800 801 cell_count_check = 0;802 for (const auto & range : cell_ranges_data) {803 cell_count_check += range.second - range.first;804 }805 GGML_ASSERT(cell_count == cell_count_check);806 807 io.write(&cell_count, sizeof(cell_count));808 809 state_write_meta(io, cell_ranges, seq_id);810 state_write_data(io, cell_ranges_data);811}812 813void llama_memory_recurrent::state_read(llama_io_read_i & io, llama_seq_id seq_id, llama_state_seq_flags flags) {814 GGML_UNUSED(flags);815 816 uint32_t cell_count;817 io.read(&cell_count, sizeof(cell_count));818 819 bool res = true;820 821 res = res && state_read_meta(io, cell_count, seq_id);822 823 try {824 res = res && state_read_data(io, cell_count);825 } catch (...) {826 res = false;827 }828 829 if (!res) {830 if (seq_id == -1) {831 clear(true);832 } else {833 seq_rm(seq_id, -1, -1);834 }835 throw std::runtime_error("failed to restore kv cache");836 }837 838 if (n_rs_seq != 0) {839 if (seq_id == -1) {840 std::fill(rs_idx.begin(), rs_idx.end(), 0);841 } else {842 set_rs_idx(seq_id, 0);843 }844 }845}846 847void 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 {848 for (const auto & range : cell_ranges) {849 for (uint32_t i = range.first; i < range.second; ++i) {850 const auto & cell = cells[i];851 const llama_pos pos = cell.pos;852 const uint32_t n_seq_id = seq_id == -1 ? cell.seq_id.size() : 0;853 854 io.write(&pos, sizeof(pos));855 io.write(&n_seq_id, sizeof(n_seq_id));856 857 if (n_seq_id) {858 for (auto seq_id : cell.seq_id) {859 io.write(&seq_id, sizeof(seq_id));860 }861 }862 }863 }864}865 866void llama_memory_recurrent::state_write_data(llama_io_write_i & io, const std::vector<std::pair<uint32_t, uint32_t>> & cell_ranges) const {867 const uint32_t s_trans = 0;868 const uint32_t n_layer = hparams.n_layer();869 870 io.write(&s_trans, sizeof(s_trans));871 io.write(&n_layer, sizeof(n_layer));872 873 // Iterate and write all the R tensors first, each row is a cell874 // Get whole range at a time875 for (uint32_t il = 0; il < n_layer; ++il) {876 // skip null layers (read_data will handle this by checking "r_l" and "s_l" for null)877 if (r_l[il] == nullptr) continue;878 879 // Write R tensor type880 const int32_t r_type_i = (int32_t)r_l[il]->type;881 io.write(&r_type_i, sizeof(r_type_i));882 883 // Write row size of R tensor884 const uint64_t r_size_row = ggml_row_size(r_l[il]->type, hparams.n_embd_r());885 io.write(&r_size_row, sizeof(r_size_row));886 887 // Write each logical cell row range. With pending recurrent rollback,888 // the logical current state may live in a rollback snapshot plane.889 for (const auto & range : cell_ranges) {890 const size_t range_size = range.second - range.first;891 const size_t buf_size = range_size * r_size_row;892 io.write_tensor(r_l[il], range.first * r_size_row, buf_size);893 }894 }895 896 if (!s_trans) {897 for (uint32_t il = 0; il < n_layer; ++il) {898 // skip null layers (read_data will handle this by checking "r_l" and "s_l" for null)899 if (s_l[il] == nullptr) continue;900 901 // Write S tensor type902 const int32_t s_type_i = (int32_t)s_l[il]->type;903 io.write(&s_type_i, sizeof(s_type_i));904 905 // Write row size of S tensor906 const uint64_t s_size_row = ggml_row_size(s_l[il]->type, hparams.n_embd_s());907 io.write(&s_size_row, sizeof(s_size_row));908 909 // Write each logical cell row range. With pending recurrent rollback,910 // the logical current state may live in a rollback snapshot plane.911 for (const auto & range : cell_ranges) {912 const size_t range_size = range.second - range.first;913 const size_t buf_size = range_size * s_size_row;914 io.write_tensor(s_l[il], range.first * s_size_row, buf_size);915 }916 }917 } else {918 // When S tensor is transposed, we also need the element size and get the element ranges from each row919 const uint32_t mem_size = size;920 for (uint32_t il = 0; il < n_layer; ++il) {921 // skip null layers (read_data will handle this by checking "r_l" and "s_l" for null)922 if (s_l[il] == nullptr) continue;923 924 const uint32_t n_embd_s = hparams.n_embd_s();925 926 // Write S tensor type927 const int32_t s_type_i = (int32_t)s_l[il]->type;928 io.write(&s_type_i, sizeof(s_type_i));929 930 // Write element size931 const uint32_t s_size_el = ggml_type_size(s_l[il]->type);932 io.write(&s_size_el, sizeof(s_size_el));933 934 // Write GQA embedding size935 io.write(&n_embd_s, sizeof(n_embd_s));936 937 // For each row, we get the element values of each logical cell938 for (uint32_t j = 0; j < n_embd_s; ++j) {939 for (const auto & range : cell_ranges) {940 const size_t range_size = range.second - range.first;941 const size_t src_offset = (range.first + j * mem_size) * s_size_el;942 const size_t buf_size = range_size * s_size_el;943 io.write_tensor(s_l[il], src_offset, buf_size);944 }945 }946 }947 }948}949 950bool llama_memory_recurrent::state_read_meta(llama_io_read_i & io, uint32_t cell_count, llama_seq_id dest_seq_id) {951 if (dest_seq_id != -1) {952 // single sequence953 seq_rm(dest_seq_id, -1, -1);954 955 if (cell_count == 0) {956 return true;957 }958 959 llama_batch_allocr balloc(hparams.n_pos_per_embd());960 961 llama_ubatch ubatch = balloc.ubatch_reserve(cell_count, 1);962 963 for (uint32_t i = 0; i < cell_count; ++i) {964 llama_pos pos;965 uint32_t n_seq_id;966 967 io.read(&pos, sizeof(pos));968 io.read(&n_seq_id, sizeof(n_seq_id));969 970 if (n_seq_id != 0) {971 LLAMA_LOG_ERROR("%s: invalid seq_id-agnostic kv cell\n", __func__);972 return false;973 }974 975 ubatch.pos[i] = pos;976 }977 ubatch.n_seq_id[0] = 1;978 ubatch.seq_id[0] = &dest_seq_id;979 980 if (!find_slot(ubatch)) {981 LLAMA_LOG_ERROR("%s: failed to find available cells in kv cache\n", __func__);982 return false;983 }984 985 // 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)986 // Assume that this is one contiguous block of cells987 GGML_ASSERT(head + cell_count <= size);988 GGML_ASSERT(cells[head].pos == ubatch.pos[0]);989 GGML_ASSERT(cells[head + cell_count - 1].pos == ubatch.pos[cell_count - 1]);990 GGML_ASSERT(cells[head].has_seq_id(dest_seq_id));991 GGML_ASSERT(cells[head + cell_count - 1].has_seq_id(dest_seq_id));992 } else {993 // whole KV cache restore994 995 if (cell_count > size) {996 LLAMA_LOG_ERROR("%s: not enough cells in kv cache\n", __func__);997 return false;998 }999 1000 clear(true);1001 1002 for (uint32_t i = 0; i < cell_count; ++i) {1003 auto & cell = cells[i];1004 1005 llama_pos pos;1006 uint32_t n_seq_id;1007 1008 io.read(&pos, sizeof(pos));1009 io.read(&n_seq_id, sizeof(n_seq_id));1010 1011 cell.pos = pos;1012 1013 for (uint32_t j = 0; j < n_seq_id; ++j) {1014 llama_seq_id seq_id;1015 io.read(&seq_id, sizeof(seq_id));1016 1017 if (seq_id < 0 || (uint32_t) seq_id >= this->n_seq_max) {1018 LLAMA_LOG_ERROR("%s: invalid seq_id, %d is out of range [0, %u)\n", __func__, seq_id, this->n_seq_max);1019 return false;1020 }1021 1022 cell.seq_id.insert(seq_id);1023 1024 int32_t & tail = cells[seq_id].tail;1025 if (tail != -1) {1026 LLAMA_LOG_ERROR("%s: duplicate tail for seq_id %d in cell %d and %d\n", __func__, seq_id, i, tail);1027 return false;1028 }1029 tail = i;1030 }1031 }1032 1033 head = 0;1034 used = cell_count;1035 }1036 1037 for (uint32_t i = 0; i < cell_count; ++i) {1038 uint32_t cell_id = head + i;1039 // make sure the recurrent states will keep their restored state1040 cells[cell_id].src = cell_id;1041 }1042 1043 return true;1044}1045 1046bool llama_memory_recurrent::state_read_data(llama_io_read_i & io, uint32_t cell_count) {1047 uint32_t s_trans;1048 uint32_t n_layer;1049 io.read(&s_trans, sizeof(s_trans));1050 io.read(&n_layer, sizeof(n_layer));1051 1052 if (n_layer != hparams.n_layer()) {1053 LLAMA_LOG_ERROR("%s: mismatched layer count (%u instead of %u)\n", __func__, n_layer, hparams.n_layer());1054 return false;1055 }1056 if (cell_count > size) {1057 LLAMA_LOG_ERROR("%s: not enough cells in kv cache to restore state (%u > %u)\n", __func__, cell_count, size);1058 return false;1059 }1060 if (false != (bool) s_trans) {1061 LLAMA_LOG_ERROR("%s: incompatible s transposition\n", __func__);1062 return false;1063 }1064 1065 // For each layer, read the keys for each cell, one row is one cell, read as one contiguous block1066 for (uint32_t il = 0; il < n_layer; ++il) {1067 // skip null layers1068 if (r_l[il] == nullptr) continue;1069 1070 // Read type of key1071 int32_t r_type_i_ref;1072 io.read(&r_type_i_ref, sizeof(r_type_i_ref));1073 const int32_t r_type_i = (int32_t) r_l[il]->type;1074 if (r_type_i != r_type_i_ref) {1075 LLAMA_LOG_ERROR("%s: mismatched r type (%d != %d, layer %d)\n", __func__, r_type_i, r_type_i_ref, il);1076 return false;1077 }1078 1079 // Read row size of key1080 uint64_t r_size_row_ref;1081 io.read(&r_size_row_ref, sizeof(r_size_row_ref));1082 const size_t r_size_row = ggml_row_size(r_l[il]->type, hparams.n_embd_r());1083 if (r_size_row != r_size_row_ref) {1084 LLAMA_LOG_ERROR("%s: mismatched r row size (%zu != %zu, layer %d)\n", __func__, r_size_row, (size_t) r_size_row_ref, il);1085 return false;1086 }1087 1088 if (cell_count) {1089 // Read and set the keys for the whole cell range1090 io.read_tensor(r_l[il], head * r_size_row, cell_count * r_size_row);1091 }1092 }1093 1094 if (!s_trans) {1095 for (uint32_t il = 0; il < n_layer; ++il) {1096 // skip null layers1097 if (s_l[il] == nullptr) continue;1098 1099 // Read type of value1100 int32_t s_type_i_ref;1101 io.read(&s_type_i_ref, sizeof(s_type_i_ref));1102 const int32_t s_type_i = (int32_t)s_l[il]->type;1103 1104 if (s_type_i != s_type_i_ref) {1105 LLAMA_LOG_ERROR("%s: mismatched s type (%d != %d, layer %d)\n", __func__, s_type_i, s_type_i_ref, il);1106 return false;1107 }1108 1109 // Read row size of value1110 uint64_t s_size_row_ref;1111 io.read(&s_size_row_ref, sizeof(s_size_row_ref));1112 const size_t s_size_row = ggml_row_size(s_l[il]->type, hparams.n_embd_s());1113 if (s_size_row != s_size_row_ref) {1114 LLAMA_LOG_ERROR("%s: mismatched s row size (%zu != %zu, layer %d)\n", __func__, s_size_row, (size_t) s_size_row_ref, il);1115 return false;1116 }1117 1118 if (cell_count) {1119 // Read and set the values for the whole cell range1120 io.read_tensor(s_l[il], head * s_size_row, cell_count * s_size_row);1121 }1122 }1123 } else {1124 // For each layer, read the values for each cell (transposed)1125 for (uint32_t il = 0; il < n_layer; ++il) {1126 // skip null layers1127 if (s_l[il] == nullptr) continue;1128 1129 const uint32_t n_embd_s = hparams.n_embd_s();1130 1131 // Read type of value1132 int32_t s_type_i_ref;1133 io.read(&s_type_i_ref, sizeof(s_type_i_ref));1134 const int32_t s_type_i = (int32_t)s_l[il]->type;1135 if (s_type_i != s_type_i_ref) {1136 LLAMA_LOG_ERROR("%s: mismatched s type (%d != %d, layer %d)\n", __func__, s_type_i, s_type_i_ref, il);1137 return false;1138 }1139 1140 // Read element size of value1141 uint32_t s_size_el_ref;1142 io.read(&s_size_el_ref, sizeof(s_size_el_ref));1143 const size_t s_size_el = ggml_type_size(s_l[il]->type);1144 if (s_size_el != s_size_el_ref) {1145 LLAMA_LOG_ERROR("%s: mismatched s element size (%zu != %zu, layer %d)\n", __func__, s_size_el, (size_t) s_size_el_ref, il);1146 return false;1147 }1148 1149 // Read state embedding size1150 uint32_t n_embd_s_ref;1151 io.read(&n_embd_s_ref, sizeof(n_embd_s_ref));1152 if (n_embd_s != n_embd_s_ref) {1153 LLAMA_LOG_ERROR("%s: mismatched s embedding size (%u != %u, layer %d)\n", __func__, n_embd_s, n_embd_s_ref, il);1154 return false;1155 }1156 1157 if (cell_count) {1158 // For each row in the transposed matrix, read the values for the whole cell range1159 for (uint32_t j = 0; j < n_embd_s; ++j) {1160 const size_t dst_offset = (head + j * size) * s_size_el;1161 io.read_tensor(s_l[il], dst_offset, cell_count * s_size_el);1162 }1163 }1164 }1165 }1166 1167 return true;1168}1169 1170//1171// llama_memory_recurrent_context1172//1173 1174llama_memory_recurrent_context::llama_memory_recurrent_context(llama_memory_status status) : status(status) {}1175 1176llama_memory_recurrent_context::llama_memory_recurrent_context(1177 llama_memory_recurrent * mem) : status(LLAMA_MEMORY_STATUS_SUCCESS), mem(mem), is_full(true) {1178}1179 1180llama_memory_recurrent_context::llama_memory_recurrent_context(1181 llama_memory_recurrent * mem,1182 std::vector<llama_ubatch> ubatches) : status(LLAMA_MEMORY_STATUS_SUCCESS), mem(mem), ubatches(std::move(ubatches)) {}1183 1184llama_memory_recurrent_context::~llama_memory_recurrent_context() = default;1185 1186bool llama_memory_recurrent_context::next() {1187 assert(status == LLAMA_MEMORY_STATUS_SUCCESS);1188 1189 if (++i_next >= ubatches.size()) {1190 return false;1191 }1192 1193 return true;1194}1195 1196bool llama_memory_recurrent_context::apply() {1197 assert(!llama_memory_status_is_fail(status));1198 1199 // no ubatches -> this is an update1200 if (ubatches.empty()) {