
    h                         S SK r S SKJr  S SKJr  S SKJr  S SKJr  S SK	J
r
  S SKJr  S SKJr  \ R                  " \5      r " S	 S
\5      r " S S\5      rg)    N)AttentionMask)FusionBartAttention)FusionOptions)FusionReshape)numpy_helper)	OnnxModel)BertOnnxModelc                   6   ^  \ rS rSrS\4U 4S jjrS rSrU =r$ )FusionBartReshape   modelc                 $   > [         TU ]  U5        g N)super__init__)selfr   	__class__s     b/var/www/fran/franai/venv/lib/python3.13/site-packages/onnxruntime/transformers/onnx_model_bart.pyr   FusionBartReshape.__init__   s        c                 R   UR                   S   U;  a  g X1R                   S      nUR                  S:w  d  [        UR                   5      S:w  a  g U R                  R	                  U/ SQ/ SQU5      nUc  g Uu  pgn/ n	U R                  R                  UR                   S   5      n
U
S:X  a  U	R                  S5        U R                  R	                  U/ SQ/ SQU5      nUGc  U R                  R                  UR                   S   5      nU R                  R                  UR                   S   5      nU R                  R                  UR                   S	   5      nUb  Ub  Uc  g [        R                  " U5      n[        R                  " U5      n[        R                  " U5      n[        U5      S:w  d  [        U5      S:w  d  [        U5      S:w  a  g US   S
:X  a  US   S:  a	  US   S:  d  g U	R                  U5        U	R                  U5        U	R                  U5        U R                  R	                  USS/SS/U5      nU R                  R	                  US/S/U5      nUb  UnOUb  UnOg US
   nUR                   S   nU R                  XU5        g Uu  nnnU R                  R                  UR                   S   5      n
U
S:X  a  U	R                  S5        U R                  R                  UR                   S   5      nU R                  R                  UR                   S	   5      nUb  Uc  g [        R                  " U5      n[        R                  " U5      n[        U5      S:w  d  [        U5      S:w  a  g US   S:  a	  US   S:  d  g U	R                  U5        U	R                  U5        U R                  R	                  U/ SQ/ SQU5      nUc  g US
   nUR                   S   nUR                   S   U:w  d  UR                   S   U:w  a  g U R                  XU5        g )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   fuseFusionBartReshape.fuse   s   a (;;)*<*<Q*?@(*c+2C2C.D.I

,,,	
 =!&g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____firstlineno__r   r   rF   __static_attributes____classcell__r   s   @r   r   r      s     i  aH aHr   r   c                   Z   ^  \ rS rSrS
U 4S jjrSS\S-  S\4U 4S jjjrS rS r	S	r
U =r$ )BartOnnxModelz   c                    > [         TU ]  XU5        [        U 5      U l        [	        X R
                  U R                  U R                  5      U l        [        U 5      U l	        g r   )
r   r   r   attention_maskr   hidden_size	num_headsattention_fusionr   bart_reshape_fusion_preprocess)r   r   rV   rU   
model_implr   s        r   r   BartOnnxModel.__init__{   sN    ;7+D1 3D:J:JDNN\`\o\o p.?.E+r   Noptionsadd_dynamic_axesc                    > Uc  SOUR                   U R                  l         Uc  SOUR                  U R                  l        [        TU ]  X5        g NF)use_multi_head_attentionrW   !disable_multi_head_attention_biasr   optimize)r   r[   r\   r   s      r   ra   BartOnnxModel.optimize   sJ    BI/W^WwWw6_E'*S*S 	? 	3r   c                 8    U R                   R                  5         g r   )rW   applyr   s    r   fuse_attentionBartOnnxModel.fuse_attention   s    ##%r   c                 X    U R                  5         U R                  R                  5         g r   )adjust_reshape_and_expandrX   rd   re   s    r   
preprocessBartOnnxModel.preprocess   s     &&(++113r   )rW   rT   rX   )hfr^   )rI   rJ   rK   rL   r   r   boolra   rf   rj   rM   rN   rO   s   @r   rQ   rQ   z   s5    F4 4 4t 4 4&4 4r   rQ   )loggingfusion_attentionr   fusion_bart_attentionr   fusion_optionsr   fusion_reshaper   onnxr   
onnx_modelr   onnx_model_bertr	   	getLoggerrI   loggerr   rQ   rH   r   r   <module>rx      sL   
  * 5 ( (    )			8	$eH eHP4M 4r   