o
    Gi3                     @   s~   d dl Z d dlm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 eG d	d
 d
eZG dd deeZdS )    N)	dataclass   )ConfigMixinregister_to_config)
BaseOutput)randn_tensor   )SchedulerMixinSchedulerOutputc                   @   s&   e Zd ZU dZejed< ejed< dS )SdeVeOutputa  
    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.
        prev_sample_mean (`torch.Tensor` of shape `(batch_size, num_channels, height, width)` for images):
            Mean averaged `prev_sample` over previous timesteps.
    prev_sampleprev_sample_meanN)__name__
__module____qualname____doc__torchTensor__annotations__ r   r   Z/home/ubuntu/.local/lib/python3.10/site-packages/diffusers/schedulers/scheduling_sde_ve.pyr      s   
 
r   c                   @   s:  e Zd ZdZdZe						d-ded	ed
edededefddZd.de	j
dede	j
fddZd/dededee	jB fddZ	d0ded
ededefddZdd Z		d1de	j
dede	j
d e	jdB d!edeeB fd"d#Z		d1de	j
de	j
d e	jdB d!edeeB f
d$d%Zd&e	j
d'e	j
d(e	j
de	j
fd)d*Zd+d, ZdS )2ScoreSdeVeSchedulera  
    `ScoreSdeVeScheduler` is a variance exploding 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 1000):
            The number of diffusion steps to train the model.
        snr (`float`, defaults to 0.15):
            A coefficient weighting the step from the `model_output` sample (from the network) to the random noise.
        sigma_min (`float`, defaults to 0.01):
            The initial noise scale for the sigma sequence in the sampling procedure. The minimum sigma should mirror
            the distribution of the data.
        sigma_max (`float`, defaults to 1348.0):
            The maximum value used for the range of continuous timesteps passed into the model.
        sampling_eps (`float`, defaults to 1e-5):
            The end value of sampling where timesteps decrease progressively from 1 to epsilon.
        correct_steps (`int`, defaults to 1):
            The number of correction steps performed on a produced sample.
    r     333333?{Gz?     @h㈵>num_train_timestepssnr	sigma_min	sigma_maxsampling_epscorrect_stepsc                 C   s    || _ d | _| |||| d S N)init_noise_sigma	timesteps
set_sigmas)selfr   r   r   r    r!   r"   r   r   r   __init__F   s   zScoreSdeVeScheduler.__init__Nsampletimestepreturnc                 C   s   |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   r   scale_model_inputX   s   z%ScoreSdeVeScheduler.scale_model_inputnum_inference_stepsdevicec                 C   s,   |dur|n| j j}tjd|||d| _dS )aA  
        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.
            sampling_eps (`float`, *optional*):
                The final timestep value (overrides value given during scheduler instantiation).
            device (`str` or `torch.device`, *optional*):
                The device to which the timesteps should be moved to. If `None`, the timesteps are not moved.

        Nr   r.   )configr!   r   linspacer%   )r'   r-   r!   r.   r   r   r   set_timestepsi   s   z!ScoreSdeVeScheduler.set_timestepsc                    s   durn| j j dur n| j j |dur|n| j j}| jdu r)| ||   | j|   | _tt	t
t
 || _t fdd| jD | _dS )a  
        Sets the noise scales used for the diffusion chain (to be run before inference). The sigmas control the weight
        of the `drift` and `diffusion` components of the sample update.

        Args:
            num_inference_steps (`int`):
                The number of diffusion steps used when generating samples with a pre-trained model.
            sigma_min (`float`, optional):
                The initial noise scale value (overrides value given during scheduler instantiation).
            sigma_max (`float`, optional):
                The final noise scale value (overrides value given during scheduler instantiation).
            sampling_eps (`float`, optional):
                The final timestep value (overrides value given during scheduler instantiation).

        Nc                    s   g | ]
}  |  qS r   r   ).0tr    r   r   r   
<listcomp>   s    z2ScoreSdeVeScheduler.set_sigmas.<locals>.<listcomp>)r0   r   r    r!   r%   r2   sigmasr   expr1   mathlogdiscrete_sigmastensor)r'   r-   r   r    r!   r   r5   r   r&   z   s   
""zScoreSdeVeScheduler.set_sigmasc                 C   s2   t |dkt ||j| j|d  |jS )Nr   r   )r   where
zeros_liketor.   r;   )r'   r%   r4   r   r   r   get_adjacent_sigma   s
   z&ScoreSdeVeScheduler.get_adjacent_sigmaTmodel_output	generatorreturn_dictc                 C   s$  | j du r	td|tj|jd |jd }|t| j d   }|| j	j}| j	| |j}| 
|||j}t|}	|d |d  d }
|
 }
t|
jt|jk rg|
d}
t|
jt|jk sX|	|
d |  }	t|j|j||j|jd	}||	 }||
|  }|s||fS t||d
S )aD  
        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`):
                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`, *optional*, defaults to `True`):
                Whether or not to return a [`~schedulers.scheduling_sde_ve.SdeVeOutput`] or `tuple`.

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

        NY`self.timesteps` is not set, you need to run 'set_timesteps' after creating the schedulerr   r/   r   r         ?)layoutrB   r.   dtype)r   r   )r%   
ValueErrorr   onesshaper.   lenlongr?   r;   r@   r>   flatten	unsqueezer   rG   rH   r   )r'   rA   r*   r)   rB   rC   r%   sigmaadjacent_sigmadrift	diffusionnoiser   r   r   r   r   	step_pred   s4   


zScoreSdeVeScheduler.step_predc                 C   s  | j du r	tdt|j|j|d|j}tj|	|jd ddd
 }tj|	|jd ddd
 }| jj| | d d }|t|jd |j }| }t|jt|jk rm|d}t|jt|jk s^|||  }	|	|d d |  }
|s|
fS t|
d	S )
a  
        Correct the predicted sample based on the `model_output` of the network. This is often run repeatedly after
        making the prediction for the previous timestep.

        Args:
            model_output (`torch.Tensor`):
                The direct output from learned diffusion model.
            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`, *optional*, defaults to `True`):
                Whether or not to return a [`~schedulers.scheduling_sde_ve.SdeVeOutput`] or `tuple`.

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

        NrD   )rG   rB   r   rF   )dimr   rE   )r   )r%   rI   r   rK   rG   r?   r.   r   normreshapemeanr0   r   rJ   rN   rL   rO   r
   )r'   rA   r)   rB   rC   rT   	grad_norm
noise_norm	step_sizer   r   r   r   r   step_correct   s$   
  

z ScoreSdeVeScheduler.step_correctoriginal_samplesrT   r%   c                 C   sh   | |j}| j |j| }|d ur||d d d d d f  nt||d d d d d f  }|| }|S r#   )r?   r.   r;   r   
randn_like)r'   r^   rT   r%   r7   noisy_samplesr   r   r   	add_noise  s   zScoreSdeVeScheduler.add_noisec                 C   s   | j jS r#   )r0   r   )r'   r   r   r   __len__)  s   zScoreSdeVeScheduler.__len__)r   r   r   r   r   r   r#   )NN)NNN)NT)r   r   r   r   orderr   intfloatr(   r   r   r,   strr.   r2   r&   r@   	Generatorboolr   tuplerU   r
   r]   ra   rb   r   r   r   r   r   -   s    

H
7
r   )r9   dataclassesr   r   configuration_utilsr   r   utilsr   utils.torch_utilsr   scheduling_utilsr	   r
   r   r   r   r   r   r   <module>   s   