Skip to content

feat(kvflash): pager serialize/deserialize + QK residency library#466

Open
dusterbloom wants to merge 2 commits into
Luce-Org:mainfrom
dusterbloom:feat/kvflash-library
Open

feat(kvflash): pager serialize/deserialize + QK residency library#466
dusterbloom wants to merge 2 commits into
Luce-Org:mainfrom
dusterbloom:feat/kvflash-library

Conversation

@dusterbloom

@dusterbloom dusterbloom commented Jun 30, 2026

Copy link
Copy Markdown
Collaborator

What

Model-agnostic KV-paging primitives in server/src/common/, already consumed by the qwen35/qwen35moe, gemma4, and laguna backends. This is the reusable foundation extracted from the kvflash snapshot-chain work — lands independently of any backend integration.

Why split it out

The full kvflash snapshot chain (~4000 LOC) couples tightly into qwen35_backend.cpp. The library (pager + QK scoring) does not — it touches only common/ headers + tests. Landing it standalone gives every backend a shared, tested KV-paging + residency layer without the integration-review burden.

Contents

  • kvflash_pager.hserialize(max_chunks)/deserialize v1 format with ledger section (was_resident + qk_score + dtype) for pooled-prefix snapshot/restore; critical-chunk pinning + deadlock guard; floor_to_chunk; identity_prefix_covers (preserved for existing consumers).
  • kvflash_qk.h — cosine QK residency scoring with seeded-fallback; overflow-safe seeded_n (limit defaults to 0, not n_chunks, preventing OOB reads when a caller omits the length).

Verification

  • Builds against main's llama.cpp submodule (no fork dependency).
  • test_kvflash_pager PASS: serde + pinning + partial-serialize + ledger round-trip.
  • test_kvflash_qk PASS: pure-math scoring incl. seeded-sentinel fallback.
  • Full dflash_common (main's server code) recompiles — identity_prefix_covers keeps laguna/gemma4 working.
  • CPU-only; no GPU required.

Scope

5 files, +713/−18. No server-backend files, no bench/docs noise. Supersedes the library portions of #429/#445/#430/#446 (those integration slices remain tracked separately).

Follow-up PR (qwen35 pooled snapshot-chain integration) will stack on top of this.

Review in cubic

@cubic-dev-ai cubic-dev-ai Bot 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.

1 issue found across 5 files

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread server/src/common/kvflash_qk.h Outdated
@dusterbloom

Copy link
Copy Markdown
Collaborator Author

Re-triggering cubic to review the seeded_n fix (commit 81ed9f7).

Model-agnostic KV-paging primitives in server/src/common/, consumed by
qwen35/qwen35moe, gemma4, and laguna backends.

kvflash_pager.h:
- serialize(max_chunks)/deserialize v1 format with ledger section
  (was_resident + qk_score + dtype), enabling pooled-prefix snapshot/restore
- critical-chunk pinning + deadlock guard
- floor_to_chunk helper
- identity_prefix_covers query (preserved for existing consumers)

kvflash_qk.h:
- cosine QK residency scoring with seeded-fallback + overflow-safe
  seeded_n (limit defaults to 0, not n_chunks, to prevent OOB reads)

Tests: test_kvflash_pager (serde + pinning + partial-serialize + ledger
round-trip) and test_kvflash_qk (pure-math scoring). Both CPU-only.
kvflash_qk_chunk_scores read seeded[c] for all c < n_chunks, but the seeded
buffer comes from a restored ledger whose chunk count can be smaller than the
current n_chunks (pool grew after restore) → out-of-bounds read.

Fix: add an explicit seeded_n length parameter; chunks at/above seeded_n fall
back to missing_score. Caller (KvFlashTargetQkScorer) now passes
seeded_scores_n(). Test test_kvflash_qk gains a phase2b case that constructs a
3-entry seeded buffer over 6 chunks and asserts no OOB.

Addresses cubic P1 on kvflash_qk.h seeded fallback.
@dusterbloom dusterbloom force-pushed the feat/kvflash-library branch from 69d51d1 to f438230 Compare July 9, 2026 13:56
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.

1 participant