[PR #3341] [Engine] Conversation cache: park and restore conversations using Disk instead of RAM (--conversation-cache-disk-mib) #4580

Open
opened 2026-11-07 15:37:37 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/3341
Author: @dsenchankau
Created: 10/20/2026
Status: 🔄 Open

Base: main ← Head: f-disk-conv-cache


📝 Commits (10+)

  • 1aa35f0 conversation cache: park and restore conversations through session files
  • 1d65384 Merge remote-tracking branch 'upstream/main' into f-disk-conv-cache
  • 221f27c serve: disk cache parks with its own small RAM floor, not the 2560 MiB cache floor
  • dea8886 Merge remote-tracking branch 'upstream/main' into f-disk-conv-cache
  • 6dfe26d serve: the disk conversation cache reads parked K/V back by streaming, not whole into RAM
  • 91b1885 Merge branch 'main' into f-disk-conv-cache
  • 43321c4 Merge branch 'main' into f-disk-conv-cache
  • 78b6f71 Removing cache files from disk by deafult, unless --conversation-cache-days is set to non 0 value
  • ae57199 Merge branch 'main' into f-disk-conv-cache
  • 63fac11 bug fixing

📊 Changes

15 files changed (+1824 additions, -141 deletions)

View changed files

📝 docs/DETAILS.md (+42 -1)
📝 include/strata/core/conversation_cache.hpp (+58 -4)
📝 include/strata/core/conversation_file.hpp (+78 -1)
📝 include/strata/core/conversation_memory.hpp (+15 -0)
📝 include/strata/core/conversation_snapshot.hpp (+19 -0)
📝 include/strata/core/session_save_reclaim.hpp (+11 -8)
📝 src/core/conversation_cache_test.cpp (+69 -0)
📝 src/core/conversation_file.cpp (+432 -37)
📝 src/core/conversation_file_test.cpp (+271 -0)
📝 src/core/conversation_memory_test.cpp (+15 -1)
📝 src/core/conversation_snapshot.cpp (+21 -0)
📝 src/core/conversation_snapshot_test.cpp (+34 -0)
📝 src/core/conversation_state.cpp (+38 -11)
📝 src/program/generate.cpp (+539 -78)
➕ tools/disk_conv_cache_smoke.sh (+182 -0)

📄 Description

Problem and result

I took this PR idea from Maya, a GLM5.3-Flash fork of Strata, where the disk conversation cache is on by default.

Right now the conversation cache has 2 options: keep it in RAM, or don't use it at all.

RAM is fine with a smaller quant (IQ3_S) and plenty of system RAM (96GB with that quant). It gets tight with a bigger quant like UD-Q4_K_XL, or with less system RAM.

UD-Q4_K_XL's experts are way bigger, and after startup the OS overhead + model + engine + coding harness have already eaten (almost) all the RAM. That leaves you two bad options: offload some experts to disk and lose decode and prefill speed, or skip the RAM cache and re-process the prompt over and over (slower the bigger the model gets).

For performance, the better move is to move the conversation cache to disk and keep more of the model's experts in RAM.

Without a cache, a conversation that comes back re-reads its whole prompt. With the cache on disk it's read back from its file and picks up where it left off. The re-reading gets worse the longer the session runs, but reading from the file stays about as cheap no matter how long it's been, so the disk cache pays off across a working session. You notice it most when you're using subagents a lot and bouncing between the main session and side ones. I measured the park/restore part (see Validation); I didn't run a reprocessing-vs-restore timing test, so I'm not putting a speedup number on it.

With this change, --conversation-cache-disk-mib N parks conversations into conv-*.sess files under <pack>/conversation_cache, and the budget counts the file bytes, so a lot more stay parked than would fit in RAM. A conversation that comes back on a later turn (same run, or after a restart) is read back from its file and continues from its own state instead of re-reading its prompt. It's off by default, the way the repo wants new features.

While the PR was open a follow-up landed. Reading a parked conversation back used to pull the whole file into host RAM first, then copy it to the device, so a ~40K-token conversation (~900 MB file) cost about the file's size in RAM for the moment of the read-back, and the RAM admission gate could refuse a read-back on a healthy machine with under ~2560 MiB free. The read-back now streams: it parses only the file's meta (bounded up front), then moves the K/V through one 16 MiB staging block straight into the pools that hold them. The transient is ~235 MiB-class (running state + deepest checkpoint + staging block), not file-size-class, and the disk-mode margin is min(--conversation-cache-min-free-mib, 512 MiB) for parking and read-back alike. RAM-cache mode and the RESTORE endpoint never take the streamed path, and they keep the full floor; their behavior is unchanged.

Another follow-up turned the conversation cache persistence off by default. Cache files are not preserved in-between the engine runs by default (unless --conversation-cache-days is set to non 0 value).

Approach

  • include/strata/core/conversation_cache.hpp: disk mode, with enable_disk, put_disk (budget counts file bytes), the slim folder-index entry, and the budget check that refuses an oversized entry before it touches anything already parked.
  • src/core/conversation_file.{hpp,cpp}: meta-only read (session_file_read_meta) and the startup folder scan (conversation_cache_index) that keeps this model's fresh conv-*.sess and deletes foreign, stale or unreadable files. Reuses the session-file format that's already there.
  • src/program/generate.cpp: the park and restore calls in the serve loop, plus the guards that skip parking (snapshot bigger than the whole budget, and RAM admission).
  • include/strata/core/{conversation_snapshot.hpp,session_save_reclaim.hpp}, src/core/conversation_state.cpp: the meta/checkpoint plumbing the index and restore need.
  • No serve/ change: the engine flags go straight through the run-config args, so server.py is untouched.
  • Tests: a disk-mode budget-refusal check in conversation_cache_test.cpp; conversation_file_test.cpp already covers the meta-only read and the folder scan.
  • docs/DETAILS.md: the flag and the measured numbers.
  • Follow-up (streaming read-back), src/core/conversation_file.{hpp,cpp}: session_file_restore_meta (meta only, sized up front by session_restore_peak_bytes) and session_file_restore_kv with a SessionKvSink that mirrors the existing streaming K/V source. The K/V is written part by part straight into the engine's pools and never forms a host-side copy. A new device error kind marks a failed pool copy (fatal exit, the file is kept for the next start).
  • Follow-up, src/program/generate.cpp: the read-back re-opens the file and pins its size to what the meta pass saw. That is a guard rail against a bigger replacement, not an identity check (the engine never replaces a published cache path). The failure contract for cache files: a broken or unreadable file is deleted, an out-of-RAM refusal keeps it, a device failure exits with it kept. A file found broken mid-stream costs that request its resume point too: the prompt is read again from scratch, because the K/V lands in the session before the final integrity check.
  • Follow-up tests: streaming, corrupt-file and device-kind checks in conversation_file_test.cpp (217 -> 251), and the read-back size bound in conversation_memory_test.cpp.

Validation

Revision 1aa35f01. Environment: Linux home-pc 7.0.0-38-generic #38~24.04.4-Ubuntu SMP PREEMPT_DYNAMIC x86_64, RTX 4080 SUPER (driver 615.71.10), CMake 3.28.3, Ninja 1.11.1, CUDA 13.4, Python 3.12.3. (This block is from before the streaming read-back follow-up; the follow-up's checks are at the end of this section.)

Passed:

  • Build (CUDA backend only; the change is common code, no backend sources or #ifdef):

    cmake -G Ninja -S . -B build-tests -DCMAKE_BUILD_TYPE=Release \
      -DSTRATA_ENABLE_CUDA=ON -DSTRATA_BUILD_CONVERSATION_TESTS=ON \
      -DCMAKE_CUDA_ARCHITECTURES=89
    cmake --build build-tests -j "$(nproc)"
    
  • The disk-cache unit tests (CPU-only), 5/5: conversation_cache_test (4789 checks), conversation_file_test (217 checks), plus conversation_split_failure_test, conversation_memory_test and session_save_reclaim_test:

    ctest --test-dir build-tests -R 'conversation_(cache|split_failure|memory|file)_test|session_save_reclaim_test' --output-on-failure
    

    5/5 passed, 1.46s.

  • The engine conversation tests, 3/3. conversation_snapshot_test uses the GPU (passed in 1.11s on an idle card; it skips with code 77 when it can't get one). conversation_validation_test and conversation_transfer_test run with CUDA_VISIBLE_DEVICES=-1:

    ctest --test-dir build-tests -R 'conversation_(snapshot|validation|transfer)_test' --output-on-failure
    

    3/3 passed, 1.17s.

  • Full suite. ctest --test-dir build-tests --output-on-failure on the branch gives the same result as master: 96% passed, 6 failed out of 157, 1 skipped (expert_multi_test). Those 6 are the usual environment/hardware ones on both branches (ple_parity and tq_cpu_parity want fixtures that aren't in the published source; ggml_tq_overlay, native_hadamard_test, tq_cpu_parity_avx2 want AMD/trellis hardware; isa_guard_check is a no-ISA check). So nothing new broke. Run this on a free GPU, or conversation_snapshot_test OOMs on whatever else is using the card.

  • Engine run on one RTX 4080 SUPER, IQ3_S pack, greedy. I launched the model the normal way from its config, adding only --conversation-cache-disk-mib 16384 to the run-config args:

    .venv/bin/python serve/server.py --engine strata --config strata-iq3_s.json --port 1337
    

    It parked 1962 tokens to disk (266,114,352 bytes), I restarted it, and it took back the same 1962 tokens from disk. The next turn only read the new part (cached_tokens 1962, prompt_n 17). The guards hold too: a snapshot bigger than the whole budget is refused and the folder stays empty, foreign files and a hand-edited conv-hacked-9.sess are deleted at startup, and --conversation-cache-days 0 empties the folder.

  • serve suite:

    PYTHONPATH=. .venv/bin/python -m unittest discover -s serve
    

    881 run, 14 skipped, 1 failed. The one failure (test_lifeline: "the child outlived a killed server") is a process-watcher timing race. It's not from this change: git diff upstream/main HEAD -- serve/ is empty, so I touched no serve file, and the test fails the same on master.

Failed: nothing here is caused by this change. The 6 C++ failures above are the same environment/hardware ones master has, with or without the branch, and the serve test_lifeline failure is a watcher race in a file this PR doesn't touch.

Not run: HIP and SYCL, build and run. I don't have AMD hardware. The change is common code only (no backend sources, no #ifdef), so it should be backend-agnostic, but that's me reading the code, not a build. Call it unconfirmed static analysis. Someone with that hardware should build it, and run it once on each if they can. Metal I didn't build or run. The park/restore numbers above are NVIDIA/CUDA only.

Follow-up (streaming read-back), revision 78b6f71:

Passed all the same checks as above.

Limits

The park/restore measurement is one pack on one card (RTX 4080 SUPER). I'm not claiming a speedup on other hardware or quants beyond the reasoning above. The disk cache is opt-in, and I didn't change the RAM cache path or how its budget is counted. HIP and SYCL are unverified because I have no hardware for them.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Niko1221/Strata/pull/3341 **Author:** [@dsenchankau](https://github.com/dsenchankau) **Created:** 10/20/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `f-disk-conv-cache` --- ### 📝 Commits (10+) - [`1aa35f0`](https://github.com/Niko1221/Strata/commit/1aa35f01f2d9ae05be4357aea0f065dde4bc8967) conversation cache: park and restore conversations through session files - [`1d65384`](https://github.com/Niko1221/Strata/commit/1d653846d6e1d3f470cb1af0bfecff0c4803c979) Merge remote-tracking branch 'upstream/main' into f-disk-conv-cache - [`221f27c`](https://github.com/Niko1221/Strata/commit/221f27ca18e54e3c865b9c1a060c2d775f0dfc50) serve: disk cache parks with its own small RAM floor, not the 2560 MiB cache floor - [`dea8886`](https://github.com/Niko1221/Strata/commit/dea88867971154eb2ed1b9a27a141e963d0b0091) Merge remote-tracking branch 'upstream/main' into f-disk-conv-cache - [`6dfe26d`](https://github.com/Niko1221/Strata/commit/6dfe26d9d28d17a40ad01b3c3f3a62230abaaed4) serve: the disk conversation cache reads parked K/V back by streaming, not whole into RAM - [`91b1885`](https://github.com/Niko1221/Strata/commit/91b188549ca7da73ee2da667a11939d31e68ced8) Merge branch 'main' into f-disk-conv-cache - [`43321c4`](https://github.com/Niko1221/Strata/commit/43321c4efc9ce1f81b41d6cc09f1b54961d04ef6) Merge branch 'main' into f-disk-conv-cache - [`78b6f71`](https://github.com/Niko1221/Strata/commit/78b6f71187760f46eb819f2bd6a190d7e666d653) Removing cache files from disk by deafult, unless --conversation-cache-days is set to non 0 value - [`ae57199`](https://github.com/Niko1221/Strata/commit/ae57199c5e79cc868811f39ad3919cafb2bdeae7) Merge branch 'main' into f-disk-conv-cache - [`63fac11`](https://github.com/Niko1221/Strata/commit/63fac11e411f7b1bee151853579014583dffa8f6) bug fixing ### 📊 Changes **15 files changed** (+1824 additions, -141 deletions) <details> <summary>View changed files</summary> 📝 `docs/DETAILS.md` (+42 -1) 📝 `include/strata/core/conversation_cache.hpp` (+58 -4) 📝 `include/strata/core/conversation_file.hpp` (+78 -1) 📝 `include/strata/core/conversation_memory.hpp` (+15 -0) 📝 `include/strata/core/conversation_snapshot.hpp` (+19 -0) 📝 `include/strata/core/session_save_reclaim.hpp` (+11 -8) 📝 `src/core/conversation_cache_test.cpp` (+69 -0) 📝 `src/core/conversation_file.cpp` (+432 -37) 📝 `src/core/conversation_file_test.cpp` (+271 -0) 📝 `src/core/conversation_memory_test.cpp` (+15 -1) 📝 `src/core/conversation_snapshot.cpp` (+21 -0) 📝 `src/core/conversation_snapshot_test.cpp` (+34 -0) 📝 `src/core/conversation_state.cpp` (+38 -11) 📝 `src/program/generate.cpp` (+539 -78) ➕ `tools/disk_conv_cache_smoke.sh` (+182 -0) </details> ### 📄 Description ## Problem and result I took this PR idea from [Maya](https://github.com/mw00/project-maya), a GLM5.3-Flash fork of Strata, where the disk conversation cache is on by default. Right now the conversation cache has 2 options: keep it in RAM, or don't use it at all. RAM is fine with a smaller quant (IQ3_S) and plenty of system RAM (96GB with that quant). It gets tight with a bigger quant like UD-Q4_K_XL, or with less system RAM. UD-Q4_K_XL's experts are way bigger, and after startup the OS overhead + model + engine + coding harness have already eaten (almost) all the RAM. That leaves you two bad options: offload some experts to disk and lose decode and prefill speed, or skip the RAM cache and re-process the prompt over and over (slower the bigger the model gets). For performance, the better move is to move the conversation cache to disk and keep more of the model's experts in RAM. Without a cache, a conversation that comes back re-reads its whole prompt. With the cache on disk it's read back from its file and picks up where it left off. The re-reading gets worse the longer the session runs, but reading from the file stays about as cheap no matter how long it's been, so the disk cache pays off across a working session. You notice it most when you're using subagents a lot and bouncing between the main session and side ones. I measured the park/restore part (see Validation); I didn't run a reprocessing-vs-restore timing test, so I'm not putting a speedup number on it. With this change, `--conversation-cache-disk-mib N` parks conversations into `conv-*.sess` files under `<pack>/conversation_cache`, and the budget counts the file bytes, so a lot more stay parked than would fit in RAM. A conversation that comes back on a later turn (same run, or after a restart) is read back from its file and continues from its own state instead of re-reading its prompt. It's off by default, the way the repo wants new features. While the PR was open a follow-up landed. Reading a parked conversation back used to pull the whole file into host RAM first, then copy it to the device, so a ~40K-token conversation (~900 MB file) cost about the file's size in RAM for the moment of the read-back, and the RAM admission gate could refuse a read-back on a healthy machine with under ~2560 MiB free. The read-back now streams: it parses only the file's meta (bounded up front), then moves the K/V through one 16 MiB staging block straight into the pools that hold them. The transient is ~235 MiB-class (running state + deepest checkpoint + staging block), not file-size-class, and the disk-mode margin is min(`--conversation-cache-min-free-mib`, 512 MiB) for parking and read-back alike. RAM-cache mode and the RESTORE endpoint never take the streamed path, and they keep the full floor; their behavior is unchanged. Another follow-up turned the conversation cache persistence off by default. Cache files are not preserved in-between the engine runs by default (unless --conversation-cache-days is set to non 0 value). ## Approach - `include/strata/core/conversation_cache.hpp`: disk mode, with `enable_disk`, `put_disk` (budget counts file bytes), the slim folder-index entry, and the budget check that refuses an oversized entry before it touches anything already parked. - `src/core/conversation_file.{hpp,cpp}`: meta-only read (`session_file_read_meta`) and the startup folder scan (`conversation_cache_index`) that keeps this model's fresh `conv-*.sess` and deletes foreign, stale or unreadable files. Reuses the session-file format that's already there. - `src/program/generate.cpp`: the park and restore calls in the serve loop, plus the guards that skip parking (snapshot bigger than the whole budget, and RAM admission). - `include/strata/core/{conversation_snapshot.hpp,session_save_reclaim.hpp}`, `src/core/conversation_state.cpp`: the meta/checkpoint plumbing the index and restore need. - No `serve/` change: the engine flags go straight through the run-config `args`, so `server.py` is untouched. - Tests: a disk-mode budget-refusal check in `conversation_cache_test.cpp`; `conversation_file_test.cpp` already covers the meta-only read and the folder scan. - `docs/DETAILS.md`: the flag and the measured numbers. - Follow-up (streaming read-back), `src/core/conversation_file.{hpp,cpp}`: `session_file_restore_meta` (meta only, sized up front by `session_restore_peak_bytes`) and `session_file_restore_kv` with a `SessionKvSink` that mirrors the existing streaming K/V source. The K/V is written part by part straight into the engine's pools and never forms a host-side copy. A new `device` error kind marks a failed pool copy (fatal exit, the file is kept for the next start). - Follow-up, `src/program/generate.cpp`: the read-back re-opens the file and pins its size to what the meta pass saw. That is a guard rail against a bigger replacement, not an identity check (the engine never replaces a published cache path). The failure contract for cache files: a broken or unreadable file is deleted, an out-of-RAM refusal keeps it, a device failure exits with it kept. A file found broken mid-stream costs that request its resume point too: the prompt is read again from scratch, because the K/V lands in the session before the final integrity check. - Follow-up tests: streaming, corrupt-file and device-kind checks in `conversation_file_test.cpp` (217 -> 251), and the read-back size bound in `conversation_memory_test.cpp`. ## Validation Revision `1aa35f01`. Environment: Linux `home-pc 7.0.0-38-generic #38~24.04.4-Ubuntu SMP PREEMPT_DYNAMIC x86_64`, RTX 4080 SUPER (driver 615.71.10), CMake 3.28.3, Ninja 1.11.1, CUDA 13.4, Python 3.12.3. (This block is from before the streaming read-back follow-up; the follow-up's checks are at the end of this section.) Passed: - Build (CUDA backend only; the change is common code, no backend sources or `#ifdef`): ```sh cmake -G Ninja -S . -B build-tests -DCMAKE_BUILD_TYPE=Release \ -DSTRATA_ENABLE_CUDA=ON -DSTRATA_BUILD_CONVERSATION_TESTS=ON \ -DCMAKE_CUDA_ARCHITECTURES=89 cmake --build build-tests -j "$(nproc)" ``` - The disk-cache unit tests (CPU-only), 5/5: `conversation_cache_test` (4789 checks), `conversation_file_test` (217 checks), plus `conversation_split_failure_test`, `conversation_memory_test` and `session_save_reclaim_test`: ```sh ctest --test-dir build-tests -R 'conversation_(cache|split_failure|memory|file)_test|session_save_reclaim_test' --output-on-failure ``` 5/5 passed, 1.46s. - The engine conversation tests, 3/3. `conversation_snapshot_test` uses the GPU (passed in 1.11s on an idle card; it skips with code 77 when it can't get one). `conversation_validation_test` and `conversation_transfer_test` run with `CUDA_VISIBLE_DEVICES=-1`: ```sh ctest --test-dir build-tests -R 'conversation_(snapshot|validation|transfer)_test' --output-on-failure ``` 3/3 passed, 1.17s. - Full suite. `ctest --test-dir build-tests --output-on-failure` on the branch gives the same result as master: 96% passed, 6 failed out of 157, 1 skipped (`expert_multi_test`). Those 6 are the usual environment/hardware ones on both branches (`ple_parity` and `tq_cpu_parity` want fixtures that aren't in the published source; `ggml_tq_overlay`, `native_hadamard_test`, `tq_cpu_parity_avx2` want AMD/trellis hardware; `isa_guard_check` is a no-ISA check). So nothing new broke. Run this on a free GPU, or `conversation_snapshot_test` OOMs on whatever else is using the card. - Engine run on one RTX 4080 SUPER, IQ3_S pack, greedy. I launched the model the normal way from its config, adding only `--conversation-cache-disk-mib 16384` to the run-config `args`: ``` .venv/bin/python serve/server.py --engine strata --config strata-iq3_s.json --port 1337 ``` It parked `1962 tokens to disk (266,114,352 bytes)`, I restarted it, and it took back the same 1962 tokens from disk. The next turn only read the new part (`cached_tokens 1962`, `prompt_n 17`). The guards hold too: a snapshot bigger than the whole budget is refused and the folder stays empty, foreign files and a hand-edited `conv-hacked-9.sess` are deleted at startup, and `--conversation-cache-days 0` empties the folder. - serve suite: ```sh PYTHONPATH=. .venv/bin/python -m unittest discover -s serve ``` 881 run, 14 skipped, 1 failed. The one failure (`test_lifeline`: "the child outlived a killed server") is a process-watcher timing race. It's not from this change: `git diff upstream/main HEAD -- serve/` is empty, so I touched no serve file, and the test fails the same on master. Failed: nothing here is caused by this change. The 6 C++ failures above are the same environment/hardware ones master has, with or without the branch, and the serve `test_lifeline` failure is a watcher race in a file this PR doesn't touch. Not run: HIP and SYCL, build and run. I don't have AMD hardware. The change is common code only (no backend sources, no `#ifdef`), so it should be backend-agnostic, but that's me reading the code, not a build. Call it unconfirmed static analysis. Someone with that hardware should build it, and run it once on each if they can. Metal I didn't build or run. The park/restore numbers above are NVIDIA/CUDA only. Follow-up (streaming read-back), revision `78b6f71`: Passed all the same checks as above. ## Limits The park/restore measurement is one pack on one card (RTX 4080 SUPER). I'm not claiming a speedup on other hardware or quants beyond the reasoning above. The disk cache is opt-in, and I didn't change the RAM cache path or how its budget is counted. HIP and SYCL are unverified because I have no hardware for them. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Niko1221/Strata#4580
No description provided.