§
    @ÄjŽ  ã            ,       óô  — d dl mZmZ d dlmZmZmZ d dlZd dlm	Z	 g d¢Z
 G d„ dej        j        ¦  «        Z G d„ d	ej        j        ¦  «        Z G d
„ de¦  «        Z G d„ dej        j        e¦  «        Z G d„ dej        j        ¦  «        Z G d„ dej        j        ¦  «        Z G d„ dej        j        ¦  «        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"ed#ed$ed%ed&ed'ed(ed)ef,d*„Zded)efd+„ZdS ),é    )ÚABCÚabstractmethod)ÚListÚOptionalÚTupleN)ÚEmformer)ÚRNNTÚemformer_rnnt_baseÚemformer_rnnt_modelc                   óx   ‡ — e Zd ZdZdeddfˆ fd„Zdej        dej        deej        ej        f         fd„Z	ˆ xZ
S )	Ú_TimeReductionzÂCoalesces frames along time dimension into a
    fewer number of frames with higher feature dimensionality.

    Args:
        stride (int): number of frames to merge for each output frame.
    ÚstrideÚreturnNc                 óV   •— t          ¦   «                              ¦   «          || _        d S ©N)ÚsuperÚ__init__r   )Úselfr   Ú	__class__s     €úGC:\Projects\Historical\venv\Lib\site-packages\torchaudio/models/rnnt.pyr   z_TimeReduction.__init__   s$   ø€ Ý‰Œ×ÒÑÔÐØˆŒˆˆó    ÚinputÚlengthsc                 ó  — |j         \  }}}||| j        z  z
  }|dd…d|…dd…f         }|                     | j        d¬¦  «        }|| j        z  }|                     |||| j        z  ¦  «        }|                     ¦   «         }||fS )a  Forward pass.

        B: batch size;
        T: maximum input sequence length in batch;
        D: feature dimension of each input sequence frame.

        Args:
            input (torch.Tensor): input sequences, with shape `(B, T, D)`.
            lengths (torch.Tensor): with shape `(B,)` and i-th element representing
                number of valid frames for i-th batch element in ``input``.

        Returns:
            (torch.Tensor, torch.Tensor):
                torch.Tensor
                    output sequences, with shape
                    `(B, T  // stride, D * stride)`
                torch.Tensor
                    output lengths, with shape `(B,)` and i-th element representing
                    number of valid frames for i-th batch element in output sequences.
        NÚtrunc)Úrounding_mode)Úshaper   ÚdivÚreshapeÚ
contiguous)	r   r   r   ÚBÚTÚDÚ
num_framesÚT_maxÚoutputs	            r   Úforwardz_TimeReduction.forward   s—   € ð* ”+‰ˆˆ1ˆaØ˜!˜dœk™/Ñ*ˆ
Ø�a�a�a˜˜*˜ a a aÐ'Ô(ˆØ—+’+˜dœk¸�+ÑAÔAˆØ˜dœkÑ)ˆà—’˜q %¨¨T¬[©Ñ9Ô9ˆØ×"Ò"Ñ$Ô$ˆØ�wˆÐr   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úintr   ÚtorchÚTensorr   r'   Ú__classcell__©r   s   @r   r   r      s’   ø€ € € € € ðð ð˜sð  tð ð ð ð ð ð ð˜Uœ\ð °E´Lð ÀUÈ5Ì<ÐY^ÔYeÐKeÔEfð ð ð ð ð ð ð ð r   r   c                   ó®   ‡ — e Zd ZdZ	 	 ddededededd	f
ˆ fd
„Zdej	        de
eej	                          deej	        eej	                 f         fd„Zˆ xZS )Ú_CustomLSTMa­  Custom long-short-term memory (LSTM) block that applies layer normalization
    to internal nodes.

    Args:
        input_dim (int): input dimension.
        hidden_dim (int): hidden dimension.
        layer_norm (bool, optional): if ``True``, enables layer normalization. (Default: ``False``)
        layer_norm_epsilon (float, optional):  value of epsilon to use in
            layer normalization layers (Default: 1e-5)
    Fçñhãˆµøä>Ú	input_dimÚ
hidden_dimÚ
layer_normÚlayer_norm_epsilonr   Nc                 ó0  •— t          ¦   «                              ¦   «          t          j                             |d|z  | ¬¦  «        | _        t          j                             |d|z  d¬¦  «        | _        |rPt          j                             ||¬¦  «        | _        t          j                             d|z  |¬¦  «        | _	        nFt          j         
                    ¦   «         | _        t          j         
                    ¦   «         | _	        || _        d S )Né   ©ÚbiasF)Úeps)r   r   r-   ÚnnÚLinearÚx2gÚp2gÚ	LayerNormÚc_normÚg_normÚIdentityr5   )r   r4   r5   r6   r7   r   s        €r   r   z_CustomLSTM.__init__C   sØ   ø€ õ 	‰Œ×ÒÑÔÐÝ”8—?’? 9¨a°*©nÈ
ÀN�?ÑTÔTˆŒÝ”8—?’? :¨q°:©~ÀE�?ÑJÔJˆŒØð 	.Ýœ(×,Ò,¨ZÐ=OÐ,ÑPÔPˆDŒKÝœ(×,Ò,¨Q°©^ÐASÐ,ÑTÔTˆDŒKˆKåœ(×+Ò+Ñ-Ô-ˆDŒKÝœ(×+Ò+Ñ-Ô-ˆDŒKà$ˆŒˆˆr   r   Ústatec                 ó:  — |€d|                      d¦  «        }t          j        || j        |j        |j        ¬¦  «        }t          j        || j        |j        |j        ¬¦  «        }n|\  }}|                      |¦  «        }g }|                     d¦  «        D ]æ}||                      |¦  «        z   }|  	                    |¦  «        }| 
                    dd¦  «        \  }	}
}}|	                     ¦   «         }	|
                     ¦   «         }
|                     ¦   «         }|                     ¦   «         }|
|z  |	|z  z   }|                      |¦  «        }||                     ¦   «         z  }|                     |¦  «         Œçt          j        |d¬¦  «        }||g}||fS )aÒ  Forward pass.

        B: batch size;
        T: maximum sequence length in batch;
        D: feature dimension of each input sequence element.

        Args:
            input (torch.Tensor): with shape `(T, B, D)`.
            state (List[torch.Tensor] or None): list of tensors
                representing internal state generated in preceding invocation
                of ``forward``.

        Returns:
            (torch.Tensor, List[torch.Tensor]):
                torch.Tensor
                    output, with shape `(T, B, hidden_dim)`.
                List[torch.Tensor]
                    list of tensors representing internal state generated
                    in current invocation of ``forward``.
        Né   )ÚdeviceÚdtyper   r9   )Údim)Úsizer-   Úzerosr5   rH   rI   r?   Úunbindr@   rC   ÚchunkÚsigmoidÚtanhrB   ÚappendÚstack)r   r   rE   r!   ÚhÚcÚgated_inputÚoutputsÚgatesÚ
input_gateÚforget_gateÚ	cell_gateÚoutput_gater&   s                 r   r'   z_CustomLSTM.forwardV   sƒ  € ð. ˆ=Ø—
’
˜1‘”ˆAÝ”˜A˜tœ°u´|È5Ì;ÐWÑWÔWˆAÝ”˜A˜tœ°u´|È5Ì;ÐWÑWÔWˆAˆAà‰DˆAˆqà—h’h˜u‘o”oˆØˆØ ×'Ò'¨Ñ*Ô*ð 	ð 	ˆEØ˜DŸHšH Q™KœKÑ'ˆEØ—K’K Ñ&Ô&ˆEØ>C¿kºkÈ!ÈQÑ>OÔ>OÑ;ˆJ˜ Y°Ø#×+Ò+Ñ-Ô-ˆJØ%×-Ò-Ñ/Ô/ˆKØ!ŸšÑ(Ô(ˆIØ%×-Ò-Ñ/Ô/ˆKØ˜a‘ *¨yÑ"8Ñ8ˆAØ—’˜A‘”ˆAØ˜aŸfšf™hœhÑ&ˆAØ�NŠN˜1ÑÔÐÐå”˜W¨!Ð,Ñ,Ô,ˆØ�A�ˆà�uˆ}Ðr   )Fr3   ©r(   r)   r*   r+   r,   ÚboolÚfloatr   r-   r.   r   r   r   r'   r/   r0   s   @r   r2   r2   7   sÑ   ø€ € € € € ð	ð 	ð !Ø$(ð%ð %àð%ð ð%ð ð	%ð
 "ð%ð 
ð%ð %ð %ð %ð %ð %ð&0Ø”\ð0Ø*2°4¸¼Ô3EÔ*Fð0à	ˆuŒ|˜T %¤,Ô/Ð/Ô	0ð0ð 0ð 0ð 0ð 0ð 0ð 0ð 0r   r2   c                   ó  — e Zd Zedej        dej        deej        ej        f         fd„¦   «         Zedej        dej        dee	e	ej                                   deej        ej        e	e	ej                          f         fd„¦   «         Z
dS )Ú_Transcriberr   r   r   c                 ó   — d S r   © )r   r   r   s      r   r'   z_Transcriber.forwardŠ   s   € àˆr   Ústatesc                 ó   — d S r   rb   )r   r   r   rc   s       r   Úinferz_Transcriber.inferŽ   s	   € ð 	ˆr   N)r(   r)   r*   r   r-   r.   r   r'   r   r   re   rb   r   r   r`   r`   ‰   sÒ   € € € € € Øð˜Uœ\ð °E´Lð ÀUÈ5Ì<ÐY^ÔYeÐKeÔEfð ð ð ñ „^ðð ðàŒ|ðð ”ðð ˜˜d 5¤<Ô0Ô1Ô2ð	ð
 
ˆuŒ|˜Uœ\¨4°°U´\Ô0BÔ+CÐCÔ	Dðð ð ñ „^ðð ð r   r`   c            !       ó„  ‡ — e Zd ZdZddddddœ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	j
        de	j
        dee	j
        e	j
        f         fd„Ze	j        j        de	j
        de	j
        deeee	j
                                   dee	j
        e	j
        eee	j
                          f         fd„¦   «         Zˆ xZS )Ú_EmformerEncodera½  Emformer-based recurrent neural network transducer (RNN-T) encoder (transcription network).

    Args:
        input_dim (int): feature dimension of each input sequence element.
        output_dim (int): feature dimension of each output sequence element.
        segment_length (int): length of input segment expressed as number of frames.
        right_context_length (int): length of right context expressed as number of frames.
        time_reduction_input_dim (int): dimension to scale each element in input sequences to
            prior to applying time reduction block.
        time_reduction_stride (int): factor by which to reduce length of input sequence.
        transformer_num_heads (int): number of attention heads in each Emformer layer.
        transformer_ffn_dim (int): hidden layer dimension of each Emformer layer's feedforward network.
        transformer_num_layers (int): number of Emformer layers to instantiate.
        transformer_left_context_length (int): length of left context.
        transformer_dropout (float, optional): transformer dropout probability. (Default: 0.0)
        transformer_activation (str, optional): activation function to use in each Emformer layer's
            feedforward network. Must be one of ("relu", "gelu", "silu"). (Default: "relu")
        transformer_max_memory_size (int, optional): maximum number of memory elements to use. (Default: 0)
        transformer_weight_init_scale_strategy (str, optional): per-layer weight initialization scaling
            strategy. Must be one of ("depthwise", "constant", ``None``). (Default: "depthwise")
        transformer_tanh_on_mem (bool, optional): if ``True``, applies tanh to memory elements. (Default: ``False``)
    ç        Úrelur   Ú	depthwiseF)Útransformer_dropoutÚtransformer_activationÚtransformer_max_memory_sizeÚ&transformer_weight_init_scale_strategyÚtransformer_tanh_on_memr4   Ú
output_dimÚsegment_lengthÚright_context_lengthÚtime_reduction_input_dimÚtime_reduction_strideÚtransformer_num_headsÚtransformer_ffn_dimÚtransformer_num_layersÚtransformer_left_context_lengthrk   rl   rm   rn   ro   r   Nc                ó¦  •— t          ¦   «                              ¦   «          t          j                             ||d¬¦  «        | _        t          |¦  «        | _        ||z  }t          ||||	||z  |||
||z  |||¬¦  «        | _	        t          j                             ||¦  «        | _
        t          j                             |¦  «        | _        d S )NFr:   )ÚdropoutÚ
activationÚleft_context_lengthrr   Úmax_memory_sizeÚweight_init_scale_strategyÚtanh_on_mem)r   r   r-   r=   r>   Úinput_linearr   Útime_reductionr   ÚtransformerÚoutput_linearrA   r6   )r   r4   rp   rq   rr   rs   rt   ru   rv   rw   rx   rk   rl   rm   rn   ro   Útransformer_input_dimr   s                    €r   r   z_EmformerEncoder.__init__°   sÝ   ø€ õ& 	‰Œ×ÒÑÔÐÝ!œHŸOšOØØ$Øð ,ñ 
ô 
ˆÔõ
 -Ð-BÑCÔCˆÔØ 8Ð;PÑ PÐÝ#Ø!Ø!ØØ"ØÐ3Ñ3Ø'Ø-Ø ?Ø!5Ð9NÑ!NØ7Ø'MØ/ð
ñ 
ô 
ˆÔõ #œXŸ_š_Ð-BÀJÑOÔOˆÔÝœ(×,Ò,¨ZÑ8Ô8ˆŒˆˆr   r   r   c                 óì   — |                       |¦  «        }|                      ||¦  «        \  }}|                      ||¦  «        \  }}|                      |¦  «        }|                      |¦  «        }	|	|fS )a¥  Forward pass for training.

        B: batch size;
        T: maximum input sequence length in batch;
        D: feature dimension of each input sequence frame (input_dim).

        Args:
            input (torch.Tensor): input frame sequences right-padded with right context, with
                shape `(B, T + right context length, D)`.
            lengths (torch.Tensor): with shape `(B,)` and i-th element representing
                number of valid frames for i-th batch element in ``input``.

        Returns:
            (torch.Tensor, torch.Tensor):
                torch.Tensor
                    output frame sequences, with
                    shape `(B, T // time_reduction_stride, output_dim)`.
                torch.Tensor
                    output input lengths, with shape `(B,)` and i-th element representing
                    number of valid elements for i-th batch element in output frame sequences.
        )r€   r�   r‚   rƒ   r6   )
r   r   r   Úinput_linear_outÚtime_reduction_outÚtime_reduction_lengthsÚtransformer_outÚtransformer_lengthsÚoutput_linear_outÚlayer_norm_outs
             r   r'   z_EmformerEncoder.forwardÜ   s…   € ð,  ×,Ò,¨UÑ3Ô3ÐØ59×5HÒ5HÐIYÐ[bÑ5cÔ5cÑ2ÐÐ2Ø/3×/?Ò/?Ð@RÐTjÑ/kÔ/kÑ,ˆÐ,Ø ×.Ò.¨Ñ?Ô?ÐØŸšÐ):Ñ;Ô;ˆØÐ2Ð2Ð2r   rc   c                 óü   — |                       |¦  «        }|                      ||¦  «        \  }}| j                             |||¦  «        \  }}}	|                      |¦  «        }
|                      |
¦  «        }|||	fS )aR  Forward pass for inference.

        B: batch size;
        T: maximum input sequence segment length in batch;
        D: feature dimension of each input sequence frame (input_dim).

        Args:
            input (torch.Tensor): input frame sequence segments right-padded with right context, with
                shape `(B, T + right context length, D)`.
            lengths (torch.Tensor): with shape `(B,)` and i-th element representing
                number of valid frames for i-th batch element in ``input``.
            state (List[List[torch.Tensor]] or None): list of lists of tensors
                representing internal state generated in preceding invocation
                of ``infer``.

        Returns:
            (torch.Tensor, torch.Tensor, List[List[torch.Tensor]]):
                torch.Tensor
                    output frame sequences, with
                    shape `(B, T // time_reduction_stride, output_dim)`.
                torch.Tensor
                    output input lengths, with shape `(B,)` and i-th element representing
                    number of valid elements for i-th batch element in output.
                List[List[torch.Tensor]]
                    output states; list of lists of tensors
                    representing internal state generated in current invocation
                    of ``infer``.
        )r€   r�   r‚   re   rƒ   r6   )r   r   r   rc   r†   r‡   rˆ   r‰   rŠ   Útransformer_statesr‹   rŒ   s               r   re   z_EmformerEncoder.inferù   s–   € ðF  ×,Ò,¨UÑ3Ô3ÐØ59×5HÒ5HÐIYÐ[bÑ5cÔ5cÑ2ÐÐ2ð
 Ô×"Ò"Ð#5Ð7MÈvÑVÔVñ		
ØØØà ×.Ò.¨Ñ?Ô?ÐØŸšÐ):Ñ;Ô;ˆØÐ2Ð4FÐFÐFr   )r(   r)   r*   r+   r,   r^   Ústrr]   r   r-   r.   r   r'   ÚjitÚexportr   r   re   r/   r0   s   @r   rg   rg   ˜   sÊ  ø€ € € € € ðð ðH &)Ø&,Ø+,Ø6AØ(-ð#*9ð *9ð *9ð ð*9ð ð	*9ð
 ð*9ð "ð*9ð #&ð*9ð  #ð*9ð  #ð*9ð !ð*9ð !$ð*9ð *-ð*9ð #ð*9ð !$ð*9ð &)ð*9ð  14ð!*9ð" "&ð#*9ð$ 
ð%*9ð *9ð *9ð *9ð *9ð *9ðX3˜Uœ\ð 3°E´Lð 3ÀUÈ5Ì<ÐY^ÔYeÐKeÔEfð 3ð 3ð 3ð 3ð: „YÔð+GàŒ|ð+Gð ”ð+Gð ˜˜d 5¤<Ô0Ô1Ô2ð	+Gð
 
ˆuŒ|˜Uœ\¨4°°U´\Ô0BÔ+CÐCÔ	Dð+Gð +Gð +Gñ Ôð+Gð +Gð +Gð +Gð +Gr   rg   c                   óö   ‡ — e Zd ZdZ	 	 	 ddedededed	ed
edededdfˆ fd„Z	 ddej	        dej	        de
eeej	                                   deej	        ej	        eeej	                          f         fd„Zˆ xZS )Ú
_Predictora  Recurrent neural network transducer (RNN-T) prediction network.

    Args:
        num_symbols (int): size of target token lexicon.
        output_dim (int): feature dimension of each output sequence element.
        symbol_embedding_dim (int): dimension of each target token embedding.
        num_lstm_layers (int): number of LSTM layers to instantiate.
        lstm_hidden_dim (int): output dimension of each LSTM layer.
        lstm_layer_norm (bool, optional): if ``True``, enables layer normalization
            for LSTM layers. (Default: ``False``)
        lstm_layer_norm_epsilon (float, optional): value of epsilon to use in
            LSTM layer normalization layers. (Default: 1e-5)
        lstm_dropout (float, optional): LSTM dropout probability. (Default: 0.0)

    Fr3   rh   Únum_symbolsrp   Úsymbol_embedding_dimÚnum_lstm_layersÚlstm_hidden_dimÚlstm_layer_normÚlstm_layer_norm_epsilonÚlstm_dropoutr   Nc	                 óL  •‡‡‡‡— t          ¦   «                              ¦   «          t          j                             |‰¦  «        | _        t          j                             ‰¦  «        | _        t          j                             ˆˆˆˆfd„t          |¦  «        D ¦   «         ¦  «        | _
        t          j                             |¬¦  «        | _        t          j                             ‰|¦  «        | _        t          j                             |¦  «        | _        || _        d S )Nc                 óF   •— g | ]}t          |d k    r‰n‰‰‰‰¬¦  «        ‘ŒS )r   )r6   r7   )r2   )Ú.0Úidxr—   r˜   r™   r•   s     €€€€r   ú
<listcomp>z'_Predictor.__init__.<locals>.<listcomp>H  sT   ø€ ð ð ð ð õ Ø,/°1ªH¨HÐ(Ð(¸/Ø#Ø.Ø'>ð	ñ ô ðð ð r   )Úp)r   r   r-   r=   Ú	EmbeddingÚ	embeddingrA   Úinput_layer_normÚ
ModuleListÚrangeÚlstm_layersÚDropoutrz   r>   ÚlinearÚoutput_layer_normrš   )
r   r”   rp   r•   r–   r—   r˜   r™   rš   r   s
      ` ``` €r   r   z_Predictor.__init__9  s  øøøøø€ õ 	‰Œ×ÒÑÔÐÝœ×+Ò+¨KÐ9MÑNÔNˆŒÝ %¤× 2Ò 2Ð3GÑ HÔ HˆÔÝ œ8×.Ò.ðð ð ð ð ð ð õ ! Ñ1Ô1ðñ ô ñ

ô 

ˆÔõ ”x×'Ò'¨,Ð'Ñ7Ô7ˆŒÝ”h—o’o o°zÑBÔBˆŒÝ!&¤×!3Ò!3°JÑ!?Ô!?ˆÔà(ˆÔÐÐr   r   r   rE   c                 óÌ  — |                      dd¦  «        }|                      |¦  «        }|                      |¦  «        }|}g }t          | j        ¦  «        D ]H\  }	}
 |
||€dn||	         ¦  «        \  }}|                      |¦  «        }|                     |¦  «         ŒI|                      |¦  «        }|                      |¦  «        }|                      ddd¦  «        ||fS )a#  Forward pass.

        B: batch size;
        U: maximum sequence length in batch;
        D: feature dimension of each input sequence element.

        Args:
            input (torch.Tensor): target sequences, with shape `(B, U)` and each element
                mapping to a target symbol, i.e. in range `[0, num_symbols)`.
            lengths (torch.Tensor): with shape `(B,)` and i-th element representing
                number of valid frames for i-th batch element in ``input``.
            state (List[List[torch.Tensor]] or None, optional): list of lists of tensors
                representing internal state generated in preceding invocation
                of ``forward``. (Default: ``None``)

        Returns:
            (torch.Tensor, torch.Tensor, List[List[torch.Tensor]]):
                torch.Tensor
                    output encoding sequences, with shape `(B, U, output_dim)`
                torch.Tensor
                    output lengths, with shape `(B,)` and i-th element representing
                    number of valid elements for i-th batch element in output encoding sequences.
                List[List[torch.Tensor]]
                    output states; list of lists of tensors
                    representing internal state generated in current invocation of ``forward``.
        rG   r   Né   )	Úpermuter¢   r£   Ú	enumerater¦   rz   rQ   r¨   r©   )r   r   r   rE   Úinput_tbÚembedding_outÚinput_layer_norm_outÚlstm_outÚ	state_outÚ	layer_idxÚlstmÚlstm_state_outÚ
linear_outÚoutput_layer_norm_outs                 r   r'   z_Predictor.forwardX  sö   € ð@ —=’=  AÑ&Ô&ˆØŸš xÑ0Ô0ˆØ#×4Ò4°]ÑCÔCÐà'ˆØ.0ˆ	Ý(¨Ô)9Ñ:Ô:ð 	-ð 	-‰OˆI�tØ'+ t¨H¸e¸m°d°dÐQVÐW`ÔQaÑ'bÔ'bÑ$ˆH�nØ—|’| HÑ-Ô-ˆHØ×Ò˜^Ñ,Ô,Ð,Ð,à—[’[ Ñ*Ô*ˆ
Ø $× 6Ò 6°zÑ BÔ BÐØ$×,Ò,¨Q°°1Ñ5Ô5°wÀ	ÐIÐIr   )Fr3   rh   r   r\   r0   s   @r   r“   r“   (  s2  ø€ € € € € ðð ð. !&Ø)-Ø!ð)ð )àð)ð ð)ð "ð	)ð
 ð)ð ð)ð ð)ð "'ð)ð ð)ð 
ð)ð )ð )ð )ð )ð )ðF 59ð	-Jð -JàŒ|ð-Jð ”ð-Jð ˜˜T %¤,Ô/Ô0Ô1ð	-Jð
 
ˆuŒ|˜Uœ\¨4°°U´\Ô0BÔ+CÐCÔ	Dð-Jð -Jð -Jð -Jð -Jð -Jð -Jð -Jr   r“   c                   óª   ‡ — e Zd ZdZddedededdfˆ fd„Zd	ej        d
ej        dej        dej        de	ej        ej        ej        f         f
d„Z
ˆ xZS )Ú_Joinera@  Recurrent neural network transducer (RNN-T) joint network.

    Args:
        input_dim (int): source and target input dimension.
        output_dim (int): output dimension.
        activation (str, optional): activation function to use in the joiner.
            Must be one of ("relu", "tanh"). (Default: "relu")

    ri   r4   rp   r{   r   Nc                 ób  •— t          ¦   «                              ¦   «          t          j                             ||d¬¦  «        | _        |dk    r%t          j                             ¦   «         | _        d S |dk    r%t          j                             ¦   «         | _        d S t          d|› �¦  «        ‚)NTr:   ri   rP   zUnsupported activation )
r   r   r-   r=   r>   r¨   ÚReLUr{   ÚTanhÚ
ValueError)r   r4   rp   r{   r   s       €r   r   z_Joiner.__init__“  s‹   ø€ Ý‰Œ×ÒÑÔÐÝ”h—o’o i°À$�oÑGÔGˆŒØ˜ÒÐÝ#œhŸmšm™oœoˆDŒOˆOˆOØ˜6Ò!Ð!Ý#œhŸmšm™oœoˆDŒOˆOˆOåÐC°zÐCÐCÑDÔDÐDr   Úsource_encodingsÚsource_lengthsÚtarget_encodingsÚtarget_lengthsc                 óþ   — |                      d¦  «                             ¦   «         |                      d¦  «                             ¦   «         z   }|                      |¦  «        }|                      |¦  «        }|||fS )a›  Forward pass for training.

        B: batch size;
        T: maximum source sequence length in batch;
        U: maximum target sequence length in batch;
        D: dimension of each source and target sequence encoding.

        Args:
            source_encodings (torch.Tensor): source encoding sequences, with
                shape `(B, T, D)`.
            source_lengths (torch.Tensor): with shape `(B,)` and i-th element representing
                valid sequence length of i-th batch element in ``source_encodings``.
            target_encodings (torch.Tensor): target encoding sequences, with shape `(B, U, D)`.
            target_lengths (torch.Tensor): with shape `(B,)` and i-th element representing
                valid sequence length of i-th batch element in ``target_encodings``.

        Returns:
            (torch.Tensor, torch.Tensor, torch.Tensor):
                torch.Tensor
                    joint network output, with shape `(B, T, U, output_dim)`.
                torch.Tensor
                    output source lengths, with shape `(B,)` and i-th element representing
                    number of valid elements along dim 1 for i-th batch element in joint network output.
                torch.Tensor
                    output target lengths, with shape `(B,)` and i-th element representing
                    number of valid elements along dim 2 for i-th batch element in joint network output.
        r«   rG   )Ú	unsqueezer    r{   r¨   )r   r¾   r¿   rÀ   rÁ   Újoint_encodingsÚactivation_outr&   s           r   r'   z_Joiner.forward�  st   € ðD +×4Ò4°QÑ7Ô7×BÒBÑDÔDÐGW×GaÒGaÐbcÑGdÔGd×GoÒGoÑGqÔGqÑqˆØŸš¨Ñ9Ô9ˆØ—’˜^Ñ,Ô,ˆØ�~ ~Ð5Ð5r   )ri   )r(   r)   r*   r+   r,   r�   r   r-   r.   r   r'   r/   r0   s   @r   r¹   r¹   ˆ  sÒ   ø€ € € € € ðð ðEð E #ð E°3ð EÀCð EÐUYð Eð Eð Eð Eð Eð Eð%6àœ,ð%6ð œð%6ð  œ,ð	%6ð
 œð%6ð 
ˆuŒ|˜Uœ\¨5¬<Ð7Ô	8ð%6ð %6ð %6ð %6ð %6ð %6ð %6ð %6r   r¹   c                   ó–  ‡ — e Zd ZdZdedededdfˆ fd„Z	 ddej	        d	ej	        d
ej	        dej	        de
eeej	                                   deej	        ej	        ej	        eeej	                          f         fd„Zej        j        dej	        d	ej	        de
eeej	                                   deej	        ej	        eeej	                          f         fd„¦   «         Zej        j        dej	        d	ej	        deej	        ej	        f         fd„¦   «         Zej        j        d
ej	        dej	        de
eeej	                                   deej	        ej	        eeej	                          f         fd„¦   «         Zej        j        dej	        d	ej	        dej	        dej	        deej	        ej	        ej	        f         f
d„¦   «         Zˆ xZS )r	   a¿  torchaudio.models.RNNT()

    Recurrent neural network transducer (RNN-T) model.

    Note:
        To build the model, please use one of the factory functions.

    See Also:
        :class:`torchaudio.pipelines.RNNTBundle`: ASR pipeline with pre-trained models.

    Args:
        transcriber (torch.nn.Module): transcription network.
        predictor (torch.nn.Module): prediction network.
        joiner (torch.nn.Module): joint network.
    ÚtranscriberÚ	predictorÚjoinerr   Nc                 ór   •— t          ¦   «                              ¦   «          || _        || _        || _        d S r   )r   r   rÇ   rÈ   rÉ   )r   rÇ   rÈ   rÉ   r   s       €r   r   zRNNT.__init__Ö  s3   ø€ Ý‰Œ×ÒÑÔÐØ&ˆÔØ"ˆŒØˆŒˆˆr   Úsourcesr¿   ÚtargetsrÁ   Úpredictor_statec                 ó´   — |                       ||¬¦  «        \  }}|                      |||¬¦  «        \  }}}|                      ||||¬¦  «        \  }}}||||fS )a  Forward pass for training.

        B: batch size;
        T: maximum source sequence length in batch;
        U: maximum target sequence length in batch;
        D: feature dimension of each source sequence element.

        Args:
            sources (torch.Tensor): source frame sequences right-padded with right context, with
                shape `(B, T, D)`.
            source_lengths (torch.Tensor): with shape `(B,)` and i-th element representing
                number of valid frames for i-th batch element in ``sources``.
            targets (torch.Tensor): target sequences, with shape `(B, U)` and each element
                mapping to a target symbol.
            target_lengths (torch.Tensor): with shape `(B,)` and i-th element representing
                number of valid frames for i-th batch element in ``targets``.
            predictor_state (List[List[torch.Tensor]] or None, optional): list of lists of tensors
                representing prediction network internal state generated in preceding invocation
                of ``forward``. (Default: ``None``)

        Returns:
            (torch.Tensor, torch.Tensor, torch.Tensor, List[List[torch.Tensor]]):
                torch.Tensor
                    joint network output, with shape
                    `(B, max output source length, max output target length, output_dim (number of target symbols))`.
                torch.Tensor
                    output source lengths, with shape `(B,)` and i-th element representing
                    number of valid elements along dim 1 for i-th batch element in joint network output.
                torch.Tensor
                    output target lengths, with shape `(B,)` and i-th element representing
                    number of valid elements along dim 2 for i-th batch element in joint network output.
                List[List[torch.Tensor]]
                    output states; list of lists of tensors
                    representing prediction network internal state generated in current invocation
                    of ``forward``.
        )r   r   ©r   r   rE   ©r¾   r¿   rÀ   rÁ   )rÇ   rÈ   rÉ   )	r   rË   r¿   rÌ   rÁ   rÍ   r¾   rÀ   r&   s	            r   r'   zRNNT.forwardÜ  s�   € ðX ,0×+;Ò+;ØØ"ð ,<ñ ,
ô ,
Ñ(Ð˜.ð =A¿NºNØØ"Ø!ð =Kñ =
ô =
Ñ9Ð˜.¨/ð
 26·²Ø-Ø)Ø-Ø)ð	 2=ñ 2
ô 2
Ñ.ˆ� ð ØØØð	
ð 	
r   rE   c                 ó:   — | j                              |||¦  «        S )a¸  Applies transcription network to sources in streaming mode.

        B: batch size;
        T: maximum source sequence segment length in batch;
        D: feature dimension of each source sequence frame.

        Args:
            sources (torch.Tensor): source frame sequence segments right-padded with right context, with
                shape `(B, T + right context length, D)`.
            source_lengths (torch.Tensor): with shape `(B,)` and i-th element representing
                number of valid frames for i-th batch element in ``sources``.
            state (List[List[torch.Tensor]] or None): list of lists of tensors
                representing transcription network internal state generated in preceding invocation
                of ``transcribe_streaming``.

        Returns:
            (torch.Tensor, torch.Tensor, List[List[torch.Tensor]]):
                torch.Tensor
                    output frame sequences, with
                    shape `(B, T // time_reduction_stride, output_dim)`.
                torch.Tensor
                    output lengths, with shape `(B,)` and i-th element representing
                    number of valid elements for i-th batch element in output.
                List[List[torch.Tensor]]
                    output states; list of lists of tensors
                    representing transcription network internal state generated in current invocation
                    of ``transcribe_streaming``.
        )rÇ   re   )r   rË   r¿   rE   s       r   Útranscribe_streamingzRNNT.transcribe_streaming  s    € ðF Ô×%Ò% g¨~¸uÑEÔEÐEr   c                 ó.   — |                       ||¦  «        S )aÆ  Applies transcription network to sources in non-streaming mode.

        B: batch size;
        T: maximum source sequence length in batch;
        D: feature dimension of each source sequence frame.

        Args:
            sources (torch.Tensor): source frame sequences right-padded with right context, with
                shape `(B, T + right context length, D)`.
            source_lengths (torch.Tensor): with shape `(B,)` and i-th element representing
                number of valid frames for i-th batch element in ``sources``.

        Returns:
            (torch.Tensor, torch.Tensor):
                torch.Tensor
                    output frame sequences, with
                    shape `(B, T // time_reduction_stride, output_dim)`.
                torch.Tensor
                    output lengths, with shape `(B,)` and i-th element representing
                    number of valid elements for i-th batch element in output frame sequences.
        )rÇ   )r   rË   r¿   s      r   Ú
transcribezRNNT.transcribeD  s   € ð6 ×Ò ¨Ñ8Ô8Ð8r   c                 ó2   — |                       |||¬¦  «        S )a  Applies prediction network to targets.

        B: batch size;
        U: maximum target sequence length in batch;
        D: feature dimension of each target sequence frame.

        Args:
            targets (torch.Tensor): target sequences, with shape `(B, U)` and each element
                mapping to a target symbol, i.e. in range `[0, num_symbols)`.
            target_lengths (torch.Tensor): with shape `(B,)` and i-th element representing
                number of valid frames for i-th batch element in ``targets``.
            state (List[List[torch.Tensor]] or None): list of lists of tensors
                representing internal state generated in preceding invocation
                of ``predict``.

        Returns:
            (torch.Tensor, torch.Tensor, List[List[torch.Tensor]]):
                torch.Tensor
                    output frame sequences, with shape `(B, U, output_dim)`.
                torch.Tensor
                    output lengths, with shape `(B,)` and i-th element representing
                    number of valid elements for i-th batch element in output.
                List[List[torch.Tensor]]
                    output states; list of lists of tensors
                    representing internal state generated in current invocation of ``predict``.
        rÏ   )rÈ   )r   rÌ   rÁ   rE   s       r   ÚpredictzRNNT.predicta  s   € ðB �~Š~ G°^È5ˆ~ÑQÔQÐQr   r¾   rÀ   c                 óF   — |                       ||||¬¦  «        \  }}}|||fS )a¶  Applies joint network to source and target encodings.

        B: batch size;
        T: maximum source sequence length in batch;
        U: maximum target sequence length in batch;
        D: dimension of each source and target sequence encoding.

        Args:
            source_encodings (torch.Tensor): source encoding sequences, with
                shape `(B, T, D)`.
            source_lengths (torch.Tensor): with shape `(B,)` and i-th element representing
                valid sequence length of i-th batch element in ``source_encodings``.
            target_encodings (torch.Tensor): target encoding sequences, with shape `(B, U, D)`.
            target_lengths (torch.Tensor): with shape `(B,)` and i-th element representing
                valid sequence length of i-th batch element in ``target_encodings``.

        Returns:
            (torch.Tensor, torch.Tensor, torch.Tensor):
                torch.Tensor
                    joint network output, with shape `(B, T, U, output_dim)`.
                torch.Tensor
                    output source lengths, with shape `(B,)` and i-th element representing
                    number of valid elements along dim 1 for i-th batch element in joint network output.
                torch.Tensor
                    output target lengths, with shape `(B,)` and i-th element representing
                    number of valid elements along dim 2 for i-th batch element in joint network output.
        rÐ   )rÉ   )r   r¾   r¿   rÀ   rÁ   r&   s         r   Újoinz	RNNT.join„  s?   € ðF 26·²Ø-Ø)Ø-Ø)ð	 2=ñ 2
ô 2
Ñ.ˆ� ð �~ ~Ð5Ð5r   r   )r(   r)   r*   r+   r`   r“   r¹   r   r-   r.   r   r   r   r'   r�   r‘   rÒ   rÔ   rÖ   rØ   r/   r0   s   @r   r	   r	   Å  sÓ  ø€ € € € € ðð ð  Lð ¸Zð ÐQXð Ð]að ð ð ð ð ð ð ?CðA
ð A
à”ðA
ð œðA
ð ”ð	A
ð
 œðA
ð " $ t¨E¬LÔ'9Ô":Ô;ðA
ð 
ˆuŒ|˜Uœ\¨5¬<¸¸dÀ5Ä<Ô>PÔ9QÐQÔ	RðA
ð A
ð A
ð A
ðF „YÔð"Fà”ð"Fð œð"Fð ˜˜T %¤,Ô/Ô0Ô1ð	"Fð
 
ˆuŒ|˜Uœ\¨4°°U´\Ô0BÔ+CÐCÔ	Dð"Fð "Fð "Fñ Ôð"FðH „YÔð9à”ð9ð œð9ð 
ˆuŒ|˜Uœ\Ð)Ô	*ð	9ð 9ð 9ñ Ôð9ð8 „YÔð Rà”ð Rð œð Rð ˜˜T %¤,Ô/Ô0Ô1ð	 Rð
 
ˆuŒ|˜Uœ\¨4°°U´\Ô0BÔ+CÐCÔ	Dð Rð  Rð  Rñ Ôð RðD „YÔð(6àœ,ð(6ð œð(6ð  œ,ð	(6ð
 œð(6ð 
ˆuŒ|˜Uœ\¨5¬<Ð7Ô	8ð(6ð (6ð (6ñ Ôð(6ð (6ð (6ð (6ð (6r   r	   r4   Úencoding_dimr”   rq   rr   rs   rt   ru   rv   rw   rk   rl   rx   rm   rn   ro   r•   r–   r˜   r™   rš   r   c                 ó®   — t          | ||||||||	|
|||||¬¦  «        }t          ||||||||¬¦  «        }t          ||¦  «        }t          |||¦  «        S )a 
  Builds Emformer-based :class:`~torchaudio.models.RNNT`.

    Note:
        For non-streaming inference, the expectation is for `transcribe` to be called on input
        sequences right-concatenated with `right_context_length` frames.

        For streaming inference, the expectation is for `transcribe_streaming` to be called
        on input chunks comprising `segment_length` frames right-concatenated with `right_context_length`
        frames.

    Args:
        input_dim (int): dimension of input sequence frames passed to transcription network.
        encoding_dim (int): dimension of transcription- and prediction-network-generated encodings
            passed to joint network.
        num_symbols (int): cardinality of set of target tokens.
        segment_length (int): length of input segment expressed as number of frames.
        right_context_length (int): length of right context expressed as number of frames.
        time_reduction_input_dim (int): dimension to scale each element in input sequences to
            prior to applying time reduction block.
        time_reduction_stride (int): factor by which to reduce length of input sequence.
        transformer_num_heads (int): number of attention heads in each Emformer layer.
        transformer_ffn_dim (int): hidden layer dimension of each Emformer layer's feedforward network.
        transformer_num_layers (int): number of Emformer layers to instantiate.
        transformer_left_context_length (int): length of left context considered by Emformer.
        transformer_dropout (float): Emformer dropout probability.
        transformer_activation (str): activation function to use in each Emformer layer's
            feedforward network. Must be one of ("relu", "gelu", "silu").
        transformer_max_memory_size (int): maximum number of memory elements to use.
        transformer_weight_init_scale_strategy (str): per-layer weight initialization scaling
            strategy. Must be one of ("depthwise", "constant", ``None``).
        transformer_tanh_on_mem (bool): if ``True``, applies tanh to memory elements.
        symbol_embedding_dim (int): dimension of each target token embedding.
        num_lstm_layers (int): number of LSTM layers to instantiate.
        lstm_layer_norm (bool): if ``True``, enables layer normalization for LSTM layers.
        lstm_layer_norm_epsilon (float): value of epsilon to use in LSTM layer normalization layers.
        lstm_dropout (float): LSTM dropout probability.

    Returns:
        RNNT:
            Emformer RNN-T model.
    )r4   rp   rq   rr   rs   rt   ru   rv   rw   rk   rl   rx   rm   rn   ro   )r•   r–   r—   r˜   r™   rš   )rg   r“   r¹   r	   )r4   rÙ   r”   rq   rr   rs   rt   ru   rv   rw   rk   rl   rx   rm   rn   ro   r•   r–   r˜   r™   rš   ÚencoderrÈ   rÉ   s                           r   r   r   °  s—   € õB ØØØ%Ø1Ø!9Ø3Ø3Ø/Ø5Ø/Ø5Ø(GØ$?Ø/UØ 7ðñ ô €Gõ" ØØØ1Ø'Ø,Ø'Ø 7Ø!ð	ñ 	ô 	€Iõ �\ ;Ñ/Ô/€FÝ�˜ FÑ+Ô+Ð+r   c                 ó”   — t          d(i dd“dd“d| “dd“dd	“d
d“dd	“dd“dd“dd“dd“dd“dd“dd“dd“dd“dd “d!d"“d#d“d$d%“d&d'“ŽS ))zÓBuilds basic version of Emformer-based :class:`~torchaudio.models.RNNT`.

    Args:
        num_symbols (int): The size of target token lexicon.

    Returns:
        RNNT:
            Emformer RNN-T model.
    r4   éP   rÙ   i   r”   rq   é   rr   r9   rs   é€   rt   ru   é   rv   i   rw   é   rk   gš™™™™™¹?rl   Úgelurx   é   rm   r   rn   rj   ro   Tr•   i   r–   é   r˜   r™   gü©ñÒMbP?rš   g333333Ó?rb   )r   )r”   s    r   r
   r
     s  € õ ð ð ð Ø�"ðà�Tðð  �Kðð �rð	ð
 ˜Qðð "% ðð  ˜aðð  ˜aðð !˜Dðð  "˜rðð  ˜Cðð  &˜vðð )+¨ðð %& Aðð 0;¨{ðð  !% ð!ð" !˜Sð#ð$ ˜ð%ð& ˜ð'ð( !% ð)ð* �Sð+ð r   )Úabcr   r   Útypingr   r   r   r-   Útorchaudio.modelsr   Ú__all__r=   ÚModuler   r2   r`   rg   r“   r¹   r	   r,   r^   r�   r]   r   r
   rb   r   r   ú<module>rê      s  ðØ #Ð #Ð #Ð #Ð #Ð #Ð #Ð #Ø (Ð (Ð (Ð (Ð (Ð (Ð (Ð (Ð (Ð (à €€€Ø &Ð &Ð &Ð &Ð &Ð &ð @Ð
?Ð
?€ð)ð )ð )ð )ð )�U”X”_ñ )ô )ð )ðXOð Oð Oð Oð O�%”(”/ñ Oô Oð Oðdð ð ð ð �3ñ ô ð ðMGð MGð MGð MGð MG�u”x”¨ñ MGô MGð MGð`]Jð ]Jð ]Jð ]Jð ]J�””ñ ]Jô ]Jð ]Jð@:6ð :6ð :6ð :6ð :6ˆeŒhŒoñ :6ô :6ð :6ðzh6ð h6ð h6ð h6ð h6ˆ5Œ8Œ?ñ h6ô h6ð h6ðV],àð],ð ð],ð ð	],ð
 ð],ð ð],ð "ð],ð ð],ð ð],ð ð],ð  ð],ð ð],ð  ð],ð &)ð],ð "%ð],ð  -0ð!],ð" "ð#],ð$ ð%],ð& ð'],ð( ð)],ð* #ð+],ð, ð-],ð. 
ð/],ð ],ð ],ð ],ð@  Cð  ¨Dð  ð  ð  ð  ð  ð  r   