
    wi                         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 ddlmZ ddlmZmZmZ 	 	 ddZ G d dee          ZdS )    N)CallableListOptionalTupleUnion   )ConfigMixinregister_to_config)	deprecate)randn_tensor   )KarrasDiffusionSchedulersSchedulerMixinSchedulerOutput+?cosinec           
      F   |dk    rd }n|dk    rd }nt          d|           g }t          |           D ]J}|| z  }|dz   | z  }|                    t          d ||           ||          z  z
  |                     Kt	          j        |t          j                  S )a  
    Create a beta schedule that discretizes the given alpha_t_bar function, which defines the cumulative product of
    (1-beta) over time from t = [0,1].

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


    Args:
        num_diffusion_timesteps (`int`): the number of betas to produce.
        max_beta (`float`): the maximum beta to use; use values lower than 1 to
                     prevent singularities.
        alpha_transform_type (`str`, *optional*, default to `cosine`): the type of noise schedule for alpha_bar.
                     Choose from `cosine` or `exp`

    Returns:
        betas (`np.ndarray`): the betas used by the scheduler to step the model outputs
    r   c                 \    t          j        | dz   dz  t           j        z  dz            dz  S )NgMb?gT㥛 ?r   )mathcospits    x/root/.openclaw/workspace/chatterbox_venv_py311/lib/python3.11/site-packages/diffusers/schedulers/scheduling_sasolver.pyalpha_bar_fnz)betas_for_alpha_bar.<locals>.alpha_bar_fn8   s,    8QY%/$'9A=>>!CC    expc                 0    t          j        | dz            S )Ng      ()r   r   r   s    r   r   z)betas_for_alpha_bar.<locals>.alpha_bar_fn=   s    8AI&&&r   z"Unsupported alpha_transform_type: r   dtype)
ValueErrorrangeappendmintorchtensorfloat32)num_diffusion_timestepsmax_betaalpha_transform_typer   betasit1t2s           r   betas_for_alpha_barr/      s    . x''	D 	D 	D 	D 
	&	&	' 	' 	' 	' T>RTTUUUE*++ M M((!e..S\\"--R0@0@@@(KKLLLL<U]3333r   c            (          e Zd ZdZd eD             ZdZedddddd	d	d
ddddddd ed           dddfde	dedede
deeej        ee         f                  de	de	de
dee         dededede
d ed!ee         d"ed#ee
         d$e
d%e	f&d&            Zed'             Zed(             ZdOd)e	fd*ZdPd+e	d,ee
ej        f         fd-Zd.ej        d/ej        fd0Zd1 Zd2 Zd3ej        d/ej        fd4Zdd5d6ej        d.ej        d/ej        fd7Zd8 Z d9 Z!d: Z"d; Z#d6ej        d.ej        d<ej        d=e	d>ej        d/ej        fd?Z$d@ej        dAej        dBej        dCej        d=e	d>ej        d/ej        fdDZ%dQdEZ&dF Z'	 	 dRd6ej        dGe	d.ej        dHed/ee(e)f         f
dIZ*d.ej        d/ej        fdJZ+dKej        d<ej        dLej,        d/ej        fdMZ-dN Z.dS )SSASolverScheduleru%  
    `SASolverScheduler` is a fast dedicated high-order solver for diffusion SDEs.

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

    Args:
        num_train_timesteps (`int`, defaults to 1000):
            The number of diffusion steps to train the model.
        beta_start (`float`, defaults to 0.0001):
            The starting `beta` value of inference.
        beta_end (`float`, defaults to 0.02):
            The final `beta` value.
        beta_schedule (`str`, defaults to `"linear"`):
            The beta schedule, a mapping from a beta range to a sequence of betas for stepping the model. Choose from
            `linear`, `scaled_linear`, or `squaredcos_cap_v2`.
        trained_betas (`np.ndarray`, *optional*):
            Pass an array of betas directly to the constructor to bypass `beta_start` and `beta_end`.
        predictor_order (`int`, defaults to 2):
            The predictor order which can be `1` or `2` or `3` or '4'. It is recommended to use `predictor_order=2` for
            guided sampling, and `predictor_order=3` for unconditional sampling.
        corrector_order (`int`, defaults to 2):
            The corrector order which can be `1` or `2` or `3` or '4'. It is recommended to use `corrector_order=2` for
            guided sampling, and `corrector_order=3` for unconditional sampling.
        prediction_type (`str`, defaults to `epsilon`, *optional*):
            Prediction type of the scheduler function; can be `epsilon` (predicts the noise of the diffusion process),
            `sample` (directly predicts the noisy sample`) or `v_prediction` (see section 2.4 of [Imagen
            Video](https://imagen.research.google/video/paper.pdf) paper).
        tau_func (`Callable`, *optional*):
            Stochasticity during the sampling. Default in init is `lambda t: 1 if t >= 200 and t <= 800 else 0`.
            SA-Solver will sample from vanilla diffusion ODE if tau_func is set to `lambda t: 0`. SA-Solver will sample
            from vanilla diffusion SDE if tau_func is set to `lambda t: 1`. For more details, please check
            https://arxiv.org/abs/2309.05019
        thresholding (`bool`, defaults to `False`):
            Whether to use the "dynamic thresholding" method. This is unsuitable for latent-space diffusion models such
            as Stable Diffusion.
        dynamic_thresholding_ratio (`float`, defaults to 0.995):
            The ratio for the dynamic thresholding method. Valid only when `thresholding=True`.
        sample_max_value (`float`, defaults to 1.0):
            The threshold value for dynamic thresholding. Valid only when `thresholding=True` and
            `algorithm_type="dpmsolver++"`.
        algorithm_type (`str`, defaults to `data_prediction`):
            Algorithm type for the solver; can be `data_prediction` or `noise_prediction`. It is recommended to use
            `data_prediction` with `solver_order=2` for guided sampling like in Stable Diffusion.
        lower_order_final (`bool`, defaults to `True`):
            Whether to use lower-order solvers in the final steps. Default = True.
        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}.
        lambda_min_clipped (`float`, defaults to `-inf`):
            Clipping threshold for the minimum value of `lambda(t)` for numerical stability. This is critical for the
            cosine (`squaredcos_cap_v2`) noise schedule.
        variance_type (`str`, *optional*):
            Set to "learned" or "learned_range" for diffusion models that predict variance. If set, the model's output
            contains the predicted Gaussian variance.
        timestep_spacing (`str`, defaults to `"linspace"`):
            The way the timesteps should be scaled. Refer to Table 2 of the [Common Diffusion Noise Schedules and
            Sample Steps are Flawed](https://huggingface.co/papers/2305.08891) for more information.
        steps_offset (`int`, defaults to 0):
            An offset added to the inference steps, as required by some model families.
    c                     g | ]	}|j         
S  )name).0es     r   
<listcomp>zSASolverScheduler.<listcomp>   s    >>>qAF>>>r   r   i  g-C6?g{Gz?linearNr   epsilonFgףp=
?      ?data_predictionTinflinspacer   num_train_timesteps
beta_startbeta_endbeta_scheduletrained_betaspredictor_ordercorrector_orderprediction_typetau_functhresholdingdynamic_thresholding_ratiosample_max_valuealgorithm_typelower_order_finaluse_karras_sigmaslambda_min_clippedvariance_typetimestep_spacingsteps_offsetc                 0   |&t          j        |t           j                  | _        n|dk    r(t          j        |||t           j                  | _        nk|dk    r1t          j        |dz  |dz  |t           j                  dz  | _        n4|dk    rt          |          | _        nt          | d| j                   d| j        z
  | _        t          j	        | 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rt          | d| j                   d | _        t'          j        d	|dz
  |t&          j                  d d d                                         }t          j        |          | _        d gt/          ||dz
            z  | _        d gt/          ||dz
            z  | _        |		d | _        n|	| _        |dk    | _        d	| _        d | _        d | _        d | _        | j                             d          | _        d S )Nr   r8   scaled_linear      ?r   squaredcos_cap_v2z is not implemented for r:   r   dimr   )r;   noise_predictionc                 "    | dk    r| dk    rdndS )N   i   r   r   r3   r   s    r   <lambda>z,SASolverScheduler.__init__.<locals>.<lambda>   s    188Saaa r   r;   cpu)!r%   r&   r'   r+   r=   r/   NotImplementedError	__class__alphascumprodalphas_cumprodsqrtalpha_tsigma_tloglambda_tsigmasinit_noise_sigmanum_inference_stepsnpcopy
from_numpy	timestepsmaxtimestep_listmodel_outputsrF   
predict_x0lower_order_numslast_sample_step_index_begin_indexto)selfr>   r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   rN   rO   rP   rm   s                        r   __init__zSASolverScheduler.__init__   s   . $m5=IIIDJJh&&
H>QY^YfgggDJJo-- OcM'-	    JJ 111,-@AADJJ%&`&`PTP^&`&`aaaDJ&#mDKQ???z$"566z!d&9"9::	$,//%)DL2I2IID//43FF3N !$!HHH%&a&aQUQ_&a&abbb $( K#6#:<OWYWabbbcgcgegcghmmoo	))44"Vc/?Q;N&O&OO"Vc/?Q;N&O&OOGGDMM$DM(,== ! knnU++r   c                     | j         S )zg
        The index counter for current timestep. It will increase 1 after each scheduler step.
        )rt   rw   s    r   
step_indexzSASolverScheduler.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.
        ru   rz   s    r   begin_indexzSASolverScheduler.begin_index   s    
   r   r~   c                     || _         dS )z
        Sets the begin index for the scheduler. This function should be run from pipeline before the inference.

        Args:
            begin_index (`int`):
                The begin index for the scheduler.
        Nr}   )rw   r~   s     r   set_begin_indexz!SASolverScheduler.set_begin_index   s     (r   ri   devicec           	         	 t          j        t          j         j        dg           j        j                  } j        j        |z
                                                                  } j        j	        dk    rpt          j        d|dz
  |dz                                             ddd         dd                                                             t          j                  }n. j        j	        dk    r||dz   z  }t          j        d|dz             |z                                  ddd         dd                                                             t          j                  }| j        j        z  }n j        j	        dk    rm j        j        |z  }t          j        |d|                                                                                               t          j                  }|dz  }nt%           j        j	         d          t          j        d j        z
   j        z  d	z            } j        j        rt          j        |          	t          j        |                                          }                     ||
          }t          j        	 fd|D                                                       }t          j        ||dd         g                              t          j                  }nt          j        |t          j        dt7          |                    |          }d j        d         z
   j        d         z  d	z  }t          j        ||gg                              t          j                  }t          j        |           _        t          j        |                              |t           j                   _        t7          |           _         dgtC           j        j"         j        j#        dz
            z   _$        d _%        d _&        d _'        d _(         j                            d           _        dS )a  
        Sets the discrete timesteps used for the diffusion chain (to be run before inference).

        Args:
            num_inference_steps (`int`):
                The number of diffusion steps used when generating samples with a pre-trained model.
            device (`str` or `torch.device`, *optional*):
                The device to which the timesteps should be moved to. If `None`, the timesteps are not moved.
        r   r=   r   NrX   leadingtrailingzY is not supported. Please make sure to choose one of 'linspace', 'leading' or 'trailing'.rS   )	in_sigmasri   c                 <    g | ]}                     |          S r3   )_sigma_to_t)r5   sigma
log_sigmasrw   s     r   r7   z3SASolverScheduler.set_timesteps.<locals>.<listcomp>  s)    !Z!Z!Z%$"2"25*"E"E!Z!Z!Zr   )r   r    r\   ))r%   searchsortedfliprf   configrM   r>   numpyitemrO   rj   r=   roundrk   astypeint64arangerP   r!   arrayra   rL   re   _convert_to_karrasconcatenater'   interplenrl   rg   rv   rm   ri   rn   rC   rD   rp   rr   rs   rt   ru   )
rw   ri   r   clipped_idxlast_timesteprm   
step_ratiorg   
sigma_lastr   s
   `        @r   set_timestepszSASolverScheduler.set_timesteps   s    (DMA3)G)GIghh+9KGNNPPVVXX ;':55A}q02E2IJJPPRRSWSWUWSWXY\Z\Y\]bbddkklnltuu I [)Y66&+>+BCJ 1&9A&=>>KRRTTUYUYWYUYZ[^\^[^_ddffmmnpnvwwI11II[)Z778;NNJ 	-ZK@@FFHHMMOOVVWYW_``INII;/  K  K  K   A 33t7JJsRSS;( 		OJWV__))++F,,vSf,ggF!Z!Z!Z!Z!ZSY!Z!Z!Z[[aaccI^VVBCC[$9::AA"*MMFFYy")As6{{*C*CVLLFt21559LQ9OOTWWJ^Vj\$:;;BB2:NNF&v..))4477vU[7YY#&y>> 
+T[-H1-LMMN !"   knnU++r   samplereturnc                 ,   |j         }|j        ^}}}|t          j        t          j        fvr|                                }|                    ||t          j        |          z            }|	                                }t          j
        || j        j        d          }t          j        |d| j        j                  }|                    d          }t          j        || |          |z  } |j        ||g|R  }|                    |          }|S )as  
        "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://arxiv.org/abs/2205.11487
        r   rU   )r$   rn   )r    shaper%   r'   float64floatreshaperj   prodabsquantiler   rH   clamprI   	unsqueezerv   )rw   r   r    
batch_sizechannelsremaining_dims
abs_sampless           r   _threshold_samplez#SASolverScheduler._threshold_sample4  s    06-
H~666\\^^F 
Hrw~7N7N,NOOZZ\\
N:t{'MSTUUUK1$+6
 
 
 KKNNVaR++a/
HF~FFF5!!r   c                    t          j        t          j        |d                    }||d d t           j        f         z
  }t          j        |dk    d                              d                              |j        d         dz
            }|dz   }||         }||         }||z
  ||z
  z  }	t          j        |	dd          }	d|	z
  |z  |	|z  z   }
|
                    |j                  }
|
S )Ng|=r   )axisr   )rn   r   )	rj   re   maximumnewaxiscumsumargmaxclipr   r   )rw   r   r   	log_sigmadistslow_idxhigh_idxlowhighwr   s              r   r   zSASolverScheduler._sigma_to_tV  s    F2:eU3344	 Jqqq"*}55 )UaZq11188a8@@EE*JZ[\J]`aJaEbbQ;!(# 9_t,GAq! UgH,IIek""r   c                 0    d|dz  dz   dz  z  }||z  }||fS )Nr   r   rS   r3   )rw   r   rc   rd   s       r   _sigma_to_alpha_sigma_tz)SASolverScheduler._sigma_to_alpha_sigma_tn  s-    q1,-'/r   r   c                 z   t          | j        d          r| j        j        }nd}t          | j        d          r| j        j        }nd}||n|d                                         }||n|d                                         }d}t          j        dd|          }|d|z  z  }|d|z  z  }||||z
  z  z   |z  }	|	S )z6Constructs the noise schedule of Karras et al. (2022).	sigma_minN	sigma_maxrX   r   g      @r   )hasattrr   r   r   r   rj   r=   )
rw   r   ri   r   r   rhorampmin_inv_rhomax_inv_rhorg   s
             r   r   z$SASolverScheduler._convert_to_karrasu  s    
 4;,, 	-III4;,, 	-III!*!6IIIbM<N<N<P<P	!*!6IIIaL<M<M<O<O	{1a!455AG,AG,k(A BBsJr   r   model_outputc                r   t          |          dk    r|d         n|                    dd          }|+t          |          dk    r	|d         }nt          d          |t          ddd           | j        | j                 }|                     |          \  }}| j        j        d	v r| j        j	        d
k    r(| j        j
        dv r|ddddf         }|||z  z
  |z  }	nL| j        j	        dk    r|}	n9| j        j	        dk    r||z  ||z  z
  }	nt          d| j        j	         d          | j        j        r|                     |	          }	|	S | j        j        dv r| j        j	        d
k    r | j        j
        dv r|ddddf         }
nX|}
nU| j        j	        dk    r|||z  z
  |z  }
n9| j        j	        dk    r||z  ||z  z   }
nt          d| j        j	         d          | j        j        rE| j        |         | j        |         }}|||
z  z
  |z  }	|                     |	          }	|||	z  z
  |z  }
|
S dS )aO  
        Convert the model output to the corresponding type the data_prediction/noise_prediction algorithm needs.
        Noise_prediction is designed to discretize an integral of the noise prediction model, and data_prediction is
        designed to discretize an integral of the data prediction model.

        <Tip>

        The algorithm and model type are decoupled. You can use either data_prediction or noise_prediction for both
        noise prediction and data prediction models.

        </Tip>

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

        Returns:
            `torch.Tensor`:
                The converted model output.
        r   timestepNr   z/missing `sample` as a required keyward argumentrm   1.0.0zPassing `timesteps` is deprecated and has no effect as model output conversion is now handled via an internal counter `self.step_index`)r;   r9   )learnedlearned_range   r   v_predictionzprediction_type given as zQ must be one of `epsilon`, `sample`, or `v_prediction` for the SASolverScheduler.)rW   )r   popr!   r   rg   r{   r   r   rJ   rE   rN   rG   r   rc   rd   )rw   r   r   argskwargsr   r   rc   rd   x0_predr9   s              r   convert_model_outputz&SASolverScheduler.convert_model_output  s   : "$ii!mm477J1M1M>4yy1}}a !RSSS Z   DO,77>>;%)<<<{*i77;,0LLL#/2A2#6L!Gl$::gE,88&,>>!F*W|-CC A0K A A A  
 {' :0099N ['+???{*i77;,0LLL*111bqb51GG*GG,88!Gl$::gE,>>!L07V3CC A0K A A A  
 {' A#'<#94<;Q!Gg$55@0099!Gg$55@N/ @?r   c                 t   |dv s
J d            |dk    r0t          j        |           t          j        ||z
            dz
  z  S |dk    r9t          j        |           |dz   t          j        ||z
            z  |dz   z
  z  S |dk    rKt          j        |           |dz  d|z  z   dz   t          j        ||z
            z  |dz  d|z  z   dz   z
  z  S |dk    r]t          j        |           |dz  d|dz  z  z   d|z  z   dz   t          j        ||z
            z  |dz  d|dz  z  z   d|z  z   dz   z
  z  S dS )	zd
        Calculate the integral of exp(-x) * x^order dx from interval_start to interval_end
        r   r   r   r   )order is only supported for 0, 1, 2 and 3r   r   r   r      Nr%   r   )rw   orderinterval_startinterval_ends       r   %get_coefficients_exponential_negativez7SASolverScheduler.get_coefficients_exponential_negative  s    $$$&Q$$$A::9l]++uy9V/W/WZ[/[\\aZZ9l]++!#uy1N'O'OOS_bcScd  aZZ9l]++"Q%77!;uyXfIf?g?gg?Q%559;  aZZ9l]++"Q):%::Q=OORSS)L>9::;?Qq%881|;KKaOQ  Zr   c                    |dv s
J d            d|dz  z   |z  }d|dz  z   |z  }|dk    r9t          j        |          dt          j        ||z
             z
  z  d|dz  z   z  S |dk    rEt          j        |          |dz
  |dz
  t          j        ||z
             z  z
  z  d|dz  z   dz  z  S |dk    rWt          j        |          |dz  d|z  z
  dz   |dz  d|z  z
  dz   t          j        ||z
             z  z
  z  d|dz  z   dz  z  S |dk    rit          j        |          |dz  d|dz  z  z
  d|z  z   dz
  |dz  d|dz  z  z
  d|z  z   dz
  t          j        ||z
             z  z
  z  d|dz  z   dz  z  S d	S )
zl
        Calculate the integral of exp(x(1+tau^2)) * x^order dx from interval_start to interval_end
        r   r   r   r   r   r   r      Nr   )rw   r   r   r   tauinterval_end_covinterval_start_covs          r   %get_coefficients_exponential_positivez7SASolverScheduler.get_coefficients_exponential_positive  s>    $$$&Q$$$ QJ,6#q&jN:A::	*++q59?ORd?d=e3f3f/fgklortuoukuv aZZ	*++%))A-=MPb=b;c1d1dde
 QJ1$& aZZ	*++%q(1/?+??!C)1,q3E/EEIi"25G"G HIIJJ QJ1$& aZZ	*++%q(1/?/B+BBQIYEYY\]])1,q3Eq3H/HH1OaKaadeei"25G"G HIIJJ QJ1$& Zr   c           	      r   |dv sJ |t          |          dz
  k    sJ |dk    rdggS |dk    r^d|d         |d         z
  z  |d          |d         |d         z
  z  gd|d         |d         z
  z  |d          |d         |d         z
  z  ggS |dk    r|d         |d         z
  |d         |d         z
  z  }|d         |d         z
  |d         |d         z
  z  }|d         |d         z
  |d         |d         z
  z  }d|z  |d          |d         z
  |z  |d         |d         z  |z  gd|z  |d          |d         z
  |z  |d         |d         z  |z  gd|z  |d          |d         z
  |z  |d         |d         z  |z  ggS |dk    r|d         |d         z
  |d         |d         z
  z  |d         |d         z
  z  }|d         |d         z
  |d         |d         z
  z  |d         |d         z
  z  }|d         |d         z
  |d         |d         z
  z  |d         |d         z
  z  }|d         |d         z
  |d         |d         z
  z  |d         |d         z
  z  }d|z  |d          |d         z
  |d         z
  |z  |d         |d         z  |d         |d         z  z   |d         |d         z  z   |z  |d          |d         z  |d         z  |z  gd|z  |d          |d         z
  |d         z
  |z  |d         |d         z  |d         |d         z  z   |d         |d         z  z   |z  |d          |d         z  |d         z  |z  gd|z  |d          |d         z
  |d         z
  |z  |d         |d         z  |d         |d         z  z   |d         |d         z  z   |z  |d          |d         z  |d         z  |z  gd|z  |d          |d         z
  |d         z
  |z  |d         |d         z  |d         |d         z  z   |d         |d         z  z   |z  |d          |d         z  |d         z  |z  ggS dS )zB
        Calculate the coefficient of lagrange polynomial
        r   r   r   r   r   N)r   )rw   r   lambda_listdenominator1denominator2denominator3denominator4s          r   lagrange_polynomial_coefficientz1SASolverScheduler.lagrange_polynomial_coefficient.  st   
 $$$$K((1,,,,,A::C5LaZZ Q+a.89 ^O{1~A'FG
 Q+a.89 ^O{1~A'FG	 	 aZZ'N[^;AQ\]^Q_@_`L'N[^;AQ\]^Q_@_`L'N[^;AQ\]^Q_@_`L $!!n_{1~5EN[^3lB $!!n_{1~5EN[^3lB $!!n_{1~5EN[^3lB " aZZQ+a.0q>KN24q>KN24  Q+a.0q>KN24q>KN24  Q+a.0q>KN24q>KN24  Q+a.0q>KN24q>KN24  $!!n_{1~5AF,V#AQ7%a.;q>9:%a.;q>9: ## "!n_{1~5AF,V
 $!!n_{1~5AF,V#AQ7%a.;q>9:%a.;q>9: ## "!n_{1~5AF,V
 $!!n_{1~5AF,V#AQ7%a.;q>9:%a.;q>9: ## "!n_{1~5AF,V
 $!!n_{1~5AF,V#AQ7%a.;q>9:%a.;q>9: ## "!n_{1~5AF,V
E- -+ Zr   c           
          |dv sJ |t          |          k    s
J d            g }|                     |dz
  |          }t          |          D ]}d}	t          |          D ]i}
| j        r1|	||         |
         |                     |dz
  |
z
  |||          z  z  }	:|	||         |
         |                     |dz
  |
z
  ||          z  z  }	j|                    |	           t          |          |k    s
J d            |S )N)r   r   r   r   z4the length of lambda list must be equal to the orderr   r   z3the length of coefficients does not match the order)r   r   r"   rq   r   r   r#   )rw   r   r   r   r   r   coefficientslagrange_coefficientr,   coefficientjs              r   get_coefficients_fnz%SASolverScheduler.get_coefficients_fn  sT   $$$$K(((((*`(((#CCEAI{[[u 	- 	-AK5\\  ? #7#:1#=@j@j	A~|SA A $ KK  #7#:1#=@j@j	A~|A A $ KK ,,,,<  E)))+`)))r   noiser   r   c                	   t          |          dk    r|d         n|                    dd          }|+t          |          dk    r	|d         }nt          d          |+t          |          dk    r	|d         }nt          d          |+t          |          dk    r	|d         }nt          d	          |+t          |          d
k    r	|d
         }nt          d          |t          ddd           | j        }	| j        | j        dz            | j        | j                 }}
|                     |
          \  }}
|                     |          \  }}t          j	        |          t          j	        |
          z
  }t          j	        |          t          j	        |          z
  }t          j
        |          }||z
  }g }t          |          D ]m}| j        |z
  }|                     | j        |                   \  }}t          j	        |          t          j	        |          z
  }|                    |           n|                     |||||          }|}| j        rK|dk    rD| j        | j        dz
           }|                     |          \  }}t          j	        |          t          j	        |          z
  }|dxx         dt          j        d|dz  z   |z            z  |dz  dz  |d|dz  z   z  dz
  t          j        d|dz  z   | z            z   d|dz  z   dz  z  z
  z  ||z
  z  z  cc<   |dxx         dt          j        d|dz  z   |z            z  |dz  dz  |d|dz  z   z  dz
  t          j        d|dz  z   | z            z   d|dz  z   dz  z  z
  z  ||z
  z  z  cc<   t          |          D ]o}| j        rA|d|dz  z   |
z  t          j        |dz   |z            z  ||         z  |	|dz             z  z  }J|d|dz  z    |z  ||         z  |	|dz             z  z  }p| j        r9|
t          j        dt          j        d|dz  z  |z            z
            z  |z  }n5||
z  t          j        t          j        d|z            dz
            z  |z  }| j        r+t          j        |dz   |z            |
|z  z  |z  |z   |z   }n||z  |z  |z   |z   }|                    |j                  }|S )ag  
        One step for the SA-Predictor.

        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 SA-Predictor at this timestep.

        Returns:
            `torch.Tensor`:
                The sample tensor at the previous timestep.
        r   prev_timestepNr   z0 missing `sample` as a required keyward argumentr   z/ missing `noise` as a required keyward argumentr   z/ missing `order` as a required keyward argumentr   z- missing `tau` as a required keyward argumentr   zPassing `prev_timestep` is deprecated and has no effect as model output conversion is now handled via an internal counter `self.step_index`r:   r   r   r!   r   rp   rg   r{   r   r%   re   
zeros_liker"   r#   r   rq   r   rb   rv   r    ) rw   r   r   r   r   r   r   r   r   model_output_listrd   sigma_s0rc   alpha_s0rf   	lambda_s0gradient_parthr   r,   sialpha_sisigma_si	lambda_sigradient_coefficientsx
temp_sigmatemp_alpha_stemp_sigma_stemp_lambda_s
noise_partx_ts                                    r   !stochastic_adams_bashforth_updatez3SASolverScheduler.stochastic_adams_bashforth_update  s   6 $'t99q==QfjjRV6W6W>4yy1}}a !STTT=4yy1}}Q !RSSS=4yy1}}Q !RSSS;4yy1}}1g !PQQQ$ ^  
 !.K!+,K(   77@@!99(CC(9W%%	'(:(::Ih''%)H*=*==	(00y u 	* 	*A1$B!%!=!=dk"o!N!NHh	(++ei.A.AAIy)))) $ 8 8	8U`be f f? 	

 "[1)<=
-1-I-I*-U-U*l %	, 7 7%)L:Q:Q Q%a(((iS!Vx 7889!tax1CF
#3a#7%)QaZUVTVDW:X:X#X^_beghbh^hmn]n"ooq !=02((( &a(((iS!Vx 7889!tax1CF
#3a#7%)QaZUVTVDW:X:X#X^_beghbh^hmn]n"ooq !=02((( u 
	r 
	rA 	raZi#q&	H 4556 ,A./ (!a%1	2 1sAv:!8;PQR;S!SVgjknojohpVq!qq? 	R 5:a%)BaK!O2L2L.L#M#MMPUUJJwEIa!e4D4Dq4H)I)IIEQJ? 	H)c1fIM**g.@AAEUXbbCCX%*]:ZGCffQWoo
r   this_model_outputrs   
last_noisethis_samplec                	   t          |          dk    r|d         n|                    dd          }	|+t          |          dk    r	|d         }nt          d          |+t          |          dk    r	|d         }nt          d          |+t          |          dk    r	|d         }nt          d	          |+t          |          d
k    r	|d
         }nt          d          |+t          |          dk    r	|d         }nt          d          |	t          ddd           | j        }
| j        | j                 | j        | j        dz
           }}|                     |          \  }}|                     |          \  }}t          j	        |          t          j	        |          z
  }t          j	        |          t          j	        |          z
  }t          j
        |          }||z
  }g }t          |          D ]m}| j        |z
  }|                     | j        |                   \  }}t          j	        |          t          j	        |          z
  }|                    |           n|
|gz   }|                     |||||          }|}| j        r|dk    r|dxx         dt          j        d|dz  z   |z            z  |dz  |d|dz  z   z  dz
  t          j        d|dz  z   | z            z   d|dz  z   dz  |z  z  z
  z  z  cc<   |dxx         dt          j        d|dz  z   |z            z  |dz  |d|dz  z   z  dz
  t          j        d|dz  z   | z            z   d|dz  z   dz  |z  z  z
  z  z  cc<   t          |          D ]o}| j        rA|d|dz  z   |z  t          j        |dz   |z            z  ||         z  ||dz             z  z  }J|d|dz  z    |z  ||         z  ||dz             z  z  }p| j        r9|t          j        dt          j        d|dz  z  |z            z
            z  |z  }n5||z  t          j        t          j        d|z            dz
            z  |z  }| j        r+t          j        |dz   |z            ||z  z  |z  |z   |z   }n||z  |z  |z   |z   }|                    |j                  }|S )a  
        One step for the SA-Corrector.

        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 order of SA-Corrector at this step.

        Returns:
            `torch.Tensor`:
                The corrected sample tensor at the current timestep.
        r   this_timestepNr   z4 missing`last_sample` as a required keyward argumentr   z3 missing`last_noise` as a required keyward argumentr   z4 missing`this_sample` as a required keyward argumentr   z. missing`order` as a required keyward argument   z, missing`tau` as a required keyward argumentr   zPassing `this_timestep` is deprecated and has no effect as model output conversion is now handled via an internal counter `self.step_index`r:   r   r   )rw   r
  rs   r  r  r   r   r   r   r  r   rd   r   rc   r   rf   r   r   r   r   r,   r   r   r   r   model_prev_listr  r  r  r  s                                 r   stochastic_adams_moulton_updatez1SASolverScheduler.stochastic_adams_moulton_update.  s   > $'t99q==QfjjRV6W6W4yy1}}"1g !WXXX4yy1}}!!W

 !VWWW4yy1}}"1g !WXXX=4yy1}}Q !QRRR;4yy1}}1g !OPPP$ ^   !.K(K!+,   77@@!99(CC(9W%%	'(:(::Ih''%)H*=*==	(55y u 	* 	*A1$B!%!=!=dk"o!N!NHh	(++ei.A.AAIy))))+/@.AA $ 8 8	8U`be f f? 	

 &a(((iS!Vx 78891uQaZ 01 4uy!c1f*RSQSAT7U7U U[\_bde_e[ejkZknoZoppr(((
 &a(((iS!Vx 78891uQaZ 01 4uy!c1f*RSQSAT7U7U U[\_bde_e[ejkZknoZoppr((( u 
	p 
	pA 	paZi#q&	H 4556 ,A./ &Ah/	0 1sAv:!8;PQR;S!SVehilmhmfnVo!oo? 	W 5:a%)BaK!O2L2L.L#M#MMPZZJJwEIa!e4D4Dq4H)I)IIJVJ? 	H)c1fIM**g.@AAEUXbbCCX%*]:ZGCffQWoo
r   c                 .   || j         }||k                                    }t          |          dk    rt          | j                   dz
  }nHt          |          dk    r|d                                         }n|d                                         }|S )Nr   r   )rm   nonzeror   r   )rw   r   schedule_timestepsindex_candidatesr{   s        r   index_for_timestepz$SASolverScheduler.index_for_timestep  s    %!%.(:CCEE  A%%T^,,q0JJ
 !""Q&&)!,1133JJ)!,1133Jr   c                     | j         Ut          |t          j                  r|                    | j        j                  }|                     |          | _        dS | j	        | _        dS )zF
        Initialize the step_index counter for the scheduler.
        N)
r~   
isinstancer%   Tensorrv   rm   r   r  rt   ru   )rw   r   s     r   _init_step_indexz"SASolverScheduler._init_step_index  sd    
 #(EL11 >#;;t~'<==#66x@@D#0Dr   r   return_dictc                    | j         t          d          | j        |                     |           | j        dk    o| j        du}|                     ||          }|rJ|                     | j        d                   }|                     || j        | j	        || j
        |          }t          t          | j        j        | j        j        dz
            dz
            D ]2}	| j        |	dz            | j        |	<   | j        |	dz            | j        |	<   3|| j        d<   || j        d<   t#          |j        ||j        |j                  }
| j        j        rlt-          | j        j        t/          | j                  | j        z
            }t-          | j        j        t/          | j                  | j        z
  dz             }n| j        j        }| j        j        }t-          || j        dz             | _        t-          || j        d	z             | _
        | j        dk    sJ | j
        dk    sJ || _        |
| _	        |                     | j        d                   }|                     |||
| j        |
          }| j        t          | j        j        | j        j        dz
            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 SA-Solver.

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

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

        NzaNumber of inference steps is 'None', you need to run 'set_timesteps' after creating the schedulerr   r   rX   )r
  rs   r  r  r   r   r   )	generatorr   r    r   )r   r   r   r   r   )prev_sample)ri   r!   r{   r  rs   r   rF   ro   r  r  this_corrector_orderr"   rn   r   rC   rD   rp   r   r   r   r    rK   r$   r   rm   rr   this_predictor_orderr	  rt   r   )rw   r   r   r   r  r  use_correctormodel_output_convertcurrent_taur,   r   r   r  r  s                 r   stepzSASolverScheduler.step  s   < #+s   ?"!!(+++!+L0@0L#88f8UU 		--(:2(>??K99"6 ,?"/ :  F s4;68SVW8WXX[\\]] 	> 	>A$($6q1u$=Dq!$($6q1u$=Dq!!!52!)2&$	
 
 
 ;( 	?#&t{'BCDWDWZ^ZiDi#j#j #&t{'BCDWDWZ^ZiDilmDm#n#n  #';#> #';#> $'(<d>SVW>W$X$X!$'(<d>SVW>W$X$X!(1,,,,(1,,,,!mmD$6r$:;;<<-+ = 
 
  3t{'BDKD_bcDc#d#ddd!!Q&!! 	A 	">!;77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.
        r3   )rw   r   r   r   s       r   scale_model_inputz#SASolverScheduler.scale_model_input:  s	     r   original_samplesrm   c                     | j                             |j                  | _         | j                             |j                  }|                    |j                  }||         dz  }|                                }t          |j                  t          |j                  k     r?|                    d          }t          |j                  t          |j                  k     ?d||         z
  dz  }|                                }t          |j                  t          |j                  k     r?|                    d          }t          |j                  t          |j                  k     ?||z  ||z  z   }|S )N)r   r   rS   rX   r   )ra   rv   r   r    flattenr   r   r   )rw   r'  r   rm   ra   sqrt_alpha_prodsqrt_one_minus_alpha_prodnoisy_sampless           r   	add_noisezSASolverScheduler.add_noiseJ  ss    #144<L<S4TT,//6F6L/MMLL!1!899	(3s:)1133/'((3/?/E+F+FFF-77;;O /'((3/?/E+F+FFF &'	)B%Bs$J!$=$E$E$G$G!+122S9I9O5P5PPP(A(K(KB(O(O% +122S9I9O5P5PPP (*::=VY^=^^r   c                     | j         j        S N)r   r>   rz   s    r   __len__zSASolverScheduler.__len__d  s    {..r   )r   )NNr/  )NT)/__name__
__module____qualname____doc__r   _compatiblesr   r
   r   intstrr   r   rj   ndarrayr   r   boolrx   propertyr{   r~   r   r%   r   r   r  r   r   r   r   r   r   r   r   r   r	  r  r  r  r   r   r$  r&  	IntTensorr-  r0  r3   r   r   r1   r1   K   s       < <| ?>$=>>>LE $("%BF  ('+",1"%/"&,1%*U5\\M'+ *)I, I, I, I, 	I,
 I,  bj$u+&= >?I, I, I, I, 8$I, I, %*I,  I, I,  I,  $D>!I," "#I,$  }%I,& 'I,( )I, I, I, I,V     X  ! ! X!( (3 ( ( ( (?, ?, ?,U3PUP\K\E] ?, ?, ?, ?,D     D  0     EL RWR^    :  $	Z Z ZlZ 	Z 
Z Z Z Zx  0* * *Xk k kZ  (}l} 	}
 |} } \} 
} } } }~C <C \	C
 LC \C C \C 
C C C CL   (
1 
1 
1"  d8 d8ld8 d8 	d8 d8 
%	&d8 d8 d8 d8L %,     , | ?	
 
   4/ / / / /r   r1   )r   r   )r   typingr   r   r   r   r   r   rj   r%   configuration_utilsr	   r
   utilsr   utils.torch_utilsr   scheduling_utilsr   r   r   r/   r1   r3   r   r   <module>rA     s  $  9 9 9 9 9 9 9 9 9 9 9 9 9 9      A A A A A A A A       , , , , , , X X X X X X X X X X !)4 )4 )4 )4XZ/ Z/ Z/ Z/ Z/ Z/ Z/ Z/ Z/ Z/r   