2022-09-03 12:08:45 +03:00
import torch
2022-09-28 10:49:07 +03:00
import inspect
2022-09-03 12:08:45 +03:00
import k_diffusion . sampling
2023-08-10 17:04:59 +03:00
from modules import sd_samplers_common , sd_samplers_extra , sd_samplers_cfg_denoiser
2023-08-10 17:21:01 +03:00
from modules . sd_samplers_cfg_denoiser import CFGDenoiser # noqa: F401
2022-09-03 12:08:45 +03:00
2023-08-08 19:20:11 +03:00
from modules . shared import opts
2022-09-03 12:08:45 +03:00
import modules . shared as shared
2022-09-03 17:21:15 +03:00
samplers_k_diffusion = [
2023-08-08 21:10:12 +03:00
( ' DPM++ 2M Karras ' , ' sample_dpmpp_2m ' , [ ' k_dpmpp_2m_ka ' ] , { ' scheduler ' : ' karras ' } ) ,
( ' DPM++ SDE Karras ' , ' sample_dpmpp_sde ' , [ ' k_dpmpp_sde_ka ' ] , { ' scheduler ' : ' karras ' , " second_order " : True , " brownian_noise " : True } ) ,
( ' DPM++ 2M SDE Exponential ' , ' sample_dpmpp_2m_sde ' , [ ' k_dpmpp_2m_sde_exp ' ] , { ' scheduler ' : ' exponential ' , " brownian_noise " : True } ) ,
( ' DPM++ 2M SDE Karras ' , ' sample_dpmpp_2m_sde ' , [ ' k_dpmpp_2m_sde_ka ' ] , { ' scheduler ' : ' karras ' , " brownian_noise " : True } ) ,
2023-05-16 11:54:02 +03:00
( ' Euler a ' , ' sample_euler_ancestral ' , [ ' k_euler_a ' , ' k_euler_ancestral ' ] , { " uses_ensd " : True } ) ,
2022-10-06 14:12:52 +03:00
( ' Euler ' , ' sample_euler ' , [ ' k_euler ' ] , { } ) ,
( ' LMS ' , ' sample_lms ' , [ ' k_lms ' ] , { } ) ,
2023-05-16 12:36:15 +03:00
( ' Heun ' , ' sample_heun ' , [ ' k_heun ' ] , { " second_order " : True } ) ,
2022-12-24 09:03:45 +03:00
( ' DPM2 ' , ' sample_dpm_2 ' , [ ' k_dpm_2 ' ] , { ' discard_next_to_last_sigma ' : True } ) ,
2023-05-16 11:54:02 +03:00
( ' DPM2 a ' , ' sample_dpm_2_ancestral ' , [ ' k_dpm_2_a ' ] , { ' discard_next_to_last_sigma ' : True , " uses_ensd " : True } ) ,
2023-05-16 12:36:15 +03:00
( ' DPM++ 2S a ' , ' sample_dpmpp_2s_ancestral ' , [ ' k_dpmpp_2s_a ' ] , { " uses_ensd " : True , " second_order " : True } ) ,
2022-11-05 18:32:22 +03:00
( ' DPM++ 2M ' , ' sample_dpmpp_2m ' , [ ' k_dpmpp_2m ' ] , { } ) ,
2023-05-21 07:31:39 +03:00
( ' DPM++ SDE ' , ' sample_dpmpp_sde ' , [ ' k_dpmpp_sde ' ] , { " second_order " : True , " brownian_noise " : True } ) ,
2023-05-22 20:06:57 +03:00
( ' DPM++ 2M SDE ' , ' sample_dpmpp_2m_sde ' , [ ' k_dpmpp_2m_sde_ka ' ] , { " brownian_noise " : True } ) ,
2023-05-16 11:54:02 +03:00
( ' DPM fast ' , ' sample_dpm_fast ' , [ ' k_dpm_fast ' ] , { " uses_ensd " : True } ) ,
( ' DPM adaptive ' , ' sample_dpm_adaptive ' , [ ' k_dpm_ad ' ] , { " uses_ensd " : True } ) ,
2022-10-06 14:12:52 +03:00
( ' LMS Karras ' , ' sample_lms ' , [ ' k_lms_ka ' ] , { ' scheduler ' : ' karras ' } ) ,
2023-05-16 12:36:15 +03:00
( ' DPM2 Karras ' , ' sample_dpm_2 ' , [ ' k_dpm_2_ka ' ] , { ' scheduler ' : ' karras ' , ' discard_next_to_last_sigma ' : True , " uses_ensd " : True , " second_order " : True } ) ,
( ' DPM2 a Karras ' , ' sample_dpm_2_ancestral ' , [ ' k_dpm_2_a_ka ' ] , { ' scheduler ' : ' karras ' , ' discard_next_to_last_sigma ' : True , " uses_ensd " : True , " second_order " : True } ) ,
( ' DPM++ 2S a Karras ' , ' sample_dpmpp_2s_ancestral ' , [ ' k_dpmpp_2s_a_ka ' ] , { ' scheduler ' : ' karras ' , " uses_ensd " : True , " second_order " : True } ) ,
2023-07-29 08:11:59 +03:00
( ' Restart ' , sd_samplers_extra . restart_sampler , [ ' restart ' ] , { ' scheduler ' : ' karras ' } ) ,
2022-09-03 17:21:15 +03:00
]
2023-07-18 07:32:01 +03:00
2022-09-03 17:21:15 +03:00
samplers_data_k_diffusion = [
2023-01-30 09:51:06 +03:00
sd_samplers_common . SamplerData ( label , lambda model , funcname = funcname : KDiffusionSampler ( funcname , model ) , aliases , options )
2022-10-06 14:12:52 +03:00
for label , funcname , aliases , options in samplers_k_diffusion
2023-07-29 08:11:59 +03:00
if callable ( funcname ) or hasattr ( k_diffusion . sampling , funcname )
2022-09-03 17:21:15 +03:00
]
2022-09-26 11:56:47 +03:00
sampler_extra_params = {
2022-09-28 10:49:07 +03: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 11:56:47 +03:00
}
2022-09-03 12:08:45 +03:00
2023-05-22 18:26:28 +03:00
k_diffusion_samplers_map = { x . name : x for x in samplers_data_k_diffusion }
2023-05-22 18:02:05 +03:00
k_diffusion_scheduler = {
2023-05-23 19:18:09 +03:00
' Automatic ' : None ,
2023-05-22 18:02:05 +03:00
' karras ' : k_diffusion . sampling . get_sigmas_karras ,
' exponential ' : k_diffusion . sampling . get_sigmas_exponential ,
' polyexponential ' : k_diffusion . sampling . get_sigmas_polyexponential
}
2022-10-22 20:48:13 +03:00
2023-08-08 22:09:40 +03:00
class CFGDenoiserKDiffusion ( sd_samplers_cfg_denoiser . CFGDenoiser ) :
@property
def inner_model ( self ) :
if self . model_wrap is None :
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 )
return self . model_wrap
2023-08-08 19:20:11 +03:00
class KDiffusionSampler ( sd_samplers_common . Sampler ) :
def __init__ ( self , funcname , sd_model ) :
super ( ) . __init__ ( funcname )
2022-09-13 21:49:58 +03:00
2023-08-08 19:20:11 +03:00
self . func = funcname if callable ( funcname ) else getattr ( k_diffusion . sampling , self . funcname )
2022-09-13 21:49:58 +03:00
2023-08-08 22:09:40 +03:00
self . model_wrap_cfg = CFGDenoiserKDiffusion ( self )
self . model_wrap = self . model_wrap_cfg . inner_model
2022-09-28 18:09:06 +03:00
2022-12-24 09:03:45 +03:00
def get_sigmas ( self , p , steps ) :
2023-01-05 10:43:21 +03: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 23:49:13 +03:00
2022-09-30 03:46:06 +03:00
if p . sampler_noise_scheduler_override :
2022-10-06 23:27:01 +03:00
sigmas = p . sampler_noise_scheduler_override ( steps )
2023-05-23 19:18:09 +03:00
elif opts . k_sched_type != " Automatic " :
2023-05-24 15:35:58 +03:00
m_sigma_min , m_sigma_max = ( self . model_wrap . sigmas [ 0 ] . item ( ) , self . model_wrap . sigmas [ - 1 ] . item ( ) )
2023-05-27 19:53:09 +03:00
sigma_min , sigma_max = ( 0.1 , 10 ) if opts . use_old_karras_scheduler_sigmas else ( m_sigma_min , m_sigma_max )
2023-05-22 18:02:05 +03:00
sigmas_kwargs = {
2023-05-27 19:53:09 +03:00
' sigma_min ' : sigma_min ,
' sigma_max ' : sigma_max ,
2023-05-22 18:02:05 +03:00
}
2023-05-24 15:35:58 +03:00
sigmas_func = k_diffusion_scheduler [ opts . k_sched_type ]
2023-05-27 19:53:09 +03:00
p . extra_generation_params [ " Schedule type " ] = opts . k_sched_type
if opts . sigma_min != m_sigma_min and opts . sigma_min != 0 :
sigmas_kwargs [ ' sigma_min ' ] = opts . sigma_min
p . extra_generation_params [ " Schedule min sigma " ] = opts . sigma_min
if opts . sigma_max != m_sigma_max and opts . sigma_max != 0 :
sigmas_kwargs [ ' sigma_max ' ] = opts . sigma_max
p . extra_generation_params [ " Schedule max sigma " ] = opts . sigma_max
default_rho = 1. if opts . k_sched_type == " polyexponential " else 7.
if opts . k_sched_type != ' exponential ' and opts . rho != 0 and opts . rho != default_rho :
2023-05-23 06:34:51 +03:00
sigmas_kwargs [ ' rho ' ] = opts . rho
2023-05-27 19:53:09 +03:00
p . extra_generation_params [ " Schedule rho " ] = opts . rho
2023-05-24 15:35:58 +03:00
2023-05-22 18:02:05 +03:00
sigmas = sigmas_func ( n = steps , * * sigmas_kwargs , device = shared . device )
2022-10-06 23:27:01 +03:00
elif self . config is not None and self . config . options . get ( ' scheduler ' , None ) == ' karras ' :
2023-01-01 09:51:37 +03:00
sigma_min , sigma_max = ( 0.1 , 10 ) if opts . use_old_karras_scheduler_sigmas else ( self . model_wrap . sigmas [ 0 ] . item ( ) , self . model_wrap . sigmas [ - 1 ] . item ( ) )
sigmas = k_diffusion . sampling . get_sigmas_karras ( n = steps , sigma_min = sigma_min , sigma_max = sigma_max , device = shared . device )
2023-08-04 07:51:49 +03:00
elif self . config is not None and self . config . options . get ( ' scheduler ' , None ) == ' exponential ' :
m_sigma_min , m_sigma_max = ( self . model_wrap . sigmas [ 0 ] . item ( ) , self . model_wrap . sigmas [ - 1 ] . item ( ) )
sigmas = k_diffusion . sampling . get_sigmas_exponential ( n = steps , sigma_min = m_sigma_min , sigma_max = m_sigma_max , device = shared . device )
2022-09-30 03:46:06 +03:00
else :
2022-10-06 23:27:01 +03:00
sigmas = self . model_wrap . get_sigmas ( steps )
2022-09-28 18:09:06 +03:00
2023-01-05 10:43:21 +03:00
if discard_next_to_last_sigma :
2022-12-19 06:16:42 +03:00
sigmas = torch . cat ( [ sigmas [ : - 2 ] , sigmas [ - 1 : ] ] )
2022-12-24 09:03:45 +03:00
return sigmas
def sample_img2img ( self , p , x , noise , conditioning , unconditional_conditioning , steps = None , image_conditioning = None ) :
2023-01-30 09:51:06 +03:00
steps , t_enc = sd_samplers_common . setup_img2img_steps ( p , steps )
2022-12-24 09:03:45 +03:00
sigmas = self . get_sigmas ( p , steps )
2022-09-28 18:09:06 +03:00
sigma_sched = sigmas [ steps - t_enc - 1 : ]
2023-08-08 19:20:11 +03:00
2022-10-11 02:02:44 +03:00
xi = x + noise * sigma_sched [ 0 ]
2023-05-11 18:28:15 +03:00
2022-10-11 02:02:44 +03:00
extra_params_kwargs = self . initialize ( p )
2023-02-11 05:12:16 +03:00
parameters = inspect . signature ( self . func ) . parameters
if ' sigma_min ' in parameters :
2022-10-11 02:36:00 +03:00
## last sigma is zero which isn't allowed by DPM Fast & Adaptive so taking value before last
2022-10-11 02:02:44 +03:00
extra_params_kwargs [ ' sigma_min ' ] = sigma_sched [ - 2 ]
2023-02-11 05:12:16 +03:00
if ' sigma_max ' in parameters :
2022-10-11 02:02:44 +03:00
extra_params_kwargs [ ' sigma_max ' ] = sigma_sched [ 0 ]
2023-02-11 05:12:16 +03:00
if ' n ' in parameters :
2022-10-11 02:02:44 +03:00
extra_params_kwargs [ ' n ' ] = len ( sigma_sched ) - 1
2023-02-11 05:12:16 +03:00
if ' sigma_sched ' in parameters :
2022-10-11 02:02:44 +03:00
extra_params_kwargs [ ' sigma_sched ' ] = sigma_sched
2023-02-11 05:12:16 +03:00
if ' sigmas ' in parameters :
2022-10-11 02:02:44 +03:00
extra_params_kwargs [ ' sigmas ' ] = sigma_sched
2022-09-28 18:09:06 +03:00
2023-05-21 07:31:39 +03:00
if self . config . options . get ( ' brownian_noise ' , False ) :
2023-02-15 11:57:18 +03:00
noise_sampler = self . create_noise_sampler ( x , sigmas , p )
2023-02-11 05:12:16 +03:00
extra_params_kwargs [ ' noise_sampler ' ] = noise_sampler
2022-09-28 18:09:06 +03:00
self . model_wrap_cfg . init_latent = x
2022-10-20 23:49:14 +03:00
self . last_latent = x
2023-08-06 17:53:33 +03:00
self . sampler_extra_args = {
2023-05-11 18:28:15 +03:00
' cond ' : conditioning ,
' image_cond ' : image_conditioning ,
' uncond ' : unconditional_conditioning ,
2023-02-04 02:19:56 +03:00
' cond_scale ' : p . cfg_scale ,
2023-03-29 01:18:28 +03:00
' s_min_uncond ' : self . s_min_uncond
2023-02-04 02:19:56 +03:00
}
2023-08-06 17:53:33 +03: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-11 02:02:44 +03:00
2023-06-27 06:18:43 +03:00
if self . model_wrap_cfg . padded_cond_uncond :
p . extra_generation_params [ " Pad conds " ] = True
2022-10-18 17:23:38 +03:00
return samples
2022-09-03 12:08:45 +03:00
2023-02-11 05:12:16 +03:00
def sample ( self , p , x , conditioning , unconditional_conditioning , steps = None , image_conditioning = None ) :
2022-09-19 16:42:56 +03:00
steps = steps or p . steps
2022-12-24 09:03:45 +03:00
sigmas = self . get_sigmas ( p , steps )
2022-10-06 14:12:52 +03:00
2022-09-03 12:08:45 +03:00
x = x * sigmas [ 0 ]
2022-09-28 18:09:06 +03:00
extra_params_kwargs = self . initialize ( p )
2023-02-11 05:12:16 +03:00
parameters = inspect . signature ( self . func ) . parameters
2023-08-08 19:20:11 +03:00
if ' n ' in parameters :
extra_params_kwargs [ ' n ' ] = steps
2023-02-11 05:12:16 +03:00
if ' sigma_min ' in parameters :
2022-09-29 13:30:33 +03: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 19:20:11 +03:00
if ' sigmas ' in parameters :
2022-09-29 13:30:33 +03:00
extra_params_kwargs [ ' sigmas ' ] = sigmas
2022-10-18 17:23:38 +03:00
2023-05-21 07:31:39 +03:00
if self . config . options . get ( ' brownian_noise ' , False ) :
2023-02-15 11:57:18 +03:00
noise_sampler = self . create_noise_sampler ( x , sigmas , p )
2023-02-11 05:12:16 +03:00
extra_params_kwargs [ ' noise_sampler ' ] = noise_sampler
2022-10-20 23:49:14 +03:00
self . last_latent = x
2023-08-06 17:53:33 +03:00
self . sampler_extra_args = {
2023-05-11 18:28:15 +03:00
' cond ' : conditioning ,
' image_cond ' : image_conditioning ,
' uncond ' : unconditional_conditioning ,
2023-03-29 01:18:28 +03:00
' cond_scale ' : p . cfg_scale ,
' s_min_uncond ' : self . s_min_uncond
2023-08-06 17:53:33 +03: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 17:23:38 +03:00
2023-06-27 06:18:43 +03:00
if self . model_wrap_cfg . padded_cond_uncond :
p . extra_generation_params [ " Pad conds " ] = True
2022-09-19 16:42:56 +03:00
return samples
2022-09-03 12:08:45 +03:00
2023-08-08 19:20:11 +03:00