
    g"                     |    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e      Z G d d	e      Zy
)    )	getLogger)Fusion)NumpyHelper)helper)	OnnxModelc            	       B     e Zd ZdZ	 	 	 ddedededef fdZd Z xZ	S )	FusionSkipLayerNormalizationz
    Fuse Add + LayerNormalization into one node: SkipLayerNormalization
    Note: This fusion does not check the input shape of Add and LayerNormalization.
    modelfused_op_typesearch_op_typesshape_inferc                     t         |   |||       |rH| j                  j                  dddd      | _        | j                  t
        j                  d       y y y )N      )
batch_sizeseq_lenT)updatez,symbolic shape inference disabled or failed.)super__init__r
   infer_runtime_shapeshape_infer_helperloggerwarning)selfr
   r   r   r   	__class__s        b/var/www/openai/venv/lib/python3.12/site-packages/onnxruntime/transformers/fusion_skiplayernorm.pyr   z%FusionSkipLayerNormalization.__init__   s`     	?&*jj&D&DTUbcEdmq&D&rD#&&.MN /     c           	      	   | j                   j                  |d|      }||j                  dk7  ry t        |j                        dk7  ry |j                  D ]  }| j                   j                  |       y  || j                  v ry |j                  dk(  }t        | d      r
| j                  | j                  j                  |j                  d         rXt        | j                  j                  |j                  d               dk7  r$t        j                  d|j                  d          y | j                  j                  |j                  d   |j                  d         sHt        j                  d	|j                  d   |j                  d          y t        j                  d
       y | j                   j                  |dgd g      }|N| j                   j                  |d   j                  d         #| j                   j                  |d   dgdg      y | j                   j                  |j                   d         d u}|xs( t        | j                   j#                  ||            dkD  }	|j                   }
|	r|
j%                  |j                   d   g       |j                   d   g}|	r!|j%                  dd|j                   d   g       | j                   j'                  ||g|
||      r| j                  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|_        |j2                  D ].  }|j4                  dk(  s|j2                  j%                  |g       0 t        |j2                        dk(  r0|j2                  j%                  t)        j6                  dd      g       | j8                  j;                  |       | j<                  | j>                  |j4                  <   y y )Nr   Add   SimplifiedLayerNormalizationr      zDskip SkipLayerNormalization fusion since shape of input %s is not 3D   zNskip SkipLayerNormalization fusion since shape of inputs (%s, %s) are not samezHskip SkipLayerNormalization fusion since symbolic shape inference failedGatherConstantOfShape SkipLayerNorm)name_prefixinputsoutputsnamecom.microsoftepsilon-q=) r
   
get_parentop_typeleninputget_initializernodes_to_removehasattrr   get_edge_shaper   debugcompare_shapematch_parent_pathfind_graph_inputfind_graph_outputoutput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add	add_input
simplifiedgather_pathadd_has_graph_output#residual_add_has_multiple_consumersoutputs_to_keepr+   r*   normalize_nodeatts                  r   fusez!FusionSkipLayerNormalization.fuse%   s   jj##D!-@A ;#++. syy>QIzz)))4@ #
 $&&& \\%CC
4-.&&2++::399Q<HD33BB399Q<PQUVVLL!gilirirstiuv ..<<SYYq\399UV<XLLh		!		!
 ghjj223
TFK"tzz'B'B;q>CWCWXYCZ'['czz++KN=N<ORSQTU]  $zz;;CJJqMJRVV ^C

(?(?EX(Y$Z]^$^ 	, ++.""CJJqM?3;;q>" /NNBCJJqM23::++S$KJ]_rs  ''d4 " 1syy|TZZ]DJJqMJiilCIIaL$**Q-@ 
 $--""ZZ001C1CQ`0a	N %4N! ~~88y(",,33SE: &
 >++,1((//1F1FyRY1Z0[\$$^4@D@T@TD(()<)<=5 tr   )SkipLayerNormalizationLayerNormalizationT)
__name__
__module____qualname____doc__r   strboolr   rV   __classcell__r   s   @r   r	   r	      sJ     63 OO O 	O
 O_Ur   r	   c                   *     e Zd Zdef fdZd Z xZS ) FusionBiasSkipLayerNormalizationr
   c                 *    t         |   |ddd       y )NrW   zadd bias)r   r   )r   r
   r   s     r   r   z)FusionBiasSkipLayerNormalization.__init__   s     8:RT^_r   c           	         t        |j                        dk7  ry g }| j                  j                  |ddgd d g||      }||\  }}n/g }| j                  j                  |g dg d||      }||\  }}}ny t        |      dk(  st        |      dk(  sJ |d   }	|	dk\  ry |j                  |d	      }
|j                  d	|d	   z
     }|j                  d	|	z
     }| j                  j	                  |      }|y t        j                  |      }|t        j                  d
       y t        |j                        d	k7  rt        j                  d       y ||g}| j                  j                  ||j                  ||      st        j                  d       y | j                  j                  |       |
||j                  d   |j                  d   |g}t        j                  d||j                  | j                  j!                  dd            }d|_        |j$                  D ].  }|j&                  dk(  s|j$                  j                  |g       0 t        |j$                        dk(  r0|j$                  j                  t        j(                  dd      g       | j*                  j-                  |       | j.                  | j0                  |j&                  <   y )Nr   r   MatMul)r   Castre   )NNNr    r"   r   r#   zBias weight not foundzBias weight is not 1DzASkip fusing SkipLayerNormalization with Bias since it is not saferW   SkipLayerNorm_AddBias_r)   r-   r.   r/   )r2   r3   r
   r:   r4   r   to_arrayr   r8   shaper@   r=   r5   r?   r   rA   rB   rC   rD   r,   rE   rF   rG   rH   rI   )r   rJ   rK   rL   return_indicenodesrM   _matmul_castadd_input_index	sln_input
bias_input
skip_inputinitializerbias_weightsubgraph_nodesr*   new_noderU   s                      r   rV   z%FusionBiasSkipLayerNormalization.fuse   s   tzz?a

,,TE83DtTlTgivw"NS' MJJ00/1CEXZgE  (-%eW=!Q&#m*<*AAA'*aIImA./	YYq=#334
ZZO 34
 jj00<!**;7LL01{  !Q&LL01zz//M`buvLL\]##N3JJqMJJqM
 ##$KK,,-EG_`	
 * >>Cxx9$""))3%0 "
 x!!"a'%%v'<'<Y'P&QR  *6:6J6J$$X]]3r   )rY   rZ   r[   r   r   rV   r_   r`   s   @r   rb   rb      s    `i `FKr   rb   N)loggingr   fusion_baser   fusion_utilsr   onnxr   
onnx_modelr   rY   r   r	   rb    r   r   <module>r|      sA      $   	8	tU6 tUnJKv JKr   