o
    °ƒðj÷  ã                   @   s`   d dl mZ d dlmZ d dlmZ d dlmZmZm	Z	 d dl
mZ eeƒZG dd„ deƒZdS )	é    )Ú	getLogger)ÚFusion)ÚNumpyHelper)Ú	NodeProtoÚTensorProtoÚhelper)Ú	OnnxModelc                       sx   e Zd Zdef‡ fdd„ZdededB fdd„Zd	ededB fd
d„Z	de
deeee
 f deee
f fdd„Z‡  ZS )ÚFusionGemmFastGeluÚmodelc                    s"   t ƒ  |ddd¡ d | _d| _d S )NÚGemmFastGeluÚFastGeluF)ÚsuperÚ__init__Úshape_inferÚshape_infer_done)Úselfr
   ©Ú	__class__© ú®/tmp/claude-1000/-var-www-html-academy/3f42efd1-c2b7-4d08-9b05-30d86bb35479/scratchpad/piper/venv/lib/python3.10/site-packages/onnxruntime/transformers/fusion_gemmfastgelu.pyr      s   
zFusionGemmFastGelu.__init__Útensor_protoÚreturnNc                 C   s"   |j j d¡rt|j jjjƒS d S )NÚshape)ÚtypeÚtensor_typeÚHasFieldÚlenr   Údim)r   r   r   r   r   Ú get_dimensions_from_tensor_proto   s   z3FusionGemmFastGelu.get_dimensions_from_tensor_protoÚ
input_namec                 C   sV   | j  |¡}|r|  |¡S | js| j jdd�| _d| _| jd ur)|  | jj| ¡S d S )NT)Úupdate)r
   Úfind_graph_inputr   r   Úinfer_runtime_shaper   Ú	known_vi_)r   r   Úgraph_inputr   r   r   Úget_dimensions   s   

z!FusionGemmFastGelu.get_dimensionsÚnodeÚinput_name_to_nodesÚoutput_name_to_nodec                 C   sÒ  d}t |jƒdkrd}| j |dgdg¡}|du rdS |d }d}d}d}	t|jƒD ]\}
}| j |¡}|du rA|  |j|
 ¡}q*|
}t |¡}	q*|	du rOdS t |	j	ƒdkrXdS |t |	j	ƒk radS d}|r’d}t|jƒD ]\}
}| j |¡}|du r{ql|
}t |¡} |du r‰dS t |j	ƒdkr’dS ||g}| j 
||jd g||¡s¥dS | j |¡ |r½|jd|  |j| |j| gn|jd|  |j| g}tjd	||j| j d	¡d
�}d|_| j |¡ | j| j|j< dS )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_namer/   )r   r&   r'   r(   Úhas_biasÚmatch_nodesÚmatmulÚweight_indexÚx_dimsÚweightÚir0   ÚinitializerÚ
bias_indexÚbias_weightÚsubgraph_nodesr-   Ú
fused_noder   r   r   Úfuse*   sn   
ÿ"ÿý
üzFusionGemmFastGelu.fuse)Ú__name__Ú
__module__Ú__qualname__r   r   r   Úintr   Ústrr%   r   ÚdictÚlistrL   Ú__classcell__r   r   r   r   r	      s    þý
ür	   N)Úloggingr   Úfusion_baser   Úfusion_utilsr   Úonnxr   r   r   Ú
onnx_modelr   rM   Úloggerr	   r   r   r   r   Ú<module>   s   