Skip to content

kv: sliding rings for laguna, gpt_oss and gemma4 - #1155

Merged
ddalcu merged 6 commits into
ddalcu:mainfrom
dmumber:sliding-ring
Nov 18, 2026
Merged

ddalcu merged 6 commits into
ddalcu:mainfrom
dmumber:sliding-ring

Conversation

@dmumber

@dmumber dmumber commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

What changed / why

The sliding-window layers of laguna (with flint and kolibri1, which share its parse), gpt_oss and gemma4 cached every token although attention only reads the last window; only MiMo kept a ring (slidingRing() was isMimo()). These plugins now declare CacheFacts.sliding_ring (window + 2048, sdk/config/sliding_ring.zig) as MiMo does, their attention appends through kv.appendSliding, and ModelConfig.slidingRing reads the declared fact instead of the arch name.

KV billed per token (kvBytesPerToken, pinned by the sliding layers keep a ring test in admission/bills_test.zig against the released configs; arithmetic from the bill, not a measured footprint): Laguna XS 160 → 40 KiB, gpt-oss-20b 48 → 24 KiB, Gemma 4 26B-A4B 220 → 20 KiB, Gemma 4 E4B 56 → 16 KiB.

One commit each for what comes with it:

  • A ring drops whole 32-row blocks, so the hd-256 band kernel tiles a whole-buffer view as it tiles the full buffer. Each plugin's proof compares a ring that dropped rows against the full buffer, logits bit-equal.
  • A request's ring is billed by the rows it can reach (prompt plus generation) instead of keep + one chunk whatever its length. A plugin whose declared ring is narrower than one of its sliding windows is declined by name at resolve.
  • SSD tier: a session whose ring had dropped a row was skipped whole. A commit now writes the full layers from position 0 and the ring layers from the first chunk the ring still holds whole (meta v11, ring.from); an older binary rejects v11.

Two commits after the first review:

  • MiMo appends through kv.appendSliding like the other three. Its own copy rang only with keep > 0, so at head 256 a prefill appended every chunk in full until the first decode row; the released pack is head 192 and takes the same arm under either gate.
  • Where no band kernel serves a ring's whole-buffer view (MiMo and gpt_oss on a CPU stream, Laguna at head 256), the composed band mask is sized from the view. It was sized for offset + rows keys, and the forward aborted once the ring had dropped rows. Each of the three has a head-256 proof; the released packs are head 192, 64 and 128 and never took that arm.

How I verified it (chip, macOS, zig build test N/0, swift test if app, the integration script)

M5 Ultra, 256 GB, macOS 27.2, at 1612223a on main 9191fbdf: zig build test --summary all -Dtest-shards=2 29/29 steps, 5179/5586 passed, 407 skipped, 0 failed; zig build check and python3 scripts/lint.py --strict clean.

Live measurement, Laguna XS

M5 Ultra, 256 GB, macOS 27.2. Laguna-XS-2.1-NVFP4-mlx, main 9191fbdf against this branch at 55292ef3 (before the two review commits; Laguna XS is head 128 and does not take the arm they change), both ReleaseFast with the same staged mlx. One fresh server and one request per cell: --ctx-size as listed, --max-concurrent 1, --prefix-cache-mem 1MB, no --kv-quant, no SSD tier, prompt of ctx − 512 tokens by /tokenize, max_tokens 256, temperature 0, thinking off. Peak phys_footprint of the server process over the request, sampled every 0.5 s (proc_pid_rusage), GB = 10^9 bytes; loaded and idle both builds sit at 23.0 GB.

ctx prompt tokens main, peak GB branch, peak GB drop GB (lowest main − highest branch) bill GB
65536 65079 47.25, 47.61 32.19, 32.19 15.07 7.74
131072 130606 67.80, 61.24, 66.77 37.62, 37.65 23.59 15.79

Output: 10 of 10 replies byte-equal between main and the branch with 256 completion tokens each (prompts of 1k, 8k, 32k, 64k and 75k tokens on one server at ctx 131072, plus the memory cells); main against itself on the 8k, 64k and 75k prompts was equal first. No prefix-cache hit in any request (cached_tokens 0).

Rule, fixed before the first cell: a drop of at least 12 GB at 131072 and 6 GB at 65536, and every output equal; passed.

The drop is larger than the bill because the peak is more than the KV rows: main grows 44.8 GB over its loaded footprint at 131072 where the bill says 21.5 GB, the branch 14.6 GB where it says 5.7 GB. On main the sampled peak misses a short spike (the kernel's lifetime maximum reads 69.07 GB in all three runs), so the drop is understated if anything. Not measured with this pack: a prefix-cache hit on a ring, the SSD tier, --kv-quant.

Live measurement, Gemma 4 E4B

M5 Ultra, 256 GB, macOS 27.2. gemma-4-e4b-it-4bit (mlx-community), main 9191fbdf against this branch at 1612223a, both ReleaseFast with the same staged mlx. The same way as the Laguna cells: one fresh server and one request per cell, --ctx-size as listed, --max-concurrent 1, --prefix-cache-mem 1MB, no --kv-quant, no SSD tier, prompt of ctx − 512 tokens by /tokenize, max_tokens 256, temperature 0, thinking off. Peak phys_footprint of the server process over the request, sampled every 0.5 s (proc_pid_rusage), GB = 10^9 bytes; loaded and idle both builds sit at 5.2 GB.

ctx prompt tokens main, peak GB branch, peak GB drop GB (lowest main − highest branch) bill GB
65536 65055 15.17, 15.17 12.72, 12.63 2.45 2.58
131072 130568 20.48, 20.48 13.61, 13.68 6.80 5.26

Output: 9 of 9 replies byte-equal between main and the branch with 256 completion tokens each (prompts of 1k, 8k, 32k, 64k and 75k tokens on one server at ctx 131072, plus the two memory cells per context); main against itself on the 8k, 64k and 75k prompts was equal first. No prefix-cache hit in any request (cached_tokens 0).

Rule, fixed before the first cell: a drop of at least 4 GB at 131072 and every output equal; passed. The 65536 row had no threshold.

The bill column is arithmetic from bills_test.zig (57344 B per token full, 16384 B for the global layers, a 2560-row ring at 40960 B), not a measurement. The peak is more than the KV rows on both builds: at 131072 main grows 15.3 GB over its loaded footprint where the bill says 7.5 GB, the branch 8.5 GB where it says 2.3 GB; what the rest is was not examined. The kernel's lifetime maximum gives the same drop at 131072 (20.48 − 13.68).

Not measured: Gemma 4 26B-A4B and gpt_oss (no pack on this machine), image or audio input, thinking on, a prefix-cache hit on a ring, several slots, the SSD tier, --kv-quant, outputs longer than 256 tokens. Two runs per arm.

Known limits: a ring session that dropped rows links no donor chunks on the SSD tier (docs/known-issues.md); a prefix-cache hit or a disk restore serves only matches that still hold the window behind them.

  • Searched open and closed issues, open PRs and recent commits; linked the existing thread if there is one (none for these archs; closest is the closed WIP: resident MiMo-V2.6-Flash-MOPD engine and full KV cache #677 on MiMo)
  • Built and ran this tree on a real Apple Silicon Mac in this session
  • No comments that restate code, carry history, or cite audit/review items
  • No source-scan tests; every test checks behaviour
  • One fix or feature; description under ~15 lines (one feature in six commits; the description runs longer)

Their sliding layers cached every token they never read: Laguna XS held
21.5 GB of KV at 128k, Gemma 4 26B-A4B 29.5 GB. The plugins now declare
`CacheFacts.sliding_ring` (window + 2048, `sdk/config/sliding_ring.zig`)
as MiMo does, their attention appends through it (`kv.appendSliding`),
and `ModelConfig.slidingRing` reads the declared fact: per token the bill
covers the full layers only (Laguna XS 160 -> 40 KiB, gpt-oss-20b
48 -> 24 KiB, Gemma 4 26B-A4B 220 -> 20 KiB, E4B 56 -> 16 KiB without
its KV-shared layers), the rings once per slot. flint and kolibri1 share
laguna's parse and keep rings too.

A ring drops whole 32-row blocks, so the hd-256 band kernel tiles a
whole-buffer view as it tiles the full buffer; Gemma's masks read the
view's own length; a ring that dropped nothing still persists to the
SSD tier; a request's ring is billed at keep + min(chunk, prompt) rows.
…g under its window

A request's ring was billed at keep + min(chunk, prompt) rows whatever its
length: a 10-token prompt with 256 tokens to generate on Laguna XS billed
2570 ring rows (316 MB) where prompt and generation reach 266. The ring now
bills at most the prompt plus the generation it may add (max_tokens capped
at RESERVE_GEN_HEADROOM, as `reservedTokens` caps it). The headroom alone
(8192) would change nothing: every shipped keep (2176-3072) is below it.
Long prompts and an omitted max_tokens keep keep + one chunk.

A plugin declaring `CacheFacts.sliding_ring` below a sliding layer's window
is declined by name at resolve; the cache only debug-asserted it, which
ReleaseFast drops.

known-issues names what the SSD guard costs: once a ring drops a row the
whole session stops persisting, its full layers too.
…t holds whole

Once a ring dropped a row the SSD tier skipped the whole session, its full
layers too, so past window + 2048 tokens (2.2k-3.1k on the shipped packs)
a session lived in RAM only. A commit now writes the full layers from
position 0 and the ring layers into the chunks from the first one the ring
still holds whole; meta v11 records the ring layers, their keep and window
and that chunk (`ring.from`). A lookup skips a match shorter than that
chunk plus the window, and a restore refuses it; otherwise the ring layers
read their newest keep rows from a 32-row block and get base, keep and
window back. An older binary rejects v11: its restore would come up short.
A ring that dropped rows links no donor chunks. A restore also refuses a
layer some chunk lacks instead of building a short cache.

Persisting only the full layers would restore nothing: a resumed forward
needs every layer's rows behind the match.
planGrow rounds the ring buffer up in 256-row steps; the same rounding applies to
full-attention layers and the runtime floor covers it.
@dmumber
dmumber marked this pull request as ready for review November 10, 2026 09:40

ddalcu commented Nov 10, 2026

Copy link
Copy Markdown
Owner

🤖 MLXServe-PRBot — code review

The shape is right:

  • The plugin declares the ring, and slidingRing stops keying on isMimo().
  • sliding_ring.rows is one helper.
  • Drops are block-aligned to 32 rows, so the hd-256 band kernel tiles a whole-buffer view exactly as it tiles the full buffer. Each plugin's proof compares a ring that dropped rows against the full buffer.
  • KV-shared layers are excluded from both bills.
  • The v11 SSD meta is gated and an older binary refuses it.

Two things before this lands:

  1. MiMo keeps the copy this PR deletes, with the old gate.
    • mimo_v2/sink_attention.zig:134 append is line for line the function you removed from laguna_attention.zig in favour of kv.appendSliding.
    • It still rings only when keep > 0. appendSliding rings on ring > 0 alone, so the ring "holds from the slot's first chunk", which is your own reason for the change.
    • So the arch the ring came from now behaves differently from the three you added: on a whole-buffer view MiMo still appends the full row.
    • Route MiMo through kv.appendSliding too, so there is one append path and one rule. If MiMo has to keep keep > 0, say why in the code.
  2. Evidence gaps (this is a memory PR, so the table has to be complete):
    • The "M5 Ultra" has no RAM figure. Add it.
    • Gemma 4 is the riskiest of the three: it has KV-shared layers reading a source layer's ring, and getKVSourceLayer is new in both bills. Yet it only has the proof test and bill arithmetic, with no live run.
    • E4B is small enough for any Mac. Run main vs branch output equality on a prompt well past the 512 window, plus one peak-footprint pair, the same way as the Laguna cells.

No conflict with main.


Generated by Claude Code

MiMo kept its own copy of the append with the old gate (a ring only when
keep > 0). Its sliding layers ask for a whole-buffer view (keep 0) only
where the hd-256 band kernel masks the band itself, at head 256 on a chunk
of 16 rows or more; the released pack is head 192 and takes the same arm
under either gate. At head 256 every prefill chunk appended in full, so a
long prompt held all its sliding rows until the first decode row, which
the bill does not charge. One append path and one rule now.

Without the band kernel (a CPU stream) the composed band mask was sized
for offset + rows keys where a ring that dropped rows hands back fewer;
`core` reads the key count from the view, as decoder_attention does.
At head 256 a chunk of 16 rows or more asks for a whole-buffer view
(keep 0). Laguna has no band kernel there and gpt_oss has none on a CPU
stream, so both compose the band mask, sized for offset + rows keys; a
ring that dropped rows hands back fewer and the forward aborted on the
broadcast. Both read the key count from the view, as decoder_attention
and MiMo do. The released packs are head 128 and 64 and never took this
arm; both configs accept 256.
@dmumber

dmumber commented Nov 10, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review.

1. MiMo's own append. Gone: mimo_v2/sink_attention.zig now appends through kv.appendSliding (solo and per slot), so the four archs share one append path and one rule. MiMo did not need keep > 0.

What the gate decided: a MiMo sliding layer only asks for a whole-buffer view (keep 0) when the hd-256 band kernel masks the band itself, i.e. at head 256 on a chunk of 16 rows or more. The released pack is head 192, where the view is always a tail and both gates take the same arm, so nothing changes for it. At head 256 the old gate appended every prefill chunk in full: the ring only started at the first decode row, and a long prompt held every row of its sliding layers until then, which is not what the bill charges.

One thing came with it. Without the band kernel (a CPU stream, a kernel that declines) the composed band mask was built for offset + rows keys, while a ring's whole-buffer view holds fewer once it dropped rows, and the forward aborted on the shape. core now reads the key count from the view, as decoder_attention.zig already does for Gemma.

Test: mimo_v2 plugin: at head 256 a prompt prefilled in whole-buffer chunks keeps its sliding rings and reads as the full buffer, through the band kernel and without it (src/plugins/mimo_v2/proof_test.zig). A head-256 pack, three prefill chunks (250, 40, 20 rows), a ring of window + 2 against the full buffer, once on the GPU stream and once on the CPU stream: logits bit-equal at every chunk, the ringed sliding layers dropped rows and the full ones did not. Before the change it fails on the drop check (the ring never starts); with appendSliding alone and the old mask length the CPU arm aborts.

The same mask length sat in two more places, fixed in a second commit: laguna_attention.zig (Laguna has no band kernel at head 256, so it failed on the GPU stream too) and gpt_oss/attention.zig (without the band kernel). Each has the same head-256 test (laguna plugin: at head 256 …, gpt_oss plugin: at head 256 …): both aborted before (exit 255, [broadcast_shapes] Shapes (1,1,40,290) and (1,4,40,66) for Laguna) and are bit-equal now; the released packs are head 128 and 64 and never took that arm. The Laguna test runs on the GPU stream only: on a CPU stream Laguna at head 256 aborts with or without a ring (force_fused=True but no fused kernel is available), which this branch does not touch.

M5 Ultra, 256 GB, macOS 27.2, at 1612223a on main 9191fbdf: zig build test --summary all -Dtest-shards=2: 29/29 steps succeeded, 5179/5586 tests passed (407 skipped), 0 failed; zig build check and python3 scripts/lint.py --strict clean.

2. Evidence. The RAM figure is in the description (M5 Ultra, 256 GB). Gemma 4 E4B ran as asked (gemma-4-e4b-it-4bit, head 256; main 9191fbdf against the branch at 1612223a, measured as the Laguna cells): at ctx 131072 the peak footprint is 20.48, 20.48 GB on main and 13.61, 13.68 GB on the branch, a drop of 6.80 GB (bill 5.26); at 65536 15.17, 15.17 against 12.72, 12.63 GB, a drop of 2.45 GB (bill 2.58). 9 of 9 replies are byte-equal between main and the branch (prompts of 1k, 8k, 32k, 64k and 75k tokens, all but the first well past the 512 window, plus the memory cells), and main against itself was equal first. The table, the rule fixed before the run and what was not measured are in the description.

ddalcu commented Nov 10, 2026

Copy link
Copy Markdown
Owner

🤖 MLXServe-PRBot approved — reviewed at head 1612223, no issues found.

Both items are done:

  • One append path. MiMo appends through kv.appendSliding, solo and per slot.
  • The mask-length bug moving MiMo exposed is fixed in all three copies. A ring's whole-buffer view holds fewer keys than offset + rows once it drops rows. MiMo, Laguna and gpt-oss now read the length from the view. bandOnce is keyed on kv_len, so ring and full layers never share a mask.
  • Tests. Each arch has a head-256 test: three prefill chunks, ring against full buffer, bit-equal at every chunk. They fail before the change on the drop check or the broadcast abort.
  • Evidence. The description has the RAM (M5 Ultra, 256 GB) and a Gemma 4 E4B main-vs-branch run: 6.80 GB less at 131072, 2.45 GB less at 65536, and 9 of 9 replies byte-equal past the window.

No conflict with main. zig is still running on this head.


Generated by Claude Code

@ddalcu
ddalcu merged commit 56d5b65 into ddalcu:main Nov 18, 2026
8 checks passed
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.

2 participants