
    g5                     f    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e      Zy)	    )	getLogger)Dict)Fusion)helper)	OnnxModelc                   4     e Zd Zdef fdZdedefdZ xZS )FusionBiasSplitGelumodelc                 (    t         |   |dd       y )NBiasSplitGeluGelu)super__init__)selfr
   	__class__s     b/var/www/openai/venv/lib/python3.12/site-packages/onnxruntime/transformers/fusion_biassplitgelu.pyr   zFusionBiasSplitGelu.__init__   s    8    input_name_to_nodesoutput_name_to_nodec                 <   |j                   d   |vry||j                   d      }t        |      dk7  s|d   j                  dk7  ry|d   }| j                  j	                  |dd|      }|y| j                  j                  |dd      d	k7  ry|j                  d   }| j                  j                  |g d
g d|      }|"| j                  j                  |g dg d|      }||d   j                  d   |k7  ry| j                  j                  |ddgddg|      }	|	|	d   |vry| j                  j	                  |dd|      }
|
y|
j                  d   |j                  d   k7  ryg ||	d   |||
|}|j                   d   }| j                  j                  ||g||      st        j                  d       y|d   }| j                  j                  |      \  }}t        |t              sy| j                  j                  |       | j                  j!                  dd      }t#        j$                  d|j                  d|z
     |j                  |   g|g|      }d|_        | j(                  j+                  |       | j,                  | j.                  |<   y)a  
        [root] --->Add -------------------->  Slice ---------------> Mul -->
                   |                            ^                    ^
                   |                            |                    |
                   +----------------------------+---Slice --> Gelu---+
                   |                            |     ^
                   |                            |-----|
                   |                            |     |
                   |                           Mul   Mul
                   |                            ^     ^
                   v                            |     |
                  Shape ---> Gather --> Add --> Div --+
        r   N   MulSlicegMbP?)delta   )DivAddGatherShaper   )r   r   r   r   r   )r   r   r   r   r    r   )r   r   r   r   r   r   r      zBSkip fuse BiasSplitGelu since it is not safe to fuse the subgraph.r   )name_prefix)inputsoutputsnamezcom.microsoft)outputlenop_typer
   match_parentfind_constant_inputinputmatch_parent_pathis_safe_to_fuse_nodesloggerinfoget_constant_input
isinstanceintnodes_to_removeextendcreate_node_namer   	make_nodedomainnodes_to_addappendthis_graph_namenode_name_to_graph_name)r   	gelu_noder   r   childrenmul_after_geluslice_before_gelu
add_outputstart_index_nodesend_index_nodesslice_before_mulsubgraph_nodessubgraph_outputadd_node
bias_index_value	node_name
fused_nodes                     r   fusezFusionBiasSplitGelu.fuse   s    A&99&y'7'7':;x=A!!4!4!=!! JJ33IwK^_$::))*;Ru)MQRR&,,Q/
 JJ884	
 $ $

 < <!?"#	! $(9"(=(C(CA(F*(T**667H5RW.[\^_Z`buv #q'9AR'R::22>7AObc# ""1%):)@)@)CC

A
 
 	

 
 
 )//2zz//_-/BDW
 KK\]$R(!ZZ::8D
F*c*##N3JJ//_/]	%%NN1z>2HNN:4NO$%	

 ,
  ,262F2F$$Y/r   )__name__
__module____qualname__r   r   r   rL   __classcell__)r   s   @r   r	   r	      s(    9i 9\G4 \Gd \Gr   r	   N)loggingr   typingr   fusion_baser   onnxr   
onnx_modelr   rM   r/   r	    r   r   <module>rW      s0   
      	8	`G& `Gr   