o
    i                     @   sh   d Z ddlZddlZddlmZ ddlmZ ddl	Z
ddlmZ G dd dejZG dd dejZdS )z0Class Declaration of Transformer's Input layers.    N)PositionalEncodingc                       s,   e Zd ZdZ	d fdd	Zdd Z  ZS )	Conv2dSubsamplingzConvolutional 2D subsampling (to 1/4 length).

    :param int idim: input dim
    :param int odim: output dim
    :param flaot dropout_rate: dropout rate

    皙?Nc           	         s   t t|   || _|  b d}dt| }tjd|ddd||d||dd| _	|d d }dt| }tj||ddd||d||dd| _
dt| }tj||||d||dd| _t||| _W d	   d	S 1 ssw   Y  d	S )
zInitialize Conv2dSubsampling.	         ?         scale)stridepadinitialWinitial_biasr   r   N)superr   __init__dropout
init_scopenpsqrtLConvolution2Dconv1conv2Linearoutr   pe)	selfchannelsidimdimsr   r   r   nstvd	__class__ g/home/ubuntu/.local/lib/python3.10/site-packages/espnet/nets/chainer_backend/transformer/subsampling.pyr      sD   
		"zConv2dSubsampling.__init__c                 C   s   | j |dddf }t| |}t| |}|j\}}}}| t|dd	|| d}| 
|	||d}ttj|tjdd tj}ttj|tjdd tj}||fS )nSubsample x.

        :param chainer.Variable x: input tensor
        :return: subsampled x and mask

        Nr   r	   )dtype)xparrayFrelur   r   shaper   swapaxesreshaper   r   ceilfloat32astypeint64)r   xsilensbatch_lengthr&   r&   r'   forward>   s    ""zConv2dSubsampling.forwardr   NN__name__
__module____qualname____doc__r   r;   __classcell__r&   r&   r$   r'   r      s
    	'r   c                       s*   e Zd ZdZd fdd	Zdd Z  ZS )	LinearSamplingzLinear 1D subsampling.

    :param int idim: input dim
    :param int odim: output dim
    :param flaot dropout_rate: dropout rate

    r   Nc                    s|   t t|   dt| }|| _|   tj||||d||dd| _	t
||| _W d   dS 1 s7w   Y  dS )zInitialize LinearSampling.r   r
   r   N)r   rC   r   r   r   r   r   r   r   linearr   r   )r   r    r!   r   r   r   r#   r$   r&   r'   r   Z   s   
"zLinearSampling.__init__c                 C   s8   t |j | j|dd}t |j | |}||fS )r(   r	   )n_batch_axes)logginginfor/   rD   r   )r   r6   r7   r&   r&   r'   r;   h   s
   
zLinearSampling.forwardr<   r=   r&   r&   r$   r'   rC   Q   s    rC   )rA   rF   chainerchainer.functions	functionsr-   chainer.linkslinksr   numpyr   1espnet.nets.chainer_backend.transformer.embeddingr   Chainr   rC   r&   r&   r&   r'   <module>   s   C