o
    ۷i                     @   sN   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 G dd deeZ	dS )	    N   )ConfigMixinregister_to_config)randn_tensor   )SchedulerMixinc                   @   sJ   e Zd ZdZdZedddZdd
eej	B fddZ
dddZdd Zd	S )ScoreSdeVpSchedulera  
    `ScoreSdeVpScheduler` is a variance preserving stochastic differential equation (SDE) scheduler.

    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 2000):
            The number of diffusion steps to train the model.
        beta_min (`int`, defaults to 0.1):
        beta_max (`int`, defaults to 20):
        sampling_eps (`int`, defaults to 1e-3):
            The end value of sampling where timesteps decrease progressively from 1 to epsilon.
         皙?   MbP?c                 C   s   d | _ d | _d | _d S N)sigmasdiscrete_sigmas	timesteps)selfnum_train_timestepsbeta_minbeta_maxsampling_eps r   g/home/ubuntu/vllm_env/lib/python3.10/site-packages/diffusers/schedulers/deprecated/scheduling_sde_vp.py__init__,   s   
zScoreSdeVpScheduler.__init__Ndevicec                 C   s   t jd| jj||d| _dS )a  
        Sets the continuous 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   N)torchlinspaceconfigr   r   )r   num_inference_stepsr   r   r   r   set_timesteps2   s   
z!ScoreSdeVpScheduler.set_timestepsc                 C   sj  | j du r	tdd|d  | jj| jj  d| | jj  }tdtd|  }| }t	|j
t	|j
k rH|d}t	|j
t	|j
k s9| | }d	t	| j  }| jj|| jj| jj   }| }t	|j
t	|j
k r|d}t	|j
t	|j
k spd
| | }	t|}
|	|
d |  }	||	|  }t|j
|j||j|jd}||
t|  |  }||fS )ax  
        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:
            score ():
            x ():
            t ():
            generator (`torch.Generator`, *optional*):
                A random number generator.
        NzY`self.timesteps` is not set, you need to run 'set_timesteps' after creating the schedulerg      пr   g      ?g      ?g       @g      g      )layout	generatorr   dtype)r   
ValueErrorr   r   r   r   sqrtexpflattenlenshape	unsqueezer   r!   r   r#   math)r   scorextr"   log_mean_coeffstddtbeta_tdrift	diffusionx_meannoiser   r   r   	step_pred>   s0   
,



zScoreSdeVpScheduler.step_predc                 C   s   | j jS r   )r   r   )r   r   r   r   __len__k   s   zScoreSdeVpScheduler.__len__)r
   r   r   r   r   )__name__
__module____qualname____doc__orderr   r   strr   r   r   r7   r8   r   r   r   r   r      s    
-r   )
r+   r   configuration_utilsr   r   utils.torch_utilsr   scheduling_utilsr   r   r   r   r   r   <module>   s   