fluxdev/stable-diffusion-webui-forge
1
1import torch2from modules import sd_samplers_kdiffusion, sd_samplers_common3 4from ldm_patched.k_diffusion import sampling as k_diffusion_sampling5from ldm_patched.modules.samplers import calculate_sigmas_scheduler6 7 8class AlterSampler(sd_samplers_kdiffusion.KDiffusionSampler):9 def __init__(self, sd_model, sampler_name, scheduler_name):10 self.sampler_name = sampler_name11 self.scheduler_name = scheduler_name12 self.unet = sd_model.forge_objects.unet13 14 sampler_function = getattr(k_diffusion_sampling, "sample_{}".format(sampler_name))15 super().__init__(sampler_function, sd_model, None)16 17 def get_sigmas(self, p, steps):18 if self.scheduler_name == 'turbo':19 timesteps = torch.flip(torch.arange(1, steps + 1) * float(1000.0 / steps) - 1, (0,)).round().long().clip(0, 999)20 sigmas = self.unet.model.model_sampling.sigma(timesteps)21 sigmas = torch.cat([sigmas, sigmas.new_zeros([1])])22 else:23 sigmas = calculate_sigmas_scheduler(self.unet.model, self.scheduler_name, steps)24 return sigmas.to(self.unet.load_device)25 26 27def build_constructor(sampler_name, scheduler_name):28 def constructor(m):29 return AlterSampler(m, sampler_name, scheduler_name)30 31 return constructor32 33 34samplers_data_alter = [35 sd_samplers_common.SamplerData('DDPM', build_constructor(sampler_name='ddpm', scheduler_name='normal'), ['ddpm'], {}),36 sd_samplers_common.SamplerData('DDPM Karras', build_constructor(sampler_name='ddpm', scheduler_name='karras'), ['ddpm_karras'], {}),37 sd_samplers_common.SamplerData('Euler A Turbo', build_constructor(sampler_name='euler_ancestral', scheduler_name='turbo'), ['euler_ancestral_turbo'], {}),38 sd_samplers_common.SamplerData('DPM++ 2M Turbo', build_constructor(sampler_name='dpmpp_2m', scheduler_name='turbo'), ['dpmpp_2m_turbo'], {}),39 sd_samplers_common.SamplerData('DPM++ 2M SDE Turbo', build_constructor(sampler_name='dpmpp_2m_sde', scheduler_name='turbo'), ['dpmpp_2m_sde_turbo'], {}),40 sd_samplers_common.SamplerData('LCM Karras', build_constructor(sampler_name='lcm', scheduler_name='karras'), ['lcm_karras'], {}),41 sd_samplers_common.SamplerData('Euler SGMUniform', build_constructor(sampler_name='euler', scheduler_name='sgm_uniform'), ['euler_sgm_uniform'], {}),42 sd_samplers_common.SamplerData('Euler A SGMUniform', build_constructor(sampler_name='euler_ancestral', scheduler_name='sgm_uniform'), ['euler_ancestral_sgm_uniform'], {}),43 sd_samplers_common.SamplerData('DPM++ 2M SGMUniform', build_constructor(sampler_name='dpmpp_2m', scheduler_name='sgm_uniform'), ['dpmpp_2m_sgm_uniform'], {}),44 sd_samplers_common.SamplerData('DPM++ 2M SDE SGMUniform', build_constructor(sampler_name='dpmpp_2m_sde', scheduler_name='sgm_uniform'), ['dpmpp_2m_sde_sgm_uniform'], {}),45]46 