
    +h"                         S SK r S SKJr  S SKJrJrJrJ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 jr " S S\\
5      rg)    N)	dataclass)ListOptionalTupleUnion   )ConfigMixinregister_to_config)
BaseOutput)randn_tensor   )SchedulerMixinc                   8    \ rS rSr% Sr\R                  \S'   Srg)DDPMWuerstchenSchedulerOutput   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.
prev_sample N)	__name__
__module____qualname____firstlineno____doc__torchTensor__annotations____static_attributes__r       i/home/james-whalen/.local/lib/python3.13/site-packages/diffusers/schedulers/scheduling_ddpm_wuerstchen.pyr   r      s     r   r   c           
      &   US:X  a  S nOUS:X  a  S nO[        SU 35      e/ n[        U 5       H<  nXP-  nUS-   U -  nUR                  [        SU" U5      U" U5      -  -
  U5      5        M>     [        R
                  " U[        R                  S9$ )a  
Create a beta schedule that discretizes the given alpha_t_bar function, which defines the cumulative product of
(1-beta) over time from t = [0,1].

Contains a function alpha_bar that takes an argument t and transforms it to the cumulative product of (1-beta) up
to that part of the diffusion process.


Args:
    num_diffusion_timesteps (`int`): the number of betas to produce.
    max_beta (`float`): the maximum beta to use; use values lower than 1 to
                 prevent singularities.
    alpha_transform_type (`str`, *optional*, default to `cosine`): the type of noise schedule for alpha_bar.
                 Choose from `cosine` or `exp`

Returns:
    betas (`np.ndarray`): the betas used by the scheduler to step the model outputs
cosinec                 h    [         R                  " U S-   S-  [         R                  -  S-  5      S-  $ )NMb?gT㥛 ?r   )mathcospits    r   alpha_bar_fn)betas_for_alpha_bar.<locals>.alpha_bar_fnE   s-    88QY%/$''9A=>!CCr   expc                 4    [         R                  " U S-  5      $ )Ng      ()r#   r*   r&   s    r   r(   r)   J   s    88AI&&r   z"Unsupported alpha_transform_type: r   dtype)
ValueErrorrangeappendminr   tensorfloat32)num_diffusion_timestepsmax_betaalpha_transform_typer(   betasit1t2s           r   betas_for_alpha_barr;   ,   s    . x'	D 
	&	' =>R=STUUE*+(!e..S\"-R0@@@(KL , <<U]]33r   c                      \ rS rSrSr\  SS\S\4S jj5       rS rSS\	R                  S	\\   S
\	R                  4S jjr   SS\S\\\      S\\\	R"                  4   4S jjr  SS\	R                  S	\S\	R                  S\S
\\\4   4
S jjrS\	R                  S\	R                  S\	R                  S
\	R                  4S jrS rS rSrg)DDPMWuerstchenSchedulerX   a  
Denoising diffusion probabilistic models (DDPMs) explores the connections between denoising score matching and
Langevin dynamics sampling.

[`~ConfigMixin`] takes care of storing all config attributes that are passed in the scheduler's `__init__`
function, such as `num_train_timesteps`. They can be accessed via `scheduler.config.num_train_timesteps`.
[`SchedulerMixin`] provides general loading and saving functionality via the [`SchedulerMixin.save_pretrained`] and
[`~SchedulerMixin.from_pretrained`] functions.

For more details, see the original paper: https://huggingface.co/papers/2006.11239

Args:
    scaler (`float`): ....
    s (`float`): ....
scalersc                     Xl         [        R                  " U/5      U l        [        R                  " U R                  SU R                  -   -  [        R
                  -  S-  5      S-  U l        SU l        g )Nr         ?r         ?)r?   r   r2   r@   r$   r%   _init_alpha_cumprodinit_noise_sigma)selfr?   r@   s      r   __init__ DDPMWuerstchenScheduler.__init__i   s\     qc"#(99TVVq466z-BUXX-MPS-S#TXY#Y  !$r   c                    U R                   S:  a  SSU-
  U R                   -  -
  nOU R                   S:  a  XR                   -  n[        R                  " XR                  R	                  U5      -   SU R                  R	                  U5      -   -  [        R
                  -  S-  5      S-  U R                  R	                  U5      -  nUR                  SS5      $ )Nr   rB   r   g-C6?gH.?)r?   r   r$   r@   tor%   rD   clamp)rF   r'   devicealpha_cumprods       r   _alpha_cumprod&DDPMWuerstchenScheduler._alpha_cumprodv   s    ;;?QUt{{**A[[1_;;A		6""q46699V+<'<=H3N
)),,V45 ""6622r   Nsampletimestepreturnc                     U$ )a  
Ensures interchangeability with schedulers that need to scale the denoising model input depending on the
current timestep.

Args:
    sample (`torch.Tensor`): input sample
    timestep (`int`, optional): current timestep

Returns:
    `torch.Tensor`: scaled input sample
r   )rF   rP   rQ   s      r   scale_model_input)DDPMWuerstchenScheduler.scale_model_input   s	     r   num_inference_steps	timestepsrL   c                     Uc  [         R                  " SSUS-   US9n[        U[         R                  5      (       d%  [         R                  " U5      R	                  U5      nX l        g)a  
Sets the discrete timesteps used for the diffusion chain. Supporting function to be run before inference.

Args:
    num_inference_steps (`Dict[float, int]`):
        the number of diffusion steps used when generating samples with a pre-trained model. If passed, then
        `timesteps` must be `None`.
    device (`str` or `torch.device`, optional):
        the device to which the timesteps are moved to. {2 / 3: 20, 0.0: 10}
NrC   g        r   rL   )r   linspace
isinstancer   rJ   rW   )rF   rV   rW   rL   s       r   set_timesteps%DDPMWuerstchenScheduler.set_timesteps   sT      sC1Dq1HQWXI)U\\22Y/226:I"r   model_outputreturn_dictc           	         UR                   nUR                  nUnU R                  U5      n	U R                  X5      R                  " UR                  S5      /UR                  SS  V
s/ s H  n
SPM     sn
Q76 nU R                  X5      R                  " U	R                  S5      /UR                  SS  V
s/ s H  n
SPM     sn
Q76 nX-  nSU-  R                  5       USU-
  U-  SU-
  R                  5       -  -
  -  n[        UR                  XAR                  UR                   S9nSU-
  SU-
  -  SU-
  -  R                  5       U-  nUUU	S:g  R                  5       R                  " U	R                  S5      /UR                  SS  V
s/ s H  n
SPM     sn
Q76 -  -   nU(       d  UR                  U5      4$ [        UR                  U5      S9$ s  sn
f s  sn
f s  sn
f )a  
Predict the sample at the previous timestep by reversing the SDE. Core function to propagate the diffusion
process from the learned model outputs (most often the predicted noise).

Args:
    model_output (`torch.Tensor`): direct output from learned diffusion model.
    timestep (`int`): current discrete timestep in the diffusion chain.
    sample (`torch.Tensor`):
        current instance of sample being created by diffusion process.
    generator: random number generator.
    return_dict (`bool`): option for returning tuple rather than DDPMWuerstchenSchedulerOutput class

Returns:
    [`DDPMWuerstchenSchedulerOutput`] or `tuple`: [`DDPMWuerstchenSchedulerOutput`] if `return_dict` is True,
    otherwise a `tuple`. When returning a tuple, the first element is the sample tensor.

r   r   NrC   )	generatorrL   r-   )r   )r-   rL   previous_timesteprN   viewsizeshapesqrtr   floatrJ   r   )rF   r^   rQ   rP   ra   r_   r-   rL   r'   prev_t_rM   alpha_cumprod_prevalphamu	std_noisestdpreds                     r   stepDDPMWuerstchenScheduler.step   s   2 ""$$''*++A6;;AFF1IfTZT`T`abacTdHeTdqTdHef!00@EEfkkRSnucicocopqprcsWtcs^_XYcsWtu2Ek!Vq5yL.HAP]L]KcKcKe.e%ef YGZGZbnbtbtu	E	c$6673;NOUUWZccC6Q;--/44V[[^dRXR^R^_`_aRbFcRbQqRbFcdddGGEN$$,HH IfWt Gds   )G
8G
Goriginal_samplesnoisec                 H   UR                   nUR                  nU R                  X4S9R                  " UR	                  S5      /UR
                  SS   Vs/ s H  nSPM     snQ76 nUR                  5       U-  SU-
  R                  5       U-  -   nUR                  US9$ s  snf )NrY   r   r   r,   )rL   r-   rN   rc   rd   re   rf   rJ   )	rF   rr   rs   rW   rL   r-   ri   rM   noisy_sampless	            r   	add_noise!DDPMWuerstchenScheduler.add_noise   s     "(( &&++I+EJJNN1
,<,B,B12,F G,Fq,F G
 &**,/??1}CTBZBZB\_dBdde,, !Hs   B
c                 .    U R                   R                  $ N)confignum_train_timesteps)rF   s    r   __len__DDPMWuerstchenScheduler.__len__   s    {{...r   c                     U R                   US   -
  R                  5       R                  5       R                  5       nU R                   US-      S    R	                  UR
                  S   5      nU$ )Nr   r   )rW   absargminitemexpandre   )rF   rQ   indexrh   s       r   rb   )DDPMWuerstchenScheduler.previous_timestep   s_    (1+-224;;=BBD	*4077q8IJr   )rD   rE   r@   r?   rW   )rC   r"   ry   )NNN)NT)r   r   r   r   r   r
   rg   rG   rN   r   r   r   intrT   r   r   strrL   r\   boolr   r   rp   rv   r|   rb   r   r   r   r   r=   r=   X   sB      
$
$ 
$ 
$3  Y^YeYe   $()-+/	# # DI&# c5<<'(	#6  ,Ill,I ,I 	,I ,I 
,e3	4,I\-,,- ||- <<	-
 
-/r   r=   )g+?r    )r#   dataclassesr   typingr   r   r   r   r   configuration_utilsr	   r
   utilsr   utils.torch_utilsr   scheduling_utilsr   r   r;   r=   r   r   r   <module>r      s[   $  ! / /  A  , , 
J 
 
 !)4XNnk Nr   