
    8h               
       v   S SK JrJrJr  S SKrS SKJr  S SKJs  Jr	  S SK
JrJr  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  S	S
KJr  \R2                  R4                  rS\\\\   4   S\\\\4      4S jr \\\\\\4   4   r!\\\!\!4   \\!\!\!\!4   4   r"S\#\\"\$4      4S jr%S\S\"S\S\\   S\&4
S jr'g)    )AnyCallableUnionN)FakeQuantizeBaseObserverBase)get_native_backend_config)!_get_pattern_to_quantize_handlers)getattr_from_fqn)GraphModule)Node   )NSNodeTargetType base_name_to_sets_of_related_opsreturnc                 *   [        5       nU R                  5        Ht  n[        U5      n[        S[	        U5      5       HM  n[        U[	        U5      5       H1  nUR                  X4   X5   45        UR                  X5   X4   45        M3     MO     Mv     U$ )Nr   )setvalueslistrangelenadd)r   type_a_related_to_bss_listidx_0idx_1s         V/var/www/fran/franai/venv/lib/python3.13/site-packages/torch/ao/ns/fx/pattern_utils.pyget_type_a_related_to_br      s     KN%-446a1c&k*Euc&k2#''(FG#''(FG 3 + 7     c            	         / n [        [        5       5      nSnUR                  5        H  n[        U[        5      (       aM  [        U5      S:X  a>  [        US   [        5      (       a&  [        US   5      S:X  a  US   US   S   US   S   4n[        U[        5      (       a  U R                  X245        [        [        4 H4  n[        U[        5      (       a  U/UQ7nOXC4nU R                  XR45        M6     M     SnS[        R                  4[        R                  [        R                  S4U4[        R                  [        R                   4U4[        R"                  [        R$                  4U4[        R&                  [        R(                  4U4[        R*                  [        R                  [        R                   4U4[        R*                  [        R"                  [        R$                  4U4[        R*                  [        R&                  [        R(                  4U4/nU HV  nU R                  U5        U R                  [        /US   Q7US   45        U R                  [        /US   Q7US   45        MX     U $ )a  
Set of potential fusions, in reverse order.  The order is reversed
to match how fusion patterns are defined in quantization code.

Fusion format:
((fusion_op_0, fusion_op_1), base_op_idx)

Where base_op_idx is the idx of the op we should use to match other related
ops. Note: base_op_idx is specified in non-reverse order, i.e. a base_op_idx
of 0 represents the first op in regular (non-reverse) order, 1 represents the
second op, etc.
r      r   to
dequantize)r	   r   keys
isinstancetupler   appendr   r   torchfloat16FrelulinearnnBatchNorm1dConv1dBatchNorm2dConv2dBatchNorm3dConv3dReLU)	resultsall_quant_patternsdefault_base_op_idxquant_patternclsnew_patternfp16_em_base_op_idxpatterns_to_addps	            r   get_reversed_fusionsr>   3   s.    /1G ;;T;VW+002
 }e,,M"a'=+U33M!$%* +1-}Q/?/BMRSDTUVDWXM
 mU++NNM?@ !"23C-//"3]3"2NNK=> 4/ 3F 
 EMM"AFFAHHlC	
 .."))	$&9:
.."))	$&9:
.."))	$&9:
''2>>299	-/BC
''2>>299	-/BC
''2>>299	-/BCO  q-!-qt45)1AaD11Q489 
 Nr   end_nodereversed_fusiongm
seen_nodesc                    U n[        [        U5      5       GH  nXC;   a    gX   nUR                  S:X  a  [        U[        5      (       + =(       a    [        U[
        5      (       + nU(       a`  UR                  U:w  a    g[        UR                  5      S:  a3  [        UR                  S   [        5      (       a  UR                  S   nM    g  gUR                  S:X  a  [        U[
        5      nU(       a  [        UR                  [        5      (       d   e[        X$R                  5      n	[        U[
        5      (       d    g[        X5      (       d    g[        UR                  5      S:  a4  [        UR                  S   [        5      (       a  UR                  S   nGM    g  gUR                  S:X  Ga  [        U[        5      =(       a    [        U5      S:H  n
[        U[        5      nU(       d  U
(       a  U(       a  UR                  U:w  a    gO_[        U[        5      (       d   eUR                  US   :w  a    g[        UR                  5      S:  a    gUR                  S   US   :w  a    g[        UR                  5      S:  a4  [        UR                  S   [        5      (       a  UR                  S   nGM    g  g  g   g)	zN
Returns true if a pattern ending with `end_node` matches
the fusion pattern.
Fcall_functionr   call_modulecall_methodr!   r   T)r   r   opr%   strtypetargetargsr   r
   r&   )r?   r@   rA   rB   cur_node
fusion_idxcur_fusion_elfusion_el_is_funfusion_el_is_mod
target_mod!fusion_el_is_meth_with_second_argfusion_el_is_meth_without_argss               r    end_node_matches_reversed_fusionrT      s5    HC01
!'3;;/)$.}c$B B  }d33   ??m3 x}}%)jq9I4.P.P'}}Q/H [[M))->!(//37777-b//B
!-66 !*<< x}}%)jq9I4.P.P'}}Q/H [[M)=%0LS5G15L . .8s-K*-1R1-7$ 8 &mU;;;;-*::$X]]+a/$!q)]1-==$x}}%)jq9I4.P.P'}}Q/H } 2@ r   )(typingr   r   r   r(   torch.nnr-   torch.nn.functional
functionalr*   torch.ao.quantizationr   r   $torch.ao.quantization.backend_configr   )torch.ao.quantization.fx.quantize_handlerr	   torch.ao.quantization.utilsr
   torch.fxr   torch.fx.graphr   ns_typesr   ops	quantizedtoqdictrH   r   r&   r   NSFusionElTypeNSFusionTyper   intr>   boolrT    r   r   <module>ri      s$   ' '     @ J W 8    & ii&*34D0E+E&F!11	23& 	S 	..
()	....
HIKPd5s):#;< PfKK!K 	K D		K
 
Kr   