mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-03 19:03:30 +07:00
[PR #2351] [CLOSED] [Engine] suffix-reuse: conversation KV prefix mount, quant-safe splices, state checkpoint ladder #3997
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#3997
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/2351
Author: @justindressler
Created: 10/14/2026
Status: ❌ Closed
Base:
main← Head:sfx-group📝 Commits (10+)
10d515bSuffix-reuse overlay on v0.1.43: Phase 3b partial-prefix mount + 5 wiring fixes + [SFX-DBG] instrumentationaafcde2k8v4 hybrid: accept the splice (K half is byte-identical int8)b937116int8 + STRATA_KV_ROT=1: splice via the exact Hadamard sandwich27fd372q4_0: splice via the Q4-sandwich (dequant Q4 -> H -> R(delta) -> H -> requant Q4)46e47f8kv_splice_apply_int8.hpp: include kv_q4.hpp (block_q4_0, QK4_0)8b8e6e3review r1 MINOR-1: partial-mount UnsupportedFormat reason string refreshed (generate.cpp:10713)dbfef66review r1 MINOR-2: T11.c q4_0 label reworded (mechanics unchanged; q4 routes to its own helper)c252ad2review r1 MINOR-3: file-header + guard docstrings refreshed to the all-quant contract170fd03review r1 MINOR-3 (b): splice_scope_guard_int8 docstring updated to accepted-format contract1f2751ahelp text: plain-words rewrite of the suffix-reuse arg hints (AGENTS.md style)📊 Changes
23 files changed (+6268 additions, -19 deletions)
View changed files
📝
CMakeLists.txt(+62 -0)➕
docs/FW2C_DESIGN_NOTES.md(+35 -0)➕
docs/SUFFIX_REUSE.md(+245 -0)📝
include/strata/core/conversation_cache.hpp(+135 -1)📝
include/strata/core/layer.hpp(+5 -0)➕
include/strata/program/kv_q8_quant.hpp(+110 -0)➕
include/strata/program/kv_splice.hpp(+98 -0)➕
include/strata/program/kv_splice_apply.hpp(+253 -0)➕
include/strata/program/kv_splice_apply_int8.hpp(+368 -0)➕
include/strata/program/kv_splice_apply_session.hpp(+192 -0)➕
include/strata/program/kv_splice_transport.hpp(+201 -0)➕
include/strata/program/suffix_reuse.hpp(+602 -0)📝
src/core/layer.cpp(+1 -0)📝
src/program/generate.cpp(+604 -18)➕
src/program/suffix_reuse_apply_test.cpp(+357 -0)➕
src/program/suffix_reuse_continuation_and_anchor_test.cpp(+380 -0)➕
src/program/suffix_reuse_dense_tail_test.cpp(+171 -0)➕
src/program/suffix_reuse_device_test.cpp(+311 -0)➕
src/program/suffix_reuse_int8_test.cpp(+662 -0)➕
src/program/suffix_reuse_off_parity_test.cpp(+302 -0)...and 3 more files
📄 Description
Problem and result
Problem. Strata re-reads the whole prompt from scratch on every request, and again after every
splice or mid-history edit. On a conversation with a long shared prefix that is repeated work: a
16-turn agent session re-prefilled the identical leading context sixteen times, and one mid-history
edit threw away the entire cache and re-read the whole 9.4k-token prefix.
Result. Three independent, default-off flags let the engine mount a parked conversation's KV
state when the incoming prompt shares a verified prefix, and bound the re-read after an edit. With
the flags off, answers are unchanged; with them on, prefill on cache hits drops from ~20 s to ~10 s
and exact replays from ~20 s to 0.2 s. Measured on the RTX 3090 that this repo's test rig uses, on a
164-row ledger (9.2k-9.6k prompt tokens): a 16-turn edit chain went from 234.2 s to 162.4 s
(-31%) with identical answers turn for turn, and an exact replay costs 0.2 s at 99.9% reuse.
This revision also rebases the branch from v0.1.45 (
477ac13c) onto currentmain(
9de04cd4, v0.2.1.4), which is what the earlier revision could not be reviewed against.Related discussion
two requests sharing the whole prefix, differing only in the final instruction). Ran on four flag
arms; the B-row reuse and wall times are in that comment.
Approach
Three orthogonal layers, each usable alone:
--cache-suffix-reuse <N>— mount a parked previous conversation whose token prefix matchesthe incoming prompt (LCP-verified), narrowing the read window. Works across all five KV formats
(fp16, int8, int8+KV_ROT, k8v4, q4_0), splicing quantized formats through their own quant/dequant
paths so reused blocks stay byte-identical.
--prompt-cache-dense-tail <N>(FW-1) — dense checkpointing of the tail region so late-turnconversations restore cheaply.
--prompt-cache-state-every <N>(FW-2b) — state checkpoint ladder at a token cadence,capping the re-read gap after mid-history edits and compaction-style rewrites.
Correctness posture: mounts only from LCP-verified prefixes; the recurrence-state restore refuses any
rung above the divergence point (fail-closed), so a stale-state probe from development never reaches
main. Compatibility: additive flags, defaults off, no change to the wire format, model format orexisting cache behaviour. Design and invariants:
docs/SUFFIX_REUSE.mdin this PR.What changed in the rebase, for review
The diff against
mainis 23 files, +6,272 / -19. The rebase itself touches one file for real (afive-line
CMakeLists.txtguard) plus the 19 rewritten upstream lines ingenerate.cpp; the rest isthe feature exactly as it was before, which is what makes this gateable in one sitting.
CMakeLists.txt. Upstream added a Win32 HIP delay-load block(
delayimp,/DELAYLOAD:hipblas.dll,libhipblaslt.dll,rocblas.dll); this branch adds acompile definition. Both kept, upstream's first. Our
STRATA_HAVE_CUDA_BUILD=1define is scopedto the GPU build so a CPU translation unit never reaches the fail-closed
#errorinkv_splice_transport.hpp:92. The wrapper sits inside the outer GPU block and is thereforeredundant in that position — kept deliberately as defence in depth.
generate.cppauto-merged, so a clean merge proves nothing by itself. All 19 upstream linesthis branch rewrites (
!gpu_sync_ok()->sync_ok,checkpoint_save->save_ok, short-circuitand fail-closed
return falsepreserved) were audited line by line, and the whole mergedtranslation unit was compiled in both trees.
lap()now accumulates unconditionally, where upstreamreturns early unless
ckpt_trace. That is this branch's own change (the FW-2a probe reads thosetimings without
STRATA_TRACE), not a rebase artifact: two extraClock::now()calls percheckpoint boundary. "Byte-identical with the flags off" therefore means identical output and KV
behaviour, with that one timing accumulator added.
Validation
Revision: branch rebased onto
mainat9de04cd4(v0.2.1.4); new headcf6c6984, 36 commitsreplayed, nothing squashed. Environment: WSL2, 32 cores, RTX 3090 24 GB, CUDA 13.0 (sm_86).
Model: Qwen3.8-Flash-Next abliterated GSQ-RCO IQ3_XXS, int8 KV, MTP spec-4, temperature 0.
Passed:
Host
ctest(CPU build,-j2): 49/54 passed, 1 skipped, 4 failed, 21.5 s. The four failures(
native_hadamard_test,tq_cpu_parity,tq_cpu_parity_avx2,isa_guard_check) failidentically on an unmodified
mainworktree built the same way — this machine's fetchedllama.cpp lacks the trellis types and has a pre-existing AVX-512 invariant violation. None of
those four touch a file in this PR.
Partial-prefix suite: 82/82 (the PR-2351 baseline), 0 failed.
Off-parity check: PASS — with the flags off, engine output is unchanged against base.
CPU build: exit 0, 0 warnings, 57 targets, 53 executables, 65 s.
generate.cppmerged-TU compile via the repo's ownmetal_compatshim: 0 errors, 0 warningsin both trees (16,611 lines this branch vs 16,573 upstream).
CUDA build (
-DCMAKE_CUDA_ARCHITECTURES=86, CUDA 13.0): exit 0,stratabinary 83,388,904 B.Built single-threaded; two concurrent
nvccfront-ends crash on this host, which is a machineconstraint, not a source finding. 21
nvccwarnings, all in files this PR does not touch.Needle-in-a-haystack on the rebased engine: 5 of 5 depths found (10/30/50/70/90%, 23,470
prompt tokens).
16-turn edit chain (cold, appends, inserts at rows 40/120/5, delete at 60, four exact replays;
each turn asks for the amount on a named row id, an amount that occurs nowhere else in the prompt):
--cache-suffix-reuse 64only--prompt-cache-dense-tail 4096 --prompt-cache-state-every 256Control: stock upstream
9de04cd4with none of this branch's commits ran the identical chainand produced the same 16 answers, turn for turn, 14/16 — zero disagreements against either arm
above. The two failures are the model's, not the cache's: turn 5 returns a neighbouring row's
amount, and turn 6 reproduces that answer from cache. A single cold request for turn 5's prompt,
in a fresh engine process, returns the same wrong answer — so it is the model reading a 9.4k-row
ledger, independent of any caching.
Deep edit (
insertat row 5, below every checkpoint) answered correctly in all three arms via thefail-closed restore-and-re-read path, at full read cost. The re-read is bounded by the deepest
anchor: designed behaviour, not a miss.
Independent review of this revision, scoped to the rebase delta rather than a re-read of the feature:
APPROVE-WITH-FINDINGS, 0 blocker, 0 major, 4 minor from two reviewers. The two findings they
shared are addressed here: the evidence snippet for the import/guard blocks is corrected, and the
redundant
CMakeLists.txtguard is documented above instead of left to confuse a reader.Failed:
Not run:
host tests above, but those backends are unverified on this revision. Per
AGENTS.mdthis PR doestouch backend-shared files, so this is a stated gap, not an oversight.
on knife-edge questions (observed 162 then 161 on identical prompts, same binary and config, with
spec-4 decoding and an 84-99% expert-cache hit). That is why the chain scores an unambiguous row-id
lookup, and why no claim above rests on a single run's answer.
Limits
falls back to a full read. Edits deeper than every anchor still pay full prefill cost.
trade, and it is why the flags are opt-in rather than default.
parked with a plan in
docs/SUFFIX_REUSE.md§10.🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.