Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bump Numpy version limit to include 2.x only + Pytorch 2.6 #8309

Draft
wants to merge 15 commits into
base: dev
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion .github/workflows/pythonapp-min.yml
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ jobs:
strategy:
fail-fast: false
matrix:
pytorch-version: ['1.13.1', '2.0.1', '2.2.2', '2.3.1', '2.4.1', 'latest']
pytorch-version: ['1.13.1', '2.0.1', '2.2.2', '2.3.1', '2.4.1', '2.5.1', 'latest']
timeout-minutes: 40
steps:
- uses: actions/checkout@v4
Expand Down
2 changes: 1 addition & 1 deletion environment-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ channels:
- nvidia
- conda-forge
dependencies:
- numpy>=1.24,<2.0
- numpy>=1.24,<3.0
- pytorch>=1.13.1
- torchio
- torchvision
Expand Down
2 changes: 1 addition & 1 deletion monai/apps/deepedit/interaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def __call__(self, engine: SupervisedTrainer | SupervisedEvaluator, batchdata: d

with torch.no_grad():
if engine.amp:
with torch.cuda.amp.autocast():
with torch.autocast("cuda"):
predictions = engine.inferer(inputs, engine.network)
else:
predictions = engine.inferer(inputs, engine.network)
Expand Down
2 changes: 1 addition & 1 deletion monai/apps/deepgrow/interaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def __call__(self, engine: SupervisedTrainer | SupervisedEvaluator, batchdata: d
engine.network.eval()
with torch.no_grad():
if engine.amp:
with torch.cuda.amp.autocast():
with torch.autocast("cuda"):
predictions = engine.inferer(inputs, engine.network)
else:
predictions = engine.inferer(inputs, engine.network)
Expand Down
2 changes: 1 addition & 1 deletion monai/apps/detection/networks/retinanet_detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def forward(self, images: torch.Tensor):
nesterov=True,
)
torch.save(detector.network.state_dict(), 'model.pt') # save model
detector.network.load_state_dict(torch.load('model.pt')) # load model
detector.network.load_state_dict(torch.load('model.pt', weights_only=True)) # load model
"""

def __init__(
Expand Down
2 changes: 1 addition & 1 deletion monai/apps/mmars/mmars.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ def load_from_mmar(
return torch.jit.load(_model_file, map_location=map_location)

# loading with `torch.load`
model_dict = torch.load(_model_file, map_location=map_location)
model_dict = torch.load(_model_file, map_location=map_location, weights_only=True)
if weights_only:
return model_dict.get(model_key, model_dict) # model_dict[model_key] or model_dict directly

Expand Down
7 changes: 3 additions & 4 deletions monai/bundle/scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,7 @@ def load(
if load_ts_module is True:
return load_net_with_metadata(full_path, map_location=torch.device(device), more_extra_files=config_files)
# loading with `torch.load`
model_dict = torch.load(full_path, map_location=torch.device(device))
model_dict = torch.load(full_path, map_location=torch.device(device), weights_only=True)

if not isinstance(model_dict, Mapping):
warnings.warn(f"the state dictionary from {full_path} should be a dictionary but got {type(model_dict)}.")
Expand Down Expand Up @@ -1279,9 +1279,8 @@ def verify_net_in_out(
if input_dtype == torch.float16:
# fp16 can only be executed in gpu mode
net.to("cuda")
from torch.cuda.amp import autocast

with autocast():
with torch.autocast("cuda"):
output = net(test_data.cuda(), **extra_forward_args_)
net.to(device_)
else:
Expand Down Expand Up @@ -1330,7 +1329,7 @@ def _export(
# here we use ignite Checkpoint to support nested weights and be compatible with MONAI CheckpointSaver
Checkpoint.load_objects(to_load={key_in_ckpt: net}, checkpoint=ckpt_file)
else:
ckpt = torch.load(ckpt_file)
ckpt = torch.load(ckpt_file, weights_only=True)
copy_model_state(dst=net, src=ckpt if key_in_ckpt == "" else ckpt[key_in_ckpt])

# Use the given converter to convert a model and save with metadata, config content
Expand Down
11 changes: 2 additions & 9 deletions monai/data/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
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
Expand Down Expand Up @@ -372,10 +371,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:
return torch.load(hashfile, weights_only=False)
else:
return torch.load(hashfile)
return torch.load(hashfile, weights_only=False)
except PermissionError as e:
if sys.platform != "win32":
raise e
Expand Down Expand Up @@ -1674,7 +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:
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)
return torch.load(self.cache_dir / meta_hash_file_name, weights_only=False)
16 changes: 8 additions & 8 deletions monai/engines/evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ class Evaluator(Workflow):
default to `True`.
to_kwargs: dict of other args for `prepare_batch` API when converting the input data, except for
`device`, `non_blocking`.
amp_kwargs: dict of the args for `torch.cuda.amp.autocast()` API, for more details:
https://pytorch.org/docs/stable/amp.html#torch.cuda.amp.autocast.
amp_kwargs: dict of the args for `torch.autocast("cuda")` API, for more details:
https://pytorch.org/docs/stable/amp.html#torch.autocast.

"""

Expand Down Expand Up @@ -214,8 +214,8 @@ class SupervisedEvaluator(Evaluator):
default to `True`.
to_kwargs: dict of other args for `prepare_batch` API when converting the input data, except for
`device`, `non_blocking`.
amp_kwargs: dict of the args for `torch.cuda.amp.autocast()` API, for more details:
https://pytorch.org/docs/stable/amp.html#torch.cuda.amp.autocast.
amp_kwargs: dict of the args for `torch.autocast("cuda")` API, for more details:
https://pytorch.org/docs/stable/amp.html#torch.autocast.
compile: whether to use `torch.compile`, default is False. If True, MetaTensor inputs will be converted to
`torch.Tensor` before forward pass, then converted back afterward with copied meta information.
compile_kwargs: dict of the args for `torch.compile()` API, for more details:
Expand Down Expand Up @@ -329,7 +329,7 @@ def _iteration(self, engine: SupervisedEvaluator, batchdata: dict[str, torch.Ten
# execute forward computation
with engine.mode(engine.network):
if engine.amp:
with torch.cuda.amp.autocast(**engine.amp_kwargs):
with torch.autocast("cuda", **engine.amp_kwargs):
engine.state.output[Keys.PRED] = engine.inferer(inputs, engine.network, *args, **kwargs)
else:
engine.state.output[Keys.PRED] = engine.inferer(inputs, engine.network, *args, **kwargs)
Expand Down Expand Up @@ -399,8 +399,8 @@ class EnsembleEvaluator(Evaluator):
default to `True`.
to_kwargs: dict of other args for `prepare_batch` API when converting the input data, except for
`device`, `non_blocking`.
amp_kwargs: dict of the args for `torch.cuda.amp.autocast()` API, for more details:
https://pytorch.org/docs/stable/amp.html#torch.cuda.amp.autocast.
amp_kwargs: dict of the args for `torch.autocast("cuda")` API, for more details:
https://pytorch.org/docs/stable/amp.html#torch.autocast.

"""

Expand Down Expand Up @@ -492,7 +492,7 @@ def _iteration(self, engine: EnsembleEvaluator, batchdata: dict[str, torch.Tenso
for idx, network in enumerate(engine.networks):
with engine.mode(network):
if engine.amp:
with torch.cuda.amp.autocast(**engine.amp_kwargs):
with torch.autocast("cuda", **engine.amp_kwargs):
if isinstance(engine.state.output, dict):
engine.state.output.update(
{engine.pred_keys[idx]: engine.inferer(inputs, network, *args, **kwargs)}
Expand Down
18 changes: 9 additions & 9 deletions monai/engines/trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ class SupervisedTrainer(Trainer):
more details: https://pytorch.org/docs/stable/generated/torch.optim.Optimizer.zero_grad.html.
to_kwargs: dict of other args for `prepare_batch` API when converting the input data, except for
`device`, `non_blocking`.
amp_kwargs: dict of the args for `torch.cuda.amp.autocast()` API, for more details:
https://pytorch.org/docs/stable/amp.html#torch.cuda.amp.autocast.
amp_kwargs: dict of the args for `torch.autocast("cuda")` API, for more details:
https://pytorch.org/docs/stable/amp.html#torch.autocast.
compile: whether to use `torch.compile`, default is False. If True, MetaTensor inputs will be converted to
`torch.Tensor` before forward pass, then converted back afterward with copied meta information.
compile_kwargs: dict of the args for `torch.compile()` API, for more details:
Expand Down Expand Up @@ -255,7 +255,7 @@ def _compute_pred_loss():
engine.optimizer.zero_grad(set_to_none=engine.optim_set_to_none)

if engine.amp and engine.scaler is not None:
with torch.cuda.amp.autocast(**engine.amp_kwargs):
with torch.autocast("cuda", **engine.amp_kwargs):
_compute_pred_loss()
engine.scaler.scale(engine.state.output[Keys.LOSS]).backward()
engine.fire_event(IterationEvents.BACKWARD_COMPLETED)
Expand Down Expand Up @@ -341,8 +341,8 @@ class GanTrainer(Trainer):
more details: https://pytorch.org/docs/stable/generated/torch.optim.Optimizer.zero_grad.html.
to_kwargs: dict of other args for `prepare_batch` API when converting the input data, except for
`device`, `non_blocking`.
amp_kwargs: dict of the args for `torch.cuda.amp.autocast()` API, for more details:
https://pytorch.org/docs/stable/amp.html#torch.cuda.amp.autocast.
amp_kwargs: dict of the args for `torch.autocast("cuda")` API, for more details:
https://pytorch.org/docs/stable/amp.html#torch.autocast.

"""

Expand Down Expand Up @@ -518,8 +518,8 @@ class AdversarialTrainer(Trainer):
more details: https://pytorch.org/docs/stable/generated/torch.optim.Optimizer.zero_grad.html.
to_kwargs: dict of other args for `prepare_batch` API when converting the input data, except for
`device`, `non_blocking`.
amp_kwargs: dict of the args for `torch.cuda.amp.autocast()` API, for more details:
https://pytorch.org/docs/stable/amp.html#torch.cuda.amp.autocast.
amp_kwargs: dict of the args for `torch.autocast("cuda")` API, for more details:
https://pytorch.org/docs/stable/amp.html#torch.autocast.
"""

def __init__(
Expand Down Expand Up @@ -689,7 +689,7 @@ def _compute_generator_loss() -> None:
engine.state.g_optimizer.zero_grad(set_to_none=engine.optim_set_to_none)

if engine.amp and engine.state.g_scaler is not None:
with torch.cuda.amp.autocast(**engine.amp_kwargs):
with torch.autocast("cuda", **engine.amp_kwargs):
_compute_generator_loss()

engine.state.output[Keys.LOSS] = (
Expand Down Expand Up @@ -737,7 +737,7 @@ def _compute_discriminator_loss() -> None:
engine.state.d_network.zero_grad(set_to_none=engine.optim_set_to_none)

if engine.amp and engine.state.d_scaler is not None:
with torch.cuda.amp.autocast(**engine.amp_kwargs):
with torch.autocast("cuda", **engine.amp_kwargs):
_compute_discriminator_loss()

engine.state.d_scaler.scale(engine.state.output[AdversarialKeys.DISCRIMINATOR_LOSS]).backward()
Expand Down
4 changes: 2 additions & 2 deletions monai/engines/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ class Workflow(Engine):
default to `True`.
to_kwargs: dict of other args for `prepare_batch` API when converting the input data, except for
`device`, `non_blocking`.
amp_kwargs: dict of the args for `torch.cuda.amp.autocast()` API, for more details:
https://pytorch.org/docs/stable/amp.html#torch.cuda.amp.autocast.
amp_kwargs: dict of the args for `torch.autocast("cuda")` API, for more details:
https://pytorch.org/docs/stable/amp.html#torch.autocast.

Raises:
TypeError: When ``data_loader`` is not a ``torch.utils.data.DataLoader``.
Expand Down
2 changes: 1 addition & 1 deletion monai/fl/client/monai_algo.py
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ def get_weights(self, extra=None):
model_path = os.path.join(self.bundle_root, cast(str, self.model_filepaths[model_type]))
if not os.path.isfile(model_path):
raise ValueError(f"No best model checkpoint exists at {model_path}")
weights = torch.load(model_path, map_location="cpu")
weights = torch.load(model_path, map_location="cpu", weights_only=True)
# if weights contain several state dicts, use the one defined by `save_dict_key`
if isinstance(weights, dict) and self.save_dict_key in weights:
weights = weights.get(self.save_dict_key)
Expand Down
2 changes: 1 addition & 1 deletion monai/handlers/checkpoint_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def __call__(self, engine: Engine) -> None:
Args:
engine: Ignite Engine, it can be a trainer, validator or evaluator.
"""
checkpoint = torch.load(self.load_path, map_location=self.map_location)
checkpoint = torch.load(self.load_path, map_location=self.map_location, weights_only=False)

k, _ = list(self.load_dict.items())[0]
# single object and checkpoint is directly a state_dict
Expand Down
2 changes: 1 addition & 1 deletion monai/losses/perceptual.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ def __init__(
else:
network = torchvision.models.resnet50(weights=None)
if pretrained is True:
state_dict = torch.load(pretrained_path)
state_dict = torch.load(pretrained_path, weights_only=True)
if pretrained_state_dict_key is not None:
state_dict = state_dict[pretrained_state_dict_key]
network.load_state_dict(state_dict)
Expand Down
4 changes: 2 additions & 2 deletions monai/networks/layers/vector_quantizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def quantize(self, inputs: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor, to
torch.Tensor: Quantization indices of shape [B,H,W,D,1]

"""
with torch.cuda.amp.autocast(enabled=False):
with torch.autocast("cuda", enabled=False):
encoding_indices_view = list(inputs.shape)
del encoding_indices_view[1]

Expand Down Expand Up @@ -138,7 +138,7 @@ def embed(self, embedding_indices: torch.Tensor) -> torch.Tensor:
Returns:
torch.Tensor: Quantize space representation of encoding_indices in channel first format.
"""
with torch.cuda.amp.autocast(enabled=False):
with torch.autocast("cuda", enabled=False):
embedding: torch.Tensor = (
self.embedding(embedding_indices).permute(self.quantization_permutation).contiguous()
)
Expand Down
9 changes: 5 additions & 4 deletions monai/networks/nets/hovernet.py
Original file line number Diff line number Diff line change
Expand Up @@ -633,9 +633,9 @@ def _remap_preact_resnet_model(model_url: str):
# download the pretrained weights into torch hub's default dir
weights_dir = os.path.join(torch.hub.get_dir(), "preact-resnet50.pth")
download_url(model_url, fuzzy=True, filepath=weights_dir, progress=False)
state_dict = torch.load(weights_dir, map_location=None if torch.cuda.is_available() else torch.device("cpu"))[
"desc"
]
map_location = None if torch.cuda.is_available() else torch.device("cpu")
state_dict = torch.load(weights_dir, map_location=map_location, weights_only=True)["desc"]

for key in list(state_dict.keys()):
new_key = None
if pattern_conv0.match(key):
Expand Down Expand Up @@ -668,7 +668,8 @@ def _remap_standard_resnet_model(model_url: str, state_dict_key: str | None = No
# download the pretrained weights into torch hub's default dir
weights_dir = os.path.join(torch.hub.get_dir(), "resnet50.pth")
download_url(model_url, fuzzy=True, filepath=weights_dir, progress=False)
state_dict = torch.load(weights_dir, map_location=None if torch.cuda.is_available() else torch.device("cpu"))
map_location = None if torch.cuda.is_available() else torch.device("cpu")
state_dict = torch.load(weights_dir, map_location=map_location, weights_only=True)
if state_dict_key is not None:
state_dict = state_dict[state_dict_key]

Expand Down
4 changes: 2 additions & 2 deletions monai/networks/nets/resnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ def _resnet(
if isinstance(pretrained, str):
if Path(pretrained).exists():
logger.info(f"Loading weights from {pretrained}...")
model_state_dict = torch.load(pretrained, map_location=device)
model_state_dict = torch.load(pretrained, map_location=device, weights_only=True)
else:
# Throw error
raise FileNotFoundError("The pretrained checkpoint file is not found")
Expand Down Expand Up @@ -665,7 +665,7 @@ def get_pretrained_resnet_medicalnet(resnet_depth: int, device: str = "cpu", dat
raise EntryNotFoundError(
f"{filename} not found on {medicalnet_huggingface_repo_basename}{resnet_depth}"
) from None
checkpoint = torch.load(pretrained_path, map_location=torch.device(device))
checkpoint = torch.load(pretrained_path, map_location=torch.device(device), weights_only=True)
else:
raise NotImplementedError("Supported resnet_depth are: [10, 18, 34, 50, 101, 152, 200]")
logger.info(f"{filename} downloaded")
Expand Down
2 changes: 1 addition & 1 deletion monai/networks/nets/senet.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ def _load_state_dict(model: nn.Module, arch: str, progress: bool):

if isinstance(model_url, dict):
download_url(model_url["url"], filepath=model_url["filename"])
state_dict = torch.load(model_url["filename"], map_location=None)
state_dict = torch.load(model_url["filename"], map_location=None, weights_only=True)
else:
state_dict = load_state_dict_from_url(model_url, progress=progress)
for key in list(state_dict.keys()):
Expand Down
2 changes: 1 addition & 1 deletion monai/networks/nets/swin_unetr.py
Original file line number Diff line number Diff line change
Expand Up @@ -1118,7 +1118,7 @@ def filter_swinunetr(key, value):
)
ssl_weights_path = "./ssl_pretrained_weights.pth"
download_url(resource, ssl_weights_path)
ssl_weights = torch.load(ssl_weights_path)["model"]
ssl_weights = torch.load(ssl_weights_path, weights_only=True)["model"]

dst_dict, loaded, not_loaded = copy_model_state(model, ssl_weights, filter_func=filter_swinunetr)

Expand Down
3 changes: 2 additions & 1 deletion monai/networks/nets/transchex.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ def from_pretrained(
weights_path = cached_file(path_or_repo_id, filename, cache_dir=cache_dir)
model = cls(num_language_layers, num_vision_layers, num_mixed_layers, bert_config, *inputs, **kwargs)
if state_dict is None and not from_tf:
state_dict = torch.load(weights_path, map_location="cpu" if not torch.cuda.is_available() else None)
map_location = "cpu" if not torch.cuda.is_available() else None
state_dict = torch.load(weights_path, map_location=map_location, weights_only=True)
if from_tf:
return load_tf_weights_in_bert(model, weights_path)
old_keys = []
Expand Down
4 changes: 2 additions & 2 deletions monai/networks/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1238,7 +1238,7 @@ def __init__(self, mod):

def forward(self, x):
dtype = x.dtype
with torch.amp.autocast("cuda", enabled=False):
with torch.autocast("cuda", enabled=False):
ret = self.mod.forward(x.to(torch.float32)).to(dtype)
return ret

Expand All @@ -1255,7 +1255,7 @@ def __init__(self, mod):

def forward(self, *args):
from_dtype = args[0].dtype
with torch.amp.autocast("cuda", enabled=False):
with torch.autocast("cuda", enabled=False):
ret = self.mod.forward(*cast_all(args, from_dtype=from_dtype, to_dtype=torch.float32))
return cast_all(ret, from_dtype=torch.float32, to_dtype=from_dtype)

Expand Down
Loading
Loading