o
    
j                     @   sZ   d dl Z d dl mZ d dlmZ e jjdd ZG dd de jjZG dd	 d	ejZ	dS )
    N)nn)parametrizec                 C   s\   |d }| | }t |d d d |d d f }t |d d |d d d f }|| }|S )Nr   )torchtanhsigmoid)input_ainput_b
n_channelsn_channels_intin_actt_acts_actacts r   Q/home/kuhnn/.local/lib/python3.10/site-packages/TTS/tts/layers/generic/wavenet.pyfused_add_tanh_sigmoid_multiply   s     r   c                       s:   e Zd ZdZ			d fdd	ZdddZd	d
 Z  ZS )WNaP  Wavenet layers with weight norm and no input conditioning.

         |-----------------------------------------------------------------------------|
         |                                    |-> tanh    -|                           |
    res -|- conv1d(dilation) -> dropout -> + -|            * -> conv1d1x1 -> split -|- + -> res
    g -------------------------------------|  |-> sigmoid -|                        |
    o --------------------------------------------------------------------------- + --------- o

    Args:
        in_channels (int): number of input channels.
        hidden_channes (int): number of hidden channels.
        kernel_size (int): filter kernel size for the first conv layer.
        dilation_rate (int): dilations rate to increase dilation per layer.
            If it is 2, dilations are 1, 2, 4, 8 for the next 4 layers.
        num_layers (int): number of wavenet layers.
        c_in_channels (int): number of channels of conditioning input.
        dropout_p (float): dropout rate.
        weight_norm (bool): enable/disable weight norm for convolution layers.
    r   Tc	                    s  t    |d dksJ |d dksJ || _|| _|| _|| _|| _|| _|| _t	j
 | _t	j
 | _t
|| _|dkrWt	j
|d| | d}	t	j
jjj|	dd| _t|D ]d}
||
 }t|| | d }|
dkr}t	j
j|d| |||d}nt	j
j|d| |||d}t	j
jjj|dd}| j| |
|d k rd| }n|}t	j
||d}t	j
jjj|dd}| j| q[|s|   d S d S )N      r   weight)name)dilationpadding)super__init__in_channelshidden_channelskernel_sizedilation_rate
num_layersc_in_channels	dropout_pr   r   
ModuleList	in_layersres_skip_layersDropoutdropoutConv1dutilsparametrizationsweight_norm
cond_layerrangeintappendremove_weight_norm)selfr   r   r   r   r   r    r!   r*   r+   ir   r   in_layerres_skip_channelsres_skip_layer	__class__r   r   r   %   sJ   

zWN.__init__Nc                 K   s&  t |}t | jg}|d u rdn|}|d ur| |}t| jD ]l}| j| |}| |}|d urN|d | j }	|d d |	|	d| j  d d f }
nt |}
t	||
|}| j
| |}|| jd k r||d d d | jd d f  | }||d d | jd d d f  }q"|| }q"|| S )Ng      ?r   r   )r   
zeros_like	IntTensorr   r+   r,   r   r#   r&   r   r$   )r0   xx_maskgkwargsoutputn_channels_tensorr1   x_incond_offsetg_lr   res_skip_actsr   r   r   forward^   s&   


&
$"
z
WN.forwardc                 C   sL   | j dkrt| jd | jD ]}t|d q| jD ]}t|d qd S )Nr   r   )r    r   remove_parametrizationsr+   r#   r$   )r0   lr   r   r   r/   u   s   


zWN.remove_weight_normr   r   TNN)__name__
__module____qualname____doc__r   rC   r/   __classcell__r   r   r5   r   r      s    
9r   c                       s2   e Zd ZdZ			d	 fdd	Zd
ddZ  ZS )WNBlocksa  Wavenet blocks.

    Note: After each block dilation resets to 1 and it increases in each block
        along the dilation rate.

    Args:
        in_channels (int): number of input channels.
        hidden_channes (int): number of hidden channels.
        kernel_size (int): filter kernel size for the first conv layer.
        dilation_rate (int): dilations rate to increase dilation per layer.
            If it is 2, dilations are 1, 2, 4, 8 for the next 4 layers.
        num_blocks (int): number of wavenet blocks.
        num_layers (int): number of wavenet layers.
        c_in_channels (int): number of channels of conditioning input.
        dropout_p (float): dropout rate.
        weight_norm (bool): enable/disable weight norm for convolution layers.
    r   Tc
                    sV   t    t | _t|D ]}
t|
dkr|n||||||||	d}| j| qd S )Nr   )r   r   r   r   r   r    r!   r*   )r   r   r   r"   	wn_blocksr,   r   r.   )r0   r   r   r   r   
num_blocksr   r    r!   r*   idxlayerr5   r   r   r      s   


zWNBlocks.__init__Nc                 C   s    |}| j D ]}||||}q|S )N)rN   )r0   r9   r:   r;   orQ   r   r   r   rC      s   
zWNBlocks.forwardrF   rG   )rH   rI   rJ   rK   r   rC   rL   r   r   r5   r   rM   ~   s    rM   )
r   r   torch.nn.utilsr   jitscriptr   Moduler   rM   r   r   r   r   <module>   s    
	n