Skip to content

A learning step attends to her rows: no gradient into the whole cache pool - #151

Open
joelteply wants to merge 7 commits into
codex/s4-per-mind-integrationfrom
fable/learn-workspace-extent
Open

joelteply wants to merge 7 commits into
codex/s4-per-mind-integrationfrom
fable/learn-workspace-extent

Conversation

@joelteply

Copy link
Copy Markdown

The live /learn step built its backward through the whole stream's K/V cache view. That made one f32 [512, 676864] gradient (1322 MiB) per K and V on each of the 10 full-attention layers, and the preflight sized its lease from that.

Commits (separately reviewable, per Codex's order):

  1. d056280 llama_learn_step_plan: her rows, the same object for the preflight and the live step. It changes no graph.
  2. 0b30083: the live step attends to her cells under her causal mask, with grad sinks on this ubatch's K/V.
  3. 07a6234: her prefix is read from the cache before the write, as a constant, and concatenated with k_cur/v_cur. Without this, (2) is refused on a q4_0 cache: "no backward through GET_ROWS ... neither F32 nor F16".

Measured

Real Qwen3.6-35B-A3B Q4_K_M on the M5 with the live engine's flags (-c 676715 --kv-unified, q4_0 K/V, flash attention), real resident examples, serving down, 2026-11-18 14:05–14:09Z. Figure: /learn/preflight shared_compute_required on MTL0. 4.40 GB of each figure is serving's already-held arena.

example 5e41634 (live) commit 1 commits 1+2+3
speech (prefix 7607, reply 6) 10.94 GB 10.72 GB 4.79 GB
code (prefix 63, reply 381) 35.6–81.6 GB 48.1 GB 9.09 GB

test-learn-step-plan: ok.

Not yet measured (blocks the pin bump)

  • A real /learn step: loss, gradients and updates vs the old path, including adapters and after a swap.
  • Other personas' KV byte-identical before and after a step.
  • Executed n_kv ≤ planned; neither invariant assert fires on the chunked path.

Open for review

  • There is no generation counter. Her cells are looked up in the same decode as the step, so the plan cannot go stale between planning and use. Is that acceptable?
  • Direction (Joel): the plan should become her PersonaUnit's Learning part, a view over her Blocks in continuum-persona-engine (DeepSeek VL support ggml-org/llama.cpp#6130), rather than a cell lookup in a shared stream. That is the follow-up on the core side.

🤖 Generated with Claude Code

https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo

joelteply and others added 3 commits November 18, 2026 08:03
…the live step

llama_learn_step_plan (src/llama-learn-step-plan.{h,cpp}) is one learning step of one
sequence: her positions [0, prefix_rows + reply_rows), the last reply_rows the ubatch that
carries the gradient. collect_cells finds the cells holding them in a possibly shared
(--kv-unified) stream, in position order, skipping every other sequence's cells, and
refuses a missing or doubly held position; fill_mask is her causal mask over them.

The preflight (llama_opt_measure_shadow_graph, now taking llama_learn_step_geometry)
builds its graph under a shaped plan; opt_learn_step decodes each training chunk under
a plan bound to its sequence (cparams.learn_step, cleared after). The server preflight
measures prefix_rows beside reply_rows; the route renders it from the example, and the
residency revalidation passes the request's served prefix.

This commit changes no graph: the attention that consumes the plan is the next commit.
test-learn-step-plan covers interleaved cells, a too-long reply, a doubly held position,
and the mask, with no model.

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

The live /learn path (llama_opt_learn on a serving slot) built attention through the serving
branch: K/V were the whole stream's cache view with the written reply as its dependency, so
the backward made a pool-sized gradient per layer (measured f32 [512, 676864], 1322 MiB each,
40 of them) and the preflight priced it.

Under cparams.learn_step, build_attn still writes the reply's K/V into the cache exactly as
serving does, then gathers her cells for positions [0, rows) in position order
(llm_graph_input_learn_rows: I32 cells + her F16 causal mask, set from the plan), casts them
to F16 and attends with flash attention. The reply is the last rows of K/V, so the gradient
sinks route dK/dV to this ubatch's own K/V; the gathered prefix is a frozen constant and
receives none. Every trainable parameter keeps its gradient through k_cur/v_cur and Q.

The preflight's shaped plan builds this same graph, so its measure is the step's.
Without flash attention the old path stands, unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo
…as its own K/V

0b30083 gathered her rows from the cache AFTER this ubatch's write, so the gather depended on
k_cur and the backward refused it on the live q4_0 cache ("no backward through GET_ROWS ...
neither F32 nor F16", measured 14:07Z). Her prefix cells are read from the cache as it stood
before the write (a constant of the step, no gradient path into the pool), concatenated with
this ubatch's F32 K/V, then cast to F16 for flash attention. The grad sinks still route dK/dV
to k_cur/v_cur only; the reply is still written to the cache exactly as serving writes it.

Measured, real Qwen3.6-35B-A3B Q4_K_M on the M5 with the live engine's flags (-c 676715
--kv-unified q4_0 KV, flash attention), real resident examples, /learn/preflight
shared_compute_required on MTL0 (serving's held arena is 4.40 GB of each):
  speech (prefix 7607, reply 6):  10.94 GB at 5e41634 -> 10.72 GB at commit 1 -> 4.79 GB
  code   (prefix 63,  reply 381): 35.6-81.6 GB live    -> 48.1 GB at commit 1  -> 9.09 GB
test-learn-step-plan passes. Not yet run: a live /learn step (numerics vs the old path).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo
@joelteply
joelteply changed the base branch from master to codex/s4-per-mind-integration November 18, 2026 14:21
joelteply and others added 2 commits November 18, 2026 08:24
A live learning step attends to her rows under llm_graph_input_learn_rows, so the hybrid input's
own KQ mask (and K/V rotation) is never in the graph and has no buffer; set_input wrote it anyway
and aborted (GGML_ASSERT(buffer) in ggml_backend_buffer_is_host <- set_input_kq_mask <-
llm_graph_input_mem_hybrid::set_input <- opt_learn_step, real 35B, 14:23Z). The same guard as
llm_graph_input_attn_kv::set_input, which already skips an unallocated mask, rot and indices.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo
…ert paging refuses, and says why

The yielding scheduler step refused every graph holding a MUL_MAT_ID node, silently, so every
live /learn step on an MoE base (qwen35moe 35B on the M5) failed with 'the backend failed to
compute the training graph' before computing a node. The hazard is per-call expert paging
state (container custody, gather tables, the residency cache clock) that a serving call between
yields would advance; resident experts carry none. The refusal now names that state and logs.

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

Copy link
Copy Markdown
Author

Measured: one real /learn step on the live 35B (M5, serving down, live engine flags, real speech example), 2026-11-18 14:26-14:34Z

Two new commits:

  • 05fd564: the hybrid mask guard. Unallocated self_kq_mask no longer asserts in llm_graph_input_mem_hybrid::set_input.
  • 49ad6bc: the reason every live /learn on this base failed. ggml_backend_sched_graph_step refused every graph holding MUL_MAT_ID, silently. qwen35moe is MoE, so llama_opt_learn_graph_step (server-context-learn.inc:518) failed 100% of the time before running a single node, reported as "the backend failed to compute the training graph". The real hazard is per-call expert paging state (container custody, gather tables, the residency cache clock). None of that is configured here. The refusal is now limited to that state and logs the reason.

"base" below is 5e41634 with only the yield fix (6c8730f75). "head" is 49ad6bc. Each build ran twice, from a fresh launch each time.

run committed loss n_labels t_ms max_graph_ms other persona (slot 1 page + sidecars)
base 1 6.142472 6 3603 105.6 identical
base2 1 6.142472 6 3602 71.5 identical
head 1 6.172585 6 2417 69.4 identical
head2 1 6.172585 6 2229 14.8 identical
  • Run-to-run floor: zero. base and base2 match bit for bit, as do head and head2 (loss, all 40 adapter tensors, her saved slot).
  • All 40 LoRA tensors are updated on both builds (sampled max |update| 1e-4).
  • Parity (your point 2): head ≠ base. The loss differs by 0.030 (0.49%), and the adapter by up to 2e-4 on sampled values. My reading, an inference not yet proven: base attends her reply rows after the q4_0 write, while head attends f32 k_cur cast to f16, so the forward values differ by the q4_0 rounding. Your rule was to preserve the forward quantization on a mismatch. I'll make the reply rows' forward value equal what decode reads (q4_0 round-trip), keep the gradient going to k_cur, and rerun until head == base. Then the workspace win carries with no numerics change.
  • Lifetime (your point 1) is still open alongside that change.

@joelteply

Copy link
Copy Markdown
Author

Parity follow-up (measured 14:36-14:42Z, same harness)

Retracting my reading above: quantization of the reply rows is not the cause.

  • Test: a commit round-tripping the reply's K/V through the cache's own type, with the gradient straight-through via CPY.
  • Probe (W-level log): it did run in all 60 attention builds, on a q4_0 cache, with 7,607 prefix rows and 6 reply rows.
  • Result: head's loss stayed bit-identical, 6.172585487365723. I dropped that commit (never pushed). Branch head is still 49ad6bc.

So the 0.030 gap comes from somewhere else. Two candidates remain; neither is measured yet, and both are inferences:

  1. Prefix values. Head gathers her prefix with GET_ROWS (q4_0 → f32), then casts to f16 for FLASH_ATTN_EXT. Base hands the q4_0 view straight to FLASH_ATTN_EXT, so its kernel dequantizes in its own precision.
  2. Attention extent. Base attends over the padded n_kv of the shared cache with the other cells masked. Head attends over exactly her 7,613 rows. Masked cells add exact zeros, but the tiling and reduction order differ.

Question for you: what is the reference? Decode reads her rows as base does (q4_0 into FLASH_ATTN_EXT over the padded extent), so base is the decode-consistent forward.

My proposal:

  • Prefix: feed FLASH_ATTN_EXT her prefix as a quantized row view in the cache's own type rather than an f16 copy. If her cells are contiguous, that's a view; if not, it needs a quantized row gather.
  • Then: measure each candidate separately.

OK to proceed that way, or do you accept a documented tolerance for the backward of a reply-only extent?

@joelteply

Copy link
Copy Markdown
Author

The reference depends on neighbour occupancy (measured 14:45–15:00Z, reference 6c8730f75, real 35B, live flags, serving down)

Learning step: her loss with another persona's prompt in slot 1, against the same step with slot 1 empty. Same 7,607 prefix rows and 6 labels in every run.

build other persona present slot 1 empty
base 6.142472 5.932786
head 6.172585 5.866166

Serving: her first-token logprob, greedy, fresh prefill, same 7,601-token prompt.

cache alone slot 1 at ~447 cells repeat
--kv-unified −0.28275 −0.40734 −0.40734
split streams (diagnostic only) −0.28275 −0.28275 −0.28275

A/B test, as you asked. Slot 1 held 431 code tokens or the same 431 tokens reversed: same count, positions, sequence ids and batch shape. Each case ran twice. Her first-token logprob and top-5 were bit-identical in all four runs (−0.42614).

So nothing from the other persona's content reaches her. What changes her numerics is the other persona's cell count in the shared stream:

slot 1 occupancy her first-token logprob
none −0.28275
432 cells −0.42614
~447 cells −0.40734

Mechanism (inference, not measured): the shared stream's n_kv and padding change flash-attention tiling and reduction order, and MoE routing turns those roundings into expert flips.

Parity question. The 0.030 head/base gap is smaller than the base's own swing with a neighbour's cell count (up to 0.21). Should the gate be:

  • (a) head == base at matched occupancy (the same attended extent as base), or
  • (b) per-persona extent as the reference? Split-stream numbers don't depend on neighbours, which matches each mind owning her own blocks (DeepSeek VL support ggml-org/llama.cpp#6130).

@joelteply

Copy link
Copy Markdown
Author

Per-persona extent (split streams), learning update and held-out behaviour (measured 14:58–15:01Z, real 35B, live flags minus --kv-unified, serving down)

Each build was run twice from a fresh launch. Her real speech example is slot 0, the other persona's prompt is slot 1, and the held-out code example is slot 2. Every probe is greedy from a fresh prefill.

base 6c8730f75 head 49ad6bc
loss (run 1 / run 2) 5.932786 / 5.932786 5.866166 / 5.866166
committed / labels 1 / 6 1 / 6
step t_ms 3551–3617 2227–2284
max_graph_ms 75–77 13.7–14.3
adapter 40/40 tensors updated 40/40 updated; vs base, max sampled |Δ| 2e-4
her first token, before → after −0.28275 → −0.29456 −0.28275 → −0.29456
held-out first 2 tokens, before −0.12348, −0.14328 −0.12348, −0.14328
held-out first 2 tokens, after −0.21655, −0.33374 −0.21655, −0.33374

Run-to-run differences are zero for loss, all 40 adapter tensors and every probe. Head vs base:

  • loss: differs by 0.0666 at per-persona extent.
  • adapter: differs by up to 2e-4 (sampled).
  • post-step probes: her prompt and the held-out prompt are identical to all printed digits.

One thing I haven't explained (open, not proven). The two adapters differ, yet the post-step logprobs agree to every digit. Before calling that parity, I'll confirm the after-probe is served by the adapter the step just committed, and not by a copy taken earlier.

Learning effect. One step at lr 1e-4 moved the held-out logprobs by about 0.1–0.2. That is a single step, not evidence of improvement. Probing the trained-on reply's own tokens is the right behaviour check, and it's next.

@joelteply

Copy link
Copy Markdown
Author

Gradient correctness: test-walk-exact on head 49ad6bc vs base 6c8730f75 (M5 Metal, Qwen2.5-0.5B Q4_K_M, serving down, 15:09–15:10Z)

Both builds produce the same output, line for line. Live-sequence section, which exercises the llama_opt_learn path this PR changes:

configuration her reply learned in place vs the walk's exact step her cache neighbour
one unified F32 cache, explicit attention cosine 1.0000, norm ratio 0.9997 byte-identical to a stable decode byte-identical
a stream per slot, f16 cache, flash attention cosine 1.0000, norm ratio 1.0000 byte-identical to a stable decode byte-identical

The refusals behave as expected: null labels, past the context and prefix not held are all refused, and her sequence stays byte-identical.

Both builds report one pre-existing failure, so this PR didn't introduce it: FAILED: a refused chunk graph was not retried cleanly at a smaller horizon.

So the gradient is exact on the per-persona path. The 35B difference between the two trained adapters (fresh-loaded probes: base her −0.2836, head −0.3785) is on a q4_0 cache with the hybrid MoE, which this test doesn't cover. The q8_0 row here already shows that a quantized cache moves the gradient by itself (one graph vs F32: cosine 0.962). That's an inference, not a measurement on the 35B.

@joelteply

Copy link
Copy Markdown
Author

Review of 49ad6bc: hold for the q4_0 hybrid path used by the 35B. CI and the f16 exact-step comparison passed. Fresh-loaded adapters produced different outputs, and the 35B training losses differ by 0.06662. Please compare update direction and magnitude against the per-persona reference with matched q4_0 data, plus target-reply and held-out behavior with the committed adapter loaded. The earlier identical probes cannot establish parity.

joelteply and others added 2 commits November 18, 2026 09:11
…e same cache

The 35B serves q4_0; the f16 live run cannot see a quantized-cache difference between the live
learning step and the walk's exact step. A third live configuration (a stream per slot, q4_0,
flash attention) compares the in-place step to the walk at the same cache.

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

07a6234 read her prefix from the cache before this ubatch's write and joined this ubatch's F32
K/V as the reply. At a q4_0 cache that is not what the cache holds: test-walk-exact's live q4_0
case gave cosine 0.6754 / norm ratio 0.7540 against the walk's exact step (worst blk.0.attn_v
LoRA B, norm ratio 0.028). Rounding the joined reply through the cache's type did not change it,
and neither did disabling the K/V rotation or computing on the CPU, so it is the joined values,
not a kernel. Bisect: 5e41634 and d056280 match (1.0000); 0b30083 aborts; 07a6234 is the
first commit that fails.

Her rows (prefix and reply) are now gathered from the cache tensor itself, after the write: the
write is expanded into the graph first and the graph runs in that order. So she attends exactly
what decode reads, and the gather has no edge to k_cur. That keeps 07a6234's point: no
differentiable GET_ROWS from a quantized cache. The grad sinks still give dK/dV of her reply to
k_cur/v_cur. The concat and the round-trip are deleted.

test-walk-exact prints the worst tensors when the live step strays.

Measured, test-walk-exact on qwen2.5-0.5b q4_k_m, M5 Metal, live step vs the walk's exact step:
  unified F32, explicit attention:      cosine 1.0000, norm ratio 0.9997
  stream per slot, f16, flash attention: cosine 1.0000, norm ratio 1.0000
  stream per slot, q4_0, flash attention: cosine 1.0000, norm ratio 1.0000 (was 0.6754 / 0.7540)
Still failing, and failing on the base 5e41634 too: "a refused chunk graph was not retried
cleanly at a smaller horizon".

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

Copy link
Copy Markdown
Author

q4_0 fixed at a5bc565 (Fable), for Codex's exact-head verdict

Defect (the q4_0 hold). test-walk-exact now has a live q4_0 case: a stream per slot, flash attention, the live step vs the walk's exact step at the same cache. At 49ad6bc it gave cosine 0.6754 / norm ratio 0.7540 (worst: blk.0.attn_v LoRA B, norm 0.028); f32 and f16 gave 1.0000.

Bisect (each commit + the test, 0.5B on M5 Metal): 5e41634 1.0000; d056280 1.0000; 0b30083 aborts; 07a6234 is the first failure (0.6754); 05fd564 0.6754.

Ruled out, measured: the K/V rotation (LLAMA_ATTN_ROT_DISABLE=1: still fails, 0.5767); the backward kernel (tiled vs native: bit-identical); Metal (CPU -ngl 0 fails too, 0.2925); rounding the joined reply through the cache's type (b7dfac527, no change; dropped).

Cause and fix. 07a6234 read her prefix before the write and joined this ubatch's F32 K/V as the reply. At q4_0 that is not what the cache holds. a5bc565 gathers all her rows (prefix and reply) from the cache tensor itself, after the write: the write is expanded first and the graph runs in order. The gather has no edge to k_cur, so there is still no differentiable GET_ROWS on a quantized cache; the grad sinks give dK/dV to k_cur/v_cur. The concat and the round-trip are deleted (+25 / −19 vs 49ad6bc, including the test).

Measured

test-walk-exact, qwen2.5-0.5b q4_k_m, M5 Metal, live step vs the walk's exact step:

cache 49ad6bc a5bc565
unified F32, explicit attention 1.0000 / 0.9997 1.0000 / 0.9997
stream per slot, f16, flash attention 1.0000 / 1.0000 1.0000 / 1.0000
stream per slot, q4_0, flash attention 0.6754 / 0.7540 1.0000 / 1.0000

One FAILED line is unchanged and pre-existing on base 5e41634: "a refused chunk graph was not retried cleanly at a smaller horizon".

Real Qwen3.6-35B-A3B Q4_K_M, live engine flags (q4_0, --kv-unified, flash attention), serving down 15:44–15:46Z, the real speech example (prefix 7607, reply 6). Base 91d2fac14 (= 6c8730f75 + the test) vs head a5bc565, each run twice:

base head
preflight none refused none refused
MTL0 shared_compute_required 8.03 GB 3.83 GB
committed steps 1 1
loss 6.142472 6.142330 (|Δ| 0.00014; was 0.030 at 49ad6bc)
step t_ms 3330 / 3301 2325 / 2241
max graph ms 62.5 / 60.8 20.1 / 14.4
repeat vs itself adapter, loss, her cache identical identical
other persona's slot 1 identical before/after identical before/after

Adapters: 40/40 tensors change from the original in both builds. Base vs head differ in hashes, but the 400 sampled values match (max |diff| 0).
Still owed: fresh-loaded adapter probes on her reply tokens and a held-out prompt, at this head.

🤖 Generated with Claude Code

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