
    "
3jA*                         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 r " S S\5      r " S	 S
\5      rg)    )	getLogger)Fusion)NumpyHelper)helper)	OnnxModelc                     [        U 5      S:w  a  g[        U5      S:X  a,  US   S:H  =(       a    US   U S   :H  =(       a    US   U S   :H  $ [        U5      S:X  a  US   U S   :H  =(       a    US   U S   :H  $ g)zCheck if skip_shape can broadcast to input_shape for SkipLayerNormalization.

The kernel supports: input 3D (B,S,H) with skip 3D (1,S,H) or skip 2D (S,H).
   Fr         )len)input_shape
skip_shapes     g/home/wildlama/miniconda3/lib/python3.13/site-packages/onnxruntime/transformers/fusion_skiplayernorm.py_is_broadcast_skipr      s    
 ;1
:!!}!ijm{1~&Ei*UV-[fgh[iJii
:!!}A.R:a=KPQN3RR    c            	       V   ^  \ rS rSrSr   SS\S\S\S\4U 4S jjjrS r	S	 r
S
rU =r$ )FusionSkipLayerNormalization   z
Fuse Add + LayerNormalization into one node: SkipLayerNormalization.
Supports broadcasting of the skip input: (1, sequence_length, hidden_size)
or (sequence_length, hidden_size) will be broadcast to match the input shape.
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        r   r    %FusionSkipLayerNormalization.__init__%   s^     	?&*jj&D&DTUbcEdmq&D&rD#&&.MN / r   c                 &   U R                   R                  UR                  S   5      nU R                   R                  UR                  S   5      nUb  Uc  gX#:X  a  [        U5      S:X  a  S$ S$ [	        X#5      (       a  g[	        X25      (       a  gg)zIdentify which Add input is the skip tensor (the one that may broadcast).

Returns (skip_index, broadcast):
    skip_index: 0 or 1 (which Add input is skip), -1 if incompatible
    broadcast: True if broadcasting is needed
r   r
   )Fr	   )r
   F)r
   T)r   T)r"   get_edge_shapeinputr   r   )r%   addshape_ashape_bs       r   get_skip_index+FusionSkipLayerNormalization.get_skip_index4   s     ))881F))881F?go!$W!2:CC g//g//r   c           
         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SnSn[        U S5      (       ao  U R                  bL  U R                  U5      u  pxUS:  a2  [        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(       a[  [        S5       HL  n
U R                   R                  XJU5      nUc  M$  UR                  S
:X  d  M6  [        R                  SU
5          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        SU-
  nU(       d:  UR                  U   UR                  U   UR                  S   UR                  S   /O+UR                  U   UR                  U   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   Addr   SimplifiedLayerNormalizationr
   Fr"   zUskip SkipLayerNormalization fusion since shapes of inputs (%s, %s) are not compatiblezHskip SkipLayerNormalization fusion since symbolic shape inference failedGatherConstantOfShapez]skip SkipLayerNormalization broadcast fusion since Add input %d comes from Gather (embedding) SkipLayerNorm)name_prefixinputsoutputsnamecom.microsoftepsilon-q=) r   
get_parentop_typer   r+   get_initializernodes_to_removehasattrr"   r/   r#   debugmatch_parent_pathfind_graph_inputrange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_noder,   	add_input
simplified
skip_index
_broadcastgather_pathiparentadd_has_graph_output#residual_add_has_multiple_consumersoutputs_to_keepr;   input_indexr:   normalize_nodeatts                       r   fuse!FusionSkipLayerNormalization.fuseL   s   jj##D!-@A ;#++. syy>QIzz)))4@ #
 &&& \\%CC


4-..&&2)-)<)<S)A&
>LLo		!		!
  " ghjj223
TFK"tzz'B'B;q>CWCWXYCZ'['czz++KN=N<ORSQTU]
 1X..s7JK%&..H*DLLw    $zz;;CJJqMJRVV ^C

(?(?(Y$Z]^$^ 	, ++.""CJJqM?3;;q>" /NNBCJJqM23::++SK_rss  ''4j.K " ;':)>

1tzzZ[}]ii,cii
.CTZZPQ]S 
 $--""ZZ001C1CQ`0a	N %4N! ~~88y(",,33SE: &
 >++,1((//1F1FyRY1Z0[\$$^4@D@T@TD(()<)<=7 tr   )r"   )SkipLayerNormalizationLayerNormalizationT)__name__
__module____qualname____firstlineno____doc__r   strboolr    r/   rg   __static_attributes____classcell__r&   s   @r   r   r      s\     63 OO O 	O
 O O0iU i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 )Nri   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   r2   MatMul)r2   Castr{   )NNNr   r	   r   r
   zBias weight not foundzBias weight is not 1DzASkip fusing SkipLayerNormalization with Bias since it is not saferi   SkipLayerNorm_AddBias_r9   r=   r>   r?   )r   r+   r   rF   rB   r   to_arrayr#   rE   shaperM   rJ   rC   rL   r   rN   rO   rP   rQ   r<   rR   rS   rT   rU   rV   )r%   rW   rX   rY   return_indicenodesr,   _matmul_castadd_input_index	sln_input
bias_input
skip_inputinitializerbias_weightsubgraph_nodesr:   new_noderf   s                      r   rg   %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    )	rk   rl   rm   rn   r   r    rg   rr   rs   rt   s   @r   rv   rv      s    `i `FK FKr   rv   N)loggingr   fusion_baser   fusion_utilsr   onnxr   
onnx_modelr   rk   r#   r   r   rv   r   r   r   <module>r      sF      $   	8	WU6 WUtJKv JKr   