U
    T?he                     @   sv   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 dZdS )	    )defaultdict)	getLogger)AnyDictListOptionalSequenceUnionN)	NodeProtohelper)	OnnxModelc                   @   s   e Zd ZdZdeeeeee f edddZedddZ	e
eeee
 f eee
f d	d
dZdd Zdeeee eedddZee
 dddZee
 ee
 d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|| _|| _|r0| 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   V/var/www/html/venv/lib/python3.8/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 dS )z,Interface for fusion that starts from a nodeN)NotImplementedError)r   r%   r&   r'   r   r   r    fuse.   s    zFusion.fusec           
      C   s<  t d| j d | j }| j }| jD ]H}| j|D ]6}| j|}|dkr^t	d|j
| _| ||| q>q.dd | jD }| jr| j D ]"\}}|rt d| d|  qn,|| j}	|	d	krt d| j d|	  | j| j | j| j| j | jr| j  n| js.| jr8| j  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   )Zop_type).0r%   r   r   r    
<listcomp>I   s     z Fusion.apply.<locals>.<listcomp>zFused z: r   )loggerdebugr   r   r&   r'   r   Zget_nodes_by_op_typeZget_graph_by_node	Exceptionnamer   r)   r   r   itemsinfocountr   Zremove_nodesr   Z	add_nodesr   r   Zupdate_graph)
r   r&   r'   Zsearch_op_typer%   graphZop_listkeyvaluer2   r   r   r    apply7   s0    


zFusion.applyTr/   	data_typedimsvalsrawc           	      C   sz   |rRt |}t|tjs.tj||d }n|| }t j||||dd}nt j||||dd}| j	
|| j |S )N)ZdtypeTr7   F)r   Ztensor_dtype_to_np_dtyper   npZndarrayarraytobytesZastypeZmake_tensorr   add_initializerr   )	r   r/   r8   r9   r:   r;   Znp_typebytesZtensorr   r   r    r?   [   s*    
zFusion.add_initializer)nodesc                 C   s$   |D ]}|| j kr| j | qd S Nr   append)r   rA   r%   r   r   r    add_nodes_to_removeu   s    
zFusion.add_nodes_to_remove)rA   nodes_to_keepc                 C   s,   |D ]"}|| j kr||kr| j | qd S rB   rC   )r   rA   rF   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   r	   r   r!   r$   r
   r   r)   r6   r   r   r   boolr?   rE   rG   r   r   r   r    r      s"   	 
	$r   )collectionsr   loggingr   typingr   r   r   r   r   r	   numpyr<   Zonnxr
   r   Z
onnx_modelr   rH   r,   r   r   r   r   r    <module>   s    