o
    –h#!  ã                   @   s°   d Z ddlZddlZddlmZ ddlZddlZddlm	  m
  mZ e d¡ZG dd„ dejƒZG dd„ dejƒZG dd	„ d	ejƒZG d
d„ dejƒZG dd„ dejƒZdS )z?
Pytorch implementation of basic sequence to Sequence modules.
é    NÚstanzac                       ó*   e Zd ZdZ‡ fdd„Zddd„Z‡  ZS )	ÚBasicAttentionz&
    A basic MLP attention layer.
    c                    sr   t t| ƒ ¡  tj||dd| _t ||¡| _tj|ddd| _tj|d |dd| _t 	¡ | _
tjdd| _d S ©NF©Úbiasé   é   ©Údim)Úsuperr   Ú__init__ÚnnÚLinearÚ	linear_inÚlinear_cÚlinear_vÚ
linear_outÚTanhÚtanhÚSoftmaxÚsm©Úselfr   ©Ú	__class__© ú_/var/www/html/env_mimamsha/lib/python3.10/site-packages/stanza/models/common/seq2seq_modules.pyr      s   
zBasicAttention.__init__NFc                 C   sê   |  d¡}|  d¡}|  d¡}|  |¡}|  | ¡  d|¡¡ |||¡}	| d¡ |¡|	 }
|  |
¡}
|  |
 d|¡¡ ||¡}
|durL|
 	|t
j ¡ |  |
¡}
|rU|
S t |
 d¡|¡ d¡}t ||fd¡}|  |  |¡¡}||
fS )úK
        input: batch x dim
        context: batch x sourceL x dim
        r   r   r	   éÿÿÿÿN)Úsizer   r   Ú
contiguousÚviewÚ	unsqueezeÚ	expand_asr   r   Úmasked_fill_ÚconstantÚINFINITY_NUMBERr   ÚtorchÚbmmÚsqueezeÚcatr   )r   ÚinputÚcontextÚmaskÚ	attn_onlyÚ
batch_sizeÚ
source_lenr   ÚtargetÚsourceÚattnÚweighted_contextÚh_tilder   r   r   Úforward   s"   



 

zBasicAttention.forward©NF©Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r7   Ú__classcell__r   r   r   r   r      s    	r   c                       r   )	ÚSoftDotAttentionznSoft Dot Attention.

    Ref: http://www.aclweb.org/anthology/D15-1166
    Adapted from PyTorch OPEN NMT.
    c                    sX   t t| ƒ ¡  tj||dd| _tjdd| _tj|d |dd| _t 	¡ | _
d| _dS )zInitialize layer.Fr   r   r
   r	   N)r   r?   r   r   r   r   r   r   r   r   r   r.   r   r   r   r   r   >   s   

zSoftDotAttention.__init__NFc                 C   sØ   |   |¡ d¡}t ||¡ d¡}|dur)| ¡ | ¡ ks!J dƒ‚| |tj ¡ |r7t 	|d¡}t 
|¡}n|  |¡}|}|rB|S | | d¡d| d¡¡}	t |	|¡ d¡}
t |
|fd¡}|  |  |¡¡}||fS )zpPropagate input through the network.

        input: batch x dim
        context: batch x sourceL x dim
        r	   Nú(Mask size must match the attention size!r   r   )r   r#   r(   r)   r*   r    r%   r&   r'   Úlog_softmaxÚexpr   r"   r+   r   r   )r   r,   r-   r.   r/   Úreturn_logattnr2   r4   Úattn_wÚattn3r5   r6   r   r   r   r7   G   s"   
zSoftDotAttention.forward)NFFr9   r   r   r   r   r?   7   s    	r?   c                       r   )	ÚLinearAttentionzM A linear attention form, inspired by BiDAF:
        a = W (u; v; u o v)
    c                    s\   t t| ƒ ¡  tj|d ddd| _tj|d |dd| _tjdd| _t 	¡ | _
d | _d S )Né   r   Fr   r	   r
   )r   rF   r   r   r   Úlinearr   r   r   r   r   r.   r   r   r   r   r   o   s   

zLinearAttention.__init__NFc                 C   sþ   |  d¡}|  d¡}|  d¡}| d¡ |¡ ¡  d|¡}| ¡  d|¡}	t ||	| |	¡fd¡}
|  |
¡ ||¡}|durR|  ¡ |  ¡ ksJJ dƒ‚| 	|t
j ¡ |  |¡}|r[|S | |d|¡}t ||¡ d¡}t ||fd¡}|  |  |¡¡}||fS ©r   r   r   r	   r   Nr@   )r    r#   r$   r!   r"   r(   r+   ÚmulrH   r%   r&   r'   r   r)   r*   r   r   )r   r,   r-   r.   r/   r0   r1   r   ÚuÚvÚattn_inr4   rE   r5   r6   r   r   r   r7   w   s$   



zLinearAttention.forwardr8   r9   r   r   r   r   rF   j   s    rF   c                       r   )	ÚDeepAttentionzp A deep attention form, invented by Robert:
        u = ReLU(Wx)
        v = ReLU(Wy)
        a = V.(u o v)
    c                    st   t t| ƒ ¡  tj||dd| _tj|ddd| _tj|d |dd| _t ¡ | _	tj
dd| _t ¡ | _d | _d S r   )r   rN   r   r   r   r   r   r   ÚReLUÚrelur   r   r   r   r.   r   r   r   r   r   ›   s   


zDeepAttention.__init__NFc                 C   s  |  d¡}|  d¡}|  d¡}| d¡ |¡ ¡  d|¡}|  |  |¡¡}|  |  | ¡  d|¡¡¡}	|  | |	¡¡ ||¡}
|durW|  ¡ |
  ¡ ksOJ dƒ‚|
 	|t
j ¡ |  |
¡}
|r`|
S |
 |d|¡}t ||¡ d¡}t ||fd¡}|  |  |¡¡}||
fS rI   )r    r#   r$   r!   r"   rP   r   r   rJ   r%   r&   r'   r   r(   r)   r*   r+   r   r   )r   r,   r-   r.   r/   r0   r1   r   rK   rL   r4   rE   r5   r6   r   r   r   r7   ¥   s$   



zDeepAttention.forwardr8   r9   r   r   r   r   rN   ”   s    
rN   c                       s,   e Zd ZdZd
‡ fdd„	Zddd	„Z‡  ZS )ÚLSTMAttentionz4A long short-term memory (LSTM) cell with attention.TÚsoftc                    s    t t| ƒ ¡  || _|| _|| _t ||¡| _|dkr!t	|ƒ| _
n%|dkr+t|ƒ| _
n|dkr5t|ƒ| _
n|dkr?t|ƒ| _
ntd |¡ƒ‚t d |¡¡ dS )zInitialize params.rR   ÚmlprH   Údeepz#Unsupported LSTM attention type: {}zUsing {} attention for LSTM.N)r   rQ   r   Ú
input_sizeÚhidden_sizeÚbatch_firstr   ÚLSTMCellÚ	lstm_cellr?   Úattention_layerr   rF   rN   Ú	ExceptionÚformatÚloggerÚdebug)r   rU   rV   rW   Ú	attn_typer   r   r   r   Å   s   zLSTMAttention.__init__NFc                 C   sâ   | j r	| dd¡}g }g }t| d¡ƒ}|D ]#}	|  ||	 |¡}|\}
}| j|
|||d\}}| |¡ | |¡ qt |d¡j	| d¡g|d  ¡ ¢R Ž }| j rW| dd¡}|rmt 
|d¡}| j rh| dd¡}|||fS ||fS )z$Propagate input through the network.r   r   )r.   rC   )rW   Ú	transposeÚranger    rY   rZ   Úappendr(   r+   r"   Ústack)r   r,   ÚhiddenÚctxÚctx_maskrC   Úoutputr4   ÚstepsÚiÚhyÚcyr6   Úalphar   r   r   r7   Ú   s(   
(
zLSTMAttention.forward)TrR   r8   r9   r   r   r   r   rQ   Â   s    rQ   )r=   Úloggingr(   Útorch.nnr   ÚmathÚnumpyÚnpÚ%stanza.models.common.seq2seq_constantÚmodelsÚcommonÚseq2seq_constantr&   Ú	getLoggerr]   ÚModuler   r?   rF   rN   rQ   r   r   r   r   Ú<module>   s    
(3*.