2022-09-03 03:08:45 -06:00
|
|
|
import torch
|
2022-09-28 01:49:07 -06:00
|
|
|
import inspect
|
2022-09-03 03:08:45 -06:00
|
|
|
import k_diffusion.sampling
|
2024-06-09 12:23:53 -06:00
|
|
|
from modules import sd_samplers_common, sd_samplers_extra, sd_samplers_cfg_denoiser, sd_schedulers, devices
|
2023-08-10 08:21:01 -06:00
|
|
|
from modules.sd_samplers_cfg_denoiser import CFGDenoiser # noqa: F401
|
2023-08-16 19:45:19 -06:00
|
|
|
from modules.script_callbacks import ExtraNoiseParams, extra_noise_callback
|
2022-09-03 03:08:45 -06:00
|
|
|
|
2023-08-08 10:20:11 -06:00
|
|
|
from modules.shared import opts
|
2022-09-03 03:08:45 -06:00
|
|
|
import modules.shared as shared
|
|
|
|
|
2022-09-03 08:21:15 -06:00
|
|
|
samplers_k_diffusion = [
|
2024-03-20 00:17:11 -06:00
|
|
|
('DPM++ 2M', 'sample_dpmpp_2m', ['k_dpmpp_2m'], {'scheduler': 'karras'}),
|
|
|
|
('DPM++ SDE', 'sample_dpmpp_sde', ['k_dpmpp_sde'], {'scheduler': 'karras', "second_order": True, "brownian_noise": True}),
|
|
|
|
('DPM++ 2M SDE', 'sample_dpmpp_2m_sde', ['k_dpmpp_2m_sde'], {'scheduler': 'exponential', "brownian_noise": True}),
|
|
|
|
('DPM++ 2M SDE Heun', 'sample_dpmpp_2m_sde', ['k_dpmpp_2m_sde_heun'], {'scheduler': 'exponential', "brownian_noise": True, "solver_type": "heun"}),
|
|
|
|
('DPM++ 2S a', 'sample_dpmpp_2s_ancestral', ['k_dpmpp_2s_a'], {'scheduler': 'karras', "uses_ensd": True, "second_order": True}),
|
|
|
|
('DPM++ 3M SDE', 'sample_dpmpp_3m_sde', ['k_dpmpp_3m_sde'], {'scheduler': 'exponential', 'discard_next_to_last_sigma': True, "brownian_noise": True}),
|
2023-05-16 02:54:02 -06:00
|
|
|
('Euler a', 'sample_euler_ancestral', ['k_euler_a', 'k_euler_ancestral'], {"uses_ensd": True}),
|
2022-10-06 05:12:52 -06:00
|
|
|
('Euler', 'sample_euler', ['k_euler'], {}),
|
|
|
|
('LMS', 'sample_lms', ['k_lms'], {}),
|
2023-05-16 03:36:15 -06:00
|
|
|
('Heun', 'sample_heun', ['k_heun'], {"second_order": True}),
|
2024-03-20 00:17:11 -06:00
|
|
|
('DPM2', 'sample_dpm_2', ['k_dpm_2'], {'scheduler': 'karras', 'discard_next_to_last_sigma': True, "second_order": True}),
|
|
|
|
('DPM2 a', 'sample_dpm_2_ancestral', ['k_dpm_2_a'], {'scheduler': 'karras', 'discard_next_to_last_sigma': True, "uses_ensd": True, "second_order": True}),
|
2023-05-16 02:54:02 -06:00
|
|
|
('DPM fast', 'sample_dpm_fast', ['k_dpm_fast'], {"uses_ensd": True}),
|
|
|
|
('DPM adaptive', 'sample_dpm_adaptive', ['k_dpm_ad'], {"uses_ensd": True}),
|
2023-08-14 03:07:38 -06:00
|
|
|
('Restart', sd_samplers_extra.restart_sampler, ['restart'], {'scheduler': 'karras', "second_order": True}),
|
2022-09-03 08:21:15 -06:00
|
|
|
]
|
|
|
|
|
2023-07-17 22:32:01 -06:00
|
|
|
|
2022-09-03 08:21:15 -06:00
|
|
|
samplers_data_k_diffusion = [
|
2023-01-29 23:51:06 -07:00
|
|
|
sd_samplers_common.SamplerData(label, lambda model, funcname=funcname: KDiffusionSampler(funcname, model), aliases, options)
|
2022-10-06 05:12:52 -06:00
|
|
|
for label, funcname, aliases, options in samplers_k_diffusion
|
2023-07-28 23:11:59 -06:00
|
|
|
if callable(funcname) or hasattr(k_diffusion.sampling, funcname)
|
2022-09-03 08:21:15 -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'],
|
2023-08-13 06:22:24 -06:00
|
|
|
'sample_dpm_fast': ['s_noise'],
|
|
|
|
'sample_dpm_2_ancestral': ['s_noise'],
|
|
|
|
'sample_dpmpp_2s_ancestral': ['s_noise'],
|
|
|
|
'sample_dpmpp_sde': ['s_noise'],
|
|
|
|
'sample_dpmpp_2m_sde': ['s_noise'],
|
|
|
|
'sample_dpmpp_3m_sde': ['s_noise'],
|
2022-09-26 02:56:47 -06:00
|
|
|
}
|
2022-09-03 03:08:45 -06:00
|
|
|
|
2023-05-22 09:26:28 -06:00
|
|
|
k_diffusion_samplers_map = {x.name: x for x in samplers_data_k_diffusion}
|
2024-03-20 00:17:11 -06:00
|
|
|
k_diffusion_scheduler = {x.name: x.function for x in sd_schedulers.schedulers}
|
2023-05-22 09:02:05 -06:00
|
|
|
|
2022-10-22 11:48:13 -06:00
|
|
|
|
2023-08-08 13:09:40 -06:00
|
|
|
class CFGDenoiserKDiffusion(sd_samplers_cfg_denoiser.CFGDenoiser):
|
|
|
|
@property
|
|
|
|
def inner_model(self):
|
|
|
|
if self.model_wrap is None:
|
2024-06-15 23:04:31 -06:00
|
|
|
denoiser_constructor = getattr(shared.sd_model, 'create_denoiser', None)
|
|
|
|
|
|
|
|
if denoiser_constructor is not None:
|
|
|
|
self.model_wrap = denoiser_constructor()
|
|
|
|
else:
|
|
|
|
denoiser = k_diffusion.external.CompVisVDenoiser if shared.sd_model.parameterization == "v" else k_diffusion.external.CompVisDenoiser
|
|
|
|
self.model_wrap = denoiser(shared.sd_model, quantize=shared.opts.enable_quantization)
|
2023-08-08 13:09:40 -06:00
|
|
|
|
|
|
|
return self.model_wrap
|
|
|
|
|
|
|
|
|
2023-08-08 10:20:11 -06:00
|
|
|
class KDiffusionSampler(sd_samplers_common.Sampler):
|
2023-08-12 03:39:59 -06:00
|
|
|
def __init__(self, funcname, sd_model, options=None):
|
2023-08-08 10:20:11 -06:00
|
|
|
super().__init__(funcname)
|
2022-09-13 12:49:58 -06:00
|
|
|
|
2023-08-13 07:08:34 -06:00
|
|
|
self.extra_params = sampler_extra_params.get(funcname, [])
|
|
|
|
|
2023-08-12 03:39:59 -06:00
|
|
|
self.options = options or {}
|
2023-08-08 10:20:11 -06:00
|
|
|
self.func = funcname if callable(funcname) else getattr(k_diffusion.sampling, self.funcname)
|
2022-09-13 12:49:58 -06:00
|
|
|
|
2023-08-08 13:09:40 -06:00
|
|
|
self.model_wrap_cfg = CFGDenoiserKDiffusion(self)
|
|
|
|
self.model_wrap = self.model_wrap_cfg.inner_model
|
2022-09-28 09:09:06 -06:00
|
|
|
|
2022-12-23 23:03:45 -07:00
|
|
|
def get_sigmas(self, p, steps):
|
2023-01-05 00:43:21 -07:00
|
|
|
discard_next_to_last_sigma = self.config is not None and self.config.options.get('discard_next_to_last_sigma', False)
|
|
|
|
if opts.always_discard_next_to_last_sigma and not discard_next_to_last_sigma:
|
|
|
|
discard_next_to_last_sigma = True
|
|
|
|
p.extra_generation_params["Discard penultimate sigma"] = True
|
|
|
|
|
|
|
|
steps += 1 if discard_next_to_last_sigma else 0
|
2022-12-26 13:49:13 -07:00
|
|
|
|
2024-03-24 02:00:16 -06:00
|
|
|
scheduler_name = (p.hr_scheduler if p.is_hr_pass else p.scheduler) or 'Automatic'
|
2024-03-20 00:17:11 -06:00
|
|
|
if scheduler_name == 'Automatic':
|
|
|
|
scheduler_name = self.config.options.get('scheduler', None)
|
|
|
|
|
|
|
|
scheduler = sd_schedulers.schedulers_map.get(scheduler_name)
|
|
|
|
|
|
|
|
m_sigma_min, m_sigma_max = self.model_wrap.sigmas[0].item(), self.model_wrap.sigmas[-1].item()
|
|
|
|
sigma_min, sigma_max = (0.1, 10) if opts.use_old_karras_scheduler_sigmas else (m_sigma_min, m_sigma_max)
|
|
|
|
|
2022-09-29 18:46:06 -06:00
|
|
|
if p.sampler_noise_scheduler_override:
|
2022-10-06 14:27:01 -06:00
|
|
|
sigmas = p.sampler_noise_scheduler_override(steps)
|
2024-03-20 00:17:11 -06:00
|
|
|
elif scheduler is None or scheduler.function is None:
|
|
|
|
sigmas = self.model_wrap.get_sigmas(steps)
|
|
|
|
else:
|
|
|
|
sigmas_kwargs = {'sigma_min': sigma_min, 'sigma_max': sigma_max}
|
|
|
|
|
2024-03-24 02:00:16 -06:00
|
|
|
if scheduler.label != 'Automatic' and not p.is_hr_pass:
|
2024-03-20 01:27:53 -06:00
|
|
|
p.extra_generation_params["Schedule type"] = scheduler.label
|
2024-03-24 02:00:16 -06:00
|
|
|
elif scheduler.label != p.extra_generation_params.get("Schedule type"):
|
|
|
|
p.extra_generation_params["Hires schedule type"] = scheduler.label
|
2024-03-20 00:17:11 -06:00
|
|
|
|
|
|
|
if opts.sigma_min != 0 and opts.sigma_min != m_sigma_min:
|
2023-05-27 10:53:09 -06:00
|
|
|
sigmas_kwargs['sigma_min'] = opts.sigma_min
|
|
|
|
p.extra_generation_params["Schedule min sigma"] = opts.sigma_min
|
2024-03-20 00:17:11 -06:00
|
|
|
|
|
|
|
if opts.sigma_max != 0 and opts.sigma_max != m_sigma_max:
|
2023-05-27 10:53:09 -06:00
|
|
|
sigmas_kwargs['sigma_max'] = opts.sigma_max
|
|
|
|
p.extra_generation_params["Schedule max sigma"] = opts.sigma_max
|
|
|
|
|
2024-03-20 00:17:11 -06:00
|
|
|
if scheduler.default_rho != -1 and opts.rho != 0 and opts.rho != scheduler.default_rho:
|
2023-05-22 21:34:51 -06:00
|
|
|
sigmas_kwargs['rho'] = opts.rho
|
2023-05-27 10:53:09 -06:00
|
|
|
p.extra_generation_params["Schedule rho"] = opts.rho
|
2024-03-20 00:17:11 -06:00
|
|
|
|
|
|
|
if scheduler.need_inner_model:
|
2024-03-19 06:05:54 -06:00
|
|
|
sigmas_kwargs['inner_model'] = self.model_wrap
|
2023-05-24 06:35:58 -06:00
|
|
|
|
2024-06-09 12:23:53 -06:00
|
|
|
sigmas = scheduler.function(n=steps, **sigmas_kwargs, device=devices.cpu)
|
2022-09-28 09:09:06 -06:00
|
|
|
|
2023-01-05 00:43:21 -07:00
|
|
|
if discard_next_to_last_sigma:
|
2022-12-18 20:16:42 -07:00
|
|
|
sigmas = torch.cat([sigmas[:-2], sigmas[-1:]])
|
|
|
|
|
2024-06-28 23:05:35 -06:00
|
|
|
return sigmas.cpu()
|
2022-12-23 23:03:45 -07:00
|
|
|
|
|
|
|
def sample_img2img(self, p, x, noise, conditioning, unconditional_conditioning, steps=None, image_conditioning=None):
|
2023-01-29 23:51:06 -07:00
|
|
|
steps, t_enc = sd_samplers_common.setup_img2img_steps(p, steps)
|
2022-12-23 23:03:45 -07:00
|
|
|
|
|
|
|
sigmas = self.get_sigmas(p, steps)
|
2022-09-28 09:09:06 -06:00
|
|
|
sigma_sched = sigmas[steps - t_enc - 1:]
|
2023-08-08 10:20:11 -06:00
|
|
|
|
2024-06-28 00:23:41 -06:00
|
|
|
if hasattr(shared.sd_model, 'add_noise_to_latent'):
|
|
|
|
xi = shared.sd_model.add_noise_to_latent(x, noise, sigma_sched[0])
|
|
|
|
else:
|
|
|
|
xi = x + noise * sigma_sched[0]
|
2023-05-11 09:28:15 -06:00
|
|
|
|
2023-08-15 00:19:19 -06:00
|
|
|
if opts.img2img_extra_noise > 0:
|
|
|
|
p.extra_generation_params["Extra noise"] = opts.img2img_extra_noise
|
2023-08-29 12:22:04 -06:00
|
|
|
extra_noise_params = ExtraNoiseParams(noise, x, xi)
|
2023-08-16 19:45:19 -06:00
|
|
|
extra_noise_callback(extra_noise_params)
|
|
|
|
noise = extra_noise_params.noise
|
2023-08-15 00:19:19 -06:00
|
|
|
xi += noise * opts.img2img_extra_noise
|
|
|
|
|
2022-10-10 17:02:44 -06:00
|
|
|
extra_params_kwargs = self.initialize(p)
|
2023-02-10 19:12:16 -07:00
|
|
|
parameters = inspect.signature(self.func).parameters
|
|
|
|
|
|
|
|
if 'sigma_min' in parameters:
|
2022-10-10 17:36:00 -06:00
|
|
|
## last sigma is zero which isn't allowed by DPM Fast & Adaptive so taking value before last
|
2022-10-10 17:02:44 -06:00
|
|
|
extra_params_kwargs['sigma_min'] = sigma_sched[-2]
|
2023-02-10 19:12:16 -07:00
|
|
|
if 'sigma_max' in parameters:
|
2022-10-10 17:02:44 -06:00
|
|
|
extra_params_kwargs['sigma_max'] = sigma_sched[0]
|
2023-02-10 19:12:16 -07:00
|
|
|
if 'n' in parameters:
|
2022-10-10 17:02:44 -06:00
|
|
|
extra_params_kwargs['n'] = len(sigma_sched) - 1
|
2023-02-10 19:12:16 -07:00
|
|
|
if 'sigma_sched' in parameters:
|
2022-10-10 17:02:44 -06:00
|
|
|
extra_params_kwargs['sigma_sched'] = sigma_sched
|
2023-02-10 19:12:16 -07:00
|
|
|
if 'sigmas' in parameters:
|
2022-10-10 17:02:44 -06:00
|
|
|
extra_params_kwargs['sigmas'] = sigma_sched
|
2022-09-28 09:09:06 -06:00
|
|
|
|
2023-05-20 22:31:39 -06:00
|
|
|
if self.config.options.get('brownian_noise', False):
|
2023-02-15 01:57:18 -07:00
|
|
|
noise_sampler = self.create_noise_sampler(x, sigmas, p)
|
2023-02-10 19:12:16 -07:00
|
|
|
extra_params_kwargs['noise_sampler'] = noise_sampler
|
|
|
|
|
2023-08-13 21:46:36 -06:00
|
|
|
if self.config.options.get('solver_type', None) == 'heun':
|
|
|
|
extra_params_kwargs['solver_type'] = 'heun'
|
|
|
|
|
2022-09-28 09:09:06 -06:00
|
|
|
self.model_wrap_cfg.init_latent = x
|
2022-10-20 14:49:14 -06:00
|
|
|
self.last_latent = x
|
2023-08-06 08:53:33 -06:00
|
|
|
self.sampler_extra_args = {
|
2023-05-11 09:28:15 -06:00
|
|
|
'cond': conditioning,
|
|
|
|
'image_cond': image_conditioning,
|
|
|
|
'uncond': unconditional_conditioning,
|
2023-02-03 16:19:56 -07:00
|
|
|
'cond_scale': p.cfg_scale,
|
2023-03-28 16:18:28 -06:00
|
|
|
's_min_uncond': self.s_min_uncond
|
2023-02-03 16:19:56 -07:00
|
|
|
}
|
|
|
|
|
2023-08-06 08:53:33 -06:00
|
|
|
samples = self.launch_sampling(t_enc + 1, lambda: self.func(self.model_wrap_cfg, xi, extra_args=self.sampler_extra_args, disable=False, callback=self.callback_state, **extra_params_kwargs))
|
2022-10-10 17:02:44 -06:00
|
|
|
|
2024-01-27 12:30:12 -07:00
|
|
|
self.add_infotext(p)
|
2023-06-26 21:18:43 -06:00
|
|
|
|
2022-10-18 08:23:38 -06:00
|
|
|
return samples
|
2022-09-03 03:08:45 -06:00
|
|
|
|
2023-02-10 19:12:16 -07:00
|
|
|
def sample(self, p, x, conditioning, unconditional_conditioning, steps=None, image_conditioning=None):
|
2022-09-19 07:42:56 -06:00
|
|
|
steps = steps or p.steps
|
|
|
|
|
2022-12-23 23:03:45 -07:00
|
|
|
sigmas = self.get_sigmas(p, steps)
|
2022-10-06 05:12:52 -06:00
|
|
|
|
2023-08-28 23:51:13 -06:00
|
|
|
if opts.sgm_noise_multiplier:
|
|
|
|
p.extra_generation_params["SGM noise multiplier"] = True
|
|
|
|
x = x * torch.sqrt(1.0 + sigmas[0] ** 2.0)
|
|
|
|
else:
|
|
|
|
x = x * sigmas[0]
|
2022-09-03 03:08:45 -06:00
|
|
|
|
2022-09-28 09:09:06 -06:00
|
|
|
extra_params_kwargs = self.initialize(p)
|
2023-02-10 19:12:16 -07:00
|
|
|
parameters = inspect.signature(self.func).parameters
|
|
|
|
|
2023-08-08 10:20:11 -06:00
|
|
|
if 'n' in parameters:
|
|
|
|
extra_params_kwargs['n'] = steps
|
|
|
|
|
2023-02-10 19:12:16 -07:00
|
|
|
if 'sigma_min' in 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()
|
2023-08-08 10:20:11 -06:00
|
|
|
|
|
|
|
if 'sigmas' in parameters:
|
2022-09-29 04:30:33 -06:00
|
|
|
extra_params_kwargs['sigmas'] = sigmas
|
2022-10-18 08:23:38 -06:00
|
|
|
|
2023-05-20 22:31:39 -06:00
|
|
|
if self.config.options.get('brownian_noise', False):
|
2023-02-15 01:57:18 -07:00
|
|
|
noise_sampler = self.create_noise_sampler(x, sigmas, p)
|
2023-02-10 19:12:16 -07:00
|
|
|
extra_params_kwargs['noise_sampler'] = noise_sampler
|
|
|
|
|
2023-08-13 21:46:36 -06:00
|
|
|
if self.config.options.get('solver_type', None) == 'heun':
|
|
|
|
extra_params_kwargs['solver_type'] = 'heun'
|
|
|
|
|
2022-10-20 14:49:14 -06:00
|
|
|
self.last_latent = x
|
2023-08-06 08:53:33 -06:00
|
|
|
self.sampler_extra_args = {
|
2023-05-11 09:28:15 -06:00
|
|
|
'cond': conditioning,
|
|
|
|
'image_cond': image_conditioning,
|
|
|
|
'uncond': unconditional_conditioning,
|
2023-03-28 16:18:28 -06:00
|
|
|
'cond_scale': p.cfg_scale,
|
|
|
|
's_min_uncond': self.s_min_uncond
|
2023-08-06 08:53:33 -06:00
|
|
|
}
|
2023-08-13 21:46:36 -06:00
|
|
|
|
2023-08-06 08:53:33 -06:00
|
|
|
samples = self.launch_sampling(steps, lambda: self.func(self.model_wrap_cfg, x, extra_args=self.sampler_extra_args, disable=False, callback=self.callback_state, **extra_params_kwargs))
|
2022-10-18 08:23:38 -06:00
|
|
|
|
2024-01-27 12:30:12 -07:00
|
|
|
self.add_infotext(p)
|
2023-06-26 21:18:43 -06:00
|
|
|
|
2022-09-19 07:42:56 -06:00
|
|
|
return samples
|
2022-09-03 03:08:45 -06:00
|
|
|
|
2023-08-08 10:20:11 -06:00
|
|
|
|