Skip to content

server : write context checkpoints beyond a RAM cap to unlinked files (--ctx-checkpoints-dir, --ctx-checkpoints-ram) - #180

Merged
dzannotti merged 3 commits into
halo-box:masterfrom
rcfa:pr/ckpt-spill
Nov 22, 2026
Merged

dzannotti merged 3 commits into
halo-box:masterfrom
rcfa:pr/ckpt-spill

Conversation

@rcfa

@rcfa rcfa commented Oct 15, 2026 •

Copy link
Copy Markdown

On a unified-memory machine, context checkpoints compete with the model for RAM. On a hybrid model each checkpoint
holds the recurrent state of every layer: 112 MiB on Qwen3.8-Flash-Next, 150 MiB on Qwen3.8-27B, up to
--ctx-checkpoints per slot.

--ctx-checkpoints-dir PATH keeps checkpoints in memory while they fit in --ctx-checkpoints-ram (MiB, default
8192) and writes the oldest beyond that to unlinked files in PATH. A server with memory to spare writes nothing. A file
is read back only to rewind a slot. --ctx-checkpoints-ram 0 writes every checkpoint, which is what the first
version of this PR always did.

The write cost, stated in the help text and the README: each checkpoint written costs its size in disk writes. A
server whose checkpoints stay within the cap writes nothing. One whose checkpoints exceed it for good (every slot's
list full all day) writes each new checkpoint once, when it becomes the oldest one in memory. Oldest-first is the
right victim: a rewind reads recent checkpoints, and old ones are mostly dropped unread. A directory on tmpfs saves
no RAM.

Changes:

  • common_prompt_checkpoint: spill() moves the target and draft state into an unlinked file (mkostemp with
    O_CLOEXEC, the unlink checked; if unlink() fails, the empty named file stays) and leaves errno set when it
    fails. copy_tgt() / copy_dft() and load_tgt() / load_dft() read it back with pread(). The draft state's
    offset in the file is kept, so clear_tgt() does not move it. size_mem() is what a checkpoint holds in memory.
  • The server tracks the checkpoints still in memory, oldest first, through weak references, so a checkpoint shared by
    a slot and the prompt cache (llama-server: Use reference-counted checkpoints instead of deep-copies ggml-org/llama.cpp#30137) is written once for all holders. Only the main loop writes or reads a
    checkpoint. A failed write leaves the checkpoint in memory, logs why, and is retried at the next checkpoint.
  • --cache-ram counts size_mem() in the cache total, in the admission check and when it makes room, so a written
    checkpoint no longer counts at full size.
  • A failed read of a checkpoint file falls back to reprocessing the prompt. Only a checkpoint held in memory still
    aborts.
  • The slot file's draft test-load checks size_dft(), not data_dft, which is empty after a write. A slot file's
    checkpoints count against --ctx-checkpoints-ram after the trim to --ctx-checkpoints and the draft test-load.
  • A slot save writes a checkpoint held in memory directly, without a copy.
  • Windows: the flag is ignored with a warning.
  • tools/server/README.md: the rows of the two options only. Running llama-gen-docs on master also rewrites about
    40 unrelated rows that are out of date; those are left out.

Tests:

  • tests/llama-context, case_rollback: the checkpoint is also written to a file and loaded into a fresh context;
    the replay logits must match exactly. A draft state written after a target state reads back after clear_tgt().
  • test_slot_save.py: the draft KV type change with the directory, with the byte counts that save and restore
    report (n_written equals the file size, n_read equals n_written, checkpoints in files and in memory); a failed
    checkpoint read (a preloaded pread() that fails with EIO); the cap (default: no file; 0: every checkpoint in a
    file, counted in /proc/<pid>/fd); --cache-ram with every checkpoint in a file (a prompt whose state fits is
    cached although it does not fit at full size).

Verification (Strix Point, Ryzen AI 9 HX PRO 375 / Radeon 890M, gfx1150; not Strix Halo), CPU build, Release, gcc 16.2:

  • test_slot_save.py and test_kv_keep_only_active.py: 31 passed.
  • The --cache-ram test fails with size() in alloc() (the prompt is skipped as 8.619 MiB over a 5 MiB limit).
  • test-llama-context --test rollback$: 16 passed, 131 skipped (models without recurrent state), 0 failed. With the
    file read broken on purpose (a zeroed buffer), all 16 fail at "loading the checkpoint from its file failed"; with
    the draft read at the old offset, all 16 fail at the clear_tgt() check.
  • The byte-count check fails on the two directory cases when a checkpoint read back from its file is written but not
    counted.
  • The write-failure messages name the error (missing directory, mode 500, a write that returns 0, ENOSPC from a
    preloaded pwrite()).

The maintainers' review of a5aaf357b measured Vulkan and ROCm on Strix Halo (greedy output identical to master,
TTFT, disk writes).

Not verified by us: Vulkan and ROCm runs of this version, Windows, macOS, a real disk error.

Written by Claude Code (an AI agent) for rcfa; rcfa owns it.

Generated with Claude Code

@rcfa

rcfa commented Oct 20, 2026

Copy link
Copy Markdown
Author

Thanks for rebasing. The rebased branch does not compile against the new master: common_prompt_checkpoint::load_tgt() / load_dft() return bool there, and the file-backed branch still aborted on its own and ended in return; (g++: return-statement with no value). 325d369 returns the result like the in-memory branch, so master's caller decides: abort for a checkpoint of this server, fall back to reprocessing for one restored from a slot file. Written by an AI agent (Claude Code) for @rcfa.

Verified on gfx1150 (Radeon 890M, Vulkan), Qwen3.8-Flash-Next IQ3_XXS with MTP: ckpt-spill-test.py, 12k-token prompts, 7 requests (an append, two rewinds to checkpoints, one of them after a slot save and restore, and a prompt cache round trip), RAM prompt cache. Without and with --ctx-checkpoints-dir: the same tokens computed and the same greedy tokens in all 7; anonymous RSS at the end 2052 vs 1374 MiB (6 x 112.6 MiB in files); after kill -9 no files are left in the directory. Not verified: Strix Halo, Windows.

Unrelated to this PR: the base 93e6d02 does not build examples/speculative-prefill (common_batch_clear / common_batch_add are gone); master fixed it in #182. llama-server builds.

rcfa added a commit to rcfa/strix-llama.cpp that referenced this pull request Oct 23, 2026
Joins halo-box#149 and halo-box#180: a disk cache entry is written from the checkpoint files, and the checkpoints
restored from an entry move to --ctx-checkpoints-dir again.

Assisted-by: Claude Code
rcfa added a commit to rcfa/strix-llama.cpp that referenced this pull request Oct 27, 2026
Joins halo-box#149 and halo-box#180: a disk cache entry is written from the checkpoint files, and the checkpoints
restored from an entry move to --ctx-checkpoints-dir again.

Assisted-by: Claude Code
rcfa added a commit to rcfa/strix-llama.cpp that referenced this pull request Nov 1, 2026
Joins halo-box#149 and halo-box#180: a disk cache entry is written from the checkpoint files, and the checkpoints
restored from an entry move to --ctx-checkpoints-dir again.

Assisted-by: Claude Code
rcfa added a commit to rcfa/strix-llama.cpp that referenced this pull request Nov 4, 2026
Joins halo-box#149 and halo-box#180: a disk cache entry is written from the checkpoint files, and the checkpoints
restored from an entry move to --ctx-checkpoints-dir again.

Assisted-by: Claude Code
@dzannotti dzannotti added in-review We are reviewing. verifying Building and running the PR to verify its claims. and removed in-review We are reviewing. labels Nov 10, 2026

@dzannotti dzannotti left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict

Changes requested. The flag works for your scenario and frees the host memory as described, but it skips the draft check on slot restore, and on a 27B hybrid model it writes about 0.45 GB to the SSD per request.

Direction

Not in this form. We want the goal, because on unified-memory boxes checkpoint memory competes with the model. But the PR writes every checkpoint to disk when it is created, whether or not anything reads it back, so the cost grows with each request. Consider one of these instead: lowering --ctx-checkpoints or raising --checkpoint-min-step (no code); spilling only when a checkpoint leaves the slot (prompt cache) or under a RAM cap; or keeping the newest N checkpoints in memory and spilling only the older ones. This is also general server code that upstream will rewrite (upstream ggml-org#30137 changes data_tgt/data_dft and the checkpoint lists), so a smaller diff carries better here.

Asks

  1. Blocking. tools/server/server-context.cpp:3037: after spill_checkpoint(cur) at :3030, data_dft is always empty, so with the flag the restore skips the draft test-load and the draft seq_rm. With a different draft model at restore, the first rewind fails load_dft and reprocesses the whole prompt (7505 tokens instead of 518 in our test). Use checkpoints.back().size_dft() > 0. We tested this one-line fix, and it restores the 518-token rewind.
  2. Blocking. On Qwen3.8-27B each checkpoint is 149.6 MiB, a multi-turn workload made about 3 checkpoints per request, and every one goes to the SSD (table below). Add a cap or a lazy spill (see Direction), and document the write cost in the help text and the server README.
  3. Should fix. server-context.cpp:4105-4108 with common/common.cpp:2545,2569: a failed pread on a checkpoint this server made now hits GGML_ABORT. A disk error should fall back to reprocessing, not stop the server.
  4. Should fix. common/common.cpp:2438-2444: use mkostemp(..., O_CLOEXEC) (or O_TMPFILE), because the server spawns child processes from other threads. Also check the unlink result.
  5. Should fix. server-context.cpp:2962-2979 copies every checkpoint into a temporary buffer even without the flag, so the default path changes. Write data_tgt/data_dft directly when the checkpoint is in memory. The return 0; at :2971/:2977 is in a bool function.

Also, on Windows spill() returns false, so the startup probe makes the server exit, not ignore the flag as the description says; regenerate the README with llama-gen-docs; add a round-trip test (tests/llama-context/cases-rs.cpp already does update_tgt/load_tgt); squash the two commits (the first does not build on master).

Correctness

PASS for the scenario you described, FAIL for a restore with a different draft (ask 1). Base, head without the flag and head with the flag gave the same prompt token counts and the same greedy text in all 7 requests, on Vulkan and on ROCm (also with HIP_LAUNCH_BLOCKING=1). Slot save files were byte-identical. server-test-model.py (4/4) and server-test-parallel-tc.py (12/12) passed the same on base and head on both backends. A dense model with the flag behaves the same as base. A missing or read-only directory gives a clean startup error. The checkpoint directory is empty after kill -9. We did not run the server pytest suite.

Disk writes / SSD wear

Measured with Qwen3.8-27B (MTP), -np 2 -c 65536, default checkpoint settings, 2 conversations of 8 turns (9k-token first prompt, then 2k-token and short follow-ups):

master head, no flag head, flag
process write_bytes, 16 requests 4 KB 4 KB 7.22 GB
reached the device (synced, idle baseline subtracted) 14 MB 6 MB 4.08 GB
per checkpoint / per request - - 157 MB / 451 MB

The rest was dropped when the short run stopped the server. In a server that runs for hours, the files live longer than the writeback delay, so expect close to 451 MB per request. At 2000 requests per day that is about 0.9 TB per day: 1% of a 600 TBW drive every 7 days, or of a 1200 TBW drive every 13 days.

Verification

Strix Halo (gfx1151), Vulkan and ROCm. We tested the PR merged into master 8acea9aa4 (the PR was 381 commits behind) and compared it with that master.

Not tested

The server pytest suite, Windows and macOS, SWA-only models, a real disk I/O error, and the draft-mismatch case on ROCm.

Thank you for the detailed measurements.

Review made with AI on behalf of the strix-llama.cpp maintainers.

Full verification (for reproduction and agents)

Environment

  • Ubuntu 26.04.1 LTS, kernel 7.0.0-43-generic, Strix Halo (Radeon 8060S, gfx1151).
  • ROCm 10.1.0 (hipconfig 7.16.26385), AMD clang 24.0.0git d6f6cb69.
  • Mesa RADV 26.0.8 (RADV STRIX_HALO).
  • cmake 4.2.3, gcc 15.2.0, Ninja.
  • Checkpoint directory on ext4 on the local NVMe (2 TB client drive, rated TBW not known). ulimit -n 524288 except in test E.

Compared

  • master tip: 8acea9aa4cd7196ac5160f3967c551b3c83dc71c
  • PR head: 325d369f1ee7c627dae707893fb532c29dee68c3
  • local merge of the PR into master (not pushed): 94d69fac4e138a4839a624378ee552c2f2d4c87a, clean
  • merge base: 93e6d0285b95b8d671aaed1e75273a8b788e8676, 381 commits behind master
  • fix check: the merge plus checkpoints.back().size_dft() > 0 at server-context.cpp:3037

Build

cmake -S <tree> -B <tree>/build-vk  -DGGML_VULKAN=ON -DGGML_HIP=OFF -DCMAKE_BUILD_TYPE=Release -G Ninja
cmake -S <tree> -B <tree>/build-hip -DGGML_HIP=ON -DGGML_VULKAN=OFF -DGPU_TARGETS=gfx1151 -DCMAKE_HIP_COMPILER=$(hipconfig -l)/clang -DCMAKE_PREFIX_PATH=$(hipconfig -R) -DCMAKE_BUILD_TYPE=Release -G Ninja
cmake -S <tree> -B <tree>/build-cpu -DGGML_VULKAN=OFF -DGGML_HIP=OFF -DCMAKE_BUILD_TYPE=Release -G Ninja
cmake --build <tree>/build-<be> -j 32 --target llama-server llama-completion

All builds were clean on both sides.

Models

model HF repo / file sha256
Qwen3.8-27B (hybrid, MTP head) Qwen3.8-27B-UD-Q4_K_XL.gguf (source repo not on record) 3f227079003add2511437e5b1e94812e363385225bf6a9b47b0054a72bc8b01e
DFlash2 draft for Qwen3.8-27B Qwen3.8-27B-DFlash2-Q4_K_M.gguf (source repo not on record) 1a25c56858e1ebe93f2718ac1d49d1151f9323325c1bbfd6209370f4db131ebd
Qwen2.5-1.5B-Instruct (dense) Qwen/Qwen2.5-1.5B-Instruct-GGUF / qwen2.5-1.5b-instruct-q4_k_m.gguf 6a1a2eb6d15622bf3c96857206351ba97e1af16c30d7a74ee38970e434e9407e
LFM2-700M (hybrid, CPU runs) LiquidAI/LFM2-700M-GGUF / LFM2-700M-Q4_K_M.gguf 684e8406dc13321452b3f6aeca432776e2a6a7e1ad6c23f7887b8fe3efbe2efa
LFM2-350M (draft, fd test) LiquidAI/LFM2-350M-GGUF / LFM2-350M-Q4_K_M.gguf a4d000c7064bd3b2e42c6845836286a899a4e79cf1791da1a6797b58d575957d
Granite 4.0 H 1B (hybrid, CPU wear) ibm-granite/granite-4.0-h-1b-GGUF / granite-4.0-h-1b-Q4_K_M.gguf da3d737121a96f3c9a316685212376257a7f167b74380855666dd488d6af3bcb

Commands

Each test started a fresh server and sent requests to /v1/chat/completions (temp 0, n_predict 16 or 48, id_slot fixed). It recorded timings.prompt_n and cache_n, /proc/<pid>/status RssAnon, /proc/meminfo Shmem, /proc/<pid>/io, the number of llama-ckpt fds in /proc/<pid>/fd, and the directory listing. Prompts were cut from the repo's tests/corpus/correctness-*.txt.

  • A (your scenario), per side: llama-server -m <27B> --spec-type draft-mtp -ngl 99 -fa on -c 32768 -np 2 --slot-save-path <dir> -rea off [--ctx-checkpoints-dir <ckdir>]. The requests: r1 A+q1 (about 7.5k tokens); r2 append; r3 diverging suffix; save slot, erase, restore; r4 diverging suffix; r5 B and r6 C on the same slot; r7 A again from the RAM prompt cache. The head-with-flag server was stopped with kill -9.
  • B (draft mismatch, Vulkan and CPU): save slot 0 from --spec-type draft-mtp, then restore into -md <DFlash2> --spec-type draft-dflash (CPU: LFM2-700M saved with draft LFM2-350M, restored with LFM2-700M as the draft), then send a diverging request. Run on head without the flag, head with the flag, and the fix with the flag.
  • C (dense): -m <qwen2.5-1.5b> -ngl 99 -fa on -c 16384 -np 2, 3 requests, base vs head with the flag.
  • D (probe): --ctx-checkpoints-dir <missing dir> and <dir with mode 500>.
  • E (fd limit): ulimit -n 32, LFM2-350M, -ngl 99 --checkpoint-min-step 0 -np 2, 40 requests.
  • F: scripts/server-test-model.py and scripts/server-test-parallel-tc.py (from the master tree) against -m <27B> -c 32768 -np 2 -ngl 99 -fa on -rea off, base vs head with the flag.
  • W (wear, Vulkan): -m <27B> --spec-type draft-mtp -ngl 99 -fa on -c 65536 -np 2 -rea off, default --ctx-checkpoints 32 and --checkpoint-min-step. 2 conversations x 8 turns: turn 0 is a 9k-token document, turns 1-3 add about 2.3k tokens each, turns 4-7 are short, 48 tokens out each. Before each side: sync, then 180 s idle, read field 7 of /sys/block/<nvme>/stat. After stopping the server: sync, wait 35 s, read it again. smartctl -a Data Units Written at the start and the end of the whole test.

Results

A, prompt tokens computed per request (identical on base, head and head with the flag, on Vulkan and ROCm; the greedy text was identical within each backend):

r1 r2 r3 r4 (after slot restore) r5 r6 r7 (from prompt cache)
7502 27 517 518 9527 12975 515

A, memory and files at the end (15 checkpoints created, 3 restored on every side):

backend side RssAnon MiB ckpt fds dir after kill -9 write_bytes cancelled_write_bytes
Vulkan master 3363 0 - 4 KB 0
Vulkan head 3363 0 - 4 KB 0
Vulkan head + flag 2166 8 empty 2.82 GB 1.26 GB
ROCm master 3561 0 - 0 0
ROCm head 3561 0 - 0 0
ROCm head + flag 2364 8 empty 2.82 GB 1.26 GB
ROCm HLB=1 head + flag 2403 8 empty 2.82 GB 1.26 GB

The slot save files of master, head and head with the flag were byte-identical on both backends.

B, restore with a different draft (prompt tokens of the first rewind after the restore):

run Vulkan, 27B CPU, LFM2-700M
head, no flag 518 ("draft context checkpoint data ... dropped") 518
head, flag 7505 ("failed to load context checkpoint restored from a slot file", full reprocess) 7638
fix, flag 518 518

The next request (r5) was 515 tokens in every run.

C: the same 3017/14/15 prompt tokens and the same text on base and on head with the flag, on both backends. No checkpoints, no abort.

D: cannot create files in --ctx-checkpoints-dir <dir>, exit code 1, on both backends, for both the missing and the read-only directory.

E: with 32 fds, 22 checkpoints stayed in memory with "could not move a context checkpoint" warnings, 0 of 40 HTTP requests failed, and the fd count stayed at 30. The same happened on CPU.

F: server-test-model.py 4 PASS / 0 FAIL and server-test-parallel-tc.py 12/12 on base and on head with the flag, on Vulkan and ROCm. With the flag, the server had 28 checkpoint fds and an empty directory listing after 48 checkpoints.

W, wear (Vulkan, 27B, 16 requests, 46 checkpoints of 149.6 MiB on each side):

side write_bytes NVMe sectors in run idle sectors / 180 s RssAnon end MiB
master 4096 27696 (14 MB) 8872 12077
head 4096 11192 (6 MB) 6672 12077
head + flag 7217283072 7975680 (4.08 GB) 14344 1753
  • SMART Data Units Written over the whole wear test: 10,171,303 -> 10,179,353 (4.12 GB, including the master and head runs and the idle time).
  • With the flag, 4.08 of 7.22 GB (57%) reached the device. The rest was still dirty when the server stopped and closed the files, so the kernel dropped it. /proc/<pid>/io was read before the stop, so its cancelled_write_bytes (4 KB) does not show that drop.
  • In test A, superseded checkpoints were dropped while the server ran: 1.26 of 2.82 GB cancelled, and 1.55 GB (Vulkan) / 0.95 GB (ROCm) reached the device for 7 requests.
  • A CPU run with Granite 4.0 H 1B (checkpoints of about 58 MB) gave 2.79 GB written for 16 requests, of which 1.51 GB reached the device.

Estimate for a long-running server: 157 MB per checkpoint, 451 MB per request, 451 GB per 1000 requests. 255 MB per request is the measured lower bound. At 2000 requests per day that is 0.5-0.9 TB per day. On a 600 TBW drive that is 1% of rated life every 7-12 days (1.8-3.2 years in total). On a 1200 TBW drive it is 1% every 13-24 days.

Correctness

check method result
claimed scenario, tokens and greedy text A, base vs head vs head + flag, Vulkan and ROCm PASS
slot save file unchanged cmp of the slot files PASS (byte-identical)
ROCm with HIP_LAUNCH_BLOCKING=1 A on head + flag PASS (same as unset)
restore with a different draft B, Vulkan and CPU FAIL with the flag (full reprocess); the fix passes
dense model unaffected C, both backends PASS
bad directory D, both backends PASS (clean error)
fd exhaustion E, Vulkan and CPU PASS (falls back to memory)
server scripts F, server-test-model.py, server-test-parallel-tc.py PASS, same counts
server pytest suite not run not available in our environment
logit parity not required no compute or sampling change
disk I/O error path not run needs a faulty device; static finding only

Performance

Not a performance PR. Run A took 120.7 s with the flag vs 120.2 s without on Vulkan, and 132.6 s vs 132.5 s on ROCm. A slot restore with the flag took 223 ms vs 153 ms on Vulkan (single runs).

Findings in full

  • server-context.cpp:3037: the draft test-load and the llama_memory_seq_rm on the draft context are skipped for spilled checkpoints. Reproduced (B).
  • SSD writes per checkpoint and per request (W). Every checkpoint is written at creation, synchronously, on the main loop, before the next decode.
  • server-context.cpp:4105-4108: an I/O error on a checkpoint with id_task != -1 aborts the server.
  • common.cpp:2438-2444: mkstemp, then unlink, then fcntl(FD_CLOEXEC). A child spawned from another thread in that window inherits the fd. Return values are not checked.
  • server-context.cpp:2962-2979: an extra copy on the default save path, and return 0; in a bool function.
  • Windows: spill() returns false, the probe fails, and the server exits.
  • One fd per distinct checkpoint. The prompt cache can hold many small (SWA) checkpoints under --cache-ram. The fallback works (E), but other opens in the server compete for the same fds.
  • --cache-ram still counts spilled checkpoints at full size.
  • A tmpfs directory saves no RAM (the pages move from anonymous to shared memory). The help text should say so.
  • load_tgt checks file while load_dft checks file && file_size_dft > 0. file_off_dft always equals file_size_tgt.
  • Upstream ggml-org#30137 (checkpoints as common_state_data in shared_ptr lists) conflicts with about 4 hunks here. std::vector<uint8_t>().swap(data_tgt) will not compile after it.
  • tools/server/README.md is generated from arg.cpp, and it is not updated.
  • No test.

Claims

  • Checkpoint state moves to unlinked files, and anonymous RSS drops: reproduced (A: 3363 -> 2166 MiB Vulkan, 3561 -> 2364 MiB ROCm; W: 12077 -> 1753 MiB).
  • Same greedy tokens and the same tokens computed with and without the flag: reproduced for your scenario; not true for a restore with a different draft (B).
  • Rewind from a checkpoint restored from a slot file and from the prompt cache: reproduced (r4 518, r7 515).
  • Nothing left after kill -9: reproduced (empty directory).
  • Without the flag nothing changes: reproduced at runtime. Statically, the slot save path now copies each checkpoint once more.
  • No effect on Windows: not tested. Statically it is not true: the server exits.
  • About 50-100 ms per checkpoint read from NVMe: not measured separately.

Not tested

The server pytest suite (not available in our environment), Windows and macOS, SWA-only models, a real disk I/O error or a full disk (no root to make a small filesystem), the draft-mismatch case on ROCm (the logic does not depend on the backend), and RAM pressure with a page cache under load.

@dzannotti dzannotti added risk:medium Risk: medium verified The claims were reproduced. waiting-user We asked the author for something. and removed verifying Building and running the PR to verify its claims. labels Nov 10, 2026
rcfa added a commit to rcfa/strix-llama.cpp that referenced this pull request Nov 12, 2026
- The slot-file restore test-loads the draft state of one checkpoint and drops the draft data of all of
  them when it does not fit. It checked data_dft, which is empty once the checkpoint is in its file, so
  the test never ran: a restore with another draft or draft KV type failed at the first rewind and
  reprocessed the whole prompt. It now checks size_dft().
- A checkpoint read back from its file can fail on a disk error. The rewind aborted for every checkpoint
  this server made; it now aborts only for one held in memory, and otherwise reprocesses the prompt.
- The file is created with mkostemp(O_CLOEXEC), so a child process that another thread spawns between
  mkstemp() and fcntl() cannot inherit it, and a failed unlink() gives up instead of leaving a named file.

Assisted-by: Claude Code
@dzannotti dzannotti assigned dzannotti and unassigned rcfa Nov 12, 2026
@dzannotti dzannotti removed the waiting-user We asked the author for something. label Nov 12, 2026
@dzannotti dzannotti assigned rcfa and unassigned dzannotti Nov 13, 2026
@dzannotti dzannotti added the waiting-user We asked the author for something. label Nov 13, 2026
rcfa added a commit to rcfa/strix-llama.cpp that referenced this pull request Nov 16, 2026
- The slot-file restore test-loads the draft state of one checkpoint and drops the draft data of all of
  them when it does not fit. It checked data_dft, which is empty once the checkpoint is in its file, so
  the test never ran: a restore with another draft or draft KV type failed at the first rewind and
  reprocessed the whole prompt. It now checks size_dft().
- A checkpoint read back from its file can fail on a disk error. The rewind aborted for every checkpoint
  this server made; it now aborts only for one held in memory, and otherwise reprocesses the prompt.
- The file is created with mkostemp(O_CLOEXEC), so a child process that another thread spawns between
  mkstemp() and fcntl() cannot inherit it, and a failed unlink() gives up instead of leaving a named file.

Assisted-by: Claude Code
@rcfa

rcfa commented Nov 16, 2026

Copy link
Copy Markdown
Author

Agreed on the direction, and on the write cost being the thing to fix rather than to document away.

Asks 1, 3 and 4 are fixed and pushed to this branch. Each was a real bug, and none of them depends on the
redesign below, so they are not waiting for it.

Pushed as 484306365, the two commits cherry-picked from our main onto this branch. On a CPU build of it,
test_slot_save.py -k "checkpoint or ckpt" is 11 of 11 green, including
test_slot_restore_checkpoints_draft_kv_type_change[q8_0-ckpt_dir] and the new
test_ctx_checkpoint_read_error_reprocesses.

  1. The slot-file restore test-loads the draft data when size_dft() > 0, instead of testing
    checkpoints.back().data_dft.empty(), which is always true after a spill. Restoring with a q8_0 draft cache,
    test_slot_restore_checkpoints_draft_kv_type_change with the directory set reprocessed all 210 prompt tokens
    before the fix, and rewinds after it.
  2. A failed read of a checkpoint file now falls back to reprocessing the prompt; only a checkpoint held in memory
    still aborts. The new test preloads a pread() that fails with EIO for the checkpoint files. Before the fix the
    server aborted in pre_decode.
  3. The file is created with mkostemp(..., O_CLOEXEC), and a failed unlink() gives up instead of leaving a named
    file behind. No test covers the race itself.

Ask 2 needs the rework, and I would rather do it once, after the fork has synced upstream ggml-org#30137. That change
makes checkpoints std::list<std::shared_ptr<const common_prompt_checkpoint>> over a no-init buffer, which is
exactly the storage a cap has to manage; writing the cap against today's container means writing it twice.

The new version keeps checkpoints in memory up to a RAM cap and writes only what does not fit, oldest first.
A server with memory to spare then writes nothing at all, so your 451 MB per request becomes 0, and the
0.9 TB/day you project at 2000 requests/day never happens. On a small unified-memory box the cap still does the job
this PR was opened for, which is keeping checkpoint memory out of RAM. The help text and the regenerated server
README will state the write cost. It will come as one commit with a round-trip test, together with ask 5 and the
Windows probe exits.

For scale at the other end: on our Strix Point box (4 slots, low traffic) llama-server wrote at most 32 GB a day
in all, checkpoints included, and it lands on an NVMe special vdev, so this PR as it stands costs us nothing
measurable. That is why the unconditional spill looked acceptable from here. Your request rate is the case the cap
is for, and a cap serves both.

I am converting this to a draft until that commit is ready.

Written by Claude Code (an AI agent) for rcfa; rcfa owns it.

@rcfa
rcfa marked this pull request as draft November 16, 2026 01:27
@rcfa

rcfa commented Nov 20, 2026

Copy link
Copy Markdown
Author

Pushed 97ec4eb32 (still one commit on master b9f3c0e87): test_slot_restore_checkpoints_draft_kv_type_change now also checks the byte counts that save and restore report. n_written must equal the file size and n_read must equal n_written, with the checkpoints in files and in memory. A save that writes a checkpoint read back from its file without counting it now fails the two directory cases; the previous version of the test passed it. test_slot_save.py and test_kv_keep_only_active.py: 30 passed (CPU build, gfx1150 host).

Written by Claude Code (an AI agent) for rcfa; rcfa owns it.

@dzannotti dzannotti added in-review We are reviewing. and removed waiting-user We asked the author for something. labels Nov 21, 2026

@dzannotti dzannotti left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict

Changes requested, for one line. The RAM cap works as described on Vulkan and ROCm, and greedy output with checkpoints read back from files is byte-identical to master. But the prompt cache still admits entries by the full checkpoint size, so --cache-ram does not yet count only what is in memory, as the description says.

Direction

Accept after work. The cap answers our earlier write-cost concern: with the default 8192 MiB cap our multi-turn workload wrote nothing, and with a 400 MiB cap only 9 of 30 checkpoints went to disk. The five asks of our last review are fixed. We rebased the branch onto current master (no conflicts).

Asks

  1. Blocking. tools/server/server-slot-sched.cpp:98: alloc() sums ckpt->size(), while server_prompt_cache_state::size() now sums size_mem(). The admission check and the make-room loop therefore count checkpoints that are in files. With --cache-ram 800 --ctx-checkpoints-ram 0, a prompt that holds 678 MiB in memory was skipped as "977.572 MiB exceeds cache size limit", and the return to that conversation reprocessed 8525 tokens instead of 632 (TTFT 23.8 s instead of 2.3 s). Use size_mem() there too.
  2. Should fix. tools/server/server-slot-sched.h:24 says a checkpoint does not change after it is created. hold_checkpoint() now moves the state of checkpoints that the prompt cache holds as shared_ptr<const ...>. This is safe because only the main loop calls it. Update the comment to say that, so that nobody later reads checkpoints off the main loop.
  3. Should fix. server-context.cpp:1883 and :3057 do not say why a write failed. Log strerror(errno) from spill(), so that an operator can tell a full disk from a permission problem.
  4. Nits: server-context.cpp:3200 calls hold_checkpoint() before the pop_front() that trims the list, so a slot file with more than --ctx-checkpoints checkpoints writes some that are dropped at once. common.cpp:2658 clear_tgt() sets file_size_tgt = 0, but copy_dft() uses it as the offset of the draft data (no caller today). common.cpp:2619/2643 allocate zero-filled std::vector<uint8_t>; common_state_data skips the fill. When unlink() fails, an empty named file stays in the directory, which the comment at :2513 does not say. common.h:737 has an extra blank line.

We pushed the rebase to your branch. Before you push again, run git fetch && git reset --hard origin/pr/ckpt-spill.

Correctness

PASS for the RAM cap, the file round trip and the error paths; FAIL for the --cache-ram claim (ask 1). On the 27B hybrid model, 14 greedy requests (append, rewinds, a return through the prompt cache, repeated rewinds to one checkpoint) gave the same text and the same prompt/cached token counts on master (twice), on the PR without the flag, and on the PR with cap 400 MiB and cap 0, on Vulkan and on ROCm. MTP (draft state in the file after the target state) is the same. A pread that fails with EIO or returns 0 falls back to reprocessing, and the output is still identical. Short reads and writes give identical output. ENOSPC keeps the checkpoints in memory. test-llama-context rollback is 16 passed on both sides, and 16 fail when we zero the data read from the file. test_slot_save.py and test_kv_keep_only_active.py: 25/25 on master, 30/30 on the PR. The full server unit suite has the same 185 failures on both sides (models not available offline). Logit parity does not apply (no compute change).

Performance

restore of a 149.6 MiB checkpoint, 516-token rewind Vulkan ROCm
state load, from memory 6-9 ms 4-5 ms
state load, from file (page cache or NVMe) 54-73 ms 64-83 ms
TTFT median, from memory 1310 ms 1505 ms
TTFT, cap 0, file in page cache / dropped to NVMe 1369-1399 / 1360-1369 ms 1599-1621 / 1561-1588 ms

The cap-0 TTFT also includes writing the request's new checkpoints, about 30-40 ms per 150 MiB.

Verification

Strix Halo (gfx1151), Vulkan and ROCm. We rebased the PR onto master 1fe05fa45 (7 commits behind, no conflicts) and pushed it to the branch. We compared it with that master.

Disk writes / SSD wear

Same 14-request workload, Vulkan, one slot, about 2 checkpoints of 149.6 MiB per prompt:

master default cap cap 400 MiB cap 0
checkpoints written / created 0 / 30 0 / 30 9 / 30 30 / 30
process write_bytes 1.8 MB (log) 1.8 MB (log) 1.41 GB 4.71 GB
reached the device 2.7 MB idle noise 607 MB 2.04 GB
per request, long-running server 0 0 about 101 MB about 336 MB
2000 requests/day: 1% of a 600 / 1200 TBW drive - - 30 / 60 days 9 / 18 days

Not tested

Windows and macOS, a real failing disk (we injected the errors), and an over-cap workload that runs for hours.

Thank you for the rework and the fault-injection test.

Review made with AI on behalf of the strix-llama.cpp maintainers.

Full verification (for reproduction and agents)

Environment

  • Ubuntu 26.04.1 LTS, kernel 7.0.0-43-generic, Strix Halo (Radeon 8060S, gfx1151).
  • ROCm 10.1 (hipconfig 7.16.26385), AMD clang 24.0.0git d6f6cb69.
  • Mesa RADV 26.0.8 (RADV STRIX_HALO).
  • cmake 4.2.3, gcc 15.2.0, Ninja, Python 3.14.4, pytest 8.3.5 (tools/server/tests/requirements.txt in a venv).
  • Checkpoint directory on ext4 on the local NVMe (2 TB client drive).

Compared

  • master tip: 1fe05fa458c4d3c60957fdbb3a9bcc75e3d85f0c
  • PR head reviewed: 97ec4eb320826078494c9fbb51aa9ccf327e5abd
  • merge base: b9f3c0e878f82f80ea1528969f305017a909b73e, 7 commits behind master
  • rebased head (tested, pushed): a5aaf357b0e291abf3c95017b7bd6eece3f2f65a

Rebase

git rebase origin/master applied the one commit without conflicts. range-diff: 97ec4eb32 = 1: a5aaf357b (identical patch). Pushed with a lease on 97ec4eb32. Before you push again: git fetch && git reset --hard origin/pr/ckpt-spill.

Build

cmake -G Ninja -S <tree> -B <tree>/build-vk  -DCMAKE_BUILD_TYPE=Release -DGGML_VULKAN=ON -DGGML_HIP=OFF
cmake -G Ninja -S <tree> -B <tree>/build-hip -DCMAKE_BUILD_TYPE=Release -DGGML_HIP=ON -DGGML_VULKAN=OFF -DGPU_TARGETS=gfx1151 -DCMAKE_HIP_COMPILER=$(hipconfig -l)/clang -DCMAKE_PREFIX_PATH=$(hipconfig -R)
cmake -G Ninja -S <tree> -B <tree>/build-cpu -DCMAKE_BUILD_TYPE=Release -DGGML_VULKAN=OFF -DGGML_HIP=OFF
cmake --build <tree>/build-<be> -j 16 --target llama-server test-llama-context test-llama-archs llama-completion

All six builds were clean. No new warnings.

Models

model file sha256
Qwen3.8-27B (hybrid, MTP head) Qwen3.8-27B-UD-Q4_K_XL.gguf 3f227079003add2511437e5b1e94812e363385225bf6a9b47b0054a72bc8b01e
LFM2-350M (startup checks, CPU) LiquidAI/LFM2-350M-GGUF / LFM2-350M-Q4_K_M.gguf a4d000c7064bd3b2e42c6845836286a899a4e79cf1791da1a6797b58d575957d
server unit tests the suite's own models (tinygemma3 and others) -

Commands

Server, every run: llama-server -m <27B> -ngl 99 -fa on -c 32768 -np 1 -rea off -lv 5 --host 127.0.0.1 [--ctx-checkpoints-dir <dir> [--ctx-checkpoints-ram N]] [--spec-type draft-mtp], default --ctx-checkpoints, --checkpoint-min-step and --cache-ram. A client sent streamed /v1/chat/completions requests (temperature 0, top_k 1, seed 1, id_slot 0, cache_prompt), and recorded the text, the time to the first content token, timings.prompt_n / cache_n, /proc/<pid>/status RssAnon, /proc/<pid>/io, and the llama-ckpt-* links in /proc/<pid>/fd with their size and mode.

Workload ("full", 14 requests; A = the first 36000 characters of tests/corpus/correctness-prose.txt, B = the first 30000 of correctness-code.txt, A/2 = the first half of A):

  1. A + question (8409 tokens); 2. append an answer and a follow-up; 3. A + a different question (rewind); 4. B + question (A goes to the prompt cache); 5. back to conversation A with another follow-up (from the prompt cache, with a rewind); 6. A/2 + question; 7-10. A/2 + a new question, 4 times (rewinds to the same checkpoint); 11-14. the same, but before each request the client opens each checkpoint fd through /proc/<pid>/fd/<n>, calls fsync and posix_fadvise(DONTNEED), so that the read comes from the NVMe (read_bytes +156.9 MB per request confirms it).

Workload ("fault", 3 requests): A/2 + question, then two rewinds with other questions. Errors were injected with an LD_PRELOAD library that changes pread/pwrite on llama-ckpt- fds only: eio (pread fails with EIO), eof (pread returns 0), short (every read and write returns a third of the request), enospc_after=N (pwrite fails with ENOSPC after N bytes), zero_write (pwrite returns 0).

Disk writes: before each run sync, read field 7 of /sys/block/<nvme>/stat, wait 60 s, read it again (idle baseline). After the server stopped: sync, wait 35 s, read it again.

Startup: --ctx-checkpoints-dir set to a missing directory, a directory with mode 500, and a regular file; --ctx-checkpoints-ram -5.

Tests (CPU build): ctest -R 'test-generate-models|^test-llama-context$'; test-llama-context --models <dir> --test 'rollback$'; pytest unit/test_slot_save.py unit/test_kv_keep_only_active.py with the tests of each side, and the master tests against the PR binary; pytest unit with HF_HUB_OFFLINE=1. Mutation: copy_tgt() zeroes the buffer after the read from the file.

Results

Full workload, all rows the same text and the same prompt_n / cache_n as master on the same backend:

run ckpt fds at end file MiB RssAnon end MiB written / created write_bytes cancelled_write_bytes dir after exit
Vulkan master (x2) 0 0 1617 0 / 30 1.83 MB 0 -
Vulkan PR, no flag 0 0 1617 0 / 30 1.83 MB 0 -
Vulkan PR, default cap 0 0 1617 0 / 30 1.83 MB 4 KB empty
Vulkan PR, cap 400 3 449 1168 9 / 30 1.41 GB 0.94 GB empty
Vulkan PR, cap 0 5 748 869 30 / 30 4.71 GB 2.51 GB empty
ROCm master 0 0 1817 0 / 30 2.24 MB 0 -
ROCm PR, no flag 0 0 1817 0 / 30 2.24 MB 0 -
ROCm PR, cap 400 3 449 1368 9 / 30 1.41 GB 0.94 GB empty
ROCm PR, cap 0 5 748 1069 30 / 30 4.71 GB 2.51 GB empty
Vulkan MTP master / PR no flag 0 0 1810 / 1813 0 / 30 2.2 MB 0 -
Vulkan MTP PR, cap 0 / cap 400 5 / 3 748 / 449 1062 / 1362 30 / 9 4.71 / 1.41 GB 2.35 / 0.94 GB empty
  • With cap 400, the checkpoint memory after each write was at most 299.3 MiB (2 checkpoints), from the debug line of hold_checkpoint(). Requests 3 and 5 read 156.9 MB from the files (rchar), so they restored written checkpoints.
  • All checkpoint fds pointed to (deleted) files with mode 0600. The directory was empty while the server ran and after it exited.
  • write_bytes for master and the default cap is the server log, which went to a file.

TTFT (ms) per request, Vulkan / ROCm:

request master PR no flag PR cap 400 PR cap 0
1 (8409 tokens) 17200 / 20856 17513 / 20851 17503 / 20874 17586 / 20942
2 append 546 / 432 553 / 433 638 / 539 642 / 549
3 rewind 1359 / 1621 1368 / 1617 1392 / 1618 1451 / 1668
5 from prompt cache 2175 / 2268 2176 / 2265 2295 / 2407 2303 / 2409
8-10 rewinds 1301-1313 / 1494-1507 1304-1311 / 1505-1508 1301-1315 / 1500-1511 1369-1399 / 1599-1621
11-14 rewinds, NVMe 1283-1312 / 1501-1504 1308-1313 / 1501-1506 1282-1312 / 1459-1509 1360-1369 / 1561-1588

Time from "new prompt" to "restored context checkpoint" in the server log: Vulkan 5.7-8.7 ms from memory, 53.7-72.6 ms from a file; ROCm 3.9-5.1 ms and 64.1-82.8 ms. Single runs per configuration; the rewinds repeat 3-4 times each.

Fault injection (Vulkan, PR, cap 0; "same" = same text as the PR without the flag):

fault request 1 rewinds (prompt_n / cache_n) result
none 2 files 512 / 3976, 510 / 3976 same
eio 2 files 4488 / 0, 4486 / 0, "failed to load context checkpoint from --ctx-checkpoints-dir" same, server alive
eof 2 files 4488 / 0, 4486 / 0, same warning same, server alive
short 2 files 512 / 3976, 510 / 3976 same
enospc_after=1 0 files, "could not move a context checkpoint ... it stays in memory" 512 / 3976, 510 / 3976 same, RssAnon as without the flag
enospc_after=200000000 1 file, the second write failed after 43 MB and its fd was closed 512 / 3976, 510 / 3976 same
zero_write - - startup probe fails: "cannot create files in --ctx-checkpoints-dir", exit 1

Disk writes at the device (sectors of 512 bytes):

run idle 60 s run run, MB
master 208 5216 2.7
PR, default cap 60672 52296 26.8 (less than the idle noise in this window; 0 checkpoints written)
PR, cap 400 592 1185976 607
PR, cap 0 1032 3991216 2043

In these short runs the server exited about 1-3 minutes after the last write, so part of the data never reached the device. Per request, a long-running server writes about write_bytes / 14: 101 MB with cap 400 and 336 MB with cap 0. At 2000 requests per day that is 0.2 TB and 0.67 TB per day. 1% of a 600 TBW drive is 6 TB: 30 days and 9 days. With the default 8192 MiB cap this workload writes nothing; a server writes only after about 54 checkpoints of this size are in memory.

--cache-ram 800, cap 0, Vulkan: "prompt state size 977.572 MiB exceeds cache size limit 800.000 MiB, skipping" twice; the same entry is listed as 678.320 MiB by update() in the cap-0 run with the default cache. Request 5 then computed 8525 prompt tokens (cache_n 0, TTFT 23.8 s) instead of 632 (2.3 s).

Startup: a missing directory, a directory with mode 500 and a regular file each give "cannot create files in --ctx-checkpoints-dir

" and exit 1. --ctx-checkpoints-ram -5 is rejected.

Tests: test-llama-context (all cases) passed on both sides; rollback 16 passed, 131 skipped on both; with the mutation, rollback 0 passed, 16 failed ("loading the checkpoint from its file failed"). test_slot_save.py + test_kv_keep_only_active.py: master 25 passed, PR 30 passed, master tests with the PR binary 25 passed. pytest unit: master 520 passed / 185 failed / 8 skipped, PR 525 / 185 / 8, the same 185 failures (184 in test_tool_call.py and 1 in test_completion.py, the server could not load models that were not cached).

Correctness

check method result
greedy output, restore from files vs memory vs master full workload, Vulkan and ROCm, cap 400 and cap 0 PASS, byte-identical
base A/A master twice, Vulkan PASS, identical
draft state in the file MTP, cap 0 and cap 400 PASS, identical
RAM under the cap debug line, RssAnon, fds PASS, at most 299 MiB after each write with cap 400
files unlinked, mode, cleanup /proc/<pid>/fd, directory listing PASS, (deleted), 0600, empty
read error at restore EIO and EOF injection PASS, falls back to reprocessing
short reads and writes injection PASS
disk full ENOSPC injection (no small filesystem available without root) PASS, stays in memory
bad directory, negative cap startup PASS, clean error
--cache-ram counts memory only --cache-ram 800, cap 0 FAIL (ask 1)
round-trip unit test test-llama-context, mutation PASS, catches a broken read
server unit tests pytest, base vs PR PASS, same failures
server-test-*.py scripts not run ran in the previous round; this change does not touch the request path
logit parity not required no compute or sampling change
ROCm with HIP_LAUNCH_BLOCKING=1 not run no GPU code changed; the previous round showed no difference

Findings in full

  • server-slot-sched.cpp:98: alloc() uses size(); server_prompt_cache_state::size() uses size_mem() (ask 1, measured).
  • server-slot-sched.h:24: the comment that checkpoints do not change is out of date. ckpt_in_mem holds weak_ptr<common_prompt_checkpoint> to objects that the slot and the cache hold as shared_ptr<const>. All calls to hold_checkpoint() are on the main loop; the tasks that run while the main loop yields (metrics, slot info, dashboard) do not touch checkpoint data.
  • server-context.cpp:1883, :3057: no errno in the messages.
  • server-context.cpp:3200: write before trim on slot restore.
  • common.cpp:2658: clear_tgt() and the draft offset.
  • common.cpp:2619/2643: zero-filled temporary buffer, 149.6 MiB per restore, not counted by the cap.
  • common.cpp:2510-2518: mkostemp with O_CLOEXEC gives 0600 and O_EXCL; the name exists only until unlink(). If unlink() fails, an empty file stays. O_TMPFILE on Linux avoids the name.
  • A spill runs on the main loop. Writing a 149.6 MiB checkpoint to the page cache took about 30-40 ms, and all slots wait for it.
  • After a rewind, the checkpoint at the same n_tokens is created again and, over the cap, written again (master supersedes it the same way). This is the "each new checkpoint once" cost in the description.
  • Security: the directory comes from the operator, the files are 0600 and unlinked, the fd has O_CLOEXEC, and the shared_ptr closes it with the last checkpoint that uses it. pread/pwrite loop over short transfers and EINTR, and treat 0 as an error. Nothing in the diff, the tests or the description downloads anything or writes outside the test and checkpoint directories.

Claims

  • Checkpoints stay in memory up to the cap, and the oldest beyond it go to unlinked files: reproduced (cap 400: 9 of 30 written, at most 299 MiB in memory).
  • A server with memory to spare writes nothing: reproduced (default cap: 0 checkpoints written, write_bytes the same as master).
  • --ctx-checkpoints-ram 0 writes every checkpoint: reproduced (30 of 30).
  • A checkpoint shared by a slot and the prompt cache is written once: reproduced (request 5 restored a written checkpoint from the prompt cache; 9 writes for 30 checkpoints).
  • A failed write leaves the checkpoint in memory: reproduced (ENOSPC injection).
  • --cache-ram counts size_mem(): partly. The cache total does, but admission and make-room do not (ask 1).
  • A failed read falls back to reprocessing: reproduced (EIO and EOF).
  • The draft test-load uses size_dft(): covered by test_slot_restore_checkpoints_draft_kv_type_change[*-ckpt_dir], which passed.
  • A slot save writes in-memory checkpoints directly: reproduced by the byte-count asserts in the same test.
  • Windows ignores the flag with a warning: not tested (static: the directory is cleared at startup).
  • The round-trip test fails when the file read is broken: reproduced (mutation, 16 of 16 fail).

Not tested

Windows and macOS (no machine). A real disk error or a real full filesystem (no root for a small loop or tmpfs device; errors were injected with LD_PRELOAD). An over-cap workload that runs for hours. More than one slot.

@dzannotti dzannotti added waiting-user We asked the author for something. and removed in-review We are reviewing. verified The claims were reproduced. labels Nov 21, 2026
@rcfa

rcfa commented Nov 21, 2026

Copy link
Copy Markdown
Author

Pushed 1aeb029f0 on your rebase a5aaf357b (master 1fe05fa45), still one commit, force-pushed with a lease on
a5aaf357b.

  1. Ask 1. alloc() sums size_mem(), so the admission check and the make-room loop count what the cache total
    counts. New test test_cache_ram_counts_checkpoints_in_memory: --cache-ram 5, every checkpoint in a file, and a
    first prompt that holds 3.4 MiB in memory and 8.6 MiB in all. With the old line it fails ("prompt state size
    8.619 MiB exceeds cache size limit 5.000 MiB", cache_n 0); with the fix it passes. Its third request is not
    pinned to a slot: a request pinned to an empty slot does not look in the prompt cache, on master either.
  2. Ask 2. The comment in server-slot-sched.h now says that after creation only hold_checkpoint() changes a
    checkpoint, on the main loop, and that checkpoints are read only there.
  3. Ask 3. spill() leaves errno set when it fails: it keeps errno across the close(), and a pwrite() that
    returns 0 gives EIO. Both messages print strerror(). Checked by hand, with a preloaded pwrite() for the last
    two: "cannot create files in --ctx-checkpoints-dir : No such file or directory", "...: Permission denied"
    (mode 500), "...: Input/output error" (a write that returns 0), and "could not move a context checkpoint to
    (No space left on device) - it stays in memory".
  4. Nits.
    • Slot restore: hold_checkpoint() runs after the trim to --ctx-checkpoints and after the draft test-load, so a
      dropped checkpoint or dropped draft data is never written.
    • clear_tgt(): the draft offset is kept in file_off_dft. I removed it as redundant in the last round;
      clear_tgt() makes it necessary. case_rollback now writes a target and a draft state, calls clear_tgt() and
      reads the draft back. With the old offset all 16 cases fail.
    • The two reads from a file use common_state_data, so the buffer is not zero-filled first.
    • The unlink() comment says that an empty named file stays when it fails. O_TMPFILE would avoid the name on
      Linux; I left it out to keep one code path and its fallbacks as they are. Say if you want it.
    • The blank line in common.h is gone.

Verification (Strix Point, Ryzen AI 9 HX PRO 375 / Radeon 890M, gfx1150; not Strix Halo), CPU build, Release,
gcc 16.2:

  • test_slot_save.py and test_kv_keep_only_active.py: 31 passed.
  • test-llama-context --test rollback$: 16 passed, 131 skipped, 0 failed.
  • The server test files that use --cache-ram or the checkpoint options (test_completion, test_kv_keep_only_active,
    test_lora, test_rerank, test_slot_save, test_systemone): 95 passed, 19 failed. The 19 are the 18 rerank
    tests, whose model is not in our offline cache, and test_completion_stream_with_openai_library_stops, which
    failed on master here before. I did not rerun them without this change.

Not verified: Vulkan and ROCm runs of this change.

Written by Claude Code (an AI agent) for rcfa; rcfa owns it.

@dzannotti dzannotti added in-review We are reviewing. and removed waiting-user We asked the author for something. labels Nov 22, 2026

@dzannotti dzannotti left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict

Approve. alloc() now counts checkpoints the same way as the cache total, and the return to a cached conversation reuses its checkpoints when the checkpoints are in files. Output stays byte-identical to master on Vulkan and ROCm.

Direction

Accept. This replaces our request-changes review of the previous head.

Asks

  1. None block the merge. We rebased onto master 2c3dc199d and pushed, so run git fetch && git reset --hard origin/pr/ckpt-spill before you push again.

Each point from our last review is done in 1aeb029f0: alloc() sums size_mem(), the server-slot-sched.h comment is updated, both warnings print strerror(), hold_checkpoint() runs after the trim and the draft test-load on slot restore, and file_off_dft keeps the draft offset after clear_tgt(). We do not need O_TMPFILE.

One correction to our last review. With --cache-ram 800, the two prompts in our workload hold 678 and 718 MiB in memory, so the cache cannot hold both of them, even when it counts only memory. The return to the first conversation then reprocesses the prompt on master and on this PR, and that is correct. We reran the repro with --cache-ram 1500, which holds both entries in memory (1397 MiB) but not at full size (1995 MiB).

Correctness

PASS. On the 27B hybrid model, 14 greedy requests (append, rewinds, a return through the prompt cache, repeated rewinds to one checkpoint) gave the same text and the same prompt and cached token counts on master and on the PR with cap 400 MiB and cap 0, on Vulkan and on ROCm. Master A/A on Vulkan is identical. MTP with cap 0 is identical to MTP on master. test-llama-context: all cases pass on both sides, and rollback has 16 passed. test_slot_save.py and test_kv_keep_only_active.py: master 26/26, PR 32/32, and the master tests with the PR binary 26/26. When we revert the alloc() line, test_cache_ram_counts_checkpoints_in_memory fails with "8.619 MiB exceeds cache size limit 5.000 MiB", so the new test catches the bug.

Performance

--cache-ram 1500, every checkpoint in a file, request 5 returns to the first conversation:

backend side prompt tokens computed cached TTFT
Vulkan master 8525 0 19.10 s
Vulkan PR, cap 0 632 7893 2.29 s
ROCm master 8524 0 21.95 s
ROCm PR, cap 0 631 7893 2.38 s

With the default cache size, the same request computes 632 tokens in 2.30 s on the PR with cap 0. Without --ctx-checkpoints-dir the code path is the same as on master.

Verification

Strix Halo (gfx1151). We compared master 2c3dc199d with this PR rebased onto it (05b4b2cd7, 2 commits behind before the rebase), on Vulkan and on ROCm.

Not tested

The server unit suite beyond the two slot and checkpoint files, and Windows.

Thanks for the quick and complete follow-up.

Review made with AI on behalf of the strix-llama.cpp maintainers.

Full verification (for reproduction and agents)

Environment

  • Ubuntu 26.04.1 LTS, kernel 7.0.0-43-generic, Strix Halo (Radeon 8060S, gfx1151).
  • ROCm 10.1 (hipconfig 7.16.26385), AMD clang 24.0.0git d6f6cb69.
  • Mesa RADV 26.0.8 (RADV STRIX_HALO).
  • cmake 4.2.3, gcc 15.2.0, Ninja, Python 3.14.4, pytest 8.3.5 (tools/server/tests/requirements.txt in a venv).
  • Checkpoint directory on ext4 on the local NVMe.

Compared

  • master tip: 2c3dc199d2a6242ddfbb595a3c61d018fa1c5a13
  • PR head reviewed: 1aeb029f073a522a365f6f1c40f1dd198f5d57f9 (on our previous rebase a5aaf357b, master 1fe05fa45)
  • rebased head (tested, pushed): 05b4b2cd7f29cde1cd895bbbf507c1e2bb0bd93f
  • commits behind master before the rebase: 2 (d2c1dea57, test only)

Rebase

Conflict in tools/server/tests/unit/test_slot_save.py only: master (d2c1dea57) and the PR both append tests at the end of the file. Resolution: the PR's file, then master's SLOT_DRAFT_MAGIC and test_slot_save_restore_draft_state_bytes appended unchanged. The rebased tree differs from 1aeb029f0 only by those 49 lines from master. range-diff 1fe05fa45..1aeb029f0 vs origin/master..05b4b2cd7: the only change is where the new tests sit relative to master's test. Pushed with a lease on 1aeb029f0.

Changes in 1aeb029f0 against a5aaf357b (range-diff): server-slot-sched.cpp alloc() uses size_mem(); server-slot-sched.h comment; common.cpp spill() keeps errno across close()/reset(), pwrite() returning 0 sets EIO, file_off_dft, common_state_data read buffers, unlink() comment; server-context.cpp strerror() in both messages, hold_checkpoint() moved after the trim and the draft test-load; cases-rs.cpp draft read after clear_tgt(); test_slot_save.py test_cache_ram_counts_checkpoints_in_memory; common.h blank line.

Build

cmake -G Ninja -S <tree> -B <tree>/build-vk  -DCMAKE_BUILD_TYPE=Release -DGGML_VULKAN=ON -DGGML_HIP=OFF
cmake -G Ninja -S <tree> -B <tree>/build-hip -DCMAKE_BUILD_TYPE=Release -DGGML_HIP=ON -DGGML_VULKAN=OFF -DGPU_TARGETS=gfx1151 -DCMAKE_HIP_COMPILER=$(hipconfig -l)/clang -DCMAKE_PREFIX_PATH=$(hipconfig -R)
cmake -G Ninja -S <tree> -B <tree>/build-cpu -DCMAKE_BUILD_TYPE=Release -DGGML_VULKAN=OFF -DGGML_HIP=OFF
cmake --build <tree>/build-<be> -j 16 --target llama-server test-llama-context test-llama-archs llama-completion

All six builds (master and PR, three backends) were clean.

Models

model file sha256
Qwen3.8-27B (hybrid, MTP head) Qwen3.8-27B-UD-Q4_K_XL.gguf 3f227079003add2511437e5b1e94812e363385225bf6a9b47b0054a72bc8b01e
server unit tests the suite's own models (tinygemma3 and others) -

Commands

Server: llama-server -m <model> -ngl 99 -fa on -c 32768 -np 1 -rea off -lv 5 [--cache-ram N] [--ctx-checkpoints-dir <dir> --ctx-checkpoints-ram R] [--spec-type draft-mtp].

Workload (streamed /v1/chat/completions, temperature 0, top_k 1, seed 1, id_slot 0, cache_prompt): t1 a 36000-character prose document plus a question; t2 append a turn; t3 the same document with another question (rewind); t4 a 30000-character code document; t5 return to the t2 conversation (through the prompt cache); t6 the first half of the prose document; then 8 requests on that half with different questions (4 of them after fsync and POSIX_FADV_DONTNEED on every checkpoint file). Per step we record the text, prompt_n, cache_n, TTFT and /proc/<pid>/io.

Fault injection: an LD_PRELOAD shim on pwrite() for files named llama-ckpt-*.

Tests:

ctest --test-dir <build-cpu> --output-on-failure -R 'test-generate-models|test-llama-context'
GGML_SCHED_DEBUG_REALLOC=1 <build-cpu>/bin/test-llama-context --models <build-cpu>/tests/test-models/ --test 'rollback$'
LLAMA_SERVER_BIN_PATH=<build-cpu>/bin/llama-server python -m pytest -v unit/test_slot_save.py unit/test_kv_keep_only_active.py

Results

Greedy workload, 14 steps, text + prompt_n + cache_n compared with master on the same backend:

run Vulkan ROCm
master A/A identical -
PR, cap 400 MiB identical identical
PR, cap 0 identical identical
PR, cap 0, --cache-ram 1500 identical text, t5 reuses the cache (see table) identical text, t5 reuses the cache
MTP draft, PR cap 0 vs master identical -

Request 5 (return through the prompt cache):

backend run prompt_n cache_n TTFT
Vulkan master, default cache 632 7893 2.17 s (previous round)
Vulkan PR, cap 0, default cache 632 7893 2.30 s
Vulkan master, --cache-ram 1500 8525 0 19.10 s
Vulkan PR, cap 0, --cache-ram 1500 632 7893 2.29 s
ROCm master, --cache-ram 1500 8524 0 21.95 s
ROCm PR, cap 0, --cache-ram 1500 631 7893 2.38 s
Vulkan master, --cache-ram 800 8525 0 18.76 s
Vulkan PR, cap 0, --cache-ram 800 8525 0 19.21 s

Cache log, --cache-ram 1500: master "cache state: 1 prompts, 977.572 MiB", then "making room ... removing oldest entry (size = 977.572 MiB)" for the 1017.587 MiB entry. PR cap 0: entries of 678.320 and 718.335 MiB, no eviction. With --cache-ram 800 the PR admits the 678.320 MiB entry (previous head: "977.572 MiB exceeds cache size limit, skipping") and evicts it for the 718.335 MiB entry, as it should.

Error messages: ENOSPC on the second write gives "could not move a context checkpoint to

(No space left on device) - it stays in memory", and the three requests still complete. A pwrite() that returns 0 makes the startup probe fail with "cannot create files in --ctx-checkpoints-dir : Input/output error", and the server exits 1.

Tests: test-generate-models and test-llama-context passed on both sides; rollback 16 passed, 131 skipped, 0 failed on both. pytest: master 26 passed, PR 32 passed, master tests with the PR binary 26 passed. Mutation (alloc() back to ckpt->size()): test_cache_ram_counts_checkpoints_in_memory fails ("prompt state size 8.619 MiB exceeds cache size limit 5.000 MiB, skipping").

Correctness

check method result
greedy output, files vs memory vs master full workload, Vulkan and ROCm, cap 400 and cap 0 PASS, byte-identical
base A/A master twice, Vulkan PASS, identical
draft state in the file MTP, cap 0 PASS, identical
--cache-ram counts memory only --cache-ram 1500, cap 0, Vulkan and ROCm PASS, 632 tokens instead of 8525
errno in the messages ENOSPC and zero-length write shim PASS
test-llama-context ctest and rollback PASS
server tests test_slot_save.py, test_kv_keep_only_active.py, base vs PR, mutation PASS
default path code read: without --ctx-checkpoints-dir, hold_checkpoint() returns at once, no checkpoint has a file, size_mem() equals size() unchanged
logit parity not required no compute or sampling change
disk writes not rerun unchanged since the previous round: the write path and the cap logic did not change

Findings in full

  • Slot restore now holds all restored checkpoints (at most --ctx-checkpoints) in memory until the loop ends, and then writes the ones beyond the cap. The peak is the same as on master without the flag. Not an ask.
  • server-context.cpp includes <deque> twice. This was already there before this PR.

Claims

  • alloc() counts memory: reproduced (Vulkan and ROCm, unit test with mutation).
  • errno in both messages: reproduced for ENOSPC and EIO.
  • Draft offset after clear_tgt(): covered by case_rollback (passes); MTP end to end is identical.
  • Write after trim on slot restore: code read; the slot tests pass.

Not tested

The full server unit suite (last round: the same failures on both sides, models not cached offline), Windows, and SSD wear (unchanged).

@dzannotti dzannotti added ready-to-merge Review done, low risk, approved. verified The claims were reproduced. and removed in-review We are reviewing. labels Nov 22, 2026
… (--ctx-checkpoints-dir, --ctx-checkpoints-ram)

On a unified-memory machine, context checkpoints compete with the model for RAM. On a hybrid model each one holds
the recurrent state of every layer (112 MiB on Qwen3.8-Flash-Next, 150 MiB on Qwen3.8-27B), up to --ctx-checkpoints
per slot.

--ctx-checkpoints-dir PATH keeps checkpoints in memory while they fit in --ctx-checkpoints-ram (MiB, default 8192)
and writes the oldest beyond that to unlinked files in PATH. A server with memory to spare writes nothing, and a file
is read back only to rewind a slot. --ctx-checkpoints-ram 0 writes every checkpoint.

- common_prompt_checkpoint: spill() moves the target and draft state into an unlinked file (mkostemp with O_CLOEXEC,
  the unlink checked; if unlink() fails, the empty named file stays) and leaves errno set when it fails;
  copy_tgt() / copy_dft() and load_tgt() / load_dft() read it back with pread() into an uninitialized buffer; the
  draft state's offset in the file is kept, so clear_tgt() does not move it; size_mem() is what a checkpoint holds
  in memory.
- The server tracks the checkpoints still in memory, oldest first, through weak references, so a checkpoint shared
  by a slot and the prompt cache (ggml-org#30137) is written once for all holders. Only the main loop writes or reads a
  checkpoint. A failed write leaves the checkpoint in memory, logs why, and is retried at the next checkpoint.
- --cache-ram counts size_mem() in the cache total, in the admission check and when it makes room, so a written
  checkpoint no longer counts at full size.
- A failed read of a checkpoint file falls back to reprocessing the prompt; only a checkpoint held in memory aborts.
- The slot file's draft test-load checks size_dft(), not data_dft, which is empty after a write. The checkpoints of a
  slot file count against --ctx-checkpoints-ram after the trim to --ctx-checkpoints and the draft test-load.
- A slot save writes a checkpoint held in memory directly, without a copy.
- Windows: the flag is ignored with a warning.
- tools/server/README.md: the rows of the two options.

Tests: tests/llama-context case_rollback writes its checkpoint to a file and loads it into a fresh context (logits
match exactly), and reads a draft state from its file after clear_tgt(). test_slot_save.py covers the draft KV type
change with the directory, with the byte counts save and restore report (n_written equals the file size, n_read
equals n_written), a failed checkpoint read (a preloaded pread() that fails with EIO), the cap (default: no file;
0: every checkpoint in a file), and --cache-ram with every checkpoint in a file (a prompt whose state fits is cached
although it does not fit at full size).

Assisted-by: Claude Code
dzannotti and others added 2 commits November 22, 2026 13:12
The type check reads a positional pytest.fail argument as the pytrace
bool, so it failed on this test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01YKmjzcT1LTvKtbw9EzBVGE
…ot file tests

The type check rejects both forms of the pytest.fail call here, so raise
the assertion directly.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@dzannotti
dzannotti merged commit 3b76dee into halo-box:master Nov 22, 2026
15 checks passed
gaetan-puleo added a commit that referenced this pull request Nov 22, 2026
The branch is on the fork master 3b76dee (#180, context checkpoints
written to unlinked files beyond a RAM cap). The import tools regenerate
common, the server and its tests. test-llama-context is built from
several files: import-apps.py copies the whole test when the fork changes
one of them, built as test-llama-context-fork and registered in ctest as
the upstream one (labels, --models, generate-models, the reallocation
check), with the fork switches. PORT.txt lists the 9 new hunks.

check-port against 3b76dee: 64730/64730 lines, 516/516 files. Same as
the fork binary: gpt-oss 5.4741, EXL3 7.4162 / 7.4148 / 7.4151,
GATED_DELTA_NET 69/69; check-ctest 91/91, ctest -L main 39/39,
test-llama-context-fork passes.

Assisted-by: Claude Opus
gaetan-puleo added a commit that referenced this pull request Nov 22, 2026
The branch is on the fork master 3b76dee (#180, context checkpoints
written to unlinked files beyond a RAM cap). The import tools regenerate
common, the server and its tests. test-llama-context is built from
several files: import-apps.py copies the whole test when the fork changes
one of them, built as test-llama-context-fork and registered in ctest as
the upstream one (labels, --models, generate-models, the reallocation
check), with the fork switches. PORT.txt lists the 9 new hunks.

check-port against 3b76dee: 64730/64730 lines, 516/516 files. Same as
the fork binary: gpt-oss 5.4741, EXL3 7.4162 / 7.4148 / 7.4151,
GATED_DELTA_NET 69/69; check-ctest 91/91, ctest -L main 39/39,
test-llama-context-fork passes.

Assisted-by: Claude Opus
rcfa added a commit to rcfa/strix-llama.cpp that referenced this pull request Nov 27, 2026
…lot_sched

ggml-org ggml-org#32154 moved the prompt cache out of server-task.cpp into server-slot-sched.{h,cpp} and the slot
choice out of get_available_slot() into server_slot_sched::select(); ggml-org#30137 shares context checkpoints as
std::shared_ptr<const common_prompt_checkpoint>. Our server stack cannot be replayed onto that, so it is
ported here in one piece, with the behaviour unchanged:

- the persistent prompt cache (--cache-dir, --cache-dir-max: memory-mapped entries with a metadata sidecar,
  owner-only files, state staged through host memory, restore at start, the key by state format and
  without the batch sizes), now on server_prompt_cache in server-slot-sched.cpp
- the idle slots saved to it on exit (server_context::save_slots_to_cache)
- update_prompt_cache(), on top of halo-box#176's purge loop (merged as f2230a8): the load passes the
  checkpoint and similarity arguments, and an entry that fails to restore on an empty pool is discarded
- select(): conversation_id first, slot_match_skip in the similarity, --slot-keep-min; upstream's LRU
  (empty slots first) is kept
- the checkpoint threshold for disk entries now follows server_prompt::get_reuse()

--ctx-checkpoints-dir is upstream's (halo-box#180 as merged, ebe8231, with --ctx-checkpoints-ram). The context
checkpoints that a load builds from a disk entry are new objects, so the prompt cache hands them to the
server (restored_from_disk), which passes them to hold_checkpoint() like the ones a task creates: the
checkpoint budget decides for them too. The earlier port wrote each of them to the directory at once.

The slot file's draft appendix is upstream's (same SDFT magic and version as our halo-box#171, but with the
speculative state as a second buffer), so slot files written by the previous build restore without
their draft state once.

Ports, in their original order: 0d1c648, 6cb2d84, 22ba4d2 (Daniele Zannotti), 19aa31c,
9c6f7d7, c03c65b, 1c4eaac, c722f60, a4e4da8, 6d4cce5, ec3d0fd, d544de2, cf99aa9,
a707cc8. Dropped: 12837c0 and f1ee218 (ggml-org ggml-org#30574 saves the draft state in the slot file).
tools/server/tests: test_prompt_cache_disk.py (with a new test: restored checkpoints follow
--ctx-checkpoints-ram, which fails on the earlier port), test_slot_save.py and test_kv_keep_only_active.py,
44 passed on a CPU build.

Co-authored-by: Daniele Zannotti <d.zannotti@me.com>
Assisted-by: Claude Code
@rcfa
rcfa deleted the pr/ckpt-spill branch November 27, 2026 19:19
rcfa added a commit to rcfa/strix-llama.cpp that referenced this pull request Dec 4, 2026
…lot_sched

ggml-org ggml-org#32154 moved the prompt cache out of server-task.cpp into server-slot-sched.{h,cpp} and the slot
choice out of get_available_slot() into server_slot_sched::select(); ggml-org#30137 shares context checkpoints as
std::shared_ptr<const common_prompt_checkpoint>. Our server stack cannot be replayed onto that, so it is
ported here in one piece, with the behaviour unchanged:

- the persistent prompt cache (--cache-dir, --cache-dir-max: memory-mapped entries with a metadata sidecar,
  owner-only files, state staged through host memory, restore at start, the key by state format and
  without the batch sizes), now on server_prompt_cache in server-slot-sched.cpp
- the idle slots saved to it on exit (server_context::save_slots_to_cache)
- update_prompt_cache(), on top of halo-box#176's purge loop (merged as f2230a8): the load passes the
  checkpoint and similarity arguments, and an entry that fails to restore on an empty pool is discarded
- select(): conversation_id first, slot_match_skip in the similarity, --slot-keep-min; upstream's LRU
  (empty slots first) is kept
- the checkpoint threshold for disk entries now follows server_prompt::get_reuse()

--ctx-checkpoints-dir is upstream's (halo-box#180 as merged, ebe8231, with --ctx-checkpoints-ram). The context
checkpoints that a load builds from a disk entry are new objects, so the prompt cache hands them to the
server (restored_from_disk), which passes them to hold_checkpoint() like the ones a task creates: the
checkpoint budget decides for them too. The earlier port wrote each of them to the directory at once.

The slot file's draft appendix is upstream's (same SDFT magic and version as our halo-box#171, but with the
speculative state as a second buffer), so slot files written by the previous build restore without
their draft state once.

Ports, in their original order: 0d1c648, 6cb2d84, 22ba4d2 (Daniele Zannotti), 19aa31c,
9c6f7d7, c03c65b, 1c4eaac, c722f60, a4e4da8, 6d4cce5, ec3d0fd, d544de2, cf99aa9,
a707cc8. Dropped: 12837c0 and f1ee218 (ggml-org ggml-org#30574 saves the draft state in the slot file).
tools/server/tests: test_prompt_cache_disk.py (with a new test: restored checkpoints follow
--ctx-checkpoints-ram, which fails on the earlier port), test_slot_save.py and test_kv_keep_only_active.py,
44 passed on a CPU build.

Co-authored-by: Daniele Zannotti <d.zannotti@me.com>
Assisted-by: Claude Code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation ready-to-merge Review done, low risk, approved. risk:medium Risk: medium server testing verified The claims were reproduced.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants