
    +h%                         S SK Jr  S SKJrJrJr  S SKrS SKrSSK	J
r
Jr  SSKJr  SSKJr  SS	KJr  \ " S
 S\5      5       r " S S\\
5      rg)    )	dataclass)OptionalTupleUnionN   )ConfigMixinregister_to_config)
BaseOutput)randn_tensor   )SchedulerMixinc                   ~    \ rS rSr% Sr\R                  \S'   \R                  \S'   Sr\	\R                     \S'   Sr
g)KarrasVeOutput   a  
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.
    derivative (`torch.Tensor` of shape `(batch_size, num_channels, height, width)` for images):
        Derivative of predicted original image sample (x_0).
    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_sample
derivativeNpred_original_sample )__name__
__module____qualname____firstlineno____doc__torchTensor__annotations__r   r   __static_attributes__r       n/home/james-whalen/.local/lib/python3.13/site-packages/diffusers/schedulers/deprecated/scheduling_karras_ve.pyr   r      s1     37(5<<07r   r   c                   *   \ rS rSrSrSr\      S!S\S\S\S\S\S	\4S
 jj5       rS"S\	R                  S\\   S\	R                  4S jjrS"S\S\\\	R                   4   4S jjr S"S\	R                  S\S\\	R$                     S\\	R                  \4   4S jjr S#S\	R                  S\S\S\	R                  S\S\\\4   4S jjr S#S\	R                  S\S\S\	R                  S\	R                  S\	R                  S\S\\\4   4S jjrS rS rg)$KarrasVeScheduler1   a  
A stochastic scheduler tailored to variance-expanding models.

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.

<Tip>

For more details on the parameters, see [Appendix E](https://huggingface.co/papers/2206.00364). The grid search
values used to find the optimal `{s_noise, s_churn, s_min, s_max}` for a specific model are described in Table 5 of
the paper.

</Tip>

Args:
    sigma_min (`float`, defaults to 0.02):
        The minimum noise magnitude.
    sigma_max (`float`, defaults to 100):
        The maximum noise magnitude.
    s_noise (`float`, defaults to 1.007):
        The amount of additional noise to counteract loss of detail during sampling. A reasonable range is [1.000,
        1.011].
    s_churn (`float`, defaults to 80):
        The parameter controlling the overall amount of stochasticity. A reasonable range is [0, 100].
    s_min (`float`, defaults to 0.05):
        The start value of the sigma range to add noise (enable stochasticity). A reasonable range is [0, 10].
    s_max (`float`, defaults to 50):
        The end value of the sigma range to add noise. A reasonable range is [0.2, 80].
r   	sigma_min	sigma_maxs_noises_churns_mins_maxc                 :    X l         S U l        S U l        S U l        g N)init_noise_sigmanum_inference_steps	timestepsschedule)selfr#   r$   r%   r&   r'   r(   s          r   __init__KarrasVeScheduler.__init__R   s"     !* )- '+&*r   Nsampletimestepreturnc                     U$ )aN  
Ensures interchangeability with schedulers that need to scale the denoising model input depending on the
current timestep.

Args:
    sample (`torch.Tensor`):
        The input sample.
    timestep (`int`, *optional*):
        The current timestep in the diffusion chain.

Returns:
    `torch.Tensor`:
        A scaled input sample.
r   )r/   r2   r3   s      r   scale_model_input#KarrasVeScheduler.scale_model_inputd   s	     r   r,   devicec                    Xl         [        R                  " SU R                   5      SSS2   R                  5       n[        R
                  " U5      R                  U5      U l        U R                   Vs/ s HX  nU R                  R                  S-  U R                  R                  S-  U R                  R                  S-  -  XAS-
  -  -  -  PMZ     nn[        R                  " U[        R                  US9U l        gs  snf )au  
Sets the discrete timesteps used for the diffusion chain (to be run before inference).

Args:
    num_inference_steps (`int`):
        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.
r   Nr      )dtyper8   )r,   nparangecopyr   
from_numpytor-   configr$   r#   tensorfloat32r.   )r/   r,   r8   r-   ir.   s         r   set_timestepsKarrasVeScheduler.set_timestepsu   s     $7 IIa!9!9:4R4@EEG	)))477? ^^

 $ %%q(;;((!+dkk.C.CQ.FFAghQhLijk $ 	 
 XU]]6R
s   4AC?sigma	generatorc                    U R                   R                  Us=::  a  U R                   R                  ::  a1  O  O.[        U R                   R                  U R
                  -  S5      nOSnU R                   R                  [        UR                  US9R                  UR                  5      -  nX$U-  -   nXS-  US-  -
  S-  U-  -   nXv4$ )uN  
Explicit Langevin-like "churn" step of adding noise to the sample according to a `gamma_i ≥ 0` to reach a
higher noise level `sigma_hat = sigma_i + gamma_i*sigma_i`.

Args:
    sample (`torch.Tensor`):
        The input sample.
    sigma (`float`):
    generator (`torch.Generator`, *optional*):
        A random number generator.
g4y?r   )rI   r         ?)rB   r'   r(   minr&   r,   r%   r   shaperA   r8   )r/   r2   rH   rI   gammaeps	sigma_hat
sample_hats           r   add_noise_to_input$KarrasVeScheduler.add_noise_to_input   s     ;;:):)::++d.F.FF
SEE kk!!L$S$V$VW]WdWd$eeEM)	1uax 7C?#EF
$$r   model_outputrP   
sigma_prevrQ   return_dictc                 Z    XBU-  -   nXF-
  U-  nXCU-
  U-  -   nU(       d  X4$ [        XU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 learned diffusion model.
    sigma_hat (`float`):
    sigma_prev (`float`):
    sample_hat (`torch.Tensor`):
    return_dict (`bool`, *optional*, defaults to `True`):
        Whether or not to return a [`~schedulers.scheduling_karras_ve.KarrasVESchedulerOutput`] or `tuple`.

Returns:
    [`~schedulers.scheduling_karras_ve.KarrasVESchedulerOutput`] or `tuple`:
        If return_dict is `True`, [`~schedulers.scheduling_karras_ve.KarrasVESchedulerOutput`] is returned,
        otherwise a tuple is returned where the first element is the sample tensor.

r   r   r   r   )	r/   rT   rP   rU   rQ   rV   r   r   sample_prevs	            r   stepKarrasVeScheduler.step   sP    8  *,DD 79D
 $:j#HH,,#Qe
 	
r   rZ   r   c                 l    XSU-  -   nXX-
  U-  n	XCU-
  SU-  SU	-  -   -  -   nU(       d  XV4$ [        XVUS9$ )a\  
Corrects the predicted sample based on the `model_output` of the network.

Args:
    model_output (`torch.Tensor`):
        The direct output from learned diffusion model.
    sigma_hat (`float`): TODO
    sigma_prev (`float`): TODO
    sample_hat (`torch.Tensor`): TODO
    sample_prev (`torch.Tensor`): TODO
    derivative (`torch.Tensor`): TODO
    return_dict (`bool`, *optional*, defaults to `True`):
        Whether or not to return a [`~schedulers.scheduling_ddpm.DDPMSchedulerOutput`] or `tuple`.

Returns:
    prev_sample (TODO): updated sample in the diffusion chain. derivative (TODO): TODO

rK   rX   rY   )
r/   rT   rP   rU   rQ   rZ   r   rV   r   derivative_corrs
             r   step_correctKarrasVeScheduler.step_correct   sa    8  +,-FF&=K $:sZ?ORUXgRg?g#hh,,#Qe
 	
r   c                     [        5       er*   )NotImplementedError)r/   original_samplesnoiser-   s       r   	add_noiseKarrasVeScheduler.add_noise   s    !##r   )r+   r,   r.   r-   )g{Gz?d   g&1?P   g?2   r*   )T)r   r   r   r   r   orderr	   floatr0   r   r   r   intr6   r   strr8   rF   	Generatorr   rR   boolr   r[   r_   re   r   r   r   r   r!   r!   1   s   < E  ++ + 	+
 + + + +"  Y^YeYe "S SeCDU>V S. Z^%ll%+0%=Eeoo=V%	u||U"	#%@ !%
ll%
 %
 	%

 LL%
 %
 
~u$	%%
^ !%
ll%
 %
 	%

 LL%
 \\%
 LL%
 %
 
~u$	%%
N$r   r!   )dataclassesr   typingr   r   r   numpyr=   r   configuration_utilsr   r	   utilsr
   utils.torch_utilsr   scheduling_utilsr   r   r!   r   r   r   <module>rw      sM     " ) )   B  - - 8Z 8 8(C$ C$r   