Skip to content

Fix TP16 host prefix records and retain deep DRAM prefixes - #264

Merged
lukealonso merged 2 commits into
mainfrom
martin/kimi-prefix-cache-dram
Nov 11, 2026
Merged

lukealonso merged 2 commits into
mainfrom
martin/kimi-prefix-cache-dram

Conversation

@voipmonitor

@voipmonitor voipmonitor commented Nov 11, 2026 •

Copy link
Copy Markdown
Collaborator

Why

Host checkpoint publication fails when the rank/plane catalog exceeds its fixed 4 KiB header. With sixteen ranks and 48 planes per rank, the u64 plane-size format needs 6,513 bytes. Compact plane sizes can represent that geometry without changing payload offsets or truncating values.

The DRAM cache also discarded interior continuation rows even with ample retention space. Keeping those rows allows deep-prefix rewinds to restore an endpoint directly. Pressure handling must exclude reserved transfer intake and reclaim redundant rows before evicting useful endpoints.

Changes

  • Add host-record version 3 with u32 plane sizes and unchanged u64 row sizes. The sixteen-rank, 48-plane header occupies 3,441 bytes. Fresh geometries with a plane larger than u32::MAX retain the lossless u64 encoding; every encoding still has to fit the fixed header block.
  • Read versions 1 and 2, preserve their bytes on unchanged rewrites, and validate their original size limits. Legacy replication-flag changes select version 1 or 2 as appropriate. Cross-rank agreement hashes canonical header fields so equivalent legacy and v3 records are not rejected because their wire bytes differ.
  • Retain redundant DRAM rows while free retention space exceeds two continuation rows. Exclude transfer intake from that headroom. Reclaim eligible redundant rows at the threshold or before an incoming allocation needs to evict other records. Keep pin/readiness checks, and discard pending thinning keys when records are removed or evicted.
  • Log host-store submission and asynchronous completion errors while preserving existing metrics, retries and checkpoint pin handling.

Result and compatibility

The compact format permits the TP16 geometry to be published. Retained continuation rows can avoid replay from an earlier endpoint, while pressure reclamation preserves the transfer reserve and bounded retention budget. These behaviors are implemented and qualified by CPU record, store and recovery tests; no inference-speed improvement is claimed.

Versions 1 and 2 remain readable, including their previously supported wide-plane geometries. Version 3 is not readable by older binaries: their recovery logic treats unsupported record versions as invalid and deletes them, so downgrading loses those cached checkpoints. Model-core format, graph export and model arithmetic are unchanged.

Validation

Review validation on the corrected branch, without GPU or end-to-end execution:

  • 401 Selene unit tests passed. Added regressions cover warm headroom separately from intake, one- and three-slot continuation rows, larger incoming records, pinned-row release and stale-key cleanup across repeated chains. Metadata-only tests cover 4 GiB planes without allocating payloads, exact legacy rewrites, replication flags and symmetric v1/v3 and v2/v3 rank agreement; different geometries remain rejected.
  • cargo check --locked --manifest-path flare-cli/Cargo.toml passed. Its unused-method warning concerns dominates_padding, outside this change.
  • git diff --check passed. Repository-wide formatting differences outside this change were not rewritten. The existing unreadable-file test bypasses its permission check when run as root.

Reproduce from the repository root:

cargo test --locked --manifest-path selene/Cargo.toml --lib
cargo check --locked --manifest-path flare-cli/Cargo.toml

Earlier serving evidence reported for the submitted implementation, not rerun for these corrections:

  • A native TP16 store/drain and DRAM-restore run reused a 153,295-token prompt as 132,593 GPU-cached tokens plus 20,702 host-restored tokens, with zero new prefill and zero host-store or host-fetch errors.
  • A separate C4 run with a 128 GiB DRAM prefix cache completed four long sequential requests and four concurrent 1,024-token requests without host I/O errors. It populated the cache but did not exercise a host hit, so it does not qualify deep restoration or the corrected pressure behavior.

🤖 Generated with Claude Code

voipmonitor and others added 2 commits November 11, 2026 02:15
Version 1 and 2 record headers cannot describe the Kimi TP16 geometry: sixteen ranks with 768 planes require 6513 bytes, exceeding the fixed 4096-byte header block and rejecting every host-cache publication.

Add record format version 3 with u32 plane sizes, reducing that header to 3441 bytes without changing segment offsets. Preserve version 1 and 2 decoding and byte-identical rewrites, keep version metadata out of semantic header equality, and reject plane sizes that do not fit the new encoding.

Keep redundant interior rows while the DRAM pool has more than two row slots of headroom. Resume eager row thinning automatically under real memory pressure so deep-prefix restore does not trade unbounded DRAM growth for retention. Update retention tests to cover direct deep-endpoint restores and pressure behavior.

Log host-store submission and completion errors instead of silently discarding their causes.

Validation: all 393 Selene unit tests pass. Native TP16 store/drain and DRAM restore tests show zero I/O errors and a complete 153295-token prompt reused as 132593 GPU tokens plus 20702 host-restored tokens with zero new prefill.

Those service tests used the db29 C1 diagnostic package. A separate C4 long-request CUDA_ERROR_ILLEGAL_ADDRESS remains under investigation and is not covered by this commit. Previous-core serving beyond signed package inspection was not separately revalidated.

Design-Signed-Off-By: Kimi (Kimi); design-review=cfcf
Co-Authored-By: Kimi <noreply@anthropic.com>
Reserved transfer intake made the row-thinning threshold unreachable during
ordinary retention pressure. Reclaim eligible redundant rows using free
warm slots, and do so before incoming allocations evict reusable endpoints.
Remove queued thinning keys with removed or compacted records so bookkeeping
remains bounded by the resident index. Pinned rows remain protected.

Record validation used the v3 size limit even for decoded legacy headers,
and cross-rank agreement hashed wire versions despite semantic equality.
Validate the stored encoding, retain lossless legacy writes for wide planes,
select valid legacy replication versions, and hash a canonical encoding for
rank agreement. Existing record bytes remain stable when rewritten without
semantic changes. Update the cache format and retention documentation.

These changes preserve usable endpoints under pressure and keep equivalent
records from being rejected across format versions. Metadata-only tests cover
4 GiB planes, byte-exact rewrites, replication flags and mixed-format agreement.
Pressure tests cover intake reserves, multi-slot rows, larger incoming records,
pins and repeated chain removal. All 401 Selene tests and the CLI cargo check
pass. No GPU or end-to-end serving test was run during this review.

Design-Signed-Off-By: Codex GPT-6; design-review=cfcf
Signed-off-by: Martin Vit <martin@voipmonitor.org>
@lukealonso
lukealonso merged commit f7c468c into main Nov 11, 2026
3 of 5 checks passed
@yatesdr

yatesdr commented Nov 11, 2026

Copy link
Copy Markdown
Collaborator

I reviewed this after merge, against the rest of Selene, assuming every process runs the new binaries. I found no correctness problems: v3 encode/decode, rank agreement, pool announcements, replicated records, pins and readiness all look right, and I found no path that frees a row while something is reading it. Two things don't do what the PR intends.

1. CI: the selene-manager header test now fails

selene-manager/src/record.rs:137-144, persisted_header_matches_the_descriptor_layout, builds a fresh header from a small descriptor and asserts its bytes equal tests/fixtures/flare-header-v1.bin. Fresh headers now pick v3 (version_for_sizes), so the byte comparison at line 144 fails. The semantic assert_eq!(decoded, planned) above it still passes.

The manager itself handles v3 correctly; only the expected bytes are stale. The validation in the description ran selene/ only, which is why this was missed. The fix is to add a v3 fixture, assert fresh headers against it, and keep the v1 fixture as a decode and legacy-rewrite check.

2. Memory + pool: proactive retirement runs before row thinning

The PR intends to "reclaim redundant rows before an incoming allocation needs to evict other records." On the store path that holds: memory.rs:399-407 calls thin_rows_for(needed) before retire_for. The poll path doesn't:

  • MemoryStore::poll calls settle → thin_rows() → thin_rows_for(2 * row_slots), which does nothing while free − intake > 2 × row_slots.
  • Then, for a standalone store with spill (memory + pool), memory.rs:1645-1648 retires announced, durable leaves until free + clean ≥ 2 × intake.
  • intake_slots is 2 × max_active × row_slots + 2 × WINDOW_PAGES, which is always more than 2 × row_slots.

So whenever intake + 2 × row_slots < free < 2 × intake − clean, every poll drops the local copy of useful endpoints the pool already holds, while redundant interior rows stay resident. Before this PR those rows were already thinned, so this band didn't exist. Nothing is lost, since the endpoints remain in the pool, but it costs pool fetches and manager traffic, the opposite of what the change is for. This comes from reading the code; I haven't measured it.

A minimal fix keeps the store-path ordering in poll:

if self.core.spill {
    let need = self.reserve(2 * self.core.pool.intake_slots());
    if need > 0 {
        self.core.thin_rows_for(usize::MAX); // reclaim redundant rows first
        self.core.retire_for(self.reserve(2 * self.core.pool.intake_slots()));
    }
}

A regression test would be a standalone spill store with a three-record chain and free slots in that band. Assert that poll thins the middle row and retires nothing.

Paths this doesn't touch

  • The default DRAM + disk (composite) tier still thins eagerly: composite.rs:516 calls drop_row directly and bypasses the new gate. If your TP16 runs use a disk tier, only the v3 format part of this PR affects them; the retention change doesn't. Worth checking which mode produced the 153K-token result.
  • Memory-only without a pool behaves as described. Once the threshold is crossed, it drops every redundant row in one pass rather than just enough. That's consistent with the tests; I'm noting it in case you wanted gradual reclamation.

yatesdr added a commit to yatesdr/flare-local-inference that referenced this pull request Nov 18, 2026
Composite and disk stores eagerly removed redundant parent rows even
with retention headroom. A single child does not establish that its
parent's endpoint is unused: a later conversation branch can still
resume there. MemoryStore already keeps such rows until pressure
(NVIDIA#264, NVIDIA#302).

Reclaim eligible rows in least-recently-used order under retention or
incoming-allocation pressure, stopping once headroom returns, and do it
before disk maintenance hands any leaf to the pool (leaves already held
for handover count toward the target). A composite spill is lossless (the
row goes to disk with the record), so it does not thin first; rows are
reclaimed where an allocation needs room. Share candidate ordering and free-retention calculations with
MemoryStore, queue restored memory rows, and remove queued keys at every
removal site (as MemoryStore does), so the queue holds only resident
records. Keep the NVIDIA#336 BACKLOG / 2 cap on in-flight truncates; at the cap,
make_room can evict a whole record before reclaiming further rows.

Parent endpoints stay reusable while space remains, and colder redundant
rows go before any other record under pressure. The disk tier now writes
the row of every spilled parent, which costs write bytes up front and a
truncate later under pressure.

Six regressions fail without the change and pass with it: rows kept
with headroom and reclaimed in access order under pressure (composite and
disk), rows reclaimed before disk maintenance retires leaves, and the thinning queue holding only resident records after expiry,
eviction, discard and retirement (composite and disk). The composite,
disk-store and memory modules pass on Linux.

Not tested: GPU serving with a branching workload.

Design-Signed-Off-By: yatesdr; design-review=ed22
yatesdr added a commit that referenced this pull request Nov 19, 2026
persisted_header_matches_the_descriptor_layout has failed on main since
#264 moved record headers to version 3: the manager now encodes version
3, and the test still compared the encoding with the version 1 fixture.

Add a version 3 fixture and compare the encoding with it. Keep the
version 1 fixture to check that a header written before #264 still
decodes to the same header, and keep its invalid-field checks.

On cn4 (Linux, x86_64) the manager's record tests pass; before, this
test failed with a version byte of 3 against 1.

Design-Signed-Off-By: yatesdr; design-review=1af4
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.

3 participants