o
    }oi]                     @   sv   d dl mZ d dlmZmZ d dlZd dlZd dlmZ d dl	m	Z	 G dd dZ
G dd	 d	ZG d
d dejZdS )    )
NormalDist)CallableTupleN)nn)tqdmc                   @   sD   e Zd Zd
defddZdejdeejejejejf fddZd	S )
EDMScaling      ?
sigma_datac                 C   s
   || _ d S N)r	   )selfr	    r   ^/home/ubuntu/.local/lib/python3.10/site-packages/nemo/collections/diffusion/sampler/edm/edm.py__init__   s   
zEDMScaling.__init__sigmareturnc                 C   sn   | j d |d | j d   }|| j  |d | j d  d  }d|d | j d  d  }d|  }||||fS )N   r      g      ?)r	   log)r   r   c_skipc_outc_inc_noiser   r   r   __call__   s
    zEDMScaling.__call__N)r   )	__name__
__module____qualname__floatr   torchTensorr   r   r   r   r   r   r      s    ,r   c                	   @   sj   e Zd Z				ddedededefd	d
ZdedejfddZdejdejde	ejejf fddZ
dS )EDMSDE333333333333?      T@Mb`?p_meanp_std	sigma_max	sigma_minc                 C   s&   t ||d| _|| _|| _tj| _d S )N)mur   )r   gaussian_distr&   r'   nprandom
_generator)r   r$   r%   r&   r'   r   r   r   r   %   s   zEDMSDE.__init__
batch_sizer   c                    s8    j j|d} fdd|D }tj|dd}t|S )N)sizec                    s   g | ]} j |qS r   )r)   inv_cdf).0cdf_valr   r   r   
<listcomp>3   s    z#EDMSDE.sample_t.<locals>.<listcomp>cuda)device)r,   uniformr   tensorexp)r   r-   cdf_valssamples_interval_gaussian	log_sigmar   r2   r   sample_t1   s   
zEDMSDE.sample_tx0r   c                 C   s   ||fS r
   r   )r   r=   r   r   r   r   marginal_prob7   s   zEDMSDE.marginal_probN)r    r!   r"   r#   )r   r   r   r   r   intr   r   r<   tupler>   r   r   r   r   r   $   s     
*r   c                   @   sh   e Zd ZdZe ddddddeddfd	ed
ejde	dedededededededejfddZ
dS )
EDMSamplera_  
    Elucidating the Design Space of Diffusion-Based Generative Models (EDM)
    # https://github.com/NVlabs/edm/blob/62072d2612c7da05165d6233d13d17d71f213fee/generate.py#L25

    Attributes:
        None

    Methods:
        forward(x0_fn: Callable, x_sigma_max: torch.Tensor, num_steps: int = 35, sigma_min: float = 0.002,
                sigma_max: float = 80, rho: float = 7, S_churn: float = 0, S_min: float = 0,
                S_max: float = float("inf"), S_noise: float = 1) -> torch.Tensor:
            Performs the forward pass for the EDM sampling process.

            Parameters:
                x0_fn (Callable): A function that takes in a tensor and returns a denoised tensor.
                x_sigma_max (torch.Tensor): The initial noise level tensor.
                num_steps (int, optional): The number of sampling steps. Default is 35.
                sigma_min (float, optional): The minimum noise level. Default is 0.002.
                sigma_max (float, optional): The maximum noise level. Default is 80.
                rho (float, optional): The rho parameter for time step discretization. Default is 7.
                S_churn (float, optional): The churn parameter for noise increase. Default is 0.
                S_min (float, optional): The minimum value for the churn parameter. Default is 0.
                S_max (float, optional): The maximum value for the churn parameter. Default is float("inf").
                S_noise (float, optional): The noise scale for the churn parameter. Default is 1.

            Returns:
                torch.Tensor: The sampled tensor after the EDM process.
    #   r#   P      r   infr   x0_fnx_sigma_max	num_stepsr'   r&   rhoS_churnS_minS_maxS_noiser   c                 C   s  |j }tj|jd ||jd}tj|tj|jd}|d|  ||d  |d|  |d|     | }t|t|d d g}|	tj}t
tt|d d |dd  ddt|d dD ]\}\}}|}||  krr|	krn nt|| tdd nd}|||  }||d |d   |
 t|  }||	||	|| 	tj}|| | }||| |  }||d k r||	||	|| 	tj}|| | }||| d	| d	|    }q`|	|S )
Nr   )dtyper5   r   F)strict)totalr   r   )rN   r   onesshaper5   arangefloat64cat
zeros_liketo	enumerater   ziplenminr*   sqrt
randn_like)r   rF   rG   rH   r'   r&   rI   rJ   rK   rL   rM   in_dtype_onesstep_indicest_stepsx_nextit_curt_nextx_curgammat_hatx_hatdenoisedd_curd_primer   r   r   forwardY   s0   .,4&""
zEDMSampler.forwardN)r   r   r   __doc__r   no_gradr   r   r   r?   rn   r   r   r   r   rA   ;   sD    	
rA   )
statisticsr   typingr   r   numpyr*   r   r   r   r   r   ModulerA   r   r   r   r   <module>   s   