
    +hI                         S SK Jr  S SKJr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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)    )	dataclass)ListOptionalTupleUnionN   )ConfigMixinregister_to_config)
BaseOutputlogging)randn_tensor   )SchedulerMixinc                   8    \ rS rSr% Sr\R                  \S'   Srg)$CMStochasticIterativeSchedulerOutput   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__torchTensor__annotations____static_attributes__r       l/home/james-whalen/.local/lib/python3.13/site-packages/diffusers/schedulers/scheduling_consistency_models.pyr   r      s     r   r   c                      \ rS rSrSrSr\       S)S\S\S\S\S\S	\S
\	4S jj5       r
\S 5       r\S 5       rS*S\4S jjrS\R                   S\\\R                   4   S\R                   4S jrS\\\R(                  4   4S jr   S+S\\   S\\\R0                  4   S\\\      4S jjrS rS rS rS,S jrS r  S-S \R                   S\\\R                   4   S\R                   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&g).CMStochasticIterativeScheduler,   ah  
Multistep and onestep sampling for consistency 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.

Args:
    num_train_timesteps (`int`, defaults to 40):
        The number of diffusion steps to train the model.
    sigma_min (`float`, defaults to 0.002):
        Minimum noise magnitude in the sigma schedule. Defaults to 0.002 from the original implementation.
    sigma_max (`float`, defaults to 80.0):
        Maximum noise magnitude in the sigma schedule. Defaults to 80.0 from the original implementation.
    sigma_data (`float`, defaults to 0.5):
        The standard deviation of the data distribution from the EDM
        [paper](https://huggingface.co/papers/2206.00364). Defaults to 0.5 from the original implementation.
    s_noise (`float`, defaults to 1.0):
        The amount of additional noise to counteract loss of detail during sampling. A reasonable range is [1.000,
        1.011]. Defaults to 1.0 from the original implementation.
    rho (`float`, defaults to 7.0):
        The parameter for calculating the Karras sigma schedule from the EDM
        [paper](https://huggingface.co/papers/2206.00364). Defaults to 7.0 from the original implementation.
    clip_denoised (`bool`, defaults to `True`):
        Whether to clip the denoised outputs to `(-1, 1)`.
    timesteps (`List` or `np.ndarray` or `torch.Tensor`, *optional*):
        An explicit timestep schedule that can be optionally specified. The timesteps are expected to be in
        increasing order.
r   num_train_timesteps	sigma_min	sigma_max
sigma_datas_noiserhoclip_denoisedc                 v   X0l         [        R                  " SSU5      nU R                  U5      n	U R	                  U	5      n
S U l        [        R                  " U	5      U l        [        R                  " U
5      U l	        SU l
        SU l        S U l        S U l        U R                  R                  S5      U l        g )Nr   r   Fcpu)init_noise_sigmanplinspace_convert_to_karras
sigma_to_tnum_inference_stepsr   
from_numpysigmas	timestepscustom_timestepsis_scale_input_called_step_index_begin_indexto)selfr#   r$   r%   r&   r'   r(   r)   rampr3   r4   s              r   __init__'CMStochasticIterativeScheduler.__init__L   s     !*{{1a!45((.OOF+	 $( &&v.)))4 %%*" kknnU+r   c                     U R                   $ )zW
The index counter for current timestep. It will increase 1 after each scheduler step.
)r7   r:   s    r   
step_index)CMStochasticIterativeScheduler.step_indexh   s    
 r   c                     U R                   $ )za
The index for the first timestep. It should be set from pipeline with `set_begin_index` method.
r8   r?   s    r   begin_index*CMStochasticIterativeScheduler.begin_indexo   s    
    r   rD   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`):
        The begin index for the scheduler.
NrC   )r:   rD   s     r   set_begin_index.CMStochasticIterativeScheduler.set_begin_indexw   s
     (r   sampletimestepreturnc                     U R                   c  U R                  U5        U R                  U R                      nXS-  U R                  R                  S-  -   S-  -  nSU l        U$ )a&  
Scales the consistency model input by `(sigma**2 + sigma_data**2) ** 0.5`.

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

Returns:
    `torch.Tensor`:
        A scaled input sample.
r         ?T)r@   _init_step_indexr3   configr&   r6   )r:   rI   rJ   sigmas       r   scale_model_input0CMStochasticIterativeScheduler.scale_model_input   s`     ??"!!(+DOO,1Ht{{'='=q'@@SHI%)"r   r3   c                     [        U[        R                  5      (       d#  [        R                  " U[        R                  S9nS[        R
                  " US-   5      -  nU$ )a"  
Gets scaled timesteps from the Karras sigmas for input to the consistency model.

Args:
    sigmas (`float` or `np.ndarray`):
        A single Karras sigma or an array of Karras sigmas.

Returns:
    `float` or `np.ndarray`:
        A scaled input timestep or scaled input timestep array.
dtypeg     @o@ggE6)
isinstancer-   ndarrayarrayfloat64log)r:   r3   r4   s      r   r0   )CMStochasticIterativeScheduler.sigma_to_t   sF     &"**--XXfBJJ7F"&&%"88	r   Nr1   devicer4   c           	      0   Uc  Uc  [        S5      eUb  Ub  [        S5      eUb  [        S[        U5      5       H  nX4   X4S-
     :  d  M  [        S5      e   US   U R                  R                  :  a#  [        SU R                  R                   S35      e[
        R                  " U[
        R                  S	9nS
U l        OXR                  R                  :  a=  [        SU SU R                  R                   SU R                  R                   S35      eXl	        U R                  R                  U R                  -  n[
        R                  " SU5      U-  R                  5       SSS2   R                  5       R                  [
        R                  5      nSU l        U R                  R                  nUSSS2   R                  5       nXvS-
  -  nU R                  U5      nU R                  U5      n[
        R                   " XR                  R"                  //5      R                  [
        R$                  5      n[&        R(                  " U5      R+                  US9U l        [/        U5      R1                  S5      (       a8  [&        R(                  " U5      R+                  U[&        R$                  S	9U l        O([&        R(                  " U5      R+                  US9U l        SU l        SU l        U R,                  R+                  S5      U l        g)a  
Sets the 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.
    timesteps (`List[int]`, *optional*):
        Custom timesteps used to support arbitrary spacing between timesteps. If `None`, then the default
        timestep spacing strategy of equal spacing between timesteps is used. If `timesteps` is passed,
        `num_inference_steps` must be `None`.
NzEExactly one of `num_inference_steps` or `timesteps` must be supplied.z:Can only pass one of `num_inference_steps` or `timesteps`.r   z(`timesteps` must be in descending order.r   z=`timesteps` must start before `self.config.train_timesteps`: .rT   Tz`num_inference_steps`: z6 cannot be larger than `self.config.train_timesteps`: zG as the unet model trained with this scheduler can only handle maximal z timesteps.F)r\   mpsr+   )
ValueErrorrangelenrO   r#   r-   rX   int64r5   r1   arangeroundcopyastyper/   r0   concatenater$   float32r   r2   r9   r3   str
startswithr4   r7   r8   )	r:   r1   r\   r4   i
step_ratior#   r;   r3   s	            r   set_timesteps,CMStochasticIterativeScheduler.set_timesteps   s   & &9+<dee*y/DYZZ  1c)n-<9U#33$%OPP . |t{{>>> STXT_T_TsTsSttuv  "((;I$(D!"[[%D%DD -.A-B C778 9  $ ? ?@M  (;$88D<T<TTJ1&9:ZGNNPQUSUQUV[[]ddegememnI$)D! #kk==2##%Q./((.OOF+	++*?*?)@ ABII"**U&&v.111@v;!!%(("--i8;;F%--;XDN"--i8;;6;JDN kknnU+r   c                     U R                   R                  nU R                   R                  nU R                   R                  nUSU-  -  nUSU-  -  nXaXV-
  -  -   U-  nU$ )z6Constructs the noise schedule of Karras et al. (2022).r   )rO   r$   r%   r(   )r:   r;   r$   r%   r(   min_inv_rhomax_inv_rhor3   s           r   r/   1CMStochasticIterativeScheduler._convert_to_karras   se      ;;00	;;00	kkooAG,AG,(A BBsJr   c                 |    U R                   R                  nUS-  US-  US-  -   -  nX-  US-  US-  -   S-  -  nX44$ )Nr   rM   )rO   r&   )r:   rP   r&   c_skipc_outs        r   get_scalings+CMStochasticIterativeScheduler.get_scalings  sS    [[++
Q%(Z]":;"eQhQ&>3%FF}r   c                     U R                   R                  nU R                   R                  nUS-  X-
  S-  US-  -   -  nX-
  U-  US-  US-  -   S-  -  nXE4$ )a4  
Gets the scalings used in the consistency model parameterization (from Appendix C of the
[paper](https://huggingface.co/papers/2303.01469)) to enforce boundary condition.

<Tip>

`epsilon` in the equations for `c_skip` and `c_out` is set to `sigma_min`.

</Tip>

Args:
    sigma (`torch.Tensor`):
        The current sigma in the Karras sigma schedule.

Returns:
    `tuple`:
        A two-element tuple where `c_skip` (which weights the current sample) is the first element and `c_out`
        (which weights the consistency model output) is the second element.
r   rM   )rO   r$   r&   )r:   rP   r$   r&   rv   rw   s         r   #get_scalings_for_boundary_conditionBCMStochasticIterativeScheduler.get_scalings_for_boundary_condition
  sn    ( KK))	[[++
Q5#4":Z]"JK"j0E1Hz1}4LQT3TT}r   c                     Uc  U R                   nX!:H  R                  5       n[        U5      S:  a  SOSnX4   R                  5       $ )Nr   r   )r4   nonzerorc   item)r:   rJ   schedule_timestepsindicesposs        r   index_for_timestep1CMStochasticIterativeScheduler.index_for_timestep&  sH    %!%%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 N)
rD   rV   r   r   r9   r4   r\   r   r7   r8   )r:   rJ   s     r   rN   /CMStochasticIterativeScheduler._init_step_index5  sX    #(ELL11#;;t~~'<'<=#66x@D#00Dr   model_output	generatorreturn_dictc                 ^   [        U[        [        R                  [        R                  45      (       a  [        SU R                   S35      eU R                  (       d  [        R                  S5        U R                  R                  nU R                  R                  nU R                  c  U R                  U5        U R                  U R                     nU R                  S-   U R                  R                   :  a  U R                  U R                  S-      n	OU R                  S   n	U R#                  U5      u  pX-  X-  -   nU R                  R$                  (       a  UR'                  SS5      n[)        U R*                  5      S:  a+  [-        UR.                  UR0                  UR2                  US9nO[        R4                  " U5      nXR                  R6                  -  nU	R'                  XgS9nXUS-  US-  -
  S	-  -  -   nU =R8                  S-  sl        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.CMStochasticIterativeSchedulerOutput`] or `tuple`.

Returns:
    [`~schedulers.scheduling_consistency_models.CMStochasticIterativeSchedulerOutput`] or `tuple`:
        If return_dict is `True`,
        [`~schedulers.scheduling_consistency_models.CMStochasticIterativeSchedulerOutput`] is returned,
        otherwise a tuple is returned where the first element is the sample tensor.
zLPassing integer indices (e.g. from `enumerate(timesteps)`) as timesteps to `z\.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   r_   )rU   r\   r   )minmaxr   rM   )r   )rV   intr   	IntTensor
LongTensorra   	__class__r6   loggerwarningrO   r$   r%   r@   rN   r3   r#   r{   r)   clamprc   r4   r   shaperU   r\   
zeros_liker'   r7   r   )r:   r   rJ   rI   r   r   r$   r%   rP   
sigma_nextrv   rw   denoisednoisez	sigma_hatr   s                    r   step#CMStochasticIterativeScheduler.step=  s   @ heoou7G7G HII( )GG  ))NNE
 KK))	KK))	??"!!(+ DOO,??Q!@!@@T__q%89J RJ @@G '&/9;;$$~~b!,H t~~" "",*<*<\EXEXdmE $$\2EKK'''$$$B	 ilY\&Ac%I!II 	A>!3LLr   original_samplesr   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 )N)r\   rU   r`   rT   r   r_   )r3   r9   r\   rU   typer   is_floating_pointr4   rj   rD   r   r@   r   flattenrc   	unsqueeze)
r:   r   r   r4   r3   r   tstep_indicesrP   noisy_sampless
             r   	add_noise(CMStochasticIterativeScheduler.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                 .    U R                   R                  $ r   )rO   r#   r?   s    r   __len__&CMStochasticIterativeScheduler.__len__  s    {{...r   )r8   r7   r5   r,   r6   r1   r3   r4   )(   gMb`?g      T@rM   g      ?g      @T)r   )NNNr   )NT)'r   r   r   r   r   orderr
   r   floatboolr<   propertyr@   rD   rG   r   r   r   rQ   r-   rW   r0   r   rk   r\   r   ro   r/   rx   r{   r   rN   	Generatorr   r   r   r   r   r   r   r   r   r!   r!   ,   s"   : E $& ", , , 	,
 , , , , ,6     ! !(3 ( eU\\FY@Z _d_k_k 2ubjj'8!9 * .2+/)-	G,%c]G, c5<<'(G, DI&	G,T
8#1 04 [Mll[M u||+,[M 	[M
 EOO,[M [M 
3U:	;[M|,, || <<	
 
B/r   r!   )dataclassesr   typingr   r   r   r   numpyr-   r   configuration_utilsr	   r
   utilsr   r   utils.torch_utilsr   scheduling_utilsr   
get_loggerr   r   r   r!   r   r   r   <module>r      s_    " / /   A ' , , 
		H	% 
: 
 
Q/^[ Q/r   