
    +h                         S SK r S SKJr  S SK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
 jr\ " S S\5      5       r " S S\\	5      rg)    N)	dataclass)OptionalTupleUnion   )ConfigMixinregister_to_config)
BaseOutput)randn_tensor   )SchedulerMixinc           
      &   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-  $ )NgMb?gT㥛 ?r   )mathcospits    m/home/james-whalen/.local/lib/python3.13/site-packages/diffusers/schedulers/scheduling_consistency_decoder.pyalpha_bar_fn)betas_for_alpha_bar.<locals>.alpha_bar_fn'   s-    88QY%/$''9A=>!CC    expc                 4    [         R                  " U S-  5      $ )Ng      ()r   r   r   s    r   r   r   ,   s    88AI&&r   z"Unsupported alpha_transform_type: r   )dtype)
ValueErrorrangeappendmintorch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                   8    \ rS rSr% Sr\R                  \S'   Srg)!ConsistencyDecoderSchedulerOutput:   a&  
Output class for the scheduler's `step` function.

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__r!   Tensor__annotations____static_attributes__r0   r   r   r-   r-   :   s     r   r-   c                   f   \ rS rSrSr\  SS\S\4S jj5       r  SS\	\   S\
\\R                  4   4S	 jjr\S
 5       rSS\R"                  S\	\   S\R"                  4S jjr  SS\R"                  S\
\\R"                  4   S\R"                  S\	\R&                     S\S\
\\4   4S jjrSrg)ConsistencyDecoderSchedulerH   r   num_train_timesteps
sigma_datac                    [        U5      nSU-
  n[        R                  " USS9n[        R                  " U5      U l        [        R                  " SU-
  5      U l        [        R                  " SU-  S-
  5      n[        R                  " SU-  5      nXrS-  -  US-  US-  -   -  U l        Xb-  US-  US-  -   S-  -  U l        XvS-  US-  -   S-  -  U l        g )Ng      ?r   )dimr   r         ?)	r+   r!   cumprodsqrtsqrt_alphas_cumprodsqrt_one_minus_alphas_cumprodc_skipc_outc_in)selfr<   r=   r'   alphasalphas_cumprodsigmassqrt_recip_alphas_cumprods           r   __init__$ConsistencyDecoderScheduler.__init__K   s     $$78uv15#(::n#= -2ZZn8L-M*C.0145$)JJs^/C$D!/a-?619z[\}C\](FAI
A,E#+MM
-Z]1Js0RR	r   Nnum_inference_stepsdevicec                    US:w  a  [        S5      e[        R                  " SS/[        R                  US9U l        U R
                  R                  U5      U l        U R                  R                  U5      U l        U R                  R                  U5      U l        U R                  R                  U5      U l	        U R                  R                  U5      U l
        g )Nr   z8Currently more than 2 inference steps are not supported.i  i   )r   rP   )r   r!   r"   long	timestepsrC   torD   rE   rF   rG   )rH   rO   rP   s      r   set_timesteps)ConsistencyDecoderScheduler.set_timestepsa   s    
 !#WXXtSkFS#'#;#;#>#>v#F -1-O-O-R-RSY-Z*kknnV,ZZ]]6*
IILL(	r   c                 :    U R                   U R                  S      $ )Nr   )rD   rS   )rH   s    r   init_noise_sigma,ConsistencyDecoderScheduler.init_noise_sigmap   s    11$..2CDDr   sampletimestepreturnc                 $    XR                   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.
)rG   )rH   rZ   r[   s      r   scale_model_input-ConsistencyDecoderScheduler.scale_model_inputt   s     		(+++r   model_output	generatorreturn_dictc                 <   U R                   U   U-  U R                  U   U-  -   n[        R                  " U R                  U:H  5      S   nU[        U R                  5      S-
  :X  a  UnO[        UR                  XFR                  UR                  S9n	U R                  U R                  US-         R                  UR                  5      U-  U R                  U R                  US-         R                  UR                  5      U	-  -   nU(       d  U4$ [        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`):
        The current timestep in the diffusion chain.
    sample (`torch.Tensor`):
        A current instance of a sample created by the diffusion process.
    generator (`torch.Generator`, *optional*):
        A random number generator.
    return_dict (`bool`, *optional*, defaults to `True`):
        Whether or not to return a
        [`~schedulers.scheduling_consistency_models.ConsistencyDecoderSchedulerOutput`] or `tuple`.

Returns:
    [`~schedulers.scheduling_consistency_models.ConsistencyDecoderSchedulerOutput`] or `tuple`:
        If return_dict is `True`,
        [`~schedulers.scheduling_consistency_models.ConsistencyDecoderSchedulerOutput`] is returned, otherwise
        a tuple is returned where the first element is the sample tensor.
r   r   )ra   r   rP   )r/   )rF   rE   r!   whererS   lenr   shaper   rP   rC   rT   rD   r-   )
rH   r`   r[   rZ   ra   rb   x_0timestep_idxr/   noises
             r   step ConsistencyDecoderScheduler.step   s   > jj"\1DKK4IF4RR{{4>>X#=>qA3t~~.22K iyyY\YcYcdE((q8H)IJMMciiX[^^44T^^LSTDT5UVYYZ]ZcZcdgllm 
 >!0[IIr   )rG   rF   rE   rC   rD   rS   )i   r@   )NN)N)NT)r1   r2   r3   r4   orderr	   intfloatrM   r   r   strr!   rP   rU   propertyrX   r6   r^   	Generatorboolr-   r   rj   r8   r0   r   r   r:   r:   H   s%   E $(S S S S. .2+/)%c]) c5<<'() E E, , ,Y^YeYe ,, 04 /Jll/J u||+,/J 	/J
 EOO,/J /J 
0%7	8/J /Jr   r:   )g+?r   )r   dataclassesr   typingr   r   r   r!   configuration_utilsr   r	   utilsr
   utils.torch_utilsr   scheduling_utilsr   r+   r-   r:   r0   r   r   <module>ry      s[     ! ) )  A  , , !)4X 

 
 
lJ.+ lJr   