Skip to content

Commit a859c10

Browse files
committed
Replace apex clip_grad_norm_ with PyTorch native in dints templates
apex.contrib.clip_grad.clip_grad_norm_ crashes on PyTorch >=2.10 with "RuntimeError: Cannot access data pointer of Tensor that doesn't have storage" because apex's multi_tensor_applier cannot handle tensors with lazy/functional storage introduced in newer PyTorch versions. The try/except only caught ModuleNotFoundError (apex not installed) but not the runtime crash when apex is installed but incompatible. torch.nn.utils.clip_grad_norm_ handles all tensor types correctly and is the standard approach. The apex version offered marginal performance gains that are not worth the compatibility breakage. Fixes Project-MONAI/MONAI#8737
1 parent 21ed8e5 commit a859c10

2 files changed

Lines changed: 2 additions & 8 deletions

File tree

auto3dseg/algorithm_templates/dints/scripts/search.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,7 @@
4040
from monai.metrics import compute_dice
4141
from monai.utils import RankFilter, set_determinism
4242

43-
try:
44-
from apex.contrib.clip_grad import clip_grad_norm_
45-
except ModuleNotFoundError:
46-
from torch.nn.utils import clip_grad_norm_
43+
from torch.nn.utils import clip_grad_norm_
4744

4845

4946
CONFIG = {

auto3dseg/algorithm_templates/dints/scripts/train.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,7 @@
4747
from torch.utils.tensorboard import SummaryWriter
4848
from tqdm import tqdm
4949

50-
try:
51-
from apex.contrib.clip_grad import clip_grad_norm_
52-
except ModuleNotFoundError:
53-
from torch.nn.utils import clip_grad_norm_
50+
from torch.nn.utils import clip_grad_norm_
5451

5552
try:
5653
_libcudart = ctypes.CDLL("libcudart.so")

0 commit comments

Comments
 (0)