Skip to content

Commit 31af40a

Browse files
committed
fix torch.cuda.amp deperecated error
Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com>
1 parent 3842e59 commit 31af40a

28 files changed

Lines changed: 54 additions & 54 deletions

File tree

3d_registration/learn2reg_nlst_paired_lung_ct.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -872,7 +872,7 @@
872872
"source": [
873873
"# Automatic mixed precision (AMP) for faster training\n",
874874
"amp_enabled = True\n",
875-
"scaler = torch.cuda.amp.GradScaler()\n",
875+
"scaler = torch.GradScaler(\"cuda\")\n",
876876
"\n",
877877
"# Tensorboard\n",
878878
"if do_save:\n",
@@ -1139,7 +1139,7 @@
11391139
"# Forward pass\n",
11401140
"model.eval()\n",
11411141
"with torch.no_grad():\n",
1142-
" with torch.cuda.amp.autocast(enabled=amp_enabled):\n",
1142+
" with torch.autocast(\"cuda\", enabled=amp_enabled):\n",
11431143
" ddf_image, ddf_keypoints, pred_image, pred_label = forward(\n",
11441144
" check_data[\"fixed_image\"].to(device),\n",
11451145
" check_data[\"moving_image\"].to(device),\n",

3d_registration/learn2reg_oasis_unpaired_brain_mr.ipynb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,7 @@
610610
"source": [
611611
"# Automatic mixed precision (AMP) for faster training\n",
612612
"amp_enabled = True\n",
613-
"scaler = torch.cuda.amp.GradScaler()\n",
613+
"scaler = torch.GradScaler(\"cuda\")\n",
614614
"\n",
615615
"# Tensorboard\n",
616616
"if do_save:\n",
@@ -646,7 +646,7 @@
646646
"\n",
647647
" # Forward pass and loss\n",
648648
" optimizer.zero_grad()\n",
649-
" with torch.cuda.amp.autocast(enabled=amp_enabled):\n",
649+
" with torch.autocast(\"cuda\", enabled=amp_enabled):\n",
650650
" ddf_image, pred_image, pred_label_one_hot = forward(\n",
651651
" fixed_image, moving_image, moving_label, model, warp_layer, num_classes=4\n",
652652
" )\n",
@@ -694,7 +694,7 @@
694694
" # moving_label_35 = batch_data[\"moving_label_35\"].to(device)\n",
695695
" n_steps += 1\n",
696696
" # Infer\n",
697-
" with torch.cuda.amp.autocast(enabled=amp_enabled):\n",
697+
" with torch.autocast(\"cuda\", enabled=amp_enabled):\n",
698698
" ddf_image, pred_image, pred_label_one_hot = forward(\n",
699699
" fixed_image, moving_image, moving_label_4, model, warp_layer, num_classes=4\n",
700700
" )\n",
@@ -860,7 +860,7 @@
860860
"# Forward pass\n",
861861
"model.eval()\n",
862862
"with torch.no_grad():\n",
863-
" with torch.cuda.amp.autocast(enabled=amp_enabled):\n",
863+
" with torch.autocast(\"cuda\", enabled=amp_enabled):\n",
864864
" ddf_image, pred_image, pred_label_one_hot = forward(\n",
865865
" fixed_image, moving_image, moving_label_35, model, warp_layer, num_classes=35\n",
866866
" )"

3d_segmentation/brats_segmentation_3d.ipynb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -473,14 +473,14 @@
473473
" )\n",
474474
"\n",
475475
" if VAL_AMP:\n",
476-
" with torch.cuda.amp.autocast():\n",
476+
" with torch.autocast(\"cuda\"):\n",
477477
" return _compute(input)\n",
478478
" else:\n",
479479
" return _compute(input)\n",
480480
"\n",
481481
"\n",
482482
"# use amp to accelerate training\n",
483-
"scaler = torch.cuda.amp.GradScaler()\n",
483+
"scaler = torch.GradScaler(\"cuda\")\n",
484484
"# enable cuDNN benchmark\n",
485485
"torch.backends.cudnn.benchmark = True"
486486
]
@@ -526,7 +526,7 @@
526526
" batch_data[\"label\"].to(device),\n",
527527
" )\n",
528528
" optimizer.zero_grad()\n",
529-
" with torch.cuda.amp.autocast():\n",
529+
" with torch.autocast(\"cuda\"):\n",
530530
" outputs = model(inputs)\n",
531531
" loss = loss_function(outputs, labels)\n",
532532
" scaler.scale(loss).backward()\n",
@@ -924,7 +924,7 @@
924924
" )\n",
925925
"\n",
926926
" if VAL_AMP:\n",
927-
" with torch.cuda.amp.autocast():\n",
927+
" with torch.autocast(\"cuda\"):\n",
928928
" return _compute(input)\n",
929929
" else:\n",
930930
" return _compute(input)"

3d_segmentation/swin_unetr_btcv_segmentation_3d.ipynb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@
493493
"torch.backends.cudnn.benchmark = True\n",
494494
"loss_function = DiceCELoss(to_onehot_y=True, softmax=True)\n",
495495
"optimizer = torch.optim.AdamW(model.parameters(), lr=1e-4, weight_decay=1e-5)\n",
496-
"scaler = torch.cuda.amp.GradScaler()"
496+
"scaler = torch.GradScaler(\"cuda\")"
497497
]
498498
},
499499
{
@@ -516,7 +516,7 @@
516516
" with torch.no_grad():\n",
517517
" for batch in epoch_iterator_val:\n",
518518
" val_inputs, val_labels = (batch[\"image\"].cuda(), batch[\"label\"].cuda())\n",
519-
" with torch.cuda.amp.autocast():\n",
519+
" with torch.autocast(\"cuda\"):\n",
520520
" val_outputs = sliding_window_inference(val_inputs, (96, 96, 96), 4, model)\n",
521521
" val_labels_list = decollate_batch(val_labels)\n",
522522
" val_labels_convert = [post_label(val_label_tensor) for val_label_tensor in val_labels_list]\n",
@@ -537,7 +537,7 @@
537537
" for step, batch in enumerate(epoch_iterator):\n",
538538
" step += 1\n",
539539
" x, y = (batch[\"image\"].cuda(), batch[\"label\"].cuda())\n",
540-
" with torch.cuda.amp.autocast():\n",
540+
" with torch.autocast(\"cuda\"):\n",
541541
" logit_map = model(x)\n",
542542
" loss = loss_function(logit_map, y)\n",
543543
" scaler.scale(loss).backward()\n",

acceleration/automatic_mixed_precision.ipynb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@
289289
" ).to(device)\n",
290290
" loss_function = DiceLoss(to_onehot_y=True, softmax=True)\n",
291291
" optimizer = torch.optim.Adam(model.parameters(), 1e-4)\n",
292-
" scaler = torch.cuda.amp.GradScaler() if amp else None\n",
292+
" scaler = torch.GradScaler(\"cuda\") if amp else None\n",
293293
"\n",
294294
" post_pred = Compose([AsDiscrete(argmax=True, to_onehot=2)])\n",
295295
" post_label = Compose([AsDiscrete(to_onehot=2)])\n",
@@ -321,7 +321,7 @@
321321
" )\n",
322322
" optimizer.zero_grad()\n",
323323
" if amp and scaler is not None:\n",
324-
" with torch.cuda.amp.autocast():\n",
324+
" with torch.autocast(\"cuda\"):\n",
325325
" outputs = model(inputs)\n",
326326
" loss = loss_function(outputs, labels)\n",
327327
" scaler.scale(loss).backward()\n",
@@ -353,7 +353,7 @@
353353
" roi_size = (160, 160, 128)\n",
354354
" sw_batch_size = 4\n",
355355
" if amp:\n",
356-
" with torch.cuda.amp.autocast():\n",
356+
" with torch.autocast(\"cuda\"):\n",
357357
" val_outputs = sliding_window_inference(val_inputs, roi_size, sw_batch_size, model)\n",
358358
" else:\n",
359359
" val_outputs = sliding_window_inference(val_inputs, roi_size, sw_batch_size, model)\n",

acceleration/distributed_training/brats_training_ddp.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ def main_worker(args):
170170
device = torch.device(f"cuda:{os.environ['LOCAL_RANK']}")
171171
torch.cuda.set_device(device)
172172
# use amp to accelerate training
173-
scaler = torch.cuda.amp.GradScaler()
173+
scaler = torch.GradScaler("cuda")
174174
torch.backends.cudnn.benchmark = True
175175

176176
total_start = time.time()
@@ -320,7 +320,7 @@ def train(train_loader, model, criterion, optimizer, lr_scheduler, scaler):
320320
for batch_data in train_loader:
321321
step += 1
322322
optimizer.zero_grad()
323-
with torch.cuda.amp.autocast():
323+
with torch.autocast("cuda"):
324324
outputs = model(batch_data["image"])
325325
loss = criterion(outputs, batch_data["label"])
326326
scaler.scale(loss).backward()
@@ -339,7 +339,7 @@ def evaluate(model, val_loader, dice_metric, dice_metric_batch, post_trans):
339339
model.eval()
340340
with torch.no_grad():
341341
for val_data in val_loader:
342-
with torch.cuda.amp.autocast():
342+
with torch.autocast("cuda"):
343343
val_outputs = sliding_window_inference(
344344
inputs=val_data["image"], roi_size=(240, 240, 160), sw_batch_size=4, predictor=model, overlap=0.6
345345
)

acceleration/fast_model_training_guide.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ nvtx.end_range(rng_train_dataload)
120120
optimizer.zero_grad()
121121

122122
rng_train_forward = nvtx.start_range(message="forward", color="green")
123-
with torch.cuda.amp.autocast():
123+
with torch.autocast("cuda"):
124124
outputs = model(inputs)
125125
loss = loss_function(outputs, labels)
126126
nvtx.end_range(rng_train_forward)
@@ -231,7 +231,7 @@ NVIDIA GPUs have been widely applied in many areas of deep learning training and
231231

232232
In 2017, NVIDIA researchers developed a methodology for mixed-precision training, which combined single-precision (FP32) with half-precision (e.g., FP16) format when training a network, and it achieved a similar accuracy as FP32 training using the same hyperparameters.
233233

234-
For the PyTorch 1.6 release, developers at NVIDIA and Facebook moved mixed precision functionality into PyTorch core as the AMP package, `torch.cuda.amp`.
234+
For the PyTorch 1.6 release, developers at NVIDIA and Facebook moved mixed precision functionality into PyTorch core as the AMP package, `torch.autocast`.
235235

236236
MONAI workflows can easily set `amp=True/False` in `SupervisedTrainer` or `SupervisedEvaluator` during training or evaluation to enable/disable AMP.
237237
We tried to compare the training speed of the spleen segmentation task if AMP ON/OFF on NVIDIA A100 GPU with CUDA 11 and obtained some benchmark results:

acceleration/fast_training_tutorial.ipynb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@
486486
" momentum=0.9,\n",
487487
" weight_decay=0.00004,\n",
488488
" )\n",
489-
" scaler = torch.cuda.amp.GradScaler()\n",
489+
" scaler = torch.GradScaler(\"cuda\")\n",
490490
" else:\n",
491491
" optimizer = Adam(model.parameters(), learning_rate)\n",
492492
"\n",
@@ -528,7 +528,7 @@
528528
" if fast:\n",
529529
" # profiling: forward\n",
530530
" with nvtx.annotate(\"forward\", color=\"green\") if profiling else no_profiling:\n",
531-
" with torch.cuda.amp.autocast():\n",
531+
" with torch.autocast(\"cuda\"):\n",
532532
" outputs = model(inputs)\n",
533533
" loss = loss_function(outputs, labels)\n",
534534
"\n",
@@ -584,7 +584,7 @@
584584
" with nvtx.annotate(\"sliding window\", color=\"green\") if profiling else no_profiling:\n",
585585
" # set AMP for MONAI validation\n",
586586
" if fast:\n",
587-
" with torch.cuda.amp.autocast():\n",
587+
" with torch.autocast(\"cuda\"):\n",
588588
" val_outputs = sliding_window_inference(val_inputs, roi_size, sw_batch_size, model)\n",
589589
" else:\n",
590590
" val_outputs = sliding_window_inference(val_inputs, roi_size, sw_batch_size, model)\n",

auto3dseg/docs/ensemble.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class InferClass:
5555
batch_data = list_data_collate([batch_data])
5656
infer_image = batch_data["image"].to(self.device)
5757

58-
with torch.cuda.amp.autocast():
58+
with torch.autocast("cuda"):
5959
batch_data["pred"] = sliding_window_inference(
6060
infer_image,
6161
self.patch_size_valid,

automl/DiNTS/search_dints.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ def main():
429429

430430
# amp
431431
if amp:
432-
from torch.cuda.amp import autocast, GradScaler
432+
from torch import autocast, GradScaler
433433

434434
scaler = GradScaler()
435435
if dist.get_rank() == 0:
@@ -638,7 +638,7 @@ def main():
638638
sw_batch_size = num_sw_batch_size
639639

640640
if amp:
641-
with torch.cuda.amp.autocast():
641+
with torch.autocast("cuda"):
642642
pred = sliding_window_inference(
643643
val_images,
644644
roi_size,

0 commit comments

Comments
 (0)