Repository navigation
feat(kvflash): pager serialize/deserialize, truncate, chunk pins and score ledger - #848
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.
CI failed on
|
|
#848 through #852 are ready for review, in stack order. CI on the stack:
#852 update: the generation-end checkpoint could store a DeltaNet state ahead of its label when spec decode stopped at EOS or a cancel inside a verified block. 0e2ff1b fixes it the way the floor path already does: restore the pre-step state and replay only the emitted tokens. The failing test came first (d664068). Details and the R9700 gate are in #852's description. |
davide221
left a comment
There was a problem hiding this comment.
Code review
No issues found. Checked for bugs and logic errors in the changed code.
Reviewed b5e393d9 · 1 candidate issue 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.
…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]>
…tached (#886) #854 made a failed cudaStreamSynchronize abort, and synced the default stream whenever any KV tensor was attached. #848's pager serde and truncate tests attach CPU-backend tensors with pageable host backing on hosted CI, where the stub driver fails cudaStreamCreate and every sync. Host tensors are paged with memcpy, so nothing is ever queued for them: only sync when a page stream exists or a device tensor is attached. 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]>
…t pool layout restore) into kvflash turn checkpoints branch Luce-Org#852 carried the pre-10-23 Luce-Org#848-Luce-Org#851 chain, which conflicted with the rebased Luce-Org#851 in 10 files. This tree is the new Luce-Org#851 branch plus this PR's own commits (046516e, d664068, 0e2ff1b, b3ebe9b) re-applied, all clean; 342f22d duplicates Luce-Org#851's 784e521 and is already there. Co-Authored-By: Claude Opus 5.5 <[email protected]>
First of five stacked PRs for KVFlash session restore (2.0a): A pager library, B pooled snapshots, C 512-token prefill grid, D state ladder and live restore, E turn checkpoints. This PR's own commits are 54e9d0c (the feature), b5e393d and eight review fixes, 9932771 to e35391f (see Review fixes). This one is header and unit tests only. Nothing in the backends calls it yet; B and D do. It supersedes #466.
What it adds to
KvFlashPager(server/src/common/kvflash_pager.h)serialize(max_chunks)/deserialize(). A position-addressed blob of the first N chunks: resident rows, host copies and the per-chunk score ledger. A pager can be rebuilt from it later. The whole blob is checked before the pager is reset. If a pinned-host allocation fails indeserialize, the buffers already allocated are freed.truncate(P). Drops every logical position >= P, as if the pager had only seen P tokens.pin_range(half-open) andunpin_all;resetclears pins. Per-chunk score-ledger accessors,floor_to_chunk, and a memset of host-resident blocks.Review fixes (2026-10-23)
A second review of b5e393d found eight defects. Each one has a failing test first, then the fix:
truncaterefill could leave protected chunks paged outdeserializeaccepted a blob whose V dtype differs from the pager'sKVFLASH2andKVFLASH1blobs are refused (d482946)attachsurvived and could starve the pool of slotsattachclears the pins (40b7dba)pin_rangenarrowed chunk indices tointbefore checking them (segfault on a large range)static_assertand a byte-layout test (e35391f)Main has no code that writes pager blobs, so the format change breaks nothing that is merged.
Tests
test_kvflash_pager_serde.cppandtest_kvflash_pager_truncate.cpp, registered inserver/CMakeLists.txt.test_server_unit(CUDA build) on e35391f: 1049 passed, 0 failed, 7 skipped (1034 on b5e393d). Main 6f2b179 gives 1016 passed.CUDA_VISIBLE_DEVICES=-1, as the CPU CI job runs it), all 26 pager cases run and pass.No runtime path changes, so there is no benchmark for this PR. The GPU checks are on D and E, which use it.