
    +h!                         S SK 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Jr  SSKJr  SSKJrJrJr  \" 5       (       a  S SKr\R0                  " \5      r  SS	 jr " S
 S\\
5      rg)    N)ListOptionalTupleUnion   )ConfigMixinregister_to_config)	deprecateis_scipy_availablelogging)randn_tensor   )KarrasDiffusionSchedulersSchedulerMixinSchedulerOutputc           
      &   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    n/home/james-whalen/.local/lib/python3.13/site-packages/diffusers/schedulers/scheduling_dpmsolver_singlestep.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   B   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_barr0   $   s    . x'	D 
	&	' =>R=STUUE*+(!e..S\"-R0@@@(KL , <<U]]33r   c            0          \ rS rSrSr\ V Vs/ s H  oR                  PM     snn rSr\	SSSSSS	S
SSSSSSSSSSSS\
" S5      * SSS4S\S\
S\
S\S\\R                     S\S\S\S\
S\
S\S\S\S \\   S!\\   S"\\   S#\\   S$\\
   S%\\   S&\
S'\\   S(\S)\4.S* jj5       rS+\S,\\   4S- jr\S. 5       r\S/ 5       rSVS0\4S1 jjr    SWS+\S2\\\R4                  4   S3\\
   S4\\\      4S5 jjrS6\R8                  S,\R8                  4S7 jrS8 rS9 rS:\R8                  S,\R8                  4S; jr S:\R8                  S+\S,\R8                  4S< jr! SXS:\R8                  S+\S=\
S>\
S,\R8                  4
S? jjr"SS@.SA\R8                  S6\R8                  S,\R8                  4SB jjr#SSSC.SA\R8                  S6\R8                  SD\\R8                     S,\R8                  4SE jjr$SSSC.SF\\R8                     S6\R8                  SD\\R8                     S,\R8                  4SG jjr%SSSC.SF\\R8                     S6\R8                  SD\\R8                     S,\R8                  4SH jjr&SSSSI.SF\\R8                     S6\R8                  SJ\SD\\R8                     S,\R8                  4
SK jjr'SYSL jr(SM r)  SZSA\R8                  SN\\\R8                  4   S6\R8                  SO\S,\\*\+4   4
SP jjr,S6\R8                  S,\R8                  4SQ jr-SR\R8                  SD\R8                  S4\R\                  S,\R8                  4SS jr/ST r0SUr1gs  snn f )[DPMSolverSinglestepSchedulerP   u  
`DPMSolverSinglestepScheduler` is a fast dedicated high-order solver for diffusion ODEs.

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 1000):
        The number of diffusion steps to train the model.
    beta_start (`float`, defaults to 0.0001):
        The starting `beta` value of inference.
    beta_end (`float`, defaults to 0.02):
        The final `beta` value.
    beta_schedule (`str`, defaults to `"linear"`):
        The beta schedule, a mapping from a beta range to a sequence of betas for stepping the model. Choose from
        `linear`, `scaled_linear`, or `squaredcos_cap_v2`.
    trained_betas (`np.ndarray`, *optional*):
        Pass an array of betas directly to the constructor to bypass `beta_start` and `beta_end`.
    solver_order (`int`, defaults to 2):
        The DPMSolver order which can be `1` or `2` or `3`. It is recommended to use `solver_order=2` for guided
        sampling, and `solver_order=3` for unconditional sampling.
    prediction_type (`str`, defaults to `epsilon`, *optional*):
        Prediction type of the scheduler function; can be `epsilon` (predicts the noise of the diffusion process),
        `sample` (directly predicts the noisy sample`) or `v_prediction` (see section 2.4 of [Imagen
        Video](https://imagen.research.google/video/paper.pdf) paper).
    thresholding (`bool`, defaults to `False`):
        Whether to use the "dynamic thresholding" method. This is unsuitable for latent-space diffusion models such
        as Stable Diffusion.
    dynamic_thresholding_ratio (`float`, defaults to 0.995):
        The ratio for the dynamic thresholding method. Valid only when `thresholding=True`.
    sample_max_value (`float`, defaults to 1.0):
        The threshold value for dynamic thresholding. Valid only when `thresholding=True` and
        `algorithm_type="dpmsolver++"`.
    algorithm_type (`str`, defaults to `dpmsolver++`):
        Algorithm type for the solver; can be `dpmsolver` or `dpmsolver++` or `sde-dpmsolver++`. The `dpmsolver`
        type implements the algorithms in the [DPMSolver](https://huggingface.co/papers/2206.00927) paper, and the
        `dpmsolver++` type implements the algorithms in the [DPMSolver++](https://huggingface.co/papers/2211.01095)
        paper. It is recommended to use `dpmsolver++` or `sde-dpmsolver++` with `solver_order=2` for guided
        sampling like in Stable Diffusion.
    solver_type (`str`, defaults to `midpoint`):
        Solver type for the second-order solver; can be `midpoint` or `heun`. The solver type slightly affects the
        sample quality, especially for a small number of steps. It is recommended to use `midpoint` solvers.
    lower_order_final (`bool`, defaults to `True`):
        Whether to use lower-order solvers in the final steps. Only valid for < 15 inference steps. This can
        stabilize the sampling of DPMSolver for steps < 15, especially for steps <= 10.
    use_karras_sigmas (`bool`, *optional*, defaults to `False`):
        Whether to use Karras sigmas for step sizes in the noise schedule during the sampling process. If `True`,
        the sigmas are determined according to a sequence of noise levels {σi}.
    use_exponential_sigmas (`bool`, *optional*, defaults to `False`):
        Whether to use exponential sigmas for step sizes in the noise schedule during the sampling process.
    use_beta_sigmas (`bool`, *optional*, defaults to `False`):
        Whether to use beta sigmas for step sizes in the noise schedule during the sampling process. Refer to [Beta
        Sampling is All You Need](https://huggingface.co/papers/2407.12173) for more information.
    final_sigmas_type (`str`, *optional*, defaults to `"zero"`):
        The final `sigma` value for the noise schedule during the sampling process. If `"sigma_min"`, the final
        sigma is the same as the last sigma in the training schedule. If `zero`, the final sigma is set to 0.
    lambda_min_clipped (`float`, defaults to `-inf`):
        Clipping threshold for the minimum value of `lambda(t)` for numerical stability. This is critical for the
        cosine (`squaredcos_cap_v2`) noise schedule.
    variance_type (`str`, *optional*):
        Set to "learned" or "learned_range" for diffusion models that predict variance. If set, the model's output
        contains the predicted Gaussian variance.
r   i  g-C6?g{Gz?linearNr   epsilonFgףp=
?      ?dpmsolver++midpointzeroinfexponentialnum_train_timesteps
beta_startbeta_endbeta_scheduletrained_betassolver_orderprediction_typethresholdingdynamic_thresholding_ratiosample_max_valuealgorithm_typesolver_typelower_order_finaluse_karras_sigmasuse_exponential_sigmasuse_beta_sigmasuse_flow_sigmas
flow_shiftfinal_sigmas_typelambda_min_clippedvariance_typeuse_dynamic_shiftingtime_shift_typec                 @   U R                   R                  (       a  [        5       (       d  [        S5      e[	        U R                   R                  U R                   R
                  U R                   R                  /5      S:  a  [        S5      eUS:X  a  Sn[        SSU5        Ub)  [        R                  " U[        R                  S9U l        OUS	:X  a*  [        R                  " X#U[        R                  S9U l        OkUS
:X  a4  [        R                  " US-  US-  U[        R                  S9S-  U l        O1US:X  a  [        U5      U l        O[        U SU R                    35      eSU R                  -
  U l        [        R$                  " U R"                  SS9U l        [        R(                  " U R&                  5      U l        [        R(                  " SU R&                  -
  5      U l        [        R.                  " U R*                  5      [        R.                  " U R,                  5      -
  U l        SU R&                  -
  U R&                  -  S-  U l        SU l        US;  a0  US:X  a  U R7                  SS9  O[        U SU R                    35      eUS;  a0  US;   a  U R7                  SS9  O[        U SU R                    35      eUS;  a  US:X  a  [        SU SU S35      eS U l        [:        R                  " SUS-
  U[:        R                  S9S S S2   R=                  5       n[        R>                  " U5      U l         S /U-  U l!        S U l"        U RG                  U5      U l$        S U l%        S U l&        U R2                  RO                  S 5      U l        g )!Nz:Make sure to install scipy if you want to use beta sigmas.r   znOnly one of `config.use_beta_sigmas`, `config.use_exponential_sigmas`, `config.use_karras_sigmas` can be used.	dpmsolverzalgorithm_type `dpmsolver` is deprecated and will be removed in a future version. Choose from `dpmsolver++` or `sde-dpmsolver++` insteadzalgorithm_types=dpmsolver1.0.0r    r4   scaled_linear      ?r   squaredcos_cap_v2z is not implemented for r6   r   dim)rT   r7   sde-dpmsolver++deisr7   )rF   )r8   heun)logrhobh1bh2r8   )rG   r7   r[   r9   z`final_sigmas_type` z' is not supported for `algorithm_type` z$. Please choose `sigma_min` instead.cpu)(configrK   r   ImportErrorsumrJ   rI   r"   r
   r&   r'   r(   r,   linspacer0   NotImplementedError	__class__alphascumprodalphas_cumprodsqrtalpha_tsigma_tloglambda_tsigmasinit_noise_sigmar	   num_inference_stepsnpcopy
from_numpy	timestepsmodel_outputssampleget_order_list
order_list_step_index_begin_indexto)selfr<   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   deprecation_messagerx   s                             r   __init__%DPMSolverSinglestepScheduler.__init__   sG   6 ;;&&/A/C/CZ[[++T[[-O-OQUQ\Q\QnQnopstt A  [( #m17<OP$m5==IDJh&
>QY^YfYfgDJo-
C3H[chcpcpquvvDJ11,-@ADJ%7OPTP^P^O_&`aaDJJ&#mmDKKQ?zz$"5"56zz!d&9&9"9:		$,,/%))DLL2IID///43F3FF3N !$ !PP'''}'E)^,<<TUYUcUcTd*eff2266''J'?)[M9QRVR`R`Qa*bcc!CCHY]cHc&'8&99`ao`p  qU  V 
 $( KK#6#:<OWYWaWabcgegcghmmo	)))4"Vl2--.AB kknnU+r   rt   returnc                 @   UnU R                   R                  nUS:  a  [        S5      eU R                   R                  (       a  US:X  aH  US-  S:X  a  / SQUS-  S-
  -  SS/-   S/-   nOUS-  S:X  a  / SQUS-  -  S/-   nO|/ SQUS-  -  SS/-   nOlUS:X  a+  US-  S:X  a  SS/US-  S-
  -  SS/-   nOJSS/US-  -  S/-   nO;US:X  a  S/U-  nO.US:X  a  / SQUS-  -  nOUS:X  a  SS/US-  -  nOUS:X  a  S/U-  nU R                   R                  S:X  a  SWS'   W$ )	z
Computes the solver order at each time step.

Args:
    num_inference_steps (`int`):
        The number of diffusion steps used when generating samples with a pre-trained model.
   z,Order > 3 is not supported by this schedulerr   )r   r   r   r   r   r9   rb   )rd   rA   r"   rH   rN   )r   rt   stepsorderorderss        r   r{   +DPMSolverSinglestepScheduler.get_order_list   s`    $((19KLL;;((z19>&%1*q.9QFBaSHFQY!^&%1*5;F&%1*5A>F!19>VuzA~6!Q?FVuz2aS8F!uz"eqj1!Q5A:.!u;;((F2F2Jr   c                     U R                   $ )zW
The index counter for current timestep. It will increase 1 after each scheduler step.
)r}   r   s    r   
step_index'DPMSolverSinglestepScheduler.step_index  s    
 r   c                     U R                   $ )za
The index for the first timestep. It should be set from pipeline with `set_begin_index` method.
r~   r   s    r   begin_index(DPMSolverSinglestepScheduler.begin_index  s    
    r   r   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.
Nr   )r   r   s     r   set_begin_index,DPMSolverSinglestepScheduler.set_begin_index$  s
     (r   devicemurx   c           	         Ub\  U R                   R                  (       a  U R                   R                  S:X  d   e[        R                  " U5      U R                   l        Uc  Uc  [        S5      eUb  Ub  [        S5      eUb&  U R                   R                  (       a  [        S5      eUb&  U R                   R                  (       a  [        S5      eUb&  U R                   R                  (       a  [        S5      eU=(       d    [        U5      nXl        Ub4  [        R                  " U5      R                  [        R                  5      nO[        R                   " [        R"                  " U R$                  S/5      U R                   R&                  5      nUR)                  5       n[        R*                  " SU R                   R,                  S-
  U-
  US-   5      R/                  5       SSS	2   SS	 R1                  5       R                  [        R                  5      n[        R                  " SU R2                  -
  U R2                  -  S
-  5      n[        R4                  " U5      nU R                   R                  (       ax  [        R"                  " U5      R1                  5       nU R7                  XaS9n[        R                  " U Vs/ s H  oR9                  X5      PM     sn5      R/                  5       nGOU R                   R                  (       aj  [        R"                  " U5      R1                  5       nU R;                  XaS9n[        R                  " U Vs/ s H  oR9                  X5      PM     sn5      nGOU R                   R                  (       aj  [        R"                  " U5      R1                  5       nU R=                  XaS9n[        R                  " U Vs/ s H  oR9                  X5      PM     sn5      nGOU R                   R>                  (       a  [        R*                  " SSU R                   R,                  -  US-   5      n	SU	-
  n[        R"                  " U R                   R
                  U-  SU R                   R
                  S-
  U-  -   -  5      SS	 R1                  5       nX`R                   R,                  -  R1                  5       nO6[        R@                  " U[        RB                  " S[        U5      5      U5      nU R                   RD                  S:X  a&  SU R2                  S   -
  U R2                  S   -  S
-  n
O?U R                   RD                  S:X  a  Sn
O"[        SU R                   RD                   35      e[        RF                  " Xj//5      R                  [        RH                  5      n[        RJ                  " U5      RM                  US9U l'        [        RJ                  " U5      RM                  U[        R                  S9U l(        S/U R                   RR                  -  U l*        SU l+        U R                   RX                  (       d@  XR                   RR                  -  S:w  a$  [Z        R]                  S5        U R_                  SS9  U R                   RX                  (       d>  U R                   RD                  S:X  a$  [Z        R]                  S5        U R_                  SS9  U Ra                  U5      U l1        SU l2        SU l3        U RN                  RM                  S5      U l'        gs  snf s  snf s  snf )a  
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.
    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 schedule is used. If `timesteps` is
        passed, `num_inference_steps` must be `None`.
Nr;   z?Must pass exactly one of  `num_inference_steps` or `timesteps`.z<Cannot use `timesteps` when `config.use_karras_sigmas=True`.zCCannot set `timesteps` with `config.use_exponential_sigmas = True`.z<Cannot set `timesteps` with `config.use_beta_sigmas = True`.r   r   rb   rW   )	in_sigmasrt   r6   	sigma_minr9   zC `final_sigmas_type` must be one of `sigma_min` or `zero`, but got )r   r   r!   zChanging scheduler {self.config} to have `lower_order_final` set to True to handle uneven amount of inference steps. Please make sure to always use an even number of `num_inference steps when using `lower_order_final=False`.T)rH   z `last_sigmas_type='zero'` is not supported for `lower_order_final=False`. Changing scheduler {self.config} to have `lower_order_final` set to True.rc   )4rd   rQ   rR   ru   r   rM   r"   rI   rJ   rK   lenrt   arrayastypeint64r&   searchsortedfliprq   rO   itemrg   r<   roundrv   rl   rp   _convert_to_karras_sigma_to_t_convert_to_exponential_convert_to_betarL   interparangerN   concatenater(   rw   r   rr   rx   rA   ry   rz   rH   loggerwarningr	   r{   r|   r}   r~   )r   rt   r   r   rx   clipped_idxrr   
log_sigmassigmarj   
sigma_lasts              r   set_timesteps*DPMSolverSinglestepScheduler.set_timesteps.  s   ( >;;338S8SWd8ddd%'VVBZDKK"&9+<^__*y/D^__ T[[%B%B[\\ T[[%G%Gbcc T[[%@%@[\\1CS^#6  +22288<I  ,,UZZs-KT[[MkMklK%**,KAt{{>>B[PRehiRij2"$!	  A 3 33t7J7JJsRSVVF^
;;((WWV_))+F,,v,gFSY!ZSY%"2"25"ESY!Z[aacI[[//WWV_))+F11F1lFSY!ZSY%"2"25"ESY!Z[I[[((WWV_))+F**V*eFSY!ZSY%"2"25"ESY!Z[I[[(([[A(G(G$GI\_`I`aF6\FWWT[[33f<T[[E[E[^_E_ciDi@ijklomopuuwF++"A"AAGGIIYYy"))As6{*CVLF;;((K7t221559L9LQ9OOTWWJ[[**f4JUVZVaVaVsVsUtu   67>>rzzJ&&v.111@)))477vU[[7Y"Vdkk&>&>>{{,,1D{{G_G_1_cd1dNN s ##d#;{{,,1N1NRX1XNN g ##d#;--.AB   kknnU+e "[ "[ "[s   ]]
#]rz   c                 X   UR                   nUR                  tp4nU[        R                  [        R                  4;  a  UR                  5       nUR                  X4[        R                  " U5      -  5      nUR                  5       n[        R                  " X`R                  R                  SS9n[        R                  " USU R                  R                  S9nUR                  S5      n[        R                  " X* U5      U-  nUR                  " X4/UQ76 nUR!                  U5      nU$ )aC  
"Dynamic thresholding: At each sampling step we set s to a certain percentile absolute pixel value in xt0 (the
prediction of x_0 at timestep t), and if s > 1, then we threshold xt0 to the range [-s, s] and then divide by
s. Dynamic thresholding pushes saturated pixels (those near -1 and 1) inwards, thereby actively preventing
pixels from saturation at each step. We find that dynamic thresholding results in significantly better
photorealism as well as better image-text alignment, especially when using very large guidance weights."

https://huggingface.co/papers/2205.11487
r   rY   )r%   max)r!   shaper&   r(   float64floatreshaperu   prodabsquantilerd   rD   clamprE   	unsqueezer   )r   rz   r!   
batch_sizechannelsremaining_dims
abs_sampless           r   _threshold_sample.DPMSolverSinglestepScheduler._threshold_sample  s     06-
~66\\^F 
rww~7N,NOZZ\
NN:{{'M'MSTUKK1$++66
 KKNVR+a/
F~F5!r   c                    [         R                  " [         R                  " US5      5      nX2S S 2[         R                  4   -
  n[         R                  " US:  SS9R                  SS9R                  UR                  S   S-
  S9nUS-   nX%   nX&   nXs-
  Xx-
  -  n	[         R                  " U	SS5      n	SU	-
  U-  X-  -   n
U
R                  UR                  5      n
U
$ )Ng|=r   )axisr   )r   r   )	ru   rp   maximumnewaxiscumsumargmaxclipr   r   )r   r   r   	log_sigmadistslow_idxhigh_idxlowhighwr   s              r   r   (DPMSolverSinglestepScheduler._sigma_to_t  s    FF2::eU34	 q"**}55 ))UaZq188a8@EE*JZJZ[\J]`aJaEbQ;!# _,GGAq! Ug,IIekk"r   c                 v    U R                   R                  (       a
  SU-
  nUnX#4$ SUS-  S-   S-  -  nX-  nX#4$ )Nr   r   rW   )rd   rL   )r   r   rn   ro   s       r   _sigma_to_alpha_sigma_t4DPMSolverSinglestepScheduler._sigma_to_alpha_sigma_t  sQ    ;;&&%iGG
  E1HqLS01GoGr   r   c                    [        U R                  S5      (       a  U R                  R                  nOSn[        U R                  S5      (       a  U R                  R                  nOSnUb  UOUS   R	                  5       nUb  UOUS   R	                  5       nSn[
        R                  " SSU5      nUSU-  -  nUSU-  -  nXXx-
  -  -   U-  n	U	$ )z6Constructs the noise schedule of Karras et al. (2022).r   N	sigma_maxrb   r   g      @r   )hasattrrd   r   r   r   ru   rg   )
r   r   rt   r   r   rhorampmin_inv_rhomax_inv_rhorr   s
             r   r   /DPMSolverSinglestepScheduler._convert_to_karras  s    
 4;;,,--II4;;,,--II!*!6IIbM<N<N<P	!*!6IIaL<M<M<O	{{1a!45AG,AG,(A BBsJr   c                    [        U R                  S5      (       a  U R                  R                  nOSn[        U R                  S5      (       a  U R                  R                  nOSnUb  UOUS   R	                  5       nUb  UOUS   R	                  5       n[
        R                  " [
        R                  " [        R                  " U5      [        R                  " U5      U5      5      nU$ )z)Constructs an exponential noise schedule.r   Nr   rb   r   )
r   rd   r   r   r   ru   r   rg   r   rp   )r   r   rt   r   r   rr   s         r   r   4DPMSolverSinglestepScheduler._convert_to_exponential  s    
 4;;,,--II4;;,,--II!*!6IIbM<N<N<P	!*!6IIaL<M<M<O	DHHY$7)9LNabcr   alphabetac           
      J   [        U R                  S5      (       a  U R                  R                  nOSn[        U R                  S5      (       a  U R                  R                  nOSnUb  UOUS   R	                  5       nUb  UOUS   R	                  5       n[
        R                  " S[
        R                  " SSU5      -
   Vs/ s H-  n[        R                  R                  R                  XsU5      PM/     sn Vs/ s H  nXXXe-
  -  -   PM     sn5      n	U	$ s  snf s  snf )zJFrom "Beta Sampling is All You Need" [arXiv:2407.12173] (Lee et. al, 2024)r   Nr   rb   r   r   )r   rd   r   r   r   ru   r   rg   scipystatsr   ppf)
r   r   rt   r   r   r   r   timestepr   rr   s
             r   r   -DPMSolverSinglestepScheduler._convert_to_beta  s    4;;,,--II4;;,,--II!*!6IIbM<N<N<P	!*!6IIaL<M<M<O	
 %&Aq:M(N$N$N KK$$(($?$NC I$9:;
 s   4D?D rz   model_outputc                   [        U5      S:  a  US   OUR                  SS5      nUc   [        U5      S:  a  US   nO[        S5      eUb  [        SSS5        U R                  R
                  S	;   Gao  U R                  R                  S
:X  a\  U R                  R                  S;   a  USS2SS24   nU R                  U R                     nU R                  U5      u  pxX(U-  -
  U-  n	OU R                  R                  S:X  a  Un	OU R                  R                  S:X  a6  U R                  U R                     nU R                  U5      u  pxXr-  X-  -
  n	O^U R                  R                  S:X  a!  U R                  U R                     nX(U-  -
  n	O#[        SU R                  R                   S35      eU R                  R                  (       a  U R                  U	5      n	U	$ U R                  R
                  S:X  Gaf  U R                  R                  S
:X  a)  U R                  R                  S;   a  USS2SS24   n
OUn
OU R                  R                  S:X  a7  U R                  U R                     nU R                  U5      u  pxX'U-  -
  U-  n
OsU R                  R                  S:X  a6  U R                  U R                     nU R                  U5      u  pxXq-  X-  -   n
O#[        SU R                  R                   S35      eU R                  R                  (       aB  U R                  U   U R                  U   pX(U
-  -
  U-  n	U R                  U	5      n	X'U	-  -
  U-  n
U
$ g)a  
Convert the model output to the corresponding type the DPMSolver/DPMSolver++ algorithm needs. DPM-Solver is
designed to discretize an integral of the noise prediction model, and DPM-Solver++ is designed to discretize an
integral of the data prediction model.

<Tip>

The algorithm and model type are decoupled. You can use either DPMSolver or DPMSolver++ for both noise
prediction and data prediction models.

</Tip>

Args:
    model_output (`torch.Tensor`):
        The direct output from the learned diffusion model.
    sample (`torch.Tensor`):
        A current instance of a sample created by the diffusion process.

Returns:
    `torch.Tensor`:
        The converted model output.
r   r   Nr   /missing `sample` as a required keyword argumentrx   rU   Passing `timesteps` is deprecated and has no effect as model output conversion is now handled via an internal counter `self.step_index`ra   r5   )learnedlearned_ranger   rz   v_predictionflow_predictionzprediction_type given as zo must be one of `epsilon`, `sample`, `v_prediction`, or `flow_prediction` for the DPMSolverSinglestepScheduler.rT   z\ must be one of `epsilon`, `sample`, or `v_prediction` for the DPMSolverSinglestepScheduler.)r   popr"   r
   rd   rF   rB   rP   rr   r   r   rC   r   rn   ro   )r   r   rz   argskwargsr   r   rn   ro   x0_predr5   s              r   convert_model_output1DPMSolverSinglestepScheduler.convert_model_output/  s8   : "$i!m47J1M>4y1}a !RSS Z ;;%%)KK{{**i7;;,,0LL#/2A2#6LDOO4#'#?#?#F !l$::gE,,8&,,>DOO4#'#?#?#F !*W-CC,,0AA++doo6 \#99 /0K0K/L Ma a 
 {{''009N [['';6{{**i7;;,,0LL*1bqb51G*G,,8DOO4#'#?#?#F !l$::gE,,>DOO4#'#?#?#F !073CC /0K0K/L ML L 
 {{''#'<<#94<<;Q!g$55@009!g$55@N7 7r   rz   noiser   c          	         [        U5      S:  a  US   OUR                  SS5      n[        U5      S:  a  US   OUR                  SS5      nUc   [        U5      S:  a  US   nO[        S5      eUb  [        SS	S
5        Ub  [        SS	S5        U R                  U R
                  S-      U R                  U R
                     pU R                  U5      u  pU R                  U	5      u  p[        R                  " U
5      [        R                  " U5      -
  n[        R                  " U5      [        R                  " U	5      -
  nX-
  nU R                  R                  S:X  a*  X-  U-  U
[        R                  " U* 5      S-
  -  U-  -
  nU$ U R                  R                  S:X  a)  X-  U-  U[        R                  " U5      S-
  -  U-  -
  nU$ U R                  R                  S:X  a~  Uc   eX-  [        R                  " U* 5      -  U-  U
S[        R                  " SU-  5      -
  -  U-  -   U[        R                  " S[        R                  " SU-  5      -
  5      -  U-  -   nW$ )a
  
One step for the first-order DPMSolver (equivalent to DDIM).

Args:
    model_output (`torch.Tensor`):
        The direct output from the learned diffusion model.
    timestep (`int`):
        The current discrete timestep in the diffusion chain.
    prev_timestep (`int`):
        The previous discrete timestep in the diffusion chain.
    sample (`torch.Tensor`):
        A current instance of a sample created by the diffusion process.

Returns:
    `torch.Tensor`:
        The sample tensor at the previous timestep.
r   r   Nr   prev_timestepr   r   rx   rU   r   Passing `prev_timestep` is deprecated and has no effect as model output conversion is now handled via an internal counter `self.step_index`r7   r6   rT   r[          )r   r   r"   r
   rr   r   r   r&   rp   rd   rF   r   rm   )r   r   rz   r   r   r   r   r   ro   sigma_srn   alpha_srq   lambda_shx_ts                   r   dpm_solver_first_order_update:DPMSolverSinglestepScheduler.dpm_solver_first_order_update  sR   2 "$i!m47J1M#&t9q=QfjjRV6W>4y1}a !RSS Z $ ^
  ;;t':;T[[=Y77@77@99W%		'(::99W%		'(::;;%%6$.'UYYr]S=P2QUa1aaC 
 [['';6$.'UYYq\C=O2PT`1``C 
 [[''+<<$$$"UYYr]2f<a%))D1H"556,FGEJJsUYYrAv->'>??%GH 
 
r   model_output_listc          	      p
   [        U5      S:  a  US   OUR                  SS5      n[        U5      S:  a  US   OUR                  SS5      nUc   [        U5      S:  a  US   nO[        S5      eUb  [        SSS	5        Ub  [        SSS
5        U R                  U R
                  S-      U R                  U R
                     U R                  U R
                  S-
     pnU R                  U5      u  pU R                  U	5      u  pU R                  U
5      u  p[        R                  " U5      [        R                  " U5      -
  n[        R                  " U5      [        R                  " U	5      -
  n[        R                  " U5      [        R                  " U
5      -
  nUS   US   nnUU-
  UU-
  nnUU-  nUSU-  UU-
  -  nnU R                  R                  S:X  a  U R                  R                  S:X  aN  X-  U-  U[        R                  " U* 5      S-
  -  U-  -
  SU[        R                  " U* 5      S-
  -  -  U-  -
  nU$ U R                  R                  S:X  aO  X-  U-  U[        R                  " U* 5      S-
  -  U-  -
  U[        R                  " U* 5      S-
  U-  S-   -  U-  -   nW$ U R                  R                  S:X  a  U R                  R                  S:X  aL  X-  U-  U[        R                  " U5      S-
  -  U-  -
  SU[        R                  " U5      S-
  -  -  U-  -
  nU$ U R                  R                  S:X  aM  X-  U-  U[        R                  " U5      S-
  -  U-  -
  U[        R                  " U5      S-
  U-  S-
  -  U-  -
  nW$ U R                  R                  S:X  Ga  Uc   eU R                  R                  S:X  a  X-  [        R                  " U* 5      -  U-  US[        R                  " SU-  5      -
  -  U-  -   SUS[        R                  " SU-  5      -
  -  -  U-  -   U[        R                  " S[        R                  " SU-  5      -
  5      -  U-  -   nU$ U R                  R                  S:X  a  X-  [        R                  " U* 5      -  U-  US[        R                  " SU-  5      -
  -  U-  -   US[        R                  " SU-  5      -
  SU-  -  S-   -  U-  -   U[        R                  " S[        R                  " SU-  5      -
  5      -  U-  -   nW$ )a  
One step for the second-order singlestep DPMSolver that computes the solution at time `prev_timestep` from the
time `timestep_list[-2]`.

Args:
    model_output_list (`List[torch.Tensor]`):
        The direct outputs from learned diffusion model at current and latter timesteps.
    timestep (`int`):
        The current and latter discrete timestep in the diffusion chain.
    prev_timestep (`int`):
        The previous discrete timestep in the diffusion chain.
    sample (`torch.Tensor`):
        A current instance of a sample created by the diffusion process.

Returns:
    `torch.Tensor`:
        The sample tensor at the previous timestep.
r   timestep_listNr   r   r   r   rU   Passing `timestep_list` is deprecated and has no effect as model output conversion is now handled via an internal counter `self.step_index`r   rb   r   r6   r7   r8   rW   r]   rT   r[   r   r   r   r"   r
   rr   r   r   r&   rp   rd   rF   rG   r   rm   )r   r  rz   r   r   r   r  r   ro   sigma_s0sigma_s1rn   alpha_s0alpha_s1rq   	lambda_s0	lambda_s1m0m1r  h_0r0D0D1r  s                            r   )singlestep_dpm_solver_second_order_updateFDPMSolverSinglestepScheduler.singlestep_dpm_solver_second_order_update  sG   4 $'t9q=QfjjRV6W#&t9q=QfjjRV6W>4y1}a !RSS$ ^ $ ^ KK!+,KK(KK!+, $  77@!99(C!99(C99W%		'(::IIh'%))H*==	IIh'%))H*==	"2&(9"(=BI%y9'<31WcBh27+B;;%%6{{&&*4'61%))QB-#"56"<=W		1"(;<=BC R 
I ((F2'61%))QB-#"56"<=599aR=3#6!";c"ABbHI F 
= [['';6{{&&*4'61%))A,"45;<W		!s(:;<rAB 6 
- ((F2'61%))A,"45;<599Q<##5":S"@ARGH * 
! [[''+<<$$${{&&*4'%))QB-76A!eiiq&9"9:b@AWEIIdQh,?(?@ABFG 

3261B+B CCeKL  
 ((F2'%))QB-76A!eiiq&9"9:b@A34!8)<#<"JS"PQUWWX 

3261B+B CCeKL  
r   c          	         [        U5      S:  a  US   OUR                  SS5      n[        U5      S:  a  US   OUR                  SS5      nUc   [        U5      S:  a  US   nO[        S5      eUb  [        SSS	5        Ub  [        SSS
5        U R                  U R
                  S-      U R                  U R
                     U R                  U R
                  S-
     U R                  U R
                  S-
     4u  ppU R                  U5      u  pU R                  U	5      u  pU R                  U
5      u  pU R                  U5      u  p[        R                  " U5      [        R                  " U5      -
  n[        R                  " U5      [        R                  " U	5      -
  n[        R                  " U5      [        R                  " U
5      -
  n[        R                  " U5      [        R                  " U5      -
  nUS   US   US   nnnUU-
  UU-
  UU-
  nnnUU-  UU-  nnUnSU-  UU-
  -  SU-  UU-
  -  nnUU-  UU-  -
  UU-
  -  nSUU-
  -  UU-
  -  n U R                  R                  S:X  Ga  U R                  R                  S:X  aQ  X-  U-  U[        R                  " U* 5      S-
  -  U-  -
  U[        R                  " U* 5      S-
  U-  S-   -  U-  -   n!U!$ U R                  R                  S:X  a|  X-  U-  U[        R                  " U* 5      S-
  -  U-  -
  U[        R                  " U* 5      S-
  U-  S-   -  U-  -   U[        R                  " U* 5      S-
  U-   US-  -  S-
  -  U -  -
  n!W!$ U R                  R                  S:X  a  U R                  R                  S:X  aO  X-  U-  U[        R                  " U5      S-
  -  U-  -
  U[        R                  " U5      S-
  U-  S-
  -  U-  -
  n!U!$ U R                  R                  S:X  ay  X-  U-  U[        R                  " U5      S-
  -  U-  -
  U[        R                  " U5      S-
  U-  S-
  -  U-  -
  U[        R                  " U5      S-
  U-
  US-  -  S-
  -  U -  -
  n!W!$ U R                  R                  S:X  Ga  Uc   eU R                  R                  S:X  a  X-  [        R                  " U* 5      -  U-  US[        R                  " SU-  5      -
  -  U-  -   US[        R                  " SU-  5      -
  SU-  -  S-   -  U-  -   U[        R                  " S[        R                  " SU-  5      -
  5      -  U-  -   n!U!$ U R                  R                  S:X  a  X-  [        R                  " U* 5      -  U-  US[        R                  " SU-  5      -
  -  U-  -   US[        R                  " SU-  5      -
  SU-  -  S-   -  U-  -   US[        R                  " SU-  5      -
  SU-  -   SU-  S-  -  S-
  -  U -  -   U[        R                  " S[        R                  " SU-  5      -
  5      -  U-  -   n!W!$ )a  
One step for the third-order singlestep DPMSolver that computes the solution at time `prev_timestep` from the
time `timestep_list[-3]`.

Args:
    model_output_list (`List[torch.Tensor]`):
        The direct outputs from learned diffusion model at current and latter timesteps.
    timestep (`int`):
        The current and latter discrete timestep in the diffusion chain.
    prev_timestep (`int`):
        The previous discrete timestep in the diffusion chain.
    sample (`torch.Tensor`):
        A current instance of a sample created by diffusion process.

Returns:
    `torch.Tensor`:
        The sample tensor at the previous timestep.
r   r  Nr   r   r   r   rU   r	  r   rb   r   r6   g       @r7   r8   r]   rW   rT   r[   r   r
  )"r   r  rz   r   r   r   r  r   ro   r  r  sigma_s2rn   r  r  alpha_s2rq   r  r  	lambda_s2r  r  m2r  r  h_1r  r1r  D1_0D1_1r  D2r  s"                                     r   (singlestep_dpm_solver_third_order_updateEDPMSolverSinglestepScheduler.singlestep_dpm_solver_third_order_updateB  s   6 $'t9q=QfjjRV6W#&t9q=QfjjRV6W>4y1}a !RSS$ ^ $ ^ KK!+,KK(KK!+,KK!+,	1
-8  77@!99(C!99(C!99(C99W%		'(::IIh'%))H*==	IIh'%))H*==	IIh'%))H*==	&r*,=b,ACTUWCXB*I	,A9yCX3q#'BBh27+cBh27-Cd4i"t)#R0D4K BG,;;%%6{{&&*4'61%))QB-#"56"<=599aR=3#6!";c"ABdJK X 
O ((F2'61%))QB-#"56"<=599aR=3#6!";c"ABbHI 599aR=3#6#:ad"BS"HIROP L 
A [['';6{{&&*4'61%))A,"45;<599Q<##5":S"@ATIJ : 
1 ((F2'61%))A,"45;<599Q<##5":S"@ARGH 599Q<##5#9QT"AC"GHBNO . 
# [[''+<<$$${{&&*4'%))QB-76A#		$((;";<BC34!8)<#<"JS"PQUYYZ 

3261B+B CCeKL  
 ((F2'%))QB-76A#		$((;";<BC34!8)<#<"JS"PQUWWX 34!8)<#<q#IdUVh[\_"\_b"bcgiij 

3261B+B CCeK	L  
r   rz   r   r   r   c                   [        U5      S:  a  US   OUR                  SS5      n[        U5      S:  a  US   OUR                  SS5      nUc   [        U5      S:  a  US   nO[        S5      eUc   [        U5      S:  a  US   nO[        S	5      eUb  [        SS
S5        Ub  [        SS
S5        US:X  a  U R	                  US   X$S9$ US:X  a  U R                  XUS9$ US:X  a  U R                  XUS9$ [        SU 35      e)a]  
One step for the singlestep DPMSolver.

Args:
    model_output_list (`List[torch.Tensor]`):
        The direct outputs from learned diffusion model at current and latter timesteps.
    timestep (`int`):
        The current and latter discrete timestep in the diffusion chain.
    prev_timestep (`int`):
        The previous discrete timestep in the diffusion chain.
    sample (`torch.Tensor`):
        A current instance of a sample created by diffusion process.
    order (`int`):
        The solver order at this step.

Returns:
    `torch.Tensor`:
        The sample tensor at the previous timestep.
r   r  Nr   r   r   r   r   z.missing `order` as a required keyword argumentrU   r	  r   rb   r   zOrder must be 1, 2, 3, got )r   r   r"   r
   r  r  r$  )	r   r  rz   r   r   r   r   r  r   s	            r   singlestep_dpm_solver_update9DPMSolverSinglestepScheduler.singlestep_dpm_solver_update  s=   8 $'t9q=QfjjRV6W#&t9q=QfjjRV6W>4y1}a !RSS=4y1}Q !QRR$ ^ $ ^ A:556G6KTZ5hhaZAABSjoAppaZ@@ARin@oo:5'BCCr   c                    Uc  U R                   nX!:H  R                  5       n[        U5      S:X  a  [        U R                   5      S-
  nU$ [        U5      S:  a  US   R                  5       nU$ US   R                  5       nU$ )Nr   r   )rx   nonzeror   r   )r   r   schedule_timestepsindex_candidatesr   s        r   index_for_timestep/DPMSolverSinglestepScheduler.index_for_timestep  s    %!%.:CCE A%T^^,q0J  !"Q&)!,113J  *!,113J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)z6
Initialize the step_index counter for the scheduler.
N)
r   
isinstancer&   Tensorr   rx   r   r.  r}   r~   )r   r   s     r   _init_step_index-DPMSolverSinglestepScheduler._init_step_index  sZ    
 #(ELL11#;;t~~'<'<=#66x@D#00Dr   r   return_dictc                    U R                   c  [        S5      eU R                  c  U R                  U5        U R	                  XS9n[        U R                  R                  S-
  5       H"  nU R                  US-      U R                  U'   M$     XR                  S'   U R                  R                  S:X  a*  [        UR                  XAR                  UR                  S9nOSnU R                  U R                     nU R                  U*    c  US-  nU R                  U*    c  M  US:X  a  X0l        U R!                  U R                  U R                  XS9n	U =R"                  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 sample with
the singlestep DPMSolver.

Args:
    model_output (`torch.Tensor`):
        The direct output from learned diffusion model.
    timestep (`int`):
        The current discrete timestep in the diffusion chain.
    sample (`torch.Tensor`):
        A current instance of a sample created by the diffusion process.
    return_dict (`bool`):
        Whether or not to return a [`~schedulers.scheduling_utils.SchedulerOutput`] or `tuple`.

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

NzaNumber of inference steps is 'None', you need to run 'set_timesteps' after creating the schedulerr   r   rb   r[   )	generatorr   r!   r&  )prev_sample)rt   r"   r   r3  r   r#   rd   rA   ry   rF   r   r   r   r!   r|   rz   r(  r}   r   )
r   r   r   rz   r7  r5  r-   r   r   r8  s
             r   step!DPMSolverSinglestepScheduler.step  sx   8 ##+s  ??"!!(+000Mt{{//!34A$($6$6q1u$=Dq! 5!-2;;%%):: ""i@S@S[g[m[mE E0   %(0QJE   %(0 A: K77t{{% 8 

 	A>!;77r   c                     U$ )z
Ensures interchangeability with schedulers that need to scale the denoising model input depending on the
current timestep.

Args:
    sample (`torch.Tensor`):
        The input sample.

Returns:
    `torch.Tensor`:
        A scaled input sample.
 )r   rz   r   r   s       r   scale_model_input.DPMSolverSinglestepScheduler.scale_model_inputc  s	     r   original_samplesc                 *   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?  U R!                  U5      u  pX-  X-  -   nU$ s  snf )Nr   mpsr    r   rb   )rr   r   r   r!   typer&   is_floating_pointrx   r(   r   r.  r   r   flattenr   r   r   )r   r?  r   rx   rr   r,  r   step_indicesr   rn   ro   noisy_sampless               r   	add_noise&DPMSolverSinglestepScheduler.add_noises  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%;%;!<<  77>2W_D _s   Hc                 .    U R                   R                  $ N)rd   r<   r   s    r   __len__$DPMSolverSinglestepScheduler.__len__  s    {{...r   )r~   r}   rn   rj   rl   r,   rs   rq   ry   rt   r|   rz   ro   rr   rx   )r   )NNNN)333333?rM  rJ  )NT)2__name__
__module____qualname____firstlineno____doc__r   name_compatiblesr   r	   r   intstrr   ru   ndarrayboolr   r   r{   propertyr   r   r   r   r&   r   r   r2  r   r   r   r   r   r   r   r  r  r$  r(  r.  r3  r   r   r9  r=  	IntTensorrG  rK  __static_attributes__).0es   00r   r2   r2   P   sM   >@ %>>$=qFF$=>LE $("%.2(",1"%+%"',116*/*/&)+1%*5\M'+%*,1V, V, V, 	V,
 V,  

+V, V, V, V, %*V,  V, V, V,  V, $D>V,  !)!V," "$#V,$ "$%V,& UO'V,( $C=)V,* "+V,,  }-V,. #/V,0 1V, V,p&# &$s) &P     ! !(3 ( $(+/")-j, j, c5<<'(j, UO	j,
 DI&j,Z  D0 ELL RWR^R^ 4 TW \a\h\h . dg<?HM[`	F  $	bllb 	b 
bP  $(,>ll> 	>
 %> 
>H  $(,m-m 	m
 %m 
mf  $(,x-x 	x
 %x 
x|  $(,=D-=D 	=D
 =D %=D 
=D@(
1"  E8llE8 U\\)*E8 	E8 E8 
%	&E8N %,,   ,,  ||  ??	 
 
 D/I  ?s   M r2   )g+?r   )r   typingr   r   r   r   numpyru   r&   configuration_utilsr   r	   utilsr
   r   r   utils.torch_utilsr   scheduling_utilsr   r   r   scipy.statsr   
get_loggerrN  r   r0   r2   r<  r   r   <module>rf     sd   "  / /   A : : , X X 			H	% !)4XF/>; F/r   