
    
3jec                         S SK r S SKJr  S SKJr  S SKrSSKJrJr  SSK	J
r
Jr  SSKJr  SS	KJr  \R                   " \5      r\ " S
 S\
5      5       r " S S\\5      rg)    N)	dataclass)Literal   )ConfigMixinregister_to_config)
BaseOutputlogging)randn_tensor   )SchedulerMixinc                   `    \ rS rSr% Sr\R                  \S'   Sr\R                  S-  \S'   Sr	g)EDMEulerSchedulerOutput   aM  
Output class for the scheduler's `step` function output.

Args:
    prev_sample (`torch.Tensor` of shape `(batch_size, num_channels, height, width)` for images):
        Computed sample `(x_{t-1})` of previous timestep. `prev_sample` should be used as next model input in the
        denoising loop.
    pred_original_sample (`torch.Tensor` of shape `(batch_size, num_channels, height, width)` for images):
        The predicted denoised sample `(x_{0})` based on the model output from the current timestep.
        `pred_original_sample` can be used to preview progress or for guidance.
prev_sampleNpred_original_sample )
__name__
__module____qualname____firstlineno____doc__torchTensor__annotations__r   __static_attributes__r       c/home/wildlama/miniconda3/lib/python3.13/site-packages/diffusers/schedulers/scheduling_edm_euler.pyr   r      s'    
 04%,,-4r   r   c                   R   \ rS rSrSr/ rSr\        S<S\S\S\S\	S   S	\
S
\	S   S\S\	S   SS4S jj5       r\S\4S j5       r\S\
4S j5       r\S\
4S j5       rS=S\
SS4S jjrS\R$                  S\\R$                  -  S\R$                  4S jrS\\R$                  -  S\R$                  4S jrS\R$                  S\R$                  S\\R$                  -  S\R$                  4S jrS\R$                  S\\R$                  -  S\R$                  4S jr   S>S\
S \\R0                  -  S!\R$                  \\   -  S-  4S" jjr  S?S#\R$                  S\S-  S\S-  S\R$                  4S$ jjr  S?S#\R$                  S\S-  S\S-  S\R$                  4S% jjr S@S\\R$                  -  S&\R$                  S-  S\
4S' jjrS\\R$                  -  SS4S( jrS)S)\" S*5      S+SS,S4S\R$                  S\\R$                  -  S\R$                  S-\S.\S/\S0\S1\R>                  S-  S2\ S3\R$                  S-  S\!\"-  4S4 jjr#S5\R$                  S6\R$                  S7\R$                  S\R$                  4S8 jr$S\\R$                  -  S\\R$                  -  4S9 jr%S\
4S: jr&S;r'g)AEDMEulerScheduler1   a  
Implements the Euler scheduler in EDM formulation as presented in Karras et al. 2022 [1].

[1] Karras, Tero, et al. "Elucidating the Design Space of Diffusion-Based Generative Models."
https://huggingface.co/papers/2206.00364

This model inherits from [`SchedulerMixin`] and [`ConfigMixin`]. Check the superclass documentation for the generic
methods the library implements for all schedulers such as loading and saving.

Args:
    sigma_min (`float`, *optional*, defaults to `0.002`):
        Minimum noise magnitude in the sigma schedule. This was set to 0.002 in the EDM paper [1]; a reasonable
        range is [0, 10].
    sigma_max (`float`, *optional*, defaults to `80.0`):
        Maximum noise magnitude in the sigma schedule. This was set to 80.0 in the EDM paper [1]; a reasonable
        range is [0.2, 80.0].
    sigma_data (`float`, *optional*, defaults to `0.5`):
        The standard deviation of the data distribution. This is set to 0.5 in the EDM paper [1].
    sigma_schedule (`Literal["karras", "exponential"]`, *optional*, defaults to `"karras"`):
        Sigma schedule to compute the `sigmas`. By default, we use the schedule introduced in the EDM paper
        (https://huggingface.co/papers/2206.00364). The `"exponential"` schedule was incorporated in this model:
        https://huggingface.co/stabilityai/cosxl.
    num_train_timesteps (`int`, *optional*, defaults to `1000`):
        The number of diffusion steps to train the model.
    prediction_type (`Literal["epsilon", "v_prediction"]`, *optional*, defaults to `"epsilon"`):
        Prediction type of the scheduler function. `"epsilon"` predicts the noise of the diffusion process, and
        `"v_prediction"` (see section 2.4 of [Imagen Video](https://huggingface.co/papers/2210.02303) paper).
    rho (`float`, *optional*, defaults to `7.0`):
        The rho parameter used for calculating the Karras sigma schedule, which is set to 7.0 in the EDM paper [1].
    final_sigmas_type (`Literal["zero", "sigma_min"]`, *optional*, defaults to `"zero"`):
        The final `sigma` value for the noise schedule during the sampling process. If `"sigma_min"`, the final
        sigma is the same as the last sigma in the training schedule. If `"zero"`, the final sigma is set to 0.
r   	sigma_min	sigma_max
sigma_datasigma_schedulekarrasexponentialnum_train_timestepsprediction_type)epsilonv_predictionrhofinal_sigmas_type)zeror!   returnNc	           	      r   US;  a  [        SU< S35      eS U l        [        R                  R                  R                  5       (       a  [        R                  O[        R                  n	[        R                  " US-   U	S9U-  n
US:X  a  U R                  U
5      n
OUS:X  a  U R                  U
5      n
U
R                  [        R                  5      n
U R                  U
5      U l        U R                  R                  S:X  a  U
S	   nO?U R                  R                  S
:X  a  SnO"[        SU R                  R                   35      e[        R                   " U
[        R"                  " SXR$                  S9/5      U l        SU l        S U l        S U l        U R&                  R                  S5      U l        g )Nr%   z-Wrong value for provided for `sigma_schedule=z`.`r   dtyper&   r'   r!   r.   r   C`final_sigmas_type` must be one of 'zero', or 'sigma_min', but got r   
fill_valuedeviceFcpu)
ValueErrornum_inference_stepsr   backendsmpsis_availablefloat32float64arange_compute_karras_sigmas_compute_exponential_sigmastoprecondition_noise	timestepsconfigr-   catfullr8   sigmasis_scale_input_called_step_index_begin_index)selfr!   r"   r#   r$   r(   r)   r,   r-   sigmas_dtyperJ   
sigma_lasts               r   __init__EDMEulerScheduler.__init__W   so    !::Mn=NcRSS $( (-(:(:(G(G(I(Iu}}u}}1A5\JM``X%008F},55f=F5==)008;;((K7J[[**f4JUVZVaVaVsVsUtu  iiDZXeXe)f gh%*" kknnU+r   c                 @    U R                   R                  S-  S-   S-  $ )z
Return the standard deviation of the initial noise distribution.

Returns:
    `float`:
        The initial noise sigma value computed as `(sigma_max**2 + 1) ** 0.5`.
r   r         ?)rG   r"   rN   s    r   init_noise_sigma"EDMEulerScheduler.init_noise_sigma   s#     %%q(1,44r   c                     U R                   $ )z
Return the index counter for the current timestep. The index will increase by 1 after each scheduler step.

Returns:
    `int` or `None`:
        The current step index, or `None` if not yet initialized.
)rL   rU   s    r   
step_indexEDMEulerScheduler.step_index   s     r   c                     U R                   $ )z
Return the index for the first timestep. This should be set from the pipeline with the `set_begin_index`
method.

Returns:
    `int` or `None`:
        The begin index, or `None` if not yet set.
rM   rU   s    r   begin_indexEDMEulerScheduler.begin_index   s        r   r]   c                     Xl         g)z
Sets the begin index for the scheduler. This function should be run from pipeline before the inference.

Args:
    begin_index (`int`, defaults to `0`):
        The begin index for the scheduler.
Nr\   )rN   r]   s     r   set_begin_index!EDMEulerScheduler.set_begin_index   s
     (r   samplesigmac                 0    U R                  U5      nX-  nU$ )a9  
Precondition the input sample by scaling it according to the EDM formulation.

Args:
    sample (`torch.Tensor`):
        The input sample tensor to precondition.
    sigma (`float` or `torch.Tensor`):
        The current sigma (noise level) value.

Returns:
    `torch.Tensor`:
        The scaled input sample.
)_get_conditioning_c_in)rN   rb   rc   c_inscaled_samples        r   precondition_inputs%EDMEulerScheduler.precondition_inputs   s!     **51r   c                     [        U[        R                  5      (       d  [        R                  " U/5      nS[        R                  " U5      -  nU$ )a  
Precondition the noise level by applying a logarithmic transformation.

Args:
    sigma (`float` or `torch.Tensor`):
        The sigma (noise level) value to precondition.

Returns:
    `torch.Tensor`:
        The preconditioned noise value computed as `0.25 * log(sigma)`.
g      ?)
isinstancer   r   tensorlog)rN   rc   c_noises      r   rE   $EDMEulerScheduler.precondition_noise   s=     %..LL%)E5))r   model_outputc                 h   U R                   R                  nUS-  US-  US-  -   -  nU R                   R                  S:X  a  X4-  US-  US-  -   S-  -  nOSU R                   R                  S:X  a  U* U-  US-  US-  -   S-  -  nO#[        SU R                   R                   S35      eXQ-  Xb-  -   nU$ )a  
Precondition the model outputs according to the EDM formulation.

Args:
    sample (`torch.Tensor`):
        The input sample tensor.
    model_output (`torch.Tensor`):
        The direct output from the learned diffusion model.
    sigma (`float` or `torch.Tensor`):
        The current sigma (noise level) value.

Returns:
    `torch.Tensor`:
        The denoised sample computed by combining the skip connection and output scaling.
r   r*   rT   r+   zPrediction type z is not supported.)rG   r#   r)   r:   )rN   rb   rp   rc   r#   c_skipc_outdenoiseds           r   precondition_outputs&EDMEulerScheduler.precondition_outputs   s    * [[++
Q%(Z]":;;;&&)3&%(Z]*Bs)JJE[[((N:FZ'5!8j!m+C*KKE/0K0K/LL^_``?U%99r   timestepc                     U R                   c  U R                  U5        U R                  U R                      nU R                  X5      nSU l        U$ )a  
Scale the denoising model input to match the Euler algorithm. Ensures interchangeability with schedulers that
need to scale the denoising model input depending on the current timestep.

Args:
    sample (`torch.Tensor`):
        The input sample tensor.
    timestep (`float` or `torch.Tensor`):
        The current timestep in the diffusion chain.

Returns:
    `torch.Tensor`:
        A scaled input sample.
T)rY   _init_step_indexrJ   rh   rK   )rN   rb   rw   rc   s       r   scale_model_input#EDMEulerScheduler.scale_model_input   sK     ??"!!(+DOO,))&8%)"r   r;   r8   rJ   c           	         Xl         [        R                  R                  R	                  5       (       a  [        R
                  O[        R                  nUc"  [        R                  " SSU R                   US9nO;[        U[        5      (       a  [        R                  " X4S9nOUR                  U5      nU R                  R                  S:X  a  U R                  U5      nO+U R                  R                  S:X  a  U R                  U5      nUR                  [        R
                  US9nU R!                  U5      U l        U R                  R$                  S:X  a  US	   nO?U R                  R$                  S
:X  a  SnO"['        SU R                  R$                   35      e[        R(                  " U[        R*                  " SXSR,                  S9/5      U l        SU l        SU l        U R.                  R                  S5      U l        g)aP  
Sets the discrete timesteps used for the diffusion chain (to be run before inference).

Args:
    num_inference_steps (`int`, *optional*):
        The number of diffusion steps used when generating samples with a pre-trained model.
    device (`str` or `torch.device`, *optional*):
        The device to which the timesteps should be moved to. If `None`, the timesteps are not moved.
    sigmas (`torch.Tensor | list[float]`, *optional*):
        Custom sigmas to use for the denoising process. If not defined, the default behavior when
        `num_inference_steps` is passed will be used.
Nr   r   r1   r&   r'   )r2   r8   r!   r3   r.   r4   r5   r6   r9   )r;   r   r<   r=   r>   r?   r@   linspacerk   floatrl   rD   rG   r$   rB   rC   rE   rF   r-   r:   rH   rI   r8   rJ   rL   rM   )rN   r;   r8   rJ   rO   rP   s         r   set_timestepsEDMEulerScheduler.set_timesteps  s   $ $7 (-(:(:(G(G(I(Iu}}u}}>^^Aq$*B*B,WF&&\\&=FYY|,F;;%%1008F[[''=855f=Fv>008;;((K7J[[**f4JUVZVaVaVsVsUtu  iiDZXeXe)f gh kknnU+r   rampc                     U=(       d    U R                   R                  nU=(       d    U R                   R                  nU R                   R                  nUSU-  -  nUSU-  -  nXaXV-
  -  -   U-  nU$ )a  
Construct the noise schedule of [Karras et al. (2022)](https://huggingface.co/papers/2206.00364).

Args:
    ramp (`torch.Tensor`):
        A tensor of values in [0, 1] representing the interpolation positions.
    sigma_min (`float`, *optional*):
        Minimum sigma value. If `None`, uses `self.config.sigma_min`.
    sigma_max (`float`, *optional*):
        Maximum sigma value. If `None`, uses `self.config.sigma_max`.

Returns:
    `torch.Tensor`:
        The computed Karras sigma schedule.
r   )rG   r!   r"   r,   )rN   r   r!   r"   r,   min_inv_rhomax_inv_rhorJ   s           r   rB   (EDMEulerScheduler._compute_karras_sigmasC  so    * 6!6!6	6!6!6	kkooAG,AG,(A BBsJr   c                 N   U=(       d    U R                   R                  nU=(       d    U R                   R                  n[        R                  " [
        R                  " U5      [
        R                  " U5      [        U5      5      R                  5       R                  S5      nU$ )aT  
Compute the exponential sigma schedule. Implementation closely follows k-diffusion:
https://github.com/crowsonkb/k-diffusion/blob/6ab5146d4a5ef63901326489f31f1d8e7dd36b48/k_diffusion/sampling.py#L26

Args:
    ramp (`torch.Tensor`):
        A tensor of values representing the interpolation positions.
    sigma_min (`float`, *optional*):
        Minimum sigma value. If `None`, uses `self.config.sigma_min`.
    sigma_max (`float`, *optional*):
        Maximum sigma value. If `None`, uses `self.config.sigma_max`.

Returns:
    `torch.Tensor`:
        The computed exponential sigma schedule.
r   )
rG   r!   r"   r   r}   mathrm   lenexpflip)rN   r   r!   r"   rJ   s        r   rC   -EDMEulerScheduler._compute_exponential_sigmasa  sn    , 6!6!6	6!6!6	 3TXXi5H#d)TXXZ__`abr   schedule_timestepsc                     Uc  U R                   nX!:H  R                  5       n[        U5      S:  a  SOSnX4   R                  5       $ )a  
Find the index of a given timestep in the timestep schedule.

Args:
    timestep (`float` or `torch.Tensor`):
        The timestep value to find in the schedule.
    schedule_timesteps (`torch.Tensor`, *optional*):
        The timestep schedule to search in. If `None`, uses `self.timesteps`.

Returns:
    `int`:
        The index of the timestep in the schedule. For the very first step, returns the second index if
        multiple matches exist to avoid skipping a sigma when starting mid-schedule (e.g., for image-to-image).
r   r   )rF   nonzeror   item)rN   rw   r   indicesposs        r   index_for_timestep$EDMEulerScheduler.index_for_timestep}  sJ    " %!%%1::< w<!#a|  ""r   c                     U R                   c[  [        U[        R                  5      (       a%  UR	                  U R
                  R                  5      nU R                  U5      U l        gU R                  U l        g)z
Initialize the step index for the scheduler based on the given timestep.

Args:
    timestep (`float` or `torch.Tensor`):
        The current timestep to initialize the step index from.
N)
r]   rk   r   r   rD   rF   r8   r   rL   rM   )rN   rw   s     r   ry   "EDMEulerScheduler._init_step_index  sZ     #(ELL11#;;t~~'<'<=#66x@D#00Dr           infg      ?Ts_churns_tmins_tmaxs_noise	generatorreturn_dictr   c                    [        U[        [        R                  [        R                  45      (       a  [        S5      eU R                  (       d  [        R                  S5        U R                  c  U R                  U5        UR                  [        R                  5      nU R                  U R                     nX[s=::  a  U::  a(  O  O%[        U[        U R                  5      S-
  -  S5      OSnXS-   -  nUS:  aA  [!        UR"                  UR$                  UR&                  US9nX-  nX?US-  US-  -
  S	-  -  -   nU
c  U R)                  X1U5      n
X:-
  U-  nU R                  U R                  S-      U-
  nUUU-  -   nUR                  UR$                  5      nU =R*                  S-  sl        U	(       d  UU
4$ [-        UU
S
9$ )a  
Predict the sample from the previous timestep by reversing the SDE. This function propagates the diffusion
process from the learned model outputs (most often the predicted noise).

Args:
    model_output (`torch.Tensor`):
        The direct output from the learned diffusion model.
    timestep (`float` or `torch.Tensor`):
        The current discrete timestep in the diffusion chain.
    sample (`torch.Tensor`):
        A current instance of a sample created by the diffusion process.
    s_churn (`float`, *optional*, defaults to `0.0`):
        The amount of stochasticity to add at each step. Higher values add more noise.
    s_tmin (`float`, *optional*, defaults to `0.0`):
        The minimum sigma threshold below which no noise is added.
    s_tmax (`float`, *optional*, defaults to `float("inf")`):
        The maximum sigma threshold above which no noise is added.
    s_noise (`float`, *optional*, defaults to `1.0`):
        Scaling factor for noise added to the sample.
    generator (`torch.Generator`, *optional*):
        A random number generator for reproducibility.
    return_dict (`bool`, *optional*, defaults to `True`):
        Whether or not to return an [`~schedulers.scheduling_edm_euler.EDMEulerSchedulerOutput`] or tuple.
    pred_original_sample (`torch.Tensor`, *optional*):
        The predicted denoised sample from a previous step. If provided, skips recomputation.

Returns:
    [`~schedulers.scheduling_edm_euler.EDMEulerSchedulerOutput`] or `tuple`:
        If `return_dict` is `True`, an [`~schedulers.scheduling_edm_euler.EDMEulerSchedulerOutput`] is
        returned, otherwise a tuple is returned where the first element is the previous sample tensor and the
        second element is the predicted original sample tensor.
zPassing integer indices (e.g. from `enumerate(timesteps)`) as timesteps to `EDMEulerScheduler.step()` is not supported. Make sure to pass one of the `scheduler.timesteps` as a timestep.zThe `scale_model_input` function should be called before `step` to ensure correct denoising. See `StableDiffusionPipeline` for a usage example.r   g4y?r   r   )r2   r8   r   r   rT   )r   r   )rk   intr   	IntTensor
LongTensorr:   rK   loggerwarningrY   ry   rD   r?   rJ   minr   r
   shaper2   r8   ru   rL   r   )rN   rp   rw   rb   r   r   r   r   r   r   r   rc   gamma	sigma_hatnoiseeps
derivativedtr   s                      r   stepEDMEulerScheduler.step  s   \ heoou7G7G HIIG  ))NNE
 ??"!!(+ 5==)DOO,EKE^X^E^Gs4;;/!34jAdgQY'	19 """((#**#	E /CY\E1H%<$DDDF  '#'#<#<VS\#]  3y@
[[1,-	9zB. "nn\%7%78 	A$ 
 ';Uijjr   original_samplesr   rF   c                     U R                   R                  UR                  UR                  S9nUR                  R                  S:X  av  [
        R                  " U5      (       a[  U R                  R                  UR                  [
        R                  S9nUR                  UR                  [
        R                  S9nO@U R                  R                  UR                  5      nUR                  UR                  5      nU R                  c!  U Vs/ s H  o`R                  Xe5      PM     nnOHU R                  b  U R                  /UR                  S   -  nOU R                  /UR                  S   -  nXG   R                  5       n[        UR                  5      [        UR                  5      :  a?  UR                  S5      n[        UR                  5      [        UR                  5      :  a  M?  XU-  -   n	U	$ s  snf )a  
Add noise to the original samples according to the noise schedule at the specified timesteps.

Args:
    original_samples (`torch.Tensor`):
        The original samples to which noise will be added.
    noise (`torch.Tensor`):
        The noise tensor to add to the original samples.
    timesteps (`torch.Tensor`):
        The timesteps at which to add noise, determining the noise level from the schedule.

Returns:
    `torch.Tensor`:
        The noisy samples with added noise scaled according to the timestep schedule.
)r8   r2   r=   r1   r   r3   )rJ   rD   r8   r2   typer   is_floating_pointrF   r?   r]   r   rY   r   flattenr   	unsqueeze)
rN   r   r   rF   rJ   r   tstep_indicesrc   noisy_sampless
             r   	add_noiseEDMEulerScheduler.add_noise  s   , '7'>'>FVF\F\]""''50U5L5LY5W5W!%!2!23C3J3JRWR_R_!2!`!%5%<%<EMMRI!%!2!23C3J3J!K!%5%<%<=I #T]^T]q33AJT]L^L__( OO,yq/AAL !,,-	0BBL$,,.%++%5%;%;!<<OOB'E %++%5%;%;!<< )5=8 _s   G;c                 P    SUS-  U R                   R                  S-  -   S-  -  nU$ )z
Compute the input conditioning factor for the EDM formulation.

Args:
    sigma (`float` or `torch.Tensor`):
        The current sigma (noise level) value.

Returns:
    `float` or `torch.Tensor`:
        The input conditioning factor `c_in`.
r   r   rT   )rG   r#   )rN   rc   rf   s      r   re   (EDMEulerScheduler._get_conditioning_c_inH  s0     UAX 6 6 99cABr   c                 .    U R                   R                  $ N)rG   r(   rU   s    r   __len__EDMEulerScheduler.__len__W  s    {{...r   )rM   rL   rK   r;   rJ   rF   )gMb`?g      T@rT   r&   i  r*   g      @r.   )r   )NNN)NNr   )(r   r   r   r   r   _compatiblesorderr   r~   r   r   rQ   propertyrV   rY   r]   r`   r   r   rh   rE   ru   rz   strr8   listr   rB   rC   r   ry   	Generatorboolr   tupler   r   re   r   r   r   r   r   r   r   1   s(    D LE !;C#'>G:@*,*, *, 	*,
   78*, !*, !!:;*, *, ##67*, 
*, *,X 5% 5 5  C     	!S 	! 	!(3 (t (%,, uu||?S X]XdXd $(<  &!! ll! u||#	!
 
!F @T Y^YeYe 4 $(%)48	/, /, ell"/, tE{*T1	/,j #'"&	ll 4< 4<	
 
B #'"&	ll 4< 4<	
 
: Y]#,#BG,,QUBU#	#>1)= 1$ 1( e,0 48jklljk %,,&jk 	jk
 jk jk jk jk ??T)jk jk $llT1jk 
!5	(jkZ.,,. ||. <<	.
 
.`EELL,@ UU\\EY / /r   r   )r   dataclassesr   typingr   r   configuration_utilsr   r   utilsr   r	   utils.torch_utilsr
   scheduling_utilsr   
get_loggerr   r   r   r   r   r   r   <module>r      s\     !   A ' , , 
		H	% 5j 5 5"g/ g/r   