Skip to content

Commit 7349088

Browse files
committed
--no-half-vae
1 parent a357823 commit 7349088

5 files changed

Lines changed: 20 additions & 5 deletions

File tree

modules/devices.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ def enable_tf32():
3636

3737
device = device_gfpgan = device_bsrgan = device_esrgan = device_scunet = device_codeformer = get_optimal_device()
3838
dtype = torch.float16
39+
dtype_vae = torch.float16
3940

4041
def randn(seed, shape):
4142
# Pytorch currently doesn't handle setting randomness correctly when the metal backend is used.
@@ -59,9 +60,12 @@ def randn_without_seed(shape):
5960
return torch.randn(shape, device=device)
6061

6162

62-
def autocast():
63+
def autocast(disable=False):
6364
from modules import shared
6465

66+
if disable:
67+
return contextlib.nullcontext()
68+
6569
if dtype == torch.float32 or shared.cmd_opts.precision == "full":
6670
return contextlib.nullcontext()
6771

modules/processing.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,13 @@ def create_random_tensors(shape, seeds, subseeds=None, subseed_strength=0.0, see
259259
return x
260260

261261

262+
def decode_first_stage(model, x):
263+
with devices.autocast(disable=x.dtype == devices.dtype_vae):
264+
x = model.decode_first_stage(x)
265+
266+
return x
267+
268+
262269
def get_fixed_seed(seed):
263270
if seed is None or seed == '' or seed == -1:
264271
return int(random.randrange(4294967294))
@@ -400,7 +407,7 @@ def infotext(iteration=0, position_in_batch=0):
400407

401408
samples_ddim = samples_ddim.to(devices.dtype)
402409

403-
x_samples_ddim = p.sd_model.decode_first_stage(samples_ddim)
410+
x_samples_ddim = decode_first_stage(p.sd_model, samples_ddim)
404411
x_samples_ddim = torch.clamp((x_samples_ddim + 1.0) / 2.0, min=0.0, max=1.0)
405412

406413
del samples_ddim
@@ -533,7 +540,7 @@ def sample(self, conditioning, unconditional_conditioning, seeds, subseeds, subs
533540
if self.scale_latent:
534541
samples = torch.nn.functional.interpolate(samples, size=(self.height // opt_f, self.width // opt_f), mode="bilinear")
535542
else:
536-
decoded_samples = self.sd_model.decode_first_stage(samples)
543+
decoded_samples = decode_first_stage(self.sd_model, samples)
537544

538545
if opts.upscaler_for_img2img is None or opts.upscaler_for_img2img == "None":
539546
decoded_samples = torch.nn.functional.interpolate(decoded_samples, size=(self.height, self.width), mode="bilinear")

modules/sd_models.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ def load_model_weights(model, checkpoint_info):
149149
model.half()
150150

151151
devices.dtype = torch.float32 if shared.cmd_opts.no_half else torch.float16
152+
devices.dtype_vae = torch.float32 if shared.cmd_opts.no_half or shared.cmd_opts.no_half_vae else torch.float16
152153

153154
vae_file = os.path.splitext(checkpoint_file)[0] + ".vae.pt"
154155
if os.path.exists(vae_file):
@@ -158,6 +159,8 @@ def load_model_weights(model, checkpoint_info):
158159

159160
model.first_stage_model.load_state_dict(vae_dict)
160161

162+
model.first_stage_model.to(devices.dtype_vae)
163+
161164
model.sd_model_hash = sd_model_hash
162165
model.sd_model_checkpoint = checkpoint_file
163166
model.sd_checkpoint_info = checkpoint_info

modules/sd_samplers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import k_diffusion.sampling
88
import ldm.models.diffusion.ddim
99
import ldm.models.diffusion.plms
10-
from modules import prompt_parser
10+
from modules import prompt_parser, devices, processing
1111

1212
from modules.shared import opts, cmd_opts, state
1313
import modules.shared as shared
@@ -83,7 +83,7 @@ def setup_img2img_steps(p, steps=None):
8383

8484

8585
def sample_to_image(samples):
86-
x_sample = shared.sd_model.decode_first_stage(samples[0:1].type(shared.sd_model.dtype))[0]
86+
x_sample = processing.decode_first_stage(shared.sd_model, samples[0:1])[0]
8787
x_sample = torch.clamp((x_sample + 1.0) / 2.0, min=0.0, max=1.0)
8888
x_sample = 255. * np.moveaxis(x_sample.cpu().numpy(), 0, 2)
8989
x_sample = x_sample.astype(np.uint8)

modules/shared.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
parser.add_argument("--gfpgan-dir", type=str, help="GFPGAN directory", default=('./src/gfpgan' if os.path.exists('./src/gfpgan') else './GFPGAN'))
2626
parser.add_argument("--gfpgan-model", type=str, help="GFPGAN model file name", default=None)
2727
parser.add_argument("--no-half", action='store_true', help="do not switch the model to 16-bit floats")
28+
parser.add_argument("--no-half-vae", action='store_true', help="do not switch the VAE model to 16-bit floats")
2829
parser.add_argument("--no-progressbar-hiding", action='store_true', help="do not hide progressbar in gradio UI (we hide it because it slows down ML if you have hardware acceleration in browser)")
2930
parser.add_argument("--max-batch-count", type=int, default=16, help="maximum batch count value for the UI")
3031
parser.add_argument("--embeddings-dir", type=str, default=os.path.join(script_path, 'embeddings'), help="embeddings directory for textual inversion (default: embeddings)")

0 commit comments

Comments
 (0)