Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
715840b
kv: a persona's KV moves between a stream and host memory as raw rows…
joelteply Nov 17, 2026
1770577
memory: a persona's whole hybrid state moves raw (attention stream + …
joelteply Nov 17, 2026
a1c5c8e
memory: one two-phase persona move (check reserves, commit cannot fai…
joelteply Nov 17, 2026
8c186c8
memory: a persona move cannot fail after it starts; her blocks carry …
joelteply Nov 17, 2026
70ab7f7
memory: layout identity computed once; allocation-fault and rollback-…
joelteply Nov 17, 2026
0441cbb
memory: recurrent rows move through per-tensor helpers (GCC -Werror=d…
joelteply Nov 17, 2026
45dadf0
tests: test-persona-blocks joins the internal-API tests (Windows shar…
joelteply Nov 17, 2026
accc9e3
kv-cells: member swap that never allocates; a persona's cells move wi…
joelteply Nov 17, 2026
2d54d48
kv-cells: the two derived indices held natively, so a persona's metad…
joelteply Nov 17, 2026
bd97171
kv-cells: indices are settled before a persona leaves; a bind and eve…
joelteply Nov 17, 2026
cfd73ad
Merge remote-tracking branch 'origin/codex/s4-per-mind-integration' i…
joelteply Nov 17, 2026
347a13a
test-kv-cell-index: main names no unused argv (GCC -Werror=unused-par…
joelteply Nov 17, 2026
8fe7f7d
test-kv-cell-index: the cost baseline is the exact previous llama_kv_…
joelteply Nov 17, 2026
cab708e
test-kv-cell-index: time with the allocation counter off; count alloc…
joelteply Nov 17, 2026
6b31e00
kv cells: a query never writes an index; each cache operation settles…
joelteply Nov 17, 2026
6aea5d4
server: /slots reads the cache on the main thread only; a determinist…
joelteply Nov 17, 2026
b8f7f58
llama: a public seam for a persona's move: init, save, load, free, st…
joelteply Nov 17, 2026
0f1acdb
llama: a persona's token history as one reserved contiguous buffer, a…
joelteply Nov 17, 2026
d853768
llama: persona token appends are idempotent across her whole sequence…
joelteply Nov 17, 2026
f3eb5ec
llama: persona receipts are reserved with her tokens; an accepted app…
joelteply Nov 17, 2026
e046d2a
llama: her token history lives in the persona beside her blocks; a C …
joelteply Nov 17, 2026
b004e38
llama: her blocks move only under the owner of her token history; a s…
joelteply Nov 18, 2026
6078f43
test-persona-tokens: zero allocations on the append path is measured,…
joelteply Nov 18, 2026
4caa55b
Merge commit '5e4163402' into fable/persona-owner
joelteply Nov 18, 2026
bc9c9ae
llama: llama_persona_bind places her in an empty stream the memory ch…
joelteply Nov 18, 2026
53609c5
llama: her token history is sized by nothing at creation; her owner g…
joelteply Nov 18, 2026
8f558c4
llama: no reserve; a persona is made with her owner and her history g…
joelteply Nov 18, 2026
57812cd
test-persona-tokens: comments say grown, not reserved
joelteply Nov 18, 2026
37baf8c
llama: ownership lives in the native save, load and bind; test histor…
joelteply Nov 18, 2026
a88f255
llama: a persona binds as whichever sequence she is bound as; her cel…
joelteply Nov 18, 2026
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
68 changes: 68 additions & 0 deletions include/llama.h
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,74 @@ extern "C" {
struct llama_memory_cells_report * out,
int32_t n_max);

// A persona's execution state as native blocks in host memory (Continuum fork, CONTINUUM-ENGINE 4.14). A switch
// between personas is raw row copies and a swap of her cell records: nothing tokenized, serialized, walked or
// recomputed. Her blocks stay tagged with the sequence they were saved from and load back under it, into any empty
// stream; the memory must hold one stream per sequence (not unified) with V not transposed (flash attention on).
struct llama_persona;

// One persona: her blocks prepared for this context's memory and her token history, owned by persona_id at epoch
// from the start (nothing is reserved or sized here; her history grows and shrinks with her). NULL when the memory
// has no persona move.
LLAMA_API struct llama_persona * llama_persona_init(struct llama_context * ctx, uint64_t persona_id, uint64_t epoch);
LLAMA_API void llama_persona_free(struct llama_persona * persona);

// Move sequence seq_id's state out of the memory into persona's blocks; her stream is left empty. Two-phase: a
// refusal leaves both sides as they were. 0 when done, else a status for llama_persona_status_name.
LLAMA_API int32_t llama_persona_save(struct llama_context * ctx, llama_seq_id seq_id, struct llama_persona * persona);

// Bind persona's blocks into the empty stream as sequence seq_id (the sequence they were saved from). 0 when done.
LLAMA_API int32_t llama_persona_load(struct llama_context * ctx, llama_seq_id seq_id, uint32_t stream, struct llama_persona * persona);

// Bind her blocks as sequence seq_id into an empty stream the memory chooses: the caller never numbers streams.
// no_empty_stream when none is empty; otherwise exactly llama_persona_load's checks and statuses.
LLAMA_API int32_t llama_persona_bind(struct llama_context * ctx, llama_seq_id seq_id, struct llama_persona * persona);

LLAMA_API const char * llama_persona_status_name(int32_t status);

// 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).
LLAMA_API bool llama_persona_owner(const struct llama_persona * persona, uint64_t * persona_id, uint64_t * epoch);

// Her token history, held in the persona beside her blocks (so it moves with them by reference, never copied):
// one buffer of her token ids and one of her append receipts, grown by her owner. A request appends only its new ids, in
// one validated transaction (see llama_persona_append). Owned once by her admitted owner; 0 when done.
// Her history grows and shrinks with her, never inside an act: grow adds room for more_tokens ids and more_receipts
// appends ahead of need; shrink gives back the room she does not hold as she cools. Neither drops anything she
// holds; out_of_memory on allocation failure, with her history unchanged.
LLAMA_API int32_t llama_persona_grow_tokens(struct llama_persona * persona, size_t more_tokens, size_t more_receipts);
LLAMA_API int32_t llama_persona_shrink_tokens(struct llama_persona * persona);

// What an append asserts about her history: her persona, her epoch, her request sequence (1, 2, 3, ...), the
// frontier it expects her history to end at, and the request's own 128-bit identity.
struct llama_persona_append_header {
uint64_t persona;
uint64_t epoch;
uint64_t seq;
uint64_t frontier;
uint64_t request_hi;
uint64_t request_lo;
};

struct llama_persona_append_receipt {
int32_t status; // llama_persona_append_status_name; 0 = appended, 1 = replayed (its original receipt)
uint64_t seq;
uint64_t from; // where the delta starts in her history
uint64_t to; // her frontier after it
};

// Append `delta` (n ids, read in place; null only when n is 0) to her history after validating `header`. A retry
// of a committed sequence returns its original receipt and writes nothing; nothing allocates.
LLAMA_API struct llama_persona_append_receipt llama_persona_append(struct llama_persona * persona,
const struct llama_persona_append_header * header,
const llama_token * delta, size_t n);

// Her history as held: the ids and their count (her frontier). Empty until her first append.
LLAMA_API const llama_token * llama_persona_token_ids(const struct llama_persona * persona, uint64_t * n);

LLAMA_API const char * llama_persona_append_status_name(int32_t status);

//
// State / sessions
//
Expand Down
156 changes: 155 additions & 1 deletion src/llama-context.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#include "llama-context.h"

#include <new>
#include "llama-persona-tokens.h"

#include "ggml.h"
#include "llama-arch.h"
#include "llama-graph.h"
Expand Down Expand Up @@ -2567,12 +2570,60 @@ llm_graph_type llama_context::graph_type() const {
throw std::runtime_error("Unsupported ctx type");
}

llama_persona_move llama_context::persona_prepare(llama_persona_blocks & blocks) const {
return memory ? memory->persona_prepare(blocks) : llama_persona_move::unsupported;
}

llama_persona_move llama_context::persona_save(llama_seq_id seq_id, llama_persona_blocks & out, const llama_persona_identity & who) {
if (!memory) {
return llama_persona_move::unsupported;
}
synchronize(); // a decode returns before its writes land; the raw copies read the same device memory
const llama_persona_move checked = memory->persona_check_save(seq_id, out);
if (checked != llama_persona_move::done) {
return checked;
}
memory->persona_commit_save(seq_id, out);
out.owned = true; // a completed save stamps her blocks with her owner
out.owner = who.persona;
out.owner_epoch = who.epoch;
return llama_persona_move::done;
}

llama_persona_move llama_context::persona_load(llama_seq_id seq_id, uint32_t stream, llama_persona_blocks & in, const llama_persona_identity & who) {
if (!memory) {
return llama_persona_move::unsupported;
}
if (!in.belongs_to(who)) {
return llama_persona_move::wrong_owner; // refused before anything is written
}
synchronize();
llama_persona_slot slot;
const llama_persona_move checked = memory->persona_check_load(seq_id, stream, in, slot);
if (checked != llama_persona_move::done) {
return checked;
}
memory->persona_commit_load(seq_id, slot, in);
return llama_persona_move::done;
}

llama_persona_move llama_context::persona_bind(llama_seq_id seq_id, llama_persona_blocks & in, const llama_persona_identity & who) {
if (!memory) {
return llama_persona_move::unsupported;
}
const int32_t stream = memory->persona_empty_stream();
if (stream < 0) {
return llama_persona_move::no_empty_stream;
}
return persona_load(seq_id, (uint32_t) stream, in, who);
}

llm_graph_params llama_context::graph_params(
llm_graph_result * res,
const llama_ubatch & ubatch,
const llama_memory_context_i * mctx,
llm_graph_type gtype) const {
return {
llm_graph_params params = {
/*.arch =*/ model.arch,
/*.hparams =*/ model.hparams,
/*.cparams =*/ cparams,
Expand All @@ -2591,6 +2642,13 @@ llm_graph_params llama_context::graph_params(
/*.cb =*/ graph_get_cb(),
/*.res =*/ res,
};
if (memory) {
params.streams.reserve(ubatch.n_seqs_unq);
for (uint32_t s = 0; s < ubatch.n_seqs_unq; ++s) {
params.streams.push_back(memory->seq_stream(ubatch.seq_id_unq[s]));
}
}
return params;
}

ggml_status llama_context::graph_compute(
Expand Down Expand Up @@ -4199,3 +4257,99 @@ llama_memory_breakdown llama_get_memory_breakdown(const struct llama_context * c
llama_context * llama_get_ctx_other(struct llama_context * ctx) {
return ctx->get_cparams().ctx_other;
}

// Continuum fork (CONTINUUM-ENGINE 4.14): the public seam of a persona's move (llama-persona-blocks.h).

struct llama_persona {
llama_persona_blocks blocks;
std::unique_ptr<llama_persona_tokens> tokens; // her token history, beside her blocks (llama-persona-tokens.h)
};

// Her owner grows her history ahead of need, between acts: an append on the hot path only writes into it.
int32_t llama_persona_grow_tokens(llama_persona * persona, size_t more_tokens, size_t more_receipts) {
return (int32_t) (persona->tokens->grow(more_tokens, more_receipts) ? llama_persona_move::done : llama_persona_move::out_of_memory);
}

// As she cools her owner gives back the room she does not hold; nothing she holds is dropped.
int32_t llama_persona_shrink_tokens(llama_persona * persona) {
return (int32_t) (persona->tokens->shrink() ? llama_persona_move::done : llama_persona_move::out_of_memory);
}

llama_persona_append_receipt llama_persona_append(llama_persona * persona, const llama_persona_append_header * header,
const llama_token * delta, size_t n) {
llama_persona_append_receipt out = { (int32_t) llama_persona_append_status::invalid, 0, 0, 0 };
if (header == nullptr) {
return out;
}
llama_persona_append_request h;
h.persona = header->persona;
h.epoch = header->epoch;
h.seq = header->seq;
h.frontier = header->frontier;
h.request.hi = header->request_hi;
h.request.lo = header->request_lo;
const llama_persona_receipt r = persona->tokens->append(h, delta, n);
out.status = (int32_t) r.status;
out.seq = r.seq;
out.from = r.from;
out.to = r.to;
return out;
}

const llama_token * llama_persona_token_ids(const llama_persona * persona, uint64_t * n) {
*n = persona->tokens->frontier();
return persona->tokens->data();
}

const char * llama_persona_append_status_name(int32_t status) {
return llama_persona_append_status_name((llama_persona_append_status) status);
}

llama_persona * llama_persona_init(llama_context * ctx, uint64_t persona_id, uint64_t epoch) {
auto * persona = new (std::nothrow) llama_persona();
if (persona == nullptr) {
return nullptr;
}
persona->tokens.reset(new (std::nothrow) llama_persona_tokens(persona_id, epoch));
if (persona->tokens == nullptr || ctx->persona_prepare(persona->blocks) != llama_persona_move::done) {
delete persona;
return nullptr;
}
return persona;
}

void llama_persona_free(llama_persona * persona) {
delete persona;
}

// Her blocks move only under her owner (the persona and epoch of her token history): the native save stamps them,
// the native load and bind refuse blocks stamped by anyone else, so no path skips the check.
static llama_persona_identity identity_of(const llama_persona * persona) {
return { persona->tokens->persona(), persona->tokens->epoch() };
}

int32_t llama_persona_save(llama_context * ctx, llama_seq_id seq_id, llama_persona * persona) {
return (int32_t) ctx->persona_save(seq_id, persona->blocks, identity_of(persona));
}

int32_t llama_persona_load(llama_context * ctx, llama_seq_id seq_id, uint32_t stream, llama_persona * persona) {
return (int32_t) ctx->persona_load(seq_id, stream, persona->blocks, identity_of(persona));
}

int32_t llama_persona_bind(llama_context * ctx, llama_seq_id seq_id, llama_persona * persona) {
return (int32_t) ctx->persona_bind(seq_id, persona->blocks, identity_of(persona));
}

// Whose blocks these are, as stamped by the last completed save (false before any).
bool llama_persona_owner(const llama_persona * persona, uint64_t * persona_id, uint64_t * epoch) {
if (!persona->blocks.owned) {
return false;
}
*persona_id = persona->blocks.owner;
*epoch = persona->blocks.owner_epoch;
return true;
}

const char * llama_persona_status_name(int32_t status) {
return llama_persona_move_name((llama_persona_move) status);
}
9 changes: 9 additions & 0 deletions src/llama-context.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ struct llama_context {
void sched_reserve();

void synchronize();

// Continuum fork (CONTINUUM-ENGINE 4.14): move a persona's state between the memory and host memory. Fences the
// queued device work, checks every condition (reserving the destination), then commits raw copies; a refusal
// changes nothing.
llama_persona_move persona_prepare(llama_persona_blocks & blocks) const;
llama_persona_move persona_save(llama_seq_id seq_id, llama_persona_blocks & out, const llama_persona_identity & who);
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);
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
7 changes: 7 additions & 0 deletions src/llama-graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,10 @@ struct llm_graph_params {

llm_graph_result * res;

// the physical attention stream of each of the ubatch's unique sequences (seq_id_unq order): the graph's K/V views
// are built at these streams, so a graph is reused only when they match, not merely the sequence ids
std::vector<uint32_t> streams = {};

// return true if the "other" params would result in a graph with the same topology as with the current params
// having the same topology allows us to reuse the graph in some cases
bool allow_reuse(const llm_graph_params & other) const {
Expand Down Expand Up @@ -859,6 +863,9 @@ struct llm_graph_params {
}
}

// the graph's K/V views are built at these physical streams, whatever the split: never reuse across streams
can_reuse_ubatch = can_reuse_ubatch && streams == other.streams;

if (!can_reuse_ubatch) {
return false;
}
Expand Down
2 changes: 2 additions & 0 deletions src/llama-kv-cache-iswa.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ class llama_kv_cache_iswa : public llama_memory_i {
//

llama_kv_cache * get_base() const;

uint32_t seq_stream(llama_seq_id seq_id) const override { return kv_base->seq_stream(seq_id); }
llama_kv_cache * get_swa () const;

private:
Expand Down
Loading
Loading