U
    “±Ëh,  ã                   @   s–   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
 d dlmZ d dlmZ d d	lmZ e  e¡ZG d
d„ de
ƒZG dd„ deƒZdS )é    N)ÚOptional)ÚAttentionMask)ÚFusionBartAttention)ÚFusionOptions)ÚFusionReshape)Únumpy_helper)Ú	OnnxModel)ÚBertOnnxModelc                       s*   e Zd Zedœ‡ fdd„Zdd„ Z‡  ZS )ÚFusionBartReshape)Úmodelc                    s   t ƒ  |¡ d S ©N)ÚsuperÚ__init__)Úselfr   ©Ú	__class__© úL/tmp/pip-unpacked-wheel-socb9apf/onnxruntime/transformers/onnx_model_bart.pyr      s    zFusionBartReshape.__init__c                 C   sp  |j d |krd S ||j d  }|jdks8t|j ƒdkr<d S | j |dddgdddg|¡}|d krfd S |\}}}g }	| j |j d ¡}
|
dkr˜|	 d¡ | j |dddgdddg|¡}|d kr(| j |j d ¡}| j |j d ¡}| j |j d	 ¡}|d ks|d ks|d krd S t 	|¡}t 	|¡}t 	|¡}t|ƒdks`t|ƒdks`t|ƒdkrdd S |d d
krŽ|d dkrŽ|d dks’d S |	 
|¡ |	 
|¡ |	 
|¡ | j |ddgddg|¡}| j |dgdg|¡}|d k	rð|}n|d k	r |}nd S |d
 }|j d }|  |	||¡ nD|\}}}| j |j d ¡}
|
dkrX|	 d¡ | j |j d ¡}| j |j d	 ¡}|d ks|d kr”d S t 	|¡}t 	|¡}t|ƒdksÄt|ƒdkrÈd S |d dkrä|d dksèd S |	 
|¡ |	 
|¡ | j |dddgdddg|¡}|d kr(d S |d
 }|j d }|j d |ksZ|j d |kr^d S |  |	||¡ d S )Né   ZConcaté   Z	UnsqueezeZGatherZShaper   é   é   éÿÿÿÿZAddZMatMulZMul)ÚinputZop_typeÚlenr   Zmatch_parent_pathZget_constant_valueÚappendZget_initializerr   Zto_arrayÚextendZreplace_reshape_node)r   Zreshape_nodeZinput_name_to_nodesZoutput_name_to_nodeZconcat_nodeZpath0Ú_Zgather_0Zshape_0ÚshapeZgather_valueÚpath1Zinput_1_protoZinput_2_protoZinput_3_protoZinput_1Zinput_2Zinput_3Zgemm_path_with_biasZgemm_path_no_biasZ	gemm_pathZ
top_matmulZ
root_inputZgather_1Zshape_1r   r   r   Úfuse   s¨    ü

ü



**


   ÿ









   ÿ

 zFusionBartReshape.fuse)Ú__name__Ú
__module__Ú__qualname__r   r   r    Ú__classcell__r   r   r   r   r
      s   r
   c                       sF   e Zd Z‡ fdd„Zdee edœ‡ fdd„Zdd	„ Zd
d„ Z	‡  Z
S )ÚBartOnnxModelc                    s>   t ƒ  |||¡ t| ƒ| _t| | j| j| jƒ| _t| ƒ| _	d S r   )
r   r   r   Zattention_maskr   Úhidden_sizeÚ	num_headsÚattention_fusionr
   Úbart_reshape_fusion_preprocess)r   r   r'   r&   r   r   r   r   |   s    
zBartOnnxModel.__init__NF)ÚoptionsÚadd_dynamic_axesc                    s>   |d krdn|j | j_ |d kr"dn|j| j_tƒ  ||¡ d S )NF)Zuse_multi_head_attentionr(   Z!disable_multi_head_attention_biasr   Úoptimize)r   r*   r+   r   r   r   r,   ‚   s    ÿzBartOnnxModel.optimizec                 C   s   | j  ¡  d S r   )r(   Úapply©r   r   r   r   Úfuse_attention‰   s    zBartOnnxModel.fuse_attentionc                 C   s   |   ¡  | j ¡  d S r   )Zadjust_reshape_and_expandr)   r-   r.   r   r   r   Ú
preprocessŒ   s    zBartOnnxModel.preprocess)NF)r!   r"   r#   r   r   r   Úboolr,   r/   r0   r$   r   r   r   r   r%   {   s   r%   )ÚloggingÚtypingr   Zfusion_attentionr   Zfusion_bart_attentionr   Zfusion_optionsr   Zfusion_reshaper   Zonnxr   Z
onnx_modelr   Zonnx_model_bertr	   Ú	getLoggerr!   Úloggerr
   r%   r   r   r   r   Ú<module>   s   
h