o
    ̳i                     @   sF   d dl mZ d dlZd dlmZmZ d dlmZ G dd dejZ	dS )    )TupleN)nnTensor)
functionalc                       sh   e Zd ZdZdededdf fddZdedeeef fd	d
Zde	fddZ
dedefddZ  ZS )VectorQuantizedEmbeddingsa  
    Vector quantized embedding layer that takes in the output of an encoder
    and performs a nearest-neighbor lookup in the embedding space.
    Vector quantization was introduced in Oord et al. 2017 (https://arxiv.org/pdf/1711.00937.pdf)
    to generate high-fidelity images, videos, and audio data.

    This module currently does not support pre-training of the embeddings via EMA.

    Code was adapted from torchmultimodal's `Codebook module
    <https://github.com/facebookresearch/multimodal/blob/main/torchmultimodal/modules/layers/codebook.py>`_.

    Args:
        num_embeddings (int): Number of vectors in the embedding space.
        embedding_dim (int): Dimensionality of the embedding vectors.
    num_embeddingsembedding_dimreturnNc                    s.   t    tt||| _|| _|| _d S N)	super__init__r   	Parametertorchempty	embeddingr   r   )selfr   r   	__class__ S/home/ubuntu/.local/lib/python3.10/site-packages/torchtune/modules/vq_embeddings.pyr      s   

z"VectorQuantizedEmbeddings.__init__zc                 C   s   |j \}}}|| jkrtd| d| j |d|}tj|| jddd }tj|dd}| |}||| 	  }||||}	|||}
|	|
fS )	a  
        Args:
            z (Tensor): Tensor containing a batch of encoder outputs of shape ``(b, s, d)``, where
                b is batch size, s is sequence length or time, and d is ``embedding_dim``.

        Returns:
            Tuple[Tensor, Tensor]: The quantized input and the embedding vector ids that were used.

        Raises:
            ValueError: if input embedding dimension does not match embedding dimension of module
        z)Expected last dimension of input tensor (z) to be embedding size of g       @)p      )dim)
shaper   
ValueErrorviewr   cdistr   argmindecodedetach)r   r   bszseq_lenz_embed_dimz_flat	distancestoken_ids_flatquantized_flat	quantized	token_idsr   r   r   forward)   s   

z!VectorQuantizedEmbeddings.forwardc                 C   s   d | j| jS )Nz#num_embeddings={}, embedding_dim={})formatr   r   )r   r   r   r   
extra_reprO   s   z$VectorQuantizedEmbeddings.extra_reprr+   c                 C   s   t || jS r
   )Fr   )r   r+   r   r   r   r!   T   s   z VectorQuantizedEmbeddings.decode)__name__
__module____qualname____doc__intr   r   r   r,   strr.   r!   __classcell__r   r   r   r   r      s    
&r   )
typingr   r   r   r   torch.nnr   r/   Moduler   r   r   r   r   <module>   s
   