o
    °ƒðjÍ  ã                   @   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   ú¦/tmp/claude-1000/-var-www-html-academy/3f42efd1-c2b7-4d08-9b05-30d86bb35479/scratchpad/piper/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   