Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions include/llama.h
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
71 changes: 71 additions & 0 deletions src/llama-context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
2 changes: 2 additions & 0 deletions src/llama-context.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
44 changes: 44 additions & 0 deletions src/llama-kv-cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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)) {
Expand Down Expand Up @@ -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<llama_pos>(p0, 0)); // a shift moves what a checkpoint past p0 counted on

uint32_t new_head = cells.size();

Expand Down Expand Up @@ -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<llama_pos>(p0, 0)); // a division moves what a checkpoint past p0 counted on

if (p0 < 0) {
p0 = 0;
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/llama-kv-cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -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]; }

Expand Down
29 changes: 28 additions & 1 deletion src/llama-kv-cells.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
#include "llama-cparams.h"
#include "llama-kv-cell-index.h"

#include <algorithm>
#include <atomic>
#include <bitset>
#include <limits>
#include <cassert>
#include <cstring>
#include <vector>
Expand Down Expand Up @@ -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<llama_pos>::max(); return ++checkpoint_serial; }

void reset() {
lineage_id = next_lineage();
lowest_cut = std::numeric_limits<llama_pos>::max();
for (uint32_t i = 0; i < pos.size(); ++i) {
pos[i] = -1;
ext[i].reset();
Expand Down Expand Up @@ -519,6 +538,14 @@ class llama_kv_cells {
}

private:
static uint64_t next_lineage() {
static std::atomic<uint64_t> next { 1 };
return next.fetch_add(1, std::memory_order_relaxed);
}
uint64_t lineage_id = next_lineage();
llama_pos lowest_cut = std::numeric_limits<llama_pos>::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
Expand Down
25 changes: 25 additions & 0 deletions src/llama-memory-hybrid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
4 changes: 4 additions & 0 deletions src/llama-memory-hybrid.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading
Loading