mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-03 19:03:30 +07:00
[PR #3517] [Engine] conversation cache: the retention pin follows the system-prompt root, not chain item 0 #4664
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#4664
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/3517
Author: @chaog992
Created: 10/20/2026
Status: 🔄 Open
Base:
main← Head:conv-cache-root-pin📝 Commits (1)
d2a3adfconversation cache: the retention pin follows the system-prompt root, not chain item 0 (#2228, #3090)📊 Changes
5 files changed (+352 additions, -11 deletions)
View changed files
📝
docs/DETAILS.md(+6 -3)📝
include/strata/program/conv_cache.hpp(+77 -0)📝
src/program/conv_cache_test.cpp(+246 -5)📝
src/program/generate.cpp(+12 -2)📝
sycl/src/program/generate.cpp(+11 -1)📄 Description
Problem and result
The retention policy keeps "the root" by pinning chain item 0 (
conv_cache.hpp). On a read from token 0,periodic checkpoints (
--prompt-cache-every, taken at chunk ends) are saved during the read, before the rootcheckpoint at the first turn boundary (
--prompt-cache-root). So when the system prompt and tools end pastthe first such chunk end (16K at the defaults), item 0 is a grid checkpoint: the pin protects that grid point
forever, and the real root rotates out at the (cap−1)-th checkpoint after its last use. From then on every new
chat and every compaction re-reads root − 16K tokens at prefill speed, and the root is never saved again (only
a read from token 0 saves it).
Result (policy tests, and an engine A/B below): the pin follows the root, so a new chat after checkpoint
churn mounts the end of the system prompt again, as docs/DETAILS.md promises ("kept for good").
Related discussion
#2228 (open): the reporter's probe of 2026-10-13 20:04 - the 159,184-token root was gone after roughly twelve
requests at
--prompt-cache 12(a policy simulation gives the 10th append), the compaction then mounted at32,768, and the root survived at
--prompt-cache 48. The within-thread grid fallbacks of that issue'soriginal report, where the 20,765-token root was item 0 all along, are a different case and are not changed
here. #3090 (closed in the 10-19 tracker reset) reached the same diagnosis from the 0.1.45 sources and asked
to pin the root explicitly; its turn-start resumes from 16,384 were from the pinned grid checkpoint.
Merge-adjacent: #1887 (reopened 10-20) and #2240 on top of it move this eviction loop into a helper - whichever
lands second passes the root index at both of its call sites; #2526 appends slot checkpoints to the chain,
which the length-based lookup does not depend on.
Approach
eviction_victimandspread_victimgain overloads that take the root's index and pin it instead of item 0,which then rotates like any other item; with root 0 (or out of range) they delegate to the existing overloads.
The serve loop names the root through two new pure helpers in the same header:
root_lengthreads theconversation itself - the first turn boundary, when it lies at or past
--prompt-cache-rootand a laterboundary exists, the position
root_atpicks (a single-boundary conversation has no system prompt endingthere) - and
root_indexfinds the chain item of that length. #3090 suggested marking the root when it is saved, aspinnedmarks a pin=N checkpoint; a lookup adds no state that the checkpoint pool, parking and restores wouldhave to carry and reset, and it runs once per eviction round, only when something must leave (~55-60 µs worst
case at 262,144 tokens, gcc 13.3 -O2 on an i7-13700K). The SYCL mirror gets the same lookup. +355/−13 over 5
files; no new flag.
Validation
Tested: 70781f4b on main
9ae93f7b(v0.2.1.1), WSL2 Ubuntu 24.04, gcc 13.3; the commit also merges cleanlyonto current main.
Passed:
g++ -O2 -std=c++20 -Wall -Wextra -Wconversion -I include src/program/conv_cache_test.cpp(also the ctestbuild): all 42 checks pass in 1.5 s. They include: the 40K-root and #2228-shaped chains keep their root for
30+ turns, driven through the production
root_length/root_indexlookups, and the 40K chain loses itwith no root named (LRU and spread controls); the lookup also drives a grids-ahead-of-root chain down to two
slots, keeping {root, newest}; the policies match references written independently from the doc comments on
every chain of up to 5 items over three length shapes (even, convex, one huge gap), every root in [0, n),
stamps with ties, caps 0..n+1 and every tail/pin subset (and absent arrays); a named-root spread case on an
uneven chain pins the expected victim; root 0 or out of range equals the old overloads the same way
(delegation guard);
root_length/root_indexunit cases.The same test against the base policy (the base header plus the new lookups and overloads that ignore the
root argument, i.e. today's behaviour): 8 of the 42 checks fail - the named-root scenarios and the reference
comparison - and the other 34 pass. That is the failing-before receipt. Command: the same g++ line with
-I <dir>holding the shim asstrata/program/conv_cache.hpp; both outputs and the shim are below.test output, this branch (42 ok)
test output against the shimmed base policy (8 FAIL)
the shim block appended to the base conv_cache.hpp
Full CPU ctest (
cmake -DCMAKE_BUILD_TYPE=Release -DSTRATA_BUILD_TESTS=ON -DSTRATA_GGML_DIR=<llama.cpp@pin>then
ctest -j8): 49 tests - 44 passed (conv_cache_test among them), 1 skipped, 4 failed; see Failed.CUDA (sm_120, nvcc 13.0.88), HIP (gfx1201, ROCm clang 22) and SYCL (icpx 2026.1.1): the full
stratatargetconfigures and links from this branch (compile and link on the dev box, which has no GPU).
Engine A/B on a GPU machine: RTX 5080 16 GB (driver 595.84), i9-14900K, Linux, Docker
(nvidia/cuda:13.0.0-devel, sm_120 build), qwen Q2_0,
--context 32768, stock settings (tuning skipped,vram_reserve_mib 4096; another service held ~3.5 GB of VRAM). A 23,378-token synthetic system prompt,seven appended turns, then a new chat with the same system prompt - base engine (
9ae93f7b) vs this branch:base mounts a grid checkpoint,
prompt 23377 tokens = 19456 reused + 3921 read(1,691 ms); this branchmounts the root,
prompt 23377 tokens = 23346 reused + 31 read(262 ms). The first reads and every turn'sdraft-acceptance numbers are identical in both runs; the four receipt lines are quoted verbatim from the
engine log, and the full log and replay script are available on request.
Failed: the four pre-existing Gyro test failures (isa_guard_check, tq_cpu_parity, tq_cpu_parity_avx2,
native_hadamard_test): the default pinned ggml has no trellis types and the guard lacks the tq entries. They
are identical at the unpatched base and untouched by this change; #3511 addresses them.
Not run: #2228's exact request sequence on the reporter's machine - the A/B above is a synthetic
conversation (note the recipe: the new chat must come after at least
--prompt-cache− 1 checkpoints sincethe root's last mount, or both builds still show the root; about 11 appends at
--prompt-cache 12). Thereporter offered to run diagnostic builds, and the replay script is ready. Windows (MSVC) and Metal (macOS
only; the shared
generate.cppcompiles into it unchanged) not built. expert_multi_test skipped (no AVX-512on the dev box).
Limits
cache goes over budget - that is the fix. Where a mount point changes, the read's chunk boundaries move, so
answers after such a mount can differ in last-place rounding (as with any different resume point).
--prompt-cache-tailor pin=N checkpoint sitting ahead of thesystem-prompt root no longer holds the root's pin (the root stays instead). Both changes are in the root's
favour.
position
root_atpicks on a full re-read of such a conversation. A new chat that shares only part of thatfirst message then reads from token 0 where it could previously mount the 16K grid point.
example a system prompt whose tail carries a date) reads from token 0 once per distinct system prompt and
then gets a proper root for it. Keeping both pinned was worse in simulation: at
--prompt-cache 2every newsave evicts itself, and one rotating slot is lost at every cap.
--prompt-cache-spreadthe first grid point stays (the gap rule never takes item 0), costing oneslot; documented in DETAILS.md.
chain until the next full read.
--batchslot hand-back still clears the main chain, as before.that reproduce both reporters' numbers; the engine A/B confirms the new-chat case on a synthetic
conversation, not the reporters' own sequences.
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.