o
     ôjZ³  ã                   @   sT  d dl Z d dlmZmZmZmZ d dlZd dlmZmZ d dl	m
Z dgZd(ded	ed
ededejjf
dd„Z						d)dededededeeeeee f  ded
ededejjfdd„Zdedefdd„ZG dd„ dejƒZG dd„ dejƒZG dd „ d ejƒZG d!d"„ d"ejƒ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)ÚListÚOptionalÚTupleÚUnion)ÚnnÚTensor)Ú
functionalÚ	Tacotron2TÚlinearÚin_dimÚout_dimÚbiasÚw_init_gainÚreturnc                 C   s4   t jj| ||d�}t jjj|jt jj |¡d� |S )a  Linear layer with xavier uniform initialization.

    Args:
        in_dim (int): Size of each input sample.
        out_dim (int): Size of each output sample.
        bias (bool, optional): If set to ``False``, the layer will not learn an additive bias. (Default: ``True``)
        w_init_gain (str, optional): Parameter passed to ``torch.nn.init.calculate_gain``
            for setting the gain parameter of ``xavier_uniform_``. (Default: ``linear``)

    Returns:
        (torch.nn.Linear): The corresponding linear layer.
    ©r   ©Úgain)Útorchr   ÚLinearÚinitÚxavier_uniform_ÚweightÚcalculate_gain)r   r   r   r   r
   © r   ún/var/www/html/academy/tools/voices/clone/cosy-venv/lib/python3.10/site-packages/torchaudio/models/tacotron2.pyÚ_get_linear_layer)   s   r   é   Úin_channelsÚout_channelsÚkernel_sizeÚstrideÚpaddingÚdilationc           	   	   C   sl   |du r|d dkrt dƒ‚t||d  d ƒ}tjj| ||||||d�}tjjj|jtjj |¡d� |S )al  1D convolution with xavier uniform initialization.

    Args:
        in_channels (int): Number of channels in the input image.
        out_channels (int): Number of channels produced by the convolution.
        kernel_size (int, optional): Number of channels in the input image. (Default: ``1``)
        stride (int, optional): Number of channels in the input image. (Default: ``1``)
        padding (str, int or tuple, optional): Padding added to both sides of the input.
            (Default: dilation * (kernel_size - 1) / 2)
        dilation (int, optional): Number of channels in the input image. (Default: ``1``)
        w_init_gain (str, optional): Parameter passed to ``torch.nn.init.calculate_gain``
            for setting the gain parameter of ``xavier_uniform_``. (Default: ``linear``)

    Returns:
        (torch.nn.Conv1d): The corresponding Conv1D layer.
    Né   r   zkernel_size must be odd)r   r    r!   r"   r   r   )	Ú
ValueErrorÚintr   r   ÚConv1dr   r   r   r   )	r   r   r   r    r!   r"   r   r   Úconv1dr   r   r   Ú_get_conv1d_layer;   s   ù
r(   Úlengthsc                 C   sF   t  | ¡ ¡ }t jd|| j| jd�}||  d¡k  ¡ }t  |d¡}|S )al  Returns a binary mask based on ``lengths``. The ``i``-th row and ``j``-th column of the mask
    is ``1`` if ``j`` is smaller than ``i``-th element of ``lengths.

    Args:
        lengths (Tensor): The length of each element in the batch, with shape (n_batch, ).

    Returns:
        mask (Tensor): The binary mask, with shape (n_batch, max of ``lengths``).
    r   )ÚdeviceÚdtyper   )	r   ÚmaxÚitemÚaranger*   r+   Ú	unsqueezeÚbyteÚle)r)   Úmax_lenÚidsÚmaskr   r   r   Ú_get_mask_from_lengthsi   s
   
r5   c                       s@   e Zd ZdZdededef‡ fdd„Zdedefd	d
„Z‡  ZS )Ú_LocationLayera  Location layer used in the Attention model.

    Args:
        attention_n_filter (int): Number of filters for attention model.
        attention_kernel_size (int): Kernel size for attention model.
        attention_hidden_dim (int): Dimension of attention hidden representation.
    Úattention_n_filterÚattention_kernel_sizeÚattention_hidden_dimc              	      sH   t ƒ  ¡  t|d d ƒ}td|||dddd�| _t||ddd�| _d S )Nr   r#   F)r   r!   r   r    r"   Útanh©r   r   )ÚsuperÚ__init__r%   r(   Úlocation_convr   Úlocation_dense)Úselfr7   r8   r9   r!   ©Ú	__class__r   r   r=   ƒ   s   
ù	ÿz_LocationLayer.__init__Úattention_weights_catr   c                 C   s$   |   |¡}| dd¡}|  |¡}|S )a�  Location layer used in the Attention model.

        Args:
            attention_weights_cat (Tensor): Cumulative and previous attention weights
                with shape (n_batch, 2, max of ``text_lengths``).

        Returns:
            processed_attention (Tensor): Cumulative and previous attention weights
                with shape (n_batch, ``attention_hidden_dim``).
        r   r#   )r>   Ú	transposer?   )r@   rC   Zprocessed_attentionr   r   r   Úforward˜   s   

z_LocationLayer.forward©	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r%   r=   r   rE   Ú__classcell__r   r   rA   r   r6   z   s    þýür6   c                       s~   e Zd ZdZdedededededdf‡ fd	d
„Zdedededefdd„Zdedededededeeef fdd„Z	‡  Z
S )Ú
_Attentionaº  Locally sensitive attention model.

    Args:
        attention_rnn_dim (int): Number of hidden units for RNN.
        encoder_embedding_dim (int): Number of embedding dimensions in the Encoder.
        attention_hidden_dim (int): Dimension of attention hidden representation.
        attention_location_n_filter (int): Number of filters for Attention model.
        attention_location_kernel_size (int): Kernel size for Attention model.
    Úattention_rnn_dimÚencoder_embedding_dimr9   Úattention_location_n_filterÚattention_location_kernel_sizer   Nc                    s\   t ƒ  ¡  t||ddd�| _t||ddd�| _t|ddd�| _t|||ƒ| _tdƒ | _	d S )NFr:   r;   r   r   Úinf)
r<   r=   r   Úquery_layerÚmemory_layerÚvr6   Úlocation_layerÚfloatÚscore_mask_value)r@   rM   rN   r9   rO   rP   rA   r   r   r=   ¶   s   
ÿýz_Attention.__init__ÚqueryÚprocessed_memoryrC   c                 C   s@   |   | d¡¡}|  |¡}|  t || | ¡¡}| d¡}|S )a=  Get the alignment vector.

        Args:
            query (Tensor): Decoder output with shape (n_batch, n_mels * n_frames_per_step).
            processed_memory (Tensor): Processed Encoder outputs
                with shape (n_batch, max of ``text_lengths``, attention_hidden_dim).
            attention_weights_cat (Tensor): Cumulative and previous attention weights
                with shape (n_batch, 2, max of ``text_lengths``).

        Returns:
            alignment (Tensor): attention weights, it is a tensor with shape (batch, max of ``text_lengths``).
        r   r#   )rR   r/   rU   rT   r   r:   Úsqueeze)r@   rX   rY   rC   Zprocessed_queryZprocessed_attention_weightsZenergiesÚ	alignmentr   r   r   Ú_get_alignment_energiesË   s
   

z"_Attention._get_alignment_energiesÚattention_hidden_stateÚmemoryr4   c           	      C   sN   |   |||¡}| || j¡}tj|dd�}t | d¡|¡}| d¡}||fS )a¹  Pass the input through the Attention model.

        Args:
            attention_hidden_state (Tensor): Attention rnn last output with shape (n_batch, ``attention_rnn_dim``).
            memory (Tensor): Encoder outputs with shape (n_batch, max of ``text_lengths``, ``encoder_embedding_dim``).
            processed_memory (Tensor): Processed Encoder outputs
                with shape (n_batch, max of ``text_lengths``, ``attention_hidden_dim``).
            attention_weights_cat (Tensor): Previous and cumulative attention weights
                with shape (n_batch, current_num_frames * 2, max of ``text_lengths``).
            mask (Tensor): Binary mask for padded data with shape (n_batch, current_num_frames).

        Returns:
            attention_context (Tensor): Context vector with shape (n_batch, ``encoder_embedding_dim``).
            attention_weights (Tensor): Attention weights with shape (n_batch, max of ``text_lengths``).
        r   ©Údim)	r\   Úmasked_fillrW   ÚFÚsoftmaxr   Úbmmr/   rZ   )	r@   r]   r^   rY   rC   r4   r[   Úattention_weightsÚattention_contextr   r   r   rE   à   s   
z_Attention.forward)rG   rH   rI   rJ   r%   r=   r   r\   r   rE   rK   r   r   rA   r   rL   «   s:    
þýüûúùþýüûú
ùrL   c                       sD   e Zd ZdZdedee ddf‡ fdd„Zdedefd	d
„Z‡  Z	S )Ú_PrenetzÒPrenet Module. It is consists of ``len(output_size)`` linear layers.

    Args:
        in_dim (int): The size of each input sample.
        output_sizes (list): The output dimension of each linear layers.
    r   Ú	out_sizesr   Nc                    s<   t ƒ  ¡  |g|d d…  }t dd„ t||ƒD ƒ¡| _d S )Néÿÿÿÿc                 S   s   g | ]\}}t ||d d�‘qS )Fr   )r   )Ú.0Úin_sizeÚout_sizer   r   r   Ú
<listcomp>  s    z$_Prenet.__init__.<locals>.<listcomp>)r<   r=   r   Ú
ModuleListÚzipÚlayers)r@   r   rh   Zin_sizesrA   r   r   r=   
  s
   

ÿz_Prenet.__init__Úxc                 C   s*   | j D ]}tjt ||ƒ¡ddd�}q|S )zÙPass the input through Prenet.

        Args:
            x (Tensor): The input sequence to Prenet with shape (n_batch, in_dim).

        Return:
            x (Tensor): Tensor with shape (n_batch, sizes[-1])
        ç      à?T)ÚpÚtraining)rp   rb   ÚdropoutÚrelu)r@   rq   r
   r   r   r   rE     s   

z_Prenet.forward)
rG   rH   rI   rJ   r%   r   r=   r   rE   rK   r   r   rA   r   rg     s    rg   c                       sD   e Zd ZdZdedededef‡ fdd„Zded	efd
d„Z‡  ZS )Ú_Postneta  Postnet Module.

    Args:
        n_mels (int): Number of mel bins.
        postnet_embedding_dim (int): Postnet embedding dimension.
        postnet_kernel_size (int): Postnet kernel size.
        postnet_n_convolution (int): Number of postnet convolutions.
    Ún_melsÚpostnet_embedding_dimÚpostnet_kernel_sizeÚpostnet_n_convolutionc           
         s¶   t ƒ  ¡  t ¡ | _t|ƒD ]D}|dkr|n|}||d kr |n|}||d kr*dnd}||d kr4|n|}	| j t t|||dt	|d d ƒd|d�t 
|	¡¡¡ qt| jƒ| _d S )Nr   r   r
   r:   r#   ©r   r    r!   r"   r   )r<   r=   r   rn   ÚconvolutionsÚrangeÚappendÚ
Sequentialr(   r%   ÚBatchNorm1dÚlenÚn_convs)
r@   rx   ry   rz   r{   Úir   r   Z	init_gainÚnum_featuresrA   r   r   r=   *  s,   

ù	öÿz_Postnet.__init__rq   r   c                 C   sZ   t | jƒD ]%\}}|| jd k rtjt ||ƒ¡d| jd�}qtj||ƒd| jd�}q|S )a  Pass the input through Postnet.

        Args:
            x (Tensor): The input sequence with shape (n_batch, ``n_mels``, max of ``mel_specgram_lengths``).

        Return:
            x (Tensor): Tensor with shape (n_batch, ``n_mels``, max of ``mel_specgram_lengths``).
        r   rr   )rt   )Ú	enumerater}   rƒ   rb   ru   r   r:   rt   )r@   rq   r„   Úconvr   r   r   rE   J  s
   
z_Postnet.forwardrF   r   r   rA   r   rw      s    	þýüû rw   c                       sH   e Zd ZdZdedededdf‡ fdd„Zd	ed
edefdd„Z‡  ZS )Ú_Encodera§  Encoder Module.

    Args:
        encoder_embedding_dim (int): Number of embedding dimensions in the encoder.
        encoder_n_convolution (int): Number of convolution layers in the encoder.
        encoder_kernel_size (int): The kernel size in the encoder.

    Examples
        >>> encoder = _Encoder(3, 512, 5)
        >>> input = torch.rand(10, 20, 30)
        >>> output = encoder(input)  # shape: (10, 30, 512)
    rN   Úencoder_n_convolutionÚencoder_kernel_sizer   Nc                    sŠ   t ƒ  ¡  t ¡ | _t|ƒD ] }t t|||dt|d d ƒddd�t 	|¡¡}| j 
|¡ qtj|t|d ƒdddd�| _| j ¡  d S )Nr   r#   rv   r|   T)Úbatch_firstÚbidirectional)r<   r=   r   rn   r}   r~   r€   r(   r%   r�   r   ÚLSTMÚlstmÚflatten_parameters)r@   rN   r‰   rŠ   Ú_Z
conv_layerrA   r   r   r=   k  s0   

ù	ö
ûz_Encoder.__init__rq   Úinput_lengthsc                 C   sv   | j D ]}t t ||ƒ¡d| j¡}q| dd¡}| ¡ }tjj	j
||dd�}|  |¡\}}tjj	j|dd�\}}|S )a_  Pass the input through the Encoder.

        Args:
            x (Tensor): The input sequences with shape (n_batch, encoder_embedding_dim, n_seq).
            input_lengths (Tensor): The length of each input sequence with shape (n_batch, ).

        Return:
            x (Tensor): A tensor with shape (n_batch, n_seq, encoder_embedding_dim).
        rr   r   r#   T)r‹   )r}   rb   ru   rv   rt   rD   Úcpur   ÚutilsÚrnnÚpack_padded_sequencerŽ   Úpad_packed_sequence)r@   rq   r‘   r‡   Úoutputsr�   r   r   r   rE   Œ  s   
z_Encoder.forwardrF   r   r   rA   r   rˆ   ]  s    þýüû!rˆ   c                !       s|  e Zd ZdZdededededededed	ed
ededededededdf‡ fdd„Zdedefdd„Z	dede
eeeeeeeef fdd„Zdedefdd„Zdededede
eeef fdd „Zd!ed"ed#ed$ed%ed&ed'ed(eded)ed*ede
eeeeeeeeef	 fd+d,„Zded-ed.ede
eeef fd/d0„Zdedefd1d2„Zejjded.ede
eeeef fd3d4„ƒZ‡  ZS )5Ú_Decodera,  Decoder with Attention model.

    Args:
        n_mels (int): number of mel bins
        n_frames_per_step (int): number of frames processed per step, only 1 is supported
        encoder_embedding_dim (int): the number of embedding dimensions in the encoder.
        decoder_rnn_dim (int): number of units in decoder LSTM
        decoder_max_step (int): maximum number of output mel spectrograms
        decoder_dropout (float): dropout probability for decoder LSTM
        decoder_early_stopping (bool): stop decoding when all samples are finished
        attention_rnn_dim (int): number of units in attention LSTM
        attention_hidden_dim (int): dimension of attention hidden representation
        attention_location_n_filter (int): number of filters for attention model
        attention_location_kernel_size (int): kernel size for attention model
        attention_dropout (float): dropout probability for attention LSTM
        prenet_dim (int): number of ReLU units in prenet layers
        gate_threshold (float): probability threshold for stop token
    rx   Ún_frames_per_steprN   Údecoder_rnn_dimÚdecoder_max_stepÚdecoder_dropoutÚdecoder_early_stoppingrM   r9   rO   rP   Úattention_dropoutÚ
prenet_dimÚgate_thresholdr   Nc                    sÆ   t ƒ  ¡  || _|| _|| _|| _|| _|| _|| _|| _	|| _
|| _|| _t|| ||gƒ| _t || |¡| _t|||	|
|ƒ| _t || |d¡| _t|| || ƒ| _t|| dddd�| _d S )NTr   Úsigmoidr;   )r<   r=   rx   r™   rN   rM   rš   rŸ   r›   r    rž   rœ   r�   rg   Úprenetr   ÚLSTMCellÚattention_rnnrL   Úattention_layerÚdecoder_rnnr   Úlinear_projectionÚ
gate_layer)r@   rx   r™   rN   rš   r›   rœ   r�   rM   r9   rO   rP   rž   rŸ   r    rA   r   r   r=   ¹  s4   
ûÿz_Decoder.__init__r^   c                 C   ó4   |  d¡}|j}|j}tj|| j| j ||d�}|S )am  Gets all zeros frames to use as the first decoder input.

        Args:
            memory (Tensor): Encoder outputs with shape (n_batch, max of ``text_lengths``, ``encoder_embedding_dim``).

        Returns:
            decoder_input (Tensor): all zeros frames with shape
                (n_batch, max of ``text_lengths``, ``n_mels * n_frames_per_step``).
        r   ©r+   r*   ©Úsizer+   r*   r   Úzerosrx   r™   ©r@   r^   Ún_batchr+   r*   Údecoder_inputr   r   r   Ú_get_initial_frameì  ó
   
z_Decoder._get_initial_framec                 C   sÈ   |  d¡}|  d¡}|j}|j}tj|| j||d�}tj|| j||d�}tj|| j||d�}tj|| j||d�}	tj||||d�}
tj||||d�}tj|| j||d�}| j 	|¡}||||	|
|||fS )a  Initializes attention rnn states, decoder rnn states, attention
        weights, attention cumulative weights, attention context, stores memory
        and stores processed memory.

        Args:
            memory (Tensor): Encoder outputs with shape (n_batch, max of ``text_lengths``, ``encoder_embedding_dim``).

        Returns:
            attention_hidden (Tensor): Hidden state of the attention LSTM with shape (n_batch, ``attention_rnn_dim``).
            attention_cell (Tensor): Hidden state of the attention LSTM with shape (n_batch, ``attention_rnn_dim``).
            decoder_hidden (Tensor): Hidden state of the decoder LSTM with shape (n_batch, ``decoder_rnn_dim``).
            decoder_cell (Tensor): Hidden state of the decoder LSTM with shape (n_batch, ``decoder_rnn_dim``).
            attention_weights (Tensor): Attention weights with shape (n_batch, max of ``text_lengths``).
            attention_weights_cum (Tensor): Cumulated attention weights with shape (n_batch, max of ``text_lengths``).
            attention_context (Tensor): Context vector with shape (n_batch, ``encoder_embedding_dim``).
            processed_memory (Tensor): Processed encoder outputs
                with shape (n_batch, max of ``text_lengths``, ``attention_hidden_dim``).
        r   r   rª   )
r¬   r+   r*   r   r­   rM   rš   rN   r¥   rS   )r@   r^   r¯   Zmax_timer+   r*   Úattention_hiddenÚattention_cellÚdecoder_hiddenÚdecoder_cellre   Úattention_weights_cumrf   rY   r   r   r   Ú_initialize_decoder_statesý  s*   

øz#_Decoder._initialize_decoder_statesÚdecoder_inputsc                 C   s@   |  dd¡}| | d¡t| d¡| j ƒd¡}|  dd¡}|S )ak  Prepares decoder inputs.

        Args:
            decoder_inputs (Tensor): Inputs used for teacher-forced training, i.e. mel-specs,
                with shape (n_batch, ``n_mels``, max of ``mel_specgram_lengths``)

        Returns:
            inputs (Tensor): Processed decoder inputs with shape (max of ``mel_specgram_lengths``, n_batch, ``n_mels``).
        r   r#   r   ri   )rD   Úviewr¬   r%   r™   )r@   r¹   r   r   r   Ú_parse_decoder_inputs.  s   ýz_Decoder._parse_decoder_inputsÚmel_specgramÚgate_outputsÚ
alignmentsc                 C   sb   |  dd¡ ¡ }|  dd¡ ¡ }|  dd¡ ¡ }|jd d| jf}|j|Ž }|  dd¡}|||fS )aq  Prepares decoder outputs for output

        Args:
            mel_specgram (Tensor): mel spectrogram with shape (max of ``mel_specgram_lengths``, n_batch, ``n_mels``)
            gate_outputs (Tensor): predicted stop token with shape (max of ``mel_specgram_lengths``, n_batch)
            alignments (Tensor): sequence of attention weights from the decoder
                with shape (max of ``mel_specgram_lengths``, n_batch, max of ``text_lengths``)

        Returns:
            mel_specgram (Tensor): mel spectrogram with shape (n_batch, ``n_mels``, max of ``mel_specgram_lengths``)
            gate_outputs (Tensor): predicted stop token with shape (n_batch, max of ``mel_specgram_lengths``)
            alignments (Tensor): sequence of attention weights from the decoder
                with shape (n_batch, max of ``mel_specgram_lengths``, max of ``text_lengths``)
        r   r   ri   r#   )rD   Ú
contiguousÚshaperx   rº   )r@   r¼   r½   r¾   rÀ   r   r   r   Ú_parse_decoder_outputsC  s   

z_Decoder._parse_decoder_outputsr°   r³   r´   rµ   r¶   re   r·   rf   rY   r4   c              	   C   sä   t  ||fd¡}|  |||f¡\}}t || j| j¡}t j| d¡| d¡fdd�}|  ||	|
||¡\}}||7 }t  ||fd¡}|  	|||f¡\}}t || j
| j¡}t j||fdd�}|  |¡}|  |¡}|||||||||f	S )a&	  Decoder step using stored states, attention and memory

        Args:
            decoder_input (Tensor): Output of the Prenet with shape (n_batch, ``prenet_dim``).
            attention_hidden (Tensor): Hidden state of the attention LSTM with shape (n_batch, ``attention_rnn_dim``).
            attention_cell (Tensor): Hidden state of the attention LSTM with shape (n_batch, ``attention_rnn_dim``).
            decoder_hidden (Tensor): Hidden state of the decoder LSTM with shape (n_batch, ``decoder_rnn_dim``).
            decoder_cell (Tensor): Hidden state of the decoder LSTM with shape (n_batch, ``decoder_rnn_dim``).
            attention_weights (Tensor): Attention weights with shape (n_batch, max of ``text_lengths``).
            attention_weights_cum (Tensor): Cumulated attention weights with shape (n_batch, max of ``text_lengths``).
            attention_context (Tensor): Context vector with shape (n_batch, ``encoder_embedding_dim``).
            memory (Tensor): Encoder output with shape (n_batch, max of ``text_lengths``, ``encoder_embedding_dim``).
            processed_memory (Tensor): Processed Encoder outputs
                with shape (n_batch, max of ``text_lengths``, ``attention_hidden_dim``).
            mask (Tensor): Binary mask for padded data with shape (n_batch, current_num_frames).

        Returns:
            decoder_output: Predicted mel spectrogram for the current frame with shape (n_batch, ``n_mels``).
            gate_prediction (Tensor): Prediction of the stop token with shape (n_batch, ``1``).
            attention_hidden (Tensor): Hidden state of the attention LSTM with shape (n_batch, ``attention_rnn_dim``).
            attention_cell (Tensor): Hidden state of the attention LSTM with shape (n_batch, ``attention_rnn_dim``).
            decoder_hidden (Tensor): Hidden state of the decoder LSTM with shape (n_batch, ``decoder_rnn_dim``).
            decoder_cell (Tensor): Hidden state of the decoder LSTM with shape (n_batch, ``decoder_rnn_dim``).
            attention_weights (Tensor): Attention weights with shape (n_batch, max of ``text_lengths``).
            attention_weights_cum (Tensor): Cumulated attention weights with shape (n_batch, max of ``text_lengths``).
            attention_context (Tensor): Context vector with shape (n_batch, ``encoder_embedding_dim``).
        ri   r   r_   )r   Úcatr¤   rb   ru   rž   rt   r/   r¥   r¦   rœ   r§   r¨   )r@   r°   r³   r´   rµ   r¶   re   r·   rf   r^   rY   r4   Z
cell_inputrC   Z decoder_hidden_attention_contextZdecoder_outputZgate_predictionr   r   r   Údecodec  s0   )
ÿ

÷z_Decoder.decodeÚmel_specgram_truthÚmemory_lengthsc                 C   s*  |   |¡ d¡}|  |¡}tj||fdd�}|  |¡}t|ƒ}|  |¡\}}}	}
}}}}g g g }}}t|ƒ| 	d¡d k r}|t|ƒ }|  
||||	|
||||||¡\	}}}}}	}
}}}|| d¡g7 }|| d¡g7 }||g7 }t|ƒ| 	d¡d k s?|  t |¡t |¡t |¡¡\}}}|||fS )aî  Decoder forward pass for training.

        Args:
            memory (Tensor): Encoder outputs
                with shape (n_batch, max of ``text_lengths``, ``encoder_embedding_dim``).
            mel_specgram_truth (Tensor): Decoder ground-truth mel-specs for teacher forcing
                with shape (n_batch, ``n_mels``, max of ``mel_specgram_lengths``).
            memory_lengths (Tensor): Encoder output lengths for attention masking
                (the same as ``text_lengths``) with shape (n_batch, ).

        Returns:
            mel_specgram (Tensor): Predicted mel spectrogram
                with shape (n_batch, ``n_mels``, max of ``mel_specgram_lengths``).
            gate_outputs (Tensor): Predicted stop token for each timestep
                with shape (n_batch,  max of ``mel_specgram_lengths``).
            alignments (Tensor): Sequence of attention weights from the decoder
                with shape (n_batch,  max of ``mel_specgram_lengths``, max of ``text_lengths``).
        r   r_   r   )r±   r/   r»   r   rÂ   r¢   r5   r¸   r‚   r¬   rÃ   rZ   rÁ   Ústack)r@   r^   rÄ   rÅ   r°   r¹   r4   r³   r´   rµ   r¶   re   r·   rf   rY   Zmel_outputsr½   r¾   Z
mel_outputÚgate_outputr¼   r   r   r   rE   ­  sb   


÷õö
ä
ÿ
z_Decoder.forwardc                 C   r©   )aU  Gets all zeros frames to use as the first decoder input

        args:
            memory (Tensor): Encoder outputs
                with shape (n_batch, max of ``text_lengths``, ``encoder_embedding_dim``).

        returns:
            decoder_input (Tensor): All zeros frames with shape(n_batch, ``n_mels`` * ``n_frame_per_step``).
        r   rª   r«   r®   r   r   r   Ú_get_go_frameù  r²   z_Decoder._get_go_framec                 C   sŽ  |  d¡|j}}|  |¡}t|ƒ}|  |¡\}}}	}
}}}}tj|gtj|d�}tj|gtj|d�}g }g }g }t	| j
ƒD ]W}|  |¡}|  ||||	|
||||||¡\	}}}}}	}
}}}| | d¡¡ | | dd¡¡ | |¡ ||   d7  < |t | d¡¡| jkO }| jr“t |¡r“ n|}q>t|ƒ| j
kr¢t d¡ tj|dd�}tj|dd�}tj|dd�}|  |||¡\}}}||||fS )a’  Decoder inference

        Args:
            memory (Tensor): Encoder outputs
                with shape (n_batch, max of ``text_lengths``, ``encoder_embedding_dim``).
            memory_lengths (Tensor): Encoder output lengths for attention masking
                (the same as ``text_lengths``) with shape (n_batch, ).

        Returns:
            mel_specgram (Tensor): Predicted mel spectrogram
                with shape (n_batch, ``n_mels``, max of ``mel_specgram_lengths``).
            mel_specgram_lengths (Tensor): the length of the predicted mel spectrogram (n_batch, ))
            gate_outputs (Tensor): Predicted stop token for each timestep
                with shape (n_batch,  max of ``mel_specgram_lengths``).
            alignments (Tensor): Sequence of attention weights from the decoder
                with shape (n_batch,  max of ``mel_specgram_lengths``, max of ``text_lengths``).
        r   rª   r   zZReached max decoder steps. The generated spectrogram might not cover the whole transcript.r_   )r¬   r*   rÈ   r5   r¸   r   r­   Úint32Úboolr~   r›   r¢   rÃ   r   r/   rD   r¡   rZ   r    r�   Úallr‚   ÚwarningsÚwarnrÂ   rÁ   )r@   r^   rÅ   Ú
batch_sizer*   r°   r4   r³   r´   rµ   r¶   re   r·   rf   rY   Úmel_specgram_lengthsÚfinishedZmel_specgramsr½   r¾   r�   r¼   rÇ   r   r   r   Úinfer
  sx   

÷
õö
ÿz_Decoder.infer)rG   rH   rI   rJ   r%   rV   rÊ   r=   r   r±   r   r¸   r»   rÁ   rÃ   rE   rÈ   r   ÚjitÚexportrÑ   rK   r   r   rA   r   r˜   ¥  s¬    þýüûúùø	÷
öõôóòñð3ÿ
þ1ÿÿÿ
þ þýüûúùø	÷
öõô
óJÿÿÿ
þL,r˜   c                /       s  e Zd ZdZ											
													d5dedededededededededededededed ed!ed"ed#ed$ed%ed&ed'ed(d)f.‡ fd*d+„Zd,ed-ed.ed/ed(e	eeeef f
d0d1„Z
ejjd6d,ed2ee d(e	eeef fd3d4„ƒZ‡  ZS )7r	   aÑ	  Tacotron2 model from *Natural TTS Synthesis by Conditioning WaveNet on Mel Spectrogram Predictions*
    :cite:`shen2018natural` based on the implementation from
    `Nvidia Deep Learning Examples <https://github.com/NVIDIA/DeepLearningExamples/>`_.

    See Also:
        * :class:`torchaudio.pipelines.Tacotron2TTSBundle`: TTS pipeline with pretrained model.

    Args:
        mask_padding (bool, optional): Use mask padding (Default: ``False``).
        n_mels (int, optional): Number of mel bins (Default: ``80``).
        n_symbol (int, optional): Number of symbols for the input text (Default: ``148``).
        n_frames_per_step (int, optional): Number of frames processed per step, only 1 is supported (Default: ``1``).
        symbol_embedding_dim (int, optional): Input embedding dimension (Default: ``512``).
        encoder_n_convolution (int, optional): Number of encoder convolutions (Default: ``3``).
        encoder_kernel_size (int, optional): Encoder kernel size (Default: ``5``).
        encoder_embedding_dim (int, optional): Encoder embedding dimension (Default: ``512``).
        decoder_rnn_dim (int, optional): Number of units in decoder LSTM (Default: ``1024``).
        decoder_max_step (int, optional): Maximum number of output mel spectrograms (Default: ``2000``).
        decoder_dropout (float, optional): Dropout probability for decoder LSTM (Default: ``0.1``).
        decoder_early_stopping (bool, optional): Continue decoding after all samples are finished (Default: ``True``).
        attention_rnn_dim (int, optional): Number of units in attention LSTM (Default: ``1024``).
        attention_hidden_dim (int, optional): Dimension of attention hidden representation (Default: ``128``).
        attention_location_n_filter (int, optional): Number of filters for attention model (Default: ``32``).
        attention_location_kernel_size (int, optional): Kernel size for attention model (Default: ``31``).
        attention_dropout (float, optional): Dropout probability for attention LSTM (Default: ``0.1``).
        prenet_dim (int, optional): Number of ReLU units in prenet layers (Default: ``256``).
        postnet_n_convolution (int, optional): Number of postnet convolutions (Default: ``5``).
        postnet_kernel_size (int, optional): Postnet kernel size (Default: ``5``).
        postnet_embedding_dim (int, optional): Postnet embedding dimension (Default: ``512``).
        gate_threshold (float, optional): Probability threshold for stop token (Default: ``0.5``).
    FéP   é”   r   é   é   é   é   éÐ  çš™™™™™¹?Té€   é    é   é   rr   Úmask_paddingrx   Ún_symbolr™   Úsymbol_embedding_dimrN   r‰   rŠ   rš   r›   rœ   r�   rM   r9   rO   rP   rž   rŸ   r{   rz   ry   r    r   Nc                    s‚   t ƒ  ¡  || _|| _|| _t ||¡| _tjj	 
| jj¡ t|||ƒ| _t||||	|
|||||||||ƒ| _t||||ƒ| _d S ©N)r<   r=   rà   rx   r™   r   Ú	EmbeddingÚ	embeddingr   r   r   r   rˆ   Úencoderr˜   Údecoderrw   Úpostnet)r@   rà   rx   rá   r™   râ   rN   r‰   rŠ   rš   r›   rœ   r�   rM   r9   rO   rP   rž   rŸ   r{   rz   ry   r    rA   r   r   r=   †  s0   
òzTacotron2.__init__ÚtokensÚtoken_lengthsr¼   rÏ   c                 C   sÀ   |   |¡ dd¡}|  ||¡}| j|||d�\}}}|  |¡}	||	 }	| jrZt|ƒ}
|
 | j|
 	d¡|
 	d¡¡}
|
 
ddd¡}
| |
d¡ |	 |
d¡ | |
dd…ddd…f d¡ ||	||fS )a´  Pass the input through the Tacotron2 model. This is in teacher
        forcing mode, which is generally used for training.

        The input ``tokens`` should be padded with zeros to length max of ``token_lengths``.
        The input ``mel_specgram`` should be padded with zeros to length max of ``mel_specgram_lengths``.

        Args:
            tokens (Tensor): The input tokens to Tacotron2 with shape `(n_batch, max of token_lengths)`.
            token_lengths (Tensor): The valid length of each sample in ``tokens`` with shape `(n_batch, )`.
            mel_specgram (Tensor): The target mel spectrogram
                with shape `(n_batch, n_mels, max of mel_specgram_lengths)`.
            mel_specgram_lengths (Tensor): The length of each mel spectrogram with shape `(n_batch, )`.

        Returns:
            [Tensor, Tensor, Tensor, Tensor]:
                Tensor
                    Mel spectrogram before Postnet with shape `(n_batch, n_mels, max of mel_specgram_lengths)`.
                Tensor
                    Mel spectrogram after Postnet with shape `(n_batch, n_mels, max of mel_specgram_lengths)`.
                Tensor
                    The output for stop token at each time step with shape `(n_batch, max of mel_specgram_lengths)`.
                Tensor
                    Sequence of attention weights from the decoder with
                    shape `(n_batch, max of mel_specgram_lengths, max of token_lengths)`.
        r   r#   )rÅ   r   g        Ng     @�@)rå   rD   ræ   rç   rè   rà   r5   Úexpandrx   r¬   ÚpermuteÚmasked_fill_)r@   ré   rê   r¼   rÏ   Úembedded_inputsÚencoder_outputsr½   r¾   Zmel_specgram_postnetr4   r   r   r   rE   ¹  s   !ÿ
zTacotron2.forwardr)   c                 C   s¢   |j \}}|du rt |g¡ |¡ |j|j¡}|dusJ ‚|  |¡ dd¡}|  	||¡}| j
 ||¡\}}}	}
|  |¡}|| }|
 d||¡ dd¡}
|||
fS )aª  Using Tacotron2 for inference. The input is a batch of encoded
        sentences (``tokens``) and its corresponding lengths (``lengths``). The
        output is the generated mel spectrograms, its corresponding lengths, and
        the attention weights from the decoder.

        The input `tokens` should be padded with zeros to length max of ``lengths``.

        Args:
            tokens (Tensor): The input tokens to Tacotron2 with shape `(n_batch, max of lengths)`.
            lengths (Tensor or None, optional):
                The valid length of each sample in ``tokens`` with shape `(n_batch, )`.
                If ``None``, it is assumed that the all the tokens are valid. Default: ``None``

        Returns:
            (Tensor, Tensor, Tensor):
                Tensor
                    The predicted mel spectrogram with shape `(n_batch, n_mels, max of mel_specgram_lengths)`.
                Tensor
                    The length of the predicted mel spectrogram with shape `(n_batch, )`.
                Tensor
                    Sequence of attention weights from the decoder with shape
                    `(n_batch, max of mel_specgram_lengths, max of lengths)`.
        Nr   r#   r   )rÀ   r   Útensorrë   Útor*   r+   rå   rD   ræ   rç   rÑ   rè   Úunfold)r@   ré   r)   r¯   Ú
max_lengthrî   rï   r¼   rÏ   r�   r¾   Zmel_outputs_postnetr   r   r   rÑ   ï  s   


zTacotron2.infer)FrÔ   rÕ   r   rÖ   rÖ   r×   rØ   rÙ   rÚ   rÛ   TrÙ   rÜ   rÝ   rÞ   rÛ   rß   rØ   rØ   rÖ   rr   rã   )rG   rH   rI   rJ   rÊ   r%   rV   r=   r   r   rE   r   rÒ   rÓ   r   rÑ   rK   r   r   rA   r   r	   e  s¨    "éþýüûúùø	÷
öõôóòñðïîíìëêéè3þýüû
ú60)Tr
   )r   r   Nr   Tr
   )rÌ   Útypingr   r   r   r   r   r   r   Útorch.nnr   rb   Ú__all__r%   rÊ   Ústrr   r   r&   r(   r5   ÚModuler6   rL   rg   rw   rˆ   r˜   r	   r   r   r   r   Ú<module>   sX   ÿ$øÿþýüûúùø	
÷.1W=H   C