o
    i                     @   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def fddZdd Z  ZS )FusionBiasGelumodelc                    s.   |rt  |ddd d S t  |dd d S )NFastGeluzadd biasBiasGeluGelu)super__init__)selfr   Zis_fastgelu	__class__ b/home/kim/smarthome/.venv/lib/python3.10/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 u r&d S |\}}d }	d}
t|jD ]\}}| j|}|d u rBq3|}
t|}	 |	d u rPd S t|	j	dkrYd S ||g}| j
||jd g||sld 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_)ZinputsZ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_typenodesaddmatmulZbias_weightZ
bias_indexir   ZinitializerZsubgraph_nodesZ
fused_noder   r   r   fuse   sH   
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   