
    biV                         d dl Z d dlmZmZmZmZ d dlZd dlZddl	m
Z
mZ ddlmZmZ ddlmZ ddlmZmZmZ  e       rd dlZ	 	 dd	Zd
 Z G d dee
      Zy)    N)ListOptionalTupleUnion   )ConfigMixinregister_to_config)	deprecateis_scipy_available)randn_tensor   )KarrasDiffusionSchedulersSchedulerMixinSchedulerOutputc           
      $   |dk(  rd }n|dk(  rd }nt        d|       g }t        |       D ]<  }|| z  }|dz   | z  }|j                  t        d ||       ||      z  z
  |             > t	        j
                  |t        j                        S )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                 f    t        j                  | dz   dz  t         j                  z  dz        dz  S )NgMb?gT㥛 ?r   )mathcospits    n/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/diffusers/schedulers/scheduling_dpmsolver_multistep.pyalpha_bar_fnz)betas_for_alpha_bar.<locals>.alpha_bar_fn;   s-    88QY%/$''9A=>!CC    expc                 2    t        j                  | dz        S )Ng      ()r   r   r   s    r   r   z)betas_for_alpha_bar.<locals>.alpha_bar_fn@   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*+ M((!e..S\"-R0@@@(KLM <<U]]33r   c                 (   d| z
  }t        j                  |d      }|j                         }|d   j                         }|d   j                         }||z  }||||z
  z  z  }|dz  }|dd |dd z  }t        j                  |dd |g      }d|z
  } | S )a.  
    Rescales betas to have zero terminal SNR Based on https://huggingface.co/papers/2305.08891 (Algorithm 1)


    Args:
        betas (`torch.Tensor`):
            the betas that the scheduler is being initialized with.

    Returns:
        `torch.Tensor`: rescaled betas with zero terminal SNR
          ?r   dimr   r   N)r$   cumprodsqrtclonecat)r*   alphasalphas_cumprodalphas_bar_sqrtalphas_bar_sqrt_0alphas_bar_sqrt_T
alphas_bars          r   rescale_zero_terminal_snrr>   O   s     5[F]]6q1N$))+O (*002'+113 ((O (,=@Q,QRRO !!#J^j"o-FYY
1Q01FJELr   c            :       >   e Zd ZdZeD  cg c]  }|j
                   c}} ZdZeddddddd	d
dddddd
d
d
d
d
d
dd e	d       dddd
d
dfde
de	de	dedeeej                  ee	   f      de
dedede	de	ded ed!ed"ed#ee   d$ee   d%ee   d&ee   d'ee   d(ee	   d)ee   d*e	d+ee   d,ed-e
d.ed/ed0ef8d1       Zed2        Zed3        Zd[d4e
fd5Z	 	 	 	 d\d6e
d7eeej0                  f   d8ee	   d9eee
      fd:Zd;ej4                  d<ej4                  fd=Zd> Zd? Zd@ej4                  d<ej4                  fdAZdBej4                  d<ej4                  fdCZd@ej4                  d6e
d<ej4                  fdDZ 	 d]d@ej4                  d6e
dEe	dFe	d<ej4                  f
dGZ!ddHdIej4                  d;ej4                  d<ej4                  fdJZ"dddKdIej4                  d;ej4                  dLeej4                     d<ej4                  fdMZ#dddKdNeej4                     d;ej4                  dLeej4                     d<ej4                  fdOZ$dddKdNeej4                     d;ej4                  dLeej4                     d<ej4                  fdPZ%d^dQZ&dR Z'	 	 	 d_dIej4                  dSee
ej4                  f   d;ej4                  dTeej4                     dUed<ee(e)f   fdVZ*d;ej4                  d<ej4                  fdWZ+dXej4                  dLej4                  d9ejX                  d<ej4                  fdYZ-dZ Z.yc c}} w )`DPMSolverMultistepScheduleru  
    `DPMSolverMultistepScheduler` 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), `v_prediction` (see section 2.4 of [Imagen
            Video](https://imagen.research.google/video/paper.pdf) paper), or `flow_prediction`.
        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`, `dpmsolver++`, `sde-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.
        euler_at_final (`bool`, defaults to `False`):
            Whether to use Euler's method in the final step. It is a trade-off between numerical stability and detail
            richness. This can stabilize the sampling of the SDE variant of DPMSolver for small number of inference
            steps, but sometimes may result in blurring.
        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.
        use_lu_lambdas (`bool`, *optional*, defaults to `False`):
            Whether to use the uniform-logSNR for step sizes proposed by Lu's DPM-Solver in the noise schedule during
            the sampling process. If `True`, the sigmas and time steps are determined according to a sequence of
            `lambda(t)`.
        use_flow_sigmas (`bool`, *optional*, defaults to `False`):
            Whether to use flow sigmas for step sizes in the noise schedule during the sampling process.
        flow_shift (`float`, *optional*, defaults to 1.0):
            The shift value for the timestep schedule for flow matching.
        final_sigmas_type (`str`, 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.
        timestep_spacing (`str`, defaults to `"linspace"`):
            The way the timesteps should be scaled. Refer to Table 2 of the [Common Diffusion Noise Schedules and
            Sample Steps are Flawed](https://huggingface.co/papers/2305.08891) for more information.
        steps_offset (`int`, defaults to 0):
            An offset added to the inference steps, as required by some model families.
        rescale_betas_zero_snr (`bool`, defaults to `False`):
            Whether to rescale the betas to have zero terminal SNR. This enables the model to generate very bright and
            dark samples instead of limiting it to samples with medium brightness. Loosely related to
            [`--offset_noise`](https://github.com/huggingface/diffusers/blob/74fd735eb073eb1d774b1ab4154a0876eb82f055/examples/dreambooth/train_dreambooth.py#L506).
    r   i  g-C6?g{Gz?linearNr   epsilonFgףp=
?r0   dpmsolver++midpointTzeroinflinspacer   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euler_at_finaluse_karras_sigmasuse_exponential_sigmasuse_beta_sigmasuse_lu_lambdasuse_flow_sigmas
flow_shiftfinal_sigmas_typelambda_min_clippedvariance_typetimestep_spacingsteps_offsetrescale_betas_zero_snruse_dynamic_shiftingtime_shift_typec                 X   | j                   j                  rt               st        d      t	        | j                   j                  | j                   j
                  | j                   j                  g      dkD  rt        d      |dv rd| d}t        dd|       |+t        j                  |t        j                  	      | _        n|d
k(  r-t        j                  |||t        j                  	      | _        nk|dk(  r6t        j                  |dz  |dz  |t        j                  	      dz  | _        n0|dk(  rt        |      | _        nt        | d| j                          |rt#        | j                        | _        d| j                  z
  | _        t        j&                  | j$                  d      | _        |rd| j(                  d<   t        j*                  | j(                        | _        t        j*                  d| j(                  z
        | _        t        j0                  | j,                        t        j0                  | j.                        z
  | _        d| j(                  z
  | j(                  z  dz  | _        d| _        |dvr2|dk(  r| j9                  d       nt        | d| j                          |dvr1|dv r| j9                  d       nt        | d| j                          |dvr|dk(  rt        d| d | d!      d | _        t=        j                  d|dz
  |t<        j                  	      d d d   j?                         }t        j@                  |      | _!        d g|z  | _"        d| _#        d | _$        d | _%        | j4                  jM                  d"      | _        y )#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.	dpmsolversde-dpmsolverzalgorithm_type zn is deprecated and will be removed in a future version. Choose from `dpmsolver++` or `sde-dpmsolver++` insteadz+algorithm_types dpmsolver and sde-dpmsolver1.0.0r   rA   scaled_linear      ?r   squaredcos_cap_v2z is not implemented for r0   r   r1   g      p>r3   )rg   rC   rh   sde-dpmsolver++deisrC   )rS   )rD   heun)logrhobh1bh2rD   )rT   rC   rm   rE   z`final_sigmas_type` z' is not supported for `algorithm_type` z$. Please choose `sigma_min` instead.cpu)'configrY   r   ImportErrorsumrX   rW   r    r
   r$   r%   r&   r*   rG   r.   NotImplementedError	__class__r>   r8   r4   r9   r5   alpha_tsigma_tloglambda_tsigmasinit_noise_sigmar	   num_inference_stepsnpcopy
from_numpy	timestepsmodel_outputslower_order_nums_step_index_begin_indexto)selfrI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   rW   rX   rY   rZ   r[   r\   r]   r^   r_   r`   ra   rb   rc   rd   deprecation_messager   s                                  r   __init__z$DPMSolverMultistepScheduler.__init__   so   @ ;;&&/A/CZ[[++T[[-O-OQUQ\Q\QnQnopstt A  ;;$3N3C  Dr  #sCWNab$m5==IDJh&
H>QY^YfYfgDJo-
C3H[chcpcpquvvDJ11,-@ADJ%7OPTP^P^O_&`aa!24::>DJDJJ&#mmDKKQ?! '-D# zz$"5"56zz!d&9&9"9:		$,,/%))DLL2IID///43F3FF3N !$ !aa'''}'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 ! kknnU+r   c                     | j                   S )zg
        The index counter for current timestep. It will increase 1 after each scheduler step.
        )r   r   s    r   
step_indexz&DPMSolverMultistepScheduler.step_index3  s    
 r   c                     | j                   S )zq
        The index for the first timestep. It should be set from pipeline with `set_begin_index` method.
        r   r   s    r   begin_indexz'DPMSolverMultistepScheduler.begin_index:  s    
    r   r   c                     || _         y)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_indexz+DPMSolverMultistepScheduler.set_begin_indexA  s     (r   r   devicemur   c           	      V   |U| j                   j                  r| j                   j                  dk(  sJ t        j                  |      | j                   _        ||t        d      ||t        d      |!| j                   j                  rt        d      |!| j                   j                  rt        d      |!| j                   j                  rt        d      |!| j                   j                  rt        d      |4t        j                  |      j                  t        j                        }n<t        j                  t        j                   | j"                  d	g      | j                   j$                        }| j                   j&                  |z
  j)                         j+                         }| j                   j,                  d
k(  rat        j.                  d	|dz
  |dz         j1                         ddd   dd j3                         j                  t        j                        }nD| j                   j,                  dk(  r||dz   z  }t        j4                  d	|dz         |z  j1                         ddd   dd j3                         j                  t        j                        }|| j                   j6                  z  }n| j                   j,                  dk(  rp| j                   j&                  |z  }t        j4                  |d	|       j1                         j3                         j                  t        j                        }|dz  }n"t        | j                   j,                   d      t        j                  d| j8                  z
  | j8                  z  dz        }t        j:                  |      }	| j                   j                  rt        j                   |      j3                         }| j=                  ||      }t        j                  |D 
cg c]  }
| j?                  |
|	       c}
      }| j                   j@                  dk7  r|j1                         }n| j                   j                  rt        j                   |	j3                               }| jC                  ||      }t        j                  |      }t        j                  |D 
cg c]  }
| j?                  |
|	       c}
      }| j                   j@                  dk7  r|j1                         }n| j                   j                  rkt        j                   |      j3                         }| jE                  ||      }t        j                  |D 
cg c]  }
| j?                  |
|	       c}
      }n| j                   j                  rkt        j                   |      j3                         }| jG                  ||      }t        j                  |D 
cg c]  }
| j?                  |
|	       c}
      }n| j                   jH                  rt        j.                  dd| j                   j&                  z  |dz         }d|z
  }t        j                   | j                   j
                  |z  d| j                   j
                  dz
  |z  z   z        dd j3                         }|| j                   j&                  z  j3                         }n4t        jJ                  |t        j4                  d	tM        |            |      }| j                   jN                  dk(  r&d| j8                  d	   z
  | j8                  d	   z  dz  }n>| j                   jN                  dk(  rd	}n"t        d| j                   jN                         t        jP                  ||gg      j                  t        jR                        }t        jT                  |      | _+        t        jT                  |      jY                  |t        j                        | _-        tM        |      | _.        dg| j                   j^                  z  | _0        d	| _1        d| _2        d| _3        | jV                  jY                  d      | _+        yc c}
w c c}
w c c}
w c c}
w )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 timesteps schedule. If `None`, timesteps will be generated
                based on the `timestep_spacing` attribute. If `timesteps` is passed, `num_inference_steps` and `sigmas`
                must be `None`, and `timestep_spacing` attribute will be ignored.
        NrH   z>Must pass exactly one of `num_inference_steps` or `timesteps`.zACan only pass one of `num_inference_steps` or `custom_timesteps`.z=Cannot use `timesteps` with `config.use_karras_sigmas = True`z:Cannot use `timesteps` with `config.use_lu_lambdas = True`zCCannot set `timesteps` with `config.use_exponential_sigmas = True`.z<Cannot set `timesteps` with `config.use_beta_sigmas = True`.r   rG   r   r3   leadingtrailingzY is not supported. Please make sure to choose one of 'linspace', 'leading' or 'trailing'.rk   )	in_sigmasr   rl   )
in_lambdasr   r0   	sigma_minrE   zC`final_sigmas_type` must be one of 'zero', or 'sigma_min', but got r   r   rt   )4ru   rc   rd   r   r   r\   r    rW   rZ   rX   rY   arrayastypeint64r$   searchsortedflipr}   r^   rI   numpyitemr`   rG   roundr   arangera   r9   r|   _convert_to_karras_sigma_to_trL   _convert_to_lu_convert_to_exponential_convert_to_betar[   interplenr]   concatenater&   r   r~   r   r   r   rN   r   r   r   r   )r   r   r   r   r   clipped_idxlast_timestep
step_ratior~   
log_sigmassigmalambdasr8   
sigma_lasts                 r   set_timestepsz)DPMSolverMultistepScheduler.set_timestepsK  s   ( >;;338S8SWd8ddd%'VVBZDKK"&9+<]^^*y/D`aa T[[%B%B\]] T[[%?%?YZZ T[[%G%Gbcc T[[%@%@[\\ +22288<I  ,,UZZs-KT[[MkMklK"kk==KRRTZZ\M {{++z9KK=1#46IA6MNUWTrT##&B(TVVBHH%	  --:*/BQ/FG
 YYq"5"9:ZGNNPQUSUQUVWZXZ[``biijljrjrs  T[[555	--;![[<<?RR
 IImQDJJLQQSZZ[][c[cd	Q	 {{334  5N  O  A 3 33t7J7JJsRSVVF^
;;((WWV_))+F,,vSf,gFSY!Z%$"2"25*"E!Z[I{{((,??%OO-	[[''ggjoo/0G))WRe)fGVVG_FSY!Z%$"2"25*"E!Z[I{{((,??%OO-	[[//WWV_))+F11FXk1lFSY!Z%$"2"25*"E!Z[I[[((WWV_))+F**VQd*eFSY!Z%$"2"25*"E!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.)))477vU[[7Y#&y>  
KK$$% !"   kknnU+e "[ "[ "[ "[s   bbb!b&samplereturnc                 b   |j                   }|j                  ^}}}|t        j                  t        j                  fvr|j                         }|j                  ||t        j                  |      z        }|j                         }t        j                  || j                  j                  d      }t        j                  |d| j                  j                        }|j                  d      }t        j                  || |      |z  } |j                  ||g| }|j!                  |      }|S )a{  
        "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   r1   )r#   max)r   shaper$   r&   float64floatreshaper   prodabsquantileru   rQ   clamprR   	unsqueezer   )r   r   r   
batch_sizechannelsremaining_dims
abs_sampless           r   _threshold_samplez-DPMSolverMultistepScheduler._threshold_sample  s     06-
H~66\\^F 
Hrww~7N,NOZZ\
NN:t{{'M'MSTUKK1$++66
 KKNVaR+a/
HF~F5!r   c                    t        j                  t        j                  |d            }||d d t         j                  f   z
  }t        j                  |dk\  d      j                  d      j                  |j                  d   dz
        }|dz   }||   }||   }||z
  ||z
  z  }	t        j                  |	dd      }	d|	z
  |z  |	|z  z   }
|
j                  |j                        }
|
S )Ng|=r   )axisr   )r   r   )	r   r|   maximumnewaxiscumsumargmaxclipr   r   )r   r   r   	log_sigmadistslow_idxhigh_idxlowhighwr   s              r   r   z'DPMSolverMultistepScheduler._sigma_to_t  s    FF2::eU34	 Jq"**}55 ))UaZq188a8@EE*JZJZ[\J]`aJaEbQ;!(# 9_t,GGAq! UgH,IIekk"r   c                 r    | j                   j                  rd|z
  }|}||fS d|dz  dz   dz  z  }||z  }||fS )Nr   r   rk   )ru   r[   )r   r   rz   r{   s       r   _sigma_to_alpha_sigma_tz3DPMSolverMultistepScheduler._sigma_to_alpha_sigma_t  sW    ;;&&%iGG
  E1HqLS01GgoGr   r   c                    t        | j                  d      r| j                  j                  }nd}t        | j                  d      r| j                  j                  }nd}||n|d   j	                         }||n|d   j	                         }d}t        j                  dd|      }|d|z  z  }|d|z  z  }||||z
  z  z   |z  }	|	S )z6Constructs the noise schedule of Karras et al. (2022).r   N	sigma_maxr3   r   g      @r   )hasattrru   r   r   r   r   rG   )
r   r   r   r   r   rhorampmin_inv_rhomax_inv_rhor~   s
             r   r   z.DPMSolverMultistepScheduler._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,k(A BBsJr   r   c                     |d   j                         }|d   j                         }d}t        j                  dd|      }|d|z  z  }|d|z  z  }||||z
  z  z   |z  }	|	S )z2Constructs the noise schedule of Lu et al. (2022).r3   r   r0   r   )r   r   rG   )
r   r   r   
lambda_min
lambda_maxr   r   r   r   r   s
             r   r   z*DPMSolverMultistepScheduler._convert_to_lu*  sz     'rN//1
&qM..0
{{1a!45 QW- QW-{)B!CCKr   c                    t        | j                  d      r| j                  j                  }nd}t        | j                  d      r| j                  j                  }nd}||n|d   j	                         }||n|d   j	                         }t        j                  t        j                  t        j                  |      t        j                  |      |            }|S )z)Constructs an exponential noise schedule.r   Nr   r3   r   )
r   ru   r   r   r   r   r   rG   r   r|   )r   r   r   r   r   r~   s         r   r   z3DPMSolverMultistepScheduler._convert_to_exponential8  s    
 4;;,--II4;;,--II!*!6IIbM<N<N<P	!*!6IIaL<M<M<O	DHHY$7)9LNabcr   alphabetac           
      (   t        | j                  d      r| j                  j                  }nd}t        | j                  d      r| j                  j                  }nd}||n|d   j	                         }||n|d   j	                         }t        j                  dt        j                  dd|      z
  D cg c]-  }t        j                  j                  j                  |||      / c}D cg c]  }||||z
  z  z    c}      }	|	S c c}w c c}w )zJFrom "Beta Sampling is All You Need" [arXiv:2407.12173] (Lee et. al, 2024)r   Nr   r3   r   r   )r   ru   r   r   r   r   r   rG   scipystatsr   ppf)
r   r   r   r   r   r   r   timestepr   r~   s
             r   r   z,DPMSolverMultistepScheduler._convert_to_betaN  s     4;;,--II4;;,--II!*!6IIbM<N<N<P	!*!6IIaL<M<M<O	
 %&Aq:M(N$N  KK$$((5$? SI	$9:;
 s   82D
/Dr   model_outputc                   t        |      dkD  r|d   n|j                  dd      }|t        |      dkD  r|d   }nt        d      |t        ddd       | j                  j
                  d	v rj| j                  j                  d
k(  r\| j                  j                  dv r|ddddf   }| j                  | j                     }| j                  |      \  }}|||z  z
  |z  }	n| j                  j                  dk(  r|}	n| j                  j                  dk(  r9| j                  | j                     }| j                  |      \  }}||z  ||z  z
  }	n^| j                  j                  dk(  r"| j                  | j                     }|||z  z
  }	n#t        d| j                  j                   d      | j                  j                  r| j                  |	      }	|	S | j                  j
                  dv rs| j                  j                  d
k(  r'| j                  j                  dv r|ddddf   }
n|}
n| j                  j                  dk(  r9| j                  | j                     }| j                  |      \  }}|||z  z
  |z  }
nu| j                  j                  dk(  r9| j                  | j                     }| j                  |      \  }}||z  ||z  z   }
n#t        d| j                  j                   d      | j                  j                  rT| j                  | j                     }| j                  |      \  }}|||
z  z
  |z  }	| j                  |	      }	|||	z  z
  |z  }
|
S y)a0  
        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 argumentr   ri   Passing `timesteps` is deprecated and has no effect as model output conversion is now handled via an internal counter `self.step_index`rs   rB   )learnedlearned_range   r   v_predictionflow_predictionzprediction_type given as zn must be one of `epsilon`, `sample`, `v_prediction`, or `flow_prediction` for the DPMSolverMultistepScheduler.rf   z[ must be one of `epsilon`, `sample`, or `v_prediction` for the DPMSolverMultistepScheduler.)r   popr    r
   ru   rS   rO   r_   r~   r   r   rP   r   )r   r   r   argskwargsr   r   rz   r{   x0_predrB   s              r   convert_model_outputz0DPMSolverMultistepScheduler.convert_model_outputm  sb   : "$i!m47J1M>4y1}a !RSS Z ;;%%)KK{{**i7;;,,0LL#/2A2#6LDOO4#'#?#?#F !Gl$::gE,,8&,,>DOO4#'#?#?#F !F*W|-CC,,0AA++doo6 7\#99 /0K0K/L M` ` 
 {{''009N [[''+II{{**i7;;,,0LL*1bqb51G*G,,8DOO4#'#?#?#F !Gl$::gE,,>DOO4#'#?#?#F !L07V3CC /0K0K/L MK K 
 {{''DOO4#'#?#?#F !Gg$55@009!Gg$55@N9 Jr   r   noiser   c          	         t        |      dkD  r|d   n|j                  dd      }t        |      dkD  r|d   n|j                  dd      }|t        |      dkD  r|d   }nt        d      |t        dd	d
       |t        dd	d       | j                  | j
                  dz      | j                  | j
                     }	}| j                  |      \  }
}| j                  |	      \  }}	t        j                  |
      t        j                  |      z
  }t        j                  |      t        j                  |	      z
  }||z
  }| j                  j                  dk(  r*||	z  |z  |
t        j                  |       dz
  z  |z  z
  }|S | j                  j                  dk(  r)|
|z  |z  |t        j                  |      dz
  z  |z  z
  }|S | j                  j                  dk(  r||J ||	z  t        j                  |       z  |z  |
dt        j                  d|z        z
  z  |z  z   |t        j                  dt        j                  d|z        z
        z  |z  z   }|S | j                  j                  dk(  rc|J |
|z  |z  d|t        j                  |      dz
  z  z  |z  z
  |t        j                  t        j                  d|z        dz
        z  |z  z   }S )a  
        One step for the first-order DPMSolver (equivalent to DDIM).

        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 sample tensor at the previous timestep.
        r   r   Nr   prev_timestepr   r   r   ri   r   Passing `prev_timestep` is deprecated and has no effect as model output conversion is now handled via an internal counter `self.step_index`rC   r0   rg   rm          rh          @r   r   r    r
   r~   r   r   r$   r|   ru   rS   r   r5   )r   r   r   r   r   r   r   r   r{   sigma_srz   alpha_sr}   lambda_shx_ts                   r   dpm_solver_first_order_updatez9DPMSolverMultistepScheduler.dpm_solver_first_order_update  s   * "$i!m47J1M#&t9q=QfjjRV6W>4y1}a !RSS Z $ ^  ;;t':;T[[=Y77@77@99W%		'(::99W%		'(::x;;%%6W$.'UYYr]S=P2QUa1aaC" 
! [['';6W$.'UYYq\C=O2PT`1``C 
 [[''+<<$$$7"UYYr]2f<a%))D1H"556,FGEJJsUYYrAv->'>??%GH  
 [[''?:$$$7"f,EIIaL3$678<GHEJJuyyQ'7#'=>>FG 
 
r   model_output_listc          	         t        |      dkD  r|d   n|j                  dd      }t        |      dkD  r|d   n|j                  dd      }|t        |      dkD  r|d   }nt        d      |t        ddd	       |t        ddd
       | j                  | j
                  dz      | j                  | j
                     | j                  | j
                  dz
     }
}	}| j                  |      \  }}| j                  |	      \  }}	| j                  |
      \  }}
t        j                  |      t        j                  |      z
  }t        j                  |      t        j                  |	      z
  }t        j                  |      t        j                  |
      z
  }|d   |d   }}||z
  ||z
  }}||z  }|d|z  ||z
  z  }}| j                  j                  dk(  r| j                  j                  dk(  rM||	z  |z  |t        j                  |       dz
  z  |z  z
  d|t        j                  |       dz
  z  z  |z  z
  }|S | j                  j                  dk(  rN||	z  |z  |t        j                  |       dz
  z  |z  z
  |t        j                  |       dz
  |z  dz   z  |z  z   }S | j                  j                  dk(  r| j                  j                  dk(  rK||z  |z  |t        j                  |      dz
  z  |z  z
  d|t        j                  |      dz
  z  z  |z  z
  }|S | j                  j                  dk(  rL||z  |z  |t        j                  |      dz
  z  |z  z
  |t        j                  |      dz
  |z  dz
  z  |z  z
  }S | j                  j                  dk(  rv|J | j                  j                  dk(  r||	z  t        j                  |       z  |z  |dt        j                  d|z        z
  z  |z  z   d|dt        j                  d|z        z
  z  z  |z  z   |t        j                  dt        j                  d|z        z
        z  |z  z   }|S | j                  j                  dk(  r||	z  t        j                  |       z  |z  |dt        j                  d|z        z
  z  |z  z   |dt        j                  d|z        z
  d|z  z  dz   z  |z  z   |t        j                  dt        j                  d|z        z
        z  |z  z   }S | j                  j                  dk(  r=|J | j                  j                  dk(  r||z  |z  d|t        j                  |      dz
  z  z  |z  z
  |t        j                  |      dz
  z  |z  z
  |t        j                  t        j                  d|z        dz
        z  |z  z   }|S | j                  j                  dk(  r||z  |z  d|t        j                  |      dz
  z  z  |z  z
  d|t        j                  |      dz
  |z  dz
  z  z  |z  z
  |t        j                  t        j                  d|z        dz
        z  |z  z   }S )a  
        One step for the second-order multistep DPMSolver.

        Args:
            model_output_list (`List[torch.Tensor]`):
                The direct outputs from learned diffusion model at current and latter timesteps.
            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   ri   Passing `timestep_list` is deprecated and has no effect as model output conversion is now handled via an internal counter `self.step_index`r   r3   r  r0   rC   rD   rk   ro   rg   rm   r   rh   r  )r   r   r    r
   r~   r   r   r$   r|   ru   rS   rT   r   r5   )r   r
  r   r   r   r   r  r   r{   sigma_s0sigma_s1rz   alpha_s0alpha_s1r}   	lambda_s0	lambda_s1m0m1r  h_0r0D0D1r  s                            r   (multistep_dpm_solver_second_order_updatezDDPMSolverMultistepScheduler.multistep_dpm_solver_second_order_update  s   * $'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x'61%))QB-#"56"<=W		1"(;<=BC r 
i ((F2x'61%))QB-#"56"<=599aR=3#6!";c"ABbHI f 
] [['';6{{&&*4x'61%))A,"45;<W		!s(:;<rAB V 
M ((F2x'61%))A,"45;<599Q<##5":S"@ARGH J 
A [[''+<<$$${{&&*4x'%))QB-76A!eiiq&9"9:b@AWEIIdQh,?(?@ABFG 

3261B+B CCeKL : 
/ ((F2x'%))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 , 
! [[''?:$$${{&&*4x'61W		!s(:;<rAB%))A,"45;< 

599QU+;c+A BBUJK  
 ((F2x'61W		!s(:;<rABW1);q(@3(FGH2MN 

599QU+;c+A BBUJK  
r   c          	         t        |      dkD  r|d   n|j                  dd      }t        |      dkD  r|d   n|j                  dd      }|t        |      dkD  r|d   }nt        d      |t        ddd	       |t        ddd
       | j                  | j
                  dz      | j                  | j
                     | j                  | j
                  dz
     | j                  | j
                  dz
     f\  }}	}
}| j                  |      \  }}| j                  |	      \  }}	| j                  |
      \  }}
| j                  |      \  }}t        j                  |      t        j                  |      z
  }t        j                  |      t        j                  |	      z
  }t        j                  |      t        j                  |
      z
  }t        j                  |      t        j                  |      z
  }|d   |d   |d   }}}||z
  ||z
  ||z
  }}}||z  ||z  }}|}d|z  ||z
  z  d|z  ||z
  z  }}||||z   z  ||z
  z  z   }d||z   z  ||z
  z  } | j                  j                  dk(  r|||	z  |z  |t        j                  |       dz
  z  |z  z
  |t        j                  |       dz
  |z  dz   z  |z  z   |t        j                  |       dz
  |z   |dz  z  dz
  z  | z  z
  }!|!S | j                  j                  dk(  ry||z  |z  |t        j                  |      dz
  z  |z  z
  |t        j                  |      dz
  |z  dz
  z  |z  z
  |t        j                  |      dz
  |z
  |dz  z  dz
  z  | z  z
  }!|!S | j                  j                  dk(  r|J ||	z  t        j                  |       z  |z  |dt        j                  d|z        z
  z  |z  z   |dt        j                  d|z        z
  d|z  z  dz   z  |z  z   |dt        j                  d|z        z
  d|z  z
  d|z  dz  z  dz
  z  | z  z   |t        j                  dt        j                  d|z        z
        z  |z  z   }!!S )a  
        One step for the third-order multistep DPMSolver.

        Args:
            model_output_list (`List[torch.Tensor]`):
                The direct outputs from learned diffusion model at current and latter timesteps.
            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   ri   r  r   r3   r  r0   rC   rk   rg   rm   r   r  r  )"r   r
  r   r   r   r   r  r   r{   r  r  sigma_s2rz   r  r  alpha_s2r}   r  r  	lambda_s2r  r  m2r  r  h_1r  r1r  D1_0D1_1r  D2r  s"                                     r   'multistep_dpm_solver_third_order_updatezCDPMSolverMultistepScheduler.multistep_dpm_solver_third_order_update  s   , $'t9q=QfjjRV6W#&t9q=QfjjRV6W>4y1}a !RSS$ ^ $ ^ KK!+,KK(KK!+,KK!+,	1
-8X  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R27^t44R"Wo$+.;;%%6 8#v-eiimc12b89uyy!}s2a7#=>"DE uyy!}s2Q6!Q$>DEKL . 
# [['';6 8#v-eiilS01R78uyy|c1Q6<=CD uyy|c1A5A=CDJK  
 [[''+<<$$$8#eiim3v=cEIIdQh$778B>?sUYYtax%88TAXFLMQSST sUYYtax%8837BsQwSTnTWZZ[_aab EJJsUYYrAv->'>??%G	H  
r   c                    || j                   }||k(  j                         }t        |      dk(  rt        | j                         dz
  }|S t        |      dkD  r|d   j                         }|S |d   j                         }|S )Nr   r   )r   nonzeror   r   )r   r   schedule_timestepsindex_candidatesr   s        r   index_for_timestepz.DPMSolverMultistepScheduler.index_for_timestep  s    %!%.(:CCE A%T^^,q0J  !"Q&)!,113J  *!,113Jr   c                     | j                   Vt        |t        j                        r%|j	                  | j
                  j                        }| j                  |      | _        y| j                  | _        y)zF
        Initialize the step_index counter for the scheduler.
        N)
r   
isinstancer$   Tensorr   r   r   r+  r   r   )r   r   s     r   _init_step_indexz,DPMSolverMultistepScheduler._init_step_index  sW    
 #(ELL1#;;t~~'<'<=#66x@D#00Dr   r   variance_noisereturn_dictc                    | j                   t        d      | j                  | j                  |       | j                  t	        | j
                        dz
  k(  xrc | j                  j                  xsK | j                  j                  xr t	        | j
                        dk  xs | j                  j                  dk(  }| j                  t	        | j
                        dz
  k(  xr0 | j                  j                  xr t	        | j
                        dk  }| j                  ||      }t        | j                  j                  dz
        D ]!  }	| j                  |	dz      | j                  |	<   # || j                  d<   |j                  t        j                         }| j                  j"                  d	v r4|2t%        |j&                  ||j(                  t        j                   
      }
nF| j                  j"                  d	v r,|j                  |j(                  t        j                         }
nd}
| j                  j                  dk(  s| j*                  dk  s|r| j-                  |||
      }ng| j                  j                  dk(  s| j*                  dk  s|r| j/                  | j                  ||
      }n| j1                  | j                  ||
      }| j*                  | j                  j                  k  r| xj*                  dz  c_        |j                  |j2                        }| xj4                  dz  c_        |s|fS t7        |      S )a  
        Predict the sample from the previous timestep by reversing the SDE. This function propagates the sample with
        the multistep 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.
            generator (`torch.Generator`, *optional*):
                A random number generator.
            variance_noise (`torch.Tensor`):
                Alternative to generating noise with `generator` by directly providing the noise for the variance
                itself. Useful for methods such as [`LEdits++`].
            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      rE   r   r   r3   )rh   rm   )	generatorr   r   r   r   )prev_sample)r   r    r   r/  r   r   ru   rV   rU   r]   r   r!   rN   r   r   r$   r&   rS   r   r   r   r   r	  r  r&  r   r   r   )r   r   r   r   r4  r0  r1  rU   lower_order_secondr+   r   r5  s               r   stepz DPMSolverMultistepScheduler.step  s   D ##+s  ??"!!(+ "__DNN0Ca0GG 
KK&& 7--J#dnn2E2J7{{,,6 	 __DNN 3a 77wT[[=Z=Zw_bcgcqcq_ruw_w 	 00f0Mt{{//!34 	>A$($6$6q1u$=Dq!	>!-2 5==);;%%)MMR`Rh ""i@S@S[`[h[hE [[''+OO"%%\-@-@%VEE;;##q(D,A,AA,EIZ<<\RX`e<fK[[%%*d.C.Ca.GK]GGHZHZciqvGwKFFtGYGYbhpuFvK  4;;#;#;;!!Q&! "nn\%7%78 	A>!;77r   c                     |S )a?  
        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   r   r   r   s       r   scale_model_inputz-DPMSolverMultistepScheduler.scale_model_inputj  s	     r   original_samplesc                 *   | j                   j                  |j                  |j                        }|j                  j                  dk(  rvt        j                  |      ra| j                  j                  |j                  t
        j                        }|j                  |j                  t
        j                        }n@| j                  j                  |j                        }|j                  |j                        }| j                   |D cg c]  }| j                  ||       }}nG| j                  | j                  g|j                  d   z  }n| j                  g|j                  d   z  }||   j                         }t        |j                        t        |j                        k  r=|j                  d      }t        |j                        t        |j                        k  r=| j!                  |      \  }	}
|	|z  |
|z  z   }|S c c}w )Nr   mpsr   r   r3   )r~   r   r   r   typer$   is_floating_pointr   r&   r   r+  r   r   flattenr   r   r   )r   r;  r   r   r~   r)  r   step_indicesr   rz   r{   noisy_sampless               r   	add_noisez%DPMSolverMultistepScheduler.add_noisey  s    '7'>'>FVF\F\]""''50U5L5LY5W!%!2!23C3J3JRWR_R_!2!`!%5%<%<EMMRI!%!2!23C3J3J!K!%5%<%<=I #T]^qD33A7IJ^L^__( OO,yq/AAL !,,-	0BBL|$,,.%++%5%;%;!<<OOB'E %++%5%;%;!<<  77>"22Wu_D _s   Hc                 .    | j                   j                  S N)ru   rI   r   s    r   __len__z#DPMSolverMultistepScheduler.__len__  s    {{...r   )r   )NNNN)333333?rG  rE  )NNT)/__name__
__module____qualname____doc__r   name_compatiblesorderr	   r   intstrr   r   r   ndarrayr   boolr   propertyr   r   r   r$   r   r   r.  r   r   r   r   r   r   r   r   r	  r  r&  r+  r/  r   r   r7  r:  	IntTensorrC  rF  ).0es   00r   r@   r@   s   s`   Sj %>>qAFF>LE $("%BF(",1"%+%"&$,116*/).*/&)+1%*5\M'+ *',%*,;d, d, d, 	d,
 d,  bjj$u+&= >?d, d, d, d, %*d,  d, d, d,  d, d,  $D>!d," !)#d,$ "$%d,& !'d,( "$)d,* UO+d,, $C=-d,. "/d,0  }1d,2 3d,4 5d,6 !%7d,8 #9d,: ;d, d,L     ! !(3 ( $(+/")-, , c5<<'(, UO	,
 DI&,D  D. ELL RWR^R^ 2 u||  TW \a\h\h . dg<?HM[`	F  $	dlld 	d 
dT  $(,CllC 	C
 %C 
CR  $(,y-y 	y
 %y 
y~  $(,]-] 	]
 %] 
]~&
1" 15 W8llW8 U\\)*W8 	W8 !.W8 W8 
%	&W8r %,,  ,,  ||  ??	 
 
 D/e ?s   Lr@   )g+?r   )r   typingr   r   r   r   r   r   r$   configuration_utilsr   r	   utilsr
   r   utils.torch_utilsr   scheduling_utilsr   r   r   scipy.statsr   r.   r>   r@   r9  r   r   <module>r]     sT   "  / /   A 1 , X X  !)4Z!Hi/.+ i/r   