
    g=                         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 j"                  e      Z G d
 de
      Z G d de      Zy)    N)Optional)AttentionMask)FusionBartAttention)FusionOptions)FusionReshape)numpy_helper)	OnnxModel)BertOnnxModelc                   *     e Zd Zdef fdZd Z xZS )FusionBartReshapemodelc                 $    t         |   |       y N)super__init__)selfr   	__class__s     ]/var/www/openai/venv/lib/python3.12/site-packages/onnxruntime/transformers/onnx_model_bart.pyr   zFusionBartReshape.__init__   s        c                    |j                   d   |vry ||j                   d      }|j                  dk7  st        |j                         dk7  ry | j                  j	                  |g dg d|      }|y |\  }}}g }	| j                  j                  |j                   d         }
|
dk(  r|	j                  d       | j                  j	                  |g dg d|      }|| j                  j                  |j                   d         }| j                  j                  |j                   d         }| j                  j                  |j                   d	         }|||y t        j                  |      }t        j                  |      }t        j                  |      }t        |      dk7  st        |      dk7  st        |      dk7  ry |d   d
k(  r|d   dkD  r|d   dkD  sy |	j                  |       |	j                  |       |	j                  |       | j                  j	                  |ddgddg|      }| j                  j	                  |dgdg|      }||}n||}ny |d
   }|j                   d   }| j                  |	||       y |\  }}}| j                  j                  |j                   d         }
|
dk(  r|	j                  d       | j                  j                  |j                   d         }| j                  j                  |j                   d	         }||y t        j                  |      }t        j                  |      }t        |      dk7  st        |      dk7  ry |d   dkD  r|d   dkD  sy |	j                  |       |	j                  |       | j                  j	                  |g dg d|      }|y |d
   }|j                   d   }|j                   d   |k7  s|j                   d   |k7  ry | j                  |	||       y )N   Concat   )	UnsqueezeGatherShape)r   r   r   r   )r   r   r         AddMatMul)Mulr    r!   )r   r   r   )inputop_typelenr   match_parent_pathget_constant_valueappendget_initializerr   to_arrayextendreplace_reshape_node)r   reshape_nodeinput_name_to_nodesoutput_name_to_nodeconcat_nodepath0_gather_0shape_0shapegather_valuepath1input_1_protoinput_2_protoinput_3_protoinput_1input_2input_3gemm_path_with_biasgemm_path_no_bias	gemm_path
top_matmul
root_inputgather_1shape_1s                            r   fusezFusionBartReshape.fuse   s   a (;;),*<*<Q*?@(*c+2C2C.D.I

,,,	
 =!&Hgzz44X^^A5FG1LLO

,,,	
 = JJ66{7H7H7KLM JJ66{7H7H7KLM JJ66{7H7H7KLM$(=AV"++M:G"++M:G"++M:G7|q CLA$5W9JAJ"$aGAJNLL!LL!LL!"&**">">uh/!Q9L# !%

 < <\H:XYWZ\o p"./	".-	"2J#))!,J%%e\;G%*"Q'::889JKLq Q JJ66{7H7H7KLM JJ66{7H7H7KLM$(="++M:G"++M:G7|q CLA$5AJNwqzA~LL!LL!

446	CVI  "2J#))!,J}}Q:-q1AZ1O%%e\;Gr   )__name__
__module____qualname__r	   r   rE   __classcell__r   s   @r   r   r      s     i  aHr   r   c                   H     e Zd Zd fd	Zddee   def fdZd Zd Z	 xZ
S )	BartOnnxModelc                     t         |   |||       t        |       | _        t	        | | j
                  | j                  | j                        | _        t        |       | _	        y r   )
r   r   r   attention_maskr   hidden_size	num_headsattention_fusionr   bart_reshape_fusion_preprocess)r   r   rP   rO   
model_implr   s        r   r   zBartOnnxModel.__init__|   sR    	;7+D1 3D$:J:JDNN\`\o\o p.?.E+r   optionsadd_dynamic_axesc                     |dn|j                   | j                  _         |dn|j                  | j                  _        t        |   ||       y NF)use_multi_head_attentionrQ   !disable_multi_head_attention_biasr   optimize)r   rT   rU   r   s      r   rZ   zBartOnnxModel.optimize   sM    BI/W^WwWw6_E'*S*S 	? 	"23r   c                 8    | j                   j                          y r   )rQ   applyr   s    r   fuse_attentionzBartOnnxModel.fuse_attention   s    ##%r   c                 X    | j                          | j                  j                          y r   )adjust_reshape_and_expandrR   r\   r]   s    r   
preprocesszBartOnnxModel.preprocess   s     &&(++113r   )hfrW   )rF   rG   rH   r   r   r   boolrZ   r^   ra   rI   rJ   s   @r   rL   rL   {   s,    F4 7 4RV 4&4r   rL   )loggingtypingr   fusion_attentionr   fusion_bart_attentionr   fusion_optionsr   fusion_reshaper   onnxr   
onnx_modelr	   onnx_model_bertr
   	getLoggerrF   loggerr   rL    r   r   <module>rp      sQ   
   * 5 ( (    )			8	$eH eHP4M 4r   