o
    3Ih                     @   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__ l/home/air/sanwanet/gpt-api/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}|rd}t|jD ]\}
}| j|}|du r{ql|
}t|} |du rdS t |j	dkrdS ||g}| j
||jd g||sd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   