Skip to content

Commit e428c38

Browse files
ericspodpre-commit-ci[bot]
authored andcommitted
Enable Pytorch 2.6 (#8309)
Partially addresses #8303. This changes the maximum Numpy version to be below 3.0 for testing with 2.x compatibility. This appears to be resolved with newer versions of dependencies. This will also include fixes for Pytorch 2.6 mostly relating to `torch.load` and `autocast` usage. <!--- Put an `x` in all the boxes that apply, and remove the not applicable items --> - [x] Non-breaking change (fix or new feature that would not break existing functionality). - [ ] Breaking change (fix or new feature that would cause existing functionality to change). - [ ] New tests added to cover the changes. - [ ] Integration tests passed locally by running `./runtests.sh -f -u --net --coverage`. - [ ] Quick tests passed locally by running `./runtests.sh --quick --unittests --disttests`. - [ ] In-line docstrings updated. - [ ] Documentation updated, tested `make html` command in the `docs/` folder. --------- Signed-off-by: Eric Kerfoot <eric.kerfoot@kcl.ac.uk> Signed-off-by: Eric Kerfoot <17726042+ericspod@users.noreply.github.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 2df4637 commit e428c38

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+183
-177
lines changed

monai/apps/deepedit/interaction.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def __call__(self, engine: SupervisedTrainer | SupervisedEvaluator, batchdata: d
7272

7373
with torch.no_grad():
7474
if engine.amp:
75-
with torch.cuda.amp.autocast():
75+
with torch.autocast("cuda"):
7676
predictions = engine.inferer(inputs, engine.network)
7777
else:
7878
predictions = engine.inferer(inputs, engine.network)

monai/apps/deepgrow/interaction.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def __call__(self, engine: SupervisedTrainer | SupervisedEvaluator, batchdata: d
6767
engine.network.eval()
6868
with torch.no_grad():
6969
if engine.amp:
70-
with torch.cuda.amp.autocast():
70+
with torch.autocast("cuda"):
7171
predictions = engine.inferer(inputs, engine.network)
7272
else:
7373
predictions = engine.inferer(inputs, engine.network)

monai/apps/detection/networks/retinanet_detector.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ def forward(self, images: torch.Tensor):
180180
nesterov=True,
181181
)
182182
torch.save(detector.network.state_dict(), 'model.pt') # save model
183-
detector.network.load_state_dict(torch.load('model.pt')) # load model
183+
detector.network.load_state_dict(torch.load('model.pt', weights_only=True)) # load model
184184
"""
185185

186186
def __init__(

monai/apps/detection/networks/retinanet_network.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ def __init__(
8888

8989
for layer in self.conv.children():
9090
if isinstance(layer, conv_type): # type: ignore
91-
torch.nn.init.normal_(layer.weight, std=0.01)
92-
torch.nn.init.constant_(layer.bias, 0)
91+
torch.nn.init.normal_(layer.weight, std=0.01) # type: ignore[arg-type]
92+
torch.nn.init.constant_(layer.bias, 0) # type: ignore[arg-type]
9393

9494
self.cls_logits = conv_type(in_channels, num_anchors * num_classes, kernel_size=3, stride=1, padding=1)
9595
torch.nn.init.normal_(self.cls_logits.weight, std=0.01)
@@ -167,8 +167,8 @@ def __init__(self, in_channels: int, num_anchors: int, spatial_dims: int):
167167

168168
for layer in self.conv.children():
169169
if isinstance(layer, conv_type): # type: ignore
170-
torch.nn.init.normal_(layer.weight, std=0.01)
171-
torch.nn.init.zeros_(layer.bias)
170+
torch.nn.init.normal_(layer.weight, std=0.01) # type: ignore[arg-type]
171+
torch.nn.init.zeros_(layer.bias) # type: ignore[arg-type]
172172

173173
def forward(self, x: list[Tensor]) -> list[Tensor]:
174174
"""
@@ -297,7 +297,7 @@ def __init__(
297297
)
298298
self.feature_extractor = feature_extractor
299299

300-
self.feature_map_channels: int = self.feature_extractor.out_channels
300+
self.feature_map_channels: int = self.feature_extractor.out_channels # type: ignore[assignment]
301301
self.num_anchors = num_anchors
302302
self.classification_head = RetinaNetClassificationHead(
303303
self.feature_map_channels, self.num_anchors, self.num_classes, spatial_dims=self.spatial_dims

monai/apps/detection/utils/box_coder.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,15 +221,15 @@ def decode_single(self, rel_codes: Tensor, reference_boxes: Tensor) -> Tensor:
221221

222222
pred_ctr_xyx_axis = dxyz_axis * whd_axis[:, None] + ctr_xyz_axis[:, None]
223223
pred_whd_axis = torch.exp(dwhd_axis) * whd_axis[:, None]
224-
pred_whd_axis = pred_whd_axis.to(dxyz_axis.dtype)
224+
pred_whd_axis = pred_whd_axis.to(dxyz_axis.dtype) # type: ignore[union-attr]
225225

226226
# When convert float32 to float16, Inf or Nan may occur
227227
if torch.isnan(pred_whd_axis).any() or torch.isinf(pred_whd_axis).any():
228228
raise ValueError("pred_whd_axis is NaN or Inf.")
229229

230230
# Distance from center to box's corner.
231231
c_to_c_whd_axis = (
232-
torch.tensor(0.5, dtype=pred_ctr_xyx_axis.dtype, device=pred_whd_axis.device) * pred_whd_axis
232+
torch.tensor(0.5, dtype=pred_ctr_xyx_axis.dtype, device=pred_whd_axis.device) * pred_whd_axis # type: ignore[arg-type]
233233
)
234234

235235
pred_boxes.append(pred_ctr_xyx_axis - c_to_c_whd_axis)

monai/apps/mmars/mmars.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ def load_from_mmar(
241241
return torch.jit.load(_model_file, map_location=map_location)
242242

243243
# loading with `torch.load`
244-
model_dict = torch.load(_model_file, map_location=map_location)
244+
model_dict = torch.load(_model_file, map_location=map_location, weights_only=True)
245245
if weights_only:
246246
return model_dict.get(model_key, model_dict) # model_dict[model_key] or model_dict directly
247247

monai/apps/reconstruction/networks/blocks/varnetblock.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def soft_dc(self, x: Tensor, ref_kspace: Tensor, mask: Tensor) -> Tensor:
5555
Returns:
5656
Output of DC block with the same shape as x
5757
"""
58-
return torch.where(mask, x - ref_kspace, self.zeros) * self.dc_weight
58+
return torch.where(mask, x - ref_kspace, self.zeros) * self.dc_weight # type: ignore
5959

6060
def forward(self, current_kspace: Tensor, ref_kspace: Tensor, mask: Tensor, sens_maps: Tensor) -> Tensor:
6161
"""

monai/bundle/scripts.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,7 @@ def load(
760760
if load_ts_module is True:
761761
return load_net_with_metadata(full_path, map_location=torch.device(device), more_extra_files=config_files)
762762
# loading with `torch.load`
763-
model_dict = torch.load(full_path, map_location=torch.device(device))
763+
model_dict = torch.load(full_path, map_location=torch.device(device), weights_only=True)
764764

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

1284-
with autocast():
1283+
with torch.autocast("cuda"):
12851284
output = net(test_data.cuda(), **extra_forward_args_)
12861285
net.to(device_)
12871286
else:
@@ -1330,7 +1329,7 @@ def _export(
13301329
# here we use ignite Checkpoint to support nested weights and be compatible with MONAI CheckpointSaver
13311330
Checkpoint.load_objects(to_load={key_in_ckpt: net}, checkpoint=ckpt_file)
13321331
else:
1333-
ckpt = torch.load(ckpt_file)
1332+
ckpt = torch.load(ckpt_file, weights_only=True)
13341333
copy_model_state(dst=net, src=ckpt if key_in_ckpt == "" else ckpt[key_in_ckpt])
13351334

13361335
# Use the given converter to convert a model and save with metadata, config content

monai/data/dataset.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import warnings
2323
from collections.abc import Callable, Sequence
2424
from copy import copy, deepcopy
25-
from inspect import signature
2625
from multiprocessing.managers import ListProxy
2726
from multiprocessing.pool import ThreadPool
2827
from pathlib import Path
@@ -372,10 +371,7 @@ def _cachecheck(self, item_transformed):
372371

373372
if hashfile is not None and hashfile.is_file(): # cache hit
374373
try:
375-
if "weights_only" in signature(torch.load).parameters:
376-
return torch.load(hashfile, weights_only=False)
377-
else:
378-
return torch.load(hashfile)
374+
return torch.load(hashfile, weights_only=False)
379375
except PermissionError as e:
380376
if sys.platform != "win32":
381377
raise e
@@ -1674,7 +1670,4 @@ def _load_meta_cache(self, meta_hash_file_name):
16741670
if meta_hash_file_name in self._meta_cache:
16751671
return self._meta_cache[meta_hash_file_name]
16761672
else:
1677-
if "weights_only" in signature(torch.load).parameters:
1678-
return torch.load(self.cache_dir / meta_hash_file_name, weights_only=False)
1679-
else:
1680-
return torch.load(self.cache_dir / meta_hash_file_name)
1673+
return torch.load(self.cache_dir / meta_hash_file_name, weights_only=False)

monai/data/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -753,7 +753,7 @@ def affine_to_spacing(affine: NdarrayTensor, r: int = 3, dtype=float, suppress_z
753753
if isinstance(_affine, torch.Tensor):
754754
spacing = torch.sqrt(torch.sum(_affine * _affine, dim=0))
755755
else:
756-
spacing = np.sqrt(np.sum(_affine * _affine, axis=0))
756+
spacing = np.sqrt(np.sum(_affine * _affine, axis=0)) # type: ignore[operator]
757757
if suppress_zeros:
758758
spacing[spacing == 0] = 1.0
759759
spacing_, *_ = convert_to_dst_type(spacing, dst=affine, dtype=dtype)

0 commit comments

Comments
 (0)