
    i              	       
   d dl Z d dlmZmZmZmZmZ d dlZd dl	Z	ddl
mZmZ ddlmZmZ ddlmZmZmZ  e       rd dlZ	 	 dded	ed
ed   de	j0                  fdZde	j0                  de	j0                  fdZ G d dee      Zy)    N)ListLiteralOptionalTupleUnion   )ConfigMixinregister_to_config)	deprecateis_scipy_available   )KarrasDiffusionSchedulersSchedulerMixinSchedulerOutputnum_diffusion_timestepsmax_betaalpha_transform_type)cosineexpreturnc           
      $   |dk(  rd }n|dk(  rd }nt        d|       g }t        |       D ]<  }|| z  }|dz   | z  }|j                  t        d ||       ||      z  z
  |             > t	        j
                  |t        j                        S )aB  
    Create a beta schedule that discretizes the given alpha_t_bar function, which defines the cumulative product of
    (1-beta) over time from t = [0,1].

    Contains a function alpha_bar that takes an argument t and transforms it to the cumulative product of (1-beta) up
    to that part of the diffusion process.

    Args:
        num_diffusion_timesteps (`int`):
            The number of betas to produce.
        max_beta (`float`, defaults to `0.999`):
            The maximum beta to use; use values lower than 1 to avoid numerical instability.
        alpha_transform_type (`"cosine"` or `"exp"`, defaults to `"cosine"`):
            The type of noise schedule for `alpha_bar`. Choose from `cosine` or `exp`.

    Returns:
        `torch.Tensor`:
            The betas used by the scheduler to step the model outputs.
    r   c                 f    t        j                  | dz   dz  t         j                  z  dz        dz  S )NgMb?gT㥛 ?r   )mathcospits    y/home/obispo/Crisostomo_bridge/mision_env/lib/python3.12/site-packages/diffusers/schedulers/scheduling_unipc_multistep.pyalpha_bar_fnz)betas_for_alpha_bar.<locals>.alpha_bar_fn<   s-    88QY%/$''9A=>!CC    r   c                 2    t        j                  | dz        S )Ng      ()r   r   r   s    r   r   z)betas_for_alpha_bar.<locals>.alpha_bar_fnA   s    88AI&&r    z"Unsupported alpha_transform_type: r   dtype)
ValueErrorrangeappendmintorchtensorfloat32)r   r   r   r   betasit1t2s           r   betas_for_alpha_barr/   "   s    0 x'	D 
	&	' =>R=STUUE*+ M((!e..S\"-R0@@@(KLM <<U]]33r    r+   c                 (   d| z
  }t        j                  |d      }|j                         }|d   j                         }|d   j                         }||z  }||||z
  z  z  }|dz  }|dd |dd z  }t        j                  |dd |g      }d|z
  } | S )a:  
    Rescales betas to have zero terminal SNR Based on https://huggingface.co/papers/2305.08891 (Algorithm 1)

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

    Returns:
        `torch.Tensor`:
            Rescaled betas with zero terminal SNR.
          ?r   dimr   r   N)r(   cumprodsqrtclonecat)r+   alphasalphas_cumprodalphas_bar_sqrtalphas_bar_sqrt_0alphas_bar_sqrt_T
alphas_bars          r   rescale_zero_terminal_snrr?   P   s     5[F]]6q1N$))+O (*002'+113 ((O (,=@Q,QRRO !!#J^j"o-FYY
1Q01FJELr    c            8       z   e Zd ZdZeD  cg c]  }|j
                   c}} ZdZeddddddd	d
dddddg dd
d
d
d
ddddd
d
dfde	de
de
ded   deeej                  ee
   f      de	ded   dede
de
ded ed!   d"ed#ee	   d$ee   d%ee   d&ee   d'ee   d(ee   d)ee
   d*ed+   d,e	d-eed.      d/ed0ed1ed   d2df6d3       Zed2ee	   fd4       Zed2ee	   fd5       Zd_d6e	d2dfd7Z	 d`d8e	d9eeeej4                  f      d:ee
   d2dfd;Zd<ej8                  d2ej8                  fd=Zd>ej                  d?ej                  d2ej                  fd@Zd>ej8                  d2eej8                  ej8                  f   fdAZ dBej8                  d8e	d2ej8                  fdCZ!dBej8                  d8e	d2ej8                  fdDZ"	 dadBej8                  d8e	dEe
dFe
d2ej8                  f
dGZ#ddHdIej8                  d<ej8                  d2ej8                  fdJZ$dddKdIej8                  d<ej8                  dLe	d2ej8                  fdMZ%ddddNdOej8                  dPej8                  dQej8                  dLe	d2ej8                  f
dRZ&	 dbdSee	ej8                  f   dTeej8                     d2e	fdUZ'dSee	ej8                  f   d2dfdVZ(	 dcdIej8                  dSee	ej8                  f   d<ej8                  dWed2ee)ef   f
dXZ*d<ej8                  d2ej8                  fdYZ+dZej8                  d[ej8                  d\ejX                  d2ej8                  fd]Z-d2e	fd^Z.yc c}} w )dUniPCMultistepScheduleru  
    `UniPCMultistepScheduler` is a training-free framework designed for the fast sampling of diffusion models.

    This model inherits from [`SchedulerMixin`] and [`ConfigMixin`]. Check the superclass documentation for the generic
    methods the library implements for all schedulers such as loading and saving.

    Args:
        num_train_timesteps (`int`, defaults to 1000):
            The number of diffusion steps to train the model.
        beta_start (`float`, defaults to 0.0001):
            The starting `beta` value of inference.
        beta_end (`float`, defaults to 0.02):
            The final `beta` value.
        beta_schedule (`"linear"`, `"scaled_linear"`, or `"squaredcos_cap_v2"`, defaults to `"linear"`):
            The beta schedule, a mapping from a beta range to a sequence of betas for stepping the model. Choose from
            `linear`, `scaled_linear`, or `squaredcos_cap_v2`.
        trained_betas (`np.ndarray`, *optional*):
            Pass an array of betas directly to the constructor to bypass `beta_start` and `beta_end`.
        solver_order (`int`, defaults to `2`):
            The UniPC order which can be any positive integer. The effective order of accuracy is `solver_order + 1`
            due to the UniC. It is recommended to use `solver_order=2` for guided sampling, and `solver_order=3` for
            unconditional sampling.
        prediction_type (`"epsilon"`, `"sample"`, `"v_prediction"`, or `"flow_prediction"`, defaults to `"epsilon"`, *optional*):
            Prediction type of the scheduler function; can be `epsilon` (predicts the noise of the diffusion process),
            `sample` (directly predicts the noisy sample`), `v_prediction` (see section 2.4 of [Imagen
            Video](https://huggingface.co/papers/2210.02303) paper), or `flow_prediction`.
        thresholding (`bool`, defaults to `False`):
            Whether to use the "dynamic thresholding" method. This is unsuitable for latent-space diffusion models such
            as Stable Diffusion.
        dynamic_thresholding_ratio (`float`, defaults to 0.995):
            The ratio for the dynamic thresholding method. Valid only when `thresholding=True`.
        sample_max_value (`float`, defaults to 1.0):
            The threshold value for dynamic thresholding. Valid only when `thresholding=True` and `predict_x0=True`.
        predict_x0 (`bool`, defaults to `True`):
            Whether to use the updating algorithm on the predicted x0.
        solver_type (`"bh1"` or `"bh2"`, defaults to `"bh2"`):
            Solver type for UniPC. It is recommended to use `bh1` for unconditional sampling when steps < 10, and `bh2`
            otherwise.
        lower_order_final (`bool`, default `True`):
            Whether to use lower-order solvers in the final steps. Only valid for < 15 inference steps. This can
            stabilize the sampling of DPMSolver for steps < 15, especially for steps <= 10.
        disable_corrector (`list`, default `[]`):
            Decides which step to disable the corrector to mitigate the misalignment between `epsilon_theta(x_t, c)`
            and `epsilon_theta(x_t^c, c)` which can influence convergence for a large guidance scale. Corrector is
            usually disabled during the first few steps.
        solver_p (`SchedulerMixin`, default `None`):
            Any other scheduler that if specified, the algorithm becomes `solver_p + UniC`.
        use_karras_sigmas (`bool`, *optional*, defaults to `False`):
            Whether to use Karras sigmas for step sizes in the noise schedule during the sampling process. If `True`,
            the sigmas are determined according to a sequence of noise levels {σi}.
        use_exponential_sigmas (`bool`, *optional*, defaults to `False`):
            Whether to use exponential sigmas for step sizes in the noise schedule during the sampling process.
        use_beta_sigmas (`bool`, *optional*, defaults to `False`):
            Whether to use beta sigmas for step sizes in the noise schedule during the sampling process. Refer to [Beta
            Sampling is All You Need](https://huggingface.co/papers/2407.12173) for more information.
        use_flow_sigmas (`bool`, *optional*, defaults to `False`):
            Whether to use flow sigmas for step sizes in the noise schedule during the sampling process.
        timestep_spacing (`"linspace"`, `"leading"`, or `"trailing"`, defaults to `"linspace"`):
            The way the timesteps should be scaled. Refer to Table 2 of the [Common Diffusion Noise Schedules and
            Sample Steps are Flawed](https://huggingface.co/papers/2305.08891) for more information.
        steps_offset (`int`, defaults to 0):
            An offset added to the inference steps, as required by some model families.
        final_sigmas_type (`"zero"` or `"sigma_min"`, defaults to `"zero"`):
            The final `sigma` value for the noise schedule during the sampling process. If `"sigma_min"`, the final
            sigma is the same as the last sigma in the training schedule. If `zero`, the final sigma is set to 0.
        rescale_betas_zero_snr (`bool`, defaults to `False`):
            Whether to rescale the betas to have zero terminal SNR. This enables the model to generate very bright and
            dark samples instead of limiting it to samples with medium brightness. Loosely related to
            [`--offset_noise`](https://github.com/huggingface/diffusers/blob/74fd735eb073eb1d774b1ab4154a0876eb82f055/examples/dreambooth/train_dreambooth.py#L506).
    r   i  g-C6?g{Gz?linearNr   epsilonFgףp=
?r1   Tbh2linspacer   zeroexponentialnum_train_timesteps
beta_startbeta_endbeta_schedule)rB   scaled_linearsquaredcos_cap_v2trained_betassolver_orderprediction_type)rC   samplev_predictionflow_predictionthresholdingdynamic_thresholding_ratiosample_max_value
predict_x0solver_typebh1rD   lower_order_finaldisable_correctorsolver_puse_karras_sigmasuse_exponential_sigmasuse_beta_sigmasuse_flow_sigmas
flow_shifttimestep_spacing)rE   leadingtrailingsteps_offsetfinal_sigmas_type)rF   	sigma_minrescale_betas_zero_snruse_dynamic_shiftingtime_shift_typer   c                    | j                   j                  rt               st        d      t	        | j                   j                  | j                   j
                  | j                   j                  g      dkD  rt        d      |+t        j                  |t        j                        | _        n|dk(  r-t        j                  |||t        j                        | _        nk|dk(  r6t        j                  |dz  |dz  |t        j                        dz  | _        n0|d	k(  rt        |      | _        nt        | d
| j                         |rt!        | j                        | _        d| j                  z
  | _        t        j$                  | j"                  d      | _        |rd| j&                  d<   t        j(                  | j&                        | _        t        j(                  d| j&                  z
        | _        t        j.                  | j*                        t        j.                  | j,                        z
  | _        d| j&                  z
  | j&                  z  dz  | _        d| _        |dvr1|dv r| j7                  d       nt        | d
| j                         || _        d | _        t=        j                  d|dz
  |t<        j                        d d d   j?                         }t        j@                  |      | _!        d g|z  | _"        d g|z  | _#        d| _$        || _%        || _&        d | _'        d | _(        d | _)        | j2                  jU                  d      | _        y )Nz:Make sure to install scipy if you want to use beta sigmas.r   znOnly one of `config.use_beta_sigmas`, `config.use_exponential_sigmas`, `config.use_karras_sigmas` can be used.r"   rB   rL         ?r   rM   z is not implemented for r1   r   r2   g      p>r4   rY   )midpointheunlogrhorD   )rX   cpu)+configr`   r   ImportErrorsumr_   r^   r$   r(   r)   r*   r+   rE   r/   NotImplementedError	__class__r?   r9   r5   r:   r6   alpha_tsigma_tloglambda_tsigmasinit_noise_sigmar
   rW   num_inference_stepsnpcopy
from_numpy	timestepsmodel_outputstimestep_listlower_order_numsr\   r]   last_sample_step_index_begin_indexto)selfrH   rI   rJ   rK   rN   rO   rP   rT   rU   rV   rW   rX   r[   r\   r]   r^   r_   r`   ra   rb   rc   rf   rg   ri   rj   rk   r   s                               r   __init__z UniPCMultistepScheduler.__init__   s   < ;;&&/A/CZ[[++T[[-O-OQUQ\Q\QnQnopstt A  $m5==IDJh&
H>QY^YfYfgDJo-
C3H[chcpcpquvvDJ11,-@ADJ%7OPTP^P^O_&`aa!24::>DJDJJ&#mmDKKQ?! '-D# zz$"5"56zz!d&9&9"9:		$,,/%))DLL2IID///43F3FF3N !$n,<<''E':)[M9QRVR`R`Qa*bcc$#' KK#6#:<OWYWaWabcgegcghmmo	)))4"Vl2"Vl2 !!2  kknnU+r    c                     | j                   S )zg
        The index counter for current timestep. It will increase 1 after each scheduler step.
        )r   r   s    r   
step_indexz"UniPCMultistepScheduler.step_index  s    
 r    c                     | j                   S )zq
        The index for the first timestep. It should be set from pipeline with `set_begin_index` method.
        r   r   s    r   begin_indexz#UniPCMultistepScheduler.begin_index   s    
    r    r   c                     || _         y)z
        Sets the begin index for the scheduler. This function should be run from pipeline before the inference.

        Args:
            begin_index (`int`, defaults to `0`):
                The begin index for the scheduler.
        Nr   )r   r   s     r   set_begin_indexz'UniPCMultistepScheduler.set_begin_index(  s     (r    r}   devicemuc           	         |U| j                   j                  r| j                   j                  dk(  sJ t        j                  |      | j                   _        | j                   j                  dk(  rut        j                  d| j                   j                  dz
  |dz         j                         ddd   dd j                         j                  t        j                        }nl| j                   j                  dk(  r| j                   j                  |dz   z  }t        j                  d|dz         |z  j                         ddd   dd j                         j                  t        j                        }|| j                   j                  z  }n| j                   j                  dk(  r| j                   j                  |z  }t        j                  | j                   j                  d|       j                         j                         j                  t        j                        }|dz  }n"t        | j                   j                   d	      t        j                   d| j"                  z
  | j"                  z  d
z        }| j                   j$                  r t        j&                  |      }t        j(                  |      j                         }| j+                  ||      }t        j                   |D cg c]  }| j-                  ||       c}      j                         }| j                   j.                  dk(  r|d   }	n>| j                   j.                  dk(  rd}	n"t        d| j                   j.                         t        j0                  ||	gg      j                  t        j2                        }n| j                   j4                  rt        j&                  |      }t        j(                  |      j                         }| j7                  ||      }t        j                   |D cg c]  }| j-                  ||       c}      }| j                   j.                  dk(  r|d   }	n>| j                   j.                  dk(  rd}	n"t        d| j                   j.                         t        j0                  ||	gg      j                  t        j2                        }ns| j                   j8                  rt        j&                  |      }t        j(                  |      j                         }| j;                  ||      }t        j                   |D cg c]  }| j-                  ||       c}      }| j                   j.                  dk(  r|d   }	n>| j                   j.                  dk(  rd}	n"t        d| j                   j.                         t        j0                  ||	gg      j                  t        j2                        }nJ| j                   j<                  rMt        j                  dd| j                   j                  z  |dz         }
d|
z
  }t        j(                  | j                   j
                  |z  d| j                   j
                  dz
  |z  z   z        dd j                         }|| j                   j                  z  j                         }| j                   j.                  dk(  r|d   }	n>| j                   j.                  dk(  rd}	n"t        d| j                   j.                         t        j0                  ||	gg      j                  t        j2                        }nt        j>                  |t        j                  dtA        |            |      }| j                   j.                  dk(  r&d| j"                  d   z
  | j"                  d   z  d
z  }	n>| j                   j.                  dk(  rd}	n"t        d| j                   j.                         t        j0                  ||	gg      j                  t        j2                        }tC        jD                  |      | _#        tC        jD                  |      jI                  |tB        j                        | _%        tA        |      | _&        dg| j                   jN                  z  | _(        d| _)        d| _*        | jV                  r'| jV                  jY                  | jL                  |       d| _-        d| _.        | jF                  jI                  d      | _#        yc c}w c c}w c c}w )a6  
        Sets the discrete timesteps used for the diffusion chain (to be run before inference).

        Args:
            num_inference_steps (`int`):
                The number of diffusion steps used when generating samples with a pre-trained model.
            device (`str` or `torch.device`, *optional*):
                The device to which the timesteps should be moved to. If `None`, the timesteps are not moved.
            mu (`float`, *optional*):
                Optional mu parameter for dynamic shifting when using exponential time shift type.
        NrG   rE   r   r   r4   rd   re   zY is not supported. Please make sure to choose one of 'linspace', 'leading' or 'trailing'.rm   )	in_sigmasr}   rh   rF   zC`final_sigmas_type` must be one of 'zero', or 'sigma_min', but got r1   r   r#   r   rq   )/rr   rj   rk   r~   r   rb   rc   rE   rH   roundr   astypeint64arangerf   r$   arrayr:   r^   ry   flip_convert_to_karras_sigma_to_trg   concatenater*   r_   _convert_to_exponentialr`   _convert_to_betara   interplenr(   r   r{   r   r   r}   rO   r   r   r   r]   set_timestepsr   r   )r   r}   r   r   r   
step_ratior{   
log_sigmassigma
sigma_lastr9   s              r   r   z%UniPCMultistepScheduler.set_timesteps2  s    >;;338S8SWd8ddd%'VVBZDKK";;'':5At{{>>BDWZ[D[\2"$!	  [[))Y688=PST=TUJ 1&9A&=>KRRTUYWYUYZ[^\^_ddfmmnpnvnvwI111I[[))Z788;NNJ 		$++"A"A1zkRXXZ__ahhikiqiqrINI;;//0  1J  K  A 3 33t7J7JJsRS;;((JWWV_))+F,,vSf,gFSY!Z%$"2"25*"E!Z[aacI{{,,;#BZ
..&8
 YZ^ZeZeZwZwYxy  ^^Vj\$:;BB2::NF[[//JWWV_))+F11FXk1lFSY!Z%$"2"25*"E!Z[I{{,,;#BZ
..&8
 YZ^ZeZeZwZwYxy  ^^Vj\$:;BB2::NF[[((JWWV_))+F**VQd*eFSY!Z%$"2"25*"E!Z[I{{,,;#BZ
..&8
 YZ^ZeZeZwZwYxy  ^^Vj\$:;BB2::NF[[(([[A(G(G$GI\_`I`aF6\FWWT[[33f<T[[E[E[^_E_ciDi@ijklomopuuwF$++"A"AAGGII{{,,;#BZ
..&8
 YZ^ZeZeZwZwYxy  ^^Vj\$:;BB2::NFYYy"))As6{*CVLF{{,,; 4#6#6q#99T=P=PQR=SSX[[
..&8
 YZ^ZeZeZwZwYxy  ^^Vj\$:;BB2::NF&&v.)))477vU[[7Y#&y>  
KK$$% !"==MM''(@(@'P   kknnU+a "[ "[ "[s   9e	0eerQ   c                 b   |j                   }|j                  ^}}}|t        j                  t        j                  fvr|j                         }|j                  ||t        j                  |      z        }|j                         }t        j                  || j                  j                  d      }t        j                  |d| j                  j                        }|j                  d      }t        j                  || |      |z  } |j                  ||g| }|j!                  |      }|S )az  
        Apply dynamic thresholding to the predicted sample.

        "Dynamic thresholding: At each sampling step we set s to a certain percentile absolute pixel value in xt0 (the
        prediction of x_0 at timestep t), and if s > 1, then we threshold xt0 to the range [-s, s] and then divide by
        s. Dynamic thresholding pushes saturated pixels (those near -1 and 1) inwards, thereby actively preventing
        pixels from saturation at each step. We find that dynamic thresholding results in significantly better
        photorealism as well as better image-text alignment, especially when using very large guidance weights."

        https://huggingface.co/papers/2205.11487

        Args:
            sample (`torch.Tensor`):
                The predicted sample to be thresholded.

        Returns:
            `torch.Tensor`:
                The thresholded sample.
        r   r2   )r'   max)r#   shaper(   r*   float64floatreshaper~   prodabsquantilerr   rU   clamprV   	unsqueezer   )r   rQ   r#   
batch_sizechannelsremaining_dims
abs_sampless           r   _threshold_samplez)UniPCMultistepScheduler._threshold_sample  s    ( 06-
H~66\\^F 
Hrww~7N,NOZZ\
NN:t{{'M'MSTUKK1$++66
 KKNVaR+a/
HF~F5!r    r   r   c                    t        j                  t        j                  |d            }||ddt         j                  f   z
  }t        j                  |dk\  d      j                  d      j                  |j                  d   dz
        }|dz   }||   }||   }||z
  ||z
  z  }	t        j                  |	dd      }	d|	z
  |z  |	|z  z   }
|
j                  |j                        }
|
S )a  
        Convert sigma values to corresponding timestep values through interpolation.

        Args:
            sigma (`np.ndarray`):
                The sigma value(s) to convert to timestep(s).
            log_sigmas (`np.ndarray`):
                The logarithm of the sigma schedule used for interpolation.

        Returns:
            `np.ndarray`:
                The interpolated timestep value(s) corresponding to the input sigma(s).
        g|=Nr   )axisr   )r   r   )	r~   ry   maximumnewaxiscumsumargmaxclipr   r   )r   r   r   	log_sigmadistslow_idxhigh_idxlowhighwr   s              r   r   z#UniPCMultistepScheduler._sigma_to_t  s     FF2::eU34	 Jq"**}55 ))UaZq188a8@EE*JZJZ[\J]`aJaEbQ;!(# 9_t,GGAq! UgH,IIekk"r    c                 r    | j                   j                  rd|z
  }|}||fS d|dz  dz   dz  z  }||z  }||fS )a(  
        Convert sigma values to alpha_t and sigma_t values.

        Args:
            sigma (`torch.Tensor`):
                The sigma value(s) to convert.

        Returns:
            `Tuple[torch.Tensor, torch.Tensor]`:
                A tuple containing (alpha_t, sigma_t) values.
        r   r   rm   )rr   ra   )r   r   rw   rx   s       r   _sigma_to_alpha_sigma_tz/UniPCMultistepScheduler._sigma_to_alpha_sigma_t  sY     ;;&&%iGG
  E1HqLS01GgoGr    r   c                    t        | j                  d      r| j                  j                  }nd}t        | j                  d      r| j                  j                  }nd}||n|d   j	                         }||n|d   j	                         }d}t        j                  dd|      }|d|z  z  }|d|z  z  }||||z
  z  z   |z  }	|	S )a  
        Construct the noise schedule as proposed in [Elucidating the Design Space of Diffusion-Based Generative
        Models](https://huggingface.co/papers/2206.00364).

        Args:
            in_sigmas (`torch.Tensor`):
                The input sigma values to be converted.
            num_inference_steps (`int`):
                The number of inference steps to generate the noise schedule for.

        Returns:
            `torch.Tensor`:
                The converted sigma values following the Karras noise schedule.
        rh   N	sigma_maxr4   r   g      @r   )hasattrrr   rh   r   itemr~   rE   )
r   r   r}   rh   r   rhorampmin_inv_rhomax_inv_rhor{   s
             r   r   z*UniPCMultistepScheduler._convert_to_karras  s    $ 4;;,--II4;;,--II!*!6IIbM<N<N<P	!*!6IIaL<M<M<O	{{1a!45AG,AG,k(A BBsJr    c                    t        | j                  d      r| j                  j                  }nd}t        | j                  d      r| j                  j                  }nd}||n|d   j	                         }||n|d   j	                         }t        j                  t        j                  t        j                  |      t        j                  |      |            }|S )a  
        Construct an exponential noise schedule.

        Args:
            in_sigmas (`torch.Tensor`):
                The input sigma values to be converted.
            num_inference_steps (`int`):
                The number of inference steps to generate the noise schedule for.

        Returns:
            `torch.Tensor`:
                The converted sigma values following an exponential schedule.
        rh   Nr   r4   r   )
r   rr   rh   r   r   r~   r   rE   r   ry   )r   r   r}   rh   r   r{   s         r   r   z/UniPCMultistepScheduler._convert_to_exponentialB  s    " 4;;,--II4;;,--II!*!6IIbM<N<N<P	!*!6IIaL<M<M<O	DHHY$7)9LNabcr    alphabetac           
      (   t        | j                  d      r| j                  j                  }nd}t        | j                  d      r| j                  j                  }nd}||n|d   j	                         }||n|d   j	                         }t        j                  dt        j                  dd|      z
  D cg c]-  }t        j                  j                  j                  |||      / c}D cg c]  }||||z
  z  z    c}      }	|	S c c}w c c}w )a  
        Construct a beta noise schedule as proposed in [Beta Sampling is All You
        Need](https://huggingface.co/papers/2407.12173).

        Args:
            in_sigmas (`torch.Tensor`):
                The input sigma values to be converted.
            num_inference_steps (`int`):
                The number of inference steps to generate the noise schedule for.
            alpha (`float`, *optional*, defaults to `0.6`):
                The alpha parameter for the beta distribution.
            beta (`float`, *optional*, defaults to `0.6`):
                The beta parameter for the beta distribution.

        Returns:
            `torch.Tensor`:
                The converted sigma values following a beta distribution schedule.
        rh   Nr   r4   r   r   )r   rr   rh   r   r   r~   r   rE   scipystatsr   ppf)
r   r   r}   r   r   rh   r   timestepr   r{   s
             r   r   z(UniPCMultistepScheduler._convert_to_betad  s    0 4;;,--II4;;,--II!*!6IIbM<N<N<P	!*!6IIaL<M<M<O	
 %&Aq:M(N$N  KK$$((5$? SI	$9:;
 s   82D
/DrQ   model_outputc                   t        |      dkD  r|d   n|j                  dd      }|t        |      dkD  r|d   }nt        d      |t        ddd       | j                  | j
                     }| j                  |      \  }}| j                  r| j                  j                  d	k(  r|||z  z
  |z  }	n| j                  j                  d
k(  r|}	n| j                  j                  dk(  r||z  ||z  z
  }	n^| j                  j                  dk(  r"| j                  | j
                     }|||z  z
  }	n#t        d| j                  j                   d      | j                  j                  r| j                  |	      }	|	S | j                  j                  d	k(  r|S | j                  j                  d
k(  r|||z  z
  |z  }
|
S | j                  j                  dk(  r||z  ||z  z   }
|
S t        d| j                  j                   d      )a  
        Convert the model output to the corresponding type the UniPC algorithm needs.

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

        Returns:
            `torch.Tensor`:
                The converted model output.
        r   r   Nr   /missing `sample` as a required keyword argumentr   1.0.0zPassing `timesteps` is deprecated and has no effect as model output conversion is now handled via an internal counter `self.step_index`rC   rQ   rR   rS   zprediction_type given as zj must be one of `epsilon`, `sample`, `v_prediction`, or `flow_prediction` for the UniPCMultistepScheduler.zW must be one of `epsilon`, `sample`, or `v_prediction` for the UniPCMultistepScheduler.)r   popr$   r   r{   r   r   rW   rr   rP   rT   r   )r   r   rQ   argskwargsr   r   rw   rx   x0_predrC   s              r   convert_model_outputz,UniPCMultistepScheduler.convert_model_output  s   , "$i!m47J1M>4y1}a !RSS Z DOO,77>??{{**i7!Gl$::gE,,8&,,>!F*W|-CC,,0AA++doo6 7\#99 /0K0K/L M\ \ 
 {{''009N{{**i7##,,8!Gl$::gE,,>!L07V3CC /0K0K/L MG G r    )rQ   orderr   c                z	   t        |      dkD  r|d   n|j                  dd      }|t        |      dkD  r|d   }nt        d      |t        |      dkD  r|d   }nt        d      |t        ddd	       | j                  }| j
                  d
   }|d
   }	|}
| j                  r)| j                  j                  |||
      j                  }|S | j                  | j                  dz      | j                  | j                     }}| j                  |      \  }}| j                  |      \  }}t        j                  |      t        j                  |      z
  }t        j                  |      t        j                  |      z
  }||z
  }|j                  }g }g }t        d|      D ]  }| j                  |z
  }||dz       }| j                  | j                  |         \  }}t        j                  |      t        j                  |      z
  }||z
  |z  }|j!                  |       |j!                  ||	z
  |z          |j!                  d       t        j"                  ||      }g }g }| j$                  r| n|}t        j&                  |      } | |z  dz
  }!d}"| j(                  j*                  dk(  r|}#n9| j(                  j*                  dk(  rt        j&                  |      }#n
t-               t        d|dz         D ]T  }|j!                  t        j.                  ||dz
               |j!                  |!|"z  |#z         |"|dz   z  }"|!|z  d|"z  z
  }!V t        j0                  |      }t        j"                  ||      }t        |      dkD  rt        j0                  |d      }|dk(  r$t        j"                  dg|
j2                  |      }$nWt        j4                  j7                  |dd
dd
f   |dd
       j9                  |      j9                  |
j2                        }$nd}| j$                  r9||z  |
z  || z  |	z  z
  }%|t        j:                  d$|      }&nd}&|%||#z  |&z  z
  }n8||z  |
z  || z  |	z  z
  }%|t        j:                  d$|      }&nd}&|%||#z  |&z  z
  }|j9                  |
j2                        }|S )a  
        One step for the UniP (B(h) version). Alternatively, `self.solver_p` is used if is specified.

        Args:
            model_output (`torch.Tensor`):
                The direct output from the learned diffusion model at the current timestep.
            prev_timestep (`int`):
                The previous discrete timestep in the diffusion chain.
            sample (`torch.Tensor`):
                A current instance of a sample created by the diffusion process.
            order (`int`):
                The order of UniP at this timestep (corresponds to the *p* in UniPC-p).

        Returns:
            `torch.Tensor`:
                The sample tensor at the previous timestep.
        r   prev_timestepNr   r   r   .missing `order` as a required keyword argumentr   zPassing `prev_timestep` is deprecated and has no effect as model output conversion is now handled via an internal counter `self.step_index`r4   r1   r   rZ   rD   r2   rm   r#   r   k,bkc...->bc...)r   r   r$   r   r   r   r]   stepprev_sampler{   r   r   r(   ry   r   r%   r&   r)   rW   expm1rr   rX   ru   powstackr#   linalgsolver   einsum)'r   r   rQ   r   r   r   r   model_output_lists0m0xx_trx   sigma_s0rw   alpha_s0rz   	lambda_s0hr   rksD1sr,   simialpha_sisigma_si	lambda_sirkRbhhh_phi_1h_phi_kfactorial_iB_hrhos_px_t_pred_ress'                                          r   multistep_uni_p_bh_updatez1UniPCMultistepScheduler.multistep_uni_p_bh_update  s~   2 $'t9q=QfjjRV6W>4y1}a !RSS=4y1}Q !QRR$ ^
 !..#r"==--$$\2q9EECJ KK!(;<dkk$//>Z77@!99(C(99W%		'(::IIh'%))H*==	y q% 	'A1$B"QU8,B!%!=!=dkk"o!NHh		(+eii.AAIi'1,BJJrNJJR2~&	' 	

3ll3v.??aR++b/B,";;""e+C[[$$-++b/C%''q%!)$ 	5AHHUYYsAE*+HHW{*S011q5 KlQ_4G		5 KKNLL6*s8a<++cq)Czse1776J++Acrc3B3hK3B@CCFKNNqwwWC??X%)Gg,=,BBD <<(963G311CX%)Gg,=,BBD <<(963G311CffQWWo
r    )r   this_sampler   this_model_outputr   r  c                n	   t        |      dkD  r|d   n|j                  dd      }|t        |      dkD  r|d   }nt        d      |t        |      dkD  r|d   }nt        d      |t        |      dkD  r|d   }nt        d	      |t        dd
d       | j                  }|d   }	|}
|}|}| j
                  | j                     | j
                  | j                  dz
     }}| j                  |      \  }}| j                  |      \  }}t        j                  |      t        j                  |      z
  }t        j                  |      t        j                  |      z
  }||z
  }|j                  }g }g }t        d|      D ]  }| j                  |dz   z
  }||dz       }| j                  | j
                  |         \  }}t        j                  |      t        j                  |      z
  }||z
  |z  }|j                  |       |j                  ||	z
  |z          |j                  d       t        j                  ||      }g }g }| j                  r| n|} t        j                  |       }!|!| z  dz
  }"d}#| j                   j"                  dk(  r| }$n9| j                   j"                  dk(  rt        j                  |       }$n
t%               t        d|dz         D ]T  }|j                  t        j&                  ||dz
               |j                  |"|#z  |$z         |#|dz   z  }#|"| z  d|#z  z
  }"V t        j(                  |      }t        j                  ||      }t        |      dkD  rt        j(                  |d      }nd}|dk(  r$t        j                  dg|
j*                  |      }%nHt        j,                  j/                  ||      j1                  |      j1                  |
j*                        }%| j                  rJ||z  |
z  ||!z  |	z  z
  }&|t        j2                  d|%dd |      }'nd}'||	z
  }(|&||$z  |'|%d   |(z  z   z  z
  }nI||z  |
z  ||!z  |	z  z
  }&|t        j2                  d|%dd |      }'nd}'||	z
  }(|&||$z  |'|%d   |(z  z   z  z
  }|j1                  |
j*                        }|S )a  
        One step for the UniC (B(h) version).

        Args:
            this_model_output (`torch.Tensor`):
                The model outputs at `x_t`.
            this_timestep (`int`):
                The current timestep `t`.
            last_sample (`torch.Tensor`):
                The generated sample before the last predictor `x_{t-1}`.
            this_sample (`torch.Tensor`):
                The generated sample after the last predictor `x_{t}`.
            order (`int`):
                The `p` of UniC-p at this step. The effective order of accuracy should be `order + 1`.

        Returns:
            `torch.Tensor`:
                The corrected sample tensor at the current timestep.
        r   this_timestepNr   z4missing `last_sample` as a required keyword argumentr   z4missing `this_sample` as a required keyword argument   r   r   zPassing `this_timestep` is deprecated and has no effect as model output conversion is now handled via an internal counter `self.step_index`r4   r1   r   rZ   rD   r2   rm   r   r   )r   r   r$   r   r   r{   r   r   r(   ry   r   r%   r&   r)   rW   r   rr   rX   ru   r   r   r#   r   r   r   r   ))r   r  r   r  r   r   r   r  r   r   r   r   model_trx   r   rw   r   rz   r   r   r   r   r   r,   r   r   r  r  r  r  r  r  r  r  r	  r
  r  rhos_cr  corr_resD1_ts)                                            r   multistep_uni_c_bh_updatez1UniPCMultistepScheduler.multistep_uni_c_bh_update^  s   8 $'t9q=QfjjRV6W4y1}"1g !WXX4y1}"1g !WXX=4y1}Q !QRR$ ^ !..r"# KK8$++dooXYFY:Z77@!99(C(99W%		'(::IIh'%))H*==	y ##q% 	'AAE*B"QU8,B!%!=!=dkk"o!NHh		(+eii.AAIi'1,BJJrNJJR2~&	' 	

3ll3v.??aR++b/B,";;""e+C[[$$-++b/C%''q%!)$ 	5AHHUYYsAE*+HHW{*S011q5 KlQ_4G		5 KKNLL6*s8a<++cq)CC A:\\3%qwwvFF\\''1-008;;AGGDF??X%)Gg,=,BBD <<(96#2;LR<D3(VBZ$5F*FGGCX%)Gg,=,BBD <<(96#2;LR<D3(VBZ$5F*FGGCffQWWo
r    r   schedule_timestepsc                    || j                   }||k(  j                         }t        |      dk(  rt        | j                         dz
  }|S t        |      dkD  r|d   j                         }|S |d   j                         }|S )a  
        Find the index for a given timestep in the schedule.

        Args:
            timestep (`int` or `torch.Tensor`):
                The timestep for which to find the index.
            schedule_timesteps (`torch.Tensor`, *optional*):
                The timestep schedule to search in. If `None`, uses `self.timesteps`.

        Returns:
            `int`:
                The index of the timestep in the schedule.
        r   r   )r   nonzeror   r   )r   r   r  index_candidatesr   s        r   index_for_timestepz*UniPCMultistepScheduler.index_for_timestep  s      %!%.(:CCE A%T^^,q0J  !"Q&)!,113J  *!,113Jr    c                     | j                   Vt        |t        j                        r%|j	                  | j
                  j                        }| j                  |      | _        y| j                  | _        y)z
        Initialize the step_index counter for the scheduler.

        Args:
            timestep (`int` or `torch.Tensor`):
                The current timestep for which to initialize the step index.
        N)
r   
isinstancer(   Tensorr   r   r   r  r   r   )r   r   s     r   _init_step_indexz(UniPCMultistepScheduler._init_step_index  sW     #(ELL1#;;t~~'<'<=#66x@D#00Dr    return_dictc                    | j                   t        d      | j                  | j                  |       | j                  dkD  xr+ | j                  dz
  | j                  vxr | j
                  du}| j                  ||      }|r)| j                  || j
                  || j                        }t        | j                  j                  dz
        D ]@  }| j                  |dz      | j                  |<   | j                  |dz      | j                  |<   B || j                  d<   || j                  d<   | j                  j                  rAt        | j                  j                  t!        | j"                        | j                  z
        }n| j                  j                  }t        || j$                  dz         | _        | j                  dkD  sJ || _        | j'                  ||| j                        }	| j$                  | j                  j                  k  r| xj$                  dz  c_        | xj(                  dz  c_        |s|	fS t+        |		      S )
a  
        Predict the sample from the previous timestep by reversing the SDE. This function propagates the sample with
        the multistep UniPC.

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

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

        NzaNumber of inference steps is 'None', you need to run 'set_timesteps' after creating the schedulerr   r   r   )r  r   r  r   r4   )r   rQ   r   )r   )r}   r$   r   r"  r\   r   r   r  
this_orderr%   rr   rO   r   r   r[   r'   r   r   r   r  r   r   )
r   r   r   rQ   r#  use_correctormodel_output_convertr,   r%  r   s
             r   r   zUniPCMultistepScheduler.step  s0   6 ##+s  ??"!!(+ OOavDOOa$7t?U?U$UvZ^ZjZjrvZv 	  $88f8U33"6 ,,"oo	 4 F t{{//!34 	>A$($6$6q1u$=Dq!$($6$6q1u$=Dq!	> "62!)2;;((T[[55s4>>7JT__7\]J11Jj$*?*?!*CD"""!44%// 5 
   4;;#;#;;!!Q&! 	A>!;77r    c                     |S )a?  
        Ensures interchangeability with schedulers that need to scale the denoising model input depending on the
        current timestep.

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

        Returns:
            `torch.Tensor`:
                A scaled input sample.
         )r   rQ   r   r   s       r   scale_model_inputz)UniPCMultistepScheduler.scale_model_inputl  s	     r    original_samplesnoiser   c                 *   | j                   j                  |j                  |j                        }|j                  j                  dk(  rvt        j                  |      ra| j                  j                  |j                  t
        j                        }|j                  |j                  t
        j                        }n@| j                  j                  |j                        }|j                  |j                        }| j                   |D cg c]  }| j                  ||       }}nG| j                  | j                  g|j                  d   z  }n| j                  g|j                  d   z  }||   j                         }t        |j                        t        |j                        k  r=|j                  d      }t        |j                        t        |j                        k  r=| j!                  |      \  }	}
|	|z  |
|z  z   }|S c c}w )a  
        Add noise to the original samples according to the noise schedule at the specified timesteps.

        Args:
            original_samples (`torch.Tensor`):
                The original samples without noise.
            noise (`torch.Tensor`):
                The noise to add to the samples.
            timesteps (`torch.IntTensor`):
                The timesteps at which to add noise to the samples.

        Returns:
            `torch.Tensor`:
                The noisy samples.
        r   mpsr"   r   r4   )r{   r   r   r#   typer(   is_floating_pointr   r*   r   r  r   r   flattenr   r   r   )r   r+  r,  r   r{   r  r   step_indicesr   rw   rx   noisy_sampless               r   	add_noisez!UniPCMultistepScheduler.add_noise|  s   , '7'>'>FVF\F\]""''50U5L5LY5W!%!2!23C3J3JRWR_R_!2!`!%5%<%<EMMRI!%!2!23C3J3J!K!%5%<%<=I #T]^qD33A7IJ^L^__( OO,yq/AAL !,,-	0BBL|$,,.%++%5%;%;!<<OOB'E %++%5%;%;!<<  77>"22Wu_D _s   Hc                 .    | j                   j                  S N)rr   rH   r   s    r   __len__zUniPCMultistepScheduler.__len__  s    {{...r    )r   )NN)333333?r8  r6  )T)/__name__
__module____qualname____doc__r   name_compatiblesr   r
   intr   r   r   r   r~   ndarrayr   boolr   r   propertyr   r   r   strr(   r   r   r!  r   r   r   r   r   r   r   r   r  r  r  r"  r   r   r*  	IntTensorr4  r7  ).0es   00r   rA   rA   t   s   EN %>>qAFF>LE $("QYBF[d",1"%-2"&')-1,116*/*/&)GQDJ',%*2?7W, W, W, 	W,
 MNW,  bjj$u+&= >?W, W, !!WXW, W, %*W,  W, W, \*W,  W,  9W,  >*!W," $D>#W,$ !)%W,& "$'W,( "$)W,* UO+W,, ""CD-W,. /W,0 $G,?$@A1W,2 !%3W,4 #5W,6 !/7W,8 
9W, W,r  HSM     !Xc] ! !(3 (t ( rv,#&,08sELL?P9Q0R,_ghm_n,	,D) ) )X" " "

 "J U\\  eELLRWR^R^D^>_  ,$ELL $s $W\WcWc $N TW \a\h\h F dg..<?.HM.[`.	.h  $	GllG 	G 
GZ  $ll 	
  
J %)$(G <<G \\	G
 \\G G 
GV `d c5<</0 FNu||F\ 	 F1sELL/@)A 1d 1* !O8llO8 U\\)*O8 	O8
 O8 
%	&O8b %,,  /,,/ ||/ ??	/
 
/b/ /c ?s   L7rA   )g+?r   )r   typingr   r   r   r   r   numpyr~   r(   configuration_utilsr	   r
   utilsr   r   scheduling_utilsr   r   r   scipy.statsr   r?  r   r!  r/   r?   rA   r)  r    r   <module>rM     s   $  8 8   A 1 X X  5=*4 *4*4 "/2*4 \\	*4\!U\\ !ell !Hz/nk z/r    