mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-03 19:03:30 +07:00
[PR #3957] [Engine] serve: a long conversation survives an engine restart - parked conversations saved at a graceful stop and loaded at the next start, to files or (Windows) kept in RAM with no disk writes (opt-in STRATA_PARK_DIR, STRATA_PARK_RAMLIKE=1… #4854
Labels
No labels
needs-format
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Niko1221/Strata#4854
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/Niko1221/Strata/pull/3957
Author: @ruibeikaa
Created: 10/24/2026
Status: 🔄 Open
Base:
main← Head:park-restart-ram📝 Commits (3)
0f1fd4fserve: parked conversations kept over a restart (opt-in STRATA_PARK_DIR; engine PARKSAVE / PARKLOAD, layer split included)5d50a16serve: STRATA_PARK_RAMLIKE=1 - the parked conversations kept in RAM over a restart, nothing written to a disk (opt-in, Windows)4b2ade7serve: the hand-over to the park keeper is bounded: ready before the page walk, one wait for all of it📊 Changes
10 files changed (+1599 additions, -11 deletions)
View changed files
📝
docs/DETAILS.md(+60 -1)📝
include/strata/core/conversation_cache.hpp(+11 -0)📝
include/strata/core/conversation_file.hpp(+9 -0)➕
serve/park_keeper.py(+342 -0)📝
serve/server.py(+144 -5)➕
serve/test_park_files.py(+153 -0)➕
serve/test_park_ramlike.py(+197 -0)📝
src/core/conversation_file.cpp(+193 -3)📝
src/core/conversation_file_test.cpp(+54 -0)📝
src/program/generate.cpp(+436 -2)📄 Description
What this improves
STRATA_PARK_DIR=<folder>the parked conversations are saved at a graceful stop and put back in the cache at the next start, before the first request. Measured on four GP100 dies (Swift 1.5 GGUF,--layer-split 12,24,36): a 260,834-token conversation that takes 296 s to read from zero was saved in 3.6 s and loaded in 3.2 s, and its next turn after the restart took 7.2 s (260,834 tokens reused, 893 read). The saving grows with the conversation: a re-read costs roughly its length divided by the prompt speed, a load costs its size divided by the disk (or memory) speed.STRATA_PARK_RAMLIKE=1. Saving to files writes several GB per long conversation at every graceful stop, which spends an SSD's write endurance. With the knob, the same session bytes go to named memory sections that a small keeper process holds in RAM from the stop to the next start. For a 103,749-token conversation the stop wrote 3.4 MiB to the system disk instead of 1.94 GB, no page went to the pagefile in the ten minutes after it, and the next turn after the restart reused 103,721 tokens and took 975 ms instead of 94-95 s from zero, with the same reply.SAVE/RESTORE) refuse--layer-split(#3539, #3744); here each stage of a conversation is its own session image, so multi-GPU splits keep their conversations. A single GPU takes the same path.POST /unload, or a switch to another model and back withinSTRATA_PARK_MAX_AGE_H- and most of all long agent sessions (100K-400K tokens) on slower prompt reads. The RAM knob is for Windows users who have the RAM to spare between the stop and the next start and do not want the disk writes.What it costs (all opt-in; unset, nothing is written, kept or read):
STRATA_PARK_MIN_TOKENS(default 16384) and ones parked more thanSTRATA_PARK_MAX_AGE_Hago (default 48 h), and writes only two checkpoints of each. Disk space up to the conversation cache's budget, and a longer stop by the save's time (3.6 s for 5.33 GB on an NVMe SSD).STRATA_PARK_MAX_AGE_H(the keeper then ends by itself). A save is refused when less thanSTRATA_PARK_RAMLIKE_FLOOR_MIB(8192) of commit would be left. Every stop copies every kept conversation again (RAM to RAM, ~1.4 s for 1.94 GB). Lost at a reboot or sign-out, or when the keeper cannot outlive the server (a job that ends its processes and allows no breakaway); then the next start reads the conversations again, as without the change, and says why. Windows only.STRATA_PARK_MIN_TOKENS=50000andSTRATA_PARK_MAX_AGE_H=1: long sessions only, restarts within the hour.)STRATA_*variable exceptSTRATA_PARK_*(plus the session files' own config fingerprint); after an update or a changed switch nothing is loaded and the next stop replaces them.Problem and result
A restart of the engine drops every parked conversation, and the next turn of a long one reads its whole prompt again. The session files keep one conversation, need a client to save and restore it, and refuse a layer split.
STRATA_PARK_DIR(files)+ STRATA_PARK_RAMLIKE=1(RAM, Windows)--layer-splitWhy named sections and not a file marked temporary: on Windows 11 a file created with
FILE_ATTRIBUTE_TEMPORARY(also withFILE_FLAG_DELETE_ON_CLOSE) and never flushed was written to the disk as it was written - 4 GiB of 4 GiB, with the handle still open (1 GiB of 1 GiB withDELETE_ON_CLOSE). A named, pagefile-backed section (CreateFileMappingW(INVALID_HANDLE_VALUE, ..., SEC_COMMIT)) is not file cache: its pages stay in the working set of a process that maps it and touches them, and they go to the pagefile only if Windows trims that working set. The sections live as long as one process holds them, so a keeper outside the server's process tree holds them while no engine runs.Related discussion
--conversation-disk-persist on|exit-only, one session file per stage) as part of a disk tier for evictions. The difference here: no change to the cache's eviction or request path, a save writes only what changed since the last one (and nothing when nothing ran), short and stale conversations are not written, two checkpoints per conversation instead of every one, the RAM knob with no disk writes, and it was run on Windows with a four-stage split. If the maintainers prefer #1937's design, the write-reduction rules and the RAM knob can go on top of itsexit-onlypath instead.--layer-split, which this covers for the restart case only.Approach
Three commits: the save / load at stop and start (files), the RAM knob on top, then a bound on the hand-over to the keeper.
src/program/generate.cpp: two commands between requests,PARKSAVE <folder>andPARKLOAD <folder>, answered likeSAVE/RESTORE(SAVED/RESTORED <conversations> <bytes> <ms>,SESSION/SWAITon the way,SERRfor a refusal that leaves the engine in step).STRATA_CKPT_ASYNC,STRATA_PARK_ASYNC), asSAVEdoes, then writes every parked conversation that is kept. A parked image is one plain single-carveSavedConversationper stage (the park already moved the checkpoints' stage parts apart), so each stage goes throughsession_file_writeas its own v1 image under the same model/config identity asSAVE. The image lends only the two checkpoints it writes (the deepest, where the next turn resumes, and the shallowest, the system prompt's root) for the length of one write and gets the whole chain back, also on a failure.park.manifest, format 2: the tag, the stage count, one line per conversation with its parked time) is written to a temporary name and renamed over the old one last; only then are the files it no longer lists removed. A save that fails keeps the old manifest and every file it lists.STRATA_VERSION, the engine file's size and modification time, the sortedSTRATA_*environment (notSTRATA_PARK_*) and the stage count. Another tag loads nothing.conversation_snapshot_validateasRESTORE, before anything is put back) andputs the image in the parked list; the next matching request restores it the usual way. A conversation that does not validate or fit is skipped with a log line; the others still load.--peer-device,--batchand without the conversation cache.SavedConversationgets two run-time fields (parked_unix,disk_name; not in the file) andConversationCacheread access to its entries; the park stampsparked_unix.park_files=1, orpark_files=2on Windows builds (files and sections), so the server knows what the engine takes.STRATA_PARK_RAMLIKE=1(Windows only; elsewhere a"mem:"path and the variable are refused with a message):src/core/conversation_file.{hpp,cpp}:session_file_write/session_file_readtake"mem:<name>"for a named section holding the session file's exact bytes behind a 64-byte header ([u64 length]). A write has no temporary file, no rename and no flush; its preflight is the free commit (aSEC_COMMITsection charges its whole size when it is created) instead of the free disk space; an existing name is refused, never written into. A written section stays held (handle and view) until the process exits, so a keeper can open it by name meanwhile.session_section_existsfor the name check.generate.cpp: each kept conversation's stage images go tomem:Local\strata-park-<name>-s<stage>(the name also carries the engine's process id and the second, so a name is never reused), the manifest ispark-mem.manifest, every kept conversation is written anew at each save, and no.sesfiles are removed.serve/park_keeper.py: after PARKSAVE the server starts the keeper through a middle process that exits at once (so the keeper is nobody's descendant and a tree kill of the server passes it by; out of the server's job when the job allows a breakaway). The keeper opens every section the manifest lists, maps it, touches its pages for at most 8 s (the rest of the walk goes on afterwards, and every page again every 30 s), sets a soft working-set floor and writeskeeper.json; the server waits for that before it closes the engine, at mostSTRATA_PARK_KEEPER_WAIT_S(default 30 s) in all. A hand-over still running 15 s after that leaves its stack in the log and the stop goes on; those conversations are then read again, as without the change. The next start's PARKLOAD reads the sections, then the server removeskeeper.jsonand the keeper ends within ~2 s - the sections go with the last handle. A keeper also ends afterSTRATA_PARK_MAX_AGE_Hor when a newer save replaced its manifest. A start that finds a manifest but no keeper (a reboot) says so, removes the manifest and loads nothing.serve/server.py:STRATA_PARK_DIRandSTRATA_PARK_RAMLIKE(read at start).StrataEngine.unload()sends PARKSAVE before it closes the engine (POST/unload, the idle unload); at a clean stop of the server (Ctrl+C, SIGTERM, the console's close)Service.park_at_exitsends it when no request holds the engine (it waits 1 s for the FIFO, else skips). After READY - first start, restart, lazy load - PARKLOAD when the folder has a manifest. Only to an engine whose INFO sayspark_files=1(files) orpark_files=2(the RAM knob, so an older engine never writes files instead); never with batch slots or replicas. Any failure is logged and the stop or start goes on.docs/DETAILS.md: what it does, what it writes and costs, why sections and not temporary files, the measurements.Compatibility: unset, no command is sent and the engine's only change is the INFO key. No file format changes (the stage images are v1 session files).
Validation
Environment note: measured only on PH402 SKU 200 boards - four GP100 dies of 48 SMs and 32 GB HBM2 each (128 GB in all), application clocks 715 / 1050 MHz - with every expert resident in VRAM (no CPU share, no PCIe share, no adaptive tier). Nothing in the change depends on the card.
Rig: Windows 11 (build 26300), driver 581.80 (TCC), CUDA 12.9, VS 2022; build
-DSTRATA_EXPERIMENTAL_SM60=ON -DCMAKE_CUDA_ARCHITECTURES=60 -DSTRATA_BUILD_CONVERSATION_TESTS=ON, ggml at the pinned 3cf03257f; NVMe SSD for the folder.Passed:
Build of this branch (main
870aedd9+ these three commits): sm_60 withSTRATA_EXPERIMENTAL_SM60,strataand the conversation tests, clean.This branch, CPU,
CUDA_VISIBLE_DEVICES=-1:conversation_file_test195 checks (12 new: a section round trip gives the file's exact bytes, no flush phase, the section held after the write, an existing name refused, a short commit refused with no section left behind, an absent name not read),conversation_cache_test4,842,conversation_split_failure_test(8 / 12 / 16 cases at 1 / 2 / 3 stages),conversation_memory_test33,conversation_validation_test972 host-only checks - all passed.python -m unittest discover -s serve -p "test_*.py"(Windows 11, Python 3.13): 1,274 tests OK (25 skipped). One timing test,test_benchmark_prefill_cancel_observes_progress_and_companion_completion, failed once in a run that shared the machine with a model load; it passed 3 of 3 alone, here and on main, and in this full run on the idle machine. New:serve/test_park_files.py(7: PARKSAVE before the close at an unload; nothing sent when unset, to an engine withoutpark_files, or with batch slots; PARKLOAD only with a manifest; a refusal leaves the engine in step; the save at exit skipped while a request holds the engine) andserve/test_park_ramlike.py(11: the keeper holds four 3 MiB sections after the process that made them exited and gives the same bytes back, ends when released and when a newer manifest replaces its own; a malformed manifest or one naming a path lists nothing; the server asks only an engine withpark_files=2, drops a manifest no keeper holds and loads nothing, loads then releases a live keeper, hands the sections to a keeper before it closes the engine, a spawn that gives up within its whole wait and says what the folder shows, and a page walk that stops at its deadline and goes on later).CPU probes on the same machine (the reason for the sections): a 4 GiB file with
FILE_ATTRIBUTE_TEMPORARY, written in 64 MiB blocks and never flushed: 4,204 MiB written to the disk during the write (physical disk write counters, 1 s samples); 1 GiB with the handle held 30 s: 1,023 MiB; 1 GiB withFILE_FLAG_DELETE_ON_CLOSEtoo: 1,024 MiB. A keeper holding two sections of 8 GiB in all for 10 minutes: working set 8.03 GiB throughout, Pages Output 0, the bytes read back equal.End to end, this branch's own revision, four GP100 dies, Swift 1.5 IQ3_S GGUF,
--layer-split 12,24,36,--pipeline-windows 2, MTP, withSTRATA_PARK_ASYNC=1,STRATA_CKPT_ASYNC=1andSTRATA_PARK_VRAM_MIB=1024on (the trace shows the background copies and restores from the VRAM slots). The reference is three turns of one conversation on one engine with no restart: 12,166 tokens read from zero in 40 s, then two short turns. The same turns across stops: 4 of 4 replies byte for byte the reference, each next turn reused all but the last 27 tokens and read them in 0.63-0.66 s.POST /unloadPOST /unloadConversations of 425-545 MB across four stage images; on a save with files, 2.3-2.9 s of that stop.
A long one on the same revision and switches, RAM knob: a 195,912-token prompt read from zero in 369 s; Ctrl+Break to the server's process group, stop 11.0 s (3.36 GB into four sections in 3.1 s, the keeper ready in 1.5 s); new server, load 2.0 s; the turn repeated with 195,929 tokens reused and 5 read took 2.5 s, its reply byte for byte the one before the stop;
POST /unloadthen took 7.0 s and a keeper held it again.End to end, four GP100 dies, earlier stacks, not this branch's revision (the save / load code is the same apart from comments, the RAM probe - main's
sample_host_memory()here, asRESTOREuses - and the RAM knob'sSTRATA_PARK_MIN_TOKENSdefault; checked with a comment-stripped diff; my serve fork's side also exported a prompt-echo table main does not have):--layer-split 12,24,36, conversation cache on, folder on an NVMe SSD: a 260,834-token conversation (296 s from zero) saved 5.33 GB in 3.6 s at the stop, loaded in 3.2 s at the start, next turn 7.2 s with 260,834 reused and 893 read; a 30K conversation and two 1K ones at a stop: only the 30K one written (0.89 GB, 0.74 s), the 1K ones not kept; a stop with no request since the load: 0 bytes written; after the 30K conversation ran again: its new image written and the old files removed; its manifest entry back-dated 3 days: not loaded, its files removed at the next stop; its turn after two restarts: 34,838 tokens reused, the reply bit for bit the one without a restart.011c9e60) with about 35 opt-in GP100 commits of mine (#3945 included), Swift 1.5 IQ3_S GGUF,--layer-split 12,24,36, MTP,STRATA_PARK_RAMLIKE=1,STRATA_PARK_MIN_TOKENS=50000. Run 1: a 103,490-token prompt read from zero in 94.1 s; graceful stop 8.2 s (1 conversation, 1,944,355,440 bytes in 1.43 s; a 66-token one not kept; the keeper holding the four stage sections); system disk 3.4 MiB during the stop against a 0.19 MiB/s background, 0.21 MiB/s median in the next 596 s, Pages Output 0, keeper working set 1.84 GiB throughout; next startpark load: 1 of 1 ... 2965 ms; the next turn (103,749 tokens) reused 103,721 and read 28 in 975 ms, reply hash equal to the same turn without the restart. Run 2: the stack with the change against the stack without it, one fresh engine per arm: 13 standard requests plus the 103K read and its next turn, 15/15 reply hashes equal, decode -0.04%, prompt reads +0.04% (noise); stop: 3 conversations, 6.75 GB in 3.6 s into 12 stage sections, the whole stop 11.1 s; next startpark load: 3 of 3 ... 3514 ms; the 103K turn reused 103,721 / read 28 in 973 ms; released at the end: the keeper exited and no section could be opened any more.Failed: none observed.
Not run:
generate.cppis compiled by all of them, the added code is host C++ with a_WIN32/ POSIX split; the section code is inside_WIN32parts) and any Linux build or run: the POSIX side (extern char** environ, the folder's file operations throughstd::filesystem) is not compiled here.strata launch --stop), which waits--shutdown-timeout+ 8 s (38 s by default) before it ends the server: a save longer than that is cut off (with files the previous save stays); the keeper under the Terminal's or the desktop app's process management (job objects).Limits
STRATA_*switches only. The RAM knob is Windows only."replicas") and batch slots (--batch/"parallel") are not parked; two servers must not share a folder (each start releases the other's keeper).STRATA_PARK_MAX_AGE_H; deletingkeeper.jsonin the folder ends the keeper at once. A reboot, a sign-out or a keeper that dies loses them; nothing is written as a fallback. The sections are in the session'sLocal\namespace with the default security of the user's processes.RESTORE.CI note: if the
metal-linkcheck is red, it is not caused by this PR: on #3945 the job failed while compiling upstream'smetal/src/mtl_rt.mm(lines 69 and 92:MTLGPUFamilyMetal4,MTLCompileOptions.mathMode,MTLMathModeFast/MTLMathModeSafe), which themacos-14runner's SDK does not provide, and those lines are unchanged on main. The workflow runs on any pull request that touchessrc/orinclude/; this PR changes no file undermetal/.🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.
[PR #3957] [Engine] serve: a long conversation survives an engine restart - parked conversations saved at a graceful stop and loaded at the next start, to files or (Windows) kept in RAM with no disk writes (opt-in STRATA_PARK_DIR, STRATA_PARK_RAMLIKE=1…to [PR #3957] [Engine] serve: a long conversation survives an engine restart - parked conversations saved at a graceful stop and loaded at the next start, to files or (Windows) kept in RAM with no disk writes (opt-in STRATA_PARK_DIR, STRATA_PARK_RAMLIKE=1…