o
    i$                     @   sj   d dl Z d dlmZ d dlmZ d dlZG dd de j	Z
G dd de j	ZG dd de j	Zdd	 ZdS )
    Nc                       2   e Zd ZdZ fddZdd Zd	ddZ  ZS )
AttDota  Compute attention based on dot product.

    Args:
        eprojs (int | None): Dimension of input vectors from encoder.
        dunits (int | None): Dimension of input vectors for decoder.
        att_dim (int): Dimension of input vectors for attention.

    c                    sz   t t|   |   t||| _t||| _W d    n1 s$w   Y  || _|| _	|| _
d | _d | _d | _d S N)superr   __init__
init_scopeLLinearmlp_encmlp_decdunitseprojsatt_dimh_lengthenc_hpre_compute_enc_h)selfr   r   r   	__class__ ^/home/ubuntu/.local/lib/python3.10/site-packages/espnet/nets/chainer_backend/rnn/attentions.pyr      s   

zAttDot.__init__c                 C      d| _ d| _d| _dS zReset states.Nr   r   r   r   r   r   r   reset      
zAttDot.reset       @c           
   	   C   s   t |}| jdu r"t|| _| jjd | _t| j| jdd| _|du r6t	
| jj|| jftjd}n||| j}ttt| |d| jj}tj| j| dd}t|| }tj| jtt|d| jj dd}	|	|fS )a  Compute AttDot forward layer.

        Args:
            enc_hs (chainer.Variable | N-dimensional array):
                Input variable from encoder.
            dec_z (chainer.Variable | N-dimensional array): Input variable of decoder.
            scaling (float): Scaling weight to make attention sharp.

        Returns:
            chainer.Variable: Weighted sum over flames.
            chainer.Variable: Attention weight.

        N      n_batch_axesdtypeaxis)lenr   Fpad_sequencer   shaper   tanhr
   chainerVariablexpzerosr   npfloat32reshapebroadcast_toexpand_dimsr   sumsoftmax)
r   enc_hsdec_zatt_prevscalingbatchuewcr   r   r   __call__%   s&   
zAttDot.__call__r   __name__
__module____qualname____doc__r   r   r?   __classcell__r   r   r   r   r      s
    	r   c                       r   )
AttLoca  Compute location-based attention.

    Args:
        eprojs (int | None): Dimension of input vectors from encoder.
        dunits (int | None): Dimension of input vectors for decoder.
        att_dim (int): Dimension of input vectors for attention.
        aconv_chans (int): Number of channels of output arrays from convolutional layer.
        aconv_filts (int): Size of filters of convolutional layer.

    c                    s   t t|   |  : t||| _tj||dd| _tj||dd| _tj	d|dd| d fd|fd| _
t|d| _W d    n1 sHw   Y  || _|| _|| _d | _d | _d | _|| _d S )NT)nobiasr   r   r   )ksizepad)r   rG   r   r   r   r	   r
   r   mlp_attConvolution2Dloc_convgvecr   r   r   r   r   r   aconv_chans)r   r   r   r   rO   aconv_filtsr   r   r   r   b   s"   
	
zAttLoc.__init__c                 C   r   r   r   r   r   r   r   r   u   r   zAttLoc.resetr   c                    sh  t |} jdu rt| _ jjd  _ j jdd _|du r3t	 j
j| jftjd}n|| j}|du rS fdd|D }dd |D }t|} ||dd j}ttj|dd	dd} j|dd}tt |d jj}tj jt| j | dddd	}t|| }	tj jtt|	d jj dd	}
|
|	fS )
a  Compute AttLoc forward layer.

        Args:
            enc_hs (chainer.Variable | N-dimensional array):
                Input variable from encoders.
            dec_z (chainer.Variable | N-dimensional array): Input variable of decoder.
            att_prev (chainer.Variable | None): Attention weight.
            scaling (float): Scaling weight to make attention sharp.

        Returns:
            chainer.Variable: Weighted sum over flames.
            chainer.Variable: Attention weight.

        Nr   r   r    r"   c                    0   g | ]} j j|jd  d|jd   tjdqS r   g      ?r"   r-   fullr)   r/   r0   .0hhr   r   r   
<listcomp>       "z#AttLoc.__call__.<locals>.<listcomp>c                 S      g | ]}t |qS r   r+   r,   rV   attr   r   r   rX          r$   )r&   r   r'   r(   r   r)   r   r
   r+   r,   r-   r.   r   r/   r0   r1   rM   swapaxessqueezerK   r2   r3   r   rN   r*   r5   r4   )r   r6   r7   r8   r9   r:   att_convdec_z_tiledr<   r=   r>   r   r   r   r?   {   sB   



zAttLoc.__call__r@   rA   r   r   r   r   rG   V   s
    rG   c                       s0   e Zd ZdZ fddZdd Zdd Z  ZS )NoAttzCompute non-attention layer.

    This layer is a dummy attention layer to be compatible with other
    attention-based models.

    c                    s*   t t|   d | _d | _d | _d | _d S r   )r   rc   r   r   r   r   r>   r   r   r   r   r      s
   
zNoAtt.__init__c                 C   s   d| _ d| _d| _d| _dS r   )r   r   r   r>   r   r   r   r   r      s   
zNoAtt.resetc                    s    j du rt| _ jjd  _|du r@ fdd|D }dd |D }t|}tj jtt|d jj dd _	 j	|fS )am  Compute NoAtt forward layer.

        Args:
            enc_hs (chainer.Variable | N-dimensional array):
                Input variable from encoders.
            dec_z: Dummy.
            att_prev (chainer.Variable | None): Attention weight.

        Returns:
            chainer.Variable: Sum over flames.
            chainer.Variable: Attention weight.

        Nr   c                    rQ   rR   rS   rU   r   r   r   rX      rY   z"NoAtt.__call__.<locals>.<listcomp>c                 S   rZ   r   r[   r\   r   r   r   rX      r^   r   r$   )
r   r'   r(   r   r)   r   r4   r2   r3   r>   )r   r6   r7   r8   r   r   r   r?      s    



zNoAtt.__call__rA   r   r   r   r   rc      s
    rc   c                 C   sd   | j dkrt| j| j| j}|S | j dkr$t| j| j| j| j| j}|S | j dkr.t }|S t	d)zReturns an attention layer given the program arguments.

    Args:
        args (Namespace): The arguments.

    Returns:
        chainer.Chain: The corresponding attention module.

    dotlocationnoattz9chainer supports only noatt, dot, and location attention.)
atyper   r   r   adimrG   rO   rP   rc   NotImplementedError)argsr]   r   r   r   att_for  s   


	
rk   )r+   chainer.functions	functionsr'   chainer.linkslinksr   numpyr/   Chainr   rG   rc   rk   r   r   r   r   <module>   s    Nq: