diff --git a/include/llama.h b/include/llama.h index 11c03a6ae0a7..f053551529e9 100644 --- a/include/llama.h +++ b/include/llama.h @@ -952,6 +952,24 @@ extern "C" { LLAMA_API const char * llama_persona_status_name(int32_t status); + // A checkpoint of a BOUND sequence at its current position, taken without moving her: her recurrent rows (every + // snapshot group) are copied out, her attention KV stays where it is. A rewind writes the rows back and cuts what she + // holds past the checkpoint's position, so the next decode continues from there. Both are two-phase: a refused + // rewind leaves her exactly as she was; a refused take leaves no checkpoint (a later rewind refuses no_checkpoint). + // A rewind never moves forward (checkpoint_ahead). The caller owns WHEN to take one (a stable prompt boundary). + // A checkpoint is stamped with the persona and epoch it was taken for, and with the lineage of the cells it was + // taken from, which travels with her between streams: a rewind for another owner is wrong_owner, and onto cells + // that are not that lineage, or were cut at or below its position since (a slot reused by another mind, a restart, + // an eviction), is checkpoint_stale. Her rows never reach anyone else's KV. + struct llama_persona_checkpoint; + LLAMA_API struct llama_persona_checkpoint * llama_persona_checkpoint_init(void); + LLAMA_API void llama_persona_checkpoint_free(struct llama_persona_checkpoint * checkpoint); + LLAMA_API int32_t llama_persona_checkpoint_take(struct llama_context * ctx, llama_seq_id seq_id, uint64_t persona_id, uint64_t epoch, struct llama_persona_checkpoint * out); + LLAMA_API int32_t llama_persona_checkpoint_rewind(struct llama_context * ctx, llama_seq_id seq_id, uint64_t persona_id, uint64_t epoch, const struct llama_persona_checkpoint * in); + // The position a taken checkpoint holds through (-1 when none is taken), and the bytes its buffers hold allocated. + LLAMA_API llama_pos llama_persona_checkpoint_pos(const struct llama_persona_checkpoint * checkpoint); + LLAMA_API size_t llama_persona_checkpoint_bytes(const struct llama_persona_checkpoint * checkpoint); + // Her blocks move only under her owner (the persona and epoch she was made for): a completed save stamps them; a // load of blocks stamped by another persona or another epoch of her returns wrong_owner before anything is // written. llama_persona_owner reads the stamp (false before a completed save). diff --git a/src/llama-context.cpp b/src/llama-context.cpp index 872894d66a56..05b83065cb36 100644 --- a/src/llama-context.cpp +++ b/src/llama-context.cpp @@ -2562,6 +2562,41 @@ ggml_cgraph * llama_context::graph_reserve( return gf; } +llama_persona_move llama_context::persona_checkpoint(llama_seq_id seq_id, llama_persona_checkpoint_blocks & out, const llama_persona_identity & who) { + // Her buffers are reused take after take (no copy of the previous checkpoint). Any refused take, an unsupported + // memory included, leaves no checkpoint at all, so a later rewind refuses (no_checkpoint) instead of restoring a + // stale one (Codex on #153). + out.taken = false; + if (!memory) { + return llama_persona_move::unsupported; + } + synchronize(); // the raw copies read device memory a queued decode may still be writing + const llama_persona_move checked = memory->persona_check_checkpoint(seq_id, out); + if (checked != llama_persona_move::done) { + return checked; + } + memory->persona_commit_checkpoint(seq_id, out); + out.owner = who; + out.taken = true; + return llama_persona_move::done; +} + +llama_persona_move llama_context::persona_rewind(llama_seq_id seq_id, const llama_persona_checkpoint_blocks & in, const llama_persona_identity & who) { + if (!memory) { + return llama_persona_move::unsupported; + } + if (in.taken && (in.owner.persona != who.persona || in.owner.epoch != who.epoch)) { + return llama_persona_move::wrong_owner; // refused before anything is written + } + synchronize(); + const llama_persona_move checked = memory->persona_check_rewind(seq_id, in); + if (checked != llama_persona_move::done) { + return checked; // refused before anything is written: she stays exactly as she was + } + memory->persona_commit_rewind(seq_id, in); + return llama_persona_move::done; +} + llm_graph_type llama_context::graph_type() const { switch (cparams.ctx_type) { case LLAMA_CONTEXT_TYPE_DEFAULT: return LLM_GRAPH_TYPE_DEFAULT; @@ -4350,6 +4385,42 @@ bool llama_persona_owner(const llama_persona * persona, uint64_t * persona_id, u return true; } +struct llama_persona_checkpoint { + llama_persona_checkpoint_blocks blocks; +}; + +llama_persona_checkpoint * llama_persona_checkpoint_init(void) { + return new (std::nothrow) llama_persona_checkpoint(); +} + +void llama_persona_checkpoint_free(llama_persona_checkpoint * checkpoint) { + delete checkpoint; +} + +int32_t llama_persona_checkpoint_take(llama_context * ctx, llama_seq_id seq_id, uint64_t persona_id, uint64_t epoch, llama_persona_checkpoint * out) { + return (int32_t) ctx->persona_checkpoint(seq_id, out->blocks, llama_persona_identity { persona_id, epoch }); +} + +int32_t llama_persona_checkpoint_rewind(llama_context * ctx, llama_seq_id seq_id, uint64_t persona_id, uint64_t epoch, const llama_persona_checkpoint * in) { + return (int32_t) ctx->persona_rewind(seq_id, in->blocks, llama_persona_identity { persona_id, epoch }); +} + +llama_pos llama_persona_checkpoint_pos(const llama_persona_checkpoint * checkpoint) { + return checkpoint->blocks.taken ? checkpoint->blocks.pos : -1; +} + +size_t llama_persona_checkpoint_bytes(const llama_persona_checkpoint * checkpoint) { + // what its buffers hold allocated (capacity, not the last take's size): what the governor must count + size_t bytes = 0; + for (const auto & row : checkpoint->blocks.rs.r) { + bytes += row.capacity(); + } + for (const auto & row : checkpoint->blocks.rs.s) { + bytes += row.capacity(); + } + return bytes; +} + const char * llama_persona_status_name(int32_t status) { return llama_persona_move_name((llama_persona_move) status); } diff --git a/src/llama-context.h b/src/llama-context.h index 2f591d621106..f09b7121fd7e 100644 --- a/src/llama-context.h +++ b/src/llama-context.h @@ -66,6 +66,8 @@ struct llama_context { llama_persona_move persona_load(llama_seq_id seq_id, uint32_t stream, llama_persona_blocks & in, const llama_persona_identity & who); // load her into an empty stream the memory chooses (no_empty_stream when none is) llama_persona_move persona_bind(llama_seq_id seq_id, llama_persona_blocks & in, const llama_persona_identity & who); + llama_persona_move persona_checkpoint(llama_seq_id seq_id, llama_persona_checkpoint_blocks & out, const llama_persona_identity & who); + llama_persona_move persona_rewind(llama_seq_id seq_id, const llama_persona_checkpoint_blocks & in, const llama_persona_identity & who); bool opt_state_write_initial(FILE * optimizer, const char * binding); bool opt_state_write(FILE * optimizer, const char * binding); bool opt_shadow_state_write(const llama_adapter_lora * shadow, FILE * optimizer, const char * binding); diff --git a/src/llama-kv-cache.cpp b/src/llama-kv-cache.cpp index 41a49ed8b7c2..d2d69e4b0d8a 100644 --- a/src/llama-kv-cache.cpp +++ b/src/llama-kv-cache.cpp @@ -412,6 +412,7 @@ bool llama_kv_cache::seq_rm(llama_seq_id seq_id, llama_pos p0, llama_pos p1) { if (seq_id >= 0) { auto & cells = v_cells[seq_to_stream[seq_id]]; auto & head = v_heads[seq_to_stream[seq_id]]; + cells.note_cut(p0); // a checkpoint at or past p0 no longer holds what it counted on uint32_t new_head = cells.size(); @@ -438,6 +439,7 @@ bool llama_kv_cache::seq_rm(llama_seq_id seq_id, llama_pos p0, llama_pos p1) { auto & head = v_heads[s]; uint32_t new_head = cells.size(); + cells.note_cut(p0); // every sequence in this stream loses what a checkpoint past p0 counted on for (uint32_t i = 0; i < cells.size(); ++i) { if (!cells.pos_in(i, p0, p1)) { @@ -598,6 +600,7 @@ void llama_kv_cache::seq_add(llama_seq_id seq_id, llama_pos p0, llama_pos p1, ll if (shift == 0) { return; } + cells.note_cut(std::max(p0, 0)); // a shift moves what a checkpoint past p0 counted on uint32_t new_head = cells.size(); @@ -648,6 +651,7 @@ void llama_kv_cache::seq_div(llama_seq_id seq_id, llama_pos p0, llama_pos p1, in if (d == 1) { return; } + cells.note_cut(std::max(p0, 0)); // a division moves what a checkpoint past p0 counted on if (p0 < 0) { p0 = 0; @@ -1327,6 +1331,46 @@ void llama_kv_cache::persona_commit_save(llama_seq_id seq_id, llama_persona_bloc v_heads[s] = 0; } +// Attention KV is never copied by a checkpoint: it stays in place, and only her position is recorded, so a rewind can +// cut what she holds past it. +llama_persona_move llama_kv_cache::persona_check_checkpoint(llama_seq_id seq_id, llama_persona_checkpoint_blocks & out) const { + if (seq_id < 0 || (size_t) seq_id >= seq_to_stream.size()) { + return llama_persona_move::invalid_seq; + } + out.pos = v_cells[seq_to_stream[seq_id]].seq_pos_max(seq_id); + out.lineage = v_cells[seq_to_stream[seq_id]].lineage(); + return llama_persona_move::done; +} + +void llama_kv_cache::persona_commit_checkpoint(llama_seq_id seq_id, llama_persona_checkpoint_blocks & out) { + out.seq = seq_id; + out.serial = v_cells[seq_to_stream[seq_id]].mark_checkpoint(); // cuts are counted from here on, for this checkpoint only +} + +llama_persona_move llama_kv_cache::persona_check_rewind(llama_seq_id seq_id, const llama_persona_checkpoint_blocks & in) const { + if (seq_id < 0 || (size_t) seq_id >= seq_to_stream.size()) { + return llama_persona_move::invalid_seq; + } + if (!in.taken) { + return llama_persona_move::no_checkpoint; + } + if (in.pos > v_cells[seq_to_stream[seq_id]].seq_pos_max(seq_id)) { + return llama_persona_move::checkpoint_ahead; + } + // the cells must be the ones it was taken from, uncut at or below its position since (Codex on #153: a slot + // reused by another mind must never receive these rows) + const auto & cells = v_cells[seq_to_stream[seq_id]]; + if (cells.lineage() != in.lineage || cells.checkpoint() != in.serial || cells.cut_low() <= in.pos) { + return llama_persona_move::checkpoint_stale; + } + return llama_persona_move::done; +} + +void llama_kv_cache::persona_commit_rewind(llama_seq_id seq_id, const llama_persona_checkpoint_blocks & in) { + // what she holds past the checkpoint is cut; what she held up to it is untouched in place + seq_rm(seq_id, in.pos + 1, -1); +} + int32_t llama_kv_cache::persona_empty_stream() const { if (n_stream == 1) { return -1; // unified: one stream holds every sequence, so there is nothing empty to bind into diff --git a/src/llama-kv-cache.h b/src/llama-kv-cache.h index 6e85ed9c25be..c9346b8b67dd 100644 --- a/src/llama-kv-cache.h +++ b/src/llama-kv-cache.h @@ -186,6 +186,10 @@ class llama_kv_cache : public llama_memory_i { llama_persona_slot & slot) const override; void persona_commit_save(llama_seq_id seq_id, llama_persona_blocks & out) override; void persona_commit_load(llama_seq_id seq_id, llama_persona_slot & slot, llama_persona_blocks & in) override; + llama_persona_move persona_check_checkpoint(llama_seq_id seq_id, llama_persona_checkpoint_blocks & out) const override; + void persona_commit_checkpoint(llama_seq_id seq_id, llama_persona_checkpoint_blocks & out) override; + llama_persona_move persona_check_rewind(llama_seq_id seq_id, const llama_persona_checkpoint_blocks & in) const override; + void persona_commit_rewind(llama_seq_id seq_id, const llama_persona_checkpoint_blocks & in) override; uint32_t seq_stream(llama_seq_id seq_id) const override { return seq_to_stream[seq_id]; } diff --git a/src/llama-kv-cells.h b/src/llama-kv-cells.h index 2196dbfb38e3..19f2e43ad657 100644 --- a/src/llama-kv-cells.h +++ b/src/llama-kv-cells.h @@ -4,7 +4,10 @@ #include "llama-cparams.h" #include "llama-kv-cell-index.h" +#include +#include #include +#include #include #include #include @@ -45,9 +48,25 @@ class llama_kv_cells { seq_pos[s].swap(other.seq_pos[s]); } std::swap(stale, other.stale); - } + std::swap(lineage_id, other.lineage_id); + std::swap(lowest_cut, other.lowest_cut); + std::swap(checkpoint_serial, other.checkpoint_serial); + } + + // Continuum fork (CONTINUUM-ENGINE 4.15): whose history these cells hold, and the lowest position cut from it since + // her last checkpoint. A lineage is minted fresh whenever the cells are reset (or made), and travels with them on a + // swap, so a checkpoint follows her between streams and is refused for anyone else's cells in the same slot. + uint64_t lineage() const { return lineage_id; } + llama_pos cut_low() const { return lowest_cut; } + void note_cut(llama_pos p0) { lowest_cut = std::min(lowest_cut, p0); } + // Only the newest checkpoint on a lineage rewinds: each take bumps the serial and starts counting cuts afresh, so + // an older one (whose cut receipt that reset erased) is refused by serial (Codex on #153). + uint64_t checkpoint() const { return checkpoint_serial; } + uint64_t mark_checkpoint() { lowest_cut = std::numeric_limits::max(); return ++checkpoint_serial; } void reset() { + lineage_id = next_lineage(); + lowest_cut = std::numeric_limits::max(); for (uint32_t i = 0; i < pos.size(); ++i) { pos[i] = -1; ext[i].reset(); @@ -519,6 +538,14 @@ class llama_kv_cells { } private: + static uint64_t next_lineage() { + static std::atomic next { 1 }; + return next.fetch_add(1, std::memory_order_relaxed); + } + uint64_t lineage_id = next_lineage(); + llama_pos lowest_cut = std::numeric_limits::max(); + uint64_t checkpoint_serial = 0; + bool has_shift = false; // the occupied cells (i.e. pos[i] != -1, allowed to not have any seq_id): a bitmap with its count and extrema diff --git a/src/llama-memory-hybrid.cpp b/src/llama-memory-hybrid.cpp index db115ba6f3ae..87d8a83f4c76 100644 --- a/src/llama-memory-hybrid.cpp +++ b/src/llama-memory-hybrid.cpp @@ -238,6 +238,31 @@ void llama_memory_hybrid::persona_commit_load(llama_seq_id seq_id, llama_persona mem_recr->persona_commit_load(seq_id, slot, in); } +// Both halves agree on her position or the checkpoint is refused: a rewind must leave attention and recurrent state at +// the same token. +llama_persona_move llama_memory_hybrid::persona_check_checkpoint(llama_seq_id seq_id, llama_persona_checkpoint_blocks & out) const { + const llama_persona_move attn = mem_attn->persona_check_checkpoint(seq_id, out); + return attn != llama_persona_move::done ? attn : mem_recr->persona_check_checkpoint_rows(seq_id, out); +} + +void llama_memory_hybrid::persona_commit_checkpoint(llama_seq_id seq_id, llama_persona_checkpoint_blocks & out) { + mem_attn->persona_commit_checkpoint(seq_id, out); + mem_recr->persona_commit_checkpoint_rows(seq_id, out); +} + +llama_persona_move llama_memory_hybrid::persona_check_rewind(llama_seq_id seq_id, const llama_persona_checkpoint_blocks & in) const { + if (in.rs.has_state && in.rs.pos != in.pos) { + return llama_persona_move::layout_mismatch; // the halves were not at one token when it was taken + } + const llama_persona_move attn = mem_attn->persona_check_rewind(seq_id, in); + return attn != llama_persona_move::done ? attn : mem_recr->persona_check_rewind_rows(seq_id, in); +} + +void llama_memory_hybrid::persona_commit_rewind(llama_seq_id seq_id, const llama_persona_checkpoint_blocks & in) { + mem_attn->persona_commit_rewind(seq_id, in); + mem_recr->persona_commit_rewind_rows(seq_id, in); +} + llama_kv_cache * llama_memory_hybrid::get_mem_attn() const { return mem_attn.get(); } diff --git a/src/llama-memory-hybrid.h b/src/llama-memory-hybrid.h index 8715212f1d78..747bb27e4989 100644 --- a/src/llama-memory-hybrid.h +++ b/src/llama-memory-hybrid.h @@ -95,6 +95,10 @@ class llama_memory_hybrid : public llama_memory_i { llama_persona_slot & slot) const override; void persona_commit_save(llama_seq_id seq_id, llama_persona_blocks & out) override; void persona_commit_load(llama_seq_id seq_id, llama_persona_slot & slot, llama_persona_blocks & in) override; + llama_persona_move persona_check_checkpoint(llama_seq_id seq_id, llama_persona_checkpoint_blocks & out) const override; + void persona_commit_checkpoint(llama_seq_id seq_id, llama_persona_checkpoint_blocks & out) override; + llama_persona_move persona_check_rewind(llama_seq_id seq_id, const llama_persona_checkpoint_blocks & in) const override; + void persona_commit_rewind(llama_seq_id seq_id, const llama_persona_checkpoint_blocks & in) override; uint32_t seq_stream(llama_seq_id seq_id) const override { return mem_attn->seq_stream(seq_id); } llama_memory_recurrent * get_mem_recr() const; diff --git a/src/llama-memory-recurrent.cpp b/src/llama-memory-recurrent.cpp index aec9c96d5693..07d8e3a63c00 100644 --- a/src/llama-memory-recurrent.cpp +++ b/src/llama-memory-recurrent.cpp @@ -551,6 +551,120 @@ void llama_memory_recurrent::persona_commit_load(llama_seq_id seq_id, llama_pers used += 1; } +// A checkpoint copies her recurrent rows (every snapshot group) out of her tail cell and leaves her bound: unlike a +// save, her cell, position and rollback index stay as they are. +llama_persona_move llama_memory_recurrent::persona_check_checkpoint_rows(llama_seq_id seq_id, llama_persona_checkpoint_blocks & out) const { + if (seq_id < 0 || (uint32_t) seq_id >= size || (uint32_t) seq_id >= n_seq_max) { + return llama_persona_move::invalid_seq; + } + const int32_t tail = cells[seq_id].tail; + if (tail >= 0 && cells[tail].seq_id.size() != 1) { + return llama_persona_move::shared_cell; + } + // reserve: every byte the commit copies, so the commit never allocates + const uint32_t n_groups = 1 + n_rs_seq; + out.rs_layout = persona_layout(); + out.rs.r.resize(r_l.size()); + out.rs.s.resize(s_l.size()); + for (size_t il = 0; il < r_l.size(); ++il) { + out.rs.r[il].resize((tail >= 0 && r_l[il]) ? ggml_row_size(r_l[il]->type, r_l[il]->ne[0]) * n_groups : 0); + out.rs.s[il].resize((tail >= 0 && s_l[il]) ? ggml_row_size(s_l[il]->type, s_l[il]->ne[0]) * n_groups : 0); + } + return llama_persona_move::done; +} + +void llama_memory_recurrent::persona_commit_checkpoint_rows(llama_seq_id seq_id, llama_persona_checkpoint_blocks & out) { + auto & rs = out.rs; + const int32_t tail = cells[seq_id].tail; + rs.has_state = tail >= 0; + out.seq = seq_id; + if (!rs.has_state) { + return; + } + const uint32_t n_groups = 1 + n_rs_seq; + const auto rows_out = [&](const ggml_tensor * src, std::vector & dst) { + if (src == nullptr || dst.empty()) { + return; + } + const size_t row = ggml_row_size(src->type, src->ne[0]); + for (uint32_t g = 0; g < n_groups; ++g) { + ggml_backend_tensor_get(src, dst.data() + g * row, (size_t) (g * size + tail) * src->nb[1], row); + } + }; + for (size_t il = 0; il < r_l.size(); ++il) { + rows_out(r_l[il], rs.r[il]); + rows_out(s_l[il], rs.s[il]); + } + rs.pos = cells[tail].pos; + rs.rs_idx = rs_idx[seq_id]; +} + +llama_persona_move llama_memory_recurrent::persona_check_rewind_rows(llama_seq_id seq_id, const llama_persona_checkpoint_blocks & in) const { + if (seq_id < 0 || (uint32_t) seq_id >= size || (uint32_t) seq_id >= n_seq_max) { + return llama_persona_move::invalid_seq; + } + if (!in.taken) { + return llama_persona_move::no_checkpoint; + } + if (in.rs_layout != persona_layout()) { + return llama_persona_move::layout_mismatch; + } + const auto & rs = in.rs; + const int32_t tail = cells[seq_id].tail; + if (!rs.has_state) { + // a checkpoint of a sequence with no recurrent state rewinds only if she still has none + return tail < 0 ? llama_persona_move::done : llama_persona_move::layout_mismatch; + } + if (tail < 0) { + return llama_persona_move::no_checkpoint; + } + if (cells[tail].seq_id.size() != 1) { + return llama_persona_move::shared_cell; + } + if (rs.pos > cells[tail].pos) { + return llama_persona_move::checkpoint_ahead; + } + if (rs.rs_idx > n_rs_seq) { + return llama_persona_move::invalid_rollback; + } + if (rs.r.size() != r_l.size() || rs.s.size() != s_l.size()) { + return llama_persona_move::layout_mismatch; + } + const uint32_t n_groups = 1 + n_rs_seq; + for (size_t il = 0; il < r_l.size(); ++il) { + const size_t r_bytes = r_l[il] ? ggml_row_size(r_l[il]->type, r_l[il]->ne[0]) * n_groups : 0; + const size_t s_bytes = s_l[il] ? ggml_row_size(s_l[il]->type, s_l[il]->ne[0]) * n_groups : 0; + if (rs.r[il].size() != r_bytes || rs.s[il].size() != s_bytes) { + return llama_persona_move::layout_mismatch; + } + } + return llama_persona_move::done; +} + +void llama_memory_recurrent::persona_commit_rewind_rows(llama_seq_id seq_id, const llama_persona_checkpoint_blocks & in) { + const auto & rs = in.rs; + if (!rs.has_state) { + return; + } + const int32_t tail = cells[seq_id].tail; + const uint32_t n_groups = 1 + n_rs_seq; + const auto rows_in = [&](ggml_tensor * dst, const std::vector & src) { + if (dst == nullptr || src.empty()) { + return; + } + const size_t row = ggml_row_size(dst->type, dst->ne[0]); + for (uint32_t g = 0; g < n_groups; ++g) { + ggml_backend_tensor_set(dst, src.data() + g * row, (size_t) (g * size + tail) * dst->nb[1], row); + } + }; + for (size_t il = 0; il < r_l.size(); ++il) { + rows_in(r_l[il], rs.r[il]); + rows_in(s_l[il], rs.s[il]); + } + cells[tail].pos = rs.pos; + rs_idx[seq_id] = rs.rs_idx; +} + void llama_memory_recurrent::set_rs_idx(llama_seq_id seq_id, uint32_t idx) { if (seq_id < 0) { std::fill(rs_idx.begin(), rs_idx.end(), 0); diff --git a/src/llama-memory-recurrent.h b/src/llama-memory-recurrent.h index 13ac2127614b..b2511657508c 100644 --- a/src/llama-memory-recurrent.h +++ b/src/llama-memory-recurrent.h @@ -95,6 +95,14 @@ class llama_memory_recurrent : public llama_memory_i { llama_persona_slot & slot) const override; void persona_commit_save(llama_seq_id seq_id, llama_persona_blocks & out) override; void persona_commit_load(llama_seq_id seq_id, llama_persona_slot & slot, llama_persona_blocks & in) override; + // Her recurrent rows for a checkpoint and a rewind, used only by the hybrid memory, whose attention half checks the + // lineage, the newest-take serial and the lowest cut before these run. Standalone recurrent memory has no lineage to + // check, so it does not override the checkpoint verbs: a checkpoint of it is refused as unsupported, never rewound + // onto a reused slot (Codex on CambrianTech/llama.cpp#153). + llama_persona_move persona_check_checkpoint_rows(llama_seq_id seq_id, llama_persona_checkpoint_blocks & out) const; + void persona_commit_checkpoint_rows(llama_seq_id seq_id, llama_persona_checkpoint_blocks & out); + llama_persona_move persona_check_rewind_rows(llama_seq_id seq_id, const llama_persona_checkpoint_blocks & in) const; + void persona_commit_rewind_rows(llama_seq_id seq_id, const llama_persona_checkpoint_blocks & in); // an undo of a draft is pending on this sequence: its next read takes a snapshot group, not the // live state (group 0) a training graph reads and writes (llm_graph_context::rs_keep) diff --git a/src/llama-memory.cpp b/src/llama-memory.cpp index 646fb357bed0..5ccaab986de7 100644 --- a/src/llama-memory.cpp +++ b/src/llama-memory.cpp @@ -76,6 +76,9 @@ const char * llama_persona_move_name(llama_persona_move m) { case llama_persona_move::wrong_owner: return "wrong_owner"; case llama_persona_move::out_of_memory: return "out_of_memory"; case llama_persona_move::no_empty_stream: return "no_empty_stream"; + case llama_persona_move::no_checkpoint: return "no_checkpoint"; + case llama_persona_move::checkpoint_ahead: return "checkpoint_ahead"; + case llama_persona_move::checkpoint_stale: return "checkpoint_stale"; } return "unknown"; } diff --git a/src/llama-memory.h b/src/llama-memory.h index 5fa23b155c92..2d484e1296f9 100644 --- a/src/llama-memory.h +++ b/src/llama-memory.h @@ -177,6 +177,22 @@ struct llama_memory_i { virtual void persona_commit_load(llama_seq_id seq_id, llama_persona_slot & slot, llama_persona_blocks & in) { (void) seq_id; (void) slot; (void) in; } + // A checkpoint of a BOUND sequence, the same two phases: check reserves every byte (sizes `out`), commit copies her + // rows out and leaves her in place. A rewind writes them back and cuts what she holds past the checkpoint. + virtual llama_persona_move persona_check_checkpoint(llama_seq_id seq_id, llama_persona_checkpoint_blocks & out) const { + (void) seq_id; (void) out; + return llama_persona_move::unsupported; + } + virtual void persona_commit_checkpoint(llama_seq_id seq_id, llama_persona_checkpoint_blocks & out) { + (void) seq_id; (void) out; + } + virtual llama_persona_move persona_check_rewind(llama_seq_id seq_id, const llama_persona_checkpoint_blocks & in) const { + (void) seq_id; (void) in; + return llama_persona_move::unsupported; + } + virtual void persona_commit_rewind(llama_seq_id seq_id, const llama_persona_checkpoint_blocks & in) { + (void) seq_id; (void) in; + } virtual std::map memory_breakdown() const = 0; diff --git a/src/llama-persona-blocks.h b/src/llama-persona-blocks.h index 24601fb4dce8..c80e8ca05900 100644 --- a/src/llama-persona-blocks.h +++ b/src/llama-persona-blocks.h @@ -82,6 +82,27 @@ enum class llama_persona_move { wrong_owner, // her blocks were saved by another persona or another epoch of her out_of_memory, // growing or shrinking her token history failed to allocate; nothing changed no_empty_stream, // a bind found no empty stream to place her in + no_checkpoint, // a rewind was asked of a checkpoint never taken + checkpoint_ahead, // a rewind was asked to a position past what she holds: a rewind never moves forward + checkpoint_stale, // her cells are not the lineage the checkpoint was taken from, or were cut at or below it since +}; + +// A copy of a bound persona at one of her positions, taken WITHOUT moving her: her recurrent rows (every snapshot +// group), her recurrent position and rollback index. Her attention KV is not copied: it stays in place, and a rewind +// cuts it back past the checkpoint's position. So a checkpoint costs her recurrent rows, never her KV (CONTINUUM-ENGINE +// 4.15: a request whose prefix matches her up to a stable boundary prefills only what follows it). +struct llama_persona_checkpoint_blocks { + bool taken = false; + llama_seq_id seq = -1; + llama_pos pos = -1; // her last position held when taken (attention and recurrent agree) + // whose it is (the persona and epoch it was taken for) and the lineage of the cells it was taken from: a rewind is + // refused for another owner, or for cells that are not that lineage or were cut at or below `pos` since (a slot + // reused by another mind, a restart, an eviction), so her rows are never spliced onto someone else's KV + llama_persona_identity owner; + uint64_t lineage = 0; + uint64_t serial = 0; // which checkpoint of that lineage: only the newest rewinds + uint64_t rs_layout = 0; + llama_rs_seq_blocks rs; }; const char * llama_persona_move_name(llama_persona_move m); diff --git a/tests/test-persona-blocks.cpp b/tests/test-persona-blocks.cpp index 16dbbfe63539..a59a46a8a191 100644 --- a/tests/test-persona-blocks.cpp +++ b/tests/test-persona-blocks.cpp @@ -377,6 +377,94 @@ int main(int argc, char ** argv) { llama_persona_free(p); } + // 8. what this catches (CONTINUUM-ENGINE 4.15): a mind re-prefilling her whole prompt every act because the engine + // could not go back to a stable boundary (the M5, 2026-11-18: reused 0 of 11k-30k tokens on every act). A + // checkpoint taken at her prompt's end, after she decoded past it, rewinds her so a DIFFERENT continuation at + // that position is byte-identical to decoding it in place; the same checkpoint rewinds twice (it is not consumed); + // a rewind of a checkpoint never taken, or one ahead of what she holds, is refused and changes nothing. + { + auto st = [](int32_t s) { return (llama_persona_move) s; }; + const llama_token other = 9 % f.n_vocab; + // the reference: her prompt, then a token she never decoded in the checkpointed run + llama_memory_clear(llama_get_memory(f.ctx), true); + f.prompt(0, 1); + const std::vector in_place = f.decode_one(0, other, f.n_prompt); + + llama_persona_checkpoint * cp = llama_persona_checkpoint_init(); + require(cp != nullptr, "a checkpoint"); + llama_memory_clear(llama_get_memory(f.ctx), true); + f.prompt(0, 1); + require_move(st(llama_persona_checkpoint_rewind(f.ctx, 0, 0xA, 1, cp)), llama_persona_move::no_checkpoint, "a rewind of nothing taken"); + require(f.pos_max(0) == f.n_prompt - 1, "a refused rewind leaves her as she was"); + require_move(st(llama_persona_checkpoint_take(f.ctx, 0, 0xA, 1, cp)), llama_persona_move::done, "checkpoint at her prompt's end"); + require(llama_persona_checkpoint_pos(cp) == f.n_prompt - 1, "it holds through her last prompt position"); + require(f.pos_max(0) == f.n_prompt - 1, "taking a checkpoint leaves her bound and whole"); + for (int round = 0; round < 2; ++round) { + f.decode_one(0, 3 % f.n_vocab, f.n_prompt); // she goes on past the boundary... + f.decode_one(0, 5 % f.n_vocab, f.n_prompt + 1); + require_move(st(llama_persona_checkpoint_rewind(f.ctx, 0, 0xA, 1, cp)), llama_persona_move::done, "rewind to the boundary"); + require(f.pos_max(0) == f.n_prompt - 1, "what she held past the boundary is cut"); + char label[96]; + snprintf(label, sizeof(label), "a different continuation after rewind %d", round + 1); + require_identical(label, in_place, f.decode_one(0, other, f.n_prompt)); // ...and continues as if she never had + require_move(st(llama_persona_checkpoint_rewind(f.ctx, 0, 0xA, 1, cp)), llama_persona_move::done, "rewind again before the next round"); + } + // what this catches (Codex on #153): A's rows spliced onto B's KV when B reuses A's slot at the same layout and + // position. B in the slot A was checkpointed in: refused as stale under A's owner, as wrong owner under B's, + // and B continues exactly as in place. + const auto ref_b = f.reference(0, 2); + llama_memory_clear(llama_get_memory(f.ctx), true); + f.prompt(0, 1); + require_move(st(llama_persona_checkpoint_take(f.ctx, 0, 0xA, 1, cp)), llama_persona_move::done, "A's checkpoint"); + llama_memory_clear(llama_get_memory(f.ctx), true); + f.prompt(0, 2); // B, same slot, same length + require_move(st(llama_persona_checkpoint_rewind(f.ctx, 0, 0xA, 1, cp)), llama_persona_move::checkpoint_stale, "A's rows never reach B's cells"); + require_move(st(llama_persona_checkpoint_rewind(f.ctx, 0, 0xB, 1, cp)), llama_persona_move::wrong_owner, "nor under B's owner"); + f.require_continues("B untouched by the refused rewinds", 0, ref_b); + + // what this catches: a checkpoint lost when she moves between streams (four minds rotate through two). Taken + // on her stream, she is saved and bound into the other one, and the rewind there continues byte-identically. + llama_memory_clear(llama_get_memory(f.ctx), true); + f.prompt(0, 1); + require_move(st(llama_persona_checkpoint_take(f.ctx, 0, 0xA, 1, cp)), llama_persona_move::done, "A's checkpoint before her move"); + f.decode_one(0, 3 % f.n_vocab, f.n_prompt); + require_move(ctx.persona_save(0, a, A), llama_persona_move::done, "A out"); + require_move(ctx.persona_load(0, 1 - llama_get_memory(f.ctx)->seq_stream(0), a, A), llama_persona_move::done, "A into the other stream"); + require_move(st(llama_persona_checkpoint_rewind(f.ctx, 0, 0xA, 1, cp)), llama_persona_move::done, "her checkpoint follows her"); + require_identical("a different continuation after her move and rewind", in_place, f.decode_one(0, other, f.n_prompt)); + + // what this catches (Codex on #153 at 12a98779b): a newer take erasing the cut receipt an older checkpoint + // needed. Take A, cut below it and refill a different suffix to the same length, take B: A is stale (by its + // serial), B still rewinds. + { + llama_persona_checkpoint * newer = llama_persona_checkpoint_init(); + require(newer != nullptr, "a second checkpoint"); + llama_memory_clear(llama_get_memory(f.ctx), true); + f.prompt(0, 1); + require_move(st(llama_persona_checkpoint_take(f.ctx, 0, 0xA, 1, cp)), llama_persona_move::done, "checkpoint A"); + const llama_pos half = f.n_prompt / 2; + require(llama_memory_seq_rm(llama_get_memory(f.ctx), 0, half, -1), "cut below A"); + for (llama_pos p = half; p < f.n_prompt; ++p) { + f.decode_one(0, 11 % f.n_vocab, p); // a different suffix, to the same length + } + require_move(st(llama_persona_checkpoint_take(f.ctx, 0, 0xA, 1, newer)), llama_persona_move::done, "checkpoint B"); + require_move(st(llama_persona_checkpoint_rewind(f.ctx, 0, 0xA, 1, cp)), llama_persona_move::checkpoint_stale, "A no longer matches what she holds"); + require_move(st(llama_persona_checkpoint_rewind(f.ctx, 0, 0xA, 1, newer)), llama_persona_move::done, "B, the newest, still rewinds"); + llama_persona_checkpoint_free(newer); + } + + // ahead: she holds less than the checkpoint does + llama_memory_clear(llama_get_memory(f.ctx), true); + const int full = f.n_prompt; + f.n_prompt = full / 2; + f.prompt(0, 1); + require_move(st(llama_persona_checkpoint_rewind(f.ctx, 0, 0xA, 1, cp)), llama_persona_move::checkpoint_ahead, "a rewind never moves forward"); + require(f.pos_max(0) == f.n_prompt - 1, "a refused forward rewind leaves her as she was"); + f.n_prompt = full; + fprintf(stderr, "checkpoint: %zu bytes of recurrent rows (attention KV stays in place)\n", llama_persona_checkpoint_bytes(cp)); + llama_persona_checkpoint_free(cp); + } + fprintf(stderr, "test-persona-blocks: ok (%zu attention layers, %zu recurrent layers)\n", a.kv.k.size(), a.rs.r.size()); return 0; }