From e03821db34fd8503ae0793e69161b177db9783ae Mon Sep 17 00:00:00 2001 From: bnbqq8 Date: Thu, 24 Oct 2024 17:42:13 +0800 Subject: [PATCH 1/6] Fix torch.load() in PersistentDataset and GDSDataset to maintain load consistency in future versions of pytorch --- monai/data/dataset.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/monai/data/dataset.py b/monai/data/dataset.py index 871b523289..691425994d 100644 --- a/monai/data/dataset.py +++ b/monai/data/dataset.py @@ -371,7 +371,7 @@ def _cachecheck(self, item_transformed): if hashfile is not None and hashfile.is_file(): # cache hit try: - return torch.load(hashfile) + return torch.load(hashfile, weights_only=False) except PermissionError as e: if sys.platform != "win32": raise e @@ -1670,4 +1670,4 @@ def _load_meta_cache(self, meta_hash_file_name): if meta_hash_file_name in self._meta_cache: return self._meta_cache[meta_hash_file_name] else: - return torch.load(self.cache_dir / meta_hash_file_name) + return torch.load(self.cache_dir / meta_hash_file_name, weights_only=False) From 38d5a71483255259bc1e8e866d7ed3221e6e7a2a Mon Sep 17 00:00:00 2001 From: bnbqq8 Date: Fri, 25 Oct 2024 11:08:15 +0800 Subject: [PATCH 2/6] Add compatibility for PyTorch v1.12.0 and below --- monai/data/dataset.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/monai/data/dataset.py b/monai/data/dataset.py index 691425994d..c71187b1c6 100644 --- a/monai/data/dataset.py +++ b/monai/data/dataset.py @@ -26,6 +26,7 @@ from multiprocessing.pool import ThreadPool from pathlib import Path from typing import IO, TYPE_CHECKING, Any, cast +from inspect import signature import numpy as np import torch @@ -371,7 +372,10 @@ def _cachecheck(self, item_transformed): if hashfile is not None and hashfile.is_file(): # cache hit try: - return torch.load(hashfile, weights_only=False) + if 'weights_only' in signature(torch.load): + return torch.load(hashfile, weights_only=False) + else: + return torch.load(hashfile) except PermissionError as e: if sys.platform != "win32": raise e @@ -1670,4 +1674,7 @@ def _load_meta_cache(self, meta_hash_file_name): if meta_hash_file_name in self._meta_cache: return self._meta_cache[meta_hash_file_name] else: - return torch.load(self.cache_dir / meta_hash_file_name, weights_only=False) + if 'weights_only' in signature(torch.load): + return torch.load(self.cache_dir / meta_hash_file_name, weights_only=False) + else: + return torch.load(self.cache_dir / meta_hash_file_name) From 719e33d6c41e183820913002d2ac71c472d58ae8 Mon Sep 17 00:00:00 2001 From: bnbqq8 Date: Fri, 25 Oct 2024 11:18:06 +0800 Subject: [PATCH 3/6] Refactor dataset.py import order --- monai/data/dataset.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/monai/data/dataset.py b/monai/data/dataset.py index c71187b1c6..43de329896 100644 --- a/monai/data/dataset.py +++ b/monai/data/dataset.py @@ -22,11 +22,11 @@ import warnings from collections.abc import Callable, Sequence from copy import copy, deepcopy +from inspect import signature from multiprocessing.managers import ListProxy from multiprocessing.pool import ThreadPool from pathlib import Path from typing import IO, TYPE_CHECKING, Any, cast -from inspect import signature import numpy as np import torch From 3c2a012797e44261510f7fb5f8561fda1c452631 Mon Sep 17 00:00:00 2001 From: bnbqq8 Date: Fri, 25 Oct 2024 11:39:12 +0800 Subject: [PATCH 4/6] Refactor dataset.py to use signature parameters for torch.load --- monai/data/dataset.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/monai/data/dataset.py b/monai/data/dataset.py index 43de329896..896decd275 100644 --- a/monai/data/dataset.py +++ b/monai/data/dataset.py @@ -372,7 +372,7 @@ def _cachecheck(self, item_transformed): if hashfile is not None and hashfile.is_file(): # cache hit try: - if 'weights_only' in signature(torch.load): + if 'weights_only' in signature(torch.load).parameters: return torch.load(hashfile, weights_only=False) else: return torch.load(hashfile) @@ -1674,7 +1674,7 @@ def _load_meta_cache(self, meta_hash_file_name): if meta_hash_file_name in self._meta_cache: return self._meta_cache[meta_hash_file_name] else: - if 'weights_only' in signature(torch.load): + if 'weights_only' in signature(torch.load).parameters: return torch.load(self.cache_dir / meta_hash_file_name, weights_only=False) else: return torch.load(self.cache_dir / meta_hash_file_name) From 5ac10ff1b01d7b91956d7b983c06a1c453f9c0fe Mon Sep 17 00:00:00 2001 From: bnbqq8 Date: Fri, 25 Oct 2024 13:38:42 +0800 Subject: [PATCH 5/6] Refactor formatted by Black --- monai/data/dataset.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/monai/data/dataset.py b/monai/data/dataset.py index 896decd275..8c53338d66 100644 --- a/monai/data/dataset.py +++ b/monai/data/dataset.py @@ -372,7 +372,7 @@ def _cachecheck(self, item_transformed): if hashfile is not None and hashfile.is_file(): # cache hit try: - if 'weights_only' in signature(torch.load).parameters: + if "weights_only" in signature(torch.load).parameters: return torch.load(hashfile, weights_only=False) else: return torch.load(hashfile) @@ -1674,7 +1674,7 @@ def _load_meta_cache(self, meta_hash_file_name): if meta_hash_file_name in self._meta_cache: return self._meta_cache[meta_hash_file_name] else: - if 'weights_only' in signature(torch.load).parameters: + if "weights_only" in signature(torch.load).parameters: return torch.load(self.cache_dir / meta_hash_file_name, weights_only=False) else: return torch.load(self.cache_dir / meta_hash_file_name) From b12261e7be548988027e255180dc2e9c1a140d4a Mon Sep 17 00:00:00 2001 From: bnbqq8 Date: Fri, 25 Oct 2024 13:41:45 +0800 Subject: [PATCH 6/6] DCO Remediation Commit for bnbqq8 I, bnbqq8 , hereby add my Signed-off-by to this commit: e03821db34fd8503ae0793e69161b177db9783ae I, bnbqq8 , hereby add my Signed-off-by to this commit: 38d5a71483255259bc1e8e866d7ed3221e6e7a2a I, bnbqq8 , hereby add my Signed-off-by to this commit: 719e33d6c41e183820913002d2ac71c472d58ae8 I, bnbqq8 , hereby add my Signed-off-by to this commit: 3c2a012797e44261510f7fb5f8561fda1c452631 I, bnbqq8 , hereby add my Signed-off-by to this commit: 5ac10ff1b01d7b91956d7b983c06a1c453f9c0fe Signed-off-by: bnbqq8