Repository navigation
llama learn: a shadow's optimizer buffers are slots native code sizes, never increments (L2 accounting; stacked on #6148) - #6149
Merged
Conversation
…s learning L1, no policy) A reply trains on one live sequence of a serving context, one chunk per step, then restores her served tail under her stable set. The run owns its admission and optimizer parameters in one boxed binding the native callbacks reach, from shadow selection until a confirmed restoration, and hands the admission back either way (Finished::Learned / Failed, Refused before it began). finish() is explicit and fallible (drain, then step until restored); dropping an unfinished run drains it and records the custody loss on its Learner, which refuses the next reply until the owner takes it. ReplyAdmission: optimizer_buffer (an increment, exact aligned size) and workspace (validates a lease already held, never takes one, so a repeated measurement is never charged twice). Typed refusals in Rust; buffer-type names and bools only at the FFI boundary; a panicking admission refuses instead of unwinding into native code. Design: docs/architecture/IN-PROCESS-LEARNING.md (#6147). Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo
… named, never a map_or (unknown_as_value 216 -> 215) Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo
…nal and blocks until fenced (Codex on #6148) - Native code keeps the callback pointers past a reply (llama-context-opt.cpp :192-193, :275-276, :330-331, :365-366), so ONE CallbackHolder lives on Context (declared after ptr, freed after llama_free). A reply places its admission there and takes it back; an empty holder refuses. - Lengths and the result are validated before llama_opt_init. - RunState {Running, Restored, Failed(refusal)}: a failed step stays failed; step() never calls native past a terminal status; finish() returns Failed and blocks the learner. - Lost custody keeps a dropped run's admission (bytes retained); reading it never clears it; only Learner::fence (memory_seq_rm of the sequence) does, handing that admission back. - Tests: failure-after-step through later steps and finish, terminal restoration, horizon encoding. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo
…ry proves native release (Codex on #6148) - The quarantine lives in the context's CallbackHolder, not the temporary Learner: a new Learner on the same context is refused while it holds, and the failed run's admission (bytes retained) stays in it. Finished::Failed no longer hands the admission back. - Learner::recover() lifts it only after llama_opt_release_workspace (workspace replaced) and llama_opt_release(shadow) (no optimizer references it) both succeed, then hands back the held admission. It no longer clears the sequence: memory_seq_rm proved nothing about optimizer memory and destroyed recoverable state; the owner restores her sequence from its own preserved state. - The trained shadow is an Arc<LoraAdapter> the holder keeps until a release proves no optimizer references it; the model is the context's own (Context::model), not an independent reference. - Test: a quarantine keeps the failed run's admission and the holder admits nothing. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo
…8 -> 2187: no clone passed into a call) Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo
…the context until a confirmed release (Codex on #6148) Native code keeps one optimizer per adapter across shadow switches (llama-context-opt.cpp opt_minds), so the holder kept only the current Arc and a switch or a refused selection could drop an adapter its optimizer still references. Now: - OptimizerLease (per shadow, its buffers' increments) is split from ReplyAdmission (per reply, workspace lease validation): optimizer residency is never charged to, or released with, a reply. - Learner::retain_shadow(Arc<LoraAdapter>, lease) keeps the adapter and its lease in the context's Shadows registry; begin() refuses a shadow not retained and only marks it selected; release_shadow() hands the lease back only after llama_opt_release confirms no optimizer references the adapter. recover() releases the workspace only. - Test: A -> B -> A and a refused B selection keep both shadows and A's leased bytes; only a release takes one out. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo
…ount rose (Codex on #6148) A begin then an immediate finish drains before any gradient, and a confirmed restoration was returned as Learned with zero committed updates. finish now reads llama_opt_shadow_steps for her shadow before begin and after restoration, and llama_opt_shadow_quarantined (as server-context-learn.inc:582-589 does): - Learned { steps: NonZeroU64, loss } only for actual commits (a drain after a commit keeps them); - Unchanged when nothing committed, with no loss; - Uncertain { steps } when native code marks the shadow's effects uncertain. Recovery order made explicit: opt_release_workspace refuses while any shadow keeps an optimizer (opt_configured), so recover() refuses with the retained count, before any native call and keeping the quarantine and every retained shadow and lease, until each is released through release_shadow. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo
…(E0596, Codex on #6148) Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo
…Codex's BigMama replay) ContextParams::default() pools (Mean, for embeddings); a reply trains on per-token logits, and native code refused only at the first labelled step. begin now reads llama_pooling_type and refuses with step "context" (the admission handed back) unless it is None. The persona scheduler's context already sets PoolingType::None (driver.rs:154 on #6140). Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo
…, never increments (Codex's A/B/A trace)
ggml_opt_static_allocation_allowed (ggml-opt.cpp:558-565 at the pin) passes the WHOLE size of the
buffer it is about to allocate, and the 32-byte optimizer_host buffer is freed (:627) and asked
for again (:908) on every init, so the old increment contract charged a revisited shadow again
for memory that was already gone.
New safe/optimizer_slots.rs (pure, tested without a model):
- Role::{Static, Host}, parsed from the two native names; any other name is refused.
- Slot { role, device: BufferType }: the boundary type. BufferType gains device() and is_host().
- OptimizerLease { admit(slot, bytes), release(slot, bytes) }: told growth and proven frees.
- Slots per retained shadow: a request means 'this slot is about to be N'; the growth is
admitted before native allocates, a shrink is released, and a revisit at the same size charges
nothing. A refused host request releases the host bytes (already freed). A refusal later in the
same init rolls back that init's earlier charges: every request precedes every allocation
(:906-908, then :912/:921).
- learn.rs: the callback charges the selected shadow's slots; begin settles them after
llama_opt_init; release_shadow releases every slot once llama_opt_release confirms.
Tests: a revisit at the same sizes charges nothing new (270368 + 32 once); a host refusal rolls
back the same init's static charge; a refused host revisit releases the freed host and keeps the
static; a shrink releases the difference and a release frees every slot; only the two native
names have a role.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo
… pasted a test into it; Codex on #6149) Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo
…er-slots # Conflicts: # core/llama/src/safe.rs # core/llama/src/safe/learn.rs
joelteply
marked this pull request as ready for review
November 18, 2026 21:10
…rs prove what survived (Codex on #6149) Requests come from llama_opt_init and from the graph builds inside learn steps, so inferring which requests form one group (the settle/rollback in the last head) was fragile. Now: - Slots::ask admits only growth past what is charged and holds it; nothing is released on inference. - After each optimizer operation (llama_opt_init, every learn step and graph step, and a confirmed release_shadow), never on ordinary decode, the Learner visits llama_opt_visit_memory on the context's owner and reconciles every retained shadow's slots to the bytes native code owns (ggml_opt_visit_owned_buffers names only optimizer_static and optimizer_host). Charged but not resident is released. - Resident but never admitted, an unknown kind, or bytes owned for a shadow the context does not retain is an accounting defect: Learner::accounting() names it and begin() refuses until an owner looks. - settle, the per-init 'before' journal and the inferred rollback are deleted. Tests: the revisit charges nothing new; a refused group is released only when the visit proves nothing survived; a refused host revisit releases the freed host and keeps the static; a proven shrink releases the difference; resident bytes never admitted are a defect; a visit attributes by shadow and role or names the defect. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo
… defect stops the run and is never Learned (Codex on #6149 at 69c07dd) - Slots::reconcile kept native excess in the charge (charged = resident), so admit 32, observe 64, free to 0 released 64 for a lease granted 32. Now the charge stays what was admitted; bytes resident beyond it are reported as Unadmitted on each visit and never released, through reconcile and release_all alike. - An accounting defect found after a learn step or graph step drains the reply (no further gradient; only the restoration runs), and finish reports Uncertain, never Learned, while accounting is untrusted. - The visitor no longer reads a size past u64 as u64::MAX (unknown_as_value): it is an unattributable buffer, so an accounting defect. - contract_shape: the visitor's seven parameters are llama.cpp's fixed callback, so one existing site is retired to offset it: mtmd eval_media's text, bytes and kind travel together as one Media. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Serves the goal that a mind learns from her own work in-process (IN-PROCESS-LEARNING.md, L2), so learning can run beside one-mind-at-a-time serving without its memory lease drifting. Stacked on #6148; retargets to canary once #6148 merges.
Why
Codex's native A/B/A trace on #6148 at 5611557: when shadow A is selected again, native code asks for
optimizer_host(32 bytes) again.ggml-opt.cpp:627freesbuf_cpubefore:921allocates the new one, andoptimizer_staticpersists. The #6148 callback was documented as an increment, so every revisit would add another 32-byte lease for memory that was already gone.Native contract (pin a88f255cc)
ggml_opt_static_allocation_allowed(:558-565) passesggml_backend_alloc_ctx_tensors_from_buft_size, the WHOLE size of the buffer about to be allocated, never an increment.optimizer_staticon backend 0's default buffer type, asked only while!buf_static(:906);optimizer_hoston the CPU buffer type, asked on every init (:908) after:627freed it.:906-908, then:912/:921).Change
core/llama/src/safe/optimizer_slots.rs(pure, tested without a model):Role::{Static, Host}parsed from the native name (an unknown name is refused);Slot { role, device: BufferType };OptimizerLease { admit(slot, bytes), release(slot, bytes) }; per-shadowSlots. A request means "this slot is about to be N": the growth is admitted before allocation, a shrink is released, and a same-size revisit charges nothing. A refused host request releases the host bytes, which are already freed. A refusal later in the same init rolls back that init's earlier charges.BufferTypegainsdevice()andis_host(); it isSend/Sync(a process-lifetime backend singleton).learn.rs: the callback charges the selected shadow's slots;beginsettles them afterllama_opt_init;release_shadowreleases every slot afterllama_opt_releaseconfirms.OptimizerBufferand the increment trait are deleted.Lines: learn.rs 820 -> 846, new optimizer_slots.rs 267 (both under 1000).
Measured
Unit tests only, so far. The native replay (Codex's A/B/A trace: A's revisit charges 0 new host bytes, static persists) is to be rerun on BigMama at this head. Not built locally: this machine serves.
🤖 Generated with Claude Code
https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo