
    iV              	       *   d dl Z d dlm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ZddlmZmZ ddlmZmZ ddlmZmZ  e       rd dlZe G d	 d
e             Z G d d      Z G d d      Z	 	 ddededed   dej:                  fdZ G d dee      Zy)    N)	dataclass)ListLiteralOptionalTupleUnion   )ConfigMixinregister_to_config)
BaseOutputis_scipy_available   )KarrasDiffusionSchedulersSchedulerMixinc                   X    e Zd ZU dZej
                  ed<   dZeej
                     ed<   y)DPMSolverSDESchedulerOutputaq  
    Output class for the scheduler's `step` function output.

    Args:
        prev_sample (`torch.Tensor` of shape `(batch_size, num_channels, height, width)` for images):
            Computed sample `(x_{t-1})` of previous timestep. `prev_sample` should be used as next model input in the
            denoising loop.
        pred_original_sample (`torch.Tensor` of shape `(batch_size, num_channels, height, width)` for images):
            The predicted denoised sample `(x_{0})` based on the model output from the current timestep.
            `pred_original_sample` can be used to preview progress or for guidance.
    prev_sampleNpred_original_sample)	__name__
__module____qualname____doc__torchTensor__annotations__r   r        w/home/obispo/Crisostomo_bridge/mision_env/lib/python3.12/site-packages/diffusers/schedulers/scheduling_dpmsolver_sde.pyr   r       s'    
 37(5<<07r   r   c                   .    e Zd ZdZddZed        Zd Zy)BatchedBrownianTreezGA wrapper around torchsde.BrownianTree that enables batches of entropy.Nc                     | j                  ||      \  }}| _        |j                  dt        j                  |            }|%t        j
                  ddg       j                         }d| _        	 t        |      |j                  d   k(  sJ |d   }|D cg c]>  }t        j                  |||j                  |j                  |j                  |ddd	      @ c}| _        y # t        $ r |g}d| _        Y ew xY wc c}w )	Nw0r   l    TFgư>   )	t0t1sizedtypedeviceentropytol	pool_sizehalfway_tree)sortsigngetr   
zeros_likerandintitembatchedlenshape	TypeErrortorchsdeBrownianIntervalr'   r(   trees)selfxr$   r%   seedkwargsr"   ss           r   __init__zBatchedBrownianTree.__init__6   s     IIb"-B	ZZe..q12<==Ir2779D	!t9
***AB  
  %%XXhhyy!


  	!6D DL	!
s   0"C" AC;"C87C8c                      | |k  r| |dfS || dfS )Nr   r   )abs     r   r-   zBatchedBrownianTree.sortQ   s    E1ay11bz1r   c           	          | j                  ||      \  }}}t        j                  | j                  D cg c]  } |||       c}      | j                  |z  z  }| j
                  r|S |d   S c c}w )Nr   )r-   r   stackr9   r.   r3   )r:   r$   r%   r.   treews         r   __call__zBatchedBrownianTree.__call__U   sc    yyR(BKK$**=$b"=>$))dBRSLLq*ad* >s   A.N)r   r   r   r   r?   staticmethodr-   rH   r   r   r   r    r    3   s#    Q
6 2 2+r   r    c                   $    e Zd ZdZdd fdZd Zy)BrownianTreeNoiseSampleras  A noise sampler backed by a torchsde.BrownianTree.

    Args:
        x (Tensor): The tensor whose shape, device and dtype to use to generate
            random samples.
        sigma_min (float): The low end of the valid interval.
        sigma_max (float): The high end of the valid interval.
        seed (int or List[int]): The random seed. If a list of seeds is
            supplied instead of a single integer, then the noise sampler will use one BrownianTree per batch item, each
            with its own seed.
        transform (callable): A function that maps sigma to the sampler's
            internal timestep.
    Nc                     | S rI   r   )r;   s    r   <lambda>z!BrownianTreeNoiseSampler.<lambda>j   s    q r   c                     || _         | j                  t        j                  |            | j                  t        j                  |            }}t        ||||      | _        y rI   )	transformr   	as_tensorr    rF   )r:   r;   	sigma_min	sigma_maxr<   rP   r$   r%   s           r   r?   z!BrownianTreeNoiseSampler.__init__j   sI    "	 :;T^^EOO\eLf=gB'2r48	r   c                     | j                  t        j                  |            | j                  t        j                  |            }}| j                  ||      ||z
  j	                         j                         z  S rI   )rP   r   rQ   rF   abssqrt)r:   sigma
sigma_nextr$   r%   s        r   rH   z!BrownianTreeNoiseSampler.__call__o   sY     67XbHc9dByyR BG==?#7#7#999r   )r   r   r   r   r?   rH   r   r   r   rL   rL   [   s     6:[ 9
:r   rL   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    r   alpha_bar_fnz)betas_for_alpha_bar.<locals>.alpha_bar_fn   s-    88QY%/$''9A=>!CCr   r]   c                 2    t        j                  | dz        S )Ng      ()ra   r]   rd   s    r   rf   z)betas_for_alpha_bar.<locals>.alpha_bar_fn   s    88AI&&r   z"Unsupported alpha_transform_type: r   r'   )
ValueErrorrangeappendminr   tensorfloat32)rY   rZ   r[   rf   betasir%   t2s           r   betas_for_alpha_barrr   u   s    0 x'	D 
	&	' =>R=STUUE*+ M((!e..S\"-R0@@@(KLM <<U]]33r   c                   B   e Zd ZdZeD  cg c]  }|j
                   c}} ZdZe	 	 	 	 	 	 	 	 	 	 	 	 d2de	de
de
dedeeej                  ee
   f      d	ed
ee   dee   dee   dee	   dede	fd       Z	 d3dee
ej(                  f   deej(                     de	fdZdee
ej(                  f   ddfdZed        Zed        Zed        Zd4de	fdZdej(                  dee
ej(                  f   dej(                  fdZ	 	 d5de	deeej:                  f   dee	   fdZd  Zd! Z d"ej(                  dej(                  fd#Z!d"ej(                  de	dej(                  fd$Z"	 d6d"ej(                  de	d%e
d&e
dej(                  f
d'Z#ed(        Z$	 	 d7d)eej(                  ej                  f   dee
ej(                  f   deej(                  ej                  f   d*ed+e
dee%e&f   fd,Z'd-ej(                  d.ej(                  d/ej(                  dej(                  fd0Z(d1 Z)yc c}} w )8DPMSolverSDEScheduleru
  
    DPMSolverSDEScheduler implements the stochastic sampler from the [Elucidating the Design Space of Diffusion-Based
    Generative Models](https://huggingface.co/papers/2206.00364) paper.

    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.00085):
            The starting `beta` value of inference.
        beta_end (`float`, defaults to 0.012):
            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` or `scaled_linear`.
        trained_betas (`np.ndarray`, *optional*):
            Pass an array of betas directly to the constructor to bypass `beta_start` and `beta_end`.
        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://huggingface.co/papers/2210.02303) paper).
        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.
        noise_sampler_seed (`int`, *optional*, defaults to `None`):
            The random seed to use for the noise sampler. If `None`, a random seed is generated.
        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.
    r	   Nnum_train_timesteps
beta_startbeta_endbeta_scheduletrained_betasprediction_typeuse_karras_sigmasuse_exponential_sigmasuse_beta_sigmasnoise_sampler_seedtimestep_spacingsteps_offsetc                    | 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                         d| j                  z
  | _        t        j"                  | j                   d      | _        | j'                  |d |       || _        d | _        |
| _        d | _        d | _        | j0                  j3                  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.rh   linearscaled_linear      ?r	   squaredcos_cap_v2z is not implemented for       ?r   )dimcpu)configr}   r   ImportErrorsumr|   r{   ri   r   rm   rn   ro   linspacerr   NotImplementedError	__class__alphascumprodalphas_cumprodset_timestepsnoise_samplerr~   _step_index_begin_indexsigmasto)r:   ru   rv   rw   rx   ry   rz   r{   r|   r}   r~   r   r   s                r   r?   zDPMSolverSDEScheduler.__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DJJ&#mmDKKQ? 	.6IJ!2!"4 kknnU+r   timestepschedule_timestepsr^   c                     || j                   }||k(  j                         }t        |      dkD  rdnd}||   j                         S )ak  
        Find the index of a given timestep in the timestep schedule.

        Args:
            timestep (`float` or `torch.Tensor`):
                The timestep value to find in the schedule.
            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. For the very first step, returns the second index if
                multiple matches exist to avoid skipping a sigma when starting mid-schedule (e.g., for image-to-image).
        r   r   )	timestepsnonzeror4   r2   )r:   r   r   indicesposs        r   index_for_timestepz(DPMSolverSDEScheduler.index_for_timestep   sN    " %!%%1::< w<!#as|  ""r   c                     | j                   Vt        |t        j                        r%|j	                  | j
                  j                        }| j                  |      | _        y| j                  | _        y)z
        Initialize the step index for the scheduler based on the given timestep.

        Args:
            timestep (`float` or `torch.Tensor`):
                The current timestep to initialize the step index from.
        N)
begin_index
isinstancer   r   r   r   r(   r   r   r   )r:   r   s     r   _init_step_indexz&DPMSolverSDEScheduler._init_step_index  sW     #(ELL1#;;t~~'<'<=#66x@D#00Dr   c                     | j                   j                  dv r| j                  j                         S | j                  j                         dz  dz   dz  S )N)r   trailingr	   r   r   )r   r   r   maxr:   s    r   init_noise_sigmaz&DPMSolverSDEScheduler.init_noise_sigma,  sH     ;;''+CC;;??$$!Q&*s22r   c                     | j                   S )zg
        The index counter for current timestep. It will increase 1 after each scheduler step.
        )r   r   s    r   
step_indexz DPMSolverSDEScheduler.step_index4  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   r   z!DPMSolverSDEScheduler.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%DPMSolverSDEScheduler.set_begin_indexC  s     (r   samplec                     | j                   | j                  |       | j                  | j                      }| j                  r|n| j                  }||dz  dz   dz  z  }|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.
            timestep (`int`, *optional*):
                The current timestep in the diffusion chain.

        Returns:
            `torch.Tensor`:
                A scaled input sample.
        r	   r   r   )r   r   r   state_in_first_ordermid_point_sigma)r:   r   r   rW   sigma_inputs        r   scale_model_inputz'DPMSolverSDEScheduler.scale_model_inputM  s`    & ??"!!(+DOO,#88ed>R>RKNQ.367r   num_inference_stepsr(   c           	      	   || _         |xs | j                  j                  }| j                  j                  dk(  r6t	        j
                  d|dz
  |t              ddd   j                         }n'| j                  j                  dk(  rw|| j                   z  }t	        j                  d|      |z  j                         ddd   j                         j                  t              }|| j                  j                  z  }n| j                  j                  dk(  r\|| j                   z  }t	        j                  |d|       j                         j                         j                  t              }|dz  }n"t        | j                  j                   d	      t	        j                  d| j                  z
  | j                  z  d
z        }t	        j                  |      }t	        j                   |t	        j                  dt#        |            |      }| j                  j$                  rF| j'                  |      }t	        j                  |D cg c]  }| j)                  ||       c}      }n| j                  j*                  rG| j-                  ||      }t	        j                  |D cg c]  }| j)                  ||       c}      }n\| j                  j.                  rF| j1                  ||      }t	        j                  |D cg c]  }| j)                  ||       c}      }| j3                  ||      }	t	        j4                  |dgg      j                  t        j6                        }t9        j:                  |      j=                  |      }t9        j>                  |dd |dd jA                  d      |dd g      | _!        t9        j:                  |      }t9        j:                  |	      }	t9        j>                  |dd |dd jA                  d      g      }|	|ddd<   tE        |      jG                  d      r'|j=                  |t8        j6                        | _$        n|j=                  |      | _$        d| _%        d| _&        d| _'        d| _(        | jB                  j=                  d      | _!        d| _)        yc c}w c c}w c c}w )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   rh   NrA   leadingr   zY is not supported. Please make sure to choose one of 'linspace', 'leading' or 'trailing'.r   )	in_sigmas)r   r   g        )r(   r	   mpsr   )*r   r   ru   r   npr   floatcopyarangeroundastyper   ri   arrayr   loginterpr4   r{   _convert_to_karras_sigma_to_tr|   _convert_to_exponentialr}   _convert_to_beta_second_order_timestepsconcatenatern   r   
from_numpyr   catrepeat_interleaver   str
startswithr   r   r   r   r   r   )
r:   r   r(   ru   r   
step_ratior   
log_sigmasrW   second_order_timestepss
             r   r   z#DPMSolverSDEScheduler.set_timestepsh  s    $7 1TT[[5T5T ;;'':5A':Q'>@S[`abfdfbfgllnI[[))Y6,0H0HHJ 1&9:ZGNNPQUSUQUV[[]ddejkI111I[[))Z7,t/G/GGJ #6J;GNNPUUW^^_deINI;;//0  1J  K  A 3 33t7J7JJsRSVVF^
9bii3v;&?H;;((,,v,>FSY!Z%$"2"25*"E!Z[I[[//11FXk1lFSY!Z%$"2"25*"E!Z[I[[((**VQd*eFSY!Z%$"2"25*"E!Z[I!%!=!=fj!Q#077

C!!&),,F,;iiVAb\-K-KA-NPVWYWZP[ \]$$Y/	!&!1!12H!IIIy!}im.M.Ma.PQR	0	!$Q$v;!!%(&\\&\FDN&\\\8DN # kknnU+!A "[ "[ "[s   S+SSc           	          d }d }d} ||      }t        j                  |      }|d d ||z  z   } ||      }	t        j                  |	D 
cg c]  }
| j                  |
|       c}
      }|S c c}
w )Nc                 .    t        j                  |        S rI   )r   r]   _ts    r   sigma_fnz?DPMSolverSDEScheduler._second_order_timesteps.<locals>.sigma_fn  s    662#;r   c                 .    t        j                  |        S rI   )r   r   _sigmas    r   t_fnz;DPMSolverSDEScheduler._second_order_timesteps.<locals>.t_fn  s    FF6N?"r   r   rA   )r   diffr   r   )r:   r   r   r   r   midpoint_ratiore   
delta_time
t_proposedsig_proposedrW   r   s               r   r   z-DPMSolverSDEScheduler._second_order_timesteps  sz    		# LWWQZ
sVj>99

+HH|\ed..ujA\]	 ]s   A.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   r   maximumnewaxiscumsumargmaxclipr5   reshape)r:   rW   r   	log_sigmadistslow_idxhigh_idxlowhighrG   re   s              r   r   z!DPMSolverSDEScheduler._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   r   c                     |d   j                         }|d   j                         }d}t        j                  dd| j                        }|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.

        Returns:
            `torch.Tensor`:
                The converted sigma values following the Karras noise schedule.
        rA   r   g      @r   )r2   r   r   r   )	r:   r   rR   rS   rhorampmin_inv_rhomax_inv_rhor   s	            r   r   z(DPMSolverSDEScheduler._convert_to_karras  s     %R=--/	$Q<,,.	{{1a!9!9: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.
        rR   NrS   rA   r   )
hasattrr   rR   rS   r2   r   r]   r   ra   r   )r:   r   r   rR   rS   r   s         r   r   z-DPMSolverSDEScheduler._convert_to_exponential  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.
        rR   NrS   rA   r   r   )r   r   rR   rS   r2   r   r   r   scipystatsr   ppf)
r:   r   r   r   r   rR   rS   r   r   r   s
             r   r   z&DPMSolverSDEScheduler._convert_to_beta'  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c                     | j                   d u S rI   )r   r   s    r   r   z*DPMSolverSDEScheduler.state_in_first_orderW  s    {{d""r   model_outputreturn_dicts_noisec                    | j                   | j                  |       | j                  a| j                  | j                  dkD     j	                         | j                  j                         }}t        |||| j                        | _        dt        j                  dt        j                  fd}dt        j                  dt        j                  fd}	| j                  r6| j                  | j                      }
| j                  | j                   dz      }n5| j                  | j                   dz
     }
| j                  | j                      }d	} |	|
       |	|      }}||z
  }|||z  z   }| j                  j                  d
k(  r| j                  r|
n ||      }|||z  z
  }n| j                  j                  dk(  r5| j                  r|
n ||      }|| |dz  dz   d	z  z  z  ||dz  dz   z  z   }nG| j                  j                  dk(  rt        d      t        d| j                  j                   d      |dk(  r||z
  |
z  }||
z
  }|||z  z   }n| j                  r|}n| j                  } ||      } ||      }t	        ||dz  |dz  |dz  z
  z  |dz  z  d	z        }|dz  |dz  z
  d	z  } |	|      } ||       ||      z  |z  ||z
  j!                         |z  z
  }|| j                   ||       ||            |z  |z  z   }| j                  r|| _         ||      | _        nd| _        d| _        | 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 diffusion
        process from the learned model outputs (most often the predicted noise).

        Args:
            model_output (`torch.Tensor` or `np.ndarray`):
                The direct output from learned diffusion model.
            timestep (`float` or `torch.Tensor`):
                The current discrete timestep in the diffusion chain.
            sample (`torch.Tensor` or `np.ndarray`):
                A current instance of a sample created by the diffusion process.
            return_dict (`bool`):
                Whether or not to return a [`~schedulers.scheduling_dpmsolver_sde.DPMSolverSDESchedulerOutput`] or
                tuple.
            s_noise (`float`, *optional*, defaults to 1.0):
                Scaling factor for noise added to the sample.

        Returns:
            [`~schedulers.scheduling_dpmsolver_sde.DPMSolverSDESchedulerOutput`] or `tuple`:
                If return_dict is `True`, [`~schedulers.scheduling_dpmsolver_sde.DPMSolverSDESchedulerOutput`] is
                returned, otherwise a tuple is returned where the first element is the sample tensor.
        Nr   r   r^   c                 >    | j                         j                         S rI   )negr]   r   s    r   r   z,DPMSolverSDEScheduler.step.<locals>.sigma_fn  s    668<<>!r   r   c                 >    | j                         j                         S rI   )r   r   r   s    r   r   z(DPMSolverSDEScheduler.step.<locals>.t_fn  s    ::<##%%r   r   r   epsilonv_predictionr	   r   z+prediction_type not implemented yet: samplezprediction_type given as z, must be one of `epsilon`, or `v_prediction`)r   r   )r   r   r   r   rl   r   rL   r~   r   r   r   r   rz   r   ri   r   expm1r   r   r   )r:   r   r   r   r   r   	min_sigma	max_sigmar   r   rW   rX   r   re   t_nextr   r   r   r   
derivativedtr   
sigma_fromsigma_tosigma_up
sigma_downancestral_ts                              r   stepzDPMSolverSDEScheduler.step[  s   < ??"!!(+ %#';;t{{Q#?#C#C#Et{{GXyI!9&)YX\XoXo!pD	" 	"%,, 	"	& 	&%,, 	& $$KK0ET__q%89J KK! 34ET__5J Kj!16aZ
n44
 ;;&&)3#'#<#<%(:BVK#)K,,F#F [[((N:#'#<#<%(:BVK#/K<;PQ>TUCUZ]B]3]#^+q.1,-$  [[((H4%&STT+DKK,G,G+HHtu  ? #775@Je#B :?2K((#!!J'H8hkZ]Xq[5P&QT^`aTa&afi%ijH"A+!3;Jz*K#K08A;>&HKeg,L- -K &(:(:8A;QWHX(Y\c(cfn(nnK(($'/'7$ #'+$ 	A$ 
 +{Ymnnr   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=|||z  z   }	|	S c c}w )am  
        Add noise to the original samples according to the noise schedule at the specified timesteps.

        Args:
            original_samples (`torch.Tensor`):
                The original samples to which noise will be added.
            noise (`torch.Tensor`):
                The noise tensor to add to the original samples.
            timesteps (`torch.Tensor`):
                The timesteps at which to add noise, determining the noise level from the schedule.

        Returns:
            `torch.Tensor`:
                The noisy samples with added noise scaled according to the timestep schedule.
        )r(   r'   r   rh   r   rA   )r   r   r(   r'   typer   is_floating_pointr   rn   r   r   r   r5   flattenr4   	unsqueeze)
r:   r  r  r   r   r   re   step_indicesrW   noisy_sampless
             r   	add_noisezDPMSolverSDEScheduler.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%;%;!<< )55=8 _s   G9c                 .    | j                   j                  S rI   )r   ru   r   s    r   __len__zDPMSolverSDEScheduler.__len__  s    {{...r   )i  g_QK?g~jt?r   Nr  FFFNr   r   rI   )r   )NN)333333?r  )Tr   )*r   r   r   r   r   name_compatiblesorderr   intr   r   r   r   r   ndarrayr   boolr?   r   r   r   r   propertyr   r   r   r   r   r(   r   r   r   r   r   r   r   r   r   r  r  r  ).0es   00r   rt   rt      s   &P %>>qAFF>LE $(#%BF(,116*/,0 *,, ,, ,, 	,,
 ,,  bjj$u+&= >?,, ,, $D>,, !),, "$,, %SM,, ,, ,, ,,` bf#eU\\12#HPQVQ]Q]H^#	#>1uell/B)C 1 1 3 3     ! !(3 ( u||+, 
	< ,0-1	M" M" c5<<'(M" &c]	M"^ "JELL U\\ 2 TW \a\h\h F dg..<?.HM.[`.	.` # # !qoELL"**45qo u||+,qo ellBJJ./	qo
 qo qo 
*E1	2qoh.,,. ||. <<	.
 
.`/i ?s   Hrt   )g+?r\   ) ra   dataclassesr   typingr   r   r   r   r   numpyr   r   r7   configuration_utilsr
   r   utilsr   r   scheduling_utilsr   r   scipy.statsr   r   r    rL   r   r   r   rr   rt   r   r   r   <module>r-     s     ! 8 8    A 2 G  8* 8 8"%+ %+P: :8 5=*4 *4*4 "/2*4 \\	*4Z^	/NK ^	/r   