
     Ng3                    N    d dl mZ d dlZddlmZ ddlmZ  G d de          ZdS )	    )annotationsN   )	ONNXModel   )Fusionc                  (     e Zd Zd fdZdd
Z xZS )FusionLayerNormalizationmodelr   c                N    t                                          |dd           d S )NLayerNormalization
ReduceMean)super__init__)selfr
   	__class__s     m/var/www/html/ai-engine/env/lib/python3.11/site-packages/onnxruntime/quantization/fusions/fusion_layernorm.pyr   z!FusionLayerNormalization.__init__   s&     4lCCCCC    reduce_mean_nodeonnx.NodeProtoinput_name_to_nodesdict[str, list[onnx.NodeProto]]output_name_to_nodedict[str, onnx.NodeProto]c                   | j                             ||          }t          |          dk    st          |          dk    rdS |j        d         }|d         j        dk    s|d         j        d         |k    rdS t          |          dk    r*|d         j        dk    s|d         j        d         |k    rdS d}|D ]}|                     |d|d          }| n |dS |                     |g d	g d
fg dg dfg|          \  }}	}
|dk     rdS |	d         }||vrdS |	d         }|                     |          \  }}||dk    s|dk    rdS |	d         }|                     |d          dk    rdS ||j	        d                  d         }|j        dk    rdS ||j	        d                  d         }|j        dk    rdS |g}|
                    |           |
                    |	dd                    |
                    |||g           |                     ||j	        ||          sdS |j        d|                     |j	        d         |          z
           }|                     |d          sdS |j        d|                     |j	        d         |          z
           }|                     |d          sdS | j        
                    |           t          j                            d|                                 |j        d         ||g|j	        d         g          }|j        
                    t          j                            dt+          |                    g           | j                            |           dS )a  
        Interface function that tries to fuse a node sequence containing a ReduceMean node into a single
        LayerNormalization node.

              +----------------------+
              |                      |
              |                      v
          [Root] --> ReduceMean -->  Sub  --> Pow --> ReduceMean --> Add --> Sqrt --> Div --> Mul --> Add
                     (axis=2 or -1)  |      (Y=2)   (axis=2 or -1)  (E-6 or E-12 or 0) ^
                                     |                                                 |
                                     +-------------------------------------------------+

         It also handles cases of duplicated sub nodes exported from older version of PyTorch:

              +----------------------+
              |                      v
              |           +-------> Sub-----------------------------------------------+
              |           |                                                           |
              |           |                                                           v
          [Root] --> ReduceMean -->  Sub  --> Pow --> ReduceMean --> Add --> Sqrt --> Div  --> Mul --> Add
              |                      ^
              |                      |
              +----------------------+
        r   r   NSubr   DivF)	recursive)SqrtAddr   Powr   )r   r   r   r   r   )r   r   r   r    Castr   )r   r   r   r   r   r   g-C6?   g       @Mulr   r   )nameinputsoutputsepsilon)r
   get_childrenleninputop_typefind_first_child_by_typematch_parent_pathsget_constant_inputfind_constant_inputoutputextendis_safe_to_fuse_nodesinput_indexis_constant_with_specified_ranknodes_to_removeonnxhelper	make_nodecreate_unique_node_name	attributemake_attributefloatnodes_to_addappend)r   r   r   r   children
root_inputdiv_nodechildpath_idparent_nodes_sub_nodesecond_add_nodei
add_weightpow_nodemul_nodelast_add_nodesubgraph_nodesweight_input
bias_inputnormalize_nodes                         r   fusezFusionLayerNormalization.fuse   s   < :**+;=PQQx==AX!2!2F%+A.
A;%''8A;+<Q+?:+M+MFx==A{"e++x{/@/Cz/Q/Q 	 	E44UECVbg4hhH# $F#'#:#:<<<oooNGGG&&&  
$
 
$
 q Q;;F#8##F&q///@@:qJ4G4GF?##Hc22a77F&xq'9:1=u$$F+HOA,>?B E))F*+h'''l3B3/000}hABBB)) 	
 
 	 F~a$*:*:8?1;Mx*X*X&XY33L!DD 	F"(T-=-=hoa>PR_-`-`)`a
33JBB 	F##N333.. --//$*1-|ZH")!,-	 / 
 
 	 '')C)CIuU_O`O`)a)a(bccc  00000r   )r
   r   )r   r   r   r   r   r   )__name__
__module____qualname__r   rR   __classcell__)r   s   @r   r	   r	      s_        D D D D D Du1 u1 u1 u1 u1 u1 u1 u1r   r	   )
__future__r   r7   
onnx_modelr   fusionr   r	    r   r   <module>r[      s    # " " " " "  " " " " " "      y1 y1 y1 y1 y1v y1 y1 y1 y1 y1r   