
    ge                         d dl mZ d dlmZ d dlmZmZmZmZm	Z	m
Z
 d dlZd dlmZmZ d dlmZ  ee      Z G d d      Zy)	    )defaultdict)	getLogger)AnyDictListOptionalSequenceUnionN)	NodeProtohelper)	OnnxModelc                       e Zd ZdZ	 ddededeeee   f   defdZdefdZ	d	e
d
eeee
   f   deee
f   fdZd Zddededee   dedef
dZdee
   fdZdee
   dee
   fdZy)Fusionz%
    Base class for Graph Fusion
    modelfused_op_typesearch_op_typesdescriptionc                     t        |t              r|gn|| _        || _        |r| d| dn|| _        || _        g | _        g | _        d| _        i | _	        d | _
        t        t              | _        y )N()F)
isinstancestrr   r   r   r   nodes_to_removenodes_to_addprune_graphnode_name_to_graph_namethis_graph_namer   intfused_count)selfr   r   r   r   s        Y/var/www/openai/venv/lib/python3.12/site-packages/onnxruntime/transformers/fusion_base.py__init__zFusion.__init__   s|     @J/[^?_?*;et"/EP=/;-q AVc %
%'"$!&-/$.2(3C(8    fused_op_namec                 2    | j                   |xx   dz  cc<   y)z7
        Increase counter of a fused operator.
           N)r   )r    r$   s     r!   increase_counterzFusion.increase_counter(   s     	'1,'r#   nodeinput_name_to_nodesoutput_name_to_nodec                     t         )z,Interface for fusion that starts from a node)NotImplementedError)r    r(   r)   r*   s       r!   fusezFusion.fuse.   s
     "!r#   c                 ~   t         j                  d| j                   d       | j                  j	                         }| j                  j                         }| j                  D ]n  }| j                  j                  |      D ]N  }| j                  j                  |      }|t        d      |j                  | _        | j                  |||       P p | j                  D cg c]  }|j                   }}| j                  rA| j                  j!                         D ]#  \  }}|s	t         j#                  d| d|        % nE|j%                  | j&                        }	|	dkD  r%t         j#                  d| j                   d|	        | j                  j)                  | j*                         | j                  j-                  | j                  | j.                         | j0                  r| j                  j1                          y| j*                  s| j                  r| j                  j3                          yyc c}w )z
        Apply graph fusion on the whole model graph.
        It searched nodes of given operators, and start fusion on each of those nodes.
        zstart z
 fusion...NzCan not find node in any graphzFused z: r   )loggerdebugr   r   r)   r*   r   get_nodes_by_op_typeget_graph_by_node	Exceptionnamer   r-   r   op_typer   itemsinfocountr   remove_nodesr   	add_nodesr   r   update_graph)
r    r)   r*   search_op_typer(   graphop_listkeyvaluer8   s
             r!   applyzFusion.apply7   s   
 	vd../z:;"jj<<>"jj<<> #22N

77G

44T:=#$DEE',zz$		$ 35HI H 3 -1,=,=>,=D4<<,=>"..446
UKK&Rw 78 7 MM$"4"45EqyfT%5%5$6b@A

 4 45

T..0L0LMJJ""$!!T%6%6JJ##% &7 ?s   #H:r4   	data_typedimsvalsrawc                    |rt        j                  |      }t        |t        j                        s&t        j
                  ||      j                         }n|j                  |      j                         }t        j                  ||||d      }nt        j                  ||||d      }| j                  j                  || j                         |S )N)dtypeT)r4   rB   rC   rD   rE   F)r   tensor_dtype_to_np_dtyper   npndarrayarraytobytesastypemake_tensorr   add_initializerr   )	r    r4   rB   rC   rD   rE   np_typebytestensors	            r!   rO   zFusion.add_initializer[   s    55i@GdBJJ/W5==?G,446''#F ''#F 	

""64+?+?@r#   nodesc                 f    |D ],  }|| j                   vs| j                   j                  |       . y Nr   append)r    rS   r(   s      r!   add_nodes_to_removezFusion.add_nodes_to_removeu   s0     D4///$$++D1 r#   nodes_to_keepc                 p    |D ]1  }|| j                   vs||vs| j                   j                  |       3 y rU   rV   )r    rS   rY   r(   s       r!   &add_nodes_to_remove_with_nodes_to_keepz-Fusion.add_nodes_to_remove_with_nodes_to_keep   s5    D4///D4M$$++D1 r#   N) )T)__name__
__module____qualname____doc__r   r   r
   r   r"   r'   r   r   r-   rA   r   r	   r   boolrO   rX   r[    r#   r!   r   r      s     99 9 sDI~.	9
 9&-c -"" "#tI"67" "#y.1	""&HC C x} TW ^b 42i 2"2DO 2\`aj\k 2r#   r   )collectionsr   loggingr   typingr   r   r   r   r	   r
   numpyrI   onnxr   r   
onnx_modelr   r]   r/   r   rb   r#   r!   <module>ri      s2   
 $  = =  "  	8	y2 y2r#   