Repository navigation
feat(kvflash): pooled prefill on an absolute 512-token grid - #850
Merged
davide221 merged 20 commits intoOct 25, 2026
Merged
Conversation
…score ledger Library additions to KvFlashPager, header and unit tests only; nothing in the backends calls them yet. - serialize(max_chunks) / deserialize(): a position-addressed blob of the first N chunks (resident rows, host copies, per-chunk score ledger), so a pager can be rebuilt later. A failed pinned-host allocation in deserialize frees the buffers already allocated. - truncate(P): drop every logical position >= P, as if the pager had only seen P tokens. The chunk containing P keeps its block with rows >= P zeroed; the pool is refilled from host copies like a fresh pager (recency, or score with a scorer) and the LRU clock is renumbered in logical order. - pin_range / unpin_all (reset clears pins), per-chunk score ledger accessors, floor_to_chunk, and memset of host-resident blocks.
…is unavailable The CPU-backend serde and truncate cases that page chunks out allocate their host backing with cudaMallocHost on GPU builds. On the CPU-only check-cpu runner the driver is a stub and that allocation fails, so 12 cases failed. Skip them there; the GPU runners still run them.
dusterbloom
force-pushed
the
kvflash/2.0a-C-prefill-grid
branch
from
October 19, 2026 14:16
7444615 to
0fe12d9
Compare
dusterbloom
marked this pull request as ready for review
October 21, 2026 10:55
davide221
reviewed
Oct 21, 2026
davide221
left a comment
Contributor
There was a problem hiding this comment.
Code review
No issues found. Checked for bugs and logic errors in the changed code.
Reviewed 0fe12d90 · 0 candidate issues checked, kept at ≥70% confidence · add the code-review label to review again
…ing the rest refill_after_truncate_ ranked host chunks by score alone, so with a scorer older high-score chunks could take every free block and leave the sink, tail window and pinned chunks non-resident, unlike a pager that only saw P tokens. Recall the protected chunks first, then rank the discretionary ones.
…VFLASH2 The header recorded only the K dtype, so a V cache switched from F16 to BF16 passed the byte-geometry checks and its values were reinterpreted. Record the V dtype as a ninth header field, validate it, and bump the magic to KVFLASH2 so blobs written by the previous layout are refused.
attach() reset the mappings but kept the pin vector, so pins set against a larger pool survived re-attaching to a smaller one and protected every chunk, starving allocation. Clear them with the other per-attach state.
A position at chunk index 2^31 narrowed to a negative int, skipped the resize and wrote pinned_ out of bounds (segfault in the unit test). Check the range in 64 bits first: refuse spans longer than the pool and chunk indices past 4M, which also bounds the range scan and the pin-flag allocation.
… as recallable zero KV deserialize marked every entry on_host, so a chunk that was never written (a hole left by a failed allocation or a position jump) became recallable zero KV that reselect could pick. Write a materialized byte in the ledger (same KVFLASH2 layout bump as the V dtype), restore holes as holes without a host copy, and refuse a blob that marks a never-materialized chunk resident.
…ents are caught The pattern repeated every 256 bytes but a segment is 512, so whole K/V/head/layer segments were interchangeable and swapping them went undetected (28 of 28 pair swaps passed the old check). Hash the byte offset within the chunk instead, in both the serde and truncate tests, and add a case that swaps every segment pair.
…geable host backing Fifteen CPU-backend cases skipped on the check-cpu runner because the pager's pinned host backing (cudaMallocHost) fails under the stub driver, so CI never ran them. Add KvFlashPager::set_pinned_host_backing(false) (pageable memory, each chunk remembers how its buffer was allocated) and a TestPager that picks it when pinned allocation is unavailable. None of these cases exercise DMA ordering (their tensors are host memory), so none stay GPU-gated.
The wire format was documented as little-endian but integers and floats are written in host byte order. Add a static_assert for little-endian hosts, fix the comment, and pin the byte layout with a test. No encoder is added.
With KVFlash on, a prefix snapshot used to be refused as soon as the live cache outgrew the resident pool, because dense snapshots copy KV rows assuming the identity layout that pooled prefill breaks. This saves such a snapshot as a pager blob and restores it without re-prefilling the prefix. - PrefixSnapshot carries the pager blob (is_pooled, kvflash_blob); the state snapshot skips the dense KV tensors (skip_kv). Pooled snapshots are taken at a chunk boundary of a pooled prefill and at the end of a prompt. - Restore, behind LUCE_KVFLASH_RESTORE_CONSUME=1 (default off, behavior then unchanged): the pager and QK pool are rebuilt from the blob and only the suffix prefills, with real token ids for the restored prefix. Without it, or when the cut is not chunk aligned, a pooled snapshot is dropped and the whole prompt prefills from 0 (kvflash_restore_plan, pure and unit tested). - A pooled snapshot is never handed to the SSD tier: an OwnedSnapshot cannot carry the pager blob, so a file written from it would restore with no KV (Luce-Org#839 hazard). disk_persistable() and SnapshotRef::pooled guard the save and the layout learning. - A dense prefill resets the pager, so a boundary snapshot never serializes the previous request's mapping. - LUCE_KVFLASH_DEBUG_RESIDENT logs the resident chunk ranges and a score-ledger hash after each prefill. - optimizations/kvflash/tests/pooled_restore_identity.sh checks restored and cold answers match at 20K/35K/64K. - Docs: ENVIRONMENT.md rows and a KVFlash session restore section in PREFIX_CACHE.md.
…ot to the disk writer The Qwen3.5 backend overrides snapshot_detach and already frees a pooled snapshot. The default implementation copies snapshot_ref() to host, which for a pooled snapshot would carry tensors without the KV held in the pager blob. Return an empty snapshot instead, and cover it with a unit test.
Pooled prefill used min(ubatch, remaining) batches counted from wherever the request started, so where a batch ended depended on the earlier history of the session and a restored run could cut differently from a cold one. Batches now end on multiples of 512 (a multiple of the 64-token pager chunk): only the first batch after an off-grid resume and the end-of-prompt remainder are short. kvflash_prefill_batch_end / kvflash_prefill_batches are pure and unit tested; the non-pooled prefill path is unchanged. Documented in PREFIX_CACHE.md.
dusterbloom
force-pushed
the
kvflash/2.0a-C-prefill-grid
branch
from
October 23, 2026 16:41
0fe12d9 to
ccbe206
Compare
…er branch kvflash_pager.h reset(): keep Luce-Org#854's sync_page_stream_() (checked wait that clears page_work_queued_) and this branch's free_host_buffer_() helper. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…led snapshot branch Via the Luce-Org#848 branch merge. server/CMakeLists.txt keeps both new unit tests (test_kvflash_restore_plan.cpp and main's test_dflash_window.cpp). Co-Authored-By: Claude Opus 5.5 <[email protected]>
…led grid branch Via the Luce-Org#849 branch merge; no new conflicts. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
deserialize() put resident chunks back in identity block order with a fresh LRU order. A cold prefill has the same chunks resident but in rotated blocks, so attention summed the same rows in a different order: rounding-level logit differences that flipped near-tie tokens. On the R9700 pooled_restore_identity.sh failed at 35K and 64K (turn 3 differs between CONSUME=0 and =1). KVFLASH3 adds a pool layout section: block and LRU rank per chunk, the use clock and the free-list order. Into a pool of the saved size every chunk gets its own block back and evictions pick the same victims; another pool size keeps the KVFLASH2 behavior (residency only, identity order). LRU stamps are saved as dense ranks, which evict exactly like the raw clock, so pagers that behave alike serialize alike. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…estore) into kvflash pooled grid branch Co-Authored-By: Claude Opus 5.5 <[email protected]>
davide221
added a commit
to dusterbloom/lucebox-hub
that referenced
this pull request
Oct 24, 2026
…sion restore branch Via the Luce-Org#850 branch merge. server/CMakeLists.txt keeps this branch's live restore and resume tests plus main's test_dflash_window.cpp. Co-Authored-By: Claude Opus 5.5 <[email protected]>
davide221
added a commit
to dusterbloom/lucebox-hub
that referenced
this pull request
Oct 24, 2026
…estore) into kvflash session restore branch Co-Authored-By: Claude Opus 5.5 <[email protected]>
Main now equals the tested Luce-Org#849 tree, so this keeps the tested Luce-Org#850 tree unchanged (a plain 3-way merge would duplicate the Luce-Org#849 hunks). Co-Authored-By: Claude Opus 5.5 <[email protected]>
davide221
added a commit
to dusterbloom/lucebox-hub
that referenced
this pull request
Oct 25, 2026
…lash DeltaNet ladder branch Clean 3-way merge against the tested Luce-Org#850 head: Luce-Org#851 changes and the main-side changes (Luce-Org#876 sampler seeding, Luce-Org#863 PFlash cleanup) are both kept exactly. Co-Authored-By: Claude Opus 5.5 <[email protected]>
davide221
added a commit
to dusterbloom/lucebox-hub
that referenced
this pull request
Oct 25, 2026
…rg#850, Luce-Org#876, Luce-Org#863) into kvflash turn checkpoints branch Clean 3-way merge against the tested Luce-Org#851 head: Luce-Org#852 changes and the main-side changes are both kept exactly. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stacked on #849 (which is on #848). This PR's own commit is the last one (ccbe206).
Why
Pooled prefill used
min(ubatch, remaining)batches, counted from wherever the request started. So where a batch ended depended on the session's earlier history. A restored run could cut its batches differently from a cold run of the same prompt, and the two could then disagree on the output.What it does
kvflash_prefill_batch_endandkvflash_prefill_batchesinqwen35/prefill_helpers.hare pure functions with unit tests. The remainder never produces NaN.LUCE_KVFLASH_SESSION). It is documented inserver/docs/PREFIX_CACHE.md.Tests
test_kvflash_restore_plan.cpp.test_server_unit(CUDA build): 1052 passed, 0 failed, 7 skipped.Rebased on 2026-10-23 onto #848 at e35391f (its review fixes). The diff against the previous head is exactly those fixes; this PR's own changes are unchanged. The test counts above were measured before the rebase.