o
    ¢Äi  ã                   @   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 e  e¡ZG d	d
„ d
eƒZG dd„ deƒZdS )é    N)ÚAttentionMask)ÚFusionBartAttention)ÚFusionOptions)ÚFusionReshape)Únumpy_helper)Ú	OnnxModel)ÚBertOnnxModelc                       s*   e Zd Zdef‡ fdd„Zdd„ Z‡  ZS )ÚFusionBartReshapeÚmodelc                    s   t ƒ  |¡ d S ©N)ÚsuperÚ__init__)Úselfr
   ©Ú	__class__© úb/home/kim/smarthome/.venv/lib/python3.10/site-packages/onnxruntime/transformers/onnx_model_bart.pyr      s   zFusionBartReshape.__init__c                 C   sN  |j d |vr	d S ||j d  }|jdkst|j ƒdkrd S | j |g d¢g d¢|¡}|d u r1d S |\}}}g }	| j |j d ¡}
|
dkrJ|	 d¡ | j |g d¢g d¢|¡}|d u r| j |j d ¡}| j |j d ¡}| j |j d	 ¡}|d u sƒ|d u sƒ|d u r…d S t 	|¡}t 	|¡}t 	|¡}t|ƒdks¦t|ƒdks¦t|ƒdkr¨d 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 urê|}n	|d urñ|}nd S |d
 }|j d }|  |	||¡ d S |\}}}| j |j d ¡}
|
dkr|	 d¡ | j |j d ¡}| j |j d	 ¡}|d u s9|d u r;d S t 	|¡}t 	|¡}t|ƒdksSt|ƒdkrUd S |d dkrc|d dksed S |	 
|¡ |	 
|¡ | j |g d¢g d¢|¡}|d u rƒd S |d
 }|j d }|j d |ksœ|j d |kržd S |  |	||¡ d S )Né   ZConcaté   )Z	UnsqueezeZGatherZShape)r   r   r   r   )r   r   r   é   é   éÿÿÿÿÚAddÚMatMul)ZMulr   r   )r   r   r   )Ú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_valueZ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                       sJ   e Zd Zd‡ fdd„	ZddedB def‡ fdd	„Zd
d„ Zdd„ Z‡  Z	S )ÚBartOnnxModelÚhfc                    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'   Z
model_implr   r   r   r   {   s   
zBartOnnxModel.__init__NFÚoptionsÚadd_dynamic_axesc                    s>   |d u rdn|j | j_ |d u 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/   r0   r   r   r   Ú
preprocess‹   s   zBartOnnxModel.preprocess)r&   r-   )
r!   r"   r#   r   r   Úboolr.   r1   r2   r$   r   r   r   r   r%   z   s
    r%   )ÚloggingZ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