Skip to content

Commit 245525a

Browse files
ydcjeffvfdev-5
andauthored
fix(hash_checkpoint): read the checkpoint chunk by chunk (#2283)
* fix(hash_checkpoint): read the checkpoint chunk by chunk * Update ignite/utils.py Co-authored-by: vfdev <vfdev.5@gmail.com>
1 parent e6a1fdf commit 245525a

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

ignite/utils.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,14 @@ def hash_checkpoint(checkpoint_path: Union[str, Path], output_dir: Union[str, Pa
311311
output_dir = Path(output_dir)
312312
output_dir.mkdir(parents=True, exist_ok=True)
313313

314-
sha_hash = hashlib.sha256(checkpoint_path.read_bytes()).hexdigest()
314+
hash_obj = hashlib.sha256()
315+
# taken from https://github.com/pytorch/vision/blob/main/references/classification/utils.py
316+
with checkpoint_path.open("rb") as f:
317+
# Read and update hash string value in blocks of 4KB
318+
for byte_block in iter(lambda: f.read(4096), b""):
319+
hash_obj.update(byte_block)
320+
sha_hash = hash_obj.hexdigest()
321+
315322
old_filename = checkpoint_path.stem
316323
new_filename = "-".join((old_filename, sha_hash[:8])) + ".pt"
317324

0 commit comments

Comments
 (0)