diff --git a/modules/devices.py b/modules/devices.py index ed3951cf9..011ef9cc0 100644 --- a/modules/devices.py +++ b/modules/devices.py @@ -207,10 +207,10 @@ def set_cuda_params(): unet_needs_upcast = shared.opts.upcast_sampling if shared.opts.inference_mode == 'inference-mode': inference_context = torch.inference_mode - elif shared.opts.inference_mode == 'no-grad': - inference_context = torch.no_grad - else: + elif shared.opts.inference_mode == 'none': inference_context = contextlib.nullcontext + else: + inference_context = torch.no_grad shared.log.debug(f'Desired Torch parameters: dtype={shared.opts.cuda_dtype} no-half={shared.opts.no_half} no-half-vae={shared.opts.no_half_vae} upscast={shared.opts.upcast_sampling}') shared.log.info(f'Setting Torch parameters: dtype={dtype} vae={dtype_vae} unet={dtype_unet} context={inference_context.__name__} fp16={fp16_ok} bf16={bf16_ok}') shared.log.debug(f'Torch default device: {torch.device(get_optimal_device_name())}') diff --git a/modules/processing.py b/modules/processing.py index 985773c80..d38b58270 100644 --- a/modules/processing.py +++ b/modules/processing.py @@ -1000,7 +1000,6 @@ class StableDiffusionProcessingTxt2Img(StableDiffusionProcessing): self.ops.append('hires') target_width = self.hr_upscale_to_x target_height = self.hr_upscale_to_y - if latent_scale_mode is not None: for i in range(samples.shape[0]): save_intermediate(samples, i) @@ -1009,6 +1008,16 @@ class StableDiffusionProcessingTxt2Img(StableDiffusionProcessing): image_conditioning = self.img2img_image_conditioning(decode_first_stage(self.sd_model, samples.to(dtype=devices.dtype_vae)), samples) else: image_conditioning = self.txt2img_image_conditioning(samples.to(dtype=devices.dtype_vae)) + if self.latent_sampler == "PLMS": + self.latent_sampler = 'UniPC' + self.sampler = modules.sd_samplers.create_sampler(self.latent_sampler or self.sampler_name, self.sd_model) + samples = samples[:, :, self.truncate_y//2:samples.shape[2]-(self.truncate_y+1)//2, self.truncate_x//2:samples.shape[3]-(self.truncate_x+1)//2] + noise = create_random_tensors(samples.shape[1:], seeds=seeds, subseeds=subseeds, subseed_strength=subseed_strength, p=self) + x = None + devices.torch_gc() # GC now before running the next img2img to prevent running out of memory + modules.sd_models.apply_token_merging(self.sd_model, self.get_token_merging_ratio(for_hr=True)) + samples = self.sampler.sample_img2img(self, samples, noise, conditioning, unconditional_conditioning, steps=self.hr_second_pass_steps or self.steps, image_conditioning=image_conditioning) + modules.sd_models.apply_token_merging(self.sd_model, self.get_token_merging_ratio()) else: decoded_samples = decode_first_stage(self.sd_model, samples.to(dtype=devices.dtype_vae)) lowres_samples = torch.clamp((decoded_samples + 1.0) / 2.0, min=0.0, max=1.0) @@ -1035,16 +1044,6 @@ class StableDiffusionProcessingTxt2Img(StableDiffusionProcessing): samples = self.sd_model.get_first_stage_encoding(self.sd_model.encode_first_stage(decoded_samples)) image_conditioning = self.img2img_image_conditioning(decoded_samples, samples) shared.state.nextjob() - if self.latent_sampler == "PLMS": - self.latent_sampler = 'UniPC' - self.sampler = modules.sd_samplers.create_sampler(self.latent_sampler or self.sampler_name, self.sd_model) - samples = samples[:, :, self.truncate_y//2:samples.shape[2]-(self.truncate_y+1)//2, self.truncate_x//2:samples.shape[3]-(self.truncate_x+1)//2] - noise = create_random_tensors(samples.shape[1:], seeds=seeds, subseeds=subseeds, subseed_strength=subseed_strength, p=self) - x = None - devices.torch_gc() # GC now before running the next img2img to prevent running out of memory - modules.sd_models.apply_token_merging(self.sd_model, self.get_token_merging_ratio(for_hr=True)) - samples = self.sampler.sample_img2img(self, samples, noise, conditioning, unconditional_conditioning, steps=self.hr_second_pass_steps or self.steps, image_conditioning=image_conditioning) - modules.sd_models.apply_token_merging(self.sd_model, self.get_token_merging_ratio()) self.is_hr_pass = False return samples