From cbb9c0e8bea079d7709bf442f5b1f01cd20c0187 Mon Sep 17 00:00:00 2001 From: lllyasviel Date: Sun, 4 Feb 2024 02:21:33 -0800 Subject: [PATCH] fix ddim inpaint --- modules/sd_samplers_cfg_denoiser.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/modules/sd_samplers_cfg_denoiser.py b/modules/sd_samplers_cfg_denoiser.py index bb86aaa9..b0103492 100644 --- a/modules/sd_samplers_cfg_denoiser.py +++ b/modules/sd_samplers_cfg_denoiser.py @@ -158,7 +158,9 @@ class CFGDenoiser(torch.nn.Module): if classic_ddim_eps_estimation: acd = self.inner_model.inner_model.alphas_cumprod fake_sigmas = ((1 - acd) / acd) ** 0.5 - real_sigma = fake_sigmas[sigma.round().long().clip(0, int(fake_sigmas.shape[0]))] + tid = sigma.round().long().clip(0, int(fake_sigmas.shape[0])) + real_sigma = fake_sigmas[tid] + sqrt_alpha_cumprod = torch.sqrt(acd[tid]) real_sigma_data = 1.0 x = x * (real_sigma ** 2.0 + real_sigma_data ** 2.0) ** 0.5 sigma = real_sigma @@ -172,11 +174,16 @@ class CFGDenoiser(torch.nn.Module): # If we use masks, blending between the denoised and original latent images occurs here. def apply_blend(current_latent): - blended_latent = current_latent * self.nmask + self.init_latent * self.mask + init_latent = self.init_latent + + if classic_ddim_eps_estimation: + init_latent = init_latent / sqrt_alpha_cumprod + + blended_latent = current_latent * self.nmask + init_latent * self.mask if self.p.scripts is not None: from modules import scripts - mba = scripts.MaskBlendArgs(current_latent, self.nmask, self.init_latent, self.mask, blended_latent, denoiser=self, sigma=sigma) + mba = scripts.MaskBlendArgs(current_latent, self.nmask, init_latent, self.mask, blended_latent, denoiser=self, sigma=sigma) self.p.scripts.on_mask_blend(self.p, mba) blended_latent = mba.blended_latent