o
    
j|                     @   sZ   d dl 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G dd	 d	ejZdS )
    N)nn)
functionalc                       2   e Zd ZdZd
 fdd	Zdd Zdd	 Z  ZS )Lineara  Linear layer with a specific initialization.

    Args:
        in_features (int): number of channels in the input tensor.
        out_features (int): number of channels in the output tensor.
        bias (bool, optional): enable/disable bias in the layer. Defaults to True.
        init_gain (str, optional): method to compute the gain in the weight initializtion based on the nonlinear activation used afterwards. Defaults to 'linear'.
    Tlinearc                    s,   t    tjj|||d| _| | d S )Nbias)super__init__torchr   r   linear_layer_init_wselfin_featuresout_featuresr   	init_gain	__class__ X/home/kuhnn/.local/lib/python3.10/site-packages/TTS/tts/layers/tacotron/common_layers.pyr
      s   
zLinear.__init__c                 C   $   t jjj| jjt jj|d d S N)gainr   r   initxavier_uniform_r   weightcalculate_gainr   r   r   r   r   r         $zLinear._init_wc                 C   s
   |  |S )N)r   )r   xr   r   r   forward   s   
zLinear.forwardTr   __name__
__module____qualname____doc__r
   r   r"   __classcell__r   r   r   r   r      s
    	r   c                       r   )LinearBNa  Linear layer with Batch Normalization.

    x -> linear -> BN -> o

    Args:
        in_features (int): number of channels in the input tensor.
        out_features (int ): number of channels in the output tensor.
        bias (bool, optional): enable/disable bias in the linear layer. Defaults to True.
        init_gain (str, optional): method to set the gain for weight initialization. Defaults to 'linear'.
    Tr   c                    s>   t    tjj|||d| _tj|ddd| _| | d S )Nr   g?gh㈵>)momentumeps)	r	   r
   r   r   r   r   BatchNorm1dbatch_normalizationr   r   r   r   r   r
   (   s   
zLinearBN.__init__c                 C   r   r   r   r   r   r   r   r   .   r    zLinearBN._init_wc                 C   sP   |  |}t|jdkr|ddd}| |}t|jdkr&|ddd}|S )z<
        Shapes:
            x: [T, B, C] or [B, C]
                 r   )r   lenshapepermuter.   )r   r!   outr   r   r   r"   1   s   

zLinearBN.forwardr#   r$   r   r   r   r   r*      s
    r*   c                       s8   e Zd ZdZdddddgdf fdd	Zdd	 Z  ZS )
Preneta  Tacotron specific Prenet with an optional Batch Normalization.

    Note:
        Prenet with BN improves the model performance significantly especially
    if it is enabled after learning a diagonal attention alignment with the original
    prenet. However, if the target dataset is high quality then it also works from
    the start. It is also suggested to disable dropout if BN is in use.

        prenet_type == "original"
            x -> [linear -> ReLU -> Dropout]xN -> o

        prenet_type == "bn"
            x -> [linear -> BN -> ReLU -> Dropout]xN -> o

    Args:
        in_features (int): number of channels in the input tensor and the inner layers.
        prenet_type (str, optional): prenet type "original" or "bn". Defaults to "original".
        prenet_dropout (bool, optional): dropout rate. Defaults to True.
        dropout_at_inference (bool, optional): use dropout at inference. It leads to a better quality for some models.
        out_features (list, optional): List of output channels for each prenet block.
            It also defines number of the prenet blocks based on the length of argument list.
            Defaults to [256, 256].
        bias (bool, optional): enable/disable bias in prenet linear layers. Defaults to True.
    originalTF   c                    s   t    || _|| _|| _|g|d d  }|dkr-t fddt||D | _d S |dkrCt fddt||D | _d S d S )Nbnc                       g | ]\}}t || d qS r   )r*   .0in_sizeout_sizer   r   r   
<listcomp>j       z#Prenet.__init__.<locals>.<listcomp>r7   c                    r;   r<   )r   r=   r   r   r   rA   n   rB   )	r	   r
   prenet_typeprenet_dropoutdropout_at_inferencer   
ModuleListziplinear_layers)r   r   rC   rD   rE   r   r   r   r   r   r
   Z   s   
	

zPrenet.__init__c                 C   sH   | j D ]}| jrtjt||d| jp| jd}qt||}q|S )Ng      ?)ptraining)rH   rD   FdropoutrelurJ   rE   )r   r!   r   r   r   r   r"   q   s
   
$zPrenet.forward)r%   r&   r'   r(   r
   r"   r)   r   r   r   r   r6   ?   s    r6   )	r   r   torch.nnr   rK   Moduler   r*   r6   r   r   r   r   <module>   s    #