o
    Si"/                     @   s   d Z ddlZddlZddlmZ ddlmZm	Z	m
Z
 G dd dejZG dd dejZG d	d
 d
ejZdd ZedkrBe  dS dS )z8Encodec SEANet-based encoder and decoder implementation.    N   )SConv1dSConvTranspose1dSLSTMc                       s   e Zd ZdZddgddgdddidi dd	d
df
dedeje deje dedededej	eej
f dedededef fddZdd Z  ZS )SEANetResnetBlockaH  Residual block from SEANet model.
    Args:
        dim (int): Dimension of the input/output
        kernel_sizes (list): List of kernel sizes for the convolutions.
        dilations (list): List of dilations for the convolutions.
        activation (str): Activation function.
        activation_params (dict): Parameters to provide to the activation function
        norm (str): Normalization method.
        norm_params (dict): Parameters to provide to the underlying normalization used along with the convolution.
        causal (bool): Whether to use fully causal convolution.
        pad_mode (str): Padding mode for the convolutions.
        compress (int): Reduced dimensionality in residual branches (from Demucs v3)
        true_skip (bool): Whether to use true skip connection or a simple convolution as the skip connection.
       r   ELUalpha      ?weight_normFreflect   Tdimkernel_sizes	dilations
activationactivation_paramsnormnorm_paramscausalpad_modecompress	true_skipc                    s   t    t|t|ksJ dtt|}||
 }g }tt||D ]/\}\}}|dkr/|n|}|t|d kr;|n|}||di |t||||||||	dg7 }q#tj| | _	|  |rdt
 | _d S t||d||||	d| _d S )Nz7Number of kernel sizes should match number of dilationsr   r   )kernel_sizedilationr   norm_kwargsr   r   )r   r   r   r   r    )super__init__lengetattrnn	enumeratezipr   
SequentialblockIdentityshortcut)selfr   r   r   r   r   r   r   r   r   r   r   acthiddenr%   ir   r   in_chsout_chs	__class__r   J/home/ubuntu/.local/lib/python3.10/site-packages/encodec/modules/seanet.pyr   $   s,   


zSEANetResnetBlock.__init__c                 C   s   |  || | S N)r'   r%   r(   xr   r   r0   forward>   s   zSEANetResnetBlock.forward__name__
__module____qualname____doc__inttpListstrdictDictAnyboolr   r4   __classcell__r   r   r.   r0   r      s0    r   c                %       s   e Zd ZdZddddg ddddid	i d
d
dddddddfdededededeje dedededej	eej
f dedededededededed ef$ fd!d"Zd#d$ Z  ZS )%SEANetEncodera  SEANet encoder.
    Args:
        channels (int): Audio channels.
        dimension (int): Intermediate representation dimension.
        n_filters (int): Base width for the model.
        n_residual_layers (int): nb of residual layers.
        ratios (Sequence[int]): kernel size and stride ratios. The encoder uses downsampling ratios instead of
            upsampling ratios, hence it will use the ratios in the reverse order to the ones specified here
            that must match the decoder order
        activation (str): Activation function.
        activation_params (dict): Parameters to provide to the activation function
        norm (str): Normalization method.
        norm_params (dict): Parameters to provide to the underlying normalization used along with the convolution.
        kernel_size (int): Kernel size for the initial convolution.
        last_kernel_size (int): Kernel size for the initial convolution.
        residual_kernel_size (int): Kernel size for the residual layers.
        dilation_base (int): How much to increase the dilation with each layer.
        causal (bool): Whether to use fully causal convolution.
        pad_mode (str): Padding mode for the convolutions.
        true_skip (bool): Whether to use true skip connection or a simple
            (streamable) convolution as the skip connection in the residual network blocks.
        compress (int): Reduced dimensionality in residual branches (from Demucs v3).
        lstm (int): Number of LSTM layers at the end of the encoder.
    r                   r   r   r	   r
   r      r   r   Fr   channels	dimension	n_filtersn_residual_layersratiosr   r   r   r   r   last_kernel_sizeresidual_kernel_sizedilation_baser   r   r   r   lstmc                    sX  t    || _|| _|| _tt|| _~|| _t	
| j| _tt|}d}t||| |
||	||dg}t| jD ]F\}}t|D ]}|t|| |dg|| dg||	||||||dg7 }qB||di |t|| || d |d |||	||dg7 }|d9 }q:|r|t|| |dg7 }||di |t|| ||||	||dg7 }tj| | _d S )Nr   r   r   r   r   )
r   r   r   r   r   r   r   r   r   r   r   )r   strider   r   r   r   
num_layersr   )r   r   rK   rL   rM   listreversedrO   rN   npprod
hop_lengthr    r!   r   r"   ranger   r   r$   model)r(   rK   rL   rM   rN   rO   r   r   r   r   r   rP   rQ   rR   r   r   r   r   rS   r)   multr^   r+   ratiojr.   r   r0   r   [   sT   



zSEANetEncoder.__init__c                 C   s
   |  |S r1   r^   r2   r   r   r0   r4      s   
zSEANetEncoder.forwardr5   r   r   r.   r0   rC   B   sJ    4rC   c                +       s   e Zd ZdZddddg ddddid	d	d
i ddddddddddfdededededeje dededej	e dej	e dedej
eejf dededededed ed!ed"ed#ed$ef* fd%d&Zd'd( Z  ZS ))SEANetDecodera  SEANet decoder.
    Args:
        channels (int): Audio channels.
        dimension (int): Intermediate representation dimension.
        n_filters (int): Base width for the model.
        n_residual_layers (int): nb of residual layers.
        ratios (Sequence[int]): kernel size and stride ratios
        activation (str): Activation function.
        activation_params (dict): Parameters to provide to the activation function
        final_activation (str): Final activation function after all convolutions.
        final_activation_params (dict): Parameters to provide to the activation function
        norm (str): Normalization method.
        norm_params (dict): Parameters to provide to the underlying normalization used along with the convolution.
        kernel_size (int): Kernel size for the initial convolution.
        last_kernel_size (int): Kernel size for the initial convolution.
        residual_kernel_size (int): Kernel size for the residual layers.
        dilation_base (int): How much to increase the dilation with each layer.
        causal (bool): Whether to use fully causal convolution.
        pad_mode (str): Padding mode for the convolutions.
        true_skip (bool): Whether to use true skip connection or a simple
            (streamable) convolution as the skip connection in the residual network blocks.
        compress (int): Reduced dimensionality in residual branches (from Demucs v3).
        lstm (int): Number of LSTM layers at the end of the encoder.
        trim_right_ratio (float): Ratio for trimming at the right of the transposed convolution under the causal setup.
            If equal to 1.0, it means that all the trimming is done at the right.
    r   rD   rE   rF   r   r	   r
   Nr   rJ   r   r   Fr   rK   rL   rM   rN   rO   r   r   final_activationfinal_activation_paramsr   r   r   rP   rQ   rR   r   r   r   r   rS   trim_right_ratioc                    s  t    || _|| _|| _|| _~|| _t| j| _	t
t|}tdt| j }t||| ||
|||dg}|rE|t|| |dg7 }t| jD ]H\}}||di |t|| || d |d ||
|||dg7 }t|D ]}|t|| d |dg|| dg|||
|||||dg7 }qo|d }qJ||di |t||||
|||dg7 }|d urt
t|}|	pi }	||di |	g7 }tj| | _d S )Nr   rT   rV   )r   rU   r   r   r   rf   r   )
r   r   r   r   r   r   r   r   r   r   r   )r   r   rL   rK   rM   rO   rN   rZ   r[   r\   r    r!   r:   r   r   r   r"   r   r]   r   r$   r^   )r(   rK   rL   rM   rN   rO   r   r   rd   re   r   r   r   rP   rQ   rR   r   r   r   r   rS   rf   r)   r_   r^   r+   r`   ra   	final_actr.   r   r0   r      s`   




zSEANetDecoder.__init__c                 C   s   |  |}|S r1   rb   )r(   zyr   r   r0   r4      s   
zSEANetDecoder.forward)r6   r7   r8   r9   r:   r;   r<   r=   r>   Optionalr?   r@   rA   floatr   r4   rB   r   r   r.   r0   rc      sZ    >rc   c                  C   sn   dd l } t }t }| ddd}||}t|jg dks#J |j||}|j|jks5J |j|jfd S )Nr   r   i]  )r   rD   K   )torchrC   rc   randnrX   shape)rm   encoderdecoderr3   rh   ri   r   r   r0   test   s    rr   __main__)r9   typingr;   numpyrZ   torch.nnr!    r   r   r   Moduler   rC   rc   rr   r6   r   r   r   r0   <module>   s   -Q^
