Dataset index cache: rank-0 crash mid-numpy.save leaves torn .npy that passes the isfile cache hit-test — restarted job crashes in dataloader
Problem
The blended-dataset index cache is written non-atomically and re-validated unsoundly:
- Rank 0 writes
.npyindex caches with plainnumpy.save(megatron/core/datasets/blended_megatron_dataset_builder.py,gpt_dataset.py,blended_dataset.py) — a crash mid-write (OOM, preemption, node failure) leaves a truncated file on shared storage. description.txtis written before the.npypayloads, so it cannot serve as a commit marker.- The cache-hit test on the next run is
os.path.isfile(...), which cannot distinguish torn from complete.
Effect
The restarted job accepts the torn cache; dataloader workers then fail at np.load(..., mmap_mode='r') with ValueError: mmap length is greater than file size — inside a worker of the restarted job, after it re-acquired its full multi-node allocation. The error points at the cache, not at the earlier crash. (On some systems beyond-EOF mmap access may SIGBUS instead — same class.)
Scope note: requires a rank-0 crash during the (minutes-long) first-time cache build of a dataset configuration; once torn, the failure recurs on every restart until the cache directory is manually cleaned.
Reproduction
Standalone script (no GPU needed): real numpy.save of a 1M-element index → truncate at 70% (simulated crash) → the production hit-test (os.path.isfile) accepts it → np.load(mmap_mode='r') raises the ValueError above.
import os, tempfile, numpy as np
with tempfile.TemporaryDirectory() as d:
cache = os.path.join(d, "dataset_index.npy")
arr = np.arange(1_000_000, dtype=np.int64)
np.save(cache, arr)
full = os.path.getsize(cache)
with open(cache, "r+b") as f:
f.truncate(int(full * 0.7)) # simulated crash mid-save
assert os.path.isfile(cache) # production hit-test: HIT (poisoned)
np.load(cache, mmap_mode="r") # ValueError: mmap length is greater than file size
Formal verification (TLA+)
The rank0-write → barrier → follower-mmap protocol with crash/restart was model-checked with TLC:
| Configuration | Result |
|---|---|
| As-shipped (non-atomic write, isfile hit-test) | RestartIntegrity violated — 5-step counterexample: WriteBegin → CrashMidWrite → JobRestart → hit-test mmaps the torn cache |
| Broken fast-cache-load guard (#5398), barrier enforcement nondeterministic per build level | IndexIntegrity violated even under partial enforcement — any level the guard silently skips exposes the mid-write race |
| Proposed fix (below) | Clean across the explored state space, including under the broken guard |
Proposed fix (verified in the model)
- Write
.npyto a temporary name andos.renameinto place (atomic on POSIX). - Write
description.txtlast — it becomes the commit marker. - Make the hit-test require the commit marker and treat leftover tmp/partial files as a miss.
The fix is robust to the #5398 guard misimplementation: with it, an in-progress write is invisible at the final path, so even an unguarded fast-path reader sees a complete cache or a loud FileNotFoundError — never a torn file.
Related
- #5398 — fast-cache-load rank-sync guard mis-checks
self.clsvsself.config; the model confirms the exposure it leaves open. - #4080 —
tools/prepare_cache.py; mitigates only the prebuilt-cache flow, not the normal build path.
Attachments
TLA+ spec (CacheBuildProtocol.tla), TLC configs and counterexample traces, and the reproduction script: see CacheBuildProtocol.zip / test file attachments (also available on request as a tarball).
Follow-up from a line-level pass over the fast-cache-load interaction — this strengthens the case for the fix.
--fast-cache-load bypasses even the isfile check. Both cache sites force a hit:
# gpt_dataset.py:520 and blended_dataset.py:143
cache_hit = (
True
if self.config.fast_cache_load
else all(map(os.path.isfile, [path_to_description, ...]))
)
So with the flag set, a torn .npy left by a crashed predecessor job is accepted unconditionally — the weak defense analyzed above is not even evaluated. The flag also skips the write path on all ranks (the build/save block is guarded by not cache_hit and rank == 0), so the risk profile is: absent caches → loud FileNotFoundError (fine); torn caches → poisoned read with zero defense (the F5 window above, made unconditional).
Also verified while tracing the guards: the fast-cache-load synchronization guards use isinstance(self.config, GPTDatasetConfig) at blended_megatron_dataset_builder.py:225/323/395/481, but line 456 checks isinstance(self.cls, GPTDatasetConfig) — self.cls is the dataset class, so that one never fires. That keeps split-level barriers ON (fail-safe direction: extra serialization, slower startup, no correctness impact). That's the bug #5398 fixes; it is orthogonal to this issue but worth landing together since both touch the same guard block.
The proposed fix (tmp-write + atomic rename + description.txt as last-written commit marker) covers both paths: on the normal path the hit-test requires the marker, and on the fast-cache-load path an in-progress write is invisible at the final name, so a fast-load either sees a complete cache or fails loudly with FileNotFoundError.