+
    ‚°ñj£˜  ã                   ó¦   € ^ RI t ^ RIt^ RIt^ RIHt  ! R R4      tR/ RRRRR/ 3R lt ! R	 R
4      tR tR t	 ! R R4      t
R tRR ltRR ltR# )é    N)Útrangec                   óH   a € ] tR t^
t o R	R ltR tR tR tR tR t	Rt
V tR# )
ÚNoiseScheduleVPNc                ó„  € VR9  d   \        RP                  V4      4      hWn        VR8X  dÐ   Ve6   R\        P                  ! ^V,
          4      P                  ^ R7      ,          pM#Vf   Q hR\        P                  ! V4      ,          p\        V4      V n        RV n        \        P                  ! RRV P                  ^,           4      R	,          P                  R4      V n        VP                  R4      V n        R# R
V n        W@n        WPn        RV n        RV n        \"        P$                  ! V P                   RV P                  ,           ,          \"        P&                  ,          4      R,          RV P                  ,           ,          \"        P&                  ,          V P                  ,
          V n        \"        P                  ! \"        P*                  ! V P                  RV P                  ,           ,          \"        P&                  ,          R,          4      4      V n        Wn        VR8X  d
   RV n        R# RV n        R# )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(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.)

ÚdiscreteÚcosinezZUnsupported noise schedule {}. The schedule needs to be 'discrete' or 'linear' or 'cosine'Nç      à?©Údimç      ð?g        :é   NNiè  gü©ñÒMb€?g     8�@ç       @gO@aÃÓï?)r   Úlinearr   )r   éÿÿÿÿ)Ú
ValueErrorÚformatÚscheduleÚtorchÚlogÚcumsumÚlenÚtotal_NÚTÚlinspaceÚreshapeÚ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_1Ú
log_alphass   &&&&&& Ú?C:\Ai_Coding\apps\studio\ComfyUI\comfy\extra_samplers\uni_pc.pyÚ__init__ÚNoiseScheduleVP.__init__   sÝ  € ðp Ð;Ô;ÜÐy÷  Añ  Að  BJó  Kó  Lð  Là ŒØ�zÔ!ØÒ Ø ¤5§9¢9¨Q°­YÓ#7×#>Ñ#>À1Ð#>Ó#EÕE‘
à%Ò1Ð1Ð1Ø ¤5§9¢9¨^Ó#<Õ<�
Ü˜z›?ˆDŒLØˆDŒFÜ Ÿ>š>¨"¨b°$·,±,ÀÕ2BÓCÀBÕG×OÑOÐPWÓXˆDŒLØ#-×#5Ñ#5°hÓ#?ˆDÖ àˆDŒLØ+ŒKØ+ŒKØ!ˆDŒMØ#'ˆDÔ Ü $§	¢	¨$×*>Ñ*>À"ÀtÇ}Á}ÕBTÕ*UÔX\×X_ÑX_Õ*_Ó `ÐceÕ eÐikÐnr×n{Ñn{Õi{Õ |ô  @D÷  @Gñ  @Gõ  !Gð  JN÷  JWñ  JWõ  !WˆDÔÜ&*§h¢h¬t¯xªx¸¿¹ÈÈdÏmÉmÕI[Õ8\Ô_c×_fÑ_fÕ8fÐikÕ8kÓ/lÓ&mˆDÔ#Ø$ŒMØ˜8Ô#ð  �–à�–ó    c                ó4  a € S P                   R8X  dr   \        VP                  R4      S P                  P	                  VP
                  4      S P                  P	                  VP
                  4      4      P                  R4      # S P                   R8X  dR   R	V^,          ,          S P                  S P                  ,
          ,          RV,          S P                  ,          ,
          # S P                   R8X  d"   V 3R lpV! V4      S P                  ,
          pV# R# )
zD
Compute log(alpha_t) of a given continuous-time label t in [0, T].
r   r   r	   r   c                 óâ   <€ \         P                  ! \         P                  ! V SP                  ,           R SP                  ,           ,          \        P
                  ,          R,          4      4      # ©r   r   )r   r   r&   r    r"   r$   )Úsr(   s   &€r.   Ú<lambda>Ú9NoiseScheduleVP.marginal_log_mean_coeff.<locals>.<lambda>Š   sD   ø€ ¤U§Y¢Y¬u¯yªy¸!¸d¿m¹mÕ:KÐPRÐUY×UbÑUbÕPbÕ9cÔfj×fmÑfmÕ9mÐprÕ9rÓ/sÔ%tr1   Nr   ©r   r   g      Ð¿)
r   Úinterpolate_fnr   r   ÚtoÚdevicer   r   r   r'   )r(   ÚtÚlog_alpha_fnÚlog_alpha_ts   f&  r.   Úmarginal_log_mean_coeffÚ'NoiseScheduleVP.marginal_log_mean_coeff�   sÓ   ø€ ð �=‰=˜JÔ&Ü! !§)¡)¨GÓ"4°d·l±l·o±oÀaÇhÁhÓ6OÐQU×QeÑQe×QhÑQhÐij×iqÑiqÓQrÓs×{Ñ{Ð}ó  Bð  BØ�]‰]˜hÔ&Ø˜1 �6•> T§[¡[°4·;±;Õ%>Õ?À#ÈÅ'ÈDÏKÉKÕBWÕWÐWØ�]‰]˜hÔ&ÜtˆLÙ'¨›?¨T×-DÑ-DÕDˆKØÐñ 'r1   c                óL   € \         P                  ! V P                  V4      4      # )z?
Compute alpha_t of a given continuous-time label t in [0, T].
©r   Úexpr?   ©r(   r<   s   &&r.   Úmarginal_alphaÚNoiseScheduleVP.marginal_alphaŽ   s   € ô �yŠy˜×5Ñ5°aÓ8Ó9Ð9r1   c           	     ó�   € \         P                  ! R\         P                  ! RV P                  V4      ,          4      ,
          4      # )z?
Compute sigma_t of a given continuous-time label t in [0, T].
r   r   ©r   ÚsqrtrC   r?   rD   s   &&r.   Úmarginal_stdÚNoiseScheduleVP.marginal_std”   s1   € ô �zŠz˜"œuŸyšy¨¨d×.JÑ.JÈ1Ó.MÕ)MÓNÕNÓOÐOr1   c                ó²   € V P                  V4      pR\        P                  ! R\        P                  ! RV,          4      ,
          4      ,          pW#,
          # ©z^
Compute lambda_t = log(alpha_t) - log(sigma_t) of a given continuous-time label t in [0, T].
r	   r   r   ©r?   r   r   rC   ©r(   r<   Úlog_mean_coeffÚlog_stds   &&  r.   Úmarginal_lambdaÚNoiseScheduleVP.marginal_lambdaš   óC   € ð ×5Ñ5°aÓ8ˆØœŸ	š	 "¤u§y¢y°°nÕ1DÓ'EÕ"EÓFÕFˆØÕ'Ð'r1   c           	     ó‚  a € S P                   R8X  dÕ   RS P                  S P                  ,
          ,          \        P                  ! RV,          \        P
                  ! R4      P                  V4      4      ,          pS P                  ^,          V,           pV\        P                  ! V4      S P                  ,           ,          S P                  S P                  ,
          ,          # S P                   R8X  dò   R\        P                  ! \        P
                  ! R4      P                  VP                  4      RV,          4      ,          p\        VP                  R4      \        P                  ! S P                  P                  VP                  4      ^.4      \        P                  ! S P                  P                  VP                  4      ^.4      4      pVP                  R	4      # R\        P                  ! RV,          \        P
                  ! R4      P                  V4      4      ,          pV 3R lpV! V4      pV# )
zP
Compute the continuous-time label t in [0, T] of a given half-logSNR lambda_t.
r   r   r   c                 ó  <€ \         P                  ! \         P                  ! V SP                  ,           4      4      R ,          RSP                  ,           ,          \
        P                  ,          SP                  ,
          # )r   r   )r   ÚarccosrC   r'   r    r"   r$   )r>   r(   s   &€r.   r6   Ú0NoiseScheduleVP.inverse_lambda.<locals>.<lambda>°   s   ø€ ¤u§|¢|´E·I²I¸kÈD×LcÑLcÕ>cÓ4dÓ'eÐhjÕ'jÐnpÐsw÷  tAñ  tAõ  oAõ  (Bô  EI÷  ELñ  ELõ  (Lð  OS÷  O\ñ  O\ö  (\r1   g       À©r   g      à¿r8   ©r   )r   r   r   r   Ú	logaddexpÚzerosr:   rI   r;   r9   r   Úflipr   r   )r(   ÚlambÚtmpÚDeltaÚ	log_alphar<   Út_fns   f&     r.   Úinverse_lambdaÚNoiseScheduleVP.inverse_lambda¢   sÇ  ø€ ð �=‰=˜HÔ$Ø˜Ÿ™ d§k¡kÕ1Õ2´U·_²_ÀSÈ4ÅZÔQV×Q\ÒQ\Ð]aÓQb×QeÑQeÐfjÓQkÓ5lÕlˆCØ—K‘K •N SÕ(ˆEØœ%Ÿ*š* UÓ+¨d¯k©kÕ9Õ:¸d¿k¹kÈDÏKÉKÕ>WÕXÐXØ�]‰]˜jÔ(ØœuŸš¬u¯{ª{¸4Ó/@×/CÑ/CÀDÇKÁKÓ/PÐRUÐX\ÕR\Ó]Õ]ˆIÜ˜y×0Ñ0°Ó9¼5¿:º:Àd×FZÑFZ×F]ÑF]Ð^b×^iÑ^iÓFjÐmnÐloÓ;pÔrw×r|Òr|ð  ~B÷  ~Jñ  ~J÷  ~Mñ  ~Mð  NR÷  NYñ  NYó  ~Zð  ]^ð  \_ó  s`ó  aˆAØ—9‘9˜UÓ#Ð#àœuŸš¨s°T­z¼5¿;º;ÀtÓ;L×;OÑ;OÐPTÓ;UÓVÕVˆIô \ˆDÙ�Y“ˆAØˆHr1   )r   r   r   r!   r'   r    r%   r   r   r   r   )r   NNgš™™™™™¹?g      4@)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__r/   r?   rE   rJ   rR   rc   Ú__static_attributes__Ú__classdictcell__©Ú__classdict__s   @r.   r   r   
   s+   ø‡ € ôtòlò:òPò(÷ð r1   r   ÚnoiseÚuncondr   c
           	     ó”   a aaaaaaaaa	aaa€ V3R loRVV VVV3R lloVV	V3R loVVVVVVVVV3	R lp
SR9   g   Q hSR9   g   Q hV
# )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                ól   <€ SP                   R8X  d"   V RSP                  ,          ,
          R,          # V # )zö
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_schedules   &€r.   Úget_model_input_timeÚ+model_wrapper.<locals>.get_model_input_time  s4   ø€ ð ×"Ñ" jÔ0Ø  2¨×(>Ñ(>Õ#>Õ>À%ÕGÐGàÐr1   c                 óÈ  <€ VP                  R4      P                  ^ ,          ^8X  d#   VP                  V P                  ^ ,          4      pS! V4      pS	! W3/ S
B pSR8X  d   V# SR8X  d[   SP                  V4      SP	                  V4      reV P                  4       pV \        WW4      V,          ,
          \        Wg4      ,          # SR8X  d[   SP                  V4      SP	                  V4      reV P                  4       p\        WW4      V,          \        Wg4      V ,          ,           # SR8X  d5   SP	                  V4      pV P                  4       p\        Wg4      ) V,          # R# )r   rm   Úx_startÚvÚscoreNrZ   )r   ÚshapeÚexpandrE   rJ   r   Úexpand_dims)Úxrq   ÚcondÚt_inputÚoutputÚalpha_tÚsigma_tÚdimsrs   ÚmodelÚmodel_kwargsÚ
model_typerr   s   &&&     €€€€€r.   Únoise_pred_fnÚ$model_wrapper.<locals>.noise_pred_fn%  s;  ø€ Ø×Ñ Ó&×,Ñ,¨QÕ/°1Ô4Ø'×.Ñ.°·±¸µ
Ó<ˆLÙ& |Ó4ˆÙ�qÑ2 \Ñ2ˆØ˜Ô ØˆMØ˜9Ô$Ø-×<Ñ<¸\ÓJÈN×LgÑLgÐhtÓLu�WØ—5‘5“7ˆDØœ GÓ2°VÕ;Õ;¼{È7Ó?YÕYÐYØ˜3ÔØ-×<Ñ<¸\ÓJÈN×LgÑLgÐhtÓLu�WØ—5‘5“7ˆDÜ˜wÓ-°Õ6¼ÀWÓ9SÐVWÕ9WÕWÐWØ˜7Ô"Ø$×1Ñ1°,Ó?ˆGØ—5‘5“7ˆDÜ Ó.Ð.°Õ7Ð7ñ #r1   c                ó8  <€ \         P                  ! 4       ;_uu_ 4        V P                  4       P                  R4      pS! W!S3/ SB p\         P                  P                  VP                  4       V4      ^ ,          uuRRR4       #   + '       g   i     R# ; i)zM
Compute the gradient of the classifier, i.e. nabla_{x} log p_t(cond | x_t).
TN)r   Úenable_gradÚdetachÚrequires_grad_ÚautogradÚgradÚsum)r|   r~   Úx_inÚlog_probÚclassifier_fnÚclassifier_kwargsÚ	conditions   &&  €€€r.   Úcond_grad_fnÚ#model_wrapper.<locals>.cond_grad_fn9  sj   ø€ ô ×Ò× Õ Ø—8‘8“:×,Ñ,¨TÓ2ˆDÙ$ T°IÑSÐARÑSˆHÜ—>‘>×&Ñ& x§|¡|£~°tÓ<¸QÕ?÷ !× × Ó ús   ŸABÂB	c                óÄ  <	€ VP                  R4      P                  ^ ,          ^8X  d#   VP                  V P                  ^ ,          4      pSR8X  d	   S! W4      # SR8X  d_   S
f   Q hS! V4      pS! W4      pSP                  V4      pS! W4      pVS\	        WCP                  4       R7      ,          V,          ,
          # SR8X  d›   SR8X  g   Sf   S! WSR7      # \        P                  ! V .^,          4      p\        P                  ! V.^,          4      p\        P                  ! SS.4      pS! WgVR7      P                  ^4      w  r•V	SWY,
          ,          ,           # R# )	zC
The noise predicition model function that is used for DPM-Solver.
rn   Ú
classifierN©r‚   úclassifier-freer   )r}   rZ   )	r   ry   rz   rJ   r{   r   r   ÚcatÚchunk)r|   rq   r~   Ú	cond_gradr�   rm   r�   Út_inÚc_inÚnoise_uncondr‘   r”   r“   rs   Úguidance_scaleÚguidance_typer†   rr   Úunconditional_conditions   &&        €€€€€€€€€r.   Úmodel_fnÚmodel_wrapper.<locals>.model_fnB  sE  ø€ ð ×Ñ Ó&×,Ñ,¨QÕ/°1Ô4Ø'×.Ñ.°·±¸µ
Ó<ˆLØ˜HÔ$Ù  Ó1Ð1Ø˜lÔ*Ø Ò,Ð,Ð,Ù*¨<Ó8ˆGÙ$ QÓ0ˆIØ$×1Ñ1°,Ó?ˆGÙ! !Ó2ˆEØ˜>¬K¸ÇmÁmÃoÔ,VÕVÐYbÕbÕbÐbØÐ/Ô/Ø Ô#Ð'>Ò'FÙ$ Q¸9ÔEÐEä—y’y !  q¥Ó)�Ü—y’y , °!Õ!3Ó4�Ü—y’yÐ"9¸9Ð!EÓF�Ù&3°DÀTÔ&J×&PÑ&PÐQRÓ&SÑ#�Ø# n¸Õ8LÕ&MÕMÐMñ 0r1   ©N)rm   rv   rw   )rn   r—   r™   © )rƒ   rr   r…   r„   r¡   r“   r¢   r    r‘   r’   r£   r”   rs   r†   s   ffffffffff @@@r.   Úmodel_wrapperr§   µ   sK   ÿü€ õJ	 ÷8ó 8÷(@÷Nõ Nð2 Ð2Ô2Ð2Ð2ØÐGÔGÐGÐGØ€Or1   c                   ó|   a € ] tR tRt o RR ltRR ltR tR tR tR t	R	 t
R
 tR tRR ltRR ltRR ltRtV tR# )ÚUniPCi`  c                óN   € Wn         W n        W`n        W0n        W@n        WPn        R# )zJConstruct a UniPC.

We support both data_prediction and noise_prediction.
N)rƒ   rr   ÚvariantÚ
predict_x0ÚthresholdingÚmax_val)r(   r£   rr   r¬   r­   r®   r«   s   &&&&&&&r.   r/   ÚUniPC.__init__a  s$   € ð Œ
Ø,ÔØŒØ$ŒØ(ÔØŽr1   Nc           	     óÖ  € VP                  4       pV P                  p\        P                  ! \        P                  ! V4      P                  VP                  ^ ,          R34      V^R7      p\        \        P                  ! WPP                  \        P                  ! V4      P                  VP                  4      ,          4      V4      p\        P                  ! W) V4      V,          pV# )z"
The dynamic thresholding method.
r
   r   )r   Údynamic_thresholding_ratior   ÚquantileÚabsr   ry   r{   ÚmaximumÚthresholding_max_valÚ	ones_liker:   r;   Úclamp)r(   Úx0r<   r‚   Úpr5   s   &&&   r.   Údynamic_thresholding_fnÚUniPC.dynamic_thresholding_fnu  s¥   € ð �v‰v‹xˆØ×+Ñ+ˆÜ�NŠNœ5Ÿ9š9 R›=×0Ñ0°"·(±(¸1µ+¸rÐ1BÓCÀQÈAÔNˆÜœŸš a×)BÑ)BÄUÇ_Â_ÐUVÓEW×EZÑEZÐ[\×[cÑ[cÓEdÕ)dÓeÐgkÓlˆÜ�[Š[˜˜R Ó# aÕ'ˆØˆ	r1   c                ó$   € V P                  W4      # )z$
Return the noise prediction model.
)rƒ   ©r(   r|   r<   s   &&&r.   Únoise_prediction_fnÚUniPC.noise_prediction_fn€  s   € ð �z‰z˜!ÓÐr1   c           	     óÄ  € V P                  W4      pVP                  4       pV P                  P                  V4      V P                  P	                  V4      reV\        Wd4      V,          ,
          \        WT4      ,          pV P                  '       dÏ   Rp\        P                  ! \        P                  ! V4      P                  VP                  ^ ,          R34      V^R7      p	\        \        P                  ! W�P                  \        P                  ! V	4      P                  V	P                   4      ,          4      V4      p	\        P"                  ! Wy) V	4      V	,          pV# )z7
Return the data prediction model (with thresholding).
g×£p=
×ï?r
   r   )r¾   r   rr   rE   rJ   r{   r­   r   r²   r³   r   ry   r´   r®   r¶   r:   r;   r·   )
r(   r|   r<   rm   r‚   r€   r�   r¸   r¹   r5   s
   &&&       r.   Údata_prediction_fnÚUniPC.data_prediction_fn†  s  € ð ×(Ñ(¨Ó.ˆØ�u‰u‹wˆØ×.Ñ.×=Ñ=¸aÓ@À$×BUÑBU×BbÑBbÐcdÓBe�Ø”+˜gÓ,¨uÕ4Õ4¼ÀGÓ8RÕRˆØ××ÐØˆAÜ—’œuŸyšy¨›}×4Ñ4°b·h±h¸qµkÀ2Ð5FÓGÈÐPQÔRˆAÜœEŸMšM¨!¯\©\¼E¿OºOÈAÓ<N×<QÑ<QÐRS×RZÑRZÓ<[Õ-[Ó\Ð^bÓcˆAÜ—’˜R  QÓ'¨!Õ+ˆBØˆ	r1   c                ój   € V P                   '       d   V P                  W4      # V P                  W4      # )zO
Convert the model to the noise prediction model or the data prediction model.
)r¬   rÁ   r¾   r½   s   &&&r.   r£   ÚUniPC.model_fn•  s/   € ð �?�?ˆ?Ø×*Ñ*¨1Ó0Ð0à×+Ñ+¨AÓ1Ð1r1   c                ón  € VR8X  dþ   V P                   P                  \        P                  ! V4      P	                  V4      4      pV P                   P                  \        P                  ! V4      P	                  V4      4      p\        P
                  ! VP                  4       P                  4       VP                  4       P                  4       V^,           4      P	                  V4      pV P                   P                  V4      # VR8X  d.   \        P
                  ! W#V^,           4      P	                  V4      # VR8X  d^   ^p	\        P
                  ! VRV	,          ,          VRV	,          ,          V^,           4      P                  V	4      P	                  V4      p
V
# \        RP                  V4      4      h)z:Compute the intermediate time steps for sampling.
        ÚlogSNRÚtime_uniformÚtime_quadraticr   zSUnsupported skip_type {}, need to be 'logSNR' or 'time_uniform' or 'time_quadratic')rr   rR   r   Útensorr:   r   ÚcpuÚitemrc   Úpowr   r   )r(   Ú	skip_typeÚt_TÚt_0ÚNr;   Úlambda_TÚlambda_0ÚlogSNR_stepsÚt_orderr<   s   &&&&&&     r.   Úget_time_stepsÚUniPC.get_time_stepsž  s\  € ð ˜Ô Ø×*Ñ*×:Ñ:¼5¿<º<ÈÓ;L×;OÑ;OÐPVÓ;WÓXˆHØ×*Ñ*×:Ñ:¼5¿<º<ÈÓ;L×;OÑ;OÐPVÓ;WÓXˆHÜ Ÿ>š>¨(¯,©,«.×*=Ñ*=Ó*?ÀÇÁÃ×ATÑATÓAVÐXYÐ\]ÕX]Ó^×aÑaÐbhÓiˆLØ×&Ñ&×5Ñ5°lÓCÐCØ˜.Ô(Ü—>’> #¨A°­EÓ2×5Ñ5°fÓ=Ð=ØÐ*Ô*ØˆGÜ—’˜s R¨'¥\Õ2°C¸"¸w½,Õ4GÈÈQÍÓO×SÑSÐT[Ó\×_Ñ_Ð`fÓgˆAØˆHäÐr×yÑyð  {Dó  Eó  Fð  Fr1   c                óÞ  € V^8X  d|   V^,          ^,           pV^,          ^ 8X  d   ^.V^,
          ,          ^^.,           pM´V^,          ^8X  d   ^.V^,
          ,          ^.,           pMŒ^.V^,
          ,          ^.,           pMrV^8X  dM   V^,          ^ 8X  d   V^,          p^.V,          pMIV^,          ^,           p^.V^,
          ,          ^.,           pMV^8X  d   Tp^.V,          pM\        R4      hVR8X  d   V P                  W4WWV4      p	W˜3# V P                  W4WQV4      \        P                  ! \        P                  ! ^ .V,           4      ^ 4      P                  V4      ,          p	W˜3# )zG
Get the order of each step for sampling by the singlestep DPM-Solver.
z"'order' must be '1' or '2' or '3'.rÆ   )r   rÕ   r   r   rÉ   r:   )
r(   ÚstepsÚorderrÍ   rÎ   rÏ   r;   ÚKÚordersÚtimesteps_outers
   &&&&&&&   r.   Ú.get_orders_and_timesteps_for_singlestep_solverÚ4UniPC.get_orders_and_timesteps_for_singlestep_solver¯  su  € ð �AŒ:Ø˜•
˜Q•ˆAØ�q�y˜AŒ~Ø˜  Q¥�¨1¨a¨&Õ0‘Ø˜•˜a”Ø˜  Q¥�¨1¨#Õ-‘à˜  Q¥�¨1¨#Õ-‘Ø�aŒZØ�q�y˜AŒ~Ø˜Q•J�Ø˜ �‘à˜Q•J •N�Ø˜  Q¥�¨1¨#Õ-‘Ø�aŒZØˆAØ�T˜E•\‰FäÐAÓBÐBØ˜Ô à"×1Ñ1°)À#È&ÓQˆOð Ð&Ð&ð #×1Ñ1°)À#ÈfÓUÔV[×VbÒVbÔch×coÒcoÐqrÐptÐw}Õp}Óc~ð  ABó  WC÷  WFñ  WFð  GMó  WNõ  OˆOØÐ&Ð&r1   c                ó$   € V P                  W4      # )zw
Denoise at the final step, which is equivalent to solve the ODE from lambda_s to infty by first-order discretization.
)rÁ   )r(   r|   r5   s   &&&r.   Údenoise_to_zero_fnÚUniPC.denoise_to_zero_fnÎ  s   € ð ×&Ñ& qÓ,Ð,r1   c                óô   € \        VP                  4      ^ 8X  d   VP                  R4      pRV P                  9   d   V P                  ! WW4V3/ VB # V P                  R8X  g   Q hV P
                  ! WW4V3/ VB # )r   ÚbhÚ
vary_coeffr   )r   ry   Úviewr«   Úmultistep_uni_pc_bh_updateÚmultistep_uni_pc_vary_update)r(   r|   Úmodel_prev_listÚt_prev_listr<   rÙ   Úkwargss   &&&&&&,r.   Úmultistep_uni_pc_updateÚUniPC.multistep_uni_pc_updateÔ  su   € Üˆq�w‰w‹<˜1ÔØ—‘�r“
ˆAØ�4—<‘<ÔØ×2Ò2°1À{ÐW\ÑgÐ`fÑgÐgà—<‘< <Ô/Ð/Ð/Ø×4Ò4°QÈÐY^ÑiÐbhÑiÐir1   c           
     óÂ  € \         P                  ! R V R24       V P                  pV\        V4      8:  g   Q hVR,          pVP	                  V4      p	VP	                  V4      p
VR,          pVP                  V4      VP                  V4      rÜVP                  V4      p\        P                  ! V4      pW©,
          p. p. p\        ^V4       Fv  pVV^,           ) ,          pVV^,           ) ,          pVP	                  V4      pVV	,
          V,          pVP                  V4       VP                  VV,
          V,          4       Kx  	  VP                  R4       \        P                  ! VVP                  R7      p\        V4      p. p\        P                  ! V4      p\        ^V^,           4       F+  pVP                  V4       VV,          V^,           ,          pK-  	  \        P                  ! V^R7      p\        V4      ^ 8”  dG   \        P                  ! V^R7      p\        P                  P!                  VRR1RR13,          4      pTpV'       d"   \        P                  P!                  V4      pTpV P"                  '       d   V) MTp \        P$                  ! V 4      p!. p"^p#T!p$\        ^V^,           4       F;  pV"P                  V$4       V$V ,          ^V#,          ,
          p$V#V^,           ,          p#K=  	  Rp%V P"                  '       EdN   WÜ,          V,          VV!,          V,          ,
          p&T&p'\        V4      ^ 8”  d\   \        V^,
          4       FE  pV'VV"V^,           ,          ,          \        P&                  ! RVXV,          4      ,          ,
          p'KG  	  V'       d±   V P)                  V'V4      p%V%V,
          p(T&p'^ p\        V^,
          4       FH  pV'VV"V^,           ,          ,          \        P&                  ! RVXV,          RR 4      ,          ,
          p'KJ  	  V'VV"V,          ,          V(XV,          R,          ,          ,          ,
          p'V'V%3# VP                  V4      VP                  V4      pp)\        P                  ! VV),
          4      V,          VV!,          V,          ,
          p&T&p'\        V4      ^ 8”  d\   \        V^,
          4       FE  pV'VV"V^,           ,          ,          \        P&                  ! RVXV,          4      ,          ,
          p'KG  	  V'       d±   V P)                  V'V4      p%V%V,
          p(T&p'^ p\        V^,
          4       FH  pV'VV"V^,           ,          ,          \        P&                  ! RVXV,          RR 4      ,          ,
          p'KJ  	  V'VV"V,          ,          V(XV,          R,          ,          ,          ,
          p'V'V%3# )z-using unified predictor-corrector with order z (solver type: vary coeff)r   ©r;   r
   Nzbkchw,k->bchwr   )ÚloggingÚinforr   r   rR   rJ   r?   r   rC   ÚrangeÚappendrÉ   r;   r¶   ÚstackÚlinalgÚinvr¬   Úexpm1Úeinsumr£   )*r(   r|   rè   ré   r<   rÙ   Úuse_correctorÚnsÚt_prev_0Úlambda_prev_0Úlambda_tÚmodel_prev_0Úsigma_prev_0r�   r>   r€   ÚhÚrksÚD1sÚiÚt_prev_iÚmodel_prev_iÚlambda_prev_iÚrkrÚ   ÚCÚcolÚkÚC_inv_pÚA_pÚC_invÚA_cÚhhÚh_phi_1Úh_phi_ksÚfactorial_kÚh_phi_kÚmodel_tÚx_t_Úx_tÚD1_tÚlog_alpha_prev_0s*   &&&&&&&                                   r.   rç   Ú"UniPC.multistep_uni_pc_vary_updateÝ  sê  € Ü�ŠÐDÀUÀGÐKeÐfÔgØ× Ñ ˆØœ˜OÓ,Ô,Ð,Ð,ð ˜r•?ˆØ×*Ñ*¨8Ó4ˆØ×%Ñ% aÓ(ˆØ& rÕ*ˆØ "§¡°Ó 9¸2¿?¹?È1Ó;M�gØ×0Ñ0°Ó3ˆÜ—)’)˜KÓ(ˆàÕ$ˆàˆØˆÜ�q˜%–ˆAØ" Q¨¥U 8Õ,ˆHØ*¨Q°­U¨8Õ4ˆLØ×.Ñ.¨xÓ8ˆMØ -Õ/°1Õ4ˆBØ�J‰J�rŒNØ�J‰J˜ |Õ3°rÕ9Ö:ñ !ð 	�
‰
�2ŒÜ�lŠl˜3 q§x¡xÔ0ˆä�‹Hˆàˆä�oŠo˜cÓ"ˆÜ�q˜!˜a�%–ˆAØ�H‰H�SŒMØ˜•)˜q 1�uÕ%ŠCñ !ô �KŠK˜˜qÔ!ˆäˆs‹8�aŒ<Ü—+’+˜c qÔ)ˆCÜ—l‘l×&Ñ& q¨¨"¨¨c¨r¨c¨¥{Ó3ˆGØˆCçÜ—L‘L×$Ñ$ QÓ'ˆEØˆCà—?—?�?ˆa‰R¨ˆÜ—+’+˜b“/ˆØˆØˆØˆÜ�q˜!˜a�%–ˆAØ�O‰O˜GÔ$Ø •l Q¨¥_Õ4ˆGØ˜A �EÕ"ŠKñ !ð
 ˆØ�?�?‰?àÕ&¨Õ*Ø˜GÕ# lÕ2õ3ð ð
 ˆCÜ�3‹x˜!Œ|ä˜q 1�už�AØ ¨(°1°qµ5­/Õ 9¼E¿LºLÈÐZ]Ð_bÐcdÕ_eÓ<fÕ fÕf’Cñ &÷ ØŸ-™-¨¨QÓ/�Ø ,Õ.�Ø�Ø�Ü˜q 1�už�AØ ¨(°1°qµ5­/Õ 9¼E¿LºLÈÐZ]Ð_bÐcdÕ_eÐfiÐgiÐ_jÓ<kÕ kÕk’Cñ &à˜G h¨q¥kÕ1°T¸CÀ½FÀ2½JÕ5FÕGÕG�ð, �Gˆ|Ðð) -/×,FÑ,FÀxÓ,PÐRT×RlÑRlÐmnÓRo˜kÐä—’˜;Ð)9Õ9Ó:¸aÕ?Ø˜WÕ$¨Õ4õ5ð ð
 ˆCÜ�3‹x˜!Œ|ä˜q 1�už�AØ ¨(°1°qµ5­/Õ 9¼E¿LºLÈÐZ]Ð_bÐcdÕ_eÓ<fÕ fÕf’Cñ &÷ ØŸ-™-¨¨QÓ/�Ø ,Õ.�Ø�Ø�Ü˜q 1�už�AØ ¨(°1°qµ5­/Õ 9¼E¿LºLÈÐZ]Ð_bÐcdÕ_eÐfiÐgiÐ_jÓ<kÕ kÕk’Cñ &à˜G h¨q¥kÕ1°T¸CÀ½FÀ2½JÕ5FÕGÕG�Ø�Gˆ|Ðr1   c                ó  € V P                   pV\        V4      8:  g   Q hVP                  4       p	VR
,          p
VP                  V
4      pVP                  V4      pVR
,          pVP	                  V
4      VP	                  V4      rþVP                  V
4      VP                  V4      pp\        P                  ! V4      pWË,
          p. p. p\        ^V4       F}  pVV^,           ) ,          pVV^,           ) ,          pVP                  V4      pVV,
          V,          ^ ,          pVP                  V4       VP                  VV,
          V,          4       K  	  VP                  R4       \        P                  ! VVP                  R7      p. p. pV P                  '       d   V^ ,          ) MV^ ,          p\        P                  ! V4      pVV,          ^,
          p^p V P                  R8X  d   Tp!M2V P                  R8X  d   \        P                  ! V4      p!M
\        4       h\        ^V^,           4       Fv  pVP                  \        P                   ! VV^,
          4      4       VP                  VV ,          V!,          4       V V^,           ,          p VV,          ^V ,          ,
          pKx  	  \        P"                  ! V4      p\        P                  ! VVP                  R7      p\        V4      ^ 8„  ;'       d    VRJ p"\        V4      ^ 8”  dy   \        P"                  ! V^R7      pVf\   V^8X  d%   \        P                  ! R.VP                  R7      p#M3\        P$                  P'                  VRR
1RR
13,          VRR
 4      p#MRpV'       dL   V^8X  d%   \        P                  ! R.VP                  R7      p$M \        P$                  P'                  VV4      p$Rp%V P                  '       Ed	   \)        Wþ,          V	4      V,          \)        VV,          V	4      V,          ,
          p&VfJ   V"'       d   \        P*                  ! VX#^.^ .3R7      p'M^ p'V&\)        VV!,          V	4      V',          ,
          pV'       dx   V P-                  Wd4      p%Ve"   \        P*                  ! VX$RR
 ^.^ .3R7      p(M^ p(V%V,
          p)V&\)        VV!,          V	4      V(X$R
,          V),          ,           ,          ,
          pVV%3# \)        \        P                  ! VV,
          4      V	4      V,          \)        VV,          V	4      V,          ,
          p&VfE   V"'       d   \        P.                  ! R	X#V4      p'M^ p'V&\)        VV!,          V	4      V',          ,
          pV'       ds   V P-                  Wd4      p%Ve   \        P.                  ! R	X$RR
 V4      p(M^ p(V%V,
          p)V&\)        VV!,          V	4      V(X$R
,          V),          ,           ,          ,
          pVV%3# )r   r   rî   Úbh1Úbh2Nr
   r	   r˜   zk,bkchw->bchwr   )rr   r   r   rR   rJ   r?   r   rC   rñ   rò   rÉ   r;   r¬   rö   r«   ÚNotImplementedErrorrÌ   ró   rô   Úsolver{   Ú	tensordotr£   r÷   )*r(   r|   rè   ré   r<   rÙ   r  rø   rù   r‚   rú   rû   rü   rý   rþ   r�   r  r>   r€   rÿ   r   r  r  r  r  r  r  ÚRÚbr  r  r  Úfactorial_iÚB_hÚuse_predictorÚrhos_pÚrhos_cr  r  Úpred_resÚcorr_resr  s*   &&&&&&&&                                  r.   ræ   Ú UniPC.multistep_uni_pc_bh_updateC  sù  € à× Ñ ˆØœ˜OÓ,Ô,Ð,Ð,Ø�u‰u‹wˆð ˜r•?ˆØ×*Ñ*¨8Ó4ˆØ×%Ñ% aÓ(ˆØ& rÕ*ˆØ "§¡°Ó 9¸2¿?¹?È1Ó;M�gØ(*×(BÑ(BÀ8Ó(LÈb×NhÑNhÐijÓNk˜+ÐÜ—)’)˜KÓ(ˆàÕ$ˆàˆØˆÜ�q˜%–ˆAØ" Q¨¥U 8Õ,ˆHØ*¨Q°­U¨8Õ4ˆLØ×.Ñ.¨xÓ8ˆMØ  =Õ0°AÕ5°qÕ9ˆBØ�J‰J�rŒNØ�J‰J˜ |Õ3°rÕ9Ö:ñ !ð 	�
‰
�2ŒÜ�lŠl˜3 q§x¡xÔ0ˆàˆØˆà—o—o�oˆa��d‰U¨1¨Q­4ˆÜ—+’+˜b“/ˆØ˜B•, Õ"ˆàˆà�<‰<˜5Ô Ø‰CØ�\‰\˜UÔ"Ü—+’+˜b“/‰Cä%Ó'Ð'ä�q˜% !�)Ö$ˆAØ�H‰H”U—Y’Y˜s A¨¥EÓ*Ô+Ø�H‰H�W˜{Õ*¨SÕ0Ô1Ø˜A �EÕ"ˆKØ •l Q¨¥_Õ4ŠGñ	 %ô �KŠK˜‹NˆÜ�LŠL˜ 1§8¡8Ô,ˆô ˜C› 1™×4Ð4¨°¨ˆÜˆs‹8�aŒ<Ü—+’+˜c qÔ)ˆCØŠ{à˜A”:Ü"Ÿ\š\¨3¨%¸¿¹ÔA‘Fä"Ÿ\™\×/Ñ/°°#°2°#°s¸°s°(µ¸Q¸sÀ¸VÓD�FøàˆCçð ˜ŒzÜŸš s e°A·H±HÔ=‘äŸ™×+Ñ+¨A¨qÓ1�àˆØ�?�?‰?ä˜GÕ2°DÓ9¸AÕ=Ü˜g¨Õ/°Ó6¸ÕDõEð ð
 Š{ß Ü$Ÿš¨s°FÀ1À#ÈÀsÀÔL‘Hà �HØœ[¨°3­¸Ó=ÀÕHÕH�çØŸ-™-¨Ó/�Ø’?Ü$Ÿš¨s°F¸3¸B°KÈÀsÈQÈCÀjÔQ‘Hà �HØ ,Õ.�Øœ[¨°3­¸Ó=ÀÈFÐSUÍJÐY]ÕL]ÕA]Õ^Õ^�ð* �Gˆ|Ðô% œEŸIšI kÐ4DÕ&DÓEÀtÓLÈqÕPÜ˜g¨Õ/°Ó6¸ÕEõFð ð Š{ß Ü$Ÿ|š|¨O¸VÀSÓI‘Hà �HØœ[¨°3­¸Ó=ÀÕHÕH�çØŸ-™-¨Ó/�Ø’?Ü$Ÿ|š|¨O¸VÀCÀR¸[È#ÓN‘Hà �HØ ,Õ.�Øœ[¨°3­¸Ó=ÀÈFÐSUÍJÐY]ÕL]ÕA]Õ^Õ^�Ø�Gˆ|Ðr1   c                óÀ  € \        V4      ^,
          pVR8X  Ed<   VV8¼  g   Q hVP                  ^ ,          ^,
          V8X  g   Q h\        VVR7       EFý  pV^ 8X  dB   V^ ,          P                  VP                  ^ ,          4      pV P	                  VV4      .pV.pEM•VV8  d€   TpVV,          P                  VP                  ^ ,          4      pV P                  VXXVVRR7      w  ppVf   V P	                  VV4      pVP                  V4       VP                  V4       EM^ pVV^,
          8X  d   ^p\        VV^,           V,           4       Fß  pVV,          P                  VP                  ^ ,          4      pV'       d   \        VV^,           V,
          4      pMTpVV8X  d   RpMRpV P                  VXXVVVR7      w  pp\        V^,
          4       F)  pVV^,           ,          VV&   VV^,           ,          VV&   K+  	  VVR	&   VV8  g   KÄ  Vf   V P	                  VV4      pVVR	&   Ká  	  Vf   EKè  V! RVRVRXR	,          /4       EK   	  V# \        4       h)
r   Ú	multistep)ÚdisableT)rø   Fr|   r  Údenoisedr   )
r   ry   r   rz   r£   rë   rò   rñ   Úminr  )r(   r|   Ú	timestepsÚt_startÚt_endrÙ   rÍ   ÚmethodÚlower_order_finalÚdenoise_to_zeroÚsolver_typeÚatolÚrtolÚ	correctorÚcallbackÚdisable_pbarrØ   Ú
step_indexÚvec_trè   ré   Ú
init_orderÚmodel_xÚextra_final_stepÚstepÚ
step_orderrø   r  s   &&&&&&&&&&&&&&&&            r.   ÚsampleÚUniPC.sample¼  sg  € ô �I“ Õ"ˆØ�[Õ Ø˜E”>Ð!�>à—?‘? 1Õ%¨Õ)¨UÔ2Ð2Ð2ä$ U°L×AÐA�
Ø ”?Ø% a�L×/Ñ/°·±¸µÓ=�EØ'+§}¡}°Q¸Ó'>Ð&?�OØ#( '’KØ %Ô'Ø!+�Jð & jÕ1×8Ñ8¸¿¹À½ÓD�EØ!%×!=Ñ!=¸aÀÐR]Ð_dÐfpð  AEÐ!=ó  "F‘J�A�wØ’Ø"&§-¡-°°5Ó"9˜Ø#×*Ñ*¨7Ô3Ø×&Ñ& uÖ-à'(Ð$Ø! e¨a¥iÔ0Ø+,Ð(Ü % j°*¸qµ.ÐCSÕ2SÖ T˜Ø )¨$¥× 6Ñ 6°q·w±w¸qµzÓ B˜ß,Ü),¨U°E¸AµIÀÕ4DÓ)E™Jà).˜Jà 5œ=à,1™Mà,0˜MØ&*×&BÑ&BÀ1ÀoÐWbÐdiÐkuð  FSÐ&Bó  'T™
˜˜7Ü!& u¨q¥yÖ!1˜AØ-8¸¸Q½Õ-?˜K¨™NØ1@ÀÀQÅÕ1G˜O¨AÓ.ñ "2ð +0˜ B™à %ž<Ø&šØ*.¯-©-¸¸5Ó*A Ø29˜O¨BÓ/ñ+ !Uð, Õ'Ù˜c 1 c¨:°zÀ?ÐSUÕCVÐW×XñU Bð^ ˆô &Ó'Ð'r1   )r®   rƒ   rr   r¬   r­   r«   )TFr   r  r¥   )T)NT)NNé   rÇ   Ú
singlestepTFÚ
dpm_solvergÞqŠŽäò?gš™™™™™©?FNF)re   rf   rg   rh   r/   rº   r¾   rÁ   r£   rÕ   rÝ   rà   rë   rç   ræ   rA  ri   rj   rk   s   @r.   r©   r©   `  sL   ø‡ € ôô(	ò òò2òFò"'ò>-òjôdôLv÷r;ò ;r1   r©   c                ó¤  € V P                   ^ ,          VP                   ^,          rC\        P                  ! V P                  ^4      VP                  ^ 4      P	                  V^^34      .^R7      p\        P
                  ! V^R7      w  rg\        P                  ! V^R7      pV^,
          p	\        P                  ! \        P                  ! V^ 4      \        P                  ! ^V P                  R7      \        P                  ! \        P                  ! W„4      \        P                  ! V^,
          V P                  R7      V	4      4      p
\        P                  ! \        P                  ! W©4      V
^,           V
^,           4      p\        P                  ! V^V
P                  ^4      R7      P                  ^4      p\        P                  ! V^VP                  ^4      R7      P                  ^4      p\        P                  ! \        P                  ! V^ 4      \        P                  ! ^ V P                  R7      \        P                  ! \        P                  ! W„4      \        P                  ! V^,
          V P                  R7      V	4      4      pVP                  ^ 4      P                  VRR4      p\        P                  ! V^VP                  ^4      R7      P                  ^4      p\        P                  ! V^V^,           P                  ^4      R7      P                  ^4      pVW,
          VV,
          ,          WÜ,
          ,          ,           pV# )aY  
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   Úindexr   )ry   r   rš   Ú	unsqueezeÚrepeatÚsortÚargminÚwhereÚeqrÉ   r;   ÚgatherÚsqueezerz   )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Úcands   &&&                r.   r9   r9   þ  sI  € ð �7‰7�1�:�r—x‘x •{€qÜ�IŠI�q—{‘{ 1“~ r§|¡|°A£×'=Ñ'=¸qÀ!ÀQ¸iÓ'HÐIÈqÔQ€EÜ#Ÿjšj¨°AÔ6Ñ€LÜ�LŠL˜¨Ô*€EØ˜Q•Y€NÜ—’Ü�Š�˜ÓÜ�Š�Q˜qŸx™xÔ(Ü�ŠÜ�HŠH�UÓ¤§¢¨Q°­U¸1¿8¹8Ô DÀnó	
ó€Iô �kŠkœ%Ÿ(š( 9Ó=¸yÈ1½}ÈiÐZ[ÍmÓ\€GÜ�lŠl˜<¨Q°i×6IÑ6IÈ!Ó6LÔM×UÑUÐVWÓX€GÜ�LŠL˜¨1°G×4EÑ4EÀaÓ4HÔI×QÑQÐRSÓT€EÜ—’Ü�Š�˜ÓÜ�Š�Q˜qŸx™xÔ(Ü�ŠÜ�HŠH�UÓ¤§¢¨Q°­U¸1¿8¹8Ô DÀnó	
ó€Jð Ÿ<™<¨›?×1Ñ1°!°R¸Ó<ÐÜ�lŠlÐ/°Q¸j×>RÑ>RÐSTÓ>UÔV×^Ñ^Ð_`Óa€GÜ�LŠLÐ-°1¸ZÈ!½^×<VÑ<VÐWXÓ<YÔZ×bÑbÐcdÓe€EØ�a•k e¨g¥oÕ6¸%½/ÕJÕJ€DØ€Kr1   c                ó>   € V RRV^,
          ,          ,           ,          # )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¦   )rw   r‚   s   &&r.   r{   r{   (  s   € ð ˆV�g˜t a�xÕ(Õ(Õ)Ð)r1   c                   ó<   a € ] tR tRt o RtR tR tR tR tRt	V t
R# )	ÚSigmaConverti5  Ú c                ód   € R \         P                  ! ^W,          ^,           ,          4      ,          # )r	   )r   r   )r(   Úsigmas   &&r.   r?   Ú$SigmaConvert.marginal_log_mean_coeff7  s"   € Ø”U—Y’Y˜q U¥]°aÕ$7Õ8Ó9Õ9Ð9r1   c                óL   € \         P                  ! V P                  V4      4      # r¥   rB   rD   s   &&r.   rE   ÚSigmaConvert.marginal_alpha:  s   € Ü�yŠy˜×5Ñ5°aÓ8Ó9Ð9r1   c           	     ó�   € \         P                  ! R \         P                  ! RV P                  V4      ,          4      ,
          4      # r4   rH   rD   s   &&r.   rJ   ÚSigmaConvert.marginal_std=  s/   € Ü�zŠz˜"œuŸyšy¨¨d×.JÑ.JÈ1Ó.MÕ)MÓNÕNÓOÐOr1   c                ó²   € V P                  V4      pR\        P                  ! R\        P                  ! RV,          4      ,
          4      ,          pW#,
          # rM   rN   rO   s   &&  r.   rR   ÚSigmaConvert.marginal_lambda@  rT   r1   r¦   N)re   rf   rg   rh   r   r?   rE   rJ   rR   ri   rj   rk   s   @r.   rb  rb  5  s$   ø‡ € Ø€Hò:ò:òP÷(ð (r1   rb  c                 óê   € VP                  VP                  R ,          RVP                  ^,
          ,          ,           4      pW^,          R,           R,          ,          pW! W3/ VB ,
          V,          # ):Nr   Nr   r	   rY   )rå   ry   Úndim)rƒ   ÚinputÚsigma_inrê   re  s   &&&, r.   Úpredict_eps_sigmarq  H  s[   € Ø�M‰M˜(Ÿ.™.¨Õ,¨t°u·z±zÀAµ~Õ/FÕFÓG€EØ˜q•j 3Õ&¨3Õ.Õ/€EØ�U˜5Ñ5¨fÑ5Õ5¸Õ>Ð>r1   c                 óæ  a € VP                  4       pVR,          ^ 8X  d   VR,          pRVR&   MVP                  4       p\        4       pV\        P                  ! RV^ ,          R,          ,           4      ,          pRp	\	        V 3R lVV	RVR7      p
\        ^\        V4      ^,
          4      p\        W¨R	R
VR7      pVP                  WRRVR	WER7      pWØP                  VR,          4      ,          pV# )r   :NNNgü©ñÒMbP?r   r   rm   c                 ó   <€ \        SW3/ VB # r¥   )rq  )ro  re  rê   rƒ   s   &&,€r.   r6   Úsample_unipc.<locals>.<lambda>[  s   ø€ Ô+<¸UÀEÑ+[ÐTZÒ+[r1   rn   )r…   r¡   r„   TF)r¬   r­   r«   rÇ   r*  )r.  rÍ   r1  rÙ   r2  r8  r9  r   )
Úclonerb  r   rI   r§   r-  r   r©   rA  rE   )rƒ   rm   ÚsigmasÚ
extra_argsr8  r+  r«   r.  rù   r…   r£   rÙ   Úuni_pcr|   s   f&&&&&&       r.   Úsample_unipcry  N  sé   ø€ Ø—L‘L“Nˆ	Ø�"�:˜Œ?Ø˜q�	ˆIØ!ˆI�bŠMàŸ™›ˆIÜ‹^ˆàœŸ
š
 3¨°1­¸Õ)<Õ#<Ó=Õ=ˆØˆ
ä Ü[ØØ!Ø"Ø#ô
ˆô �A”s˜9“~¨Õ)Ó*ˆÜ�x°À5ÐRYÔZˆØ�M‰M˜%ÀÐWbÐjoð  DHð  S[ˆMó  rˆØ	×Ñ˜y¨�}Ó-Õ-ˆØˆr1   c           
      ó    € \        WW#WER R7      # )r  )r«   )ry  )rƒ   rm   rv  rw  r8  r+  s   &&&&&&r.   Úsample_unipc_bh2r{  h  s   € Ü˜ f¸(ÐUZÔ[Ð[r1   )NNFr  )NNF)r   r"   rï   Ú	tqdm.autor   r   r§   r©   r9   r{   rb  rq  ry  r{  r¦   r1   r.   Ú<module>r}     ss   ðó Û Û å ÷hñ hð\ ØØØØ ØØØôh÷VWñ Wò|'òT
*÷(ñ (ò&?ôö4\r1   