Repository navigation
kv: sliding rings for laguna, gpt_oss and gemma4 - #1155
Conversation
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.
|
🤖 MLXServe-PRBot — code review The shape is right:
Two things before this lands:
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.
|
Thanks for the review. 1. MiMo's own append. Gone: What the gate decided: a MiMo sliding layer only asks for a whole-buffer view ( One thing came with it. Without the band kernel (a CPU stream, a kernel that declines) the composed band mask was built for Test: The same mask length sat in two more places, fixed in a second commit: M5 Ultra, 256 GB, macOS 27.2, at 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 |
|
🤖 MLXServe-PRBot approved — reviewed at head Both items are done:
No conflict with main. Generated by Claude Code |
What changed / why
The sliding-window layers of
laguna(withflintandkolibri1, which share its parse),gpt_ossandgemma4cached every token although attention only reads the last window; only MiMo kept a ring (slidingRing()wasisMimo()). These plugins now declareCacheFacts.sliding_ring(window + 2048,sdk/config/sliding_ring.zig) as MiMo does, their attention appends throughkv.appendSliding, andModelConfig.slidingRingreads the declared fact instead of the arch name.KV billed per token (
kvBytesPerToken, pinned by thesliding layers keep a ringtest inadmission/bills_test.zigagainst 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:
ring.from); an older binary rejects v11.Two commits after the first review:
kv.appendSlidinglike the other three. Its own copy rang only withkeep > 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.gpt_osson 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 testN/0,swift testif app, the integration script)M5 Ultra, 256 GB, macOS 27.2, at
1612223aon main9191fbdf:zig build test --summary all -Dtest-shards=229/29 steps, 5179/5586 passed, 407 skipped, 0 failed;zig build checkandpython3 scripts/lint.py --strictclean.Live measurement, Laguna XS
M5 Ultra, 256 GB, macOS 27.2. Laguna-XS-2.1-NVFP4-mlx, main
9191fbdfagainst this branch at55292ef3(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-sizeas listed,--max-concurrent 1,--prefix-cache-mem 1MB, no--kv-quant, no SSD tier, prompt of ctx − 512 tokens by/tokenize,max_tokens256, temperature 0, thinking off. Peakphys_footprintof 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.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_tokens0).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
9191fbdfagainst this branch at1612223a, both ReleaseFast with the same staged mlx. The same way as the Laguna cells: one fresh server and one request per cell,--ctx-sizeas listed,--max-concurrent 1,--prefix-cache-mem 1MB, no--kv-quant, no SSD tier, prompt of ctx − 512 tokens by/tokenize,max_tokens256, temperature 0, thinking off. Peakphys_footprintof 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.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_tokens0).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.