integrity of checkpoint pre-file v1 - #7
Open
chango9543 wants to merge 1 commit into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Implement per-file SHA-256 integrity verification for checkpoints
Related issue: #6
Summary
Add an opt-in integrity verification mechanism for checkpoint directories. On save, every file in the checkpoint directory is hashed with SHA-256 and the hashes are written to
_integrity_manifest.json. On load, the hashes are recomputed and compared before any weight is materialised into the model. Any mismatch indicates silent corruption (bit rot, truncated writes, transfer errors) and the load is rejected withCheckpointingException.Background
See #6 — silent data corruption (bit rot) is a known failure mode on TB-scale checkpoint stores.
torch_checkpointingcurrently has no content-level verification before load, so corrupted weights silently reach the model without loss blowing up immediately.Design
Granularity: Per-file (v1). One hash per checkpoint file, computed by streaming the already-serialized bytes. Layout-agnostic — works for
TorchSerialization,SafetensorsSerialization, or any serialization format.Algorithm: Hardcoded SHA-256 (matches Megatron-LM's choice). The
_compute_hash_dispatch+_collect_hashes_dispatchpattern allows adding future algorithms (e.g. blake3, xxhash64) with zero changes to call sites — just implement a new function and add one branch. Only"sha256"is implemented now; other values raiseNotImplementedError.Manifest format (
_integrity_manifest.json):{ "version": 1, "granularity": "file", "algorithm": "sha256", "files": { "checkpoint_0.pt": "e3b0c4...", ".metadata": "a1b2c3..." } }algorithmandgranularityare recorded in the manifest so the verifier reads them from the manifest, not the current config — an old manifest can still be verified even if the default has changed.Manifest write location: After
finalize_callbackinCheckpointWriter.write(). At this point files are written, the barrier has passed, and the directory has been renamed to the final path — so the manifest hashes exactly the on-disk layout a verifier will see.Manifest verify location: Before
CheckpointReader.read()inCheckpointLoader.load(). A corrupt checkpoint is rejected before any weight reaches the model.Distributed-safe: Only rank 0 does the file I/O; the verdict (success or error string) is broadcast via
broadcast_object_list(src=0)so all ranks raise in lock-step or proceed together.Exception type preservation:
FileNotFoundError(manifest absent) andCheckpointingException(hash mismatch) are broadcast separately via a type-tagged payload so callers can catch them independently. The loader catchesFileNotFoundErrorand silently skips — old checkpoints remain loadable.Hardcoded policies (no per-run knobs):
CheckpointingException(loading known-corrupt weights is never legitimate).Config surface
A single boolean flag on both config classes:
Defaults to
False— existing checkpoints and CI loops are unaffected.Files changed
torch_checkpointing/integrity.pytorch_checkpointing/checkpoint_writer.pyCheckpointWriterConfig.verify_integrity;write()integrationtorch_checkpointing/checkpoint_loader.py__init__acceptsconfig;load()integrationtorch_checkpointing/config.pyCheckpointLoaderConfig.verify_integritytorch_checkpointing/checkpoint_manager.pyconfig.loadtoCheckpointLoadertorch_checkpointing/__init__.pyCheckpointingException,MANIFEST_FILENAMEtests/test_integrity.pyTest coverage
16 unit tests covering:
NotImplementedError)