o
    °ƒðj  ã                   @   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ÚBiasSplitGeluÚGelu)ÚsuperÚ__init__)Ú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_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rŠdS | j |dd|¡}
|
du r™dS |
jd |jd kr¥dS g |¢|	d ‘|‘|‘|
‘|‘}|j d }| j ||g||¡sËt	 
d¡ dS |d }| j |¡\}}t|tƒsÞd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ÚSliceéÿÿÿÿgü©ñÒMbP?)Ú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   )Úname_prefix)ÚinputsÚoutputsÚnamezcom.microsoft)ÚoutputÚlenÚop_typer   Úmatch_parentÚfind_constant_inputÚinputÚmatch_parent_pathÚis_safe_to_fuse_nodesÚloggerÚinfoÚget_constant_inputÚ
isinstanceÚintÚnodes_to_removeÚextendÚcreate_node_namer   Ú	make_nodeÚdomainÚnodes_to_addÚappendÚthis_graph_nameÚnode_name_to_graph_name)r   Ú	gelu_noder   r   ÚchildrenÚmul_after_geluÚslice_before_geluÚ
add_outputÚstart_index_nodesÚend_index_nodesÚslice_before_mulÚsubgraph_nodesÚsubgraph_outputÚadd_nodeÚ
bias_indexÚ_valueÚ	node_nameÚ
fused_noder   r   r   Úfuse   sŠ   
üüÿþýüûú

ÿ

üzFusionBiasSplitGelu.fuse)Ú__name__Ú
__module__Ú__qualname__r   r   ÚdictrH   Ú__classcell__r   r   r   r   r      s    r   N)Úloggingr   Úfusion_baser   Úonnxr   Ú
onnx_modelr   rI   r+   r   r   r   r   r   Ú<module>   s   