
    shϓ                        % S SK r S SKrS SKJr  S SKJrJr  S SKrS SKJ	s  J
s  Jr  S SKJ
r
  S SKJ
s  Jr  S SKJr  S SKJrJrJr  S SKJrJr  S SKJr  S SKJr  S	S
KJrJ r J!r!  / r"\#\   \$S'   S\RJ                  S\&S\RJ                  S\RJ                  4S jr'\RP                  \RR                  \RT                  \RV                  0r, " S S\
RZ                  5      r. " S S\
RZ                  5      r/S\.S\/S\RJ                  4S jr0 " S S\" SSS/5      5      r1\.Re                  \Rf                  \RT                  S9r4\/Re                  \Rj                  \RV                  S9r6\1" \4\6S9r7S\84S jr9S\84S jr:S\84S  jr;S!\S\84S" jr<S#\
RZ                  S\84S$ jr=S%\S&\S'\>\?\
RZ                  4   S\@\\   \\/   4   4S( jrAS)\S'\>\?\
RZ                  4   S\\   4S* jrBS!\S'\>\?\
RZ                  4   S\\.   4S+ jrCS!\S'\>\?\
RZ                  4   S\\RJ                     4S, jrDS!\S'\>\?\
RZ                  4   SS4S- jrES!\S'\>\?\
RZ                  4   S.\RJ                  S/\\RJ                     SS4
S0 jrFS)\S&\S'\>\?\
RZ                  4   S.\RJ                  S/\\RJ                     SS4S1 jrGS)\S'\>\?\
RZ                  4   SS4S2 jrHS&\S!\S3\4S4 jrIS&\S'\>\?\
RZ                  4   S\>\?\/4   4S5 jrJS&\S'\>\?\
RZ                  4   S6\>\?\/4   SS4S7 jrKS&\4S8 jrLS9\
RZ                  S:\
RZ                  S;\RJ                  S\>\?\M4   4S< jrNS=\>\?\M4   S>\&S\4S? jrOg)@    N)
namedtuple)AnyOptional)_get_observed_graph_module_attr)
_with_argsObserverBasePerChannelMinMaxObserver)_parent_namecheck_min_max_valid)GraphModule)Node   )get_new_attr_name_with_prefixmaybe_get_next_modulenode_arg_is_weightCUSTOM_MODULE_SUPP_LISTscaleaxisinputreturnc                 j    S/UR                   -  nUR                  U5      X1'   U R                  U5      $ )zMReshapes the scale so that we can multiply it to the input by the given axis.r   )ndimsizeview)r   r   r   	new_shapes       v/Users/tiagomarins/Projetos/claudeai/copy_bank/venv/lib/python3.13/site-packages/torch/ao/quantization/fx/_equalize.pyreshape_scaler      s1    ejj Ijj&IO::i      c                      ^  \ rS rSrSr\R                  \R                  SSS4 S
U 4S jjjrS r	S r
S rS r\" \5      rS	rU =r$ )_InputEqualizationObserver,   a  Observer for tracking the running min/max values of input columns, and
computing the quantization parameters for the overall min/max input values.

Args:
    dtype: Quantized data type
    qscheme: Quantization scheme
    quant_min: Minimum quantization value. If unspecified, it will
        follow the 8-bit setup.
    quant_max: Maximum quantization value. If unspecified, it will
        follow the 8-bit setup.

The running minimum/maximum :math:`x_\text{min/max}` are computed in the
same way as :class:`~torch.ao.quantization.observer.PerChannelMinMaxObserver`,
with the difference that the running min/max values are stored per column.
This observer is intended to be used along with a WeightEqualizationObserver
to calculate the equalization scale.
Nc           	        > [         TU ]  5         U[        R                  [        R                  1;  a  [        S5      eXl        X l        [        U   n[        SUUUUUS9U l
        [        R                  " S5      U l        / U l        g )Nz Input qscheme must be per-tensorr   ch_axisdtypeqscheme	quant_min	quant_maxfactory_kwargs)super__init__torchper_tensor_affineper_tensor_symmetric	TypeErrorr%   r&   (qsheme_mapping_per_tensor_to_per_channelr	   	input_obstensorequalization_scaleequalization_shapeselfr%   r&   r'   r(   r)   per_channel_qscheme	__class__s          r   r+   #_InputEqualizationObserver.__init__?   s     	522E4N4NOO>??
FwO1')
 #(,,q/-/r   c                     UR                   S:  a  UR                   S::  d  [        S5      eS/UR                   -  U l        UR                  S5      U R                  S'   U R	                  U5      $ )N      >InputEqualizationObserver only supports Linear and Conv layersr   )r   
ValueErrorr4   r   r1   )r6   x_origs     r   forward"_InputEqualizationObserver.forward\   se    q V[[A%5P 
 $%#"3%+[[^"~~f%%r   c                 Z    U R                   R                  U R                   R                  4$ N)r1   min_valmax_valr6   s    r   get_input_minmax+_InputEqualizationObserver.get_input_minmaxh   s!    &&(>(>??r   c                     UR                  5       S:X  a  U[        R                  " S5      :X  a  g [        R                  " XR                  5      U l        g )Nr   )nelementr,   r2   reshaper4   r3   r6   r3   s     r   set_equalization_scale1_InputEqualizationObserver.set_equalization_scalek   sC     &&(A-2DUV2W"'-- 7 7#
r   c                    U R                   R                  5       S:X  a;  U R                   [        R                  " S5      :X  a  [        R
                  " S5        gU R                  5       u  p[        U R                   SU5      n[        R                  " [        R                  " X5      5      n[        R                  " [        R                  " X#5      5      nXE4$ )z!Returns the scaled min/max inputsr   z}Must call calculate_equalization_scale before calling calculate_scaled_minmax. Will not scale the next quantization observer.NNr   )r3   rJ   r,   r2   warningswarnrG   r   minmulmax)r6   
min_inputs
max_inputsequalization_scale_reshapedmin_input_scaledmax_input_scaleds         r   calculate_scaled_minmax2_InputEqualizationObserver.calculate_scaled_minmaxt   s     ##,,.!3''5<<?:MMC 
 $(#8#8#: &3##Q
'
# !99UYYz%WX 99UYYz%WX11r   )r%   r3   r4   r1   r&   r   N)__name__
__module____qualname____firstlineno____doc__r,   quint8r-   r+   r@   rG   rM   r[   classmethodr   	with_args__static_attributes____classcell__r8   s   @r   r    r    ,   sX    ( ll''0 
0 0:
&@
20 J'Ir   r    c                      ^  \ rS rSrSr\R                  \R                  SSS4 S	U 4S jjjrS r	S r
S r\" \5      rSrU =r$ )
_WeightEqualizationObserver   aC  Observer for tracking the running min/max values of weight columns and
rows, and computing the quantization parameters for the weight rows.

Args:
    dtype: Quantized data type
    qscheme: Quantization scheme
    quant_min: Minimum quantization value. If unspecified, it will
        follow the 8-bit setup.
    quant_max: Maximum quantization value. If unspecified, it will
        follow the 8-bit setup.

This observer is made up of 1 PerChannelMinMaxObserver `weight_col_obs` used
to record the running minimum and maximum of columns of incoming weight
tensors. This observer is intended to be used along with an
InputEqualizationObserver to calculate the equalization scale.

The running minimum/maximum :math:`w_\text{min/max}` are computed in the
same way as :class:`~torch.ao.quantization.observer.PerChannelMinMaxObserver`.
Nc           	        > [         TU ]  5         Xl        X l        SU l        UnU[
        R                  [
        R                  1;   a	  [        U   n[        SUUUUUS9U l
        [
        R                  " S5      U l        g )Nr   r#   )r*   r+   r%   r&   r$   r,   r-   r.   r0   r	   weight_col_obsr2   r3   r5   s          r   r+   $_WeightEqualizationObserver.__init__   s|     	
%u..0J0JKK"J7"S6')
 #(,,q/r   c                 z    UR                   S:  a  UR                   S::  d  [        S5      eU R                  U5      $ )Nr;   r<   r=   )r   r>   rm   )r6   w_origs     r   r@   #_WeightEqualizationObserver.forward   s<    q V[[A%5P  ""6**r   c                 Z    U R                   R                  U R                   R                  4$ rC   )rm   rD   rE   rF   s    r   get_weight_col_minmax1_WeightEqualizationObserver.get_weight_col_minmax   s%    ##++T-@-@-H-HIIr   c                     Xl         g rC   )r3   rL   s     r   rM   2_WeightEqualizationObserver.set_equalization_scale   s    "4r   )r$   r%   r3   r&   rm   r]   )r^   r_   r`   ra   rb   r,   qint8r-   r+   r@   rs   rM   rd   r   re   rf   rg   rh   s   @r   rj   rj      sS    , kk''2 
2 28+J5 J'Ir   rj   r1   
weight_obsc                    U R                  5       u  p#UR                  5       u  pE[        X#5      (       a  [        XE5      (       d,  [        R                  " S5        [
        R                  " S5      $ UR                  UR                  :X  d)  [        SSUR                   SUR                   S3-   5      e[
        R                  " XT-
  X2-
  -  5      nSXfS:H  '   [
        R                  " USSSS9nU$ )	zCalculates the equalization scale and sets the equalization_scale value
in the observers.

Args:
    input_obs: Observer that tracks the ranges for the input columns
    weight_obs: Observer that tracks the ranges for the weight columns
ztMust run observer before calling calculate_equalization_scale. Returning default equalization scale torch.tensor(1).r   z6Input and Weight must have the same column dimension. zFound z and z shapes instead.g        )nanposinfneginf)rG   rs   r   rQ   rR   r,   r2   shaper>   sqrt
nan_to_num)r1   rx   rV   rW   min_weightsmax_weightsr3   s          r   calculate_equalization_scaler      s      )99;Z!+!A!A!C[ 	J3399F	
 ||A 1 11Dz''(k.?.?-@@PQR
 	

 		"z'>? 56S01))*<!AVWXr   c                      ^  \ rS rSrSrSr\R                  R                  \R                  R                  4U 4S jjr	Sr
U =r$ )EqualizationQConfig   a.  
Describes how to quantize a layer or a part of the network specifically for
input-weight equalization by providing settings (observer classes) for
inputs, outputs, and weights.

Note that EqualizationQConfig needs to contain observer **classes** (like
MinMaxObserver) or a callable that returns instances on invocation, not the
concrete observer instances themselves.
Quantization function will instantiate observers multiple times for each of
the layers.

Observer classes have usually reasonable default arguments, but they can be
overwritten with `with_args` method (that behaves like functools.partial):

my_qconfig = EqualizationQConfig(input_activation=_InputEqualizationObserver.with_args(dtype=torch.qint8),
                                weight=_WeightEqualizationObserver.with_args(dtype=torch.qint8))
 c                    > [        U[        R                  5      (       d  [        U[        R                  5      (       a  [        S5      e[        TU ]  XU5      nU$ )NzEqualizationQConfig received observer instance, please pass observer class instead. Use MyObserver.with_args(x=1) to override arguments to constructor if needed)
isinstancennModuler>   r*   __new__)clsinput_activationweightr6   r8   s       r   r   EqualizationQConfig.__new__  sO    &		22j6S6Sa  wsf=r   )r^   r_   r`   ra   rb   	__slots__r,   r   Identityr   rf   rg   rh   s   @r   r   r      s2    $ I&+hh&7&7@Q@Q  r   r   r   r   )r%   r&   )r   r   c                     [        U 5      [        R                  [        R                  [        R                  [        R
                  4;   $ )z/Checks if the fused node supports equalization.)typenni
LinearReLU
ConvReLU1d
ConvReLU2d
ConvReLU3dmodules    r   "fused_module_supports_equalizationr   $  s4    <	  r   c                     [        U 5      [        R                  [        R                  [        R                  [        R
                  4;   $ )z2Checks if the torch.nn node supports equalization.)r   r   LinearConv1dConv2dConv3dr   s    r   nn_module_supports_equalizationr   .  s*    <BIIryy"))RYYGGGr   c                 &    [        U 5      [        ;   $ )z0Checks if the custom node supports equalization.)r   r   r   s    r   #custom_module_supports_equalizationr   3  s    <222r   nodec                    U R                   S:X  aq  [        U[        U R                  5         5      =(       dI    [	        U[        U R                  5         5      =(       d!    [        U[        U R                  5         5      $ U R                   S:X  aK  U R                  [        R                  [        R                  [        R                  [        R                  4;   $ g)zxChecks if the current node supports equalization
Currently we only support nn.Linear/F.Linear and nn.Conv/F.conv layers
call_modulecall_functionF)opr   strtargetr   r   Flinearconv1dconv2dconv3d)r   moduless     r   node_supports_equalizationr   8  s     ww-+GC4D,EF N1'#dkk:J2KLN273t{{;K3LM	

 
O	#{{qxx188QXXFFFr   observerc                 .    [        U [        [        45      $ rC   )r   r    rj   )r   s    r   is_equalization_observerr   G  s    -/JK r   input_eq_obs_nodemodelr   c                 (   SnU R                   R                  5        H  n[        XB5      (       d  M  Un  O   Uc   eUR                  S:X  av  [	        US5      nUc   eUnUR                  UR                  S5      c   eUR                  UR                  S5      R                  5       n[        U[        5      (       d   eX74$ UR                  S:X  a@  [        X25      nUb2  U[        UR                  5         n[        U[        5      (       d   eX74$ g)a  Gets the following weight equalization observer. There should always
exist a weight equalization observer after an input equalization observer.

Returns the operation node that follows the input equalization observer node
and the weight equalization observer
Nr   !equalization_node_name_to_qconfigr   rP   )userskeysr   r   r   getnamer   r   rj   maybe_get_weight_eq_obs_noder   r   )	r   r   r   op_nodeuser&maybe_equalization_node_name_to_configr   weight_eq_obsweight_nodes	            r   get_op_node_and_weight_eq_obsr   R  s'    G!'',,.%d44G /
 zz]" 2Q62
. 6AAA<b)044W\\4HTTT9==LL$

&( 	 -)DEEEE%%		&27D"#C(:(:$;<Mm-HIIII))r   r   c                    U R                   S:X  d   eU R                   Hi  n[        X5      (       d  M  [        U[        5      (       a;  UR                   S:X  a+  [        U[        UR                  5         [        5      (       d   eUs  $    g)z8Gets the weight equalization observer node if it exists.r   r   N)r   argsr   r   r   r   r   rj   )r   r   node_args      r   r   r   ~  s     ::(((LLg008T**KK=0C013N  O ! r   c                 T   [        X5      (       d   e[        X[        R                  5      nUc  [        X[        R
                  S9nUc  [        X[        5      O[        X![        5      nUc  g[        X1[        5      nUc  gU[        U5         n[        U[        5      (       d   eU$ )a  Gets the following input equalization observer if it exists.

For example, in the case of connecting linear layers:
    x -> inp_obs1 -> eq_obs1 -> linear1 -> out_obs1 -> eq_obs2 -> linear2 -> out_obs2
If the node being passed in is the linear1 node, then we want to return eq_obs2,
the following equalization observer for linear2.

However, if there are no connecting layers:
    x -> inp_obs1 -> eq_obs1 -> linear1 -> out_obs1 -> add
Then we want to return None.

In the case of an unfused linear-relu layer with a connecting linear layer:
    linear1 -> relu -> out_obs1 -> eq_obs2 -> linear2 -> out_obs2
Since it is unfused, we want to skip over the relu layer and return eq_obs2,
the following equalization observer for linear2.
N)target_functional_type)
r   r   r   ReLUr   relur   r    r   r   )r   r   maybe_relu_nodemaybe_obs_nodemaybe_eq_obs_nodemaybe_eq_obss         r   maybe_get_next_input_eq_obsr     s    ( &d4444 ,D277CO/!&&
 " 	d\:"?\J 
 -!;  3012Ll$>????r   c                     [        X5      nU(       aO  UR                  R                  5       S:X  a%  UR                  [        R                  " S5      :X  a  gUR                  $ g)aA  If the next next node is an InputEqualizationObserver then we want to
return its equalization scale, else we return 1

This is used in the case where there are two connecting linear layers:
    linear1 -> LinearOutObs -> InputEqObs -> linear2
In this case, the node given is linear1 and we want to locate the InputEqObs.
r   N)r   r3   rJ   r,   r2   )r   r   next_inp_eq_obss      r   !maybe_get_next_equalization_scaler     sP     2$@O..779Q>22ell1oE111r   c                 T   U[        U R                  5         n[        U[        5      (       d   eU R                  S   n[        U[
        5      (       d   eU[        UR                  5         n[        U[        5      (       d  gUR                  5       u  pVUc  Uc  gXTl        Xdl	        g)zScales the following input quantization observer's min/max values by
updating the values with the scaled min/max values calculated by the input
equalization observer
r   N)
r   r   r   r    r   r   r   r[   rD   rE   )r   r   input_eq_obsinput_quant_obs_nodeinput_quant_obsrY   rZ   s          r   scale_input_observerr     s    
 3t{{+,Ll$>????99Q<*D1111c"6"="=>?Oo|44)5)M)M)O&$4$<..r   r3   next_equalization_scalec                 f   Uc  g[        U[        U R                  5         5      (       a  U[        U R                  5         S   nOU[        U R                  5         n[        U5      (       d  [	        U5      (       d   eUR
                  n[        U[        R                  5      (       d   e[        USU5      n[        R                  " U[        R                  " U5      5      nUc  [        R                  " U5      Ul        g[        USU5      n[        R                  " Xx5      n[        R                  " U5      Ul        UR                  n	U	c  g[        U	[        R                  5      (       d   e[        USU	5      n[        R                  " X5      n
[        R                  " U
5      Ul        g)a  Scale the weights for input-weight equalization by multiplying the
weight by 1/equalization_scale and next_equalization_scale

Args:
    node: Current node whose weights we want to scale
    equalization_scale: Current node's calculated equalization scale
    next_equalization_scale: Next node's calculated equalization scale if
       the following node needs to be equalized, 1 otherwise
Nr   r   )r   r   r   r   r   r   r   r,   Tensorr   rT   
reciprocalr   	Parameterbias)r   r   r3   r   	op_moduler   rX   scaled_weight next_equalization_scale_reshapedr   scaled_biass              r   scale_weight_noder     so    !)'#dkk2B*CDDC,-a0	C,-	* 	,Y	7	78 8 Ffell++++ #00BAv"NIIfe&6&67R&STM&<<6	 (55LaQW'X$IImNM||M2I >>D|dELL)))) (55LaQU'V$))DCK\\+.INr   c                 j   Uc  g[        X5      nUc  gUR                  S   nUc  g[        U[        5      (       a+  [        U[	        UR
                  5         [        5      (       d   eUR                  S   nUc  g[        U[        5      (       a  UR                  S:X  d   e[        UR
                  5      u  p[        X(   U	5      n
[        USU
5      n[        R                  " U
[        R                  " U5      5      nUc  [        X(   X5        g[        USU5      n[        R                  " X5      n[        X(   X5        [        R                  " UR!                  [	        UR
                  5      5      U5      (       d   eSnU R                   H@  n[        U[        5      (       d  M  UR                  S:X  d  M,  SUR"                  ;   d  M>  Un  O   Uc  g[        UR
                  5      u  nn[        UU   U5      n[        USU5      n[        R                  " UU5      n[        UU   UU5        g)z-Scales the weight value for functional layersNr   get_attrr   r   )r   r   r   r   r   r   r   r   r
   getattrr   r,   rT   r   setattrallclose
get_bufferr   )r   r   r   r3   r   weight_eq_obs_nodeweight_quant_obs_noder   weight_parent_nameweight_namer   rX   r   r   	bias_noder   bias_parent_name	bias_namer   r   s                       r   scale_weight_functionalr   (  s     ! 6gG! /33A6$+T22z)0012L8 8  
 (,,Q/Kk4(([^^z-III&2;3E3E&F#W0+>F
 #00BAv"NIIfe&6&67R&STM&+[H (5M($ IImNMG'D>>%**3{/A/A+BC]SSSS IdD!!dgg&;$))@SI	 
 ".y/?/?"@i7+,i8D (55LaQU'V$))D"BCKG$%y+>r   c                     [        X5      nUc  gUR                  S   nUc  g[        U[        5      (       d   eU[	        UR
                  5         n[        U[	        UR
                  5         [        5      (       d   eUR                  5         g)zlGiven the operation node, we want find the corresponding quantization
observer and reset its min/max values
Nr   )r   r   r   r   r   r   r   reset_min_max_vals)r   r   r   r   weight_quant_obss        r   clear_weight_quant_obs_noder   v  s     6gG!.33A6$+T2222s#8#?#?@Agc"7">">?@,OOOO'')r   	prev_nodec                     [        UR                  R                  5       5      nU H  nUR                  X5        M     U R                  R                  U5        g)zaRemoves the given node from the model by replacing all of its users with
the given previous node
N)listr   r   replace_input_withgraph
erase_node)r   r   r   
orig_users	user_nodes        r   remove_noder     sE     djjoo'(J	$$T5   
KK4 r   c                    0 nU R                   R                   GHM  nUR                  S:X  d  M  [        XR                     [
        5      (       d  M9  XR                     n[        U[
        5      (       d   e[        X0U5      u  pVUb  Uc  Mt  UR                  S:X  a  [        U[        UR                  5         5      (       a@  U[        UR                  5         S   n[        U5      (       d   eU" UR                  5        O(U" U[        UR                  5         R                  5        [        XF5      nUR                  U5        UR                  U5        XbUR                  '   GMP     U$ )ag  Update all of the observer's equalization scale. For each
InputEqualizationObserver, we will find the location of the next
WeightEqualizationObserver, create it, and calculate the equalization scale
based on the two observers.

We will then return a dictionary mapping operation node names to
the corresponding WeightEqualizationObservers for that operation.
r   r   )r   nodesr   r   r   r    r   r   r   r   r   r   rM   r   )	r   r   weight_eq_obs_dictr   r   r   r   r   r3   s	            r   update_obs_for_equalizationr    s>    !!77m#
KK "<)
 )
 #;;/Ll,FGGGG%B4PW%X"G-"7zz]* 6gc'..>Q6RSS$S%89!<F:6BBBB!&--0!'#gnn*=">"E"EF ">" //0BC001CD/<w||,9 "< r   r   c           	      N   U R                   R                   GH  nUR                  S:X  Gan  [        XR                     [
        5      (       GaL  UR                  S   nUR                  S   n[        XQ5      (       d  SUR                  ;   a  [        XU5        M  [        X15        U R                   R                  U5         [        UR                  S-   5      nU" U5      n[        XXR                     R                  5        U R                   R                  SU5      nSSS5        U R                   R!                  W5         XX4n	U R                   R                  S["        R$                  U	5      n
SSS5        UR'                  UW
5        [        XU5        GM  UR)                  UR                  S5      c  GM  UR)                  UR                  5      n[        U[*        5      (       d   eUR                  nUR-                  5       S:X  a  U["        R.                  " S5      :X  a  Sn[1        X15      nUR                  S:X  a  [3        X1X5        GM;  UR                  S:X  au  [5        UU UUU5        [7        X15      nUc    g[        U[9        UR                  5         [*        5      (       d   e[;        X15        UR                  S   n[        XU5        GM  [=        S	S
UR                   SUR                   S3-   5      e   g! , (       d  f       GN= f! , (       d  f       GN= f)a  Converts the equalization operations and updates the other nodes in the
following way:
    - Removes the input equalization observers and inserts a mul operator
      along with an equalization scale node wherever applicable (we do not
      want to insert a mul operator between connecting linear layers).
    - Updates the input quantization observers with the scaled input min/max
      values.
    - Scales the weights by the current and next equalization scales.
    - Removes the weight equalization observer node if it exists.

Before (after prepare):
                                weight values
                                      |
                                WeightQuantObs
                                      |
                                  WeightEqObs
                                      |
    x -> InpQuantObs -> InpEqObs -> linear -> OutQuantObs

After this function:
                                          scaled weight values
                                                  |
   equalization scale                       WeightQuantObs
          |                                       |
    x -> mul -> InpQuantObs (scaled min/max) -> linear -> OutQuantObs

After convert:
   equalization scale                 scaled weight values
          |                                    |
    x -> mul -> quantize_per_tensor -> quantized::linear

Note that although the equalization observer appeared after the quantization
observer after prepare_fx, the mul node appears before the quantization node
after convert_fx. This is because placing the equalization observer after
the quantization observer in prepare_fx would allow us to keep the invariant
that the graph before the current node inserts its observers is not
modified.

Having the equalization observer before the quantization observer would also
cause some inconsistences between the ordering of the quantization and
equalization observers.
For example, a single linear layer would look like:
    x -> InpEqObs1 -> InpQuantObs1 -> linear1 -> OutQuantObs1
But between two connected linear layers, it would look like:
    linear1 -> OutQuantObs1 -> InpEqObs2 -> linear2 -> OutQuantObs2
r   r   r   _equalization_scaler   Nr   r   z=Expected operation node to be 'call_module' or 'call_functionzInstead got node z as 'z'.)r   r   r   r   r   r    r   r   r   r   r   inserting_beforer   r   r3   create_nodeinserting_afterr,   rT   r   r   rj   rJ   r2   r   r   r   r   r   r   r>   )r   r   r   r   inp_quant_obs_noder   get_new_eq_scale_namer   eq_scale_nodeinputsmul_noder   r3   maybe_next_equalization_scaler   s                  r   convert_eq_obsr    s   f !!77m#
KK "<)
 )
 "&1*//2I +9>>Y^^+E);< !/ --.@A(ENN%::)% -W5W[[%9%L%LM % 7 7
D I B ,,];#3 ;;22?EIIvV < 11)XF%78##DIIt4@.22499=Mm-HIIII!.!A!A #++-2&%,,q/9%)",M-)
 ww-'!#5 O+'&1 &B$%P"%-!C 2 9 9:;=X    ,D: /33A6	EyA S)$))E$''"EF u ": BA <;s    AL/L
L	
L$	c                     [        U R                  SS95      n[        X5      n[        XU5        [	        X R
                  5      $ )zbReference function which applies changes needed for equalization, but
does not quantize the nodes
F)remove_duplicate)dictnamed_modulesr  r  r   r   )r   r   r   s      r   _convert_equalization_refr  V  sC     5&&&>?G 5UD5#56ukk**r   model_amodel_bxc           	         SSK Js  Js  Jn  SSKJn  U" 5       nUS   R                  [        R                  5        UR                  SU SUUR                  US9u  pgU" U5        U" U5        UR                  XgUR                  S5      nUR                  USS[        R                  R                  R                  R                  R                  S5        0 n	U H*  n
X   S	   S   S   S
   nX   S	   S   S   S   S   nXU'   M,     U	$ )a  Runs the Numeric Suite on model_a and model_b and returns a dictionary
containing the SQNR between layers in model_a and model_b.

Note: In order to support equalized models, this function has a hacky fix in
which we do not match any torch.mul operators. This is because equalized
models contain extra mul operators to scale the input by the equalization
scale, but this edge case has not been resolved yet within the numeric suite code.

Args:
    model_a: A float model
    model_b: A quantized model
    x: Inputs to use during calibration
r   N)get_unmatchable_types_mapfuns_unmatchablefp32int8)unmatchable_types_mapsqnrnode_outputfqn)torch.ao.ns._numeric_suite_fxaons_numeric_suite_fxtorch.ao.ns.fx.mappingsr  addr,   rT   add_loggersOutputLoggerextract_logger_info%extend_logger_results_with_comparisonfxutilscompute_sqnr)r  r  r  r!  r  r  
model_a_ns
model_b_nsactivation_comparison_dictlayer_sqnr_dictkeylayerr  s                r   get_layer_sqnr_dictr2  i  s     /.A57,-11%))<^^
3 , J qMqM!#!7!7" ,,")) O)*/>vFqI%P).}=fEaHPQRS!% *
 r   r/  num_layers_to_equalizec                     [        U R                  5       [        R                  " S5      S9nUSU nU Vs/ s H  oDS   [        4PM     nnSU0nU$ s  snf )a  Given the layer to SQNR dictionary, find the layers with the highest
quantization errors, and return an equalization_qconfig_dict
specifying to only equalize those top layers.

Args:
    layer_sqnr_dict: Dictionary mapping layer names to SQNR values (found
        when comparing an equalized model against a float model)
    num_layers_to_equalize: Number of layers with the highest quantization
       errors to equalize
r   )r0  Nr   module_name)sorteditemsoperator
itemgetterdefault_equalization_qconfig)r/  r3  layer_sqnr_sortedlayers_to_equalizeitemmodule_to_qconfig_listequalization_qconfig_dicts          r   get_equalization_qconfig_dictr@    sv      446H<O<OPQ<RS*+B,BC
 =O<NDa./<N   "/0F G$$	s   A)Pr8  rQ   collectionsr   typingr   r   r,   torch.ao.nn.intrinsicr   r   	intrinsicr   torch.nntorch.nn.functional
functionalr   %torch.ao.quantization.fx.graph_moduler   torch.ao.quantization.observerr   r   r	   torch.ao.quantization.utilsr
   r   torch.fxr   torch.fx.graphr   r*  r   r   r   r   r   __annotations__r   intr   r-   per_channel_affiner.   per_channel_symmetricr0   r   r    rj   r   r   re   rc   input_equalization_observerrw   weight_equalization_observerr:  boolr   r   r   r   r   r  r   tupler   r   r   r   r   r   r   r   r   r  r  r  floatr2  r@  r   r   r   <module>rV     s     "    # #    Q 
 J     &( c '! !S ! !%,, ! 
U55	 ; ;, (`( `(F?(")) ?(D$)$7R$
\\$N$'98&DEB 9BB
,, : : C    ;DD
++u::  E     309U  
$ Ht H
34 3
T t ryy T ))$/):>sBII~:N)
8D>8$?@@A)X bii0d^$/
/c299n-/()/d
c299n-ell*/t /d3		>.B /t /,8/
8/#ryy.!8/ 8/ &ell3	8/
 
8/vK?K?K? #ryy.!K? 	K?
 &ell3K? 
K?\* *S"))^8L *QU *$!{ !$ !4 !**!%c299n!5*	#*
*+*ZPP#ryy.!P S"==>P 
	Pf+[ +&4YY4!#4/4||4	#u*4n%#u*%%?B%%r   