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)FusionUtils)helper)	OnnxModelc                       s4   e Zd Zdef fddZdedefddZ  ZS )FusionQOrderedGelumodelc                    s   t  |dddg d S )NQOrderedGeluZGeluZFastGelu)super__init__)selfr   	__class__ g/home/kim/smarthome/.venv/lib/python3.10/site-packages/onnxruntime/transformers/fusion_qordered_gelu.pyr      s   zFusionQOrderedGelu.__init__input_name_to_nodesoutput_name_to_nodec                 C   s  | j ||}t|dkr|d jdks*t|dkr(|d jdkr(|d jdks*dS |d }d}t|dkr:|d }t|| j sCdS | j |dgdgfg|\}}}	|dk rYdS |d }
t|
| j sfdS |g}|||
g | j ||dur|j	d |j	d gn|j	||st
d dS | j| tjd	|
jd |
jd |jd g|j	d g| j jd	d	d
d}|dur| j ||jd |j	d  |jtddg |jtddg d|_| j| | j| j|j< dS )a  
        INPUT PATTERN
        Fuse (quantized) Gelu subgraph into one node QOrderedGelu:
            -> quantized input  -> DQ -> Gelu -> Q ->

        (or)

            -> quantized input  -> DQ -> FastGelu -> Q ->

        OUTPUT PATTERN
            -> QOrderedGelu ->
           r   ZQuantizeLinear   ZShapeNZDequantizeLinearz.It is not safe to fuse QOrderedGelu node. Skipr	   )Zname_prefix)ZinputsZoutputsnameZorder_XZorder_Yzcom.microsoft)r   Zget_childrenlenZop_typer   Zcheck_qdq_node_for_fusionZmatch_parent_pathsextendZis_safe_to_fuse_nodesoutputloggerdebugZnodes_to_remover   Z	make_nodeinputZcreate_node_nameZreplace_node_input	attributeZmake_attributedomainZnodes_to_addappendZthis_graph_nameZnode_name_to_graph_namer   )r   noder   r   Zgelu_childrenZdownstream_quantize_nodeZdownstream_shape_nodeZfirst_path_idZfirst_input_parent_nodes_Zupstream_dequantize_nodeZsubgraph_nodesZordered_gelu_noder   r   r   fuse   sj   



zFusionQOrderedGelu.fuse)__name__
__module____qualname__r   r   dictr!   __classcell__r   r   r   r   r      s    r   N)loggingr   Zfusion_baser   Zfusion_utilsr   Zonnxr   Z
onnx_modelr   r"   r   r   r   r   r   r   <module>   s   