From a859c103e8d83e2936d336c31860c957a5bafbdc Mon Sep 17 00:00:00 2001 From: Heyang Qin Date: Sat, 21 Mar 2026 23:23:38 -0700 Subject: [PATCH] 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 --- auto3dseg/algorithm_templates/dints/scripts/search.py | 5 +---- auto3dseg/algorithm_templates/dints/scripts/train.py | 5 +---- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/auto3dseg/algorithm_templates/dints/scripts/search.py b/auto3dseg/algorithm_templates/dints/scripts/search.py index c0357527..5079c486 100644 --- a/auto3dseg/algorithm_templates/dints/scripts/search.py +++ b/auto3dseg/algorithm_templates/dints/scripts/search.py @@ -40,10 +40,7 @@ from monai.metrics import compute_dice from monai.utils import RankFilter, set_determinism -try: - from apex.contrib.clip_grad import clip_grad_norm_ -except ModuleNotFoundError: - from torch.nn.utils import clip_grad_norm_ +from torch.nn.utils import clip_grad_norm_ CONFIG = { diff --git a/auto3dseg/algorithm_templates/dints/scripts/train.py b/auto3dseg/algorithm_templates/dints/scripts/train.py index 9d0e36c1..fb996b6f 100644 --- a/auto3dseg/algorithm_templates/dints/scripts/train.py +++ b/auto3dseg/algorithm_templates/dints/scripts/train.py @@ -47,10 +47,7 @@ from torch.utils.tensorboard import SummaryWriter from tqdm import tqdm -try: - from apex.contrib.clip_grad import clip_grad_norm_ -except ModuleNotFoundError: - from torch.nn.utils import clip_grad_norm_ +from torch.nn.utils import clip_grad_norm_ try: _libcudart = ctypes.CDLL("libcudart.so")