Skip to content

One decode serves minds wearing different adapters, each adapter on her own rows - #172

Open
joelteply wants to merge 4 commits into
fable/output-ringfrom
fable/seq-adapters
Open

joelteply wants to merge 4 commits into
fable/output-ringfrom
fable/seq-adapters

Conversation

@joelteply

Copy link
Copy Markdown

Stacked on #171. Implements the per-sequence adapters design Codex approved (continuum THE-LANE-ON-SAGE.md, open question 1, "Across genes"; 75d1726b9a), with his five conditions:

Condition (Codex, 07:53Z) Where
Bind an explicit set (including empty/base) per stream; clear on release/rebind llama_set_seq_adapters_lora (empty = base), llama_clear_seq_adapters_lora
A token shared by sequences wearing different sets needs separate evaluations; never pick the first seq decode refuses it (seq_adapters_refuse), nothing decoded
Preserve caller logits indices after grouping rows never move: consecutive groups are views, interleaved ones gather and set_rows back by index
Key baked-in scales, adapter identity/version, interleaved row mapping llama_adapter_seq_groups (sets by value with scales, rows, the model's lora_frees) is compared in allow_reuse
Rebind at another scale on a warm graph; empty/base after an adapted owner test phases 2 and 3

Layout (kept as the fork's other additions)

  • src/llama-adapter-seq.{h,cpp}: the data model (sets, groups, the refusal), the context's verbs, the graph (seq_lora_mm, seq_lora_mm_id, seq_lora_embd over one seq_lora_apply), the API.
  • Upstream code gains one-line hooks: before each of the three LoRA loops (build_lora_mm, build_lora_mm_id, the token embedding), in graph_params, decode and graph_max_nodes; plus the seq_groups field and its compare in llm_graph_params.

Measured (M5, 2026-11-28, test-seq-adapters)

Four minds, own sets (a; b; a + b·0.75; base), against upstream stepping one mind at a time under the context's set; six phases × six steps; both KV layouts.

  • qwen35-0.8b, CPU: bit-exact in every phase (smallest adapter effect 1.4–2.0 in logits).
  • qwen35-0.8b, Metal: ≤ 1.8e-3, 1.0–1.6× the kernels' own difference between one row and four; 0 argmax flips.
  • random qwen35moe with every expert weight adapted (exercises build_lora_mm_id): exact on CPU, ≤ 1.2e-7 on Metal.
  • graphs reused after each phase's first step: equal to upstream's batch with no adapter, every phase.
  • interleaved rows (phase 4): gathered and counted 6 of 6; every other phase 0.
  • test-graph-slots: unchanged (the routing-capture regression passes).

Found on the way

  • Upstream adds a context set's adapters in its unordered_map order. On the hybrid, one ulp at a step moved the next step's logits by 0.19 on the CPU, so the reference is taken in both orders. Ours is ordered by address.
  • With kv_unified off (the Core's layout), split_equal takes a ubatch's sequences only in consecutive increasing ids. A batch out of that order, or over non-adjacent streams, is split into several ubatches and reuses no graph (phases 1 and 5; upstream's path too). The Core's driver has to submit in stream order and bind gene-mates into adjacent streams; continuum side to follow.

🤖 Generated with Claude Code

https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo

…er own rows

A context applied ONE adapter set to every row of a ubatch, so minds bound in streams of one context shared a step
only while they wore the same adapters. On the M5 (2026-11-28, Qwen3.6-35B-A3B) every resident wore her own, and a
turn chose a batch 85 times and batched nothing.

A sequence now names its own set (llama_set_seq_adapters_lora), once, when its owner binds a mind to it; an empty
set is the base. A sequence that names none wears the context's set, and a context that never names one builds
upstream's graph. At graph build the ubatch's rows are grouped by the set they wear and each adapter's delta is
added to its group's rows only: consecutive rows are a view, interleaved rows are gathered and written back by
index (correct, slower, counted by llama_seq_adapters_gathered). Rows never move, so output indices hold; base rows
are in no group. The groups (sets, scales, rows, the model's adapter frees) are part of the graph's identity, so a
set rebound under another scale rebuilds the graph and the same step again reuses it. Decode refuses a token shared
by sequences wearing different sets, and a set named before an adapter was freed.

Organized as the fork's other additions: the data model and the graph in llama-adapter-seq.{h,cpp}; upstream code
gains one-line hooks (the three LoRA sites, graph_params, decode, graph_max_nodes) and two lines in llm_graph_params.

test-seq-adapters, against upstream stepping one mind at a time under the context's set (Codex's cases: rows swapped
with sequence ids kept, a rebind at another scale on a warm graph, the base after an adapted owner; interleaved
groups; the shared-token refusal), on both KV layouts:
- qwen35-0.8b CPU: bit-exact in every phase (adapter effect 1.4-2.0); Metal: within 1.0-1.6x the kernels' own
  difference between one row and four (<=1.8e-3), 0 argmax flips of 24 per phase
- a random qwen35moe with every expert weight adapted (build_lora_mm_id): exact on CPU, <=1.2e-7 on Metal
- graph reuse after each phase's first step equal to upstream's batch with no adapter, every phase
Found on the way: upstream adds a context set's adapters in its hash map's order, and on a hybrid one ulp at a step
moved the next step's logits by 0.19 on the CPU, so the test takes upstream in both orders. And with kv_unified off
(the Core's layout) split_equal takes a ubatch's sequences only in consecutive increasing ids: a batch out of that
order is split into several ubatches and reuses no graph (phases 1 and 5, upstream's path too).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo
…own adapter refuses it

Codex's two blockers on #172:

- Sparse output rows. Past inp_out_ids a tensor holds only the rows whose logits were asked for, so the token-space
  rows a group knew were the wrong rows there (or out of range: the CPU's get_rows aborted on the new test's phase 6
  before this). A group now knows its rows in both spaces, among the ubatch's tokens and among its outputs (the
  output rank, as inp_out_ids lists them in row order), and each site reads its tensor's row count exactly (the
  expert ids, the token ids, a projection input's outermost dimension) to pick the space; a count that is neither
  aborts rather than adding a delta to the wrong rows.
- Model-wide free invalidation. Any adapter freed anywhere on the model refused every named set and rebuilt every
  graph (the new test's unrelated free mid-phase refused the next decode before this). An adapter is now named by its
  address and a serial unique per load (llama_adapter_lora::serial, one field in upstream's struct): a set is refused
  only when one of its own adapters is gone or its address holds another load, and the graph's identity compares the
  serials, so a free of anything else changes nothing.

test-seq-adapters gains two phases of two tokens per mind with logits at the end only (outputs a subset;
interleaved, then consecutive), an adapter nobody wears loaded and freed mid-phase (reuse must still equal
upstream's), and the refusal of a set whose own adapter was freed. Both old failures reproduced against 0d2e3d9's
library with this test.

## Measured (the M5, 2026-11-28)
- qwen35-0.8b CPU: bit-exact in all 8 phases on both KV layouts (adapter effect 1.4-2.0), both refusals hold.
- qwen35-0.8b Metal: <= 3.2e-3, 1.0-1.6x the kernels' own one-row-against-four difference, 0 argmax flips.
- random qwen35moe, every expert adapted: OK on CPU and Metal.
- graphs reused after each phase's first step equal upstream's batch with no adapter, every phase, including the
  phase with the unrelated free. test-graph-slots unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo
@joelteply

Copy link
Copy Markdown
Author

Codex's two blockers fixed at 97426c1: (1) past inp_out_ids a group adds to its own output rows (each group holds its rows among tokens and among outputs; each site reads its tensor's exact row count; a count that is neither aborts); (2) adapters carry a per-load serial, so only a free of a set's own adapter refuses it and graph identity no longer changes on an unrelated free. Both reproduced against 0d2e3d9 with the extended test-seq-adapters (two-token phases with logits at the end; an unrelated adapter freed mid-phase; a named adapter freed). Now: qwen35-0.8b CPU bit-exact in all 8 phases on both KV layouts, Metal ≤ 3.2e-3 (1.0–1.6× the kernels' own difference), random qwen35moe with every expert adapted OK on both, graph reuse equal to upstream's in every phase.

joelteply and others added 2 commits November 28, 2026 08:08
…t follows the genes worn, not the genome loaded

- refuses() checked every sequence's named set, so a mind parked with a gene unloaded refused every other mind's
  step (Codex, #172 re-review). It now checks only the sequences the batch decodes.
- seq_adapters_max_nodes counted every adapter loaded on the model times n_seq_max. A dynamic genome holds many
  genes loaded and a few worn per mind, so the budget grew with the genome for nothing. It now counts the distinct
  named sets' own adapters, each worn set once, skipping an adapter freed since it was named (never read).

test-seq-adapters: with a parked mind's gene freed, another mind decodes, she is refused, and she decodes once named
again; each arm's compute buffers are read (llama_get_memory_breakdown), and the batch is run again with 16 more
genes loaded and worn by no one.

## Measured (the M5, 2026-11-28 14:08Z, qwen35-0.8b, n_ubatch 1024, Metal)
- compute buffers: the batch under per-mind genes 1006.09 MiB, upstream's batch with no adapter 1006.09, one mind at
  a time under the context's set 1006.09 (kv_unified off); 1018.22 for all three with it on. Byte-identical: the
  groups add no buffer beyond upstream's worst-case reserve.
- the genome: 2 genes loaded 992.05 MiB, 18 loaded (2 worn) 992.05 MiB.
- every phase as before on 0.8b and the random qwen35moe, CPU and Metal: OK.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo
…borting at its bound

The M5 (2026-11-28 14:08Z, 86af36cf71 on 97426c1) restart-looped: 82 aborts at llama-lived-routing.cpp:120,
GGML_ASSERT(graph.n_tokens <= buf->ne[1] / lived->n_layer), on decode -> sched_reserve -> graph_reserve, beside a
lesson's fresh shadow. The capture was sized once, for the ubatch (rounded to the streams), but a context builds
graphs past it: a lesson decodes a whole reply in one graph and raises n_ubatch for it, and a scheduler re-reserved
then (a stream naming her genes can raise the node budget, #172) reserves at that size.

One owner sizes the capture: routing_capture_alloc(rows) (set_routing_capture's allocation, moved), and
routing_capture_fit(n_tokens), called by graph_reserve and process_ubatch before a graph is built (one line each).
A graph with more rows than the capture holds grows it to them (rounded to the streams), after the queued reads
settle, and drops the kept graphs that view the old buffer. A capture the device has no room to grow turns off by
name (logged): serving goes on, and the lessons that need it are refused as when capture is unavailable.

test-graph-slots: after the three-stream reserve, a graph of three ubatches is reserved with capture on and the
context decodes again. Against the old library: the M5's assert, exactly. Now: "the routing capture grew from 2049
to 6144 rows a graph", exit 0. test-seq-adapters now runs every arm with capture on, as serving does.

## Measured (the M5, 2026-11-28 14:44Z)
test-graph-slots and test-seq-adapters OK on qwen35-0.8b and the random qwen35moe, CPU and Metal.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant