Profile field memfd_extents: ple_only (needs kar_source memfd and an explicit prefix_cache_mib; default full, not serialized). The sealed memfd keeps the archive's length and file offsets but holds only the ~16 GB the serve path reads (PLE rows, header, directory, books); everything else is a hole, so readers need no change. - deploy: sealed::seal_extents (parallel copy restricted to the extents, holes elsewhere, same seals; st_blocks test), leleka seal-exec [--sparse-ple] (Rust form of leleka-seal-exec for A/B runs outside leleka run), supervisor seals sparse, passes --kar-sparse-extents-sha256 to every engine, never attests or stamps the archive from a sparse copy, refuses to start without a pinned digest (PLE_EXTENTS_SHA256, or LELEKA_KAR_EXTENTS_SHA256 while that is unmeasured); resource planner, preflight RAM minimum and the inherited-descriptor credit use the memfd's real size. - engine: --kar-sparse-extents-sha256, InheritedKarArchive::open_sparse, verify_sparse, extents handed to the governor. - leleka-memory: FdMapping::lock_ranges, compact-space heal walk, guard_archive_extents. Locking or populating a hole allocates a zero page: tested with a negative control next to the extents-only test that asserts st_blocks does not move. - scripts/e2e/decode/sparse-memfd-ab.sh (paired 5-turn 245k A/B + teacher-forced identity gate; NOT queued, not a holder job), docs/kar-sparse-memfd.md. Not run on a GPU or against the real archive. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
7.7 KiB
Sparse KAR memfd: only the bytes the serve path reads (experimental, off by default)
Status: implemented on branch claude/sparse-memfd, default OFF. Nothing here is a speed or RAM result: the numbers marked E are derived, the A/B that would turn them into
results is scripts/e2e/decode/sparse-memfd-ab.sh and has not been run. Design and evidence: docs/design/memory-single-copy-and-elastic-state.md (section A.4, B0, T1-T4).
What and why
The 55,295,529,472-byte level-2 KAR archive is copied into a sealed memfd (shmem, 51.5 GiB) for the life of the service. The engine reads one thing from it:
the PLE n-gram rows (read_rows_chunk: 10 B of scales and two 20-byte plane rows per row; 320,001,536 rows = 16,000,076,800 B) plus the header, the directory and two 8-byte books at load.
The rest is a level-2 re-encoding of the shard-0 tensors that the resident expert bank was built from (from GGUF shard 0, not from the memfd) and nothing on the serve path reads it
(E until the census below prints it).
memfd_extents: ple_only makes the supervisor copy only those byte ranges (page-aligned, merged) into a memfd of the same length and the same file offsets; everything else is a hole
(no page allocated, reads return zeros, nothing reads them). Readers need no change.
The census (task 1): what the archive holds and what the serve path reads
crates/model-format/examples/kar_census.rs reads the 256-byte header, the names and the directory (about a megabyte) and nothing of the payload. It prints every record (index, kind,
section offset and size, elements, stored planes, bytes the serve path reads, bytes a sparse copy holds), the totals per kind (ple-rows (read), nested (never read), raw (never read)),
the per-partition read ranges, and these lines for the claim "71 % is never read": serve_read_exact_bytes, sparse_copy_bytes, never_read_bytes (with percentages of the archive),
unread_records_section_bytes, bytes_before_first_ple_section.
Run it on the server (read-only, light I/O). Build once on the laptop in WSL (or on the server under /data/leleka-e2e/sparse-memfd/, never in ~/work):
# laptop, WSL, inside the worktree: a 650 KB dynamic binary (glibc, works on the server's Ubuntu 26.04)
cargo build --release --locked -p leleka-model-format --example kar_census # -> target/release/examples/kar_census
# copy that one small file to the server, e.g. /data/leleka-e2e/sparse-memfd/kar_census (the orchestrator does this)
# server: the table, the totals and the extents (reads ~1 MB)
/data/leleka-e2e/sparse-memfd/kar_census /home/para/work/leleka/models/leleka-gates/assets/qwen-iq3-s-l2-v4.kar --extents > /data/leleka-e2e/sparse-memfd/census.txt
# only the totals and the PLE partitions: ... kar_census ARCHIVE --summary
--hash-extents [--throttle-mib-s N] additionally reads the extents (about 16 GB, default 200 MiB/s, about 80 s) and prints the pinned digest and the line to commit
(const_line: pub const PLE_EXTENTS_SHA256 ...). That is heavy I/O on the model volume: run it only when the host is steady, and only for an archive that has matched its full pinned SHA-256
(leleka verify stamp). Without that digest a sparse copy cannot be verified, and the supervisor refuses to start one.
Mechanism
kar_extents.rs(model-format):KarInspection::serve_reads(PLE_PART_NAMES)derives the ranges from the directory with the sameVerifiedPartgeometryread_rows_chunkuses;normalize_extentspage-aligns and merges (the last extent is clamped to the archive length);digest_extentsis the pinned hash (SHA-256 over a domain tag, the extent list, the extent bytes).VerifiedKarPleArtifact::verify_sparse(source, profile, extents_sha256): same identity and partition checks asverify, no stamp or attestation (they speak for the whole archive), refuses a read range outside the extents, hashes the extents and compares with the pin (KAR sparse extents SHA-256 mismatch). The directory digest in the header (checked byinspect, inside the extents) covers the rest. The full-archive verifier does not accept a sparse copy (tested).- Pin:
PLE_EXTENTS_SHA256inkar_extents.rsis all zero until measured. While it is zero the operator suppliesLELEKA_KAR_EXTENTS_SHA256(64 hex digits from the census) and the supervisor logs that it is experimental; once it is committed the binary's value wins and an environment value is ignored. leleka_deploy::sealed::seal_extents: the parallel copy of the full fill restricted to the extents (final length first, holes elsewhere, same seals).leleka seal-exec --file ARCHIVE --expect-bytes N [--sparse-ple] -- COMMAND {FD}is the Rust form ofleleka-seal-execwith the sparse copy, for runs outsideleleka run.- Profile field
memfd_extents:full(default, not serialized) orple_only. Validation: needskar_source: memfd, and an explicitmemory.prefix_cache_mib(or0):autois 75 % of the memory that is free when the stack opens, so it would hand the freed ~36 GiB to the cache.memfd_bytes()(16.1 GB estimate until the census prints the exact figure) feeds the resource planner, the preflight RAM minimum and the inherited-descriptor credit. A plan that falls back to the mapped form resets it tofull. - Supervisor: sparse seal,
--kar-sparse-extents-sha256 HEXto every engine (also after a restart), no--kar-attested-by-stamp, no stamp of the archive recorded from the sparse memfd. - Engine:
InheritedKarArchive::open_sparsederives the extents from the memfd's own header and directory;load_pinned_inherited_archiveverifies withverify_sparseand checks that the extents the governor locks are exactly the ones the verifier hashed. - Memory governor:
guard_archive_extentslocks (FdMapping::lock_ranges), heals (compact_piecewalks the concatenation of the extents) and reports (extent_bytes) the extents only. Locking or populating a hole allocates a zero page for it: tested with a negative control (locking_the_whole_mapping_of_a_sparse_memfd_does_allocate_the_holes...) next to the extents-only test that assertsst_blocksdoes not move.
Prefix hard cap (task 2)
The 5090 profile carries memory.prefix_cache_mib: "6144" (what auto resolves to there today, 5.7-6.1 GiB; a 245k session needs about 4 GiB). An explicit value is clamped to the host and never grows
(leleka_memory::plan::explicit_prefix_budget; tests: +37 GiB available leaves it at 6144 MiB, auto grows by more than 20 GiB). The engine logs prefix cache budget: N MiB (...) for auto, explicit and off.
The A/B (not run)
scripts/e2e/decode/sparse-memfd-ab.sh is not a holder job (the holder's 55 GB memfd and a second one do not fit): run it with the holder stopped. Prerequisites and arms are in its header. Acceptance gates G1-G4:
bitwise-identical top-16 rows (A/A and sparse against full), system Shmem lower by 36.6 GiB +- 1.5 and MemAvailable higher by >= 35 GiB at turn 5 with zero swap, decode and TTFT within the run-to-run noise,
the prefix store held at 6144 MiB. Until that passes, ple_only stays off.
Not verified
- The census numbers of the real archive (the 36.6 GiB / 71 % are derived from two independent derivations in the design doc, not read from the directory).
PLE_EXTENTS_SHA256of the real archive; the time of the 16 GB extents hash at engine start (E: about 10 s, single-threaded SHA-256 over memory).- Everything on the GPU: identity of the logprobs, speed, the engine's behaviour with a sparse memfd end to end (
open_sparseand the supervisor'sserve_extentshave only been exercised on synthetic archives and a fake engine; the real directory has 700+ records and the pinned part names). mlockof 16 GB under the server's RLIMIT_MEMLOCK (8 MiB by default: the lock falls back to detect + heal exactly as for the full memfd).