Skip to content

Commit 72cf9b0

Browse files
committed
Update deprecated autocast functions
1 parent d3beb52 commit 72cf9b0

File tree

8 files changed

+18
-18
lines changed

8 files changed

+18
-18
lines changed

references/classification/train.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def train_one_epoch(model, criterion, optimizer, data_loader, device, epoch, arg
2626
for i, (image, target) in enumerate(metric_logger.log_every(data_loader, args.print_freq, header)):
2727
start_time = time.time()
2828
image, target = image.to(device), target.to(device)
29-
with torch.cuda.amp.autocast(enabled=scaler is not None):
29+
with torch.amp.autocast("cuda", enabled=scaler is not None):
3030
output = model(image)
3131
loss = criterion(output, target)
3232

references/depth/stereo/cascade_evaluation.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ def _evaluate(
139139
logger.add_meter("fl-all", fmt="{global_avg:.4f}")
140140

141141
num_processed_samples = 0
142-
with torch.cuda.amp.autocast(enabled=args.mixed_precision, dtype=torch.float16):
142+
with torch.amp.autocast("cuda", enabled=args.mixed_precision, dtype=torch.float16):
143143
batch_idx = 0
144144
for blob in metric_logger.log_every(val_loader, print_freq, header):
145145
image_left, image_right, disp_gt, valid_disp_mask = (x.to(device) for x in blob)

references/depth/stereo/train.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ def _evaluate(
169169
logger.add_meter("fl-all", fmt="{global_avg:.4f}")
170170

171171
num_processed_samples = 0
172-
with torch.cuda.amp.autocast(enabled=args.mixed_precision, dtype=torch.float16):
172+
with torch.amp.autocast("cuda", enabled=args.mixed_precision, dtype=torch.float16):
173173
for blob in metric_logger.log_every(val_loader, print_freq, header):
174174
image_left, image_right, disp_gt, valid_disp_mask = (x.to(device) for x in blob)
175175
padder = utils.InputPadder(image_left.shape, mode=padder_mode)
@@ -314,7 +314,7 @@ def run(model, optimizer, scheduler, train_loader, val_loaders, logger, writer,
314314

315315
# unpack the data blob
316316
image_left, image_right, disp_mask, valid_disp_mask = (x.to(device) for x in data_blob)
317-
with torch.cuda.amp.autocast(enabled=args.mixed_precision, dtype=torch.float16):
317+
with torch.amp.autocast("cuda", enabled=args.mixed_precision, dtype=torch.float16):
318318
disp_predictions = model(image_left, image_right, flow_init=None, num_iters=args.recurrent_updates)
319319
# different models have different outputs, make sure we get the right ones for this task
320320
disp_predictions = make_stereo_flow(disp_predictions, model_out_channels)

references/detection/engine.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def train_one_epoch(model, optimizer, data_loader, device, epoch, print_freq, sc
2727
for images, targets in metric_logger.log_every(data_loader, print_freq, header):
2828
images = list(image.to(device) for image in images)
2929
targets = [{k: v.to(device) if isinstance(v, torch.Tensor) else v for k, v in t.items()} for t in targets]
30-
with torch.cuda.amp.autocast(enabled=scaler is not None):
30+
with torch.amp.autocast("cuda", enabled=scaler is not None):
3131
loss_dict = model(images, targets)
3232
losses = sum(loss for loss in loss_dict.values())
3333

references/segmentation/train.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def train_one_epoch(model, criterion, optimizer, data_loader, lr_scheduler, devi
107107
header = f"Epoch: [{epoch}]"
108108
for image, target in metric_logger.log_every(data_loader, print_freq, header):
109109
image, target = image.to(device), target.to(device)
110-
with torch.cuda.amp.autocast(enabled=scaler is not None):
110+
with torch.amp.autocast("cuda", enabled=scaler is not None):
111111
output = model(image)
112112
loss = criterion(output, target)
113113

references/video_classification/train.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def train_one_epoch(model, criterion, optimizer, lr_scheduler, data_loader, devi
2525
for video, target, _ in metric_logger.log_every(data_loader, print_freq, header):
2626
start_time = time.time()
2727
video, target = video.to(device), target.to(device)
28-
with torch.cuda.amp.autocast(enabled=scaler is not None):
28+
with torch.amp.autocast("cuda", enabled=scaler is not None):
2929
output = model(video)
3030
loss = criterion(output, target)
3131

test/test_models.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,7 @@ def checkOut(out):
606606

607607
checkOut(out)
608608

609-
with torch.cuda.amp.autocast():
609+
with torch.amp.autocast("cuda"):
610610
out = model(model_input)
611611

612612
checkOut(out)
@@ -705,7 +705,7 @@ def test_classification_model(model_fn, dev):
705705
_check_fx_compatible(model, x, eager_out=out)
706706

707707
if dev == "cuda":
708-
with torch.cuda.amp.autocast():
708+
with torch.amp.autocast("cuda"):
709709
out = model(x)
710710
# See autocast_flaky_numerics comment at top of file.
711711
if model_name not in autocast_flaky_numerics:
@@ -761,7 +761,7 @@ def check_out(out):
761761
_check_fx_compatible(model, x, eager_out=out)
762762

763763
if dev == "cuda":
764-
with torch.cuda.amp.autocast(), torch.no_grad(), freeze_rng_state():
764+
with torch.amp.autocast("cuda"), torch.no_grad(), freeze_rng_state():
765765
out = model(x)
766766
# See autocast_flaky_numerics comment at top of file.
767767
if model_name not in autocast_flaky_numerics:
@@ -864,7 +864,7 @@ def compute_mean_std(tensor):
864864
_check_jit_scriptable(model, ([x],), unwrapper=script_model_unwrapper.get(model_name, None), eager_out=out)
865865

866866
if dev == "cuda":
867-
with torch.cuda.amp.autocast(), torch.no_grad(), freeze_rng_state():
867+
with torch.amp.autocast("cuda"), torch.no_grad(), freeze_rng_state():
868868
out = model(model_input)
869869
# See autocast_flaky_numerics comment at top of file.
870870
if model_name not in autocast_flaky_numerics:
@@ -941,7 +941,7 @@ def test_video_model(model_fn, dev):
941941
assert out.shape[-1] == num_classes
942942

943943
if dev == "cuda":
944-
with torch.cuda.amp.autocast():
944+
with torch.amp.autocast("cuda"):
945945
out = model(x)
946946
# See autocast_flaky_numerics comment at top of file.
947947
if model_name not in autocast_flaky_numerics:

test/test_ops.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ def func(z):
232232
@pytest.mark.parametrize("x_dtype", (torch.float, torch.half))
233233
@pytest.mark.parametrize("rois_dtype", (torch.float, torch.half))
234234
def test_autocast(self, x_dtype, rois_dtype):
235-
with torch.cuda.amp.autocast():
235+
with torch.amp.autocast("cuda"):
236236
self.test_forward(torch.device("cuda"), contiguous=False, x_dtype=x_dtype, rois_dtype=rois_dtype)
237237

238238
def _helper_boxes_shape(self, func):
@@ -497,7 +497,7 @@ def test_forward(self, device, contiguous, deterministic, aligned, x_dtype, rois
497497
@pytest.mark.parametrize("rois_dtype", (torch.float, torch.half))
498498
@pytest.mark.opcheck_only_one()
499499
def test_autocast(self, aligned, deterministic, x_dtype, rois_dtype):
500-
with torch.cuda.amp.autocast():
500+
with torch.amp.autocast("cuda"):
501501
self.test_forward(
502502
torch.device("cuda"),
503503
contiguous=False,
@@ -513,7 +513,7 @@ def test_autocast(self, aligned, deterministic, x_dtype, rois_dtype):
513513
@pytest.mark.parametrize("x_dtype", (torch.float, torch.bfloat16))
514514
@pytest.mark.parametrize("rois_dtype", (torch.float, torch.bfloat16))
515515
def test_autocast_cpu(self, aligned, deterministic, x_dtype, rois_dtype):
516-
with torch.cpu.amp.autocast():
516+
with torch.amp.autocast("cpu"):
517517
self.test_forward(
518518
torch.device("cpu"),
519519
contiguous=False,
@@ -856,14 +856,14 @@ def test_nms_gpu(self, iou, device, dtype=torch.float64):
856856
@pytest.mark.parametrize("dtype", (torch.float, torch.half))
857857
@pytest.mark.opcheck_only_one()
858858
def test_autocast(self, iou, dtype):
859-
with torch.cuda.amp.autocast():
859+
with torch.amp.autocast("cuda"):
860860
self.test_nms_gpu(iou=iou, dtype=dtype, device="cuda")
861861

862862
@pytest.mark.parametrize("iou", (0.2, 0.5, 0.8))
863863
@pytest.mark.parametrize("dtype", (torch.float, torch.bfloat16))
864864
def test_autocast_cpu(self, iou, dtype):
865865
boxes, scores = self._create_tensors_with_iou(1000, iou)
866-
with torch.cpu.amp.autocast():
866+
with torch.amp.autocast("cpu"):
867867
keep_ref_float = ops.nms(boxes.to(dtype).float(), scores.to(dtype).float(), iou)
868868
keep_dtype = ops.nms(boxes.to(dtype), scores.to(dtype), iou)
869869
torch.testing.assert_close(keep_ref_float, keep_dtype)
@@ -1193,7 +1193,7 @@ def test_compare_cpu_cuda_grads(self, contiguous):
11931193
@pytest.mark.parametrize("dtype", (torch.float, torch.half))
11941194
@pytest.mark.opcheck_only_one()
11951195
def test_autocast(self, batch_sz, dtype):
1196-
with torch.cuda.amp.autocast():
1196+
with torch.amp.autocast("cuda"):
11971197
self.test_forward(torch.device("cuda"), contiguous=False, batch_sz=batch_sz, dtype=dtype)
11981198

11991199
def test_forward_scriptability(self):

0 commit comments

Comments
 (0)