1717from typing import List , Optional , Tuple , Union
1818
1919import numpy as np
20+ import scipy .stats
2021import torch
2122
2223from ..configuration_utils import ConfigMixin , register_to_config
23- from ..utils import BaseOutput , logging
24+ from ..utils import BaseOutput , is_scipy_available , logging
2425from ..utils .torch_utils import randn_tensor
2526from .scheduling_utils import KarrasDiffusionSchedulers , SchedulerMixin
2627
@@ -160,6 +161,9 @@ class EulerDiscreteScheduler(SchedulerMixin, ConfigMixin):
160161 the sigmas are determined according to a sequence of noise levels {σi}.
161162 use_exponential_sigmas (`bool`, *optional*, defaults to `False`):
162163 Whether to use exponential sigmas for step sizes in the noise schedule during the sampling process.
164+ use_beta_sigmas (`bool`, *optional*, defaults to `False`):
165+ Whether to use beta sigmas for step sizes in the noise schedule during the sampling process. Refer to
166+ [Beta Sampling is All You Need](https://huggingface.co/papers/2407.12173) for more information.
163167 timestep_spacing (`str`, defaults to `"linspace"`):
164168 The way the timesteps should be scaled. Refer to Table 2 of the [Common Diffusion Noise Schedules and
165169 Sample Steps are Flawed](https://huggingface.co/papers/2305.08891) for more information.
@@ -189,6 +193,7 @@ def __init__(
189193 interpolation_type : str = "linear" ,
190194 use_karras_sigmas : Optional [bool ] = False ,
191195 use_exponential_sigmas : Optional [bool ] = False ,
196+ use_beta_sigmas : Optional [bool ] = False ,
192197 sigma_min : Optional [float ] = None ,
193198 sigma_max : Optional [float ] = None ,
194199 timestep_spacing : str = "linspace" ,
@@ -197,8 +202,12 @@ def __init__(
197202 rescale_betas_zero_snr : bool = False ,
198203 final_sigmas_type : str = "zero" , # can be "zero" or "sigma_min"
199204 ):
200- if sum ([self .config .use_exponential_sigmas , self .config .use_karras_sigmas ]) > 1 :
201- raise ValueError ("Only one of `config.use_exponential_sigmas`, `config.use_karras_sigmas` can be used." )
205+ if self .config .use_beta_sigmas and not is_scipy_available ():
206+ raise ImportError ("Make sure to install scipy if you want to use beta sigmas." )
207+ if sum ([self .config .use_beta_sigmas , self .config .use_exponential_sigmas , self .config .use_karras_sigmas ]) > 1 :
208+ raise ValueError (
209+ "Only one of `config.use_beta_sigmas`, `config.use_exponential_sigmas`, `config.use_karras_sigmas` can be used."
210+ )
202211 if trained_betas is not None :
203212 self .betas = torch .tensor (trained_betas , dtype = torch .float32 )
204213 elif beta_schedule == "linear" :
@@ -241,6 +250,7 @@ def __init__(
241250 self .is_scale_input_called = False
242251 self .use_karras_sigmas = use_karras_sigmas
243252 self .use_exponential_sigmas = use_exponential_sigmas
253+ self .use_beta_sigmas = use_beta_sigmas
244254
245255 self ._step_index = None
246256 self ._begin_index = None
@@ -340,6 +350,8 @@ def set_timesteps(
340350 raise ValueError ("Cannot set `timesteps` with `config.use_karras_sigmas = True`." )
341351 if timesteps is not None and self .config .use_exponential_sigmas :
342352 raise ValueError ("Cannot set `timesteps` with `config.use_exponential_sigmas = True`." )
353+ if timesteps is not None and self .config .use_beta_sigmas :
354+ raise ValueError ("Cannot set `timesteps` with `config.use_beta_sigmas = True`." )
343355 if (
344356 timesteps is not None
345357 and self .config .timestep_type == "continuous"
@@ -408,6 +420,10 @@ def set_timesteps(
408420 sigmas = self ._convert_to_exponential (in_sigmas = sigmas , num_inference_steps = self .num_inference_steps )
409421 timesteps = np .array ([self ._sigma_to_t (sigma , log_sigmas ) for sigma in sigmas ])
410422
423+ elif self .config .use_beta_sigmas :
424+ sigmas = self ._convert_to_beta (in_sigmas = sigmas , num_inference_steps = self .num_inference_steps )
425+ timesteps = np .array ([self ._sigma_to_t (sigma , log_sigmas ) for sigma in sigmas ])
426+
411427 if self .config .final_sigmas_type == "sigma_min" :
412428 sigma_last = ((1 - self .alphas_cumprod [0 ]) / self .alphas_cumprod [0 ]) ** 0.5
413429 elif self .config .final_sigmas_type == "zero" :
@@ -502,6 +518,37 @@ def _convert_to_exponential(self, in_sigmas: torch.Tensor, num_inference_steps:
502518 sigmas = torch .linspace (math .log (sigma_max ), math .log (sigma_min ), num_inference_steps ).exp ()
503519 return sigmas
504520
521+ def _convert_to_beta (
522+ self , in_sigmas : torch .Tensor , num_inference_steps : int , alpha : float = 0.6 , beta : float = 0.6
523+ ) -> torch .Tensor :
524+ # From "Beta Sampling is All You Need" [arXiv:2407.12173] (Lee et. al, 2024) """
525+
526+ # Hack to make sure that other schedulers which copy this function don't break
527+ # TODO: Add this logic to the other schedulers
528+ if hasattr (self .config , "sigma_min" ):
529+ sigma_min = self .config .sigma_min
530+ else :
531+ sigma_min = None
532+
533+ if hasattr (self .config , "sigma_max" ):
534+ sigma_max = self .config .sigma_max
535+ else :
536+ sigma_max = None
537+
538+ sigma_min = sigma_min if sigma_min is not None else in_sigmas [- 1 ].item ()
539+ sigma_max = sigma_max if sigma_max is not None else in_sigmas [0 ].item ()
540+
541+ sigmas = torch .FloatTensor (
542+ [
543+ sigma_min + (ppf * (sigma_max - sigma_min ))
544+ for ppf in [
545+ scipy .stats .beta .ppf (timestep , alpha , beta )
546+ for timestep in 1 - np .linspace (0 , 1 , num_inference_steps )
547+ ]
548+ ]
549+ )
550+ return sigmas
551+
505552 def index_for_timestep (self , timestep , schedule_timesteps = None ):
506553 if schedule_timesteps is None :
507554 schedule_timesteps = self .timesteps
0 commit comments