o
    
j                     @   sb   d dl mZ d dlZd dlZd dlm  mZ d dl	m
Z
 d dlmZ dZG dd dejjZdS )    )ListN)parametrize)LVCBlockg?c                       s   e Zd Z	ddededededee deded	ed
edef fddZdd Zdd Zdd Z	e
dd fddZedd Ze dd Z  ZS )UnivnetGeneratorTin_channelsout_channelshidden_channelscond_channelsupsample_factorslvc_layers_each_blocklvc_kernel_sizekpnet_hidden_channelskpnet_conv_sizedropoutc                    s   t    || _|| _|| _t|| _t|| _	t
jj||ddddd| _t
j | _d}t| j	D ]}|||  }t|||| |||||	|
d	}|  j|g7  _q3t
jt
jj||dddddg| _|rm|   dS dS )a  Univnet Generator network.

        Paper: https://arxiv.org/pdf/2106.07889.pdf

        Args:
            in_channels (int): Number of input tensor channels.
            out_channels (int): Number of channels of the output tensor.
            hidden_channels (int): Number of hidden network channels.
            cond_channels (int): Number of channels of the conditioning tensors.
            upsample_factors (List[int]): List of uplsample factors for the upsampling layers.
            lvc_layers_each_block (int): Number of LVC layers in each block.
            lvc_kernel_size (int): Kernel size of the LVC layers.
            kpnet_hidden_channels (int): Number of hidden channels in the key-point network.
            kpnet_conv_size (int): Number of convolution channels in the key-point network.
            dropout (float): Dropout rate.
            use_weight_norm (bool, optional): Enable/disable weight norm. Defaults to True.
                 T)kernel_sizepaddingdilationbias)	r   r	   upsample_ratioconv_layersconv_kernel_sizecond_hop_lengthr   r   kpnet_dropoutN)super__init__r   r   r	   npprodupsample_scalelenlvc_block_numstorchnnConv1d
first_conv
ModuleList
lvc_blocksranger   last_conv_layersapply_weight_norm)selfr   r   r   r	   r
   r   r   r   r   r   use_weight_normr   nlvcb	__class__ W/home/kuhnn/.local/lib/python3.10/site-packages/TTS/vocoder/models/univnet_generator.pyr      sD   
 
	zUnivnetGenerator.__init__c                 C   s   t |jd | j|jd g}|| jjj}| |}t| j	D ]
}| j
| ||}q!| jD ]}t|t}||}q/t |}|S )zCalculate forward propagation.
        Args:
            c (Tensor): Local conditioning auxiliary features (B, C ,T').
        Returns:
            Tensor: Output tensor (B, out_channels, T)
        r      )r#   randnshaper   tor&   r   devicer)   r"   r(   r*   F
leaky_reluLRELU_SLOPEtanh)r,   cxr.   fr2   r2   r3   forwardY   s   



zUnivnetGenerator.forwardc                 C      dd }|  | dS )z:Remove weight normalization module from all of the layers.c                 S   s(   z	t | d W d S  ty   Y d S w )Nweight)r   remove_parametrizations
ValueErrormr2   r2   r3   _remove_weight_normr   s
   z@UnivnetGenerator.remove_weight_norm.<locals>._remove_weight_normNapply)r,   rG   r2   r2   r3   remove_weight_normo   s   z#UnivnetGenerator.remove_weight_normc                 C   rA   )z9Apply weight normalization module from all of the layers.c                 S   s.   t | tjjtjjfrtjjj|  d S d S )N)
isinstancer#   r$   r%   Conv2dutilsparametrizationsweight_normrE   r2   r2   r3   _apply_weight_norm~   s   z>UnivnetGenerator.apply_weight_norm.<locals>._apply_weight_normNrH   )r,   rP   r2   r2   r3   r+   {   s   z"UnivnetGenerator.apply_weight_normc                 C   s   d|  S )Nr4   r2   )r>   r2   r2   r3   <lambda>   s    zUnivnetGenerator.<lambda>c                    sD   | | dksJ | |  fddt | D }|d t| d S )Nr   c                    s   g | ]} | qS r2   r2   ).0ir   layers_per_cycler2   r3   
<listcomp>   s    z>UnivnetGenerator._get_receptive_field_size.<locals>.<listcomp>r   )r)   sum)layersstacksr   r   	dilationsr2   rT   r3   _get_receptive_field_size   s   z*UnivnetGenerator._get_receptive_field_sizec                 C   s   |  | j| j| jS )zReturn receptive field size.)r[   rX   rY   r   )r,   r2   r2   r3   receptive_field_size   s   z%UnivnetGenerator.receptive_field_sizec                 C   sJ   t |jd | j|jd g}|| jjj}|t| 	 }| 
|S )zPerform inference.
        Args:
            c (Tensor): Local conditioning auxiliary features :math:`(B, C, T)`.
        Returns:
            Tensor: Output tensor (T, out_channels)
        r   r4   )r#   r5   r6   r   r7   r&   r   r8   next
parametersr@   )r,   r=   r>   r2   r2   r3   	inference   s   
zUnivnetGenerator.inference)T)__name__
__module____qualname__intr   floatr   r@   rJ   r+   staticmethodr[   propertyr\   r#   no_gradr_   __classcell__r2   r2   r0   r3   r      s@    	
K

r   )typingr   numpyr   r#   torch.nn.functionalr$   
functionalr9   torch.nn.utilsr   TTS.vocoder.layers.lvc_blockr   r;   Moduler   r2   r2   r2   r3   <module>   s    