mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-03 19:03:30 +07:00
[GH-ISSUE #4206] [Engine] --prompt-cache-every is a no-op when N is below the prefill chunk size #1849
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#1849
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?
Originally created by @3chet on GitHub (Oct 26, 2026).
Original GitHub issue: https://github.com/Niko1221/Strata/issues/4206
Version: Strata 0.1.50 | engine 0.1.50
Hardware: 2x AMD Radeon Pro VII 16 GB (gfx906) | 64 GB RAM | Ubuntu 26.04.1 | ROCm 7.1.1 (Docker)
Model: Qwen3.8-Flash-Next IQ3_S | 163840 context | pictures off | layer split K=12 | MTP self-speculation on
Reproduction status: reproduced locally
What happened
A client that appends one turn to the end of an existing history — the prefix is
byte-identical, nothing is edited, nothing is re-generated — still re-reads the whole
prompt:
I expected ~12742 of those to be reused. Two separate things combine to make it zero,
and I think the first one is a documentation/behaviour mismatch rather than a design
choice.
1.
--prompt-cache-everyis clamped up to the prefill chunk size.The help text says:
but the checkpoint is only created inside the chunk callback, where
doneis the end ofa whole chunk (
generate.cpp:9934and:9942in 0.1.50):donenever takes an intermediate value, so the effective spacing ismax(--prompt-cache-every, chunk). With the default--prefill auto:16384, a 12742-tokenprompt is exactly one chunk, which yields exactly one checkpoint, at the very end — and
setting
--prompt-cache-every 4096changes nothing at all (measured below).2. That single end checkpoint is then unusable, because it is longer than the new prompt.
conversation_prefix()(include/strata/core/conversation_cache.hpp:187) requires astrict prefix that is shorter than the incoming prompt:
The end checkpoint carries the tokens that opened the turn (the session parks at 12750),
so for an incoming 12747-token prompt
n >= prompt.size()is true and the wholecheckpoint is discarded. There is no earlier checkpoint to fall back to, so the prompt is
read from zero.
--prompt-cache-spreaddoes not help here, which is consistent with its help text — itonly changes which checkpoint is evicted (
conv_cache::spread_victim,generate.cpp:9885),not how many exist. Reports of "rewriting the tail still re-reads everything" will
probably land on
--prompt-cache-spreadfirst, so it may be worth noting that thecheckpoint density is bounded by the chunk size.
What does work: lowering the chunk cap.
--prefill auto:8192produces twocheckpoints for the same prompt; the 8192 one sits before the divergence and is usable:
So the workaround is to treat the chunk size as the checkpoint granularity.
Smallest steps or command that reproduce it
Probe: send A (12742 tokens), then B (12743 tokens, different prefix, to force A to be
parked), then
A + "\n\nReply with OK."(12747 tokens — A's entire prefix unchanged,five tokens appended at the end).
With
--prefill auto:16384 --prompt-cache 8192 --prompt-cache-every 4096, the thirdrequest reports
0 reused + 12747 read,1 checkpoints.Restart the same server with
--prefill auto:8192only, repeat the probe, and the thirdrequest reports
8192 reused + 4555 read,2 checkpoints,restored 8192 tokens (checkpoint) in 73.6 ms.Same probe, four configurations, two runs each:
--prefill auto:16384(default) +--prompt-cache-spread--prefill auto:16384+--prompt-cache-spread+--prompt-cache-every 4096--prefill auto:4096--prefill auto:8192Cost of the smaller chunk on a cold 24k-token prompt (two runs each,
--short-read 256):So the workaround costs 3–4% prefill throughput on long prompts, and halves the wall time
of a tail-appended turn (25.2 s → 12.1 s). Decode and expert-cache hit rate are unchanged
(50.5–50.8 tok/s, 0.985–0.988).
What would fix it
Option A, smallest: when
--prompt-cache-every < chunk, treateveryas an upper boundon the chunk too, so the requested density is honoured instead of silently clamped. This
is exactly what I am doing by hand now, and it makes the option mean what its help text
says.
Option B: split the chunk at
pp_next_checkinternally, so a checkpoint can landmid-way through what would otherwise be one chunk.
Either way, a line in the help text saying "effective spacing is
max(N, chunk)" wouldavoid this report for the next person.
A question about the checkpoint at the end of the last chunk
With
--prefill auto:8192there are2 checkpoints, but only 8192 is reused — eventhough a checkpoint at 12742 would be a valid strict prefix of the 12747-token prompt
(
12742 < 12747). Is the last chunk's checkpoint being dropped, or stored past theprompt end? In the multi-GPU path I see a completeness test that discards a set that is
not fully assembled (
generate.cpp:9958):If the last (short) chunk tends to produce an incomplete set, that would explain it, and
it would mean that on multi-GPU a tail-appended turn can never reuse the deepest
checkpoint. If that checkpoint could be kept, this case would go from 8k reused to ~12.7k
(roughly 12.1 s → 4 s).
Related, and lower priority:
--prompt-cache-tailsays "single GPU" in its help text, andthe code excludes it with
!multi_gpu(generate.cpp:9946). Sincepart_atalreadyassembles per-stage checkpoints for the multi-GPU path, extending the tail checkpoint there
would give layer-split users the one mechanism that directly covers an appended turn.
@github-actions[bot] commented on GitHub (Oct 26, 2026):
Thanks for the issue. To keep the tracker readable, every item needs an area prefix in the title and the parts of the issue form filled in. This one is missing:
Edit the issue (title and description) and this note goes away on its own. If it is still missing parts in 5 days it will be closed, which is not a rejection: fix it and comment
/reopen.Details: CONTRIBUTING.md
@3chet commented on GitHub (Oct 26, 2026):
Correction: the end checkpoint is not lost to the length check — appending more text does not help
In the report above I assumed the end checkpoint would become usable once the incoming prompt
grew past it (
n >= prompt.size()). I went back and tested that instead of leaving it as aguess, and it does not hold.
Same probe, only the appended text differs:
"\n\nReply with OK."(~5 tokens)The second prompt is longer than any checkpoint this conversation can hold, so
n >= prompt.size()is no longer what rejects it — and the reuse is still 8192, not ~12742.Both runs log the same shape:
2 checkpointson the prompt line, butrestored 8192 tokens (checkpoint)on the way in. So a second checkpoint is recorded, and itis simply not the one that gets restored.
With a layer split, restored checkpoints are merged back per stage
(
generate.cpp:12945-12950):So the end checkpoint stops being usable at one of two places: the completeness test at save
time (
generate.cpp:9958,parts.size() == stages.size() + 1), or this merge on restore,which keeps only what lines up across stages. Which one is it — is the last chunk's
checkpoint expected to be unavailable on a multi-GPU layer split?
From the outside the effect is that on a layer split the deepest usable checkpoint is much
shallower than the prompt: 8192 for a 12.7k conversation. It also stays 8192 at
--prefill auto:4096(measured 12.09 s, same 8192 reused), so lowering the chunk below 8192does not deepen the reuse at all.
That last point narrows the workaround I had in the first post:
--prefill auto:8192is theuseful setting (25.2 s → 12.1 s on this prompt), and going lower only costs prefill speed
without buying anything back.
@3chet commented on GitHub (Oct 26, 2026):
Root cause found: the deepest checkpoint is not dropped, it is moved by two tokens
I instrumented
conversation_prefix()to log the first diverging position and the two windowsaround it, and then tested a patch. Both hypotheses I wrote above were wrong, including the
one I made in my previous comment — so, answering my own question first: the completeness
test (
parts.size() == stages.size() + 1) and the per-stage merge are not involved. The endcheckpoint is saved, it comes back as a candidate, and it is then rejected for a genuine
content mismatch.
1. What the checkpoint at
turn_atactually contains.turn_atis the index of the last<|im_start|>(generate.cpp:14025). A checkpoint savedthere covers the history up to that header — which means it also covers the previous
message's
<|im_end|>and the\nafter it. A client that appends to the last message, orextends/rewrites it, puts its own content exactly where those two tokens were:
248046is<|im_end|>,198is\n. Six tokens match, the last two do not, so the whole12738-token checkpoint is unusable — and because it is the deepest one, reuse falls back to
the previous chunk boundary (8192), which is exactly the "8192 for a 12.7k conversation" I
reported.
2. This is also why
--prompt-cache-everycannot fix it, even if it worked.Every checkpoint it produces lands on a chunk boundary, and the body end is not one. Same for
Option B — splitting the chunk at
pp_next_checkgives you more checkpoints, but not one atturn_at - 2. The missing checkpoint is at a semantic position, not at a multiple of N, sodensity and granularity are two different knobs here.
3. One more cut at
turn_at - kdoes fix it.Local patch, three places (env-gated with
STRATA_CKPT_BODY_AT=<k>in my build so I couldA/B the same binary;
k = 0is stock):k = 2for this chat template (<|im_end|>+\n); it should be derived from the templaterather than hardcoded.
4. Measurements. Same probe, same binary family,
--prefill auto:8192on both arms(
--prefillis the other variable and I did not want it in the comparison):body_atk=212735 of 12747 is the whole history minus the 12 tokens that really do differ. Cold-read
throughput is unchanged (−0.2% on average, inside run-to-run noise), decode is unchanged, and
multi-turn appends are identical to stock (cached 3578 / 7355 / 11132 / 14909 / 18686 over six
turns). The cost is one more checkpoint per conversation, so with
--prompt-cache 24evictionsget marginally more frequent.
5. Correction on
--prefill auto:4096.In my previous comment I said reuse stays at 8192 with
auto:4096, "so lowering the chunkbelow 8192 does not deepen the reuse". That measurement was real but the reason is not the
cache:
auto:is clamped (generate.cpp:2584-2586):auto:4096runs as 8192, so that row never tested a 4096 chunk at all. Same family ofmismatch as the
--prompt-cache-everyone: the value in the flag is not the value in effect.A fixed
--prefill 4096does work, and it is slower (--prefill 12288was −9% on cold readhere,
4096worse), so I would rather not buy density that way.If adding the body-end cut as a default is acceptable, I am happy to send it as a PR — the
diff is the three hunks above. Otherwise it might be the natural thing to teach
--prompt-cache-tailto do on a layer split: keep the checkpoint at the end of the lastmessage's body, not at the end of the prompt.
@3chet commented on GitHub (Oct 26, 2026):
Follow-up: once the body-end cut exists, the
turn_atcut is redundant — dropping it halves the tail readWith
body_at(=turn_at - k) saved as its own checkpoint, keeping theturn_atcut splits the tail intothree parts instead of two, and the part in between carries only the
ktokens the client moves(
<|im_end|>and\nin this template). Those two tokens pay a full fixed overhead — stage sync, copies,buffers — measured at 341 ms on this box. The
body_atcheckpoint already covers everything before them.One small change on top of the patch in my previous comment:
Same probe, same binary,
--prefill auto:8192,STRATA_CKPT_BODY_AT=2; the only variable is the two lines above:body_atonlybody_at,turn_atcut droppedMulti-turn appends still land on the same checkpoints, at the cost of re-reading exactly those
ktokens:body_atonlyturn_atcutSo: 2 tokens less reused per turn, in exchange for one less part per request. Cold read is unchanged
(578.5 / 570.4 tok/s on a 26242-token prompt, same as with both cuts). One checkpoint fewer per
conversation also takes pressure off
--prompt-cache, since what was the "cost" of the previous patch(+1 checkpoint per conversation) is now back to stock.
Two things to watch if this goes upstream.
--prompt-cache-spreadand the eviction path rank checkpoints by adjacency, so removing a cut changeswhat gets evicted first. Here it is one checkpoint fewer per conversation, which is the direction we want,
but it is a behaviour change worth keeping in mind.
STRATA_SLOT_CKPT_MOVEidentifies "this request's own turn checkpoint" withbest->ids.size() == turn_at(
generate.cpp:15534). With theturn_atcut gone that test never matches, so the move optimizationsilently stops applying. It needs to accept
body_atas well.Net effect on my workload — appending to or editing the last message, which is what an IDE-style client does
on every turn — is 11.5 s → 0.44 s for the prompt read, with cold-read throughput and decode unchanged.
@github-actions[bot] commented on GitHub (Nov 1, 2026):
Closing this issue because it still did not follow the format after 5 days. That is housekeeping, not a rejection.
To bring it back: fix the title (area prefix) and the missing parts, then comment
/reopen. A check runs and reopens it, or tells you what is still missing. See CONTRIBUTING.md.