Skip to content

Commit 5a10bb9

Browse files
Merge pull request #16235 from v0xie/beta-sampling
Feature: Beta scheduler
2 parents fa0ba93 + 8749540 commit 5a10bb9

File tree

4 files changed

+21
-0
lines changed

4 files changed

+21
-0
lines changed

modules/sd_samplers_kdiffusion.py

+4
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ def get_sigmas(self, p, steps):
120120
if scheduler.need_inner_model:
121121
sigmas_kwargs['inner_model'] = self.model_wrap
122122

123+
if scheduler.label == 'Beta':
124+
p.extra_generation_params["Beta schedule alpha"] = opts.beta_dist_alpha
125+
p.extra_generation_params["Beta schedule beta"] = opts.beta_dist_beta
126+
123127
sigmas = scheduler.function(n=steps, **sigmas_kwargs, device=devices.cpu)
124128

125129
if discard_next_to_last_sigma:

modules/sd_schedulers.py

+13
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import torch
33
import k_diffusion
44
import numpy as np
5+
from scipy import stats
56

67
from modules import shared
78

@@ -115,6 +116,17 @@ def ddim_scheduler(n, sigma_min, sigma_max, inner_model, device):
115116
return torch.FloatTensor(sigs).to(device)
116117

117118

119+
def beta_scheduler(n, sigma_min, sigma_max, inner_model, device):
120+
# From "Beta Sampling is All You Need" [arXiv:2407.12173] (Lee et. al, 2024) """
121+
alpha = shared.opts.beta_dist_alpha
122+
beta = shared.opts.beta_dist_beta
123+
timesteps = 1 - np.linspace(0, 1, n)
124+
timesteps = [stats.beta.ppf(x, alpha, beta) for x in timesteps]
125+
sigmas = [sigma_min + (x * (sigma_max-sigma_min)) for x in timesteps]
126+
sigmas += [0.0]
127+
return torch.FloatTensor(sigmas).to(device)
128+
129+
118130
schedulers = [
119131
Scheduler('automatic', 'Automatic', None),
120132
Scheduler('uniform', 'Uniform', uniform, need_inner_model=True),
@@ -127,6 +139,7 @@ def ddim_scheduler(n, sigma_min, sigma_max, inner_model, device):
127139
Scheduler('simple', 'Simple', simple_scheduler, need_inner_model=True),
128140
Scheduler('normal', 'Normal', normal_scheduler, need_inner_model=True),
129141
Scheduler('ddim', 'DDIM', ddim_scheduler, need_inner_model=True),
142+
Scheduler('beta', 'Beta', beta_scheduler, need_inner_model=True),
130143
]
131144

132145
schedulers_map = {**{x.name: x for x in schedulers}, **{x.label: x for x in schedulers}}

modules/shared_options.py

+2
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,8 @@
405405
'uni_pc_lower_order_final': OptionInfo(True, "UniPC lower order final", infotext='UniPC lower order final'),
406406
'sd_noise_schedule': OptionInfo("Default", "Noise schedule for sampling", gr.Radio, {"choices": ["Default", "Zero Terminal SNR"]}, infotext="Noise Schedule").info("for use with zero terminal SNR trained models"),
407407
'skip_early_cond': OptionInfo(0.0, "Ignore negative prompt during early sampling", gr.Slider, {"minimum": 0.0, "maximum": 1.0, "step": 0.01}, infotext="Skip Early CFG").info("disables CFG on a proportion of steps at the beginning of generation; 0=skip none; 1=skip all; can both improve sample diversity/quality and speed up sampling"),
408+
'beta_dist_alpha': OptionInfo(0.6, "Beta scheduler - alpha", gr.Slider, {"minimum": 0.01, "maximum": 1.0, "step": 0.01}, infotext='Beta scheduler alpha').info('Default = 0.6; the alpha parameter of the beta distribution used in Beta sampling'),
409+
'beta_dist_beta': OptionInfo(0.6, "Beta scheduler - beta", gr.Slider, {"minimum": 0.01, "maximum": 1.0, "step": 0.01}, infotext='Beta scheduler beta').info('Default = 0.6; the beta parameter of the beta distribution used in Beta sampling'),
408410
}))
409411

410412
options_templates.update(options_section(('postprocessing', "Postprocessing", "postprocessing"), {

scripts/xyz_grid.py

+2
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,8 @@ def __init__(self, *args, **kwargs):
259259
AxisOption("Schedule min sigma", float, apply_override("sigma_min")),
260260
AxisOption("Schedule max sigma", float, apply_override("sigma_max")),
261261
AxisOption("Schedule rho", float, apply_override("rho")),
262+
AxisOption("Beta schedule alpha", float, apply_override("beta_dist_alpha")),
263+
AxisOption("Beta schedule beta", float, apply_override("beta_dist_beta")),
262264
AxisOption("Eta", float, apply_field("eta")),
263265
AxisOption("Clip skip", int, apply_override('CLIP_stop_at_last_layers')),
264266
AxisOption("Denoising", float, apply_field("denoising_strength")),

0 commit comments

Comments
 (0)