o
    °ƒðj‡(  ã                   @  s@   d dl mZ d dlZddlmZ ddlmZ G dd„ deƒZdS )	é    )ÚannotationsNé   )Ú	ONNXModelé   )ÚFusionc                      sF   e Zd Zd‡ fdd„Zddd„Zddd„Zddd„Zddd„Z‡  ZS )Ú
FusionGeluÚmodelr   c                   s   t ƒ  |dd¡ d S )NÚGeluÚErf)Ú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/quantization/fusions/fusion_gelu.pyr      s   zFusionGelu.__init__Úerf_nodeúonnx.NodeProtoÚinput_name_to_nodesúdict[str, list[onnx.NodeProto]]Úoutput_name_to_nodeúdict[str, onnx.NodeProto]c                 C  s@   |   |||¡s|  |||¡s|  |||¡r| j dd¡ dS dS )z
        Interface function that tries to fuse a node sequence containing an Erf node into a single
        Gelu node.
        úcom.microsoftr   N)Úfuse_1Úfuse_2Úfuse_3r   Úset_opset_import)r   r   r   r   r   r   r   Úfuse   s   ÿþýûzFusionGelu.fuseÚreturnÚboolc                 C  sú  |j d |vr	dS ||j d  }t|ƒdks|d jdkrdS |d }|  |d¡s+dS |j d |vr4dS ||j d  }t|ƒdksH|d jdkrJdS |d }|  |dd|¡}|du r\dS | j|dd	d
�dkrhdS |jd }|jd |j d krydnd}	||j|	 krª||j d  }t|ƒdks–|d jdkr˜dS |d }
|  |
d¡s¤dS |
j d }n"|  |d|	|¡}
|
du r¸dS |  |
d¡sÀdS ||
jvrÇdS |j d }|||||
g}|  ||g||¡sÞdS | j 	|¡ t
jjd|  ¡ |g|gd�}d|_| j |¡ dS )ay  
        This pattern is from PyTorch model
        Fuse Gelu with Erf into one node:
        Pattern 1:
                       +-------Mul(0.5)---------------------+
                       |                                    |
                       |                                    v
                    [root] --> Div -----> Erf  --> Add --> Mul -->
                              (B=1.4142...)       (1)

        Pattern 2:
                       +------------------------------------+
                       |                                    |
                       |                                    v
                    [root] --> Div -----> Erf  --> Add --> Mul -->Mul -->
                              (B=1.4142...)       (1)            (0.5)

        Note that constant input for Add and Mul could be first or second input: like either A=0.5 or B=0.5 is fine.
        r   Fr   ÚAddÚMulÚDivNçà-� ö?çü©ñÒMbP?©Údeltaç      à?r	   ©ÚnameÚinputsÚoutputsr   T©ÚoutputÚlenÚop_typeÚhas_constant_inputÚmatch_parentÚfind_constant_inputÚinputÚis_safe_to_fuse_nodesÚnodes_to_removeÚextendÚonnxÚhelperÚ	make_nodeÚcreate_unique_node_nameÚdomainÚnodes_to_addÚappend)r   r   r   r   ÚchildrenÚadd_after_erfÚmul_after_erfÚdivÚsubgraph_inputÚanotherÚmul_halfÚsubgraph_outputÚsubgraph_nodesÚ
fused_noder   r   r   r   #   s^   


ÿzFusionGelu.fuse_1c                 C  sæ  |j d |vr	dS ||j d  }t|ƒdks|d jdkrdS |d }|  |d¡s+dS |j d |vr4dS ||j d  }t|ƒdksH|d jdkrJdS |d }|  |d¡sVdS |j d |vr_dS ||j d  }t|ƒdkss|d jdkrudS |d }|  |dd|¡}|du r‡dS d}	| j|d	d
d�dkr©|  |dd|¡}	|	du r¡dS |  |	d¡s©dS |jd }
|
|jvrµdS |||||g}|	rÃ| |	¡ |  ||j d g||¡sÑdS | j	 
|¡ tjjd|  ¡ |
g|j d gd�}d|_| j |¡ dS )a&  
        This pattern is from Keras model
        Fuse Gelu with Erf into one node:
                       +------------------------------------------+
                       |                                          |
                       |                                          v
                    [root] --> Div -----> Erf  --> Add --> Mul -->Mul
                              (B=1.4142...)       (A=1)   (A=0.5)

        Note that constant input for Add and Mul could be first or second input: like either A=0.5 or B=0.5 is fine.
        r   Fr   r    r!   r'   r"   Nr#   r$   r%   ÚSqrtg       @r	   r(   r   T)r-   r.   r/   r0   r1   r2   r3   r=   r4   r5   r6   r7   r8   r9   r:   r;   r<   )r   r   r   r   r>   r?   r@   ÚmulrA   Ú	sqrt_noderB   rF   rG   r   r   r   r   z   s^   


ÿzFusionGelu.fuse_2c                 C  sÊ  |j d |vr	dS ||j d  }t|ƒdks|d jdkrdS |d }|  |d¡s+dS |j d |vr4dS ||j d  }t|ƒdksH|d jdkrJdS |d }|  |d¡sVdS |  |dd|¡}|du rddS | j|dd	d
�}|dk rrdS d| }	|j|	 }
|j d |vr„dS ||j d  }t|ƒdks˜|d jdkršdS |d }|jd |
ks®|jd |
ks®dS |||||g}|  ||j d g||¡sÃdS | j 	|¡ t
jjd|  ¡ |
g|j d gd�}d|_| j |¡ dS )a?  
        This pattern is from TensorFlow model
        Fuse Gelu with Erf into one node:
                       +----------------------------------------------+
                       |                                              |
                       |                                              v
                    [root] --> Mul -----> Erf    -->   Add --> Mul -->Mul
                               (A=0.7071067690849304)  (B=1)  (B=0.5)

        Note that constant input for Add and Mul could be first or second input: like either A=0.5 or B=0.5 is fine.
        r   Fr   r    r!   r'   Ng   `ž æ?r$   r%   r	   r(   r   Tr,   )r   r   r   r   r>   r?   rD   Ú	first_mulÚiÚroot_input_indexrB   Úlast_mulrF   rG   r   r   r   r   Æ   s^   

üÿzFusionGelu.fuse_3)r   r   )r   r   r   r   r   r   )r   r   r   r   r   r   r   r   )	Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   Ú__classcell__r   r   r   r   r      s    


WLr   )Ú
__future__r   r7   Ú
onnx_modelr   Úfusionr   r   r   r   r   r   Ú<module>   s
   