o
    Oôj8+  ã                	   @   s�  d dl Z d dlmZ d dlmZ d dlZd dlZd dlmZ d dl	m
Z
 d dlmZ ddlmZ d d	lmZ d d
lmZ d dlmZ d dlmZ dd„ ZG dd„ dejƒZG dd„ dejƒZG dd„ dejƒZG dd„ dejƒZG dd„ dejƒZG dd„ de
eƒZedk�rFd dlZd dlmZ eƒ  d¡Z e  !¡ D ]"\Z"Z#e# $¡ Z%e&dd„ e# '¡ D ƒƒZ(d d!„ Z)e*e#d"ee)e%e(d#�ƒ q›e+e ƒ e+d$e&d%d„ e  '¡ D ƒƒƒ d&Z,e -dde,¡ e j.¡Z/e/ 0d'¡ e/ 1¡  e e/ƒd( Z2e+d)e/j3ƒ e+d*e2j3ƒ e 4e2¡Z5de5dd…dd…e5j3d+ d, f< e2 6e5¡ e/j5 7d ¡Z8e8d k &d ¡Z8e8d k &¡ Z9e+d-e9 :¡ › �ƒ ee -ddd.¡d/ƒZ/e j;e j<e/d'd0�d'd0� dS dS )1é    N)ÚList)ÚUnion)ÚAudioSignal)Ú	BaseModel)Únné   )Ú
CodecMixin)ÚSnake1d)ÚWNConv1d)ÚWNConvTranspose1d)ÚResidualVectorQuantizec                 C   s6   t | tjƒrtjj| jdd� tj | jd¡ d S d S )Ng{®Gáz”?)Ústdr   )Ú
isinstancer   ÚConv1dÚinitÚtrunc_normal_ÚweightÚ	constant_Úbias)Úm© r   ú^/var/www/html/academy/tools/voices/clone/sv-venv/lib/python3.10/site-packages/dac/model/dac.pyÚinit_weights   s   þr   c                       ó0   e Zd Zd	dedef‡ fdd„Zdd„ Z‡  ZS )
ÚResidualUnité   r   ÚdimÚdilationc              
      sL   t ƒ  ¡  d| d }t t|ƒt||d||d�t|ƒt||dd�¡| _d S )Né   é   é   )Úkernel_sizer   Úpaddingr   )r!   )ÚsuperÚ__init__r   Ú
Sequentialr	   r
   Úblock)Úselfr   r   Úpad©Ú	__class__r   r   r$      s   

üzResidualUnit.__init__c                 C   sD   |   |¡}|jd |jd  d }|dkr|d|| …f }|| S )Néÿÿÿÿr   r   .)r&   Úshape)r'   ÚxÚyr(   r   r   r   Úforward#   s
   
zResidualUnit.forward©r   r   ©Ú__name__Ú
__module__Ú__qualname__Úintr$   r/   Ú__classcell__r   r   r)   r   r      s    
r   c                       r   )
ÚEncoderBlockr   r   r   Ústridec                    sn   t ƒ  ¡  t t|d dd�t|d dd�t|d dd�t|d ƒt|d |d| |t |d ¡d�¡| _	d S )Nr   r   ©r   é   é	   ©r!   r8   r"   )
r#   r$   r   r%   r   r	   r
   ÚmathÚceilr&   )r'   r   r8   r)   r   r   r$   ,   s   

û
ûzEncoderBlock.__init__c                 C   ó
   |   |¡S ©N©r&   ©r'   r-   r   r   r   r/   <   ó   
zEncoderBlock.forwardr0   r1   r   r   r)   r   r7   +   s    r7   c                       s>   e Zd Zdg d¢dfdededef‡ fdd„Zdd	„ Z‡  ZS )
ÚEncoderé@   ©r   é   é   rH   Úd_modelÚstridesÚd_latentc              	      s‚   t ƒ  ¡  td|ddd�g| _|D ]}|d9 }|  jt||d�g7  _q|  jt|ƒt||ddd�g7  _tj| jŽ | _|| _d S )Nr   r    r:   ©r!   r"   r   )r8   )	r#   r$   r
   r&   r7   r	   r   r%   Zenc_dim)r'   rI   rJ   rK   r8   r)   r   r   r$   A   s   
þ
zEncoder.__init__c                 C   r?   r@   rA   rB   r   r   r   r/   Z   rC   zEncoder.forward)r2   r3   r4   r5   Úlistr$   r/   r6   r   r   r)   r   rD   @   s    üþýürD   c                       s4   e Zd Zddededef‡ fdd„Zd	d
„ Z‡  ZS )ÚDecoderBlockr   rH   r   Ú	input_dimÚ
output_dimr8   c                    sZ   t ƒ  ¡  t t|ƒt||d| |t |d ¡d�t|dd�t|dd�t|dd�¡| _	d S )Nr   r<   r   r9   r:   r;   )
r#   r$   r   r%   r	   r   r=   r>   r   r&   )r'   rO   rP   r8   r)   r   r   r$   _   s   
û



õzDecoderBlock.__init__c                 C   r?   r@   rA   rB   r   r   r   r/   o   rC   zDecoderBlock.forward)r   rH   r   r1   r   r   r)   r   rN   ^   s    rN   c                       s.   e Zd Z	ddef‡ fdd„Zdd„ Z‡  ZS )ÚDecoderr   Úd_outc           
         sŽ   t ƒ  ¡  t||ddd�g}t|ƒD ]\}}|d|  }|d|d   }	|t||	|ƒg7 }q|t|	ƒt|	|ddd�t ¡ g7 }tj|Ž | _	d S )Nr    r:   rL   r   r   )
r#   r$   r
   Ú	enumeraterN   r	   r   ÚTanhr%   Úmodel)
r'   Zinput_channelÚchannelsZratesrR   ÚlayersÚir8   rO   rP   r)   r   r   r$   t   s   
ýzDecoder.__init__c                 C   r?   r@   )rU   rB   r   r   r   r/   �   rC   zDecoder.forward)r   r1   r   r   r)   r   rQ   s   s    ûûrQ   c                       sÂ   e Zd Zdg d¢ddg d¢dddd	d
f
dedee dededee dededeeef dedef‡ fdd„Zdd„ Z		d"de
jdefdd„Zde
jfdd„Z		d#de
jdedefd d!„Z‡  ZS )$ÚDACrE   rF   Ni   )rH   rH   rG   r   r;   i   rH   FéD¬  Úencoder_dimÚencoder_ratesÚ
latent_dimÚdecoder_dimÚdecoder_ratesÚn_codebooksÚcodebook_sizeÚcodebook_dimÚquantizer_dropoutÚsample_ratec                    s²   t ƒ  ¡  || _|| _|| _|| _|
| _|d u r |dt|ƒ  }|| _t	 
|¡| _t|||ƒ| _|| _|| _|| _t|||||	d�| _t|||ƒ| _|
| _|  t¡ |  ¡ | _d S )Nr   )rO   r`   ra   rb   rc   )r#   r$   r[   r\   r^   r_   rd   Úlenr]   ÚnpÚprodÚ
hop_lengthrD   Úencoderr`   ra   rb   r   Ú	quantizerrQ   ÚdecoderÚapplyr   Ú	get_delayÚdelay)r'   r[   r\   r]   r^   r_   r`   ra   rb   rc   rd   r)   r   r   r$   ”   s:   
ûý
zDAC.__init__c                 C   sV   |d u r| j }|| j ksJ ‚|jd }t || j ¡| j | }tj |d|f¡}|S )Nr+   r   )rd   r,   r=   r>   rh   r   Ú
functionalr(   )r'   Ú
audio_datard   ÚlengthÚ	right_padr   r   r   Ú
preprocessÆ   s   
zDAC.preprocessrp   Ún_quantizersc                 C   s.   |   |¡}|  ||¡\}}}}}|||||fS )ae  Encode given audio data and return quantized latent codes

        Parameters
        ----------
        audio_data : Tensor[B x 1 x T]
            Audio data to encode
        n_quantizers : int, optional
            Number of quantizers to use, by default None
            If None, all quantizers are 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
            "length" : int
                Number of samples in input audio
        )ri   rj   )r'   rp   rt   ÚzÚcodesÚlatentsÚcommitment_lossÚcodebook_lossr   r   r   ÚencodeÑ   s
   
"ÿz
DAC.encoderu   c                 C   r?   )aÅ  Decode given latent codes and return audio data

        Parameters
        ----------
        z : Tensor[B x D x T]
            Quantized continuous representation of input
        length : int, optional
            Number of samples in output audio, by default None

        Returns
        -------
        dict
            A dictionary with the following keys:
            "audio" : Tensor[B x 1 x length]
                Decoded audio data.
        )rk   )r'   ru   r   r   r   Údecodeù   s   
z
DAC.decodec                 C   sT   |j d }|  ||¡}|  ||¡\}}}}}	|  |¡}
|
dd|…f |||||	dœS )a%  Model forward pass

        Parameters
        ----------
        audio_data : Tensor[B x 1 x T]
            Audio data to encode
        sample_rate : int, optional
            Sample rate of audio data in Hz, by default None
            If None, defaults to `self.sample_rate`
        n_quantizers : int, optional
            Number of quantizers to use, by default None.
            If None, all quantizers are 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
            "length" : int
                Number of samples in input audio
            "audio" : Tensor[B x 1 x length]
                Decoded audio data.
        r+   .N)Úaudioru   rv   rw   zvq/commitment_losszvq/codebook_loss)r,   rs   rz   r{   )r'   rp   rd   rt   rq   ru   rv   rw   rx   ry   r-   r   r   r   r/     s   
(ÿ
úzDAC.forwardr@   )NN)r2   r3   r4   r5   r   r   rM   Úboolr$   rs   ÚtorchÚTensorrz   r{   r/   r6   r   r   r)   r   rY   “   sb    õþýüûúùø
	÷
öõ2ýþ
ý(üþýürY   Ú__main__)ÚpartialÚcpuc                 C   ó   g | ]	}t  | ¡ ¡‘qS r   ©rf   rg   Úsize©Ú.0Úpr   r   r   Ú
<listcomp>M  ó    r‰   c                 C   s   | d|d d›d� S )Nú g    €„.Az<.3fz	M params.r   ©Úorˆ   r   r   r   Ú<lambda>N  s    rŽ   Ú
extra_reprrŒ   zTotal # of params: c                 C   rƒ   r   r„   r†   r   r   r   r‰   Q  rŠ   i± Tr|   zInput shape:zOutput shape:r+   r   zReceptive field: ið_( rZ   )Úverbose)=r=   Útypingr   r   Únumpyrf   r~   Ú
audiotoolsr   Zaudiotools.mlr   r   Úbaser   Zdac.nn.layersr	   r
   r   Údac.nn.quantizer   r   ÚModuler   r7   rD   rN   rQ   rY   r2   Ú	functoolsr�   ÚtorU   Únamed_modulesÚnr   r�   r�   ÚsumÚ
parametersrˆ   ÚfnÚsetattrÚprintrq   ÚrandnÚdevicer-   Úrequires_grad_Úretain_gradÚoutr,   Ú
zeros_likeÚgradÚbackwardÚsqueezeZgradmapÚrfÚitemÚ
decompressÚcompressr   r   r   r   Ú<module>   sb      
3

 
Ù