o
    }oiC                      @   s   d Z ddlZddlZddlmZ ddlm  mZ z
ddl	m
Z
 dZW n ey2   ed dZY nw dd Zd	d
 Zdd Zd"ddZdd Zdd Zdd Zd#ddZdejfddZG dd dejZG dd dejZG d d! d!ejZdS )$z
Brought from:
https://github.com/openai/guided-diffusion/blob/main/guided_diffusion/nn.py

Various utilities for neural networks.
    N	GroupNormTz2Fused optimized group norm has not been installed.Fc                 O   V   | dkrt j|i |S | dkrt j|i |S | dkr$t j|i |S td|  )z4
    Create a 1D, 2D, or 3D convolution module.
             unsupported dimensions: )nnConv1dConv2dConv3d
ValueErrordimsargskwargs r   v/home/ubuntu/.local/lib/python3.10/site-packages/nemo/collections/multimodal/modules/imagen/diffusionmodules/layers.pyconv_nd8      r   c                  O   s   t j| i |S )z!
    Create a linear module.
    )r	   Linear)r   r   r   r   r   linearE   s   r   c                 O   r   )z8
    Create a 1D, 2D, or 3D average pooling module.
    r   r   r   r   )r	   	AvgPool1d	AvgPool2d	AvgPool3dr   r   r   r   r   avg_pool_ndL   r   r   Gz?c                 C   s4   t | |D ]\}}| |j|d| d qdS )a#  
    Update target parameters to be closer to those of source parameters using
    an exponential moving average.

    :param target_params: the target parameter sequence.
    :param source_params: the source parameter sequence.
    :param rate: the EMA rate (closer to 1 means slower).
    r   )alphaN)zipdetachmul_add_)target_paramssource_paramsratetargsrcr   r   r   
update_emaY   s   	r'   c                 C   s   |   D ]}|   q| S )z<
    Zero out the parameters of a module and return it.
    )
parametersr   zero_)modulepr   r   r   zero_modulef   s   r,   c                 C   s    |   D ]	}| | q| S )z9
    Scale the parameters of a module and return it.
    )r(   r   r    )r*   scaler+   r   r   r   scale_moduleo   s   r.   c                 C   s   | j ttdt| jdS )z6
    Take the mean over all non-batch dimensions.
    r   dim)meanlistrangelenshape)tensorr   r   r   	mean_flatx   s   r7    c                 C   s   t d| |dS )z
    Make a standard normalization layer.

    :param channels: number of input channels.
    :return: an nn.Module for normalization.
        )actr   )channelsr:   r   r   r   normalization   s   r<   i'  c                 C   s   |d }t t| t jd|t jd | j| jd}| dddf  |d  }|j|d}t j	t 
|t |gdd}|d rXt j	|t |dddd	f gdd}|S )
aY  
    Create sinusoidal timestep embeddings.

    :param timesteps: a 1-D Tensor of N indices, one per batch element.
                      These may be fractional.
    :param dim: the dimension of the output.
    :param max_period: controls the minimum frequency of the embeddings.
    :return: an [N x dim] Tensor of positional embeddings.
    r   r   )startenddtype)deviceN)r?   r/   r   )thexpmathlogarangefloat32tor@   floatcatcossin
zeros_like)	timestepsr0   
max_periodr?   halffreqsr   	embeddingr   r   r   timestep_embedding   s   
((rS   c                       *   e Zd ZdZd fdd	Zdd Z  ZS )	UpsampleaB  
    An upsampling layer with an optional convolution.

    :param channels: channels in the inputs and outputs.
    :param use_conv: a bool determining if a convolution is applied.
    :param dims: determines if the signal is 1D, 2D, or 3D. If 3D, then
                 upsampling occurs in the inner-two dimensions.
    r   Nc                    sJ   t    || _|p|| _|| _|| _|r#t|| j| jddd| _d S d S )Nr   r   )padding)super__init__r;   out_channelsuse_convr   r   convselfr;   rZ   r   rY   	__class__r   r   rX      s   

zUpsample.__init__c                 C   st   |j d | jks
J | jdkr(tj||j d |j d d |j d d fdd}ntj|ddd}| jr8| |}|S )Nr   r   r      nearest)mode)scale_factorrb   )r5   r;   r   FinterpolaterZ   r[   r]   xr   r   r   forward   s   
2
zUpsample.forwardr   N__name__
__module____qualname____doc__rX   rh   __classcell__r   r   r^   r   rU      s    		rU   c                       rT   )	UpsampleLearnableaX  
    Upsampling based on ConvTranspose2d. This is needed for bfloat support.

    :param channels: channels in the inputs and outputs.
    :param use_conv: a bool determining if a convolution is applied.
    :param dims: determines if the signal is 1D, 2D, or 3D. If 3D, then
                 upsampling occurs in the inner-two dimensions.
    r   Nc                    s|   t    || _|p|| _|| _|| _| jdkr&t| j| jddd| _d S | jdkr:tj	| j| jdddd| _d S t
d	)
Nr   r`   r   r   )r   r`   r`   r   r   r   )r   r   r   )kernel_sizestriderV   z%Upsampling support only for 2D and 3D)rW   rX   r;   rY   rZ   r   r	   ConvTranspose2dr[   ConvTranspose3dr   r\   r^   r   r   rX      s   



zUpsampleLearnable.__init__c                 C   s"   |j d | jks
J | |}|S Nr   )r5   r;   r[   rf   r   r   r   rh      s   
zUpsampleLearnable.forwardri   rj   r   r   r^   r   rp      s    	rp   c                       rT   )	
DownsampleaE  
    A downsampling layer with an optional convolution.

    :param channels: channels in the inputs and outputs.
    :param use_conv: a bool determining if a convolution is applied.
    :param dims: determines if the signal is 1D, 2D, or 3D. If 3D, then
                 downsampling occurs in the inner-two dimensions.
    r   Nc                    s|   t    || _|p|| _|| _|| _|dkrdnd}|r,t|| j| jd|dd| _d S | j| jks4J t|||d| _d S )Nr   r   rq   r   )rs   rV   )rr   rs   )	rW   rX   r;   rY   rZ   r   r   opr   )r]   r;   rZ   r   rY   rs   r^   r   r   rX      s   

zDownsample.__init__c                 C   s   |j d | jks
J | |S rv   )r5   r;   rx   rf   r   r   r   rh      s   
zDownsample.forwardri   rj   r   r   r^   r   rw      s    	rw   )r   )r8   )rn   rD   torchrB   torch.nnr	   torch.nn.functional
functionalrd   apex.contrib.group_normr   OPT_GROUP_NORM	Exceptionprintr   r   r   r'   r,   r.   r7   r<   rG   rS   ModulerU   rp   rw   r   r   r   r   <module>   s0   !
		

 