o
    3Ih                     @   sr   d dl mZ d dlmZ d dlmZ d dlmZ d dlZ	d dl
mZmZmZ d dlmZ eeZG dd	 d	ZdS )
    )defaultdict)Sequence)	getLogger)AnyN)	NodeProtoTensorProtohelper)	OnnxModelc                   @   s   e Zd ZdZ	d%dededeee B defddZd	efd
dZde	de
eee	 f de
ee	f fddZdd Zd&dededee dedef
ddZdefddZdee	 fdd Zdee	 d!ee	 fd"d#Zd$S )'Fusionz%
    Base class for Graph Fusion
     modelfused_op_typesearch_op_typesdescriptionc                 C   sh   t |tr|gn|| _|| _|r| d| dn|| _|| _g | _g | _d| _i | _	d | _
tt| _d S )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    r   d/home/air/sanwanet/gpt-api/venv/lib/python3.10/site-packages/onnxruntime/transformers/fusion_base.py__init__   s   zFusion.__init__fused_op_namec                 C   s   | j |  d7  < dS )z7
        Increase counter of a fused operator.
           N)r   )r   r   r   r   r   increase_counter)   s   zFusion.increase_counternodeinput_name_to_nodesoutput_name_to_nodec                 C   s   t )z,Interface for fusion that starts from a node)NotImplementedError)r   r"   r#   r$   r   r   r   fuse/   s   zFusion.fusec           
      C   s<  t d| j d | j }| j }| jD ]$}| j|D ]}| j|}|du r/t	d|j
| _| ||| qqdd | jD }| jr_| j D ]\}}|r]t d| d|  qLn|| j}	|	d	krut d| j d|	  | j| j | j| j| j | jr| j  dS | js| jr| j  dS dS )
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 graphc                 S   s   g | ]}|j qS r   )op_type).0r"   r   r   r   
<listcomp>J   s    z Fusion.apply.<locals>.<listcomp>zFused z: r   )loggerdebugr   r   r#   r$   r   get_nodes_by_op_typeget_graph_by_node	Exceptionnamer   r&   r   r   itemsinfocountr   remove_nodesr   	add_nodesr   r   update_graph)
r   r#   r$   search_op_typer"   graphop_listkeyvaluer2   r   r   r   apply8   s8   


zFusion.applyTr/   	data_typedimsvalsrawc           	      C   sz   |r)t |}t|tjstj||d }n|| }t j||||dd}n
t j||||dd}| j	
|| j |S )N)dtypeT)r/   r<   r=   r>   r?   F)r   tensor_dtype_to_np_dtyper   npndarrayarraytobytesastypemake_tensorr   add_initializerr   )	r   r/   r<   r=   r>   r?   np_typebytestensorr   r   r   rH   \   s*   
zFusion.add_initializerrK   c                 C   s   | j | d S N)r   remove_initializer)r   rK   r   r   r   rM   v   s   zFusion.remove_initializernodesc                 C   s$   |D ]}|| j vr| j | qd S rL   r   append)r   rN   r"   r   r   r   add_nodes_to_removey   s
   
zFusion.add_nodes_to_removenodes_to_keepc                 C   s,   |D ]}|| j vr||vr| j | qd S rL   rO   )r   rN   rR   r"   r   r   r   &add_nodes_to_remove_with_nodes_to_keep   s
   z-Fusion.add_nodes_to_remove_with_nodes_to_keepN)r   )T)__name__
__module____qualname____doc__r	   r   listr   r!   r   dictr&   r;   r   r   r   boolrH   r   rM   rQ   rS   r   r   r   r   r
      s2    	



	$$r
   )collectionsr   collections.abcr   loggingr   typingr   numpyrB   onnxr   r   r   
onnx_modelr	   rT   r*   r
   r   r   r   r   <module>   s   