From 4b88e24ebe776680b327e33fe96d7fcf38e2e5d2 Mon Sep 17 00:00:00 2001 From: Kohaku-Blueleaf <59680068+KohakuBlueleaf@users.noreply.github.com> Date: Wed, 24 May 2023 20:35:58 +0800 Subject: [PATCH] improvements See: https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/10649#issuecomment-1561047723 --- modules/generation_parameters_copypaste.py | 20 ++++++++++++---- modules/sd_samplers_kdiffusion.py | 27 ++++++++++++++-------- modules/shared.py | 4 ++-- scripts/xyz_grid.py | 8 +++---- 4 files changed, 39 insertions(+), 20 deletions(-) diff --git a/modules/generation_parameters_copypaste.py b/modules/generation_parameters_copypaste.py index e98866fce..4f827a6f0 100644 --- a/modules/generation_parameters_copypaste.py +++ b/modules/generation_parameters_copypaste.py @@ -306,6 +306,18 @@ Steps: 20, Sampler: Euler a, CFG scale: 7, Seed: 965400086, Size: 512x512, Model if "RNG" not in res: res["RNG"] = "GPU" + if "KDiff Sched Type" not in res: + res["KDiff Sched Type"] = "Automatic" + + if "KDiff Sched max sigma" not in res: + res["KDiff Sched max sigma"] = 14.6 + + if "KDiff Sched min sigma" not in res: + res["KDiff Sched min sigma"] = 0.3 + + if "KDiff Sched rho" not in res: + res["KDiff Sched rho"] = 7.0 + return res @@ -318,10 +330,10 @@ infotext_to_setting_name_mapping = [ ('Conditional mask weight', 'inpainting_mask_weight'), ('Model hash', 'sd_model_checkpoint'), ('ENSD', 'eta_noise_seed_delta'), - ('KDiffusion Scheduler Type', 'k_sched_type'), - ('KDiffusion Scheduler sigma_max', 'sigma_max'), - ('KDiffusion Scheduler sigma_min', 'sigma_min'), - ('KDiffusion Scheduler rho', 'rho'), + ('KDiff Sched Type', 'k_sched_type'), + ('KDiff Sched max sigma', 'sigma_max'), + ('KDiff Sched min sigma', 'sigma_min'), + ('KDiff Sched rho', 'rho'), ('Noise multiplier', 'initial_noise_multiplier'), ('Eta', 'eta_ancestral'), ('Eta DDIM', 'eta_ddim'), diff --git a/modules/sd_samplers_kdiffusion.py b/modules/sd_samplers_kdiffusion.py index a4c797c6d..d2d172e4c 100644 --- a/modules/sd_samplers_kdiffusion.py +++ b/modules/sd_samplers_kdiffusion.py @@ -296,12 +296,6 @@ class KDiffusionSampler: k_diffusion.sampling.torch = TorchHijack(self.sampler_noises if self.sampler_noises is not None else []) - if opts.k_sched_type != "Automatic": - p.extra_generation_params["KDiffusion Scheduler Type"] = opts.k_sched_type - p.extra_generation_params["KDiffusion Scheduler sigma_max"] = opts.sigma_max - p.extra_generation_params["KDiffusion Scheduler sigma_min"] = opts.sigma_min - p.extra_generation_params["KDiffusion Scheduler rho"] = opts.rho - extra_params_kwargs = {} for param_name in self.extra_params: if hasattr(p, param_name) and param_name in inspect.signature(self.func).parameters: @@ -326,14 +320,27 @@ class KDiffusionSampler: if p.sampler_noise_scheduler_override: sigmas = p.sampler_noise_scheduler_override(steps) elif opts.k_sched_type != "Automatic": - 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_func = k_diffusion_scheduler[opts.k_sched_type] + 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) sigmas_kwargs = { - 'sigma_min': opts.sigma_min or sigma_min, - 'sigma_max': opts.sigma_max or sigma_max + 'sigma_min': sigma_min if opts.use_old_karras_scheduler_sigmas else m_sigma_min, + 'sigma_max': sigma_max if opts.use_old_karras_scheduler_sigmas else m_sigma_max } + + sigmas_func = k_diffusion_scheduler[opts.k_sched_type] + p.extra_generation_params["KDiff Sched Type"] = opts.k_sched_type + + if opts.sigma_min != 0.3: + # take 0.0 as model default + sigmas_kwargs['sigma_min'] = opts.sigma_min or m_sigma_min + p.extra_generation_params["KDiff Sched min sigma"] = opts.sigma_min + if opts.sigma_max != 14.6: + sigmas_kwargs['sigma_max'] = opts.sigma_max or m_sigma_max + p.extra_generation_params["KDiff Sched max sigma"] = opts.sigma_max if opts.k_sched_type != 'exponential': sigmas_kwargs['rho'] = opts.rho + p.extra_generation_params["KDiff Sched rho"] = opts.rho + sigmas = sigmas_func(n=steps, **sigmas_kwargs, device=shared.device) elif self.config is not None and self.config.options.get('scheduler', None) == 'karras': 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()) diff --git a/modules/shared.py b/modules/shared.py index da7f7cfb7..00fcced89 100644 --- a/modules/shared.py +++ b/modules/shared.py @@ -518,8 +518,8 @@ options_templates.update(options_section(('sampler-params', "Sampler parameters" 's_tmin': OptionInfo(0.0, "sigma tmin", gr.Slider, {"minimum": 0.0, "maximum": 1.0, "step": 0.01}), 's_noise': OptionInfo(1.0, "sigma noise", gr.Slider, {"minimum": 0.0, "maximum": 1.0, "step": 0.01}), 'k_sched_type': OptionInfo("Automatic", "scheduler type", gr.Dropdown, {"choices": ["Automatic", "karras", "exponential", "polyexponential"]}), - 'sigma_max': OptionInfo(0.0, "sigma max", gr.Number).info("the maximum noise strength for the scheduler. Set to 0 to use the same value which 'xxx karras' samplers use."), - 'sigma_min': OptionInfo(0.0, "sigma min", gr.Number).info("the minimum noise strength for the scheduler. Set to 0 to use the same value which 'xxx karras' samplers use."), + 'sigma_max': OptionInfo(14.6, "sigma max", gr.Number).info("the maximum noise strength for the scheduler. Set to 0 to use the same value which 'xxx karras' samplers use."), + 'sigma_min': OptionInfo(0.3, "sigma min", gr.Number).info("the minimum noise strength for the scheduler. Set to 0 to use the same value which 'xxx karras' samplers use."), 'rho': OptionInfo(7.0, "rho", gr.Number).info("higher will make a more steep noise scheduler (decrease faster). default for karras is 7.0, for polyexponential is 1.0"), 'eta_noise_seed_delta': OptionInfo(0, "Eta noise seed delta", gr.Number, {"precision": 0}).info("ENSD; does not improve anything, just produces different results for ancestral samplers - only useful for reproducing images"), 'always_discard_next_to_last_sigma': OptionInfo(False, "Always discard next-to-last sigma").link("PR", "https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/6044"), diff --git a/scripts/xyz_grid.py b/scripts/xyz_grid.py index a4126e789..41fc21070 100644 --- a/scripts/xyz_grid.py +++ b/scripts/xyz_grid.py @@ -220,10 +220,10 @@ axis_options = [ AxisOption("Sigma min", float, apply_field("s_tmin")), AxisOption("Sigma max", float, apply_field("s_tmax")), AxisOption("Sigma noise", float, apply_field("s_noise")), - AxisOption("KDiffusion Scheduler Type", str, apply_override("k_sched_type"), choices=lambda: list(sd_samplers_kdiffusion.k_diffusion_scheduler)), - AxisOption("KDiffusion Scheduler Sigma Min", float, apply_override("sigma_min")), - AxisOption("KDiffusion Scheduler Sigma Max", float, apply_override("sigma_max")), - AxisOption("KDiffusion Scheduler rho", float, apply_override("rho")), + AxisOption("KDiff Sched Type", str, apply_override("k_sched_type"), choices=lambda: list(sd_samplers_kdiffusion.k_diffusion_scheduler)), + AxisOption("KDiff Sched min sigma", float, apply_override("sigma_min")), + AxisOption("KDiff Sched max sigma", float, apply_override("sigma_max")), + AxisOption("KDiff Sched rho", float, apply_override("rho")), AxisOption("Eta", float, apply_field("eta")), AxisOption("Clip skip", int, apply_clip_skip), AxisOption("Denoising", float, apply_field("denoising_strength")),