
    
3j)               	           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
  SSKJr  SS	KJr    SS
\S\S\S   S\R"                  4S jjr\ " S S\
5      5       r " S S\\5      rg)    N)	dataclass)Literal   )ConfigMixinregister_to_config)
BaseOutput)randn_tensor   )SchedulerMixinnum_diffusion_timestepsmax_betaalpha_transform_type)cosineexplaplacereturnc           
      :   US:X  a  S nO"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`, defaults to `0.999`):
        The maximum beta to use; use values lower than 1 to avoid numerical instability.
    alpha_transform_type (`str`, defaults to `"cosine"`):
        The type of noise schedule for `alpha_bar`. Choose from `cosine`, `exp`, or `laplace`.

Returns:
    `torch.Tensor`:
        The betas used by the scheduler to step the model outputs.
r   c                 h    [         R                  " U S-   S-  [         R                  -  S-  5      S-  $ )NgMb?gT㥛 ?r   )mathcospits    m/home/wildlama/miniconda3/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    r   c           	         S[         R                  " SSU -
  5      -  [         R                  " SS[         R                  " SU -
  5      -  -
  S-   5      -  n[         R                  " U5      n[         R
                  " USU-   -  5      $ )Ng      r
         ?r   gư>)r   copysignlogfabsr   sqrt)r   lmbsnrs      r   r   r   -   sm    q#'22TXXa!diiPSVWPWFXBX>X[_>_5``C((3-C99SAG_--r   r   c                 4    [         R                  " U S-  5      $ )Ng      ()r   r   r   s    r   r   r   4   s    88AI&&r   z"Unsupported alpha_transform_type: r
   )dtype)
ValueErrorrangeappendmintorchtensorfloat32)r   r   r   r   betasit1t2s           r   betas_for_alpha_barr3      s    0 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)!ConsistencyDecoderSchedulerOutputB   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__r8   r   r   r5   r5   B   s     r   r5   c                   ~   \ rS rSrSrSr\  SS\S\SS4S jj5       r	  SS	\S-  S
\
\R                  -  4S jjr\S\R                  4S j5       rSS\R                  S\S-  S\R                  4S jjr  SS\R                  S\\R                  -  S\R                  S\R$                  S-  S\S\\-  4S jjrSrg)ConsistencyDecoderSchedulerP   a  
A scheduler for the consistency decoder used in Stable Diffusion pipelines.

This scheduler implements a two-step denoising process using consistency models for decoding latent representations
into images.

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:
    num_train_timesteps (`int`, *optional*, defaults to `1024`):
        The number of diffusion steps to train the model.
    sigma_data (`float`, *optional*, defaults to `0.5`):
        The standard deviation of the data distribution. Used for computing the skip and output scaling factors.
r
   num_train_timesteps
sigma_datar   Nc                    [        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   )	r3   r,   cumprodr#   sqrt_alphas_cumprodsqrt_one_minus_alphas_cumprodc_skipc_outc_in)selfrD   rE   r/   alphasalphas_cumprodsigmassqrt_recip_alphas_cumprods           r   __init__$ConsistencyDecoderScheduler.__init__c   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   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'   rV   )r(   r,   r-   long	timestepsrI   torJ   rK   rL   rM   )rN   rU   rV   s      r   set_timesteps)ConsistencyDecoderScheduler.set_timestepsy   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      $ )z
Return the standard deviation of the initial noise distribution.

Returns:
    `torch.Tensor`:
        The initial noise sigma value from the precomputed `sqrt_one_minus_alphas_cumprod` at the first
        timestep.
r   )rJ   rY   )rN   s    r   init_noise_sigma,ConsistencyDecoderScheduler.init_noise_sigma   s     11$..2CDDr   sampletimestepc                 $    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.
)rM   )rN   r`   ra   s      r   scale_model_input-ConsistencyDecoderScheduler.scale_model_input   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$ )aQ  
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 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 for reproducibility.
    return_dict (`bool`, *optional*, defaults to `True`):
        Whether or not to return a
        [`~schedulers.scheduling_consistency_decoder.ConsistencyDecoderSchedulerOutput`] or `tuple`.

Returns:
    [`~schedulers.scheduling_consistency_decoder.ConsistencyDecoderSchedulerOutput`] or `tuple`:
        If `return_dict` is `True`,
        [`~schedulers.scheduling_consistency_decoder.ConsistencyDecoderSchedulerOutput`] is returned, otherwise
        a tuple is returned where the first element is the sample tensor.
r   r
   )rf   r'   rV   )r7   )rL   rK   r,   whererY   lenr	   shaper'   rV   rI   rZ   rJ   r5   )
rN   re   ra   r`   rf   rg   x_0timestep_idxr7   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   )rM   rL   rK   rI   rJ   rY   )i   r   )NN)N)NT)r9   r:   r;   r<   r=   orderr   intfloatrS   strr,   rV   r[   propertyr>   r^   rc   	Generatorboolr5   tuplero   r@   r8   r   r   rB   rB   P   s3     E $(S S S 
	S S. +/%)) 4Z) ell") 	E%,, 	E 	E, ,d
 ,V[VbVb ,, -1 /Jll/J %,,&/J 	/J
 ??T)/J /J 
+U	2/J /Jr   rB   )g+?r   )r   dataclassesr   typingr   r,   configuration_utilsr   r   utilsr   utils.torch_utilsr	   scheduling_utilsr   rr   rs   r>   r3   r5   rB   r8   r   r   <module>r      s     !   A  , , @H14 1414 ""<=14 \\	14h 

 
 
DJ.+ DJr   