
    h"                     |    S SK Jr  S SKJr  S SKJr  S SKJr  S SKJ	r	  \" \
5      r " S S\5      r " S S	\5      rg
)    )	getLogger)Fusion)NumpyHelper)helper)	OnnxModelc            	       P   ^  \ rS rSrSr   S
S\S\S\S\4U 4S jjjrS r	S	r
U =r$ )FusionSkipLayerNormalization   z
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U ]  XU5        U(       aF  U R                  R                  SSS.SS9U l        U R                  c  [
        R                  S5        g g g )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        g/var/www/fran/franai/venv/lib/python3.13/site-packages/onnxruntime/transformers/fusion_skiplayernorm.pyr   %FusionSkipLayerNormalization.__init__   s^     	?&*jj&D&DTUbcEdmq&D&rD#&&.MN /     c           
      z	   U R                   R                  USU5      nUb  UR                  S:w  a  g [        UR                  5      S:w  a  g UR                   H!  nU R                   R                  U5      c  M!    g    X@R                  ;   a  g UR                  S:H  n[        U S5      (       Ga  U R                  b  U R                  R                  UR                  S   5      (       aY  [        U R                  R                  UR                  S   5      5      S:w  a$  [        R                  SUR                  S   5        g U R                  R                  UR                  S   UR                  S   5      (       d2  [        R                  S	UR                  S   UR                  S   5        g O[        R                  S
5        g U R                   R                  US/S /5      nUbP  U R                   R                  US   R                  S   5      c$  U R                   R                  US   S/S/5      c  g U R                   R                  UR                   S   5      S LnU=(       d'    [        U R                   R#                  XB5      5      S:  n	UR                   n
U	(       a  U
R%                  UR                   S   /5        UR                   S   /nU	(       a!  UR%                  SSUR                   S   /5        U R                   R'                  XA/XU5      (       Ga  U R                  R%                  XA/5        U(       d:  UR                  S   UR                  S   UR                  S   UR                  S   /O+UR                  S   UR                  S   UR                  S   /n[(        R*                  " U R,                  UUU R                   R/                  U R,                  SS9S9nSUl        UR2                   H1  nUR4                  S:X  d  M  UR2                  R%                  U/5        M3     [        UR2                  5      S:X  a1  UR2                  R%                  [(        R6                  " SS5      /5        U R8                  R;                  U5        U R<                  U R>                  UR4                  '   g g )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   fuse!FusionSkipLayerNormalization.fuse%   s   jj##D!-@A ;#++. syy>QIzz)))4@ #
 &&& \\%CC
4-..&&2++::399Q<HHD33BB399Q<PQUVVLL!gilirirstiuv ..<<SYYq\399UV<XXLLh		!		!
  Y ghjj223
TFK"tzz'B'B;q>CWCWXYCZ'['czz++KN=N<ORSQTU]  $zz;;CJJqMJRVV ^C

(?(?(Y$Z]^$^ 	, ++.""CJJqM?3;;q>" /NNBCJJqM23::++SK_rss  ''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   )r   )SkipLayerNormalizationLayerNormalizationT)__name__
__module____qualname____firstlineno____doc__r   strboolr   rX   __static_attributes____classcell__r   s   @r   r	   r	      sW     63 OO O 	O
 O O_U _Ur   r	   c                   6   ^  \ rS rSrS\4U 4S jjrS rSrU =r$ ) FusionBiasSkipLayerNormalization   r   c                 *   > [         TU ]  USSS5        g )NrZ   zadd bias)r   r   )r   r   r   s     r   r   )FusionBiasSkipLayerNormalization.__init__   s     8:RT^_r   c           	         [        UR                  5      S:w  a  g / nU R                  R                  USS/S S /X45      nUb  Uu  pgO./ nU R                  R                  U/ SQ/ SQX45      nUb  Uu  phnOg [        U5      S:X  d  [        U5      S:X  d   eUS   n	U	S:  a  g UR                  US	      n
UR                  S	US	   -
     nUR                  S	U	-
     nU R                  R	                  U5      nUc  g [
        R                  " U5      nUc  [        R                  S
5        g [        UR                  5      S	:w  a  [        R                  S5        g X/nU R                  R                  XR                  X#5      (       d  [        R                  S5        g U R                  R                  U5        U
UUR                  S   UR                  S   U/n[        R                  " SUUR                  U R                  R!                  SS5      S9nSUl        UR$                   H1  nUR&                  S:X  d  M  UR$                  R                  U/5        M3     [        UR$                  5      S:X  a1  UR$                  R                  [        R(                  " SS5      /5        U R*                  R-                  U5        U R.                  U R0                  UR&                  '   g )Nr   r!   MatMul)r!   Castrl   )NNNr"   r$   r   r%   zBias weight not foundzBias weight is not 1DzASkip fusing SkipLayerNormalization with Bias since it is not saferZ   SkipLayerNorm_AddBias_r+   r/   r0   r1   )r4   r5   r   r<   r6   r   to_arrayr   r:   shaperB   r?   r7   rA   r   rC   rD   rE   rF   r.   rG   rH   rI   rJ   rK   )r   rL   rM   rN   return_indicenodesrO   _matmul_castadd_input_index	sln_input
bias_input
skip_inputinitializerbias_weightsubgraph_nodesr,   new_noderW   s                      r   rX   %FusionBiasSkipLayerNormalization.fuse   s   tzz?a

,,TE83DtTlTgw"NS' MJJ00/1CEXE  (-%W=!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`vvLL\]##N3JJqMJJqM
 ##$KK,,-EG_`	
 * >>Cxx9$""))3%0 "
 x!!"a'%%v'<'<Y'P&QR  *6:6J6J$$X]]3r    )	r\   r]   r^   r_   r   r   rX   rc   rd   re   s   @r   rg   rg      s    `i `FK FKr   rg   N)loggingr   fusion_baser   fusion_utilsr   onnxr   
onnx_modelr   r\   r   r	   rg   r~   r   r   <module>r      sA      $   	8	tU6 tUnJKv JKr   