Skip to content

feat: spill evicted conversation prefixes to disk, and --checkpoint-slots - #68

Closed
gilby wants to merge 1 commit into
ashhart:mainfrom
gilby:feat/spill-evicted-conversations
Closed

gilby wants to merge 1 commit into
ashhart:mainfrom
gilby:feat/spill-evicted-conversations

Conversation

@gilby

@gilby gilby commented Sep 28, 2026

Copy link
Copy Markdown
Contributor

Problem

An evicted conversation costs a full prefill on its next turn. GDN state can't be truncated to a shorter stored prefix, and conversations are only written to disk at shutdown (the newest two). With several conversations interleaved, every time one comes back it re-prefills from scratch: 20–70 s for 30K–100K tokens on Qwen3.8-27B.

A second limit hides behind --prompt-cache-gib: the store also caps entries at max(3 × lanes, 8). With 30K-token conversations (~2 GiB each on Qwen3.8-27B), that slot cap binds at ~16 GiB whatever byte budget you set.

Change

  • CheckpointStore(on_evict=…): called outside the lock, on the scheduler thread that owns the arrays, for each evicted conversation. It covers both insert() (slot and byte budget) and evict_one() (memory pressure). It skips pinned system blocks, and older checkpoints of a conversation that has moved on (a strict prefix of a remaining entry), so SSD writes stay proportional to conversations actually pushed out.
  • spill_conversation(): writes the entry into the session-snapshot directory, which _read_disk_block already searches, and keeps this model's files there under a byte limit, oldest first.
  • --spill-gib N (default 0, off). With it on, the shutdown save keeps the whole directory within the same limit instead of only the newest two.
  • --checkpoint-slots N (default unchanged: max(3 × lanes, 8)).
  • README option rows, and tests/test_checkpoint_spill.py.

The write is synchronous. It measured ~0.2 s per 2 GiB on an M5 Ultra's internal SSD, small next to the prefill it replaces, and it keeps the memory controller's evictions actually freeing memory.

Measurements

M5 Ultra (256 GB), Vontra/Qwen3.8-27B-MLX-4bit + z-lab/Qwen3.8-27B-DFlash2, this branch, --prompt-cache-gib 6 --checkpoint-slots 4 --spill-gib 20. Conversation A (35,583 tokens) is evicted by three other ~33K-token conversations, then continued:

A's next turn time cached tokens output sha256
spill on: prefix read back from disk 2.3 s 35,396 3982be5a2be3
cold (spill off, fresh server) 26.2 s 0 3982be5a2be3
  • Each spill: 2.2–2.4 GiB in 0.18–0.19 s. Read-back: 0.24 s. The output is byte-identical to the cold run.
  • On 0.3.4.1, a live run with 36 × 30K-token conversations at --prompt-cache-gib 64 showed the slot cap. The default 8 slots held 15.2 GiB with 29 evictions; --checkpoint-slots 32 held 60 GiB with 5.

Tests

  • tests/test_checkpoint_spill.py: 8 tests covering hook on slot, budget and memory-pressure evictions; the superseded-prefix skip; pinned blocks never spilled; a failing hook never fails an insert; a spilled entry is found by DiskBlocks.best and round-trips; byte-limit pruning.
  • Full suite on macOS (M5 Ultra): 1787 passed, 84 skipped. tests/cuda was not run.

🤖 Generated with Claude Code

…lots

An evicted conversation costs a full prefill on its next turn: GDN state cannot be truncated to a shorter
stored prefix, and conversations were only written to disk at shutdown (the newest two). Under several
interleaved conversations that is a 20-70 s re-prefill each time one comes back.

- CheckpointStore gains an on_evict hook, called outside the lock for each evicted conversation from both
  insert() (slot and byte budget) and evict_one() (memory pressure). An older checkpoint of a conversation
  that moved on (a strict prefix of a remaining entry) and pinned system blocks are skipped.
- spill_conversation() writes the entry to the session-snapshot directory, which _read_disk_block already
  searches, and keeps this model's files there under a byte limit (oldest first).
- --spill-gib N (default 0: off) turns it on; with it, the shutdown save keeps the whole directory within
  the same limit instead of the newest two.
- --checkpoint-slots N: the store also caps entries at max(3 x lanes, 8), which with 30K-token
  conversations (~2 GiB each on Qwen3.8-27B) binds at ~16 GiB whatever --prompt-cache-gib says.

Measured on an M5 Ultra, Vontra/Qwen3.8-27B-MLX-4bit + DFlash2, --prompt-cache-gib 6 --checkpoint-slots 4
--spill-gib 20: conversation A (35,583 tokens) evicted by three others; each spill wrote 2.2-2.4 GiB in
0.18-0.19 s; A's next turn read its prefix back in 0.24 s and answered in 2.3 s, against 26.2 s cold, with
byte-identical output. Tests: tests/test_checkpoint_spill.py; full suite 1787 passed, 84 skipped (tests/cuda
not run).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@ashhart

ashhart commented Sep 28, 2026

Copy link
Copy Markdown
Owner

Thanks @gilby, this is a real fix and it lands in 0.3.6.2 under your name: 675d4c2e. The hook covering both
slot/budget evictions and memory pressure, plus the superseded-prefix skip, is right, and your byte-identical check
matched ours.

On our side: rebased onto 0.3.6.2, and the co-author trailer dropped (we keep commit authorship to people). We cut the
docstring and a comment to one line each, since we keep measurements in the ledger rather than in code. The shutdown
save now prunes the directory to --spill-gib too, so saving every conversation at exit can't overshoot it.

Measured again here with #55's case, two 35k-token conversations taking turns at a 48 GB Mac's default budget on an
M5 Max: the returning conversation started in 0.27 s instead of 75 s, read back in 0.08 s after a 1.6 s spill, and its
output was identical to a cold prefill. On an M3 Ultra it was 0.9 s instead of 122 s, with 2.4 GiB spills in 0.4 s.
It also answers #55 directly, and I pointed Deesha at it. I hope you'll send the next one too.

@ashhart ashhart closed this Sep 28, 2026
wojo added a commit to wojo/TensorFold that referenced this pull request Oct 4, 2026
…ll-gib)

On CUDA a kept prompt state that leaves memory costs a full prefill on its next turn (minutes at 200k tokens
on two GB10s), and nothing survives a restart. --spill-gib, the Mac flag from ashhart#68, now turns on a disk tier
for the CUDA engines that set CUDA_SPILL (GLM-5.3-Flash today; other families refuse the flag).

- cuda/spill.py: a model-agnostic store. A family hands it the snapshot object it already keeps; encode walks
  its fields (tensors to a safetensors file, plain values to JSON, the class by import path, as
  engine.prefix_snapshots does for MLX) with an optional materialize hook for rows still in the live caches.
- Writes run on writer threads with their own CUDA stream and pinned staging buffers, never on the engine
  thread (ashhart#155); the engine waits for a copy only before overwriting rows it still reads. Files go to a
  .partial name, are fsynced and renamed, the directory fsynced; a size/header mismatch or a .partial is
  dropped at start. Files sit under spill-<compat>/rank<N>/, the hash over format, model, drafter, KV layout
  and TP layout; other builds' folders are pruned at start (--spill-keep-builds).
- Two ranks: rank 0 decides (save, early write, load, flush) and rank 1 follows through the request header;
  sizes and the free-disk floor are agreed in one all-gather a save, a read is used only if it worked and
  fits the request's drafters on both ranks, and at start only entries every rank holds are kept.
- --spill-highwater writes the states eviction takes next in the background; --spill-min-tokens and
  --spill-min-free-gib bound what is written; a clean shutdown (SIGTERM, Ctrl-C) writes what is kept.
- /health "spill" and tensorfold_spill_* metrics; docs/cuda-spill.md; tests/test_cuda_spill.py (CPU).
ashhart added a commit that referenced this pull request Oct 26, 2026
…eckpoint-slots bounds RAM, on Macs (#840)

Python #68 spilled a conversation pushed out of the prompt cache to disk
and read it back when the conversation returned; --checkpoint-slots set
how many stayed in memory. The native engine had the store for it
(--learn-conversations' rows store) but refused both flags.

On the four Metal families with a rows image (Qwen3.8-27B, Nemotron,
Flash Next, GLM-5.3-Flash) --spill-gib N opens that same store under
~/.cache/tensorfold/spilled with an N GiB cap, in a mode that writes
nothing behind: Store.remove hands an evicted (not expired) entry to
rows.write.evicted, which writes it before it goes unless the disk holds
it already. Reading back is --learn-conversations' path unchanged, so a
resumed state equals a fresh pass. Past the cap the least recently used
spilled state goes. --checkpoint-slots N sets the RAM store's `most`
beside the disk store (rows.attach); a Mac refuses it without one.
With --learn-conversations too, states are still written behind and an
eviction finishes a write under way instead of giving it up; the cap is
the smaller of the two. recall now finds the state it reads by name
after making room, since a spill past the cap can drop disk states.

Off by default: without either flag Open.spill is .off and every path
is the old one. CUDA and HIP do not list --spill-gib (refused as
before), and HIP keeps its own --checkpoint-slots.

Co-authored-by: gilby <880013+gilby@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants