o
    ´‹
j¨   ã                   @   s~   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
 G dd	„ d	ejƒZG d
d„ dejƒZG dd„ dejƒZdS )é    N)ÚVersion)Únn)Ú
functional)ÚWNé   )Ú	LayerNormc                       s(   e Zd ZdZ‡ fdd„Zdd„ Z‡  ZS )ÚResidualConv1dLayerNormBlocka_  Conv1d with Layer Normalization and residual connection as in GlowTTS paper.
    https://arxiv.org/pdf/1811.00002.pdf

    ::

        x |-> conv1d -> layer_norm -> relu -> dropout -> + -> o
          |---------------> conv1d_1x1 ------------------|

    Args:
        in_channels (int): number of input tensor channels.
        hidden_channels (int): number of inner layer channels.
        out_channels (int): number of output tensor channels.
        kernel_size (int): kernel size of conv1d filter.
        num_layers (int): number of blocks.
        dropout_p (float): dropout rate for each block.
    c              	      sÞ   t ƒ  ¡  || _|| _|| _|| _|| _|| _|dksJ dƒ‚|d dks)J dƒ‚t 	¡ | _
t 	¡ | _t|ƒD ]}| j
 tj|dkrD|n||||d d¡ | j t|ƒ¡ q7t ||d¡| _| jjj ¡  | jjj ¡  d S )Né   z$ [!] number of layers should be > 0.r   z& [!] kernel size should be odd number.r   )Úpadding)ÚsuperÚ__init__Úin_channelsÚhidden_channelsÚout_channelsÚkernel_sizeÚ
num_layersÚ	dropout_pr   Ú
ModuleListÚconv_layersÚnorm_layersÚrangeÚappendÚConv1dr   ÚprojÚweightÚdataÚzero_Úbias)Úselfr   r   r   r   r   r   Úidx©Ú	__class__© úO/home/kuhnn/.local/lib/python3.10/site-packages/TTS/tts/layers/glow_tts/glow.pyr      s*   


ÿÿz%ResidualConv1dLayerNormBlock.__init__c                 C   sh   |}t | jƒD ]!}| j| || ƒ}| j| || ƒ}tjt |¡| j| jd}q||  	|¡ }|| S )úd
        Shapes:
            - x: :math:`[B, C, T]`
            - x_mask: :math:`[B, 1, T]`
        )Útraining)
r   r   r   r   ÚFÚdropoutÚrelur   r%   r   )r   ÚxÚx_maskÚx_resÚir"   r"   r#   Úforward7   s   z$ResidualConv1dLayerNormBlock.forward)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r-   Ú__classcell__r"   r"   r    r#   r      s    r   c                       s4   e Zd ZdZd‡ fdd„	Zddd„Zd	d
„ Z‡  ZS )ÚInvConvNearaú  Invertible Convolution with input splitting as in GlowTTS paper.
    https://arxiv.org/pdf/1811.00002.pdf

    Args:
        channels (int): input and output channels.
        num_splits (int): number of splits, also H and W of conv layer.
        no_jacobian (bool): enable/disable jacobian computations.

    Note:
        Split the input into groups of size self.num_splits and
        perform 1x1 convolution separately. Cast 1x1 conv operation
        to 2d by reshaping the input for efficiency.
    é   Fc                    sÄ   t ƒ  ¡  |d dksJ ‚|| _|| _|| _d | _ttjƒtdƒk r2t 	t 
| j| j¡ ¡ ¡d }ntj 	t 
| j| j¡ ¡ d¡d }t |¡dk rZd|d d …df  |d d …df< t |¡| _d S )Nr   r   z1.9Úcompleteéÿÿÿÿ)r   r   ÚchannelsÚ
num_splitsÚno_jacobianÚ
weight_invr   ÚtorchÚ__version__ÚqrÚFloatTensorÚnormal_ÚlinalgÚdetr   Ú	Parameterr   )r   r7   r8   r9   ÚkwargsÚw_initr    r"   r#   r   U   s   
 " zInvConvNear.__init__Nc                 K   sn  |  ¡ \}}}|| j dksJ ‚|du r$d}tj|f|j|jd| }nt |ddg¡}| |d|| j | jd |¡}| ddddd¡ 	¡  || j|| j |¡}|rl| j
dur[| j
}	nt | j ¡ ¡j| jjd}	d}
n| j}	| jrud}
nt | j¡|| j  | }
|	 | j| jdd¡}	t ||	¡}| |d| jd || j |¡}| ddddd¡ 	¡  |||¡| }||
fS )	r$   r   Nr	   )ÚdtypeÚdevicer   é   r4   ©rE   )Úsizer8   r;   ÚonesrE   rF   ÚsumÚviewÚpermuteÚ
contiguousr:   Úinverser   ÚfloatÚtor9   Úlogdetr&   Úconv2d)r   r)   r*   ÚreverserC   ÚbÚcÚtÚx_lenr   rR   Úzr"   r"   r#   r-   f   s,   *
$zInvConvNear.forwardc                 C   s0   t  | j ¡ ¡j| jjd}tj|dd| _d S )NrH   F)Úrequires_grad)	r;   rO   r   rP   rQ   rE   r   rB   r:   )r   r:   r"   r"   r#   Ústore_inverse‹   s   zInvConvNear.store_inverse)r4   F)NF©r.   r/   r0   r1   r   r-   r[   r2   r"   r"   r    r#   r3   F   s
    
%r3   c                       s:   e Zd ZdZ			d‡ fdd„	Zddd„Zd	d
„ Z‡  ZS )ÚCouplingBlockad  Glow Affine Coupling block as in GlowTTS paper.
    https://arxiv.org/pdf/1811.00002.pdf

    ::

        x --> x0 -> conv1d -> wavenet -> conv1d --> t, s -> concat(s*x1 + t, x0) -> o
        '-> x1 - - - - - - - - - - - - - - - - - - - - - - - - - ^

    Args:
         in_channels (int): number of input tensor channels.
         hidden_channels (int): number of hidden channels.
         kernel_size (int): WaveNet filter kernel size.
         dilation_rate (int): rate to increase dilation by each layer in a decoder block.
         num_layers (int): number of WaveNet layers.
         c_in_channels (int): number of conditioning input channels.
         dropout_p (int): wavenet dropout rate.
         sigmoid_scale (bool): enable/disable sigmoid scaling for output scale.

    Note:
         It does not use the conditional inputs differently from WaveGlow.
    r   Fc	                    s¬   t ƒ  ¡  || _|| _|| _|| _|| _|| _|| _|| _	t
j |d |d¡}	t
jjj |	¡}	|	| _t
j ||d¡}
|
jj ¡  |
jj ¡  |
| _t|||||||ƒ| _d S )Nr   r	   )r   r   r   r   r   Údilation_rater   Úc_in_channelsr   Úsigmoid_scaler;   r   r   ÚutilsÚparametrizationsÚweight_normÚstartr   r   r   r   Úendr   Úwn)r   r   r   r   r^   r   r_   r   r`   rd   re   r    r"   r#   r   §   s"   
zCouplingBlock.__init__Nc                 K   s.  |du rd}|dd…d| j d …f |dd…| j d d…f }}|  |¡| }|  |||¡}|  |¡}|}	|dd…d| j d …dd…f }
|dd…| j d d…dd…f }| jret dt |d ¡ ¡}|rv||
 t | ¡ | }d}n|
t |¡|  | }t 	|| ddg¡}t 
|	|gd¡}||fS )z‡
        Shapes:
            - x: :math:`[B, C, T]`
            - x_mask: :math:`[B, 1, T]`
            - g: :math:`[B, C, 1]`
        Nr	   r   gíµ ÷Æ°>)r   rd   rf   re   r`   r;   ÚlogÚsigmoidÚexprK   Úcat)r   r)   r*   rT   ÚgrC   Úx_0Úx_1ÚoutÚz_0rW   ÚsÚz_1rR   rY   r"   r"   r#   r-   É   s$   6
  zCouplingBlock.forwardc                 C   s   | j  ¡  d S )N)rf   Úremove_weight_norm)r   r"   r"   r#   r[   è   s   zCouplingBlock.store_inverse)r   r   F)NFNr\   r"   r"   r    r#   r]      s    ÷
"r]   )r;   Úpackaging.versionr   r   Útorch.nnr   r&   ÚTTS.tts.layers.generic.wavenetr   Úgeneric.normalizationr   ÚModuler   r3   r]   r"   r"   r"   r#   Ú<module>   s    ;J