#5375·LMCache

[RFC][MP][L2] End-to-end integrity check for the fs L2 adapter (CRC32 trailer, verify on load)

Author: malikoyvCreated Sep 27, 2026Updated Sep 28, 2026

Summary

The MP fs L2 adapter checks only a file's length when it loads a KV chunk. A file of the right size but with wrong bytes is served to the engine as a cache hit. The model then produces fluent garbage, and users debug the model rather than the cache.

This RFC proposes an opt-in integrity check:

  • On store, write a small trailer (magic + CRC32) after the payload.
  • On load, verify it; a mismatch becomes a cache miss and the file is deleted.

A draft implementation with tests is ready. I'd like agreement on the on-disk format and the failure semantics before opening the PR.

Motivation

Silent-corruption reports keep reaching the tracker, and each was found by a person reading bad output rather than by LMCache:

  • #4991: fs_native failed stores stay visible and cache hits produce gibberish. The analysis there found two concurrent writers of one key sharing a staging file, which leaves a torn file in place.
  • #5263: a buffer is reclaimed while an L2 load is still writing into it.

A checksum does not fix any of these root causes. It turns an L2 content error into a detected miss, so the engine recomputes instead of silently serving wrong KV. Some related reports are not L2 content errors and a checksum would not catch them: #5344 (a tensor-parallel shard never written to L1) and #4247 (layout/view mismatch). This RFC is scoped to the L2 bytes.

Ways the same-size wrong bytes can reach fs today:

  1. Torn concurrent writes. Writers of the same key share one temp path. The Python fs adapter uses the same <name>.tmp pattern as fs_native.
  2. Files from another writer or version in a shared or reused directory.
  3. Media or firmware errors (silent data corruption): rare per drive, but non-zero at fleet scale.
  4. Power loss or crash. There is no fsync before rename, so after a crash a renamed file can contain stale or zeroed blocks.

What exists today:

Mechanism Covers
fs load length check Truncated files only
AES-GCM serde Tampering, but only when encryption with key management is enabled
POST /cache/checksums Manual GPU-block debugging, not the load path

Proposal

Config. A new fs adapter field, a string rather than a boolean so it can gain algorithms later:

json
{"type": "fs", "base_path": "/data/lmcache/l2", "checksum": "crc32"}

"none" is the default and keeps today's on-disk format byte for byte.

On-disk format (with crc32):

<payload bytes> | trailer: magic "LMC1" (4 B) | zlib.crc32(payload) (4 B, little-endian)
  • Under use_odirect, the trailer is padded to one filesystem block so all writes stay aligned. Readers only look at the first 8 bytes after the payload, so buffered and O_DIRECT files are readable by either mode.
  • The magic carries a format version (LMC1), so the layout can change later without misreading old files.

Load semantics.

  • The payload is re-hashed off the adapter's event loop (run_in_executor; zlib.crc32 releases the GIL on large buffers).
  • A mismatch, missing trailer or unknown magic becomes a miss for that key. The prefetch controller already treats it as a gap, and the engine recomputes.
  • The file is deleted, and listeners get on_l2_keys_deleted. Otherwise the key would miss forever: _execute_store skips existing files, and fs never evicts.

Accounting. Store reports the full file length, so usage stays consistent with delete(), which subtracts st_size.

Related fix. On the O_DIRECT path, check the byte count os.write returns. Today a short write is renamed into place as if complete.

Cost

A typical chunk: Llama-3-8B, bf16, 256 tokens, about 32 MiB.

Overhead
Space 8 B per file (one extra 4 KiB block allocated; 4 KiB under O_DIRECT), about 0.01%
Extra read bandwidth 8 B per load (at most one extra 4 KiB page)
CPU per store and per load One CRC32 pass. Measured on one host with zlib-ng 1.3.1: about 48 GB/s per core, about 0.7 ms per 32 MiB. Classic zlib is several times slower.

Space and bandwidth are negligible; the real cost is latency. _execute_load handles keys sequentially, so each CRC adds directly to that chunk's load time. That's why this is opt-in. Hashing chunk N while reading chunk N+1 is the obvious follow-up. I'll post a none vs crc32 load-throughput table on NVMe with the PR.

Compatibility

  • Default "none": no behaviour or format change.
  • Enabling "crc32" on an existing directory: old files have no trailer, so each is discarded on first access, a one-time miss.
  • A "none" reader on files written with "crc32": reads the payload length and ignores the trailer, so it works.
  • Shared directories: all writers sharing a directory should use the same setting. It's documented.
  • Shared directories with fs_native: it uses the same filenames, so the same rule applies. This RFC changes only the Python fs adapter.

Alternatives considered

Option Why not (for now)
A checksum serde type (runs through the existing SerdeL2AdapterWrapper) Covers every backend, but an adapter takes only one serde today, so it can't combine with fp8/turboquant; it also adds a temp-buffer copy. It's a good follow-up once serdes can chain.
xattr for the CRC Not available on every filesystem (some NFS/FUSE setups); lost when files are copied.
A separate checksum file per object Two files per object, and no atomic publish of both.
XOR / parity No faster in practice (numpy XOR fold about 42 GB/s vs zlib-ng CRC32 about 48 GB/s on the same host), and it misses word reordering and paired bit flips.
crc32c (google-crc32c) / xxh3 Faster on some CPUs but adds a dependency. The checksum string leaves room to add them.
Verifying at lookup Would need a full read per lookup; load time is the natural point.

Open questions for maintainers

  1. Delete on mismatch: is deleting the file acceptable, or should it be moved to a quarantine directory for debugging (with a size cap)?
  2. Default: keep "none" for now, and revisit making "crc32" the default once the throughput numbers are in?
  3. Metrics: reuse lmcache_mp.l2_prefetch_failure with a new reason="corrupt", or add a dedicated lmcache_mp.l2_integrity_failure{l2_name}? Corruption on a flash tier should alert differently from a plain miss.
  4. fs_native: should the C++ connector adopt the same trailer format in a follow-up, so both adapters stay interchangeable on one directory?

Plan

  1. PR 1 (ready #5374): the fs adapter checksum option, the tests below, and a docs section in fs.rst.
  2. PR 2: the metric (question 3) and a flip-a-byte mode in fault_inject_l2_adapter for end-to-end tests.
  3. PR 3: overlap the CRC with reads, plus the throughput benchmark.
  4. Follow-up: fs_native trailer support, or a checksum serde type once serdes can chain.

Tests (tests/v1/distributed/test_fs_l2_adapter_checksum.py, CPU-only):

  • Round trip with checksums on.
  • Single-bit flips in the payload and in the trailer each become a miss and the file is deleted.
  • A discarded key can be stored again and loads correctly.
  • Usage returns to 0 after store then delete.
  • Compatibility between none and crc32 in both directions.
  • The O_DIRECT block-padded layout, and cross-mode reads.