o
    Oôjd#  ã                   @   sÀ   d dl mZ d dlZd dlZd dlmZ d dlm  mZ	 d dl
m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ed
kr^edd�Ze ddd¡ZeeƒZeed jƒ dS dS )é    )ÚUnionN)Ú	rearrange)Úweight_norm)ÚWNConv1dc                       sN   e Zd ZdZdededef‡ fdd„Zdd„ Zd	d
„ Zdd„ Zdd„ Z	‡  Z
S )ÚVectorQuantizea×  
    Implementation of VQ similar to Karpathy's repo:
    https://github.com/karpathy/deep-vector-quantization
    Additionally uses following tricks from Improved VQGAN
    (https://arxiv.org/pdf/2110.04627.pdf):
        1. Factorized codes: Perform nearest neighbor lookup in low-dimensional space
            for improved codebook usage
        2. l2-normalized codes: Converts euclidean distance to cosine similarity which
            improves training stability
    Ú	input_dimÚcodebook_sizeÚcodebook_dimc                    sH   t ƒ  ¡  || _|| _t||dd�| _t||dd�| _t ||¡| _	d S )Né   )Úkernel_size)
ÚsuperÚ__init__r   r	   r   Úin_projÚout_projÚnnÚ	EmbeddingÚcodebook)Úselfr   r   r	   ©Ú	__class__© ú`/var/www/html/academy/tools/voices/clone/sv-venv/lib/python3.10/site-packages/dac/nn/quantize.pyr      s   
zVectorQuantize.__init__c                 C   s|   |   |¡}|  |¡\}}tj|| ¡ dd� ddg¡}tj|| ¡ dd� ddg¡}|||  ¡  }|  |¡}|||||fS )a·  Quantized the input tensor using a fixed codebook and returns
        the corresponding codebook vectors

        Parameters
        ----------
        z : Tensor[B x D x T]

        Returns
        -------
        Tensor[B x D x T]
            Quantized continuous representation of input
        Tensor[1]
            Commitment loss to train encoder to predict vectors closer to codebook
            entries
        Tensor[1]
            Codebook loss to update the codebook
        Tensor[B x T]
            Codebook indices (quantized discrete representation of input)
        Tensor[B x D x T]
            Projected latents (continuous representation of input before quantization)
        Únone)Ú	reductionr
   é   )r   Údecode_latentsÚFÚmse_lossÚdetachÚmeanr   )r   ÚzZz_eÚz_qÚindicesÚcommitment_lossÚcodebook_lossr   r   r   Úforward"   s   
ÿ
zVectorQuantize.forwardc                 C   s   t  || jj¡S ©N)r   Ú	embeddingr   Úweight©r   Zembed_idr   r   r   Ú
embed_codeH   s   zVectorQuantize.embed_codec                 C   s   |   |¡ dd¡S )Nr
   r   )r*   Ú	transposer)   r   r   r   Údecode_codeK   s   zVectorQuantize.decode_codec                 C   s”   t |dƒ}| jj}t |¡}t |¡}| d¡jddd�d| | ¡   | d¡jddd� ¡  }t |  d¡d d| 	d¡d�}|  
|¡}||fS )	Nzb d t -> (b t) dr   r
   T)Úkeepdimz(b t) -> b tr   )Úb)r   r   r(   r   Ú	normalizeÚpowÚsumÚtÚmaxÚsizer,   )r   ÚlatentsÚ	encodingsr   Údistr"   r!   r   r   r   r   N   s   


ÿþÿ 
zVectorQuantize.decode_latents)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úintr   r%   r*   r,   r   Ú__classcell__r   r   r   r   r      s    	&r   c                       sz   e Zd ZdZ					ddeded	ed
eeef def
‡ fdd„Zddefdd„Z	de
jfdd„Zde
jfdd„Z‡  ZS )ÚResidualVectorQuantizezg
    Introduced in SoundStream: An end2end neural audio codec
    https://arxiv.org/abs/2107.03312
    é   é	   é   é   ç        r   Ún_codebooksr   r	   Úquantizer_dropoutc                    sh   t ƒ  ¡  tˆ tƒr‡ fdd„t|ƒD ƒ‰ || _ˆ | _ˆ| _t 	‡ ‡‡fdd„t|ƒD ƒ¡| _
|| _d S )Nc                    s   g | ]}ˆ ‘qS r   r   )Ú.0Ú_©r	   r   r   Ú
<listcomp>q   s    z3ResidualVectorQuantize.__init__.<locals>.<listcomp>c                    s   g | ]
}t ˆˆˆ | ƒ‘qS r   )r   )rF   Úi©r	   r   r   r   r   rI   x   s    ÿÿ)r   r   Ú
isinstancer<   ÚrangerD   r	   r   r   Ú
ModuleListÚ
quantizersrE   )r   r   rD   r   r	   rE   r   rK   r   r   g   s   

þÿ
zResidualVectorQuantize.__init__NÚn_quantizersc                 C   sr  d}|}d}d}g }g }|du r| j }| jrLt |jd f¡| j  d }t d| j d |jd f¡}	t|jd | j ƒ}
|	d|
… |d|
…< | |j	¡}t
| jƒD ]R\}}| jdu r`||kr` nD||ƒ\}}}}}tj|jd f||j	d�|k }|||dd…ddf   }|| }|||  ¡ 7 }|||  ¡ 7 }| |¡ | |¡ qQtj|dd�}tj|dd�}|||||fS )aÑ  Quantized the input tensor using a fixed set of `n` codebooks and returns
        the corresponding codebook vectors
        Parameters
        ----------
        z : Tensor[B x D x T]
        n_quantizers : int, optional
            No. of quantizers to use
            (n_quantizers < self.n_codebooks ex: for quantizer dropout)
            Note: if `self.quantizer_dropout` is True, this argument is ignored
                when in training mode, and a random number of quantizers is used.
        Returns
        -------
        dict
            A dictionary with the following keys:

            "z" : Tensor[B x D x T]
                Quantized continuous representation of input
            "codes" : Tensor[B x N x T]
                Codebook indices for each codebook
                (quantized discrete representation of input)
            "latents" : Tensor[B x N*D x T]
                Projected latents (continuous representation of input before quantization)
            "vq/commitment_loss" : Tensor[1]
                Commitment loss to train encoder to predict vectors closer to codebook
                entries
            "vq/codebook_loss" : Tensor[1]
                Codebook loss to update the codebook
        r   Nr
   F)Ú
fill_valueÚdevice©Údim)rD   ÚtrainingÚtorchÚonesÚshapeÚrandintr<   rE   ÚtorR   Ú	enumeraterO   Úfullr   ÚappendÚstackÚcat)r   r    rP   r!   Úresidualr#   r$   Zcodebook_indicesr5   ÚdropoutÚ	n_dropoutrJ   Z	quantizerÚz_q_iZcommitment_loss_iZcodebook_loss_iZ	indices_iZz_e_iÚmaskÚcodesr   r   r   r%      s>   ÿÿ
zResidualVectorQuantize.forwardre   c                 C   sx   d}g }|j d }t|ƒD ]$}| j|  |dd…|dd…f ¡}| |¡ | j|  |¡}|| }q|tj|dd�|fS )a?  Given the quantized codes, reconstruct the continuous representation
        Parameters
        ----------
        codes : Tensor[B x N x T]
            Quantized discrete representation of input
        Returns
        -------
        Tensor[B x D x T]
            Quantized continuous representation of input
        rC   r
   NrS   )rX   rM   rO   r,   r]   r   rV   r_   )r   re   r!   Úz_prD   rJ   Úz_p_irc   r   r   r   Ú
from_codesÈ   s   
"

z!ResidualVectorQuantize.from_codesr5   c                 C   sæ   d}g }g }t  dgdd„ | jD ƒ ¡}t  ||jd k¡d jddd�d }t|ƒD ]8}|| ||d  }}	| j|  |dd…||	…dd…f ¡\}
}| |
¡ | |¡ | j|  	|
¡}|| }q+|t
j|dd�t
j|dd�fS )	a¿  Given the unquantized latents, reconstruct the
        continuous representation after quantization.

        Parameters
        ----------
        latents : Tensor[B x N x T]
            Continuous representation of input after projection

        Returns
        -------
        Tensor[B x D x T]
            Quantized representation of full-projected space
        Tensor[B x D x T]
            Quantized representation of latent space
        r   c                 S   s   g | ]}|j ‘qS r   rH   )rF   Úqr   r   r   rI   ñ   s    z7ResidualVectorQuantize.from_latents.<locals>.<listcomp>r
   T)ÚaxisÚkeepdimsNrS   )ÚnpÚcumsumrO   ÚwhererX   r3   rM   r   r]   r   rV   r_   r^   )r   r5   r!   rf   re   ÚdimsrD   rJ   ÚjÚkrg   Zcodes_irc   r   r   r   Úfrom_latentsÞ   s    ÿ*


z#ResidualVectorQuantize.from_latents)r?   r@   rA   rB   rC   r&   )r8   r9   r:   r;   r<   r   ÚlistÚfloatr   r%   rV   ÚTensorrh   rr   r=   r   r   r   r   r>   a   s*    úþýü
ûúIr>   Ú__main__T)rE   é   r?   éP   r5   )Útypingr   Únumpyrl   rV   Útorch.nnr   Ztorch.nn.functionalÚ
functionalr   Úeinopsr   Ztorch.nn.utilsr   Zdac.nn.layersr   ÚModuler   r>   r8   ZrvqÚrandnÚxÚyÚprintrX   r   r   r   r   Ú<module>   s"    T "
ü