o
    ߥi6                    @   s   d dl Z d dlZd dlm  mZ g dZdd ZG dd dZdi dddd	di fd
dZ	i ddddfddZ
G dd dZdd Zdd ZdS )    N)NoiseScheduleVPmodel_wrappermodel_wrapper_guided_diffusion
DPM_Solverinterpolate_fnc                 C   s:   | dfd|jd   }|j}| || ||S )z?Index tensor using t and format the output according to x.
    r      r   )sizendimdevicetoview)tensortxshaper    r   j/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/models/multi_modal/videocomposer/dpm_solver.py_i   s   r   c                   @   sL   e Zd ZdddddejfddZdd Zd	d
 Zdd Zdd Z	dd Z
dS )r   discreteNg?g      4@c                 C   sV  |dvrt d||| _|dkr[|dur$dtd| jdd }n|dus*J dt| }t|| _d	| _t	d
d	| jd dd 
dj|d| _|
dj|d| _dS d| _|| _|| _d| _d| _t| jd	| j  tj d d	| j  tj | j | _tt| jd	| j  tj d | _|| _|dkrd| _dS d	| _dS )a  Create a wrapper class for the forward SDE (VP type).

        ***
        Update: We support discrete-time diffusion models by implementing a picewise
                linear interpolation for log_alpha_t.
                We recommend to use schedule='discrete' for the discrete-time diffusion models,
                especially for high-resolution images.
        ***

        The forward SDE ensures that the condition distribution q_{t|0}(x_t | x_0) = N ( alpha_t * x_0, sigma_t^2 * I ).
        We further define lambda_t = log(alpha_t) - log(sigma_t),
        which is the half-logSNR (described in the DPM-Solver paper).
        Therefore, we implement the functions for computing alpha_t, sigma_t and lambda_t. For t in [0, T], we have:

            log_alpha_t = self.marginal_log_mean_coeff(t)
            sigma_t = self.marginal_std(t)
            lambda_t = self.marginal_lambda(t)

        Moreover, as lambda(t) is an invertible function, we also support its inverse function:

            t = self.inverse_lambda(lambda_t)

        ===============================================================

        We support both discrete-time DPMs (trained on n = 0, 1, ..., N-1)
        and continuous-time DPMs (trained on t in [t_0, T]).

        1. For discrete-time DPMs:

            For discrete-time DPMs trained on n = 0, 1, ..., N-1,
                we convert the discrete steps to continuous time steps by:
                t_i = (i + 1) / N
            e.g. for N = 1000, we have t_0 = 1e-3 and T = t_{N-1} = 1.
            We solve the corresponding diffusion ODE from time T = 1 to time t_0 = 1e-3.

            Args:
                betas: A `torch.Tensor`. The beta array for the discrete-time DPM.
                (See the original DDPM paper for details)
                alphas_cumprod: A `torch.Tensor`. The cumprod alphas for the discrete-time DPM.
                (See the original DDPM paper for details)

            Note that we always have alphas_cumprod = cumprod(1 - betas).
            Therefore, we only need to set one of `betas` and `alphas_cumprod`.

            **Important**:  Please pay special attention for the args for `alphas_cumprod`:
                The `alphas_cumprod` is the \hat{alpha_n} arrays in the notations of DDPM. Specifically,
                DDPMs assume that
                    q_{t_n | 0}(x_{t_n} | x_0) = N ( \sqrt{\hat{alpha_n}} * x_0, (1 - \hat{alpha_n}) * I ).
                Therefore, the notation \hat{alpha_n} is different from the notation alpha_t in DPM-Solver.
                In fact, we have
                    alpha_{t_n} = \sqrt{\hat{alpha_n}},
                and
                    log(alpha_{t_n}) = 0.5 * log(\hat{alpha_n}).

        2. For continuous-time DPMs:

            We support two types of VPSDEs: linear (DDPM) and cosine (improved-DDPM).
            The hyperparameters for the noise
            schedule are the default settings in DDPM and improved-DDPM:

            Args:
                beta_min: A `float` number. The smallest beta for the linear schedule.
                beta_max: A `float` number. The largest beta for the linear schedule.
                cosine_s: A `float` number. The hyperparameter in the cosine schedule.
                cosine_beta_max: A `float` number. The hyperparameter in the cosine schedule.
                T: A `float` number. The ending time of the forward process.

        ===============================================================

        Args:
            schedule: A `str`. The noise schedule of the forward SDE.
            'discrete' for discrete-time DPMs,
                    'linear' or 'cosine' for continuous-time DPMs.
        Returns:
            A wrapper object of the forward SDE (VP type).

        ===============================================================

        Example:

        # For discrete-time DPMs, given betas (the beta array for n = 0, 1, ..., N - 1):
        >>> ns = NoiseScheduleVP('discrete', betas=betas)

        # For discrete-time DPMs, given alphas_cumprod (the \hat{alpha_n}
        # array for n = 0, 1, ..., N - 1):
        >>> ns = NoiseScheduleVP('discrete', alphas_cumprod=alphas_cumprod)

        # For continuous-time DPMs (VPSDE), linear schedule:
        >>> ns = NoiseScheduleVP('linear', continuous_beta_0=0.1, continuous_beta_1=20.)

        )r   linearcosinezZUnsupported noise schedule {}. The schedule needs to be 'discrete' or 'linear' or 'cosine'r   N      ?r   r   dim      ?g        )r   )dtype  gMb?g     8@       @r   gO@a?)
ValueErrorformatscheduletorchlogcumsumlentotal_NTlinspacereshaper   t_arraylog_alpha_arraybeta_0beta_1cosine_scosine_beta_maxmathatanpicosine_t_maxcoscosine_log_alpha_0)selfr"   betasalphas_cumprodcontinuous_beta_0continuous_beta_1r   
log_alphasr   r   r   __init__   sb   c


zNoiseScheduleVP.__init__c                    s    j dkrt|d j|j j|jdS  j dkr3d|d   j j  d|  j  S  j dkrG fd	d
}|| j	 }|S dS )zT
        Compute log(alpha_t) of a given continuous-time label t in [0, T].
        r   r   r   r   r   g      п   r   r   c                    s*   t t |  j d j  tj d S )Nr   r   )r#   r$   r5   r/   r1   r3   )sr7   r   r   <lambda>   s    z9NoiseScheduleVP.marginal_log_mean_coeff.<locals>.<lambda>N)
r"   r   r*   r+   r   r   r,   r.   r-   r6   )r7   r   log_alpha_fnlog_alpha_tr   rA   r   marginal_log_mean_coeff   s$   


z'NoiseScheduleVP.marginal_log_mean_coeffc                 C   s   t | |S )zO
        Compute alpha_t of a given continuous-time label t in [0, T].
        )r#   exprE   r7   r   r   r   r   marginal_alpha   s   zNoiseScheduleVP.marginal_alphac              	   C   s   t dt d| |  S )zO
        Compute sigma_t of a given continuous-time label t in [0, T].
        r   r   )r#   sqrtrF   rE   rG   r   r   r   marginal_std   s   zNoiseScheduleVP.marginal_stdc                 C   s.   |  |}dtdtd|   }|| S )zn
        Compute lambda_t = log(alpha_t) - log(sigma_t) of a given continuous-time label t in [0, T].
        r   r   r   )rE   r#   r$   rF   )r7   r   log_mean_coefflog_stdr   r   r   marginal_lambda   s   
zNoiseScheduleVP.marginal_lambdac                    s   j dkr2d j j  td| td| } jd | }|t| j   j j  S  j dkrjdttd|jd|  }t	|
dt j|jd	gt j|jd	g}|
d
S dtd| td| } fdd}||}|S )z`
        Compute the continuous-time label t in [0, T] of a given half-logSNR lambda_t.
        r   r   g       r   r?   r   g      r>   r   r   c                    s0   t t |  j d d j  tj  j S )Nr   r   )r#   arccosrF   r6   r/   r1   r3   )rD   rA   r   r   rB      s    z0NoiseScheduleVP.inverse_lambda.<locals>.<lambda>)r"   r.   r-   r#   	logaddexpzerosr   rI   r   r   r*   flipr,   r+   )r7   lambtmpDelta	log_alphar   t_fnr   rA   r   inverse_lambda   s2   



zNoiseScheduleVP.inverse_lambda)__name__
__module____qualname__r#   float32r=   rE   rH   rJ   rM   rX   r   r   r   r   r      s    
 r   noiseuncondr   c
              	      sj   fddd	fdd	
 fdd 
f	dd	}
	d
v s-J dv s3J |
S )a]  Create a wrapper function for the noise prediction model.

    DPM-Solver needs to solve the continuous-time diffusion ODEs.
    For DPMs trained on discrete-time labels, we need to
    firstly wrap the model function to a noise prediction model
    that accepts the continuous time as the input.

    We support four types of the diffusion model by setting `model_type`:

        1. "noise": noise prediction model. (Trained by predicting noise).

        2. "x_start": data prediction model. (Trained by predicting the data x_0 at time 0).

        3. "v": velocity prediction model. (Trained by predicting the velocity).
            The "v" prediction is derivation detailed in Appendix D of [1],
            and is used in Imagen-Video [2].

            [1] Salimans, Tim, and Jonathan Ho.
                "Progressive distillation for fast sampling of diffusion models."
                arXiv preprint arXiv:2202.00512 (2022).
            [2] Ho, Jonathan, et al. "Imagen Video: High Definition
                Video Generation with Diffusion Models."
                arXiv preprint arXiv:2210.02303 (2022).

        4. "score": marginal score function. (Trained by denoising score matching).
            Note that the score function and the noise prediction model follows a simple relationship:
            ```
                noise(x_t, t) = -sigma_t * score(x_t, t)
            ```

    We support three types of guided sampling by DPMs by setting `guidance_type`:
        1. "uncond": unconditional sampling by DPMs.
            The input `model` has the following format:
            ``
                model(x, t_input, **model_kwargs) -> noise | x_start | v | score
            ``

        2. "classifier": classifier guidance sampling [3] by DPMs and another classifier.
            The input `model` has the following format:
            ``
                model(x, t_input, **model_kwargs) -> noise | x_start | v | score
            ``

            The input `classifier_fn` has the following format:
            ``
                classifier_fn(x, t_input, cond, **classifier_kwargs) -> logits(x, t_input, cond)
            ``

            [3] P. Dhariwal and A. Q. Nichol, "Diffusion models beat GANs on image synthesis,"
                in Advances in Neural Information Processing Systems, vol. 34, 2021, pp. 8780-8794.

        3. "classifier-free": classifier-free guidance sampling by conditional DPMs.
            The input `model` has the following format:
            ``
                model(x, t_input, cond, **model_kwargs) -> noise | x_start | v | score
            ``
            And if cond == `unconditional_condition`, the model output is the unconditional DPM output.

            [4] Ho, Jonathan, and Tim Salimans. "Classifier-free diffusion guidance."
                arXiv preprint arXiv:2207.12598 (2022).


    The `t_input` is the time label of the model, which may be discrete-time labels (i.e. 0 to 999)
    or continuous-time labels (i.e. epsilon to T).

    We wrap the model function to accept only `x` and `t_continuous` as inputs,
    and outputs the predicted noise:
    ``
        def model_fn(x, t_continuous) -> noise:
            t_input = get_model_input_time(t_continuous)
            return noise_pred(model, x, t_input, **model_kwargs)
    ``
    where `t_continuous` is the continuous time labels (i.e. epsilon to T).
    And we use `model_fn` for DPM-Solver.

    ===============================================================

    Args:
        model: A diffusion model with the corresponding format described above.
        noise_schedule: A noise schedule object, such as NoiseScheduleVP.
        model_type: A `str`. The parameterization type of the diffusion model.
                    "noise" or "x_start" or "v" or "score".
        model_kwargs: A `dict`. A dict for the other inputs of the model function.
        guidance_type: A `str`. The type of the guidance for sampling.
                    "uncond" or "classifier" or "classifier-free".
        condition: A pytorch tensor. The condition for the guided sampling.
                    Only used for "classifier" or "classifier-free" guidance type.
        unconditional_condition: A pytorch tensor. The condition for the unconditional sampling.
                    Only used for "classifier-free" guidance type.
        guidance_scale: A `float`. The scale for the guided sampling.
        classifier_fn: A classifier function. Only used for the classifier guidance.
        classifier_kwargs: A `dict`. A dict for the other inputs of the classifier function.
    Returns:
        A noise prediction model that accepts the noised data and the continuous time as the inputs.
    c                         j dkr| d j  d S | S a  
        Convert the continuous-time `t_continuous` (in [epsilon, T]) to the model input time.
        For discrete-time DPMs, we convert `t_continuous` in [1 / N, 1] to `t_input` in [0, 1000 * (N - 1) / N].
        For continuous-time DPMs, we just use `t_continuous`.
        r   r        @@r"   r'   t_continuousnoise_scheduler   r   get_model_input_timeQ     
z+model_wrapper.<locals>.get_model_input_timeNc                    s    |}|d u r| |fi }n
| ||fi }dkr"|S dkr9 ||}}| ||  | S dkrP ||}}|| ||   S dkr^|}| | S d S )Nr]   x_startvscore)rH   rJ   )r   rd   condt_inputoutputalpha_tsigma_t)rg   modelmodel_kwargs
model_typerf   r   r   noise_pred_fn\  s0   

z$model_wrapper.<locals>.noise_pred_fnc                    sd   t  $ |  d} ||fi }t j| |d W  d   S 1 s+w   Y  dS )z]
        Compute the gradient of the classifier, i.e. nabla_{x} log p_t(cond | x_t).
        Tr   N)r#   enable_graddetachrequires_grad_autogradgradsum)r   rm   x_inlog_prob)classifier_fnclassifier_kwargs	conditionr   r   cond_grad_fnp  s   
$z#model_wrapper.<locals>.cond_grad_fnc           
         s   dkr	| |S dkr. dusJ |}| |} |}| |}|| |  S dkrldks:du rA| |dS t| gd }t|gd }tg}|||dd\}	}|	||	   S dS )S
        The noise predicition model function that is used for DPM-Solver.
        r^   
classifierNclassifier-freer   )rl   r?   )rJ   r#   catchunk)
r   rd   rm   	cond_gradrp   r]   r{   t_inc_innoise_uncond)	r}   r   r   rg   guidance_scaleguidance_typert   rf   unconditional_conditionr   r   model_fnz  s,   



zmodel_wrapper.<locals>.model_fn)r]   ri   rj   )r^   r   r   Nr   )rq   rf   rs   rr   r   r   r   r   r}   r~   r   r   )r}   r~   r   r   rg   r   r   rq   rr   rs   rt   rf   r   r   r      s   j
r   Fr   c	           
         sH   	
fdd fdd fddfdd}	|	S )	a  Create a wrapper function for the noise prediction model guided diffusion.

    DPM-Solver needs to solve the continuous-time diffusion ODEs.
    For DPMs trained on discrete-time labels, we need to
    firstly wrap the model function to a noise prediction model that accepts the continuous time as the input.

    We support three types of the diffusion model by setting `mean_type`:

        1. "x_{t-1}": previous data prediction model.
           (Trained by predicting the data x_{t-1} at time t-1).

        2. "x0": data prediction model. (Trained by predicting the data x0 at time 0).

        3. "eps": noise prediction model. (Trained by predicting the noise).

    We support three types of guided sampling by DPMs:
        1. unconditional sampling by DPMs.
            The input `model` has the following format:
            ``
                model(xt, t_input, **model_kwargs) -> x_{t-1} | x0 | eps
            ``

        2. classifier guidance sampling [3] by DPMs and another classifier.
            The input `model` has the following format:
            ``
                model(xt, t_input, **model_kwargs) -> x_{t-1} | x0 | eps
            ``

            The input `condition_fn` has the following format:
            ``
                condition_fn(xt, t_input, **model_kwargs) -> logits(xt, t_input)
            ``

            [3] P. Dhariwal and A. Q. Nichol, "Diffusion models beat GANs on image synthesis,"
                in Advances in Neural Information Processing Systems, vol. 34, 2021, pp. 8780-8794.

        3. classifier-free guidance sampling by conditional DPMs.
            The input `model` has the following format:
            ``
                model(xt, t_input, **model_kwargs) -> x_{t-1} | x0 | eps
            ``

            [4] Ho, Jonathan, and Tim Salimans. "Classifier-free diffusion guidance."
                arXiv preprint arXiv:2207.12598 (2022).


    The `t_input` is the time label of the model, which may be discrete-time labels
    or continuous-time labels (i.e. epsilon to T).

    We wrap the model function to accept only `x` and `t_continuous` as inputs,
    and outputs the predicted noise:
    ``
        def model_fn(x, t_continuous) -> noise:
            t_input = get_model_input_time(t_continuous)
            return noise_pred_fn(model, x, t_input, **model_kwargs)
    ``
    where `t_continuous` is the continuous time labels (i.e. epsilon to T).
    And we use `model_fn` for DPM-Solver.

    ===============================================================

    Args:
        model: A diffusion model with the corresponding format described above.
        noise_schedule: A noise schedule object, such as NoiseScheduleVP.
        var_type: A `str`. The variance type of the diffusion model.
                    "learned" or "learned_range" or "fixed_large" or "fixed_small".
        mean_type: A `str`. The prediction type of the diffusion model.
                    "x_{t-1}" or "x0" or "eps".
        model_kwargs: A `dict`. A dict for the other inputs of the model function.
        clamp: A `float`. The range to clamp the data.
        rescale_timesteps: A `bool`. Whether to rescale the timesteps.
        num_timesteps: An `int`. The number of the total diffusion steps.
        guide_scale: A `float`. The strength of the classifier-free guidance.
        condition_fn: A function. The function of the classifier guidance.
    Returns:
        A noise prediction model that accepts the noised data and the continuous time as the inputs.
    c                    s   r
|   d   S | S )Nra   )float)r   )num_timestepsrescale_timestepsr   r   _scale_timesteps  s   z8model_wrapper_guided_diffusion.<locals>._scale_timestepsc                    r_   r`   rb   rc   re   r   r   rg     rh   z<model_wrapper_guided_diffusion.<locals>.get_model_input_timec                    s  | djd dkr|| jd } |}| |fi |}dkr/|jddd\}}ndkr<|jddd\}}dkrC|}n'd	krItdkrj||}}|	 }
| t||
|  t||
 }d ur||}}|d| 
 | |fi |  }|S )NrN   r   r   learnedr?   r   learned_rangeepszx_{t-1}r   r   x0)r*   r   expandr   NotImplementedErrorr"   expand_dimsrH   rJ   r   rI   )xtrd   rr   rm   out_r   muposterior_mean_coef1posterior_mean_coef2dimsr   ro   rp   )r   condition_fnrg   	mean_typerq   rf   var_typer   r   rt     sD   

z5model_wrapper_guided_diffusion.<locals>.noise_pred_fnc                    s   | djd dkr|| jd } du r!| |fi }|S ttr,tdks.J | |fi d }| |fi d }drN|dn|dd }|ddd|f  |ddd|f |ddd|f    }|S )r   rN   r   r   Nr?   fixed)r*   r   r   
isinstancelistr&   
startswithr	   )r   rd   r   y_outu_outr   )guide_scalerr   rt   r   r   r   r   &  s"   &z0model_wrapper_guided_diffusion.<locals>.model_fnr   )
rq   rf   r   r   rr   r   r   r   r   r   r   )r   r   rg   r   r   rq   rr   rt   rf   r   r   r   r   r     s
   W(r   c                   @   s  e Zd Z				d6ddZdd Zdd	 Zd
d Zdd Zdd Zdd Z			d7ddZ
				d8ddZ						d9ddZ	d:ddZ	d:dd Z				d;d!d"Z	d:d#d$Z	%	&	%	'	(	d<d)d*Zd=d+d,Z	-			.	/	0	1			&	%	d>d2d3Z	-			.	/	0	1			&	%d?d4d5ZdS )@r   dpmsolver++Nr   c                 C   s(   || _ || _|| _|| _|| _|| _dS )a  Construct a DPM-Solver.

        We support both the noise prediction model ("predicting epsilon")
        and the data prediction model ("predicting x0").
        If `predict_x0` is False, we use the solver for the noise prediction model (DPM-Solver).
        If `predict_x0` is True, we use the solver for the data prediction model (DPM-Solver++).
            In such case, we further support the "dynamic thresholding"
            in [1] when `thresholding` is True.
            The "dynamic thresholding" can greatly improve the sample quality for pixel-space DPMs
            with large guidance scales.

        Args:
            model_fn: A noise prediction model function which accepts the continuous-time input (t in [epsilon, T]):
                ``
                def model_fn(x, t_continuous):
                    return noise
                ``
            noise_schedule: A noise schedule object, such as NoiseScheduleVP.
            predict_x0: A `bool`. If true, use the data prediction model; else, use the noise prediction model.
            thresholding: A `bool`. Valid when `predict_x0` is True. Whether to use the "dynamic thresholding" in [1].
            max_val: A `float`. Valid when both `predict_x0` and
                    `thresholding` are True. The max value for thresholding.

        [1] Chitwan Saharia, William Chan, Saurabh Saxena, Lala Li, Jay Whang,
            Emily Denton, Seyed Kamyar Seyed Ghasemipour,
            Burcu Karagol Ayan, S Sara Mahdavi, Rapha Gontijo Lopes, et al.
            Photorealistic text-to-image diffusion models with deep language understanding.
            arXiv preprint arXiv:2205.11487, 2022b.
        N)rq   rf   algorithm_type
percentilethresholding_max_valclamp)r7   r   rf   r   r   r   r   r   r   r   r=   @  s   $
zDPM_Solver.__init__c                 C      |  ||S )z4
        Return the noise prediction model.
        )rq   r7   r   r   r   r   r   noise_prediction_fnk  s   zDPM_Solver.noise_prediction_fnc           	      C   s   |  ||}| }| j|| j|}}|t|||  t|| }| jdur]tjt	|
|jd df| jdd}tt|| jt||j |}t|| || }|S | jduri|t t}|S )zG
        Return the data prediction model (with thresholding).
        Nr   r   r   r   )r   r   rf   rH   rJ   r   r   r#   quantileabsr*   r   maximumr   	ones_liker   r   r   )	r7   r   r   r]   r   ro   rp   r   r@   r   r   r   data_prediction_fnq  s<   


zDPM_Solver.data_prediction_fnc                 C   s"   | j dkr| ||S | ||S )z_
        Convert the model to the noise prediction model or the data prediction model.
        r   )r   r   r   r   r   r   r   r     s   
zDPM_Solver.model_fnc                 C   s   |dkr6| j t||}| j t||}t|  |  |d |}| j |S |dkrFt|||d |S |dkred}	t|d|	  |d|	  |d 	|	|}
|
S t
d|)a7  Compute the intermediate time steps for sampling.

        Args:
            skip_type: A `str`. The type for the spacing of the time steps. We support three types:
                - 'logSNR': uniform logSNR for the time steps.
                - 'time_uniform': uniform time for the time steps. (**Recommended for high-resolutional data**.)
                - 'time_quadratic': quadratic time for the time steps. (Used in DDIM for low-resolutional data.)
            t_T: A `float`. The starting time of the sampling (default is T).
            t_0: A `float`. The ending time of the sampling (default is epsilon).
            N: A `int`. The total number of the spacing of the time steps.
            device: A torch device.
        Returns:
            A pytorch tensor of the time steps, with the shape (N + 1,).
        logSNRr   time_uniformtime_quadraticr?   r   zSUnsupported skip_type {}, need to be 'logSNR' or 'time_uniform' or 'time_quadratic')rf   rM   r#   r   r   r)   cpuitemrX   powr    r!   )r7   	skip_typet_Tt_0Nr   lambda_Tlambda_0logSNR_stepst_orderr   r   r   r   get_time_steps  s8   
zDPM_Solver.get_time_stepsc           
      C   s4  |dkr8|d d }|d dkrdg|d  ddg }nQ|d dkr-dg|d  dg }n@dg|d  dg }n5|dkr]|d dkrL|d }dg| }n!|d d }dg|d  dg }n|dkrid}dg| }nt d|dkr~| |||||}	|	|fS | |||||ttdg| d| }	|	|fS )a  
        Get the order of each step for sampling by the singlestep DPM-Solver.

        We combine both DPM-Solver-1,2,3 to use all the function evaluations, which is named as "DPM-Solver-fast".
        Given a fixed number of function evaluations by `steps`, the sampling procedure by DPM-Solver-fast is:
            - If order == 1:
                We take `steps` of DPM-Solver-1 (i.e. DDIM).
            - If order == 2:
                - Denote K = (steps // 2). We take K or (K + 1) intermediate time steps for sampling.
                - If steps % 2 == 0, we use K steps of DPM-Solver-2.
                - If steps % 2 == 1, we use K steps of DPM-Solver-2 and 1 step of DPM-Solver-1.
            - If order == 3:
                - Denote K = (steps // 3 + 1). We take K intermediate time steps for sampling.
                - If steps % 3 == 0, we use (K - 2) steps of DPM-Solver-3,
                  and 1 step of DPM-Solver-2 and 1 step of DPM-Solver-1.
                - If steps % 3 == 1, we use (K - 1) steps of DPM-Solver-3 and 1 step of DPM-Solver-1.
                - If steps % 3 == 2, we use (K - 1) steps of DPM-Solver-3 and 1 step of DPM-Solver-2.

        ============================================
        Args:
            order: A `int`. The max order for the solver (2 or 3).
            steps: A `int`. The total number of function evaluations (NFE).
            skip_type: A `str`. The type for the spacing of the time steps. We support three types:
                - 'logSNR': uniform logSNR for the time steps.
                - 'time_uniform': uniform time for the time steps. (**Recommended for high-resolutional data**.)
                - 'time_quadratic': quadratic time for the time steps. (Used in DDIM for low-resolutional data.)
            t_T: A `float`. The starting time of the sampling (default is T).
            t_0: A `float`. The ending time of the sampling (default is epsilon).
            device: A torch device.
        Returns:
            orders: A list of the solver order of each step.
           r   r   r?   z"'order' must be '1' or '2' or '3'.r   )r    r   r#   r%   r   r   )
r7   stepsorderr   r   r   r   Korderstimesteps_outerr   r   r   .get_orders_and_timesteps_for_singlestep_solver  s|   #	z9DPM_Solver.get_orders_and_timesteps_for_singlestep_solverc                 C   r   )z
        Denoise at the final step, which is equivalent to solve
        the ODE from lambda_s to infty by first-order discretization.
        )r   )r7   r   r@   r   r   r   denoise_to_zero_fn  s   zDPM_Solver.denoise_to_zero_fnFc                 C   s   | j }||||}}|| }	||||}
}||||}}t|}| jdkrXt|	 }|du rB| ||}|| | || |  }|rV|d|ifS |S t|	}|du rg| ||}t||
 | || |  }|r~|d|ifS |S )a  
        DPM-Solver-1 (equivalent to DDIM) from time `s` to time `t`.

        Args:
            x: A pytorch tensor. The initial value at time `s`.
            s: A pytorch tensor. The starting time, with the shape (1,).
            t: A pytorch tensor. The ending time, with the shape (1,).
            model_s: A pytorch tensor. The model function evaluated at time `s`.
                If `model_s` is None, we evaluate the model by `x` and `s`; otherwise we directly use it.
            return_intermediate: A `bool`. If true, also return the model value at time `s`.
        Returns:
            x_t: A pytorch tensor. The approximated solution at time `t`.
        r   Nmodel_s)	rf   rM   rE   rJ   r#   rF   r   expm1r   )r7   r   r@   r   r   return_intermediatenslambda_slambda_thlog_alpha_srD   sigma_srp   ro   phi_1x_tr   r   r   dpm_solver_first_update  s8   



z"DPM_Solver.dpm_solver_first_updater   	dpmsolverc                 C   s  |dvrt d||du rd}| j}||||}	}
|
|	 }|	||  }||}||||||}}}||||||}}}t|t|}}| j	dkrt
| | }t
| }|du rx| ||}|| | || |  }| ||}|dkr|| | || |  d| ||  ||   }n|dkr|| | || |  d| ||| d   ||   }npt
|| }t
|}|du r| ||}t|| | || |  }| ||}|dkrt|| | || |  d| ||  ||   }n$|dkr8t|| | || |  d| ||| d   ||   }|rB|||d	fS |S )
a  
        Singlestep solver DPM-Solver-2 from time `s` to time `t`.

        Args:
            x: A pytorch tensor. The initial value at time `s`.
            s: A pytorch tensor. The starting time, with the shape (1,).
            t: A pytorch tensor. The ending time, with the shape (1,).
            r1: A `float`. The hyperparameter of the second-order solver.
            model_s: A pytorch tensor. The model function evaluated at time `s`.
                If `model_s` is None, we evaluate the model by `x` and `s`; otherwise we directly use it.
            return_intermediate: A `bool`.
                                 If true, also return the model value at time `s` and `s1` (the intermediate time).
            solver_type: either 'dpmsolver' or 'taylor'. The type for the high-order solvers.
                The type slightly impacts the performance. We recommend to use 'dpmsolver' type.
        Returns:
            x_t: A pytorch tensor. The approximated solution at time `t`.
        r   taylor<'solver_type' must be either 'dpmsolver' or 'taylor', got {}Nr   r   r   r   r   )r   model_s1r    r!   rf   rM   rX   rE   rJ   r#   rF   r   r   r   )r7   r   r@   r   r1r   r   solver_typer   r   r   r   	lambda_s1s1r   log_alpha_s1rD   r   sigma_s1rp   alpha_s1ro   phi_11r   x_s1r   r   r   r   r   #singlestep_dpm_solver_second_update>  s   














z.DPM_Solver.singlestep_dpm_solver_second_updateUUUUUU?UUUUUU?c
           +      C   sr  |	dvrt d|	|du rd}|du rd}| j}
|
||
|}}|| }|||  }|||  }|
|}|
|}|
||
||
||
|f\}}}}|
||
||
||
|f\}}}}t|t|t|}}}| j	dkrSt
| | }t
| | }t
| }t
| | ||  d } || d }!|!| d }"|du r| ||}|du r|| | || |  }#| |#|}|| | || |  || ||   ||   }$| |$|}%|	d	kr|| | || |  d| ||!  |%|   }&n|	d
krRd| ||  }'d| |%|  }(||' ||(  ||  })d|(|'  ||  }*|| | || |  ||! |)  ||" |*  }&nt
|| }t
|| }t
|}t
|| ||  d } || d }!|!| d }"|du r| ||}|du rt|| | || |  }#| |#|}t|| | || |  || ||   ||   }$| |$|}%|	d	krt|| | || |  d| ||!  |%|   }&nF|	d
kr,d| ||  }'d| |%|  }(||' ||(  ||  })d|(|'  ||  }*t|| | || |  ||! |)  ||" |*  }&|r7|&|||%dfS |&S )a  
        Singlestep solver DPM-Solver-3 from time `s` to time `t`.

        Args:
            x: A pytorch tensor. The initial value at time `s`.
            s: A pytorch tensor. The starting time, with the shape (1,).
            t: A pytorch tensor. The ending time, with the shape (1,).
            r1: A `float`. The hyperparameter of the third-order solver.
            r2: A `float`. The hyperparameter of the third-order solver.
            model_s: A pytorch tensor. The model function evaluated at time `s`.
                If `model_s` is None, we evaluate the model by `x` and `s`; otherwise we directly use it.
            model_s1: A pytorch tensor. The model function evaluated at time `s1` (the intermediate time given by `r1`).
                If `model_s1` is None, we evaluate the model at `s1`; otherwise we directly use it.
            return_intermediate: A `bool`. If true,
                                 also return the model value at time `s`, `s1` and `s2` (the intermediate times).
            solver_type: either 'dpmsolver' or 'taylor'. The type for the high-order solvers.
                The type slightly impacts the performance. We recommend to use 'dpmsolver' type.
        Returns:
            x_t: A pytorch tensor. The approximated solution at time `t`.
        r   r   Nr   r   r   r   r   r   r   r   )r   r   model_s2r   )+r7   r   r@   r   r   r2r   r   r   r   r   r   r   r   r   	lambda_s2r   s2r   r   log_alpha_s2rD   r   r   sigma_s2rp   r   alpha_s2ro   r   phi_12r   phi_22phi_2phi_3r   x_s2r   r   D1_0D1_1D1D2r   r   r   "singlestep_dpm_solver_third_update  s  

























z-DPM_Solver.singlestep_dpm_solver_third_updatec                 C   s  |dvrt d|| j}|d |d }}|d |d }	}
||	||
||}}}||
||}}||
||}}t|}|| }|| }|| }d| ||  }| jdkrt	| }|dkr|| | || |  d||  |  }|S |d	kr|| | || |  ||| d  |  }|S t	|}|dkrt|| | || |  d||  |  }|S |d	krt|| | || |  ||| d  |  }|S )
a  
        Multistep solver DPM-Solver-2 from time `t_prev_list[-1]` to time `t`.

        Args:
            x: A pytorch tensor. The initial value at time `s`.
            model_prev_list: A list of pytorch tensor. The previous computed model values.
            t_prev_list: A list of pytorch tensor. The previous times, each time has the shape (1,)
            t: A pytorch tensor. The ending time, with the shape (1,).
            solver_type: either 'dpmsolver' or 'taylor'. The type for the high-order solvers.
                The type slightly impacts the performance. We recommend to use 'dpmsolver' type.
        Returns:
            x_t: A pytorch tensor. The approximated solution at time `t`.
        r   r   r   r   r   r   r   r   )
r    r!   rf   rM   rE   rJ   r#   rF   r   r   )r7   r   model_prev_listt_prev_listr   r   r   model_prev_1model_prev_0t_prev_1t_prev_0lambda_prev_1lambda_prev_0r   log_alpha_prev_0rD   sigma_prev_0rp   ro   h_0r   r0r   r   r   r   r   r   "multistep_dpm_solver_second_update  s~   









z-DPM_Solver.multistep_dpm_solver_second_updatec           #      C   s  | j }|\}}}	|\}
}}||
||||||f\}}}}||||}}||||}}t|}|| }|| }|| }|| || }}d| |	|  }d| ||  }||||  ||   }d||  ||  }| jdkrt| }|| d } | | d }!|| | || |	  ||  |  ||! |  }"|"S t|}|| d } | | d }!t|| | || |	  ||  |  ||! |  }"|"S )a  
        Multistep solver DPM-Solver-3 from time `t_prev_list[-1]` to time `t`.

        Args:
            x: A pytorch tensor. The initial value at time `s`.
            model_prev_list: A list of pytorch tensor. The previous computed model values.
            t_prev_list: A list of pytorch tensor. The previous times, each time has the shape (1,)
            t: A pytorch tensor. The ending time, with the shape (1,).
            solver_type: either 'dpmsolver' or 'taylor'. The type for the high-order solvers.
                The type slightly impacts the performance. We recommend to use 'dpmsolver' type.
        Returns:
            x_t: A pytorch tensor. The approximated solution at time `t`.
        r   r   r   )rf   rM   rE   rJ   r#   rF   r   r   )#r7   r   r   r   r   r   r   model_prev_2r  r  t_prev_2r  r  lambda_prev_2r  r  r   r  rD   r  rp   ro   h_1r	  r   r
  r   r   r   r   r   r   r   r   r   r   r   r   !multistep_dpm_solver_third_updateJ  sb   











z,DPM_Solver.multistep_dpm_solver_third_updatec	           	   	   C   sf   |dkr| j ||||dS |dkr| j||||||dS |dkr,| j|||||||dS td|)a  
        Singlestep DPM-Solver with the order `order` from time `s` to time `t`.

        Args:
            x: A pytorch tensor. The initial value at time `s`.
            s: A pytorch tensor. The starting time, with the shape (1,).
            t: A pytorch tensor. The ending time, with the shape (1,).
            order: A `int`. The order of DPM-Solver. We only support order == 1 or 2 or 3.
            return_intermediate: A `bool`. If true, also return the model value at time `s`,
                                 `s1` and `s2` (the intermediate times).
            solver_type: either 'dpmsolver' or 'taylor'. The type for the high-order solvers.
                The type slightly impacts the performance. We recommend to use 'dpmsolver' type.
            r1: A `float`. The hyperparameter of the second-order or third-order solver.
            r2: A `float`. The hyperparameter of the third-order solver.
        Returns:
            x_t: A pytorch tensor. The approximated solution at time `t`.
        r   r   r?   )r   r   r   r   )r   r   r   r   (Solver order must be 1 or 2 or 3, got {})r   r   r   r    r!   )	r7   r   r@   r   r   r   r   r   r   r   r   r   singlestep_dpm_solver_update  s4   	z'DPM_Solver.singlestep_dpm_solver_updatec                 C   sh   |dkr| j ||d ||d dS |dkr| j|||||dS |dkr-| j|||||dS td|)a0  
        Multistep DPM-Solver with the order `order` from time `t_prev_list[-1]` to time `t`.

        Args:
            x: A pytorch tensor. The initial value at time `s`.
            model_prev_list: A list of pytorch tensor. The previous computed model values.
            t_prev_list: A list of pytorch tensor. The previous times, each time has the shape (1,)
            t: A pytorch tensor. The ending time, with the shape (1,).
            order: A `int`. The order of DPM-Solver. We only support order == 1 or 2 or 3.
            solver_type: either 'dpmsolver' or 'taylor'. The type for the high-order solvers.
                The type slightly impacts the performance. We recommend to use 'dpmsolver' type.
        Returns:
            x_t: A pytorch tensor. The approximated solution at time `t`.
        r   r   )r   r?   r   r   r  )r   r  r  r    r!   )r7   r   r   r   r   r   r   r   r   r   multistep_dpm_solver_update  s   

z&DPM_Solver.multistep_dpm_solver_update皙?q??h㈵>c              
      s  j }|td| }||}||t|| }|t|| }|}d}|dkrBd fdd} fdd}n!|dkr\d	\  fd
d} fdd}ntd|t|| 	 |	kr|
|| }||||\}}||||fi |}tt||| |tt|t| }dd }||| |  }t|dkr|}|}|}||}t|| t|d|   || }||7 }t|| 	 |	ksntd| |S )uZ  
        The adaptive step size solver based on singlestep DPM-Solver.

        Args:
            x: A pytorch tensor. The initial value at time `t_T`.
            order: A `int`. The (higher) order of the solver. We only support order == 2 or 3.
            t_T: A `float`. The starting time of the sampling (default is T).
            t_0: A `float`. The ending time of the sampling (default is epsilon).
            h_init: A `float`. The initial step size (for logSNR).
            atol: A `float`. The absolute tolerance of the solver.
                  For image data, the default setting is 0.0078, followed [1].
            rtol: A `float`. The relative tolerance of the solver.
                  The default setting is 0.05.
            theta: A `float`. The safety hyperparameter for adapting the step size.
                   The default setting is 0.9, followed [1].
            t_err: A `float`. The tolerance for the time.
                   We solve the diffusion ODE until the absolute error between the
                   current time and `t_0` is less than `t_err`. The default setting is 1e-5.
            solver_type: either 'dpmsolver' or 'taylor'. The type for the high-order solvers.
                The type slightly impacts the performance. We recommend to use 'dpmsolver' type.
        Returns:
            x_0: A pytorch tensor. The approximated solution at time `t_0`.

        [1] A. Jolicoeur-Martineau, K. Li, R. Piché-Taillefer, T. Kachman, and I. Mitliagkas,
            "Gotta go fast when generating data with score-based models,"
            arXiv preprint arXiv:2105.14080, 2021.
        r   r   r?   r   c                    s    j | ||ddS )NTr  )r   r   r@   r   rA   r   r   rB     s    z0DPM_Solver.dpm_solver_adaptive.<locals>.<lambda>c                    s   j | ||f d|S )N)r   r   r   r   r@   r   kwargsr   r7   r   r   r   rB     s    r   )r   r   c                    s   j | || ddS )NT)r   r   r   r  r  r  r   r   rB     s    c                    s   j | ||f d|S )N)r   r   r   )r   r  r   r   r7   r   r   r   rB     s    z;For adaptive step size solver, order must be 2 or 3, got {}c                 S   s*   t t | | jd dfjdddS )Nr   r   T)r   keepdim)r#   rI   squarer*   r   mean)rj   r   r   r   rB   %  s    r   g      zadaptive solver nfe)rf   r#   onesr   rM   r   r    r!   r   r"  rX   maxallminfloat_powerr   print)r7   r   r   r   r   h_initatolrtolthetat_errr   r   r@   r   r   r   x_prevnfelower_updatehigher_updater   x_lowerlower_noise_kwargsx_higherdeltanorm_fnEr   r  r   dpm_solver_adaptive  sT   &
	

zDPM_Solver.dpm_solver_adaptivec                 C   s   | j || j |}}|du r!tj|jd g|jR |jd}|dg|jR }t||	 | t||	 |  }|jd dkrI|
dS |S )a#  
        Compute the noised input xt = alpha_t * x + sigma_t * noise.

        Args:
            x: A `torch.Tensor` with shape `(batch_size, *shape)`.
            t: A `torch.Tensor` with shape `(t_size,)`.
        Returns:
            xt with shape `(t_size, batch_size, *shape)`.
        Nr   r   r   r   )rf   rH   rJ   r#   randnr   r   r*   r   r   squeeze)r7   r   r   r]   ro   rp   r   r   r   r   	add_noise5  s    

 
zDPM_Solver.add_noise   r?   r   	multistepTc                 C   sh   |du r
d| j j n|}|du r| j jn|}|dkr|dks"J d| j|||||||||	|
|||dS )z
        Inverse the sample `x` from time `t_start` to `t_end` by DPM-Solver.
        For discrete-time DPMs, we use `t_start=1/N`, where `N` is the total time steps during training.
        Nr   r   zTime range needs to be greater than 0. For discrete-time DPMs,                                     it needs to be in [1 / N, 1], where N is the length of betas array)r   t_startt_endr   r   methodlower_order_finaldenoise_to_zeror   r*  r+  r   )rf   r'   r(   sample)r7   r   r   r?  r@  r   r   rA  rB  rC  r   r*  r+  r   r   r   r   r   r   inverseK  s$   zDPM_Solver.inversec                  C   sT  |du r
d| j j n|}|du r| j jn|}|dkr|dks"J d|j}t u |dkr=| j|||||||
d}n?|dkr||ksGJ | j|||||d}|jd d	 |ks\J d}|| }|g}| 	||g}t
d	|D ]}|| }| j||||||
d
}|| || 	|| qqt
||d	 D ]L}|| }|r|dk rt||d	 | }n|}| j||||||
d
}t
|d	 D ]}||d	  ||< ||d	  ||< q||d< ||k r| 	|||d< qn|dv ru|dkr| j||||||d\}}n|dkr|| }|g| }| j|||||d}t|D ]Z\}}|| ||d	  }}| j|| | ||d}| j |}|d |d  }|d	krKdn	|d	 |d  | }|dkr\dn	|d |d  | }| j|||||
||d}qntd||	rtd|| }| ||}W d   |S W d   |S 1 sw   Y  |S )a  
        Compute the sample at time `t_end` by DPM-Solver, given the initial `x` at time `t_start`.

        =====================================================

        We support the following algorithms for both noise prediction model and data prediction model:
            - 'singlestep':
                Singlestep DPM-Solver (i.e. "DPM-Solver-fast" in the paper),
                which combines different orders of singlestep DPM-Solver.
                We combine all the singlestep solvers with order <=
                `order` to use up all the function evaluations (steps).
                The total number of function evaluations (NFE) == `steps`.
                Given a fixed NFE == `steps`, the sampling procedure is:
                    - If `order` == 1:
                        - Denote K = steps. We use K steps of DPM-Solver-1 (i.e. DDIM).
                    - If `order` == 2:
                        - Denote K = (steps // 2) + (steps % 2). We take K intermediate time steps for sampling.
                        - If steps % 2 == 0, we use K steps of singlestep DPM-Solver-2.
                        - If steps % 2 == 1, we use (K - 1) steps of singlestep DPM-Solver-2 and 1 step of DPM-Solver-1.
                    - If `order` == 3:
                        - Denote K = (steps // 3 + 1). We take K intermediate time steps for sampling.
                        - If steps % 3 == 0, we use (K - 2) steps of singlestep DPM-Solver-3,
                          and 1 step of singlestep DPM-Solver-2 and 1 step of DPM-Solver-1.
                        - If steps % 3 == 1, we use (K - 1) steps of singlestep DPM-Solver-3 and 1 step of DPM-Solver-1.
                        - If steps % 3 == 2, we use (K - 1)
                          steps of singlestep DPM-Solver-3 and 1 step of singlestep DPM-Solver-2.
            - 'multistep':
                Multistep DPM-Solver with the order of `order`.
                The total number of function evaluations (NFE) == `steps`.
                We initialize the first `order` values by lower order multistep solvers.
                Given a fixed NFE == `steps`, the sampling procedure is:
                    Denote K = steps.
                    - If `order` == 1:
                        - We use K steps of DPM-Solver-1 (i.e. DDIM).
                    - If `order` == 2:
                        - We firstly use 1 step of DPM-Solver-1, then use (K - 1) step of multistep DPM-Solver-2.
                    - If `order` == 3:
                        - We firstly use 1 step of DPM-Solver-1, then 1 step of multistep DPM-Solver-2,
                          then (K - 2) step of multistep DPM-Solver-3.
            - 'singlestep_fixed':
                Fixed order singlestep DPM-Solver
                (i.e. DPM-Solver-1 or singlestep DPM-Solver-2 or singlestep DPM-Solver-3).
                We use singlestep DPM-Solver-`order` for `order`=1 or 2 or 3,
                with total [`steps` // `order`] * `order` NFE.
            - 'adaptive':
                Adaptive step size DPM-Solver (i.e. "DPM-Solver-12" and "DPM-Solver-23" in the paper).
                We ignore `steps` and use adaptive step size DPM-Solver with a higher order of `order`.
                You can adjust the absolute tolerance `atol` and the relative tolerance
                `rtol` to balance the computatation costs
                (NFE) and the sample quality.
                    - If `order` == 2, we use DPM-Solver-12 which combines DPM-Solver-1
                      and singlestep DPM-Solver-2.
                    - If `order` == 3, we use DPM-Solver-23 which combines singlestep
                      DPM-Solver-2 and singlestep DPM-Solver-3.

        =====================================================

        Some advices for choosing the algorithm:
            - For **unconditional sampling** or **guided sampling with small guidance scale** by DPMs:
                Use singlestep DPM-Solver ("DPM-Solver-fast" in the paper) with `order = 3`.
                e.g.
                    >>> dpm_solver = DPM_Solver(model_fn, noise_schedule, predict_x0=False)
                    >>> x_sample = dpm_solver.sample(x, steps=steps, t_start=t_start, t_end=t_end, order=3,
                            skip_type='time_uniform', method='singlestep')
            - For **guided sampling with large guidance scale** by DPMs:
                Use multistep DPM-Solver with `predict_x0 = True` and `order = 2`.
                e.g.
                    >>> dpm_solver = DPM_Solver(model_fn, noise_schedule, predict_x0=True)
                    >>> x_sample = dpm_solver.sample(x, steps=steps, t_start=t_start, t_end=t_end, order=2,
                            skip_type='time_uniform', method='multistep')

        We support three types of `skip_type`:
            - 'logSNR': uniform logSNR for the time steps. **Recommended for low-resolutional images**
            - 'time_uniform': uniform time for the time steps. **Recommended for high-resolutional images**.
            - 'time_quadratic': quadratic time for the time steps.

        =====================================================
        Args:
            x: A pytorch tensor. The initial value at time `t_start`
                e.g. if `t_start` == T, then `x` is a sample from the standard normal distribution.
            steps: A `int`. The total number of function evaluations (NFE).
            t_start: A `float`. The starting time of the sampling.
                If `T` is None, we use self.noise_schedule.T (default is 1.0).
            t_end: A `float`. The ending time of the sampling.
                If `t_end` is None, we use 1. / self.noise_schedule.total_N.
                e.g. if total_N == 1000, we have `t_end` == 1e-3.
                For discrete-time DPMs:
                    - We recommend `t_end` == 1. / self.noise_schedule.total_N.
                For continuous-time DPMs:
                    - We recommend `t_end` == 1e-3 when `steps` <= 15; and `t_end` == 1e-4 when `steps` > 15.
            order: A `int`. The order of DPM-Solver.
            skip_type: A `str`. The type for the spacing of the time steps.
                       'time_uniform' or 'logSNR' or 'time_quadratic'.
            method: A `str`. The method for sampling.
                   'singlestep' or 'multistep' or 'singlestep_fixed' or 'adaptive'.
            denoise_to_zero: A `bool`. Whether to denoise to time 0 at the final step.
                Default is `False`. If `denoise_to_zero` is `True`, the total NFE is (`steps` + 1).

                This trick is firstly proposed by DDPM (https://arxiv.org/abs/2006.11239) and
                score_sde (https://arxiv.org/abs/2011.13456). Such trick can improve the FID
                for diffusion models sampling by diffusion SDEs for low-resolutional images
                (such as CIFAR-10). However, we observed that such trick does not matter for
                high-resolutional images. As it needs an additional NFE, we do not recommend
                it for high-resolutional images.
            lower_order_final: A `bool`. Whether to use lower order solvers at the final steps.
                Only valid for `method=multistep` and `steps < 15`. We empirically find that
                this trick is a key to stabilizing the sampling by DPM-Solver with very few steps
                (especially for steps <= 10). So we recommend to set it to be `True`.
            solver_type: A `str`. The taylor expansion type for the solver.
                         `dpmsolver` or `taylor`. We recommend `dpmsolver`.
            atol: A `float`. The absolute tolerance of the adaptive step size solver. Valid when `method` == 'adaptive'.
            rtol: A `float`. The relative tolerance of the adaptive step size solver. Valid when `method` == 'adaptive'.
        Returns:
            x_end: A pytorch tensor. The approximated solution at time `t_end`.

        Nr   r   zTime range needs to be greater than 0. For discrete-time DPMs,                            it needs to be in [1 / N, 1], where N is the length of betas arrayadaptive)r   r   r   r*  r+  r   r>  )r   r   r   r   r   r   r  
   r   )
singlestepsinglestep_fixedrH  )r   r   r   r   r   r   rI  r?   )r   r   r   zGot wrong method {}r   )rf   r'   r(   r   r#   no_gradr8  r   r   r   ranger  appendr&  r   	enumerater   rM   r  r    r!   r#  r   r   ) r7   r   r   r?  r@  r   r   rA  rB  rC  r   r*  r+  r   r   r   	timestepsstepr   r   r   
step_orderir   r   r   r@   timesteps_innerlambda_innerr   r   r   r   r   r   rD  q  s    





e
eezDPM_Solver.sample)r   Nr   N)NF)r   NFr   )r   r   NNFr   )r   )Fr   NN)r  r  r  r  r  r   r   )r=  NNr?   r   r>  TFr   r  r  F)r=  NNr?   r   r>  TFr   r  r  )rY   rZ   r[   r=   r   r   r   r   r   r   r   r   r   r  r  r  r  r8  r<  rE  rD  r   r   r   r   r   >  s    
+	$P
4
Y
 
@
=
8
'

_
(r   c                 C   s  | j d |j d }}tj| d|d|ddfgdd}tj|dd\}}tj|dd}|d }	tt|dtj	d| j
dtt||tj	|d | j
d|	}
tt|
|	|
d |
d }tj|d|
ddd}tj|d|ddd}tt|dtj	d| j
dtt||tj	|d | j
d|	}|d|dd}tj|d|ddd}tj|d|d ddd}|| | ||  ||   }|S )a  
    A piecewise linear function y = f(x), using xp and yp as keypoints.
    We implement f(x) in a differentiable way (i.e. applicable for autograd).
    The function f(x) is well-defined for all x-axis. (For x beyond the bounds of xp,
    we use the outmost points of xp to define the linear function.)

    Args:
        x: PyTorch tensor with shape [N, C], where N is the batch size,
           C is the number of channels (we use C = 1 for DPM-Solver).
        xp: PyTorch tensor with shape [C, K], where K is the number of keypoints.
        yp: PyTorch tensor with shape [C, K].
    Returns:
        The function values f(x), with shape [N, C].
    r   r   r?   r   r9  )r   indexr   )r   r#   r   	unsqueezerepeatsortargminwhereeqr   r   gatherr;  r   )r   xpypr   r   all_xsorted_all_x	x_indicesx_idxcand_start_idx	start_idxend_idxstart_xend_x
start_idx2y_positions_expandedstart_yend_ycandr   r   r   r   `  sh    

	

	r   c                 C   s   | dd|d    S )z
    Expand the tensor `v` to the dim `dims`.

    Args:
        `v`: a PyTorch tensor with shape [N].
        `dim`: a `int`.
    Returns:
        a PyTorch tensor with shape [N, 1, 1, ..., 1] and the total dimension is `dims`.
    ).r   r   r   )rj   r   r   r   r   r     s   
r   )r1   r#   torch.nn.functionalnn
functionalF__all__r   r   r   r   r   r   r   r   r   r   r   <module>   sF    T
 5
 (        *7