U
    T?h                      @   sh   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G dd	 d	eZd
S )    )	getLogger)Fusion)NumpyHelper)helper)	OnnxModelc                       s4   e Zd ZdZd	eeed fddZdd Z  ZS )
FusionSkipLayerNormalizationz
    Fuse Add + LayerNormalization into one node: SkipLayerNormalization
    Note: This fusion does not check the input shape of Add and LayerNormalization.
    SkipLayerNormalizationLayerNormalization)modelfused_op_typesearch_op_typesc                    s@   t  ||| | jjddddd| _| jd kr<td d S )N      )Z
batch_sizeZseq_lenT)updatez,symbolic shape inference disabled or failed.)super__init__r
   Zinfer_runtime_shapeshape_infer_helperloggerwarning)selfr
   r   r   	__class__ _/var/www/html/venv/lib/python3.8/site-packages/onnxruntime/transformers/fusion_skiplayernorm.pyr      s    
z%FusionSkipLayerNormalization.__init__c                 C   s  | j |d|}|d ks"|jdkr&d S t|jdkr8d S |jD ]}| j |d k	r> d S q>|| jkrhd S |jdk}| jd k	r| j|jd |jd st	
d|jd |jd  d S nt	
d d S | j |dgd g}|d k	r | j |d jd d kr | j |d d	gdgd kr d S | j |jd d k	}|pPt| j ||dk}	|j}
|	rp|
|jd g |jd g}|	r|d
d
|jd g | j ||g|
||r| j||g |s|jd |jd |jd |jd gn|jd |jd |jd g}tj| j||| j j| jddd}d|_|jD ] }|jdkr4|j|g q4t|jdkr||jtddg | j| | j| j|j< d S )Nr   Add   ZSimplifiedLayerNormalization   zNskip SkipLayerNormalization fusion since shape of inputs (%s, %s) are not samezHskip SkipLayerNormalization fusion since symbolic shape inference failedZGatherZConstantOfShape ZSkipLayerNorm)Zname_prefixinputsoutputsnamecom.microsoftepsilon-q=)r
   Z
get_parentZop_typeleninputget_initializernodes_to_remover   Zcompare_shaper   debugmatch_parent_pathZfind_graph_inputZfind_graph_outputoutputZget_childrenextendis_safe_to_fuse_nodesr   	make_noder   create_node_namedomain	attributer!   make_attributenodes_to_addappendthis_graph_namenode_name_to_graph_name)r   nodeinput_name_to_nodesoutput_name_to_nodeaddZ	add_inputZ
simplifiedZgather_pathZadd_has_graph_outputZ#residual_add_has_multiple_consumersZoutputs_to_keepr    r   Znormalize_nodeattr   r   r   fuse$   sl    




&(
z!FusionSkipLayerNormalization.fuse)r   r	   )	__name__
__module____qualname____doc__r   strr   r<   __classcell__r   r   r   r   r      s     r   c                       s*   e Zd Zed fddZdd Z  ZS ) FusionBiasSkipLayerNormalization)r
   c                    s   t  |ddd d S )Nr   zadd bias)r   r   )r   r
   r   r   r   r      s    z)FusionBiasSkipLayerNormalization.__init__c                 C   s  t |jdkrd S g }| j|ddgd d g||}|d k	rD|\}}n<g }| j|dddgd d d g||}|d k	r||\}}}nd S t |dkst |dkst|d }	|	dkrd S |j|d  }
|jd|d   }|jd|	  }| j|}|d krd S t|}|d krt	d	 d S t |j
dkr6t	d
 d S ||g}| j||j||sbt	d d S | j| |
||jd |jd |g}tjd||j| jddd}d|_|jD ] }|jdkr|j|g qt |jdkr|jtddg | j| | j| j|j< d S )Nr   r   ZMatMulZCastr      r   r   zBias weight not foundzBias weight is not 1DzASkip fusing SkipLayerNormalization with Bias since it is not safer   ZSkipLayerNorm_AddBias_r   r"   r#   r$   )r%   r&   r
   r*   AssertionErrorr'   r   Zto_arrayr   r)   shaper-   r+   r(   r,   r   r.   r/   r0   r1   r!   r2   r3   r4   r5   r6   )r   r7   r8   r9   Zreturn_indiceZnodesr:   Z_matmulZ_castZadd_input_indexZ	sln_inputZ
bias_inputZ
skip_inputZinitializerZbias_weightZsubgraph_nodesr   new_noder;   r   r   r   r<      sv    
    





z%FusionBiasSkipLayerNormalization.fuse)r=   r>   r?   r   r   r<   rB   r   r   r   r   rC   ~   s   rC   N)loggingr   Zfusion_baser   Zfusion_utilsr   Zonnxr   Z
onnx_modelr   r=   r   r   rC   r   r   r   r   <module>   s   n