o
    Siw                     @   sl   d Z ddlmZmZ ddlZddlZddlZddlmZ ddl	m
Z
 eG dd dZG d	d
 d
ejZdS )z)Residual vector quantizer implementation.    )	dataclassfieldN)nn   )ResidualVectorQuantizationc                   @   sR   e Zd ZU ejed< ejed< ejed< dZejej ed< e	e
dZe
ed< dS )QuantizedResult	quantizedcodes	bandwidthNpenalty)default_factorymetrics)__name__
__module____qualname__torchTensor__annotations__r   tpOptionalr   dictr    r   r   K/home/ubuntu/.local/lib/python3.10/site-packages/encodec/quantization/vq.pyr      s   
 


r   c                       s   e Zd ZdZ							d"d	ed
edededededef fddZd#dej	dede
je defddZd#dede
je defddZdefddZd#dej	dede
je dej	fddZdej	dej	fd d!Z  ZS )$ResidualVectorQuantizera  Residual Vector Quantizer.
    Args:
        dimension (int): Dimension of the codebooks.
        n_q (int): Number of residual vector quantizers used.
        bins (int): Codebook size.
        decay (float): Decay for exponential moving average over the codebooks.
        kmeans_init (bool): Whether to use kmeans to initialize the codebooks.
        kmeans_iters (int): Number of iterations used for kmeans initialization.
        threshold_ema_dead_code (int): Threshold for dead code expiration. Replace any codes
            that have an exponential moving average cluster size less than the specified threshold with
            randomly selected vector from the current batch.
             Gz?T2      	dimensionn_qbinsdecaykmeans_initkmeans_itersthreshold_ema_dead_codec              	      s^   t    || _|| _|| _|| _|| _|| _|| _t	| j| j| j| j| j| j| jd| _
d S )N)dimcodebook_sizenum_quantizersr#   r$   r%   r&   )super__init__r!   r    r"   r#   r$   r%   r&   r   vq)selfr    r!   r"   r#   r$   r%   r&   	__class__r   r   r+   )   s"   

z ResidualVectorQuantizer.__init__Nxsample_rater
   returnc           
      C   sT   |  |}| ||}| j||d\}}}t|| |}	t|||	t|dS )a  Residual vector quantization on the given input tensor.
        Args:
            x (torch.Tensor): Input tensor.
            sample_rate (int): Sample rate of the input tensor.
            bandwidth (float): Target bandwidth.
        Returns:
            QuantizedResult:
                The quantized (or approximately quantized) representation with
                the associated bandwidth and any penalty term for the loss.
        r!   )r   )get_bandwidth_per_quantizer get_num_quantizers_for_bandwidthr,   r   tensortor   mean)
r-   r0   r1   r
   bw_per_qr!   r   r	   commit_lossbwr   r   r   forwardE   s
   
zResidualVectorQuantizer.forwardc                 C   s8   |  |}| j}|r|dkrttdt|| }|S )z8Return n_q based on specified target bandwidth.
        g        r   )r4   r!   intmaxmathfloor)r-   r1   r
   r9   r!   r   r   r   r5   V   s
   
z8ResidualVectorQuantizer.get_num_quantizers_for_bandwidthc                 C   s   t | j| d S )zFReturn bandwidth per quantizer for a given input sample rate.
        i  )r?   log2r"   )r-   r1   r   r   r   r4   _   s   z3ResidualVectorQuantizer.get_bandwidth_per_quantizerc                 C   s    |  ||}| jj||d}|S )zEncode a given input tensor with the specified sample rate at the given bandwidth.
        The RVQ encode method sets the appropriate number of quantizer to use
        and returns indices for each quantizer.
        r3   )r5   r,   encode)r-   r0   r1   r
   r!   r	   r   r   r   rB   d   s   zResidualVectorQuantizer.encoder	   c                 C   s   | j |}|S )z@Decode the given codes to the quantized representation.
        )r,   decode)r-   r	   r   r   r   r   rC   m   s   zResidualVectorQuantizer.decode)r   r   r   r   Tr   r   )N)r   r   r   __doc__r=   floatboolr+   r   r   r   r   r   r<   r5   r4   rB   rC   __classcell__r   r   r.   r   r      s:    $	&	r   )rD   dataclassesr   r   r?   typingr   r   r   core_vqr   r   Moduler   r   r   r   r   <module>   s   