
    zIg                      |    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                   <     e Zd ZdZ	 	 d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_typesc                     t         |   |||       | j                  j                  dddd      | _        | j                  t
        j                  d       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   	__class__s       j/var/www/html/answerous/venv/lib/python3.12/site-packages/onnxruntime/transformers/fusion_skiplayernorm.pyr   z%FusionSkipLayerNormalization.__init__   sX     	?"&**"@"@PQ^_A`im"@"n""*NNIJ +    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(  }| j                  h| 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|_        |j.                  D ].  }|j0                  dk(  s|j.                  j!                  |g       0 t        |j.                        dk(  r0|j.                  j!                  t%        j2                  dd      g       | j4                  j7                  |       | j8                  | j:                  |j0                  <   y y )Nr   Add   SimplifiedLayerNormalization   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_remover   compare_shaper   debug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
"".**881syyQR|TdIIaLIIaL
 LLcd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LayerNormalization)	__name__
__module____qualname____doc__r   strr   rR   __classcell__r   s   @r   r	   r	      s<     63	KK K 	KW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 )NrS   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   Castr`   )NNNr      r   r!   zBias weight not foundzBias weight is not 1DzASkip fusing SkipLayerNormalization with Bias since it is not saferS   SkipLayerNorm_AddBias_r'   r+   r,   r-   )r0   r1   r
   r6   r2   r   to_arrayr   r5   shaper<   r9   r3   r;   r   r=   r>   r?   r@   r*   rA   rB   rC   rD   rE   )r   rF   rG   rH   return_indicenodesrI   _matmul_castadd_input_index	sln_input
bias_input
skip_inputinitializerbias_weightsubgraph_nodesr(   new_noderQ   s                      r   rR   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_`	
 * >> 	1Cxx9$""))3%0	1
 x!!"a'%%v'<'<Y'P&QR  *6:6J6J$$X]]3r   )rU   rV   rW   r   r   rR   rZ   r[   s   @r   r]   r]   ~   s    `i `FKr   r]   N)loggingr   fusion_baser   fusion_utilsr   onnxr   
onnx_modelr   rU   r   r	   r]    r   r   <module>rx      sA      $   	8	kU6 kU\JKv JKr   