Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions ignite/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,19 +292,24 @@ def hash_checkpoint(checkpoint_path: Union[str, Path], output_dir: Union[str, Pa

Args:
checkpoint_path: Path to the checkpoint file.
output_dir: Output directory to store the hashed checkpoint file.
output_dir: Output directory to store the hashed checkpoint file
(will be created if not exist).

Returns:
Path to the hashed checkpoint file, The 8 digits of SHA256 hash.
Path to the hashed checkpoint file, the first 8 digits of SHA256 hash.

.. versionadded:: 0.5.0
"""

if isinstance(checkpoint_path, str):
checkpoint_path = Path(checkpoint_path)

if not checkpoint_path.exists():
raise FileNotFoundError(f"{checkpoint_path.name} does not exist in {checkpoint_path.parent}.")

if isinstance(output_dir, str):
output_dir = Path(output_dir)
output_dir.mkdir(parents=True, exist_ok=True)

sha_hash = hashlib.sha256(checkpoint_path.read_bytes()).hexdigest()
old_filename = checkpoint_path.stem
Expand Down
4 changes: 4 additions & 0 deletions tests/ignite/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 +256,7 @@ def test_hash_checkpoint(tmp_path):
model.load_state_dict(torch.load(hash_checkpoint_path), True)
assert sha_hash[:8] == "b66bff10"
assert hash_checkpoint_path.name == f"squeezenet1_0-{sha_hash[:8]}.pt"

# test non-existent checkpoint_path
with pytest.raises(FileNotFoundError, match=r"not_found.pt does not exist in *"):
hash_checkpoint(f"{tmp_path}/not_found.pt", tmp_path)