@@ -109,33 +109,33 @@ class KDPM2DiscreteScheduler(SchedulerMixin, ConfigMixin):
109109 methods the library implements for all schedulers such as loading and saving.
110110
111111 Args:
112- num_train_timesteps (`int`, defaults to 1000):
112+ num_train_timesteps (`int`, defaults to ` 1000` ):
113113 The number of diffusion steps to train the model.
114- beta_start (`float`, defaults to 0.00085):
114+ beta_start (`float`, defaults to ` 0.00085` ):
115115 The starting `beta` value of inference.
116- beta_end (`float`, defaults to 0.012):
116+ beta_end (`float`, defaults to ` 0.012` ):
117117 The final `beta` value.
118118 beta_schedule (`str`, defaults to `"linear"`):
119119 The beta schedule, a mapping from a beta range to a sequence of betas for stepping the model. Choose from
120120 `linear` or `scaled_linear`.
121- trained_betas (`np.ndarray`, *optional*):
121+ trained_betas (`np.ndarray` or `list[float]` , *optional*):
122122 Pass an array of betas directly to the constructor to bypass `beta_start` and `beta_end`.
123- use_karras_sigmas (`bool`, *optional*, defaults to `False`):
123+ use_karras_sigmas (`bool`, defaults to `False`):
124124 Whether to use Karras sigmas for step sizes in the noise schedule during the sampling process. If `True`,
125125 the sigmas are determined according to a sequence of noise levels {σi}.
126- use_exponential_sigmas (`bool`, *optional*, defaults to `False`):
126+ use_exponential_sigmas (`bool`, defaults to `False`):
127127 Whether to use exponential sigmas for step sizes in the noise schedule during the sampling process.
128- use_beta_sigmas (`bool`, *optional*, defaults to `False`):
128+ use_beta_sigmas (`bool`, defaults to `False`):
129129 Whether to use beta sigmas for step sizes in the noise schedule during the sampling process. Refer to [Beta
130130 Sampling is All You Need](https://huggingface.co/papers/2407.12173) for more information.
131- prediction_type (`str`, defaults to `epsilon`, *optional* ):
131+ prediction_type (`str`, defaults to `" epsilon"` ):
132132 Prediction type of the scheduler function; can be `epsilon` (predicts the noise of the diffusion process),
133133 `sample` (directly predicts the noisy sample`) or `v_prediction` (see section 2.4 of [Imagen
134134 Video](https://huggingface.co/papers/2210.02303) paper).
135135 timestep_spacing (`str`, defaults to `"linspace"`):
136136 The way the timesteps should be scaled. Refer to Table 2 of the [Common Diffusion Noise Schedules and
137137 Sample Steps are Flawed](https://huggingface.co/papers/2305.08891) for more information.
138- steps_offset (`int`, defaults to 0 ):
138+ steps_offset (`int`, defaults to `0` ):
139139 An offset added to the inference steps, as required by some model families.
140140 """
141141
@@ -156,7 +156,7 @@ def __init__(
156156 prediction_type : str = "epsilon" ,
157157 timestep_spacing : str = "linspace" ,
158158 steps_offset : int = 0 ,
159- ):
159+ ) -> None :
160160 if self .config .use_beta_sigmas and not is_scipy_available ():
161161 raise ImportError ("Make sure to install scipy if you want to use beta sigmas." )
162162 if sum ([self .config .use_beta_sigmas , self .config .use_exponential_sigmas , self .config .use_karras_sigmas ]) > 1 :
@@ -187,29 +187,43 @@ def __init__(
187187 self .sigmas = self .sigmas .to ("cpu" ) # to avoid too much CPU/GPU communication
188188
189189 @property
190- def init_noise_sigma (self ):
191- # standard deviation of the initial noise distribution
190+ def init_noise_sigma (self ) -> torch .Tensor :
191+ """
192+ The standard deviation of the initial noise distribution.
193+
194+ Returns:
195+ `torch.Tensor`:
196+ The standard deviation of the initial noise distribution.
197+ """
192198 if self .config .timestep_spacing in ["linspace" , "trailing" ]:
193199 return self .sigmas .max ()
194200
195201 return (self .sigmas .max () ** 2 + 1 ) ** 0.5
196202
197203 @property
198- def step_index (self ):
204+ def step_index (self ) -> int | None :
199205 """
200- The index counter for current timestep. It will increase 1 after each scheduler step.
206+ The index counter for the current timestep. It increases by 1 after each scheduler step.
207+
208+ Returns:
209+ `int` or `None`:
210+ The current step index, or `None` if it has not been initialized.
201211 """
202212 return self ._step_index
203213
204214 @property
205- def begin_index (self ):
215+ def begin_index (self ) -> int | None :
206216 """
207217 The index for the first timestep. It should be set from pipeline with `set_begin_index` method.
218+
219+ Returns:
220+ `int` or `None`:
221+ The begin index, or `None` if it has not been set.
208222 """
209223 return self ._begin_index
210224
211225 # Copied from diffusers.schedulers.scheduling_dpmsolver_multistep.DPMSolverMultistepScheduler.set_begin_index
212- def set_begin_index (self , begin_index : int = 0 ):
226+ def set_begin_index (self , begin_index : int = 0 ) -> None :
213227 """
214228 Sets the begin index for the scheduler. This function should be run from pipeline before the inference.
215229
@@ -231,7 +245,7 @@ def scale_model_input(
231245 Args:
232246 sample (`torch.Tensor`):
233247 The input sample.
234- timestep (`int`, *optional* ):
248+ timestep (`float` or `torch.Tensor` ):
235249 The current timestep in the diffusion chain.
236250
237251 Returns:
@@ -252,9 +266,9 @@ def scale_model_input(
252266 def set_timesteps (
253267 self ,
254268 num_inference_steps : int ,
255- device : str | torch .device = None ,
256- num_train_timesteps : int = None ,
257- ):
269+ device : str | torch .device | None = None ,
270+ num_train_timesteps : int | None = None ,
271+ ) -> None :
258272 """
259273 Sets the discrete timesteps used for the diffusion chain (to be run before inference).
260274
@@ -263,6 +277,9 @@ def set_timesteps(
263277 The number of diffusion steps used when generating samples with a pre-trained model.
264278 device (`str` or `torch.device`, *optional*):
265279 The device to which the timesteps should be moved to. If `None`, the timesteps are not moved.
280+ num_train_timesteps (`int`, *optional*):
281+ The number of diffusion steps used to train the model. If `None`, `self.config.num_train_timesteps` is
282+ used.
266283 """
267284 self .num_inference_steps = num_inference_steps
268285
@@ -334,7 +351,14 @@ def set_timesteps(
334351 self .sigmas = self .sigmas .to ("cpu" ) # to avoid too much CPU/GPU communication
335352
336353 @property
337- def state_in_first_order (self ):
354+ def state_in_first_order (self ) -> bool :
355+ """
356+ Whether the scheduler is currently performing a first-order update.
357+
358+ Returns:
359+ `bool`:
360+ `True` if the scheduler is performing a first-order update, otherwise `False`.
361+ """
338362 return self .sample is None
339363
340364 # Copied from diffusers.schedulers.scheduling_euler_discrete.EulerDiscreteScheduler.index_for_timestep
@@ -385,7 +409,7 @@ def _init_step_index(self, timestep: float | torch.Tensor) -> None:
385409 self ._step_index = self ._begin_index
386410
387411 # Copied from diffusers.schedulers.scheduling_euler_discrete.EulerDiscreteScheduler._sigma_to_t
388- def _sigma_to_t (self , sigma , log_sigmas ) :
412+ def _sigma_to_t (self , sigma : np . ndarray , log_sigmas : np . ndarray ) -> np . ndarray :
389413 """
390414 Convert sigma values to corresponding timestep values through interpolation.
391415
@@ -422,7 +446,7 @@ def _sigma_to_t(self, sigma, log_sigmas):
422446 return t
423447
424448 # Copied from diffusers.schedulers.scheduling_euler_discrete.EulerDiscreteScheduler._convert_to_karras
425- def _convert_to_karras (self , in_sigmas : torch .Tensor , num_inference_steps ) -> torch .Tensor :
449+ def _convert_to_karras (self , in_sigmas : torch .Tensor , num_inference_steps : int ) -> torch .Tensor :
426450 """
427451 Construct the noise schedule as proposed in [Elucidating the Design Space of Diffusion-Based Generative
428452 Models](https://huggingface.co/papers/2206.00364).
@@ -549,24 +573,25 @@ def step(
549573 timestep : float | torch .Tensor ,
550574 sample : torch .Tensor | np .ndarray ,
551575 return_dict : bool = True ,
552- ) -> KDPM2DiscreteSchedulerOutput | tuple :
576+ ) -> KDPM2DiscreteSchedulerOutput | tuple [ torch . Tensor , torch . Tensor ] :
553577 """
554578 Predict the sample from the previous timestep by reversing the SDE. This function propagates the diffusion
555579 process from the learned model outputs (most often the predicted noise).
556580
557581 Args:
558- model_output (`torch.Tensor`):
582+ model_output (`torch.Tensor` or `np.ndarray` ):
559583 The direct output from learned diffusion model.
560- timestep (`float`):
584+ timestep (`float` or `torch.Tensor` ):
561585 The current discrete timestep in the diffusion chain.
562- sample (`torch.Tensor`):
586+ sample (`torch.Tensor` or `np.ndarray` ):
563587 A current instance of a sample created by the diffusion process.
564- return_dict (`bool`):
588+ return_dict (`bool`, defaults to `True` ):
565589 Whether or not to return a [`~schedulers.scheduling_k_dpm_2_discrete.KDPM2DiscreteSchedulerOutput`] or
566590 tuple.
567591
568592 Returns:
569- [`~schedulers.scheduling_k_dpm_2_discrete.KDPM2DiscreteSchedulerOutput`] or `tuple`:
593+ [`~schedulers.scheduling_k_dpm_2_discrete.KDPM2DiscreteSchedulerOutput`] or `tuple[torch.Tensor,
594+ torch.Tensor]`:
570595 If return_dict is `True`, [`~schedulers.scheduling_k_dpm_2_discrete.KDPM2DiscreteSchedulerOutput`] is
571596 returned, otherwise a tuple is returned where the first element is the sample tensor.
572597 """
@@ -686,5 +711,5 @@ def add_noise(
686711 noisy_samples = original_samples + noise * sigma
687712 return noisy_samples
688713
689- def __len__ (self ):
714+ def __len__ (self ) -> int :
690715 return self .config .num_train_timesteps
0 commit comments