Files
leleka/crates/model-format
Pavlo RatushnyiandClaude Sonnet 5.5 40fc3eab2b Fast cold load: large-block, deep-queue reads for the sealed archive copy, the expert bank and the hash passes
The cold start of the 5090 server is dominated by reading two things: the 55 GB KAR archive (copied into the sealed memfd) and the 50 GB of
routed-expert tensors of shard 0 (copied into the page-locked bank). A plain read() loop asks the NVMe for one 128 KiB readahead window
at a time (queue depth 1-2; the drive's max_sectors_kb is 128): 8-35 MB/s after a reboot, 2003.8 s for the first holder
(docs/experiments/baseline-pair-5090.md section 9).

* model-format/bulk_read.rs (new, no unsafe): N threads each read blocks of 8 MiB. Direct (default): O_DIRECT through the open file's
  /proc/self/fd entry (no path lookup) into 4 KiB-aligned bounce buffers, so the page cache is never touched and nothing has to be evicted;
  Advise: buffered pread with POSIX_FADV_WILLNEED ahead and DONTNEED behind (a bounded window); Plain: large pread + DONTNEED. Direct falls
  back to Advise where O_DIRECT is refused. Memory is threads x 8 MiB. Switches for A/B in one binary: LELEKA_LOAD_READ=direct|advise|plain,
  LELEKA_LOAD_THREADS, LELEKA_LOAD_CHUNK_MIB, LELEKA_LOAD_AHEAD_MIB; LELEKA_LOAD_READ=legacy keeps the old sequential loop for the bank.
* deploy/sealed.rs: the memfd fill uses it (seal_file_report also returns how the file was read; seal_file_with_identity is unchanged).
* gguf.rs read_file_span_into: spans of 64 MiB and more (the expert tensors) use it; smaller spans and every failure mode are as before.
* readahead.rs (the SHA-256/CRC passes of a first verification): WILLNEED 512 MiB ahead of the reader thread.
* examples/bulk_read_bench.rs + bulk_read_digest.py: offline cold-read measurement with a reproducible digest (SHA-256 over the SHA-256 of
  consecutive blocks), device-level read/write accounting so a shared disk shows.

Measured on rnr5090 (Lexar NM610PRO, DRAM-less, HMB; kernel 7.0.0-38; another agent's jobs share the drive, so every figure carries its
"other" volume; page cache of the span evicted first with posix_fadvise(DONTNEED), never drop_caches):
* whole KAR file, 51.5 GiB, new default (direct, 32 x 8 MiB): 152.9 s (362 MB/s) and 159.8 s (346 MB/s, with the digest), nothing else on the
  drive; Cached 62 -> 59 GiB (no cache growth). Target < 5 min met.
* old patterns, same day: one thread, 16-32 MiB reads (the holder's pattern) 65, 86, 105, 154 MB/s on 2-8 GiB cold slices (55 GB in 6-14 min);
  8 threads x 32 MiB buffered (the old sealed.rs pattern) 240 MB/s while sharing the drive; the recorded cold-after-reboot holder 2003.8 s.
* strategy matrix (4 rounds x 7 configs, fresh 2 GiB regions, MB/s per round, median of 4): advise 16x16 MiB 230 574 194 1126 (402);
  direct 32x8 185 554 1133 113 (369); advise 32x8 277 454 229 510 (365); plain 16x16 385 257 979 207 (321); direct 16x8 180 311 351 174 (245);
  direct 8x32 149 317 271 138 (210); one thread 65 201 123 86 (105). The drive's speed varies 10x between regions and moments whatever the
  strategy, so the multi-thread configs are indistinguishable here; every one of them beats one thread. Direct is the default because it
  leaves the page cache untouched (nothing to evict, the machine's free-memory state is not changed), not because it is faster.
* identity: an 8 GiB slice read with the new code has the same block-digest chain as a plain sequential pass in Python
  (8b8b21b830cf938b1f0cec60a5a8127d2489e33e1f853cd792b18497b2b081a6, both); unit tests compare every strategy with the source for aligned and
  ragged spans. The engine's own SHA-256/CRC check of the sealed memfd and the replay gate are in the GPU run recorded in docs/unattended.md.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-02 20:54:28 +01:00
..

Leleka model format foundation

leleka-model-format exposes the existing bounded GGUF v3 parser as an independent Rust crate. It depends directly on sha2 and the standard library; it has no build script, C/FFI shim, GGML, Strata process/source link, CUDA or Python dependency. The existing control plane reexports its gguf module, so preparation, inspection, tokenizer extraction and tensor-export consumers keep their public paths.

The original parser geometry and its three tests were moved unchanged from control-plane/src/gguf.rs. Tensor block geometry and the legacy Flash-Next metadata guard retain their upstream references; see the provenance record. Those helpers inspect metadata and byte layout, not tensor values or inference. Recognizing a quantization block size is not support for decoding or serving it.

Contract and limits

  • inspect validates GGUF v3 header structure and reports metadata/layout.
  • tensor_index additionally requires known block geometry, checked dimensions, alignment, bounded nonoverlapping tensor spans and consistent file bounds.
  • tensor_sha256 hashes one indexed tensor through a fixed 1 MiB buffer.
  • tokenizer_metadata extracts the existing Qwen35 tokenizer fields after header inspection; the separate tokenizer crate implements tokenization.
  • check_flash_next_profile is the existing specialized metadata admission check. It does not validate a complete model, tokenizer or numerical output.

All preexisting bounds/rejections remain unchanged. Path-based functions reopen the source; callers must keep the verified file stable between inspection and reading. File length checks do not detect same-size replacement or mutation. VerifiedGgufFile::open is a separate owned-file API: it accepts a sha256:<64 hex digits> artifact digest, opens one regular file, hashes the full file with a buffer of at most 64 KiB, and parses the strict tensor index through that same handle. The private file/index pair is retained; index() offers only an immutable borrow. stream_tensor validates a requested tensor number and byte range before delivering at most 64 KiB per callback, and tensor_sha256 hashes the indexed payload through that stream. Neither tensor operation reopens the pathname or allocates the full tensor. A consumer may explicitly collect chunks or return an I/O error to abort. Empty ranges at the tensor end are valid.

Renaming or replacing the original pathname after opening does not redirect this handle to a different file. The preliminary path metadata check rejects ordinary nonregular paths; the opened-file metadata check determines whether the accepted handle is regular. This does not make pathname lookup atomic against a malicious concurrent substitution during opening. The caller must supply a stable regular source for opening and prevent in-place writes for the entire verification, indexing and read lifetime. Keeping a file descriptor does not make its bytes immutable: even same-size writes to the same file remain visible and are not caught by file length checks. The stored artifact digest records verification at open, not a later content attestation. Size changes are refused before/after streaming, and short reads propagate errors; callbacks already delivered before an I/O failure cannot be retracted.

The existing path APIs and tensor geometry remain unchanged. Descriptor, tensor and tokenizer semantics, memory admission, GPU allocation, readiness and real-model parity remain separate work. The current strata-control still has its legacy GGML/build dependencies; extracting this crate does not remove them from that package or its runtime.

Standalone checks

cargo test --locked -p leleka-model-format
cargo clippy --locked -p leleka-model-format --all-targets -- -D warnings
cargo tree --locked -p leleka-model-format

These are container/parser checks with sanitized synthetic bytes. They do not establish inference, model quality, GPU operation or performance.

Owned-file tests use generated synthetic GGUF bytes: full-digest and tensor-byte oracles, multi-chunk and partial streaming, malformed digest, nonregular source, tamper, truncate, unsupported version/type and overlapping geometry rejection, range/overflow checks before callbacks, callback abort/recovery, Unix pathname replacement retaining the original file, and an explicit same-size in-place mutation counterexample. These tests do not qualify an immutable artifact store or a complete model loader.