o
    i                     @   sL   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)helper)	OnnxModelc                       s4   e Zd Zdef fddZdedefddZ  ZS )FusionBiasSplitGelumodelc                    s   t  |dd d S )NBiasSplitGeluZGelu)super__init__)selfr   	__class__ g/home/kim/smarthome/.venv/lib/python3.10/site-packages/onnxruntime/transformers/fusion_biassplitgelu.pyr
      s   zFusionBiasSplitGelu.__init__input_name_to_nodesoutput_name_to_nodec                 C   s"  |j d |vr	dS ||j d  }t|dks|d jdkrdS |d }| j|dd|}|du r2dS | jj|dddd	kr?dS |jd }| j|g d
g d|}|du rb| j|g dg d|}|du so|d jd |krqdS | j|ddgddg|}	|	du s|	d |vrdS | j|dd|}
|
du rdS |
jd |jd krdS g ||	d |||
|}|j d }| j||g||st	
d dS |d }| j|\}}t|tsdS | j| | jjddd}tjd|jd|  |j| g|g|d}d|_| j| | j| j|< dS )a  
        [root] --->Add -------------------->  Slice ---------------> Mul -->
                   |                            ^                    ^
                   |                            |                    |
                   +----------------------------+---Slice --> Gelu---+
                   |                            |     ^
                   |                            |-----|
                   |                            |     |
                   |                           Mul   Mul
                   |                            ^     ^
                   v                            |     |
                  Shape ---> Gather --> Add --> Div --+
        r   N   MulSlicegMbP?)delta   )DivAddGatherShaper   )r   r   r   r   r   )r   r   r   r   r   r   )r   r   r   r   r   r   r      zBSkip fuse BiasSplitGelu since it is not safe to fuse the subgraph.r   )Zname_prefix)ZinputsZoutputsnamezcom.microsoft)outputlenZop_typer   Zmatch_parentZfind_constant_inputinputZmatch_parent_pathZis_safe_to_fuse_nodesloggerinfoZget_constant_input
isinstanceintZnodes_to_removeextendZcreate_node_namer   Z	make_nodedomainZnodes_to_addappendZthis_graph_nameZnode_name_to_graph_name)r   Z	gelu_noder   r   childrenZmul_after_geluZslice_before_geluZ
add_outputZstart_index_nodesZend_index_nodesZslice_before_mulZsubgraph_nodesZsubgraph_outputadd_nodeZ
bias_index_valueZ	node_nameZ
fused_noder   r   r   fuse   s   




zFusionBiasSplitGelu.fuse)__name__
__module____qualname__r   r
   dictr,   __classcell__r   r   r   r   r      s    r   N)loggingr   Zfusion_baser   Zonnxr   Z
onnx_modelr   r-   r"   r   r   r   r   r   <module>   s   