Skip to content

Commit 4d5f169

Browse files
committed
Use devices.autocast instead of torch.autocast
1 parent 21effd6 commit 4d5f169

File tree

5 files changed

+6
-11
lines changed

5 files changed

+6
-11
lines changed

modules/hypernetworks/hypernetwork.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ def train_hypernetwork(hypernetwork_name, learn_rate, batch_size, gradient_step,
495495
if shared.state.interrupted:
496496
break
497497

498-
with torch.autocast("cuda"):
498+
with devices.autocast():
499499
x = batch.latent_sample.to(devices.device, non_blocking=pin_memory)
500500
if tag_drop_out != 0 or shuffle_tags:
501501
shared.sd_model.cond_stage_model.to(devices.device)

modules/interrogate.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,7 @@ def interrogate(self, pil_image):
148148

149149
clip_image = self.clip_preprocess(pil_image).unsqueeze(0).type(self.dtype).to(devices.device_interrogate)
150150

151-
precision_scope = torch.autocast if shared.cmd_opts.precision == "autocast" else contextlib.nullcontext
152-
with torch.no_grad(), precision_scope("cuda"):
151+
with torch.no_grad(), devices.autocast():
153152
image_features = self.clip_model.encode_image(clip_image).type(self.dtype)
154153

155154
image_features /= image_features.norm(dim=-1, keepdim=True)

modules/swinir_model.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@
1313
from modules.swinir_model_arch_v2 import Swin2SR as net2
1414
from modules.upscaler import Upscaler, UpscalerData
1515

16-
precision_scope = (
17-
torch.autocast if cmd_opts.precision == "autocast" else contextlib.nullcontext
18-
)
19-
2016

2117
class UpscalerSwinIR(Upscaler):
2218
def __init__(self, dirname):
@@ -112,7 +108,7 @@ def upscale(
112108
img = np.moveaxis(img, 2, 0) / 255
113109
img = torch.from_numpy(img).float()
114110
img = img.unsqueeze(0).to(devices.device_swinir)
115-
with torch.no_grad(), precision_scope("cuda"):
111+
with torch.no_grad(), devices.autocast():
116112
_, _, h_old, w_old = img.size()
117113
h_pad = (h_old // window_size + 1) * window_size - h_old
118114
w_pad = (w_old // window_size + 1) * window_size - w_old

modules/textual_inversion/dataset.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def __init__(self, data_root, width, height, repeats, flip_p=0.5, placeholder_to
8282
torchdata = torch.from_numpy(npimage).permute(2, 0, 1).to(device=device, dtype=torch.float32)
8383
latent_sample = None
8484

85-
with torch.autocast("cuda"):
85+
with devices.autocast():
8686
latent_dist = model.encode_first_stage(torchdata.unsqueeze(dim=0))
8787

8888
if latent_sampling_method == "once" or (latent_sampling_method == "deterministic" and not isinstance(latent_dist, DiagonalGaussianDistribution)):
@@ -101,7 +101,7 @@ def __init__(self, data_root, width, height, repeats, flip_p=0.5, placeholder_to
101101
entry.cond_text = self.create_text(filename_text)
102102

103103
if include_cond and not (self.tag_drop_out != 0 or self.shuffle_tags):
104-
with torch.autocast("cuda"):
104+
with devices.autocast():
105105
entry.cond = cond_model([entry.cond_text]).to(devices.cpu).squeeze(0)
106106

107107
self.dataset.append(entry)

modules/textual_inversion/textual_inversion.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ def train_embedding(embedding_name, learn_rate, batch_size, gradient_step, data_
316316
if shared.state.interrupted:
317317
break
318318

319-
with torch.autocast("cuda"):
319+
with devices.autocast():
320320
# c = stack_conds(batch.cond).to(devices.device)
321321
# mask = torch.tensor(batch.emb_index).to(devices.device, non_blocking=pin_memory)
322322
# print(mask)

0 commit comments

Comments
 (0)