Skip to content

Commit

Permalink
rename ModelOnDevice to SwapToDevice
Browse files Browse the repository at this point in the history
  • Loading branch information
palp committed Aug 6, 2023
1 parent ced97f0 commit 6c18c84
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions sgm/inference/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def do_sample(
with autocast(device) as precision_scope:
with model.ema_scope():
num_samples = [num_samples]
with ModelOnDevice(model.conditioner, device):
with SwapToDevice(model.conditioner, device):
batch, batch_uc = get_batch(
get_unique_embedder_keys_from_conditioner(model.conditioner),
value_dict,
Expand Down Expand Up @@ -189,11 +189,11 @@ def denoiser(input, sigma, c):
model.model, input, sigma, c, **additional_model_inputs
)

with ModelOnDevice(model.denoiser, device):
with ModelOnDevice(model.model, device):
with SwapToDevice(model.denoiser, device):
with SwapToDevice(model.model, device):
samples_z = sampler(denoiser, randn, cond=c, uc=uc)

with ModelOnDevice(model.first_stage_model, device):
with SwapToDevice(model.first_stage_model, device):
samples_x = model.decode_first_stage(samples_z)
samples = torch.clamp((samples_x + 1.0) / 2.0, min=0.0, max=1.0)

Expand Down Expand Up @@ -293,7 +293,7 @@ def do_img2img(
with torch.no_grad():
with autocast(device):
with model.ema_scope():
with ModelOnDevice(model.conditioner, device):
with SwapToDevice(model.conditioner, device):
batch, batch_uc = get_batch(
get_unique_embedder_keys_from_conditioner(model.conditioner),
value_dict,
Expand All @@ -313,7 +313,7 @@ def do_img2img(
if skip_encode:
z = img
else:
with ModelOnDevice(model.first_stage_model, device):
with SwapToDevice(model.first_stage_model, device):
z = model.encode_first_stage(img)

noise = torch.randn_like(z)
Expand All @@ -336,11 +336,11 @@ def do_img2img(
def denoiser(x, sigma, c):
return model.denoiser(model.model, x, sigma, c)

with ModelOnDevice(model.denoiser, device):
with ModelOnDevice(model.model, device):
with SwapToDevice(model.denoiser, device):
with SwapToDevice(model.model, device):
samples_z = sampler(denoiser, noised_z, cond=c, uc=uc)

with ModelOnDevice(model.first_stage_model, device):
with SwapToDevice(model.first_stage_model, device):
samples_x = model.decode_first_stage(samples_z)
samples = torch.clamp((samples_x + 1.0) / 2.0, min=0.0, max=1.0)

Expand All @@ -352,7 +352,7 @@ def denoiser(x, sigma, c):
return samples


class ModelOnDevice(object):
class SwapToDevice(object):
def __init__(
self,
model: Union[torch.nn.Module, torch.Tensor],
Expand Down

0 comments on commit 6c18c84

Please sign in to comment.