o
    }oiK
                     @   sP   d dl Z d dlZd dlmZ d dlmZ G dd dejZG dd dejZdS )    N)	rearrangec                       s*   e Zd ZdZ	  fddZdd Z  ZS )LearnedSinusoidalPosEmbz> following @crowsonkb 's lead with learned sinusoidal pos emb c                    s8   t    |d dksJ |d }tt|| _d S )N   r   )super__init__nn	Parametertorchrandnweights)selfdimhalf_dim	__class__ t/home/ubuntu/.local/lib/python3.10/site-packages/nemo/collections/multimodal/modules/imagen/diffusionmodules/embs.pyr      s   
z LearnedSinusoidalPosEmb.__init__c                 C   sT   t |d}|t | jd d tj }tj| | fdd}tj||fdd}|S )Nzb -> b 1zd -> 1 dr   r   )r   r   mathpir	   catsincos)r   xfreqs	fourieredr   r   r   forward    s
   
zLearnedSinusoidalPosEmb.forward)__name__
__module____qualname____doc__r   r   __classcell__r   r   r   r   r      s
    r   c                       s&   e Zd Zd fdd	Zdd Z  ZS )UnLearnedSinusoidalPosEmb'  c                    s.   t    || _|| _td| d|  dS )as  
        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.
        z+Unlearned Timestep Embedding Schedule: dim=z, max_period=N)r   r   r   
max_periodprint)r   r   r%   r   r   r   r   )   s   

z"UnLearnedSinusoidalPosEmb.__init__c           	      C   s   | j }|d }| j}|j}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 ratj|t|d d d df gdd}|S )	Nr   r   )startenddtype)device)r)   r   r      )r   r%   r)   r	   expr   logarangefloat32tor*   floatr   r   r   
zeros_like)	r   	timestepsr   halfr%   r)   r   args	embeddingr   r   r   r   8   s   ((z!UnLearnedSinusoidalPosEmb.forward)r$   )r   r   r    r   r   r"   r   r   r   r   r#   (   s    r#   )	r   r	   torch.nnr   einopsr   Moduler   r#   r   r   r   r   <module>   s   