83 lines
8.7 KiB
Markdown
83 lines
8.7 KiB
Markdown
# Sparse KAR memfd: only the bytes the serve path reads (the form of `rtx5090-32g`)
|
|
|
|
Status (claude/single-copy, 2026-11-02): the census was run on the real archive (below), the extents digest is pinned in the binary (`PLE_EXTENTS_SHA256`), the copy reads the extents with
|
|
the O_DIRECT bulk reader (no page-cache duplicate) and `rtx5090-32g` has `memfd_extents: ple_only`. The identity and A/B jobs are `scripts/e2e/decode/single-copy-ab.sh` (arms: main with the full
|
|
memfd / sparse memfd / `rtx5090-32g-file`); their results are recorded in the task report, not here. Design and evidence: `docs/design/memory-single-copy-and-elastic-state.md` (section A.4, B0, T1-T4).
|
|
|
|
Census of `qwen-iq3-s-l2-v4.kar` (2026-11-02, `kar_census`, reads about 1 MB): 1225 records, 2 PLE parts (16,000,077,312 B of sections, 16,000,076,816 B actually read per full sweep), 1223 nested records
|
|
never read (39,295,185,152 B); a sparse copy is **16,000,348,672 B (14.901 GiB, 28.94 %) in 2 extents**, `never_read_bytes` 39,295,180,800 (36.596 GiB, 71.06 %), PLE rows 320,001,536. Pinned digest of the
|
|
extents: `7568b1211419a7c10a9b39a86230f75b7fa9c02839e36c9b5f2494ec7af72143` (161.9 s at <= 300 MiB/s). The 144 routed-expert records of the archive are 262,144,256 B each (37.75 GB = 35.16 GiB in
|
|
all, 0.7506 of the 46.84 GiB the GGUF bank holds): see the report for why they are not a serve source.
|
|
|
|
## 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`):
|
|
|
|
```sh
|
|
# 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 same `VerifiedPart` geometry `read_rows_chunk` uses;
|
|
`normalize_extents` page-aligns and merges (the last extent is clamped to the archive length); `digest_extents` is 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 as `verify`, 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 by `inspect`, inside the extents) covers the rest.
|
|
The full-archive verifier does **not** accept a sparse copy (tested).
|
|
* Pin: `PLE_EXTENTS_SHA256` in `kar_extents.rs` is set (the value above). Had it been zero the operator supplies `LELEKA_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 of `leleka-seal-exec` with the sparse copy, for runs outside `leleka run`.
|
|
* Profile field `memfd_extents`: `full` (default, not serialized) or `ple_only`. Validation: needs `kar_source: memfd`, and an explicit `memory.prefix_cache_mib` (or `0`): `auto` is 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 to `full`.
|
|
* Supervisor: sparse seal, `--kar-sparse-extents-sha256 HEX` to every engine (also after a restart), no `--kar-attested-by-stamp`, no stamp of the archive recorded from the sparse memfd.
|
|
* Engine: `InheritedKarArchive::open_sparse` derives the extents from the memfd's own header and directory; `load_pinned_inherited_archive` verifies with `verify_sparse` and checks that the extents the governor locks are
|
|
exactly the ones the verifier hashed.
|
|
* Memory governor: `guard_archive_extents` locks (`FdMapping::lock_ranges`), heals (`compact_piece` walks 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 asserts `st_blocks` does 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_SHA256` of 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_sparse` and the supervisor's `serve_extents` have only been exercised on synthetic
|
|
archives and a fake engine; the real directory has 700+ records and the pinned part names).
|
|
* `mlock` of 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).
|