
    h                     n    S SK Jr  S SKJr  S SKJr  S SKJrJrJ	r	  S SK
Jr  \" \5      r " S S\5      rg)	    )	getLogger)Fusion)NumpyHelper)	NodeProtoTensorProtohelper)	OnnxModelc                      ^  \ rS rSrS\4U 4S jjrS\S\S-  4S jrS\	S\S-  4S	 jr
S
\S\\	\\   4   S\\	\4   4S jrSrU =r$ )FusionGemmFastGelu   modelc                 F   > [         TU ]  USSS5        S U l        SU l        g )NGemmFastGeluFastGeluF)super__init__shape_infershape_infer_done)selfr   	__class__s     f/var/www/fran/franai/venv/lib/python3.13/site-packages/onnxruntime/transformers/fusion_gemmfastgelu.pyr   FusionGemmFastGelu.__init__   s&    
NK %    tensor_protoreturnNc                     UR                   R                  R                  S5      (       a3  [        UR                   R                  R                  R
                  5      $ g )Nshape)typetensor_typeHasFieldlenr   dim)r   r   s     r    get_dimensions_from_tensor_proto3FusionGemmFastGelu.get_dimensions_from_tensor_proto   sE    ((11'::|((44::>>??r   
input_namec                 @   U R                   R                  U5      nU(       a  U R                  U5      $ U R                  (       d%  U R                   R	                  SS9U l        SU l        U R
                  b(  U R                  U R
                  R                  U   5      $ g )NT)update)r   find_graph_inputr#   r   infer_runtime_shaper   	known_vi_)r   r%   graph_inputs      r   get_dimensions!FusionGemmFastGelu.get_dimensions   s    jj11*=88EE$$#zz==T=JD$(D!'889I9I9S9ST^9_``r   nodeinput_name_to_nodesoutput_name_to_nodec                 8   Sn[        UR                  5      S:X  a  SnU R                  R                  US/S/5      nUc  gUS   nSnSnSn	[	        UR                  5       H[  u  pU R                  R                  U5      nUc   U R                  UR                  U
   5      nMC  U
n[        R                  " U5      n	M]     U	c  g[        U	R                  5      S:w  a  gU[        U	R                  5      :  a  gSnU(       av  Sn[	        UR                  5       H=  u  pU R                  R                  U5      nUc  M%  U
n[        R                  " U5      n  O   Uc  g[        UR                  5      S:w  a  gX/nU R                  R                  XR                  S   /X#5      (       d  gU R                  R                  U5        U(       a/  UR                  SU-
     UR                  U   UR                  U   /O UR                  SU-
     UR                  U   /n[        R                  " S	UUR                  U R                  R!                  S	5      S
9nSUl        U R$                  R'                  U5        U R(                  U R*                  UR,                  '   g)z{
This pattern is from PyTorch bert model
Fuse MatMul with FastGelu into one node:

    [root] --> MatMul --> FastGelu -->

F   TMatMulr   N   r   )inputsoutputsnamezcom.microsoft)r!   inputr   match_parent_path	enumerateget_initializerr,   r   to_arrayr   is_safe_to_fuse_nodesoutputnodes_to_removeextendr   	make_nodecreate_node_namedomainnodes_to_addappendthis_graph_namenode_name_to_graph_namer8   )r   r.   r/   r0   has_biasmatch_nodesmatmulweight_indexx_dimsweightir9   initializer
bias_indexbias_weightsubgraph_nodesr6   
fused_nodes                     r   fuseFusionGemmFastGelu.fuse*   sl    tzz?aHjj224(aSIQ !&,,/HA**44U;K",,V\\!_= $--k: 0 >v||!C%% 
K%djj1"jj88?&
)22;? 2 ";$$%*zz//[[^,.A
 
 ##N3  \\!l*+V\\,-GT^I_`,,q</0&,,|2LM 	 %%KK,,^<	

 ,
  ,8<8L8L$$Z__5r   )r   r   )__name__
__module____qualname____firstlineno__r	   r   r   intr#   strr,   r   dictlistrU   __static_attributes____classcell__)r   s   @r   r   r      s    &i &
[ SSWZ  t OMOM "#tI"67OM "#y.1	OM OMr   r   N)loggingr   fusion_baser   fusion_utilsr   onnxr   r   r   
onnx_modelr	   rW   loggerr    r   r   <module>rh      s3      $ / /  	8	iM iMr   