o
    
j]                    @   sZ   d dl Z d dlZG dd dZdi dddddi fddZG d	d
 d
Zdd Zdd ZdS )    Nc                   @   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 )NoiseScheduleVP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 )aL  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   dim      ?g        )r   )dtypei  gMb?g     8@       @r   gO@a?)
ValueErrorformatscheduletorchlogcumsumlentotal_NTlinspacereshapeto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_alphas r,   U/home/kuhnn/.local/lib/python3.10/site-packages/TTS/tts/layers/tortoise/dpm_solver.py__init__   sP   Y
,(

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   r$   r   r    r"   )sr&   r,   r-   log_alpha_fn      *z=NoiseScheduleVP.marginal_log_mean_coeff.<locals>.log_alpha_fnN)
r   interpolate_fnr   r   r   devicer   r   r   r%   )r&   tr3   log_alpha_tr,   r2   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   expr9   r&   r7   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   sqrtr:   r9   r;   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   )r9   r   r   r:   )r&   r7   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   r0   r   g      r/   r   )r   c                    s0   t t |  j d d j  tj  j S )Nr   r
   )r   arccosr:   r%   r   r    r"   )r8   r2   r,   r-   t_fn   s   z,NoiseScheduleVP.inverse_lambda.<locals>.t_fn)r   r   r   r   	logaddexpzerosr   r=   r6   r5   r   flipr   r   )r&   lambtmpDelta	log_alphar7   rD   r,   r2   r-   inverse_lambda   s    
, 
"
 	zNoiseScheduleVP.inverse_lambda)__name__
__module____qualname__r   float32r.   r9   r<   r>   rA   rL   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                    s     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
   g     @@)r   r   )t_continuous)noise_scheduler,   r-   get_model_input_time:  s   
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 )NrQ   x_startvscore)r<   r>   )xrS   condt_inputoutputalpha_tsigma_t)rU   modelmodel_kwargs
model_typerT   r,   r-   noise_pred_fnE  s    

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)rY   r[   x_inlog_prob)classifier_fnclassifier_kwargs	conditionr,   r-   cond_grad_fnW  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 )zS
        The noise predicition model function that is used for DPM-Solver.
        rR   
classifierNclassifier-freer
   )rZ   r0   )r>   r   catchunk)
rY   rS   r[   	cond_gradr^   rQ   ri   t_inc_innoise_uncond)	rk   rn   rm   rU   guidance_scaleguidance_typerb   rT   unconditional_conditionr,   r-   model_fn`  s$   



zmodel_wrapper.<locals>.model_fn)rQ   rV   rW   rX   )rR   ro   rp   Nr,   )r_   rT   ra   r`   rx   rm   ry   rw   rk   rl   rz   r,   )rk   rl   rn   rm   rU   rw   rx   r_   r`   ra   rb   rT   ry   r-   model_wrapper   s   e	r|   c                   @   s  e Zd Z					d9ddZdd Zd	d
 Zdd Zdd Zdd Zdd Z	d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d=d&d'Z	(	)	(	*	+	d?d,d-Zd@d.d/Z	0			1	2	3	4			)	(	dAd5d6Z	0			1	2	3	4			)	(	dAd7d8ZdS )B
DPM_Solverdpmsolver++Nr
   ףp=
?c                    sT    fdd| _ || _|dv sJ || _|dkr| j| _n|| _|| _|| _|| _dS )aN  Construct a DPM-Solver.

        We support both DPM-Solver (`algorithm_type="dpmsolver"`) and DPM-Solver++ (`algorithm_type="dpmsolver++"`).

        We also support the "dynamic thresholding" method in Imagen[1]. For pixel-space diffusion models, you
        can set both `algorithm_type="dpmsolver++"` and `correcting_x0_fn="dynamic_thresholding"` to use the
        dynamic thresholding. The "dynamic thresholding" can greatly improve the sample quality for pixel-space
        DPMs with large guidance scales. Note that the thresholding method is **unsuitable** for latent-space
        DPMs (such as stable-diffusion).

        To support advanced algorithms in image-to-image applications, we also support corrector functions for
        both x0 and xt.

        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
                ``
                The shape of `x` is `(batch_size, **shape)`, and the shape of `t_continuous` is `(batch_size,)`.
            noise_schedule: A noise schedule object, such as NoiseScheduleVP.
            algorithm_type: A `str`. Either "dpmsolver" or "dpmsolver++".
            correcting_x0_fn: A `str` or a function with the following format:
                ```
                def correcting_x0_fn(x0, t):
                    x0_new = ...
                    return x0_new
                ```
                This function is to correct the outputs of the data prediction model at each sampling step. e.g.,
                ```
                x0_pred = data_pred_model(xt, t)
                if correcting_x0_fn is not None:
                    x0_pred = correcting_x0_fn(x0_pred, t)
                xt_1 = update(x0_pred, xt, t)
                ```
                If `correcting_x0_fn="dynamic_thresholding"`, we use the dynamic thresholding proposed in Imagen[1].
            correcting_xt_fn: A function with the following format:
                ```
                def correcting_xt_fn(xt, t, step):
                    x_new = ...
                    return x_new
                ```
                This function is to correct the intermediate samples xt at each sampling step. e.g.,
                ```
                xt = ...
                xt = correcting_xt_fn(xt, t, step)
                ```
            thresholding_max_val: A `float`. The max value for thresholding.
                Valid only when use `dpmsolver++` and `correcting_x0_fn="dynamic_thresholding"`.
            dynamic_thresholding_ratio: A `float`. The ratio for dynamic thresholding (see Imagen[1] for details).
                Valid only when use `dpmsolver++` and `correcting_x0_fn="dynamic_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.
        c                    s    | | | jd S )Nr   )expandshape)rY   r7   rz   r,   r-   <lambda>  s    z%DPM_Solver.__init__.<locals>.<lambda>)	dpmsolverr~   dynamic_thresholdingN)r_   rT   algorithm_typedynamic_thresholding_fncorrecting_x0_fncorrecting_xt_fndynamic_thresholding_ratiothresholding_max_val)r&   rz   rT   r   r   r   r   r   r,   r   r-   r.   }  s   B

zDPM_Solver.__init__c                 C   sr   |  }| j}tjt||jd df|dd}tt|| j	t
||j |}t|| || }|S )z2
        The dynamic thresholding method.
        r   r   r   r   )r	   r   r   quantileabsr   r   expand_dimsmaximumr   	ones_liker   r6   clamp)r&   x0r7   dimspr1   r,   r,   r-   r     s   &z"DPM_Solver.dynamic_thresholding_fnc                 C      |  ||S )z4
        Return the noise prediction model.
        )r_   r&   rY   r7   r,   r,   r-   noise_prediction_fn     zDPM_Solver.noise_prediction_fnc                 C   sP   |  ||}| j|| j|}}|||  | }| jdur&| ||}|S )zD
        Return the data prediction model (with corrector).
        N)r   rT   r<   r>   r   )r&   rY   r7   rQ   r]   r^   r   r,   r,   r-   data_prediction_fn  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-   rz     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_quadraticr0   r
   zSUnsupported skip_type {}, need to be 'logSNR' or 'time_uniform' or 'time_quadratic')rT   rA   r   tensorr   r   cpuitemrL   powr   r   )r&   	skip_typet_Tt_0Nr6   lambda_Tlambda_0logSNR_stepst_orderr7   r,   r,   r-   get_time_steps  s   (.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   r0   z"'order' must be '1' or '2' or '3'.r   )r   r   r   r   r   r   )
r&   stepsorderr   r   r   r6   Korderstimesteps_outerr,   r,   r-   .get_orders_and_timesteps_for_singlestep_solver  sv    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   )r&   rY   r1   r,   r,   r-   denoise_to_zero_fnk  r   zDPM_Solver.denoise_to_zero_fnFc                 C   s  | j }| }||||}}	|	| }
||||}}||||}}t|}| jdkr\t|
 }|du rF| 	||}|| | || |  }|rZ|d|ifS |S t|
}|du rk| 	||}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)
rT   r	   rA   r9   r>   r   r:   r   expm1rz   )r&   rY   r1   r7   r   return_intermediatensr   lambda_slambda_thlog_alpha_sr8   sigma_sr^   r]   phi_1x_tr,   r,   r-   dpm_solver_first_updateq  s,   


z"DPM_Solver.dpm_solver_first_updater   r   c                 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   rT   rA   rL   r9   r>   r   r:   r   r   rz   )r&   rY   r1   r7   r1r   r   solver_typer   r   r   r   	lambda_s1s1r   log_alpha_s1r8   r   sigma_s1r^   alpha_s1r]   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   )+r&   rY   r1   r7   r   r2r   r   r   r   r   r   r   r   r   	lambda_s2r   s2r   r   log_alpha_s2r8   r   r   sigma_s2r^   r   alpha_s2r]   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   rT   rA   r9   r>   r   r:   r   r   )r&   rY   model_prev_listt_prev_listr7   r   r   model_prev_1model_prev_0t_prev_1t_prev_0lambda_prev_1lambda_prev_0r   log_alpha_prev_0r8   sigma_prev_0r^   r]   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   )rT   rA   r9   r>   r   r:   r   r   )#r&   rY   r   r   r7   r   r   model_prev_2r   r   t_prev_2r   r   lambda_prev_2r   r   r   r   r8   r   r^   r]   h_1r   r   r   r   r   r   r   r   r   r   r   r   r,   r,   r-   !multistep_dpm_solver_third_update  sX   











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   r0   )r   r   r   r   )r   r   r   r   (Solver order must be 1 or 2 or 3, got {})r   r   r   r   r   )	r&   rY   r1   r7   r   r   r   r   r   r,   r,   r-   singlestep_dpm_solver_update  s,   
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   r0   r   r   r   )r   r   r   r   r   )r&   rY   r   r   r7   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 )u  
        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.
        rB   r   r0   r   c                    s    j | ||ddS )NTr   )r   rY   r1   r7   r2   r,   r-   lower_updatel  s   z4DPM_Solver.dpm_solver_adaptive.<locals>.lower_updatec                    s   j | ||f d|S )N)r   r   r   rY   r1   r7   kwargsr   r&   r   r,   r-   higher_updateo  s   z5DPM_Solver.dpm_solver_adaptive.<locals>.higher_updater   )r   r   c                    s   j | || ddS )NT)r   r   r   r   r   r  r,   r-   r   u  s   c                    s   j | ||f d|S )N)r   r   r   )r   r   r   r   r&   r   r,   r-   r  z  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   r=   squarer   r   mean)rW   r,   r,   r-   norm_fn  r4   z/DPM_Solver.dpm_solver_adaptive.<locals>.norm_fnr
   g      zadaptive solver nfe)rT   r   onesr   rA   r   r   r   r   r  rL   maxallminfloat_powerfloatprint)r&   rY   r   r   r   h_initatolrtolthetat_errr   r   r1   r   r   r   x_prevnfer   r  r7   x_lowerlower_noise_kwargsx_higherdeltar  Er,   r  r-   dpm_solver_adaptive@  sN   "


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   r6   r   r   )rT   r<   r>   r   randnr   r6   r   r   r	   squeeze)r&   rY   r7   rQ   r]   r^   xtr,   r,   r-   	add_noise  s   
 $
zDPM_Solver.add_noise   r0   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   Time 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   )rT   r   r   sample)r&   rY   r   r$  r%  r   r   r&  r'  r(  r   r  r  r   r   r   r,   r,   r-   inverse  s(   zDPM_Solver.inversec           "      C   sX  |du r
d| j j n|}|du r| j jn|}|dkr|dks"J d|r,|dv s,J d| jdur9|dv s9J d|j}g }t  |dkrV| j|||||||
d	}n|d
kr=||ksaJ | j|||||d}|j	d d |ksvJ d}|| }|g}| 
||g}| jdur| |||}|r|| td|D ]2}|| }| j||||||
d}| jdur| |||}|r|| || || 
|| qt||d D ]c}|| }|r|dk rt||d | }n|}| j||||||
d}| jdur| |||}|r|| t|d D ]}||d  ||< ||d  ||< q||d< ||k r;| 
|||d< qn|dv r|dkrU| j||||||d\}}n|dkrm|| }|g| }| j|||||d}t|D ]o\}}|| ||d  }}| j|| | ||d}| j |}|d |d  }|dkrdn	|d |d  | } |dkrdn	|d |d  | }!| j|||||
| |!d}| jdur| |||}|r|| qqntd||	rtd|| }| ||}| jdur| |||d }|r|| W d   n	1 sw   Y  |r*||fS |S )a6   
        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 or DPM-Solver++ ("DPM-Solver-fast" in the paper) with `order = 3`.
                e.g., DPM-Solver:
                    >>> dpm_solver = DPM_Solver(model_fn, noise_schedule, algorithm_type="dpmsolver")
                    >>> x_sample = dpm_solver.sample(x, steps=steps, t_start=t_start, t_end=t_end, order=3,
                            skip_type='time_uniform', method='singlestep')
                e.g., DPM-Solver++:
                    >>> dpm_solver = DPM_Solver(model_fn, noise_schedule, algorithm_type="dpmsolver++")
                    >>> 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 `algorithm_type="dpmsolver++"` and `order = 2`.
                e.g.
                    >>> dpm_solver = DPM_Solver(model_fn, noise_schedule, algorithm_type="dpmsolver++")
                    >>> 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'.
            return_intermediate: A `bool`. Whether to save the xt at each step.
                When set to `True`, method returns a tuple (x0, intermediates); when set to False, method returns only x0.
        Returns:
            x_end: A pytorch tensor. The approximated solution at time `t_end`.

        Nr
   r   r#  )r"  
singlestepsinglestep_fixedz:Cannot use adaptive solver when saving intermediate valuesz<Cannot use adaptive solver when correcting_xt_fn is not Noneadaptive)r   r   r   r  r  r   r"  )r   r   r   r   r6   r   r   
   r   )r+  r,  r+  )r   r   r   r   r   r6   r,  r0   )r   r   r   zGot wrong method {}rB   )rT   r   r   r   r6   r   no_gradr  r   r   rz   appendranger   r  r   	enumerater   rA   r   r   r   r  r   r   )"r&   rY   r   r$  r%  r   r   r&  r'  r(  r   r  r  r   r   r   r6   intermediates	timestepsstepr7   r   r   
step_orderir   r   r   r1   timesteps_innerlambda_innerr   r   r   r,   r,   r-   r)    s  |




	









	""

rzDPM_Solver.sample)r~   NNr
   r   )NF)r   NFr   )r   r   NNFr   )r   )Fr   NN)r   r   r   r   r   r   r{   )r!  NNr0   r   r"  TFr   r   r   F)rM   rN   rO   r.   r   r   r   rz   r   r   r   r   r   r   r   r   r   r   r  r   r*  r)  r,   r,   r,   r-   r}   |  s    
N	Y
.
a
 

:?

3

Y
,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   r0   r   r  )r	   indexr   )r   r   rq   	unsqueezerepeatsortargminwhereeqr   r6   gatherr  r   )rY   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-   r5     s>   *

	

	 r5   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,   )rW   r   r,   r,   r-   r     s   
r   )r    r   r   r|   r}   r5   r   r,   r,   r,   r-   <module>   s2     S
 (        n.