§
    @Äjp·  ã                   ó  — 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„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„Zdedefd„Z G d„ dej        ¦  «        Z G d„ dej        ¦  «        Z G d„ dej        ¦  «        Z G d„ dej        ¦  «        Z G d „ d!ej        ¦  «        Z G d"„ d#ej        ¦  «        Z G 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                 óä   — t           j                             | ||¬¦  «        }t           j        j                             |j        t           j        j                             |¦  «        ¬¦  «         |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   s        úLC:\Projects\Historical\venv\Lib\site-packages\torchaudio/models/tacotron2.pyÚ_get_linear_layerr   )   sT   € õ ŒX�_Š_˜V W°4ˆ_Ñ8Ô8€FÝ	„H„M×!Ò! &¤-µe´h´m×6RÒ6RÐS^Ñ6_Ô6_Ð!Ñ`Ô`Ð`Ø€Mó    é   Úin_channelsÚout_channelsÚkernel_sizeÚstrideÚpaddingÚdilationc           	      óP  — |€0|dz  dk    rt          d¦  «        ‚t          ||dz
  z  dz  ¦  «        }t          j                             | ||||||¬¦  «        }t          j        j                             |j        t          j        j                             |¦  «        ¬¦  «         |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   Úconv1ds	            r   Ú_get_conv1d_layerr+   ;   sª   € ð4 €Ø˜‰?˜aÒÐÝÐ6Ñ7Ô7Ð7Ý�h +°¡/Ñ2°QÑ6Ñ7Ô7ˆåŒX�_Š_ØØØØØØØð ñ ô €Fõ 
„H„M×!Ò! &¤-µe´h´m×6RÒ6RÐS^Ñ6_Ô6_Ð!Ñ`Ô`Ð`à€Mr   Úlengthsc                 ó  — t          j        | ¦  «                             ¦   «         }t          j        d|| j        | j        ¬¦  «        }||                      d¦  «        k                          ¦   «         }t          j        |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Úmasks       r   Ú_get_mask_from_lengthsr9   i   su   € õ Œi˜Ñ Ô ×%Ò%Ñ'Ô'€GÝ
Œ,�q˜'¨'¬.ÀÄÐ
NÑ
NÔ
N€CØ�'×#Ò# AÑ&Ô&Ò&×,Ò,Ñ.Ô.€DÝŒ8�D˜!ÑÔ€DØ€Kr   c                   ó@   ‡ — e Zd ZdZdededefˆ fd„Zdedefd„Zˆ x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           	      óØ   •— t          ¦   «                              ¦   «          t          |dz
  dz  ¦  «        }t          d|||ddd¬¦  «        | _        t          ||dd¬¦  «        | _        d S )Nr   r&   F)r!   r#   r   r"   r$   Útanh©r   r   )ÚsuperÚ__init__r(   r+   Úlocation_convr   Úlocation_dense)Úselfr<   r=   r>   r#   Ú	__class__s        €r   rC   z_LocationLayer.__init__ƒ   sˆ   ø€ õ 	‰Œ×ÒÑÔÐÝÐ,¨qÑ0°AÑ5Ñ6Ô6ˆÝ.ØØØ-ØØØØð
ñ 
ô 
ˆÔõ 0ØÐ 4¸5Èfð
ñ 
ô 
ˆÔÐÐr   Úattention_weights_catr   c                 ó†   — |                       |¦  «        }|                     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&   )rD   Ú	transposerE   )rF   rH   Úprocessed_attentions      r   Úforwardz_LocationLayer.forward˜   sJ   € ð #×0Ò0Ð1FÑGÔGÐØ1×;Ò;¸A¸qÑAÔAÐà"×1Ò1Ð2EÑFÔFÐØ"Ð"r   ©	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r(   rC   r   rL   Ú__classcell__©rG   s   @r   r;   r;   z   s…   ø€ € € € € ðð ð
àð
ð  #ð
ð "ð	
ð 
ð 
ð 
ð 
ð 
ð*#¨Vð #¸ð #ð #ð #ð #ð #ð #ð #ð #r   r;   c                   ó„   ‡ — e Zd ZdZdedededededdfˆ fd	„Zd
edededefd„Zdedededededeeef         fd„Z	ˆ x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_dimr>   Úattention_location_n_filterÚattention_location_kernel_sizer   Nc                 ó,  •— t          ¦   «                              ¦   «          t          ||dd¬¦  «        | _        t          ||dd¬¦  «        | _        t          |dd¬¦  «        | _        t          |||¦  «        | _        t          d¦  «         | _	        d S )NFr@   rA   r   r   Úinf)
rB   rC   r   Úquery_layerÚmemory_layerÚvr;   Úlocation_layerÚfloatÚscore_mask_value)rF   rV   rW   r>   rX   rY   rG   s         €r   rC   z_Attention.__init__¶   s¦   ø€ õ 	‰Œ×ÒÑÔÐÝ,Ð->Ð@TÐ[`ÐntÐuÑuÔuˆÔÝ-Ø!Ð#7¸eÐQWð
ñ 
ô 
ˆÔõ #Ð#7¸ÀÐGÑGÔGˆŒÝ,Ø'Ø*Ø ñ
ô 
ˆÔõ
 "' u¡¤ ˆÔÐÐr   ÚqueryÚprocessed_memoryrH   c                 ó  — |                       |                     d¦  «        ¦  «        }|                      |¦  «        }|                      t	          j        ||z   |z   ¦  «        ¦  «        }|                     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&   )r\   r3   r_   r^   r   r@   Úsqueeze)rF   rb   rc   rH   Úprocessed_queryÚprocessed_attention_weightsÚenergiesÚ	alignments           r   Ú_get_alignment_energiesz"_Attention._get_alignment_energiesË   sx   € ð ×*Ò*¨5¯?ª?¸1Ñ+=Ô+=Ñ>Ô>ˆØ&*×&9Ò&9Ð:OÑ&PÔ&PÐ#Ø—6’6�%œ* _Ð7RÑ%RÐUeÑ%eÑfÔfÑgÔgˆà×$Ò$ QÑ'Ô'ˆ	ØÐr   Úattention_hidden_stateÚmemoryr8   c                 ó  — |                       |||¦  «        }|                     || j        ¦  «        }t          j        |d¬¦  «        }t          j        |                     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)	rj   Úmasked_fillra   ÚFÚsoftmaxr   Úbmmr3   re   )	rF   rk   rl   rc   rH   r8   ri   Úattention_weightsÚattention_contexts	            r   rL   z_Attention.forwardà   sŒ   € ð. ×0Ò0Ð1GÐIYÐ[pÑqÔqˆ	à×)Ò)¨$°Ô0EÑFÔFˆ	åœI i°QÐ7Ñ7Ô7ÐÝ!œIÐ&7×&AÒ&AÀ!Ñ&DÔ&DÀfÑMÔMÐØ-×5Ò5°aÑ8Ô8Ðà Ð"3Ð3Ð3r   )rN   rO   rP   rQ   r(   rC   r   rj   r   rL   rR   rS   s   @r   rU   rU   «   s
  ø€ € € € € ðð ð.àð.ð  #ð.ð "ð	.ð
 &)ð.ð ),ð.ð 
ð.ð .ð .ð .ð .ð .ð*¨Vð Àvð Ðflð Ðqwð ð ð ð ð*4à &ð4ð ð4ð !ð	4ð
  &ð4ð ð4ð 
ˆv�vˆ~Ô	ð4ð 4ð 4ð 4ð 4ð 4ð 4ð 4r   rU   c                   óL   ‡ — e Zd ZdZdedee         ddfˆ fd„Zdedefd„Zˆ x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                 óÆ   •— t          ¦   «                              ¦   «          |g|d d…         z   }t          j        d„ t	          ||¦  «        D ¦   «         ¦  «        | _        d S )Néÿÿÿÿc                 ó8   — g | ]\  }}t          ||d ¬¦  «        ‘ŒS )Fr   )r   )Ú.0Úin_sizeÚout_sizes      r   ú
<listcomp>z$_Prenet.__init__.<locals>.<listcomp>  s-   € ÐrÐrÐrÑBUÀ7ÈHÕ˜w¨°uÐ=Ñ=Ô=ÐrÐrÐrr   )rB   rC   r   Ú
ModuleListÚzipÚlayers)rF   r   rx   Úin_sizesrG   s       €r   rC   z_Prenet.__init__
  sb   ø€ Ý‰Œ×ÒÑÔÐØ�8˜i¨¨¨œnÑ,ˆÝ”mØrÐrÕY\Ð]eÐgpÑYqÔYqÐrÑrÔrñ
ô 
ˆŒˆˆr   Úxc                 ó~   — | j         D ]4}t          j        t          j         ||¦  «        ¦  «        dd¬¦  «        }Œ5|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)r‚   rq   ÚdropoutÚrelu)rF   r„   r   s      r   rL   z_Prenet.forward  sF   € ð ”kð 	Cð 	CˆFÝ”	�!œ&  ¨¡¤Ñ+Ô+¨s¸TÐBÑBÔBˆAˆAØˆr   )
rN   rO   rP   rQ   r(   r   rC   r   rL   rR   rS   s   @r   rw   rw     s‚   ø€ € € € € ðð ð
˜sð 
¨t°C¬yð 
¸Tð 
ð 
ð 
ð 
ð 
ð 
ð˜ð  Fð ð ð ð ð ð ð ð r   rw   c                   óD   ‡ — e Zd ZdZdedededefˆ fd„Zdedefd	„Zˆ x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                 óü  •— t          ¦   «                              ¦   «          t          j        ¦   «         | _        t          |¦  «        D ]™}|dk    r|n|}||dz
  k    r|n|}||dz
  k    rdnd}||dz
  k    r|n|}	| j                             t          j        t          |||dt          |dz
  dz  ¦  «        d|¬¦  «        t          j
        |	¦  «        ¦  «        ¦  «         Œšt          | j        ¦  «        | _        d S )Nr   r   r   r@   r&   ©r!   r"   r#   r$   r   )rB   rC   r   r€   ÚconvolutionsÚrangeÚappendÚ
Sequentialr+   r(   ÚBatchNorm1dÚlenÚn_convs)rF   r�   rŽ   r�   r�   Úir   r    Ú	init_gainÚnum_featuresrG   s             €r   rC   z_Postnet.__init__*  s0  ø€ õ 	‰Œ×ÒÑÔÐÝœM™OœOˆÔåÐ,Ñ-Ô-ð 	ð 	ˆAØ$%¨¢F F˜&˜&Ð0EˆKØ%&Ð+@À1Ñ+DÒ%EÐ%E˜6˜6ÐK`ˆLØ$%Ð*?À!Ñ*CÒ$DÐ$D˜˜È&ˆIØ%&Ð+@À1Ñ+DÒ%EÐ%E˜6˜6ÐK`ˆLØÔ×$Ò$Ý”Ý%Ø#Ø$Ø$7Ø Ý #Ð%8¸1Ñ%<ÀÑ$AÑ BÔ BØ!"Ø$-ðñ ô õ ”N <Ñ0Ô0ñô ñô ð ð õ ˜4Ô,Ñ-Ô-ˆŒˆˆr   r„   r   c                 ó  — t          | j        ¦  «        D ]p\  }}|| j        dz
  k     r8t          j        t          j         ||¦  «        ¦  «        d| j        ¬¦  «        }ŒKt          j         ||¦  «        d| j        ¬¦  «        }Œ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   r†   )rˆ   )Ú	enumerater“   r™   rq   r‰   r   r@   rˆ   )rF   r„   rš   Úconvs       r   rL   z_Postnet.forwardJ  s‰   € õ ! Ô!2Ñ3Ô3ð 	Dð 	D‰GˆAˆtØ�4”< !Ñ#Ò#Ð#Ý”I�eœj¨¨¨a©¬Ñ1Ô1°3ÀÄÐOÑOÔO��å”I˜d˜d 1™gœg s°T´]ÐCÑCÔC��àˆr   rM   rS   s   @r   rŒ   rŒ      s�   ø€ € € € € ðð ð.àð.ð  #ð.ð !ð	.ð
  #ð.ð .ð .ð .ð .ð .ð@˜ð  Fð ð ð ð ð ð ð ð r   rŒ   c                   óH   ‡ — e Zd ZdZdedededdfˆ fd„Zded	edefd
„Zˆ x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)
    rW   Úencoder_n_convolutionÚencoder_kernel_sizer   Nc                 óú  •— t          ¦   «                              ¦   «          t          j        ¦   «         | _        t          |¦  «        D ]j}t          j        t          |||dt          |dz
  dz  ¦  «        dd¬¦  «        t          j	        |¦  «        ¦  «        }| j         
                    |¦  «         Œkt          j        |t          |dz  ¦  «        ddd¬¦  «        | _        | j                             ¦   «          d S )Nr   r&   rŠ   r’   T)Úbatch_firstÚbidirectional)rB   rC   r   r€   r“   r”   r–   r+   r(   r—   r•   ÚLSTMÚlstmÚflatten_parameters)rF   rW   r¢   r£   Ú_Ú
conv_layerrG   s         €r   rC   z_Encoder.__init__k  s	  ø€ õ 	‰Œ×ÒÑÔÐåœM™OœOˆÔÝÐ,Ñ-Ô-ð 	1ð 	1ˆAÝœÝ!Ø)Ø)Ø 3ØÝÐ!4°qÑ!8¸AÑ =Ñ>Ô>ØØ &ðñ ô õ ”Ð4Ñ5Ô5ñô ˆJð Ô×$Ò$ ZÑ0Ô0Ð0Ð0å”GØ!ÝÐ%¨Ñ)Ñ*Ô*ØØØð
ñ 
ô 
ˆŒ	ð 	Œ	×$Ò$Ñ&Ô&Ð&Ð&Ð&r   r„   Úinput_lengthsc                 óª  — | j         D ]8}t          j        t          j         ||¦  «        ¦  «        d| j        ¦  «        }Œ9|                     dd¦  «        }|                     ¦   «         }t          j        j	         
                    ||d¬¦  «        }|                      |¦  «        \  }}t          j        j	                             |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).
        r†   r   r&   T)r¥   )r“   rq   r‰   rŠ   rˆ   rJ   Úcpur   ÚutilsÚrnnÚpack_padded_sequencer¨   Úpad_packed_sequence)rF   r„   r¬   rŸ   Úoutputsrª   s         r   rL   z_Encoder.forwardŒ  s·   € ð Ô%ð 	?ð 	?ˆDÝ”	�!œ&   a¡¤™/œ/¨3°´Ñ>Ô>ˆAˆAà�KŠK˜˜1ÑÔˆà%×)Ò)Ñ+Ô+ˆÝŒHŒL×-Ò-¨a°ÈDÐ-ÑQÔQˆà—Y’Y˜q‘\”\‰
ˆ�Ý”X”\×5Ò5°gÈ4Ð5ÑPÔP‰
ˆ�àˆr   rM   rS   s   @r   r¡   r¡   ]  s—   ø€ € € € € ðð ð'à"ð'ð  #ð'ð !ð	'ð
 
ð'ð 'ð 'ð 'ð 'ð 'ðB˜ð °ð ¸6ð ð ð ð ð ð ð ð r   r¡   c            !       ó²  ‡ — 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„Zdedefd„Z	dede
eeeeeeeef         fd„Zdedefd„Zdededede
eeef         f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&„Zded'ed(ede
eeef         fd)„Zdedefd*„Zej        j        ded(ede
eeeef         fd+„¦   «         Zˆ xZS ),Ú_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
    r�   Ún_frames_per_steprW   Údecoder_rnn_dimÚdecoder_max_stepÚdecoder_dropoutÚdecoder_early_stoppingrV   r>   rX   rY   Úattention_dropoutÚ
prenet_dimÚgate_thresholdr   Nc                 ó(  •— t          ¦   «                              ¦   «          || _        || _        || _        || _        || _        || _        || _        || _	        || _
        || _        || _        t          ||z  ||g¦  «        | _        t          j        ||z   |¦  «        | _        t%          |||	|
|¦  «        | _        t          j        ||z   |d¦  «        | _        t+          ||z   ||z  ¦  «        | _        t+          ||z   ddd¬¦  «        | _        d S )NTr   ÚsigmoidrA   )rB   rC   r�   r¶   rW   rV   r·   r¼   r¸   r½   r»   r¹   rº   rw   Úprenetr   ÚLSTMCellÚattention_rnnrU   Úattention_layerÚdecoder_rnnr   Úlinear_projectionÚ
gate_layer)rF   r�   r¶   rW   r·   r¸   r¹   rº   rV   r>   rX   rY   r»   r¼   r½   rG   s                  €r   rC   z_Decoder.__init__¹  s2  ø€ õ$ 	‰Œ×ÒÑÔÐØˆŒØ!2ˆÔØ%:ˆÔ"Ø!2ˆÔØ.ˆÔØ$ˆŒØ 0ˆÔØ,ˆÔØ!2ˆÔØ.ˆÔØ&<ˆÔ#å˜fÐ'8Ñ8¸:ÀzÐ:RÑSÔSˆŒåœ[¨Ð6KÑ)KÐM^Ñ_Ô_ˆÔå)ØØ!Ø Ø'Ø*ñ 
ô  
ˆÔõ œ;Ð'8Ð;PÑ'PÐRaÐcgÑhÔhˆÔå!2°?ÐEZÑ3ZÐ\bÐevÑ\vÑ!wÔ!wˆÔå+ØÐ3Ñ3°Q¸TÈyð
ñ 
ô 
ˆŒˆˆr   rl   c                 ó–   — |                      d¦  «        }|j        }|j        }t          j        || j        | j        z  ||¬¦  «        }|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   Úzerosr�   r¶   ©rF   rl   Ún_batchr/   r.   Údecoder_inputs         r   Ú_get_initial_framez_Decoder._get_initial_frameì  óK   € ð —+’+˜a‘.”.ˆØ”ˆØ”ˆÝœ G¨T¬[¸4Ô;QÑ-QÐY^ÐgmÐnÑnÔnˆØÐr   c                 ó<  — |                      d¦  «        }|                      d¦  «        }|j        }|j        }t          j        || j        ||¬¦  «        }t          j        || j        ||¬¦  «        }t          j        || j        ||¬¦  «        }t          j        || j        ||¬¦  «        }	t          j        ||||¬¦  «        }
t          j        ||||¬¦  «        }t          j        || j        ||¬¦  «        }| 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Ë   rV   r·   rW   rÃ   r]   )rF   rl   rÍ   Úmax_timer/   r.   Úattention_hiddenÚattention_cellÚdecoder_hiddenÚdecoder_cellrt   Úattention_weights_cumru   rc   s                 r   Ú_initialize_decoder_statesz#_Decoder._initialize_decoder_statesý  s-  € ð* —+’+˜a‘.”.ˆØ—;’;˜q‘>”>ˆØ”ˆØ”ˆå œ; w°Ô0FÈeÐ\bÐcÑcÔcÐÝœ W¨dÔ.DÈEÐZ`ÐaÑaÔaˆåœ W¨dÔ.BÈ%ÐX^Ð_Ñ_Ô_ˆÝ”{ 7¨DÔ,@ÈÐV\Ð]Ñ]Ô]ˆå!œK¨°ÀÈvÐVÑVÔVÐÝ %¤¨G°XÀUÐSYÐ ZÑ ZÔ ZÐÝ!œK¨°Ô1KÐSXÐagÐhÑhÔhÐàÔ/×<Ò<¸VÑDÔDÐð ØØØØØ!ØØð	
ð 		
r   Údecoder_inputsc                 ó  — |                      dd¦  «        }|                     |                     d¦  «        t          |                     d¦  «        | j        z  ¦  «        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   rz   )rJ   ÚviewrÊ   r(   r¶   )rF   rÙ   s     r   Ú_parse_decoder_inputsz_Decoder._parse_decoder_inputs.  s€   € ð (×1Ò1°!°QÑ7Ô7ˆØ'×,Ò,Ø×Ò Ñ"Ô"Ý�×#Ò# AÑ&Ô&¨Ô)?Ñ?Ñ@Ô@Øñ
ô 
ˆð (×1Ò1°!°QÑ7Ô7ˆØÐr   Úmel_specgramÚgate_outputsÚ
alignmentsc                 óf  — |                      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   rz   r&   )rJ   Ú
contiguousÚshaper�   rÛ   )rF   rÝ   rÞ   rß   râ   s        r   Ú_parse_decoder_outputsz_Decoder._parse_decoder_outputsC  s®   € ð&  ×)Ò)¨!¨QÑ/Ô/×:Ò:Ñ<Ô<ˆ
à#×-Ò-¨a°Ñ3Ô3×>Ò>Ñ@Ô@ˆà#×-Ò-¨a°Ñ3Ô3×>Ò>Ñ@Ô@ˆàÔ# AÔ&¨¨D¬KÐ8ˆØ(�|Ô(¨%Ð0ˆà#×-Ò-¨a°Ñ3Ô3ˆà˜\¨:Ð5Ð5r   rÎ   rÓ   rÔ   rÕ   rÖ   rt   r×   ru   rc   r8   c           	      ó¢  — t          j        ||fd¦  «        }|                      |||f¦  «        \  }}t          j        || j        | j        ¦  «        }t          j        |                     d¦  «        |                     d¦  «        fd¬¦  «        }|                      ||	|
||¦  «        \  }}||z  }t          j        ||fd¦  «        }|  	                    |||f¦  «        \  }}t          j        || j
        | j        ¦  «        }t          j        ||f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``).
        rz   r   rn   )r   ÚcatrÂ   rq   r‰   r»   rˆ   r3   rÃ   rÄ   r¹   rÅ   rÆ   )rF   rÎ   rÓ   rÔ   rÕ   rÖ   rt   r×   ru   rl   rc   r8   Ú
cell_inputrH   Ú decoder_hidden_attention_contextÚdecoder_outputÚgate_predictions                    r   Údecodez_Decoder.decodec  s‚  € õR ”Y Ð/@ÐAÀ2ÑFÔFˆ
à+/×+=Ò+=¸jÐK[Ð]kÐJlÑ+mÔ+mÑ(Ð˜.Ýœ9Ð%5°tÔ7MÈtÌ}Ñ]Ô]Ðå %¤	Ð+<×+FÒ+FÀqÑ+IÔ+IÐK`×KjÒKjÐklÑKmÔKmÐ*nÐtuÐ vÑ vÔ vÐØ/3×/CÒ/CØ˜fÐ&6Ð8MÈtñ0
ô 0
Ñ,ÐÐ,ð 	Ð!2Ñ2ÐÝœ	Ð#3Ð5FÐ"GÈÑLÔLˆà'+×'7Ò'7¸ÈÐXdÐGeÑ'fÔ'fÑ$ˆ˜Ýœ >°4Ô3GÈÌÑWÔWˆå+0¬9°nÐFWÐ5XÐ^_Ð+`Ñ+`Ô+`Ð(Ø×/Ò/Ð0PÑQÔQˆàŸ/š/Ð*JÑKÔKˆð ØØØØØØØ!Øð

ð 
	
r   Úmel_specgram_truthÚmemory_lengthsc                 óx  — |                       |¦  «                             d¦  «        }|                      |¦  «        }t          j        ||fd¬¦  «        }|                      |¦  «        }t          |¦  «        }|                      |¦  «        \  }}}	}
}}}}g g g }}}t          |¦  «        | 	                    d¦  «        dz
  k     rŸ|t          |¦  «                 }|  
                    ||||	|
||||||¦  «        \	  }}}}}	}
}}}||                     d¦  «        gz  }||                     d¦  «        gz  }||gz  }t          |¦  «        | 	                    d¦  «        dz
  k     °Ÿ|                      t          j        |¦  «        t          j        |¦  «        t          j        |¦  «        ¦  «        \  }}}|||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   rn   r   )rÏ   r3   rÜ   r   rå   rÀ   r9   rØ   r˜   rÊ   rê   re   rã   Ústack)rF   rl   rë   rì   rÎ   rÙ   r8   rÓ   rÔ   rÕ   rÖ   rt   r×   ru   rc   Úmel_outputsrÞ   rß   Ú
mel_outputÚgate_outputrÝ   s                        r   rL   z_Decoder.forward­  sû  € ð, ×/Ò/°Ñ7Ô7×AÒAÀ!ÑDÔDˆØ×3Ò3Ð4FÑGÔGˆÝœ M°>Ð#BÈÐJÑJÔJˆØŸš ^Ñ4Ô4ˆå% nÑ5Ô5ˆð ×+Ò+¨FÑ3Ô3ñ		
ØØØØØØ!ØØð 13°B¸ :�\ˆÝ�+ÑÔ ×!4Ò!4°QÑ!7Ô!7¸!Ñ!;Ò;Ð;Ø*­3¨{Ñ+;Ô+;Ô<ˆMð —’ØØ ØØØØ!Ø%Ø!ØØ Øñô ñ
ØØØ ØØØØ!Ø%Ø!ð ˜J×.Ò.¨qÑ1Ô1Ð2Ñ2ˆKØ˜[×0Ò0°Ñ3Ô3Ð4Ñ4ˆLØÐ,Ð-Ñ-ˆJõ9 �+ÑÔ ×!4Ò!4°QÑ!7Ô!7¸!Ñ!;Ò;Ð;ð< 26×1LÒ1LÝŒK˜Ñ$Ô$¥e¤k°,Ñ&?Ô&?ÅÄÈZÑAXÔAXñ2
ô 2
Ñ.ˆ�l Jð ˜\¨:Ð5Ð5r   c                 ó–   — |                      d¦  «        }|j        }|j        }t          j        || j        | j        z  ||¬¦  «        }|S )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Ì   s         r   Ú_get_go_framez_Decoder._get_go_frameù  rÐ   r   c                 ó°  — |                      d¦  «        |j        }}|                      |¦  «        }t          |¦  «        }|                      |¦  «        \  }}}	}
}}}}t          j        |gt
          j        |¬¦  «        }t          j        |gt
          j        |¬¦  «        }g }g }g }t          | j
        ¦  «        D �]
}|                      |¦  «        }|                      ||||	|
||||||¦  «        \	  }}}}}	}
}}}|                     |                     d¦  «        ¦  «         |                     |                     dd¦  «        ¦  «         |                     |¦  «         || xx         dz  cc<   |t          j        |                     d¦  «        ¦  «        | j        k    z  }| j        rt          j        |¦  «        r n|}�Œt+          |¦  «        | j
        k    rt-          j        d¦  «         t          j        |d¬¦  «        }t          j        |d¬¦  «        }t          j        |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.rn   )rÊ   r.   ró   r9   rØ   r   rË   Úint32Úboolr”   r¸   rÀ   rê   r•   r3   rJ   r¿   re   r½   rº   Úallr˜   ÚwarningsÚwarnrå   rã   )rF   rl   rì   Ú
batch_sizer.   rÎ   r8   rÓ   rÔ   rÕ   rÖ   rt   r×   ru   rc   Úmel_specgram_lengthsÚfinishedÚmel_specgramsrÞ   rß   rª   rÝ   rñ   s                          r   Úinferz_Decoder.infer
  s—  € ð& $Ÿ[š[¨™^œ^¨V¬]�Fˆ
à×*Ò*¨6Ñ2Ô2ˆå% nÑ5Ô5ˆð ×+Ò+¨FÑ3Ô3ñ		
ØØØØØØ!ØØõ  %œ{¨J¨<½u¼{ÐSYÐZÑZÔZÐÝ”; 
˜|µ5´:ÀfÐMÑMÔMˆØ&(ˆØ%'ˆØ#%ˆ
Ý�tÔ,Ñ-Ô-ð #	)ñ #	)ˆAØ ŸKšK¨Ñ6Ô6ˆMð —’ØØ ØØØØ!Ø%Ø!ØØ Øñô ñ
ØØØ ØØØØ!Ø%Ø!ð × Ò  ×!7Ò!7¸Ñ!:Ô!:Ñ;Ô;Ð;Ø×Ò × 5Ò 5°a¸Ñ ;Ô ;Ñ<Ô<Ð<Ø×ÒÐ/Ñ0Ô0Ð0Ø  ( Ð+Ð+Ô+¨qÑ0Ð+Ð+Ñ+à�œ k×&9Ò&9¸!Ñ&<Ô&<Ñ=Ô=ÀÔ@SÒSÑSˆHØÔ*ð ­u¬y¸Ñ/BÔ/Bð Ø�à(ˆM‰Måˆ}ÑÔ Ô!6Ò6Ð6ÝŒMØoñô ð õ œ	 -°QÐ7Ñ7Ô7ˆÝ”y °1Ð5Ñ5Ô5ˆÝ”Y˜z¨qÐ1Ñ1Ô1ˆ
à26×2MÒ2MÈmÐ]iÐkuÑ2vÔ2vÑ/ˆ�| ZàÐ2°LÀ*ÐLÐLr   )rN   rO   rP   rQ   r(   r`   rö   rC   r   rÏ   r   rØ   rÜ   rã   rê   rL   ró   r   ÚjitÚexportrþ   rR   rS   s   @r   rµ   rµ   ¥  s  ø€ € € € € ðð ð&1
àð1
ð ð1
ð  #ð	1
ð
 ð1
ð ð1
ð ð1
ð !%ð1
ð ð1
ð "ð1
ð &)ð1
ð ),ð1
ð !ð1
ð ð1
ð ð1
ð  
ð!1
ð 1
ð 1
ð 1
ð 1
ð 1
ðf¨ð °Fð ð ð ð ð"/
Øð/
à	ˆv�v˜v v¨v°v¸vÀvÐMÔ	Nð/
ð /
ð /
ð /
ðb°Fð ¸vð ð ð ð ð*6Ø"ð6Ø28ð6ØFLð6à	ˆv�v˜vÐ%Ô	&ð6ð 6ð 6ð 6ð@H
àðH
ð !ðH
ð ð	H
ð
 ðH
ð ðH
ð "ðH
ð  &ðH
ð "ðH
ð ðH
ð !ðH
ð ðH
ð 
ˆv�v˜v v¨v°v¸vÀvÈvÐUÔ	VðH
ð H
ð H
ð H
ðTJ6ØðJ6Ø28ðJ6ØJPðJ6à	ˆv�v˜vÐ%Ô	&ðJ6ð J6ð J6ð J6ðX Fð ¨vð ð ð ð ð" „YÔðWM˜Fð WM°Fð WM¸uÀVÈVÐU[Ð]cÐEcÔ?dð WMð WMð WMñ ÔðWMð WMð WMð WMð WMr   rµ   c            /       ó6  ‡ — e Zd ZdZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d2d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*„Zd+ed,ed-ed.ed(e	eeeef         f
d/„Z
ej        j        d3d+ed0ee         d(e	eeef         fd1„¦   «         Zˆ xZS )4r
   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é€   é    é   é   r†   Úmask_paddingr�   Ún_symbolr¶   Úsymbol_embedding_dimrW   r¢   r£   r·   r¸   r¹   rº   rV   r>   rX   rY   r»   r¼   r�   r�   rŽ   r½   r   Nc                 óž  •— t          ¦   «                              ¦   «          || _        || _        || _        t          j        ||¦  «        | _        t          j        j	         
                    | j        j        ¦  «         t          |||¦  «        | _        t          ||||	|
|||||||||¦  «        | _        t!          ||||¦  «        | _        d S ©N)rB   rC   r  r�   r¶   r   Ú	EmbeddingÚ	embeddingr   r   r   r   r¡   Úencoderrµ   ÚdecoderrŒ   Úpostnet)rF   r  r�   r  r¶   r  rW   r¢   r£   r·   r¸   r¹   rº   rV   r>   rX   rY   r»   r¼   r�   r�   rŽ   r½   rG   s                          €r   rC   zTacotron2.__init__†  sÒ   ø€ õ2 	‰Œ×ÒÑÔÐà(ˆÔØˆŒØ!2ˆÔÝœ hÐ0DÑEÔEˆŒÝŒŒ×%Ò% d¤nÔ&;Ñ<Ô<Ð<ÝÐ 5Ð7LÐNaÑbÔbˆŒÝØØØ!ØØØØ"ØØ Ø'Ø*ØØØñ
ô 
ˆŒõ    Ð(=Ð?RÐTiÑjÔjˆŒˆˆr   ÚtokensÚtoken_lengthsrÝ   rû   c                 ót  — |                       |¦  «                             dd¦  «        }|                      ||¦  «        }|                      |||¬¦  «        \  }}}|                      |¦  «        }	||	z   }	| j        r·t          |¦  «        }
|
                     | 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  rJ   r  r  r  r  r9   Úexpandr�   rÊ   ÚpermuteÚmasked_fill_)rF   r  r  rÝ   rû   Úembedded_inputsÚencoder_outputsrÞ   rß   Úmel_specgram_postnetr8   s              r   rL   zTacotron2.forward¹  s<  € ðB Ÿ.š.¨Ñ0Ô0×:Ò:¸1¸aÑ@Ô@ˆàŸ,š, ¸ÑFÔFˆØ15·²Ø˜\¸-ð 2>ñ 2
ô 2
Ñ.ˆ�l Jð  $Ÿ|š|¨LÑ9Ô9ÐØ+Ð.BÑBÐàÔð 	:Ý)Ð*>Ñ?Ô?ˆDØ—;’;˜tœ{¨D¯IªI°a©L¬L¸$¿)º)ÀA¹,¼,ÑGÔGˆDØ—<’<  1 aÑ(Ô(ˆDà×%Ò% d¨CÑ0Ô0Ð0Ø ×-Ò-¨d°CÑ8Ô8Ð8Ø×%Ò% d¨1¨1¨1¨a°°°¨7¤m°SÑ9Ô9Ð9àÐ1°<ÀÐKÐKr   r,   c                 ó   — |j         \  }}|€Ft          j        |g¦  «                             |¦  «                             |j        |j        ¦  «        }|€J ‚|                      |¦  «                             dd¦  «        }|  	                    ||¦  «        }| j
                             ||¦  «        \  }}}	}
|                      |¦  «        }||z   }|
                     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  rJ   r  r  rþ   r  Úunfold)rF   r  r,   rÍ   Ú
max_lengthr  r  rÝ   rû   rª   rß   Úmel_outputs_postnets               r   rþ   zTacotron2.inferï  sÿ   € ð2 %œlÑˆ�Øˆ?Ý”l J <Ñ0Ô0×7Ò7¸Ñ@Ô@×CÒCÀFÄMÐSYÔS_Ñ`Ô`ˆGàÐ"Ð"Ð"ØŸ.š.¨Ñ0Ô0×:Ò:¸1¸aÑ@Ô@ˆØŸ,š, ¸Ñ@Ô@ˆØ<@¼L×<NÒ<NÈÐ`gÑ<hÔ<hÑ9ˆÐ*¨A¨zà"Ÿlšl¨<Ñ8Ô8ÐØ*Ð-@Ñ@Ðà×&Ò& q¨'°7Ñ;Ô;×EÒEÀaÈÑKÔKˆ
à"Ð$8¸*ÐDÐDr   )Fr  r  r   r  r  r  r  r  r  r	  Tr  r
  r  r  r	  r  r  r  r  r†   r  )rN   rO   rP   rQ   rö   r(   r`   rC   r   r   rL   r   rÿ   r   r   rþ   rR   rS   s   @r   r
   r
   e  sO  ø€ € € € € ðð ðD #ØØØ!"Ø$'Ø%(Ø%&Ø#$Ø#Ø $Ø!$Ø'+Ø!%Ø$'Ø+-Ø.0Ø#&ØØ%&Ø#$Ø%(Ø #ð/1kð 1kàð1kð ð1kð ð	1kð
 ð1kð "ð1kð  #ð1kð  #ð1kð !ð1kð ð1kð ð1kð ð1kð !%ð1kð ð1kð "ð1kð  &)ð!1kð" ),ð#1kð$ !ð%1kð& ð'1kð(  #ð)1kð* !ð+1kð,  #ð-1kð. ð/1kð0 
ð11kð 1kð 1kð 1kð 1kð 1kðf4Làð4Lð ð4Lð ð	4Lð
 %ð4Lð 
ˆv�v˜v vÐ-Ô	.ð4Lð 4Lð 4Lð 4Lðl „YÔð&Eð &E˜Fð &E¨X°fÔ-=ð &EÈÈvÐW]Ð_eÐOeÔIfð &Eð &Eð &Eñ Ôð&Eð &Eð &Eð &Eð &Er   )Tr   )r   r   Nr   Tr   )rø   Útypingr   r   r   r   r   r   r   Útorch.nnr	   rq   Ú__all__r(   rö   Ústrr   r   r)   r+   r9   ÚModuler;   rU   rw   rŒ   r¡   rµ   r
   © r   r   ú<module>r-     sí  ðð8 €€€Ø /Ð /Ð /Ð /Ð /Ð /Ð /Ð /Ð /Ð /Ð /Ð /à €€€Ø Ð Ð Ð Ð Ð Ð Ð Ø $Ð $Ð $Ð $Ð $Ð $ð ð€ð
ð ˜cð ¨Cð °tð ÐQTð ÐdiÔdlÔdsð ð ð ð ð* ØØ59ØØØð+ð +Øð+àð+ð ð+ð ð	+ð
 �e˜C  e¨C¤jÐ0Ô1Ô2ð+ð ð+ð ð+ð ð+ð „X„_ð+ð +ð +ð +ð\ Fð ¨vð ð ð ð ð".#ð .#ð .#ð .#ð .#�R”Yñ .#ô .#ð .#ðbT4ð T4ð T4ð T4ð T4�”ñ T4ô T4ð T4ðnð ð ð ð ˆbŒiñ ô ð ð<:ð :ð :ð :ð :ˆrŒyñ :ô :ð :ðzEð Eð Eð Eð EˆrŒyñ Eô Eð EðP}Mð }Mð }Mð }Mð }MˆrŒyñ }Mô }Mð }Mð@qEð qEð qEð qEð qE�”	ñ qEô qEð qEð qEð qEr   