Skip to content

feat(kvflash): pager serialize/deserialize, truncate, chunk pins and score ledger - #848

Merged
davide221 merged 11 commits into
Luce-Org:mainfrom
dusterbloom:kvflash/2.0a-A-pager
Oct 24, 2026
Merged

davide221 merged 11 commits into
Luce-Org:mainfrom
dusterbloom:kvflash/2.0a-A-pager

Conversation

@dusterbloom

@dusterbloom dusterbloom commented Oct 15, 2026 •

Copy link
Copy Markdown
Collaborator

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 in deserialize, the buffers already allocated are freed.
  • truncate(P). Drops 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: by recency, or by score when a scorer is attached.
    • The LRU clock is renumbered in logical order.
  • Pins, the score ledger and helpers. pin_range (half-open) and unpin_all; reset clears 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:

Defect Fix
truncate refill could leave protected chunks paged out the refill pages protected chunks back before ranking the rest (9932771)
deserialize accepted a blob whose V dtype differs from the pager's the V dtype is a ninth header field and is validated; the blob magic is now KVFLASH2 and KVFLASH1 blobs are refused (d482946)
pins from a previous attach survived and could starve the pool of slots attach clears the pins (40b7dba)
pin_range narrowed chunk indices to int before checking them (segfault on a large range) the range is checked in 64 bits first; spans longer than the pool are refused (53aa04f)
holes came back from a blob as recallable zero KV the ledger stores a materialized flag per chunk; holes restore as holes (487effb)
the serde tests could not tell swapped segments apart the fill pattern differs per chunk offset; a test swaps every pair (529ea55)
12 pager tests were skipped on hosted CI (no pinned memory) pageable host backing for tests, so all 26 pager cases run with the GPU hidden; the skip macro is gone (9cf5978)
the blob layout assumed a little-endian host silently static_assert and 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.cpp and test_kvflash_pager_truncate.cpp, registered in server/CMakeLists.txt.
  • test_server_unit (CUDA build) on e35391f: 1049 passed, 0 failed, 7 skipped (1034 on b5e393d). Main 6f2b179 gives 1016 passed.
  • With the GPU hidden (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.

View guided diff

…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.
@davide221

Copy link
Copy Markdown
Contributor

CI failed on b5e393d9

DeepSeek model-load test was killed mid-load on the Strix Halo runner; the kvflash change is not at fault

The only failing test loads a very large DeepSeek model on the GPU runner, and its process was killed partway through the load, most likely because the machine ran out of memory (the model buffer alone is about 91 GiB of the box's 125 GiB). Everything related to this pull request passed: the full 97-test GPU suite, including all pager-adjacent tests, was green. Re-running the job should clear it.

How to fix: Re-run the failed job; the test was killed by runner memory pressure, not by this change.

  1. In GitHub Actions, choose "Re-run failed jobs" for this run — the failure is a killed process on the self-hosted runner, not a code defect.
  2. If it fails again the same way, on the lucebox3 runner run sudo dmesg -T | grep -iE 'oom|killed process' to confirm the OOM kill.
  3. If OOM is confirmed, drop page cache before the model-backed step (sync && echo 3 | sudo tee /proc/sys/vm/drop_caches) and keep the box otherwise idle during that step.

test process killed on gpu runner (likely oom) · medium confidence · run

Details and evidence

Failed: GPU tests (Strix Halo Radeon 8060S, gfx1151 / ROCm) › Run model-backed ROCm tests

Job GPU tests (Strix Halo Radeon 8060S, gfx1151 / ROCm), step Run model-backed ROCm tests (.github/workflows/ci.yml:413-419). CTest reports 1088 - ds4_load_transactional (Subprocess killed) after 210.55 s and exits 8. The test's own output stops right after the loader banner and ggml_cuda: device 0 integrated, alloc 91.1 GiB (> 45% of 125.1 GiB RAM) -> unified (managed) memory, i.e. it died during the first real load_deepseek4_gguf call (server/test/unit/test_ds4_load_transactional.cpp:116). Subprocess killed means the child died by signal; no CTest TIMEOUT is set for this test (server/CMakeLists.txt:1835 is a plain add_test) and 210 s matches no configured limit, so this was an external SIGKILL — most consistent with the Linux OOM killer on the 125 GiB unified-memory APU while a ~91 GiB model buffer plus the model file's page cache were resident. I cannot confirm the OOM kill without the runner's dmesg, so the exact signal sender is inferred, not proven. The PR only adds kvflash_pager.h code plus unit tests that nothing in the backends calls yet; the failing test does not include that header, and the preceding 97-test ctest run (which covers the pager's build) passed 100%.

Other things to try

  • Add a flake guard to the model-backed step: ctest ... --repeat until-pass:2 for the ds4_load_transactional|LoadsConfiguredModel invocation.
  • Reproduce on the runner: DS4_TEST_MODEL=/opt/models/DeepSeek-V4-Flash-0731-ROCMFPX-MIX-STRIX.gguf ctest --test-dir $RUNNER_TEMP/rocmfp-build -R '^ds4_load_transactional$' --output-on-failure while watching free -g.
  • If OOM is confirmed and retries are not enough, make the test skip when free RAM is below the model size, or drop the second reload leg on unified-memory APUs.
Prompt for a coding agent
A GitHub Actions run failed in Luce-Org/lucebox. Please fix it.

- Pull request #848 (branch `kvflash/2.0a-A-pager`), commit b5e393d9f73a4190109583f1f5cc98b5215fe6c2
- Workflow: CI (https://github.com/Luce-Org/lucebox/actions/runs/39267441429)
- Failed: GPU tests (Strix Halo Radeon 8060S, gfx1151 / ROCm) › Run model-backed ROCm tests

Diagnosis:
Job `GPU tests (Strix Halo Radeon 8060S, gfx1151 / ROCm)`, step `Run model-backed ROCm tests` (`.github/workflows/ci.yml:413-419`). CTest reports `1088 - ds4_load_transactional (Subprocess killed)` after 210.55 s and exits 8. The test's own output stops right after the loader banner and `ggml_cuda: device 0 integrated, alloc 91.1 GiB (> 45% of 125.1 GiB RAM) -> unified (managed) memory`, i.e. it died during the first real `load_deepseek4_gguf` call (`server/test/unit/test_ds4_load_transactional.cpp:116`). `Subprocess killed` means the child died by signal; no CTest `TIMEOUT` is set for this test (`server/CMakeLists.txt:1835` is a plain `add_test`) and 210 s matches no configured limit, so this was an external SIGKILL — most consistent with the Linux OOM killer on the 125 GiB unified-memory APU while a ~91 GiB model buffer plus the model file's page cache were resident. I cannot confirm the OOM kill without the runner's `dmesg`, so the exact signal sender is inferred, not proven. The PR only adds `kvflash_pager.h` code plus unit tests that nothing in the backends calls yet; the failing test does not include that header, and the preceding 97-test ctest run (which covers the pager's build) passed 100%.

Proposed fix: Re-run the failed job; the test was killed by runner memory pressure, not by this change.
1. In GitHub Actions, choose "Re-run failed jobs" for this run — the failure is a killed process on the self-hosted runner, not a code defect.
2. If it fails again the same way, on the `lucebox3` runner run `sudo dmesg -T | grep -iE 'oom|killed process'` to confirm the OOM kill.
3. If OOM is confirmed, drop page cache before the model-backed step (`sync && echo 3 | sudo tee /proc/sys/vm/drop_caches`) and keep the box otherwise idle during that step.

Check out that commit, confirm the diagnosis, make the fix (adjust it if the diagnosis is wrong), and re-run the failing step locally before pushing.

Reply with /ci-fix and what to change to get a revised diagnosis.

@dusterbloom
dusterbloom marked this pull request as ready for review October 21, 2026 10:55
@dusterbloom

dusterbloom commented Oct 21, 2026 •

Copy link
Copy Markdown
Collaborator Author

#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 davide221 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

dusterbloom and others added 9 commits October 23, 2026 16:26
…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]>
@davide221
davide221 merged commit 0711d4c into Luce-Org:main Oct 24, 2026
4 of 5 checks passed
davide221 added a commit that referenced this pull request Oct 24, 2026
…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]>
davide221 added a commit to dusterbloom/lucebox-hub that referenced this pull request Oct 24, 2026
…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]>
davide221 added a commit to dusterbloom/lucebox-hub that referenced this pull request Oct 24, 2026
…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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants