
    h)                     f    S SK Jr  S SKrS SKJr  S SKJrJr  S SK	J
r
  \" \5      r " S S\5      rg)    )	getLoggerN)Fusion)TensorProtohelper)	OnnxModelc                   F   ^  \ rS rSrSS\4U 4S jjjrS\S\4S jrSrU =r	$ )	FusionGroupNorm   modelc                 4   > [         TU ]  USS5        X l        g )N	GroupNormAdd)super__init__channels_last)selfr   r   	__class__s      d/var/www/fran/franai/venv/lib/python3.13/site-packages/onnxruntime/transformers/fusion_group_norm.pyr   FusionGroupNorm.__init__   s    U3*    input_name_to_nodesoutput_name_to_nodec           
      6   U R                   R                  U/ SQ/ SQU5      nUc  gUu  pVpxUR                  S   n	U R                   R                  US/S/U5      n
U
c  gU
S   R                  S   U	:w  a  gU
S   nU R                   R                  US5      nSnUb(  U R                   R                  US/S/U5      nUb  US   nUR                  SU R                   R	                  UR
                  S   U5      -
     nU R                   R                  US	S
5      (       d  gUR                  SU R                   R	                  UR
                  S   U5      -
     nU R                   R                  US	S5      (       d  gU R                   R                  U5      nUc  g[        UR                  5      S	:X  a&  UR                  S   S:X  a  UR                  S   S:X  d  gU R                   R                  U5      nUc  g[        UR                  5      S	:X  a&  UR                  S   S:X  a  UR                  S   S:X  d  g[        [        R                  " UR                  5      5      n[        [        R                  " UR                  5      5      nUU:w  a  gU R                   R                  UR                  S   5      nUb  [        UR                  5      S:w  a  g[        UR                  S   5      nU R                   R                  UR                  S   5      nUb  UR                  UR                  :w  a  g[        R                  " [        R                  " U5      U5      (       d  g[        R                  " [        R                  " U5      U5      (       d  gU R                   R!                  SSS9nU R#                  US-   [$        R&                  U/US9  U R#                  US-   [$        R&                  U/US9  UnXXgX/nU=(       a    UnU(       a  U(       a  UR)                  X/5        UnU R                   R+                  UUR
                  UU5      (       d  U R,                  R)                  U/5        OU R,                  R)                  U5        SU l        U	nUR
                  S   nU R0                  (       a  US-   OUnU R0                  (       a  US-   OUnU R0                  (       ar  [2        R4                  " SU/U/U R                   R!                  SSS9/ SQS9n U R6                  R9                  U 5        U R:                  U R<                  U R>                  '   [2        R4                  " SUUS-   US-   /U/US9n!U!R@                  R)                  UR@                  5        U!R@                  R)                  [2        RB                  " SU5      /5        U!R@                  R)                  [2        RB                  " SU(       a  SOS5      /5        U R0                  (       d1  U!R@                  R)                  [2        RB                  " SS5      /5        SU!l"        U R6                  R9                  U!5        U R:                  U R<                  U!R>                  '   U R0                  (       as  [2        R4                  " SU/U/U R                   R!                  SSS9/ SQS9n"U R6                  R9                  U"5        U R:                  U R<                  U"R>                  '   gg)a*  
 Fuse Group Normalization subgraph into one node GroupNorm.
 The following is the pattern with swish activation:
       +----------------Shape-------------------------------+
       |                                                    |
       |    (0, 32, -1)                                     v     (512x1x1) (512x1x1) (optional)
   [Root] --> Reshape -------> InstanceNormalization --> Reshape ---> Mul --> Add --> Mul--> [output]
Bx512xHxW                 (scale=ones(32), B=zeros(32))                        |       ^     Bx512xHxW
                                                                               |       |
                                                                               +--->Sigmoid (optional)
The Mul and Sigmoid before output is for Swish activation. They are optional.
)MulReshapeInstanceNormalizationr   )r   r   r   r   Nr   Shape   r   Sigmoid   zgroup norm weightzlayernorm bias   r   )name_prefix_gamma)name	data_typedimsvals_betaT_NHWC	TransposeTranspose_NCHW_to_NHWC)r   r!   r    r   )r$   perm)inputsoutputsr$   groups
activationr   zcom.microsoftTranspose_NHWC_to_NCHW)r   r    r   r!   )#r   match_parent_pathinputfind_first_child_by_typeinput_indexoutput$is_constant_with_specified_dimensionget_constant_valuelenshapeintnpprodallclose	ones_like
zeros_likecreate_node_nameadd_initializerr   FLOATextendis_safe_to_fuse_nodesnodes_to_removeprune_graphr   r   	make_nodenodes_to_addappendthis_graph_namenode_name_to_graph_namer$   	attributemake_attributedomain)#r   add_noder   r   nodes
weight_mul
reshape_4dinstance_norm
reshape_3drootparents
shape_node	swish_mulswish_sigmoidsigmoid_pathweight_input
bias_inputweightbiasweight_elementsbias_elementsinstance_norm_scale
num_groupsinstance_norm_biasgroup_norm_name	last_nodesubgraph_nodeshas_swish_activation
input_nameoutput_namegroup_norm_input_namegroup_norm_output_nametranspose_inputnew_nodetranspose_outputs#                                      r   fuseFusionGroupNorm.fuse   sf    

,,Ll\o
 =<A9
"**..zG9qcK^_?1:A$&QZ
 JJ77%H	 ::77	I;QUPVXklL' ,Q!''DJJ,B,B:CTCTUVCWYc,d(dezz>>|QPcdd^^A

(>(>z?P?PQR?SU](^$^_
zz>>z1N^__..|<>FLL!Q&6<<?a+?FLLQROWXDXzz,,Z8<DJJ1$A!);

1QR@Rbggfll34BGGDJJ/0m+"jj;;M<O<OPQ<RS&#.A.G.G*HA*M,22156
!ZZ::=;N;Nq;QR%)<)B)BFYF_F_)_{{2<<(;<>QRR{{2==);<>PQQ**55k{5[ 8+!''!"	 	 	
 	 7*!''	 	 	
 	"
:b(:]!!9"<=!Izz//	
 
   ''4  ''7  
&&q)8<8J8J
W 4PZ:>:L:Lw!6R] $..&'ZZ00Jb0c!O $$_5AEAUAUD(()=)=>##)?X+EY`G`a+, 	
 	!!-"9"9:!!6#8#8:#N"OP!!6#8#8L`qfg#h"ij!!%%v'<'<_a'P&QR)  *6:6J6J$$X]]3 %//'(ZZ00Jb0c!  $$%56BFBVBVD(()9)>)>? r   )r   rG   )T)
__name__
__module____qualname____firstlineno__r   r   dictrp   __static_attributes____classcell__)r   s   @r   r	   r	      s4    +i + +`W$ `WT `W `Wr   r	   )loggingr   numpyr<   fusion_baser   onnxr   r   
onnx_modelr   rr   loggerr	    r   r   <module>r      s0   
    $  	8	eWf eWr   