o
    i'                     @   s   d Z ddlZddlmZ ddlZddlmZ ddlmZ ddl	m
Z
 ddlmZ ddlmZmZ dd	lmZmZmZ dd
lmZmZmZmZ ddlmZ ddlmZmZ ddlmZ ddl m!Z! G dd deZ"dS )zConformers PostEncoder.    N)Tuple)check_argument_types)AbsPostEncoder)ConvolutionModuleEncoderLayer)get_activationmake_pad_mask)%LegacyRelPositionMultiHeadedAttentionMultiHeadedAttentionRelPositionMultiHeadedAttention)LegacyRelPositionalEncodingPositionalEncodingRelPositionalEncodingScaledPositionalEncoding)	LayerNorm)Conv1dLinearMultiLayeredConv1d)PositionwiseFeedForward)repeatc                -       s   e Zd ZdZ	 											
			
							
		d1d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, fd(d)Zd*e	j
d+e	j
d,ee	j
e	j
f fd-d.Zd,efd/d0Z  ZS )2ConformerPostEncoderz&Hugging Face Transformers PostEncoder.            皙?        linearTF   legacyrel_posrel_selfattnswish   
input_sizeoutput_sizeattention_headslinear_units
num_blocksdropout_ratepositional_dropout_rateattention_dropout_rateinput_layernormalize_beforeconcat_afterpositionwise_layer_typepositionwise_conv_kernel_sizemacaron_stylerel_pos_typepos_enc_layer_typeselfattention_layer_typeactivation_typeuse_cnn_module	zero_triucnn_module_kernelpadding_idxc                    sn  t  sJ t   | _|dkr|dkrd}|dkrd}n|dkr/|dks(J |dks.J ntd| t|}|dkr@t}n3|d	krGt}n,|dkrT|dksQJ t}n|dkrf|dks^J t	}t
d
 n|dkrmd }ntd| |	dkrtj||| _n!t|	tjjrtj|	||| _n|	dkrd | _ntd|	 | _|dkrt	||f
n|dkrt	||f
n|dkrt	||f
ntd|dkrt||fn1|dkr|dksJ t||ft
d n|dkr|dksJ t|||fntd| t||ft| 	
fdd| _| jr5t| _d S d S )Nr   r    legacy_rel_posr!   legacy_rel_selfattnlatestzunknown rel_pos_type: abs_posscaled_abs_posz=Using legacy_rel_pos and it will be deprecated in the future.Nonezunknown pos_enc_layer: r   zunknown input_layer: conv1dzconv1d-linearzSupport only linear or conv1d.selfattnzBUsing legacy_rel_selfattn and it will be deprecated in the future.zunknown encoder_attn_layer: c              	      s6   t  	
 r	
 nd r nd  S )Nr   )lnumr/   convolution_layerconvolution_layer_argsr*   encoder_selfattn_layerencoder_selfattn_layer_argsr2   r.   r&   positionwise_layerpositionwise_layer_argsr7    a/home/ubuntu/.local/lib/python3.10/site-packages/espnet2/slu/postencoder/conformer_postencoder.py<lambda>   s    z/ConformerPostEncoder.__init__.<locals>.<lambda>)r   super__init___output_size
ValueErrorr   r   r   r   r   loggingwarningtorchnn
Sequentialembed
isinstanceModuler.   r   r   r   NotImplementedErrorr   r
   r   r   r   encodersr   
after_norm)selfr%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   
activationpos_enc_class	__class__rD   rL   rO   K   s   



 zConformerPostEncoder.__init__inputinput_lengthsreturnc                 C   s   |}t | |d j}| jdu r|}n| |}||jd d|jd }| ||\}}t|tr8|d }| j	r@| 
|}|dd}||fS )zForward.r   N   )r	   todevicerW   reshapeshaper[   rX   tupler.   r\   squeezesum)r]   rb   rc   xs_padmasksolensrK   rK   rL   forward   s   



zConformerPostEncoder.forwardc                 C   s   | j S )zGet the output size.)rP   )r]   rK   rK   rL   r&      s   z ConformerPostEncoder.output_size)r   r   r   r   r   r   r   r   TFr   r   Fr   r    r!   r"   TFr#   r$   )__name__
__module____qualname____doc__intfloatstrboolrO   rT   Tensorr   rp   r&   __classcell__rK   rK   r`   rL   r   &   s    %	
 
r   )#rt   rR   typingr   rT   	typeguardr   'espnet2.asr.postencoder.abs_postencoderr   1espnet.nets.pytorch_backend.conformer.convolutionr   3espnet.nets.pytorch_backend.conformer.encoder_layerr   &espnet.nets.pytorch_backend.nets_utilsr   r	   1espnet.nets.pytorch_backend.transformer.attentionr
   r   r   1espnet.nets.pytorch_backend.transformer.embeddingr   r   r   r   2espnet.nets.pytorch_backend.transformer.layer_normr   8espnet.nets.pytorch_backend.transformer.multi_layer_convr   r   Aespnet.nets.pytorch_backend.transformer.positionwise_feed_forwardr   .espnet.nets.pytorch_backend.transformer.repeatr   r   rK   rK   rK   rL   <module>   s    