Skip to content

server: one shadow per mind on the serving context: each shadow keeps its own optimizer, /learn trains any loaded shadow, and unload releases it - #85

Closed
joelteply wants to merge 5 commits into
codex/s4-resident-microstepsfrom
cormac/per-mind-shadow
Closed

joelteply wants to merge 5 commits into
codex/s4-resident-microstepsfrom
cormac/per-mind-shadow

Conversation

@joelteply

@joelteply joelteply commented Nov 1, 2026 •

Copy link
Copy Markdown

Continuum card eaeb2b87, S4 (a), the in-place dream stage (ONE-RESIDENT-MODEL §8.1 / §9). Today a serving context learns for exactly ONE mind per server run: a second resident's /learn is refused with "one shadow per server run". This lets every resident on a lane learn in place, each with her own shadow and optimizer.

Change

  • One optimizer per shadow. llama_context keeps one optimizer per shadow (opt_minds), and opt_ctx/opt_shadow are the active pair, so every existing training site is unchanged.
    • llama_opt_init with another adapter on a configured context calls opt_select. It creates that shadow's optimizer the first time and keeps each shadow's moments and accumulators across switches.
    • Only the active shadow's tensors carry PARAM, so a graph never trains a parameter the active optimizer lacks.
  • opt_configured() replaces opt_ctx != nullptr where the question is whether the context is sized for training (reserve skip, graph sizing). Releasing the active shadow would otherwise have rebuilt a scheduler that other shadows' optimizers still hold.
  • Unload releases instead of refusing. llama_opt_release drops one shadow's optimizer. The server's unload now releases a shadow that learned, instead of refusing it forever. Its moments are lost; the snapshot in (d) will be the durable form.
  • The server's single-shadow learn_lora_id is deleted.

Measured (IntelMac CPU, Release, Qwen2.5-0.5B Q4_K_M, -np 1, 2026-11-01)

  • learn-every-format.py --two-minds, fresh server: shadow-05 and a byte copy, shadow-06, learn alternately. Losses: 4.3179, 4.3179, 3.5043, 3.5043.
    • B's first step is untouched by A's step, and each second step continues its own optimizer.
    • Matching trajectories hold only if the weights and optimizers are independent; the test asserts it.
  • --shadow-retirement: a bound shadow unloads, and a reloaded one learns (loss 4.3179, 974 ms).
  • Not built: Metal/CUDA. This is host-side state only; no kernel changes.

Contract for the core: a shadow IS its file

POST /lora-adapters/load deduplicates by path on purpose: a client that dropped after posting and retries must get back the id it never received. So loading the same file twice returns the SAME adapter, with its trained weights.

  • Measured on a fresh server: load shadow-05 as A, take one step, load shadow-05 again. It returns id 0 again, and its next loss is 3.5043, A's second step.
  • Two minds learn independently only from distinct files. The core gives each mind her own shadow file, a copy of her stable gene, before her first learn.
  • The two-minds test uses a byte copy for this reason.

🤖 Generated with Claude Code

https://claude.ai/code/session_01FRQWzgSfo79JtnwZywHKrE

@joelteply
joelteply changed the base branch from master to 889580fc/seq-state-across-stream-counts November 1, 2026 07:14
@github-actions github-actions Bot added the server label Nov 1, 2026
@joelteply
joelteply force-pushed the cormac/per-mind-shadow branch from 12cf0ea to 7d885c8 Compare November 7, 2026 17:21
joelteply added a commit that referenced this pull request Nov 7, 2026
…g changes, never asserted (Fable on #85)

opt_select (another mind's shadow) GGML_ASSERTed on caller input: a learn naming a loaded F16 copy of
a shadow killed the serving process (exit 134 at llama-context-opt.cpp:205), and the same asserts
sat on the first learn. Now one validator, llama_context::opt_untrainable (attached at a non-zero
scale, every A/B F32, the filter selecting at least one tensor), runs in opt_init and opt_select
before any parameter flag changes; on failure opt_failure says why and llama_opt_init returns
false. The /learn path restores her adapter set and refuses the learn by name; /train fails the run
by name. Test: learn-every-format.py --non-f32-refused (make-f16-shadow.py writes the F16 copy):
the F32 shadow learns, its F16 copy is refused naming non-F32, /health answers, and the F32 shadow
learns again. The default and --two-minds modes pass unchanged (CPU, Qwen2.5-0.5B, --no-repack).

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01FRQWzgSfo79JtnwZywHKrE
@joelteply
joelteply force-pushed the cormac/per-mind-shadow branch from 7d885c8 to f359df5 Compare November 7, 2026 18:58
joelteply and others added 4 commits November 8, 2026 16:14
… its own optimizer, /learn trains any loaded shadow, and unload releases it

A serving context trained exactly one shadow per server run (learn_lora_id):
the second mind's /learn was refused, so only one resident could ever
learn in place (continuum S4 (a), card eaeb2b87).

- llama_context keeps an optimizer per shadow (opt_minds); opt_ctx and
  opt_shadow point at the active one, so every training site reads the
  active pair as before. llama_opt_init with another adapter on a
  configured context selects it (opt_select), creating its optimizer the
  first time and keeping each shadow's moments and accumulators across
  switches. Only the active shadow's tensors carry the PARAM flag, so a
  graph never carries a parameter the active optimizer was not built
  with.
- opt_configured() is "this context is sized for training" (scheduler and
  graph): it was read as opt_ctx != nullptr, which a released active
  shadow would have falsified while other shadows' optimizers still held
  the scheduler.
- llama_opt_release forgets one shadow's optimizer; the server's unload
  releases a shadow that learned (its moments go; a snapshot is the
  durable form) instead of refusing it forever.
- learn-every-format.py --two-minds: two shadows from the same file learn
  alternately; their losses match step for step (4.3179, 4.3179, 3.5043,
  3.5043 on Qwen2.5-0.5B, IntelMac CPU), which holds only if each keeps
  its own weights and optimizer. --shadow-retirement now unloads a bound
  shadow and learns on a reloaded one.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01FRQWzgSfo79JtnwZywHKrE
…g changes, never asserted (Fable on #85)

opt_select (another mind's shadow) GGML_ASSERTed on caller input: a learn naming a loaded F16 copy of
a shadow killed the serving process (exit 134 at llama-context-opt.cpp:205), and the same asserts
sat on the first learn. Now one validator, llama_context::opt_untrainable (attached at a non-zero
scale, every A/B F32, the filter selecting at least one tensor), runs in opt_init and opt_select
before any parameter flag changes; on failure opt_failure says why and llama_opt_init returns
false. The /learn path restores her adapter set and refuses the learn by name; /train fails the run
by name. Test: learn-every-format.py --non-f32-refused (make-f16-shadow.py writes the F16 copy):
the F32 shadow learns, its F16 copy is refused naming non-F32, /health answers, and the F32 shadow
learns again. The default and --two-minds modes pass unchanged (CPU, Qwen2.5-0.5B, --no-repack).

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01FRQWzgSfo79JtnwZywHKrE
…d while a live reply holds it (Codex on #100)

Between a live reply's chunks the training flags are cleared but the active optimizer and its
accumulators still belong to that reply until its restoration finishes. opt_init (and the
opt_select it makes) and opt_release refuse by name while opt_reply is held; the server's unload
refuses the shadow a learn is training, whether or not any slot's stable set names it.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01FRQWzgSfo79JtnwZywHKrE
…ulators, AdamW moments), the per-shadow memory a serving context carries

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01FRQWzgSfo79JtnwZywHKrE
@joelteply
joelteply force-pushed the cormac/per-mind-shadow branch from f359df5 to c534ae8 Compare November 8, 2026 22:23
@joelteply
joelteply changed the base branch from 889580fc/seq-state-across-stream-counts to codex/s4-resident-microsteps November 8, 2026 22:24
…the shadow a live reply trains is refused (Codex on #85)

llama_opt_release refused every adapter while a live reply was held, and the server ignored the
refusal: unloading idle mind B during mind A's learn freed B's tensors while B's optimizer stayed in
opt_minds (Codex's s4-unload-other-shadow, Windows). An idle mind's optimizer is its own buffers
(ggml_opt_free touches no shared scheduler), so release now frees it during another mind's reply and
refuses only the shadow that reply trains; it returns true when no optimizer references the adapter
(released, or none), and the server refuses the unload on false instead of ignoring it.

Measured (IntelMac CPU, Qwen2.5-0.5B Q4_K_M, -np 2 --no-repack): learn-unload-during-learn.py:
B trained, A's 681-label reply in flight, unload B 200 (retired), unload A 503 (restoration
pending), A completes (loss 0.1268, 41.8 s CPU), reloaded B learns again (loss 4.2457).

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01FRQWzgSfo79JtnwZywHKrE
@joelteply

Copy link
Copy Markdown
Author

Closing (Cormac, Joel 2026-11-11 'get all your stuff merged'): this head is already an ancestor of codex/s4-per-mind-integration, so its change has landed.

@joelteply joelteply closed this Nov 11, 2026
joelteply added a commit that referenced this pull request Nov 11, 2026
…shadow unloads while another mind learns

c5551ed made ggml_opt_free assert that the SHARED scheduler had no pending graph. A serving
context keeps one optimizer per shadow on one scheduler, and llama_context::opt_release frees an
idle shadow's optimizer while another shadow's step is in flight (#85's contract: "ggml_opt_free
touches no shared scheduler"). That assert then aborted llama-server whenever the unload landed
mid-step (fork #106/#107 reproducer, learn-unload-during-learn.py, 2 of 3 runs on CPU).

ggml_opt_free releases only this context's own buffers and metadata. The check is now
context-local: ggml_backend_sched_graph_pending_for(sched, graph), true only when the scheduler is
part-way through THAT graph (graph_step already keys its continuation on the caller's graph
pointer). Freeing a context whose own graph is in flight still aborts. ggml_opt_reset and
ggml_opt_prepare_alloc keep the scheduler-wide guard: reset zeroes the last graph's gradients,
which in a dynamic graph can sit in the shared compute buffer another mind now uses, and
prepare_alloc allocates onto the scheduler.

Tests (test-opt-dynamic-accum, built with -fsanitize=address on CPU, IntelMac):
- run_frees_idle_mind_while_another_steps: B learns a step, A stops one node into its step
  (scheduler pending), B is freed, A completes with exactly the update it makes alone. PASS.
- run_free_own_pending_graph_aborts: freeing a context whose own graph is in flight aborts
  (observed in a forked child; skipped with a message where fork() is unavailable). PASS.
- the existing suite: PASS.
- Mutation: restoring the scheduler-wide check aborts the first test at the same assert the
  engine hit (exit 134).
End to end: the reproducer passes 10/10 on the fixed CPU server, but it is timing-dependent (it
aborts only if the unload lands mid-graph; 0/6 on the unfixed engine in a later run), so the unit
test is the proof.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01FRQWzgSfo79JtnwZywHKrE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant