added a check for autocast

pull/13483/head
Bala-Vignesh-Reddy 2025-01-07 19:13:25 +05:30
parent 1b82b72ed5
commit a5a8bf375f
2 changed files with 8 additions and 3 deletions

View File

@ -108,7 +108,8 @@ def run(
action = "validating" if dataloader.dataset.root.stem == "val" else "testing"
desc = f"{pbar.desc[:-36]}{action:>36}" if pbar else f"{action}"
bar = tqdm(dataloader, desc, n, not training, bar_format=TQDM_BAR_FORMAT, position=0)
with torch.amp.autocast("cuda", enabled=device.type != "cpu"):
device_amp = torch.is_autocast_available(device_type=device.type)
with torch.amp.autocast(device_type=device.type, enabled=device_amp):
for images, labels in bar:
with dt[0]:
images, labels = images.to(device, non_blocking=True), labels.to(device)

View File

@ -352,7 +352,11 @@ def train(hyp, opt, device, callbacks):
maps = np.zeros(nc) # mAP per class
results = (0, 0, 0, 0, 0, 0, 0) # P, R, mAP@.5, mAP@.5-.95, val_loss(box, obj, cls)
scheduler.last_epoch = start_epoch - 1 # do not move
scaler = amp.GradScaler(enabled=device.type != "cpu")
#checking if autocast is available
device_amp = torch.is_autocast_available(device_type=device.type)
scaler = torch.amp.GradScaler(enabled=(device_amp and device.type != "cpu"))
stopper, stop = EarlyStopping(patience=opt.patience), False
compute_loss = ComputeLoss(model) # init loss class
callbacks.run("on_train_start")
@ -409,7 +413,7 @@ def train(hyp, opt, device, callbacks):
imgs = nn.functional.interpolate(imgs, size=ns, mode="bilinear", align_corners=False)
# Forward
with torch.amp.autocast("cuda", enabled=device.type != "cpu"):
with torch.amp.autocast(device_type=device.type, enabled=device_amp):
pred = model(imgs) # forward
loss, loss_items = compute_loss(pred, targets.to(device)) # loss scaled by batch_size
if RANK != -1: