2022-09-03 03:08:45 -06:00
|
|
|
from collections import namedtuple
|
2022-09-06 14:10:12 -06:00
|
|
|
import numpy as np
|
2022-09-03 03:08:45 -06:00
|
|
|
import torch
|
|
|
|
import tqdm
|
2022-09-06 14:10:12 -06:00
|
|
|
from PIL import Image
|
2022-09-28 01:49:07 -06:00
|
|
|
import inspect
|
2022-09-03 03:08:45 -06:00
|
|
|
import k_diffusion.sampling
|
2022-09-05 17:09:01 -06:00
|
|
|
import ldm.models.diffusion.ddim
|
|
|
|
import ldm.models.diffusion.plms
|
2022-09-15 04:10:16 -06:00
|
|
|
from modules import prompt_parser
|
2022-09-03 03:08:45 -06:00
|
|
|
|
|
|
|
from modules.shared import opts, cmd_opts, state
|
|
|
|
import modules.shared as shared
|
|
|
|
|
2022-09-03 08:21:15 -06:00
|
|
|
|
|
|
|
SamplerData = namedtuple('SamplerData', ['name', 'constructor', 'aliases'])
|
|
|
|
|
|
|
|
samplers_k_diffusion = [
|
|
|
|
('Euler a', 'sample_euler_ancestral', ['k_euler_a']),
|
|
|
|
('Euler', 'sample_euler', ['k_euler']),
|
|
|
|
('LMS', 'sample_lms', ['k_lms']),
|
|
|
|
('Heun', 'sample_heun', ['k_heun']),
|
|
|
|
('DPM2', 'sample_dpm_2', ['k_dpm_2']),
|
|
|
|
('DPM2 a', 'sample_dpm_2_ancestral', ['k_dpm_2_a']),
|
2022-09-29 01:15:38 -06:00
|
|
|
('DPM fast', 'sample_dpm_fast', ['k_dpm_fast']),
|
|
|
|
('DPM adaptive', 'sample_dpm_adaptive', ['k_dpm_ad']),
|
2022-09-03 08:21:15 -06:00
|
|
|
]
|
|
|
|
|
|
|
|
samplers_data_k_diffusion = [
|
|
|
|
SamplerData(label, lambda model, funcname=funcname: KDiffusionSampler(funcname, model), aliases)
|
|
|
|
for label, funcname, aliases in samplers_k_diffusion
|
|
|
|
if hasattr(k_diffusion.sampling, funcname)
|
|
|
|
]
|
|
|
|
|
2022-09-03 03:08:45 -06:00
|
|
|
samplers = [
|
2022-09-03 08:21:15 -06:00
|
|
|
*samplers_data_k_diffusion,
|
2022-09-05 17:09:01 -06:00
|
|
|
SamplerData('DDIM', lambda model: VanillaStableDiffusionSampler(ldm.models.diffusion.ddim.DDIMSampler, model), []),
|
|
|
|
SamplerData('PLMS', lambda model: VanillaStableDiffusionSampler(ldm.models.diffusion.plms.PLMSSampler, model), []),
|
2022-09-03 03:08:45 -06:00
|
|
|
]
|
2022-09-29 04:30:33 -06:00
|
|
|
samplers_for_img2img = [x for x in samplers if x.name not in ['PLMS', 'DPM fast', 'DPM adaptive']]
|
2022-09-03 03:08:45 -06:00
|
|
|
|
2022-09-26 02:56:47 -06:00
|
|
|
sampler_extra_params = {
|
2022-09-28 01:49:07 -06:00
|
|
|
'sample_euler': ['s_churn', 's_tmin', 's_tmax', 's_noise'],
|
|
|
|
'sample_heun': ['s_churn', 's_tmin', 's_tmax', 's_noise'],
|
|
|
|
'sample_dpm_2': ['s_churn', 's_tmin', 's_tmax', 's_noise'],
|
2022-09-26 02:56:47 -06:00
|
|
|
}
|
2022-09-03 03:08:45 -06:00
|
|
|
|
2022-09-19 07:42:56 -06:00
|
|
|
def setup_img2img_steps(p, steps=None):
|
|
|
|
if opts.img2img_fix_steps or steps is not None:
|
|
|
|
steps = int((steps or p.steps) / min(p.denoising_strength, 0.999)) if p.denoising_strength > 0 else 0
|
2022-09-16 04:38:02 -06:00
|
|
|
t_enc = p.steps - 1
|
|
|
|
else:
|
|
|
|
steps = p.steps
|
|
|
|
t_enc = int(min(p.denoising_strength, 0.999) * steps)
|
|
|
|
|
|
|
|
return steps, t_enc
|
|
|
|
|
|
|
|
|
2022-09-06 14:10:12 -06:00
|
|
|
def sample_to_image(samples):
|
|
|
|
x_sample = shared.sd_model.decode_first_stage(samples[0:1].type(shared.sd_model.dtype))[0]
|
|
|
|
x_sample = torch.clamp((x_sample + 1.0) / 2.0, min=0.0, max=1.0)
|
|
|
|
x_sample = 255. * np.moveaxis(x_sample.cpu().numpy(), 0, 2)
|
|
|
|
x_sample = x_sample.astype(np.uint8)
|
|
|
|
return Image.fromarray(x_sample)
|
|
|
|
|
|
|
|
|
|
|
|
def store_latent(decoded):
|
|
|
|
state.current_latent = decoded
|
|
|
|
|
|
|
|
if opts.show_progress_every_n_steps > 0 and shared.state.sampling_step % opts.show_progress_every_n_steps == 0:
|
|
|
|
if not shared.parallel_processing_allowed:
|
|
|
|
shared.state.current_image = sample_to_image(decoded)
|
|
|
|
|
|
|
|
|
2022-09-03 03:08:45 -06:00
|
|
|
|
2022-09-05 17:09:01 -06:00
|
|
|
def extended_tdqm(sequence, *args, desc=None, **kwargs):
|
|
|
|
state.sampling_steps = len(sequence)
|
|
|
|
state.sampling_step = 0
|
|
|
|
|
2022-09-08 07:37:13 -06:00
|
|
|
for x in tqdm.tqdm(sequence, *args, desc=state.job, file=shared.progress_print_out, **kwargs):
|
2022-09-05 17:09:01 -06:00
|
|
|
if state.interrupted:
|
|
|
|
break
|
|
|
|
|
|
|
|
yield x
|
|
|
|
|
|
|
|
state.sampling_step += 1
|
2022-09-08 07:37:13 -06:00
|
|
|
shared.total_tqdm.update()
|
2022-09-05 17:09:01 -06:00
|
|
|
|
|
|
|
|
|
|
|
ldm.models.diffusion.ddim.tqdm = lambda *args, desc=None, **kwargs: extended_tdqm(*args, desc=desc, **kwargs)
|
|
|
|
ldm.models.diffusion.plms.tqdm = lambda *args, desc=None, **kwargs: extended_tdqm(*args, desc=desc, **kwargs)
|
|
|
|
|
|
|
|
|
2022-09-03 03:08:45 -06:00
|
|
|
class VanillaStableDiffusionSampler:
|
|
|
|
def __init__(self, constructor, sd_model):
|
|
|
|
self.sampler = constructor(sd_model)
|
2022-09-08 10:34:20 -06:00
|
|
|
self.orig_p_sample_ddim = self.sampler.p_sample_ddim if hasattr(self.sampler, 'p_sample_ddim') else self.sampler.p_sample_plms
|
2022-09-03 03:08:45 -06:00
|
|
|
self.mask = None
|
|
|
|
self.nmask = None
|
|
|
|
self.init_latent = None
|
2022-09-13 12:49:58 -06:00
|
|
|
self.sampler_noises = None
|
2022-09-15 04:10:16 -06:00
|
|
|
self.step = 0
|
2022-09-28 09:09:06 -06:00
|
|
|
self.eta = None
|
|
|
|
self.default_eta = 0.0
|
2022-09-15 04:10:16 -06:00
|
|
|
|
2022-09-13 12:49:58 -06:00
|
|
|
def number_of_needed_noises(self, p):
|
|
|
|
return 0
|
2022-09-03 03:08:45 -06:00
|
|
|
|
2022-09-15 04:10:16 -06:00
|
|
|
def p_sample_ddim_hook(self, x_dec, cond, ts, unconditional_conditioning, *args, **kwargs):
|
|
|
|
cond = prompt_parser.reconstruct_cond_batch(cond, self.step)
|
|
|
|
unconditional_conditioning = prompt_parser.reconstruct_cond_batch(unconditional_conditioning, self.step)
|
|
|
|
|
|
|
|
if self.mask is not None:
|
|
|
|
img_orig = self.sampler.model.q_sample(self.init_latent, ts)
|
|
|
|
x_dec = img_orig * self.mask + self.nmask * x_dec
|
|
|
|
|
|
|
|
res = self.orig_p_sample_ddim(x_dec, cond, ts, unconditional_conditioning=unconditional_conditioning, *args, **kwargs)
|
|
|
|
|
|
|
|
if self.mask is not None:
|
|
|
|
store_latent(self.init_latent * self.mask + self.nmask * res[1])
|
|
|
|
else:
|
|
|
|
store_latent(res[1])
|
|
|
|
|
|
|
|
self.step += 1
|
|
|
|
return res
|
2022-09-03 03:08:45 -06:00
|
|
|
|
2022-09-28 09:09:06 -06:00
|
|
|
def initialize(self, p):
|
|
|
|
self.eta = p.eta or opts.eta_ddim
|
|
|
|
|
|
|
|
for fieldname in ['p_sample_ddim', 'p_sample_plms']:
|
|
|
|
if hasattr(self.sampler, fieldname):
|
|
|
|
setattr(self.sampler, fieldname, self.p_sample_ddim_hook)
|
|
|
|
|
|
|
|
self.mask = p.mask if hasattr(p, 'mask') else None
|
|
|
|
self.nmask = p.nmask if hasattr(p, 'nmask') else None
|
|
|
|
|
2022-09-19 07:42:56 -06:00
|
|
|
def sample_img2img(self, p, x, noise, conditioning, unconditional_conditioning, steps=None):
|
|
|
|
steps, t_enc = setup_img2img_steps(p, steps)
|
2022-09-03 03:08:45 -06:00
|
|
|
|
2022-09-28 13:30:52 -06:00
|
|
|
self.initialize(p)
|
|
|
|
|
2022-09-13 11:12:24 -06:00
|
|
|
# existing code fails with cetain step counts, like 9
|
2022-09-03 03:08:45 -06:00
|
|
|
try:
|
2022-09-28 09:09:06 -06:00
|
|
|
self.sampler.make_schedule(ddim_num_steps=steps, ddim_eta=self.eta, ddim_discretize=p.ddim_discretize, verbose=False)
|
2022-09-03 03:08:45 -06:00
|
|
|
except Exception:
|
2022-09-28 09:09:06 -06:00
|
|
|
self.sampler.make_schedule(ddim_num_steps=steps+1, ddim_eta=self.eta, ddim_discretize=p.ddim_discretize, verbose=False)
|
2022-09-03 03:08:45 -06:00
|
|
|
|
|
|
|
x1 = self.sampler.stochastic_encode(x, torch.tensor([t_enc] * int(x.shape[0])).to(shared.device), noise=noise)
|
|
|
|
|
2022-09-19 07:42:56 -06:00
|
|
|
self.init_latent = x
|
2022-09-15 23:51:21 -06:00
|
|
|
self.step = 0
|
2022-09-03 03:08:45 -06:00
|
|
|
|
|
|
|
samples = self.sampler.decode(x1, conditioning, t_enc, unconditional_guidance_scale=p.cfg_scale, unconditional_conditioning=unconditional_conditioning)
|
|
|
|
|
|
|
|
return samples
|
|
|
|
|
2022-09-19 07:42:56 -06:00
|
|
|
def sample(self, p, x, conditioning, unconditional_conditioning, steps=None):
|
2022-09-28 09:09:06 -06:00
|
|
|
self.initialize(p)
|
|
|
|
|
2022-09-08 10:20:41 -06:00
|
|
|
self.init_latent = None
|
2022-09-15 23:51:21 -06:00
|
|
|
self.step = 0
|
2022-09-08 10:20:41 -06:00
|
|
|
|
2022-09-19 07:42:56 -06:00
|
|
|
steps = steps or p.steps
|
|
|
|
|
2022-09-13 11:12:24 -06:00
|
|
|
# existing code fails with cetin step counts, like 9
|
|
|
|
try:
|
2022-09-28 09:09:06 -06:00
|
|
|
samples_ddim, _ = self.sampler.sample(S=steps, conditioning=conditioning, batch_size=int(x.shape[0]), shape=x[0].shape, verbose=False, unconditional_guidance_scale=p.cfg_scale, unconditional_conditioning=unconditional_conditioning, x_T=x, eta=self.eta)
|
2022-09-13 11:12:24 -06:00
|
|
|
except Exception:
|
2022-09-28 09:09:06 -06:00
|
|
|
samples_ddim, _ = self.sampler.sample(S=steps+1, conditioning=conditioning, batch_size=int(x.shape[0]), shape=x[0].shape, verbose=False, unconditional_guidance_scale=p.cfg_scale, unconditional_conditioning=unconditional_conditioning, x_T=x, eta=self.eta)
|
2022-09-13 11:12:24 -06:00
|
|
|
|
2022-09-03 03:08:45 -06:00
|
|
|
return samples_ddim
|
|
|
|
|
|
|
|
|
|
|
|
class CFGDenoiser(torch.nn.Module):
|
|
|
|
def __init__(self, model):
|
|
|
|
super().__init__()
|
|
|
|
self.inner_model = model
|
|
|
|
self.mask = None
|
|
|
|
self.nmask = None
|
|
|
|
self.init_latent = None
|
2022-09-15 04:10:16 -06:00
|
|
|
self.step = 0
|
2022-09-03 03:08:45 -06:00
|
|
|
|
|
|
|
def forward(self, x, sigma, uncond, cond, cond_scale):
|
2022-09-15 04:10:16 -06:00
|
|
|
cond = prompt_parser.reconstruct_cond_batch(cond, self.step)
|
|
|
|
uncond = prompt_parser.reconstruct_cond_batch(uncond, self.step)
|
|
|
|
|
2022-09-03 03:08:45 -06:00
|
|
|
if shared.batch_cond_uncond:
|
|
|
|
x_in = torch.cat([x] * 2)
|
|
|
|
sigma_in = torch.cat([sigma] * 2)
|
|
|
|
cond_in = torch.cat([uncond, cond])
|
|
|
|
uncond, cond = self.inner_model(x_in, sigma_in, cond=cond_in).chunk(2)
|
|
|
|
denoised = uncond + (cond - uncond) * cond_scale
|
|
|
|
else:
|
|
|
|
uncond = self.inner_model(x, sigma, cond=uncond)
|
|
|
|
cond = self.inner_model(x, sigma, cond=cond)
|
|
|
|
denoised = uncond + (cond - uncond) * cond_scale
|
|
|
|
|
|
|
|
if self.mask is not None:
|
|
|
|
denoised = self.init_latent * self.mask + self.nmask * denoised
|
|
|
|
|
2022-09-15 04:10:16 -06:00
|
|
|
self.step += 1
|
|
|
|
|
2022-09-03 03:08:45 -06:00
|
|
|
return denoised
|
|
|
|
|
|
|
|
|
2022-09-19 07:42:56 -06:00
|
|
|
def extended_trange(sampler, count, *args, **kwargs):
|
2022-09-05 17:09:01 -06:00
|
|
|
state.sampling_steps = count
|
|
|
|
state.sampling_step = 0
|
|
|
|
|
2022-09-08 07:37:13 -06:00
|
|
|
for x in tqdm.trange(count, *args, desc=state.job, file=shared.progress_print_out, **kwargs):
|
2022-09-03 03:08:45 -06:00
|
|
|
if state.interrupted:
|
|
|
|
break
|
|
|
|
|
2022-09-19 07:42:56 -06:00
|
|
|
if sampler.stop_at is not None and x > sampler.stop_at:
|
|
|
|
break
|
|
|
|
|
2022-09-03 03:08:45 -06:00
|
|
|
yield x
|
|
|
|
|
2022-09-05 17:09:01 -06:00
|
|
|
state.sampling_step += 1
|
2022-09-08 07:37:13 -06:00
|
|
|
shared.total_tqdm.update()
|
2022-09-05 17:09:01 -06:00
|
|
|
|
2022-09-03 03:08:45 -06:00
|
|
|
|
2022-09-16 00:47:03 -06:00
|
|
|
class TorchHijack:
|
|
|
|
def __init__(self, kdiff_sampler):
|
|
|
|
self.kdiff_sampler = kdiff_sampler
|
|
|
|
|
|
|
|
def __getattr__(self, item):
|
|
|
|
if item == 'randn_like':
|
|
|
|
return self.kdiff_sampler.randn_like
|
|
|
|
|
|
|
|
if hasattr(torch, item):
|
|
|
|
return getattr(torch, item)
|
|
|
|
|
|
|
|
raise AttributeError("'{}' object has no attribute '{}'".format(type(self).__name__, item))
|
|
|
|
|
2022-09-13 12:49:58 -06:00
|
|
|
|
2022-09-03 03:08:45 -06:00
|
|
|
class KDiffusionSampler:
|
|
|
|
def __init__(self, funcname, sd_model):
|
2022-09-15 05:55:38 -06:00
|
|
|
self.model_wrap = k_diffusion.external.CompVisDenoiser(sd_model, quantize=shared.opts.enable_quantization)
|
2022-09-03 03:08:45 -06:00
|
|
|
self.funcname = funcname
|
|
|
|
self.func = getattr(k_diffusion.sampling, self.funcname)
|
2022-09-28 01:49:07 -06:00
|
|
|
self.extra_params = sampler_extra_params.get(funcname, [])
|
2022-09-03 03:08:45 -06:00
|
|
|
self.model_wrap_cfg = CFGDenoiser(self.model_wrap)
|
2022-09-13 12:49:58 -06:00
|
|
|
self.sampler_noises = None
|
|
|
|
self.sampler_noise_index = 0
|
2022-09-19 07:42:56 -06:00
|
|
|
self.stop_at = None
|
2022-09-28 09:09:06 -06:00
|
|
|
self.eta = None
|
|
|
|
self.default_eta = 1.0
|
2022-09-03 03:08:45 -06:00
|
|
|
|
2022-09-06 10:33:51 -06:00
|
|
|
def callback_state(self, d):
|
2022-09-06 14:10:12 -06:00
|
|
|
store_latent(d["denoised"])
|
2022-09-06 10:33:51 -06:00
|
|
|
|
2022-09-13 12:49:58 -06:00
|
|
|
def number_of_needed_noises(self, p):
|
|
|
|
return p.steps
|
|
|
|
|
|
|
|
def randn_like(self, x):
|
|
|
|
noise = self.sampler_noises[self.sampler_noise_index] if self.sampler_noises is not None and self.sampler_noise_index < len(self.sampler_noises) else None
|
|
|
|
|
|
|
|
if noise is not None and x.shape == noise.shape:
|
|
|
|
res = noise
|
|
|
|
else:
|
2022-09-16 00:47:03 -06:00
|
|
|
res = torch.randn_like(x)
|
2022-09-13 12:49:58 -06:00
|
|
|
|
|
|
|
self.sampler_noise_index += 1
|
|
|
|
return res
|
|
|
|
|
2022-09-28 09:09:06 -06:00
|
|
|
def initialize(self, p):
|
2022-09-19 07:42:56 -06:00
|
|
|
self.model_wrap_cfg.mask = p.mask if hasattr(p, 'mask') else None
|
|
|
|
self.model_wrap_cfg.nmask = p.nmask if hasattr(p, 'nmask') else None
|
2022-09-15 23:51:21 -06:00
|
|
|
self.model_wrap.step = 0
|
2022-09-18 14:43:37 -06:00
|
|
|
self.sampler_noise_index = 0
|
2022-09-28 09:09:06 -06:00
|
|
|
self.eta = p.eta or opts.eta_ancestral
|
2022-09-03 03:08:45 -06:00
|
|
|
|
|
|
|
if hasattr(k_diffusion.sampling, 'trange'):
|
2022-09-19 07:42:56 -06:00
|
|
|
k_diffusion.sampling.trange = lambda *args, **kwargs: extended_trange(self, *args, **kwargs)
|
2022-09-03 03:08:45 -06:00
|
|
|
|
2022-09-16 00:47:03 -06:00
|
|
|
if self.sampler_noises is not None:
|
|
|
|
k_diffusion.sampling.torch = TorchHijack(self)
|
|
|
|
|
2022-09-26 02:56:47 -06:00
|
|
|
extra_params_kwargs = {}
|
2022-09-28 01:49:07 -06:00
|
|
|
for param_name in self.extra_params:
|
|
|
|
if hasattr(p, param_name) and param_name in inspect.signature(self.func).parameters:
|
|
|
|
extra_params_kwargs[param_name] = getattr(p, param_name)
|
2022-09-26 02:56:47 -06:00
|
|
|
|
2022-09-28 09:09:06 -06:00
|
|
|
if 'eta' in inspect.signature(self.func).parameters:
|
|
|
|
extra_params_kwargs['eta'] = self.eta
|
|
|
|
|
|
|
|
return extra_params_kwargs
|
|
|
|
|
|
|
|
def sample_img2img(self, p, x, noise, conditioning, unconditional_conditioning, steps=None):
|
|
|
|
steps, t_enc = setup_img2img_steps(p, steps)
|
|
|
|
|
|
|
|
sigmas = self.model_wrap.get_sigmas(steps)
|
|
|
|
|
|
|
|
noise = noise * sigmas[steps - t_enc - 1]
|
|
|
|
xi = x + noise
|
|
|
|
|
|
|
|
extra_params_kwargs = self.initialize(p)
|
|
|
|
|
|
|
|
sigma_sched = sigmas[steps - t_enc - 1:]
|
|
|
|
|
|
|
|
self.model_wrap_cfg.init_latent = x
|
|
|
|
|
2022-09-26 02:56:47 -06:00
|
|
|
return self.func(self.model_wrap_cfg, xi, sigma_sched, extra_args={'cond': conditioning, 'uncond': unconditional_conditioning, 'cond_scale': p.cfg_scale}, disable=False, callback=self.callback_state, **extra_params_kwargs)
|
2022-09-03 03:08:45 -06:00
|
|
|
|
2022-09-19 07:42:56 -06:00
|
|
|
def sample(self, p, x, conditioning, unconditional_conditioning, steps=None):
|
|
|
|
steps = steps or p.steps
|
|
|
|
|
|
|
|
sigmas = self.model_wrap.get_sigmas(steps)
|
2022-09-03 03:08:45 -06:00
|
|
|
x = x * sigmas[0]
|
|
|
|
|
2022-09-28 09:09:06 -06:00
|
|
|
extra_params_kwargs = self.initialize(p)
|
2022-09-29 01:15:38 -06:00
|
|
|
if 'sigma_min' in inspect.signature(self.func).parameters:
|
2022-09-29 04:30:33 -06:00
|
|
|
extra_params_kwargs['sigma_min'] = self.model_wrap.sigmas[0].item()
|
|
|
|
extra_params_kwargs['sigma_max'] = self.model_wrap.sigmas[-1].item()
|
2022-09-29 01:15:38 -06:00
|
|
|
if 'n' in inspect.signature(self.func).parameters:
|
2022-09-29 04:30:33 -06:00
|
|
|
extra_params_kwargs['n'] = steps
|
|
|
|
else:
|
|
|
|
extra_params_kwargs['sigmas'] = sigmas
|
|
|
|
samples = self.func(self.model_wrap_cfg, x, extra_args={'cond': conditioning, 'uncond': unconditional_conditioning, 'cond_scale': p.cfg_scale}, disable=False, callback=self.callback_state, **extra_params_kwargs)
|
2022-09-19 07:42:56 -06:00
|
|
|
return samples
|
2022-09-03 03:08:45 -06:00
|
|
|
|