o
    –hþ  ã                   @   s²   d dl Z d dlmZ d dlm  m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
ed
krWe  dd¡Ze  dd¡Ze
ddddƒZeeeeƒƒ dS dS )é    Nc                       s*   e Zd ZdZd‡ fdd„	Zdd„ Z‡  ZS )ÚPairwiseBilinearz´ A bilinear module that deals with broadcasting for efficient memory usage.
    Input: tensors of sizes (N x L1 x D1) and (N x L2 x D2)
    Output: tensor of size (N x L1 x L2 x O)Tc                    sV   t ƒ  ¡  || _|| _|| _t t |||¡¡| _	|r&t t |¡¡| _
d S d| _
d S )Nr   )ÚsuperÚ__init__Úinput1_sizeÚinput2_sizeÚoutput_sizeÚnnÚ	ParameterÚtorchÚTensorÚweightÚbias)Úselfr   r   r   r   ©Ú	__class__© úX/var/www/html/env_mimamsha/lib/python3.10/site-packages/stanza/models/common/biaffine.pyr   	   s   
$zPairwiseBilinear.__init__c                 C   s¸   t | ¡ ƒ}t | ¡ ƒ}|d |d |d | jg}t | d|d ¡| j d| j| j ¡¡}| dd¡}| |d |d | j |d ¡ 	|¡}| |d |d | j|d ¡ dd¡}|S )Nr   é   éÿÿÿÿé   é   )
ÚlistÚsizer   r
   ÚmmÚviewr   r   Ú	transposeÚbmm)r   Úinput1Úinput2r   r   r   ÚintermediateÚoutputr   r   r   Úforward   s   *&&zPairwiseBilinear.forward)T)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r!   Ú__classcell__r   r   r   r   r      s    
r   c                       ó$   e Zd Z‡ fdd„Zdd„ Z‡  ZS )ÚBiaffineScorerc                    sB   t ƒ  ¡  t |d |d |¡| _| jjj ¡  | jjj ¡  d S ©Nr   )	r   r   r   ÚBilinearÚW_bilinr   ÚdataÚzero_r   ©r   r   r   r   r   r   r   r   $   s   
zBiaffineScorer.__init__c                 C   ó|   t  ||jg | ¡ d d… ¢d‘R Ž gt| ¡ ƒd ¡}t  ||jg | ¡ d d… ¢d‘R Ž gt| ¡ ƒd ¡}|  ||¡S ©Nr   r   ©r
   ÚcatÚnew_onesr   Úlenr+   ©r   r   r   r   r   r   r!   +   ó   88zBiaffineScorer.forward©r"   r#   r$   r   r!   r&   r   r   r   r   r(   #   ó    r(   c                       r'   )ÚPairwiseBiaffineScorerc                    s@   t ƒ  ¡  t|d |d |ƒ| _| jjj ¡  | jjj ¡  d S r)   )r   r   r   r+   r   r,   r-   r   r.   r   r   r   r   1   s   
zPairwiseBiaffineScorer.__init__c                 C   r/   r0   r1   r5   r   r   r   r!   8   r6   zPairwiseBiaffineScorer.forwardr7   r   r   r   r   r9   0   r8   r9   c                       s.   e Zd Zejddf‡ fdd„	Zdd„ Z‡  ZS )ÚDeepBiaffineScorerr   Tc                    s^   t ƒ  ¡  t ||¡| _t ||¡| _|| _|r t|||ƒ| _nt	|||ƒ| _t 
|¡| _d S ©N)r   r   r   ÚLinearÚW1ÚW2Úhidden_funcr9   Úscorerr(   ÚDropoutÚdropout)r   r   r   Úhidden_sizer   r?   rB   Úpairwiser   r   r   r   >   s   
zDeepBiaffineScorer.__init__c              
   C   s0   |   |  |  |  |¡¡¡|  |  |  |¡¡¡¡S r;   )r@   rB   r?   r=   r>   r5   r   r   r   r!   I   s   0zDeepBiaffineScorer.forward)r"   r#   r$   ÚFÚrelur   r!   r&   r   r   r   r   r:   =   s    r:   Ú__main__r   é   é   é   é   )r
   Útorch.nnr   Útorch.nn.functionalÚ
functionalrE   ÚModuler   r(   r9   r:   r"   ÚrandnÚx1Úx2r@   Úprintr   r   r   r   Ú<module>   s    ü