U
    T?h                     @   sX   d dl mZ d dlmZ d dlmZ d dlmZ d dlm	Z	 ee
ZG dd deZdS )	    )	getLogger)Fusion)NumpyHelper)helper)	OnnxModelc                       s*   e Zd Zed fddZdd Z  ZS )FusionBiasGelu)modelc                    s,   |rt  |ddd nt  |dd d S )NFastGeluzadd biasBiasGeluGelu)super__init__)selfr   Zis_fastgelu	__class__ Z/var/www/html/venv/lib/python3.8/site-packages/onnxruntime/transformers/fusion_biasgelu.pyr      s    zFusionBiasGelu.__init__c              	   C   s>  |j }|dkrdnd}t|jdkr(d S | j|ddgdd g}|d krLd S |\}}d }	d}
t|jD ]0\}}| j|}|d krqf|}
t|}	 qqf|	d krd S t|	j	dkrd S ||g}| j
||jd g||sd S | j| tj||jd |j|
 g|j| j||d	 d
}d|_| j| | j| j|j< d S )Nr   r
   r	      AddZMatMulr   Z	_AddBias_)inputsoutputsnamezcom.microsoft)Zop_typeleninputr   Zmatch_parent_path	enumerateZget_initializerr   Zto_arrayshapeZis_safe_to_fuse_nodesoutputZnodes_to_removeextendr   Z	make_nodeZcreate_node_namedomainZnodes_to_addappendZthis_graph_nameZnode_name_to_graph_namer   )r   nodeZinput_name_to_nodesZoutput_name_to_nodeZgelu_op_typeZfuse_op_typeZnodesaddmatmulZbias_weightZ
bias_indexir   ZinitializerZsubgraph_nodesZ
fused_noder   r   r   fuse   sN    
 
  zFusionBiasGelu.fuse)__name__
__module____qualname__r   r   r%   __classcell__r   r   r   r   r      s   r   N)loggingr   Zfusion_baser   Zfusion_utilsr   Zonnxr   Z
onnx_modelr   r&   loggerr   r   r   r   r   <module>   s   