
    ȅi!              
       ~   S SK Jr  S SK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  \R4                  R6                  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)    )Callable)Any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        Hs  n[        U5      n[        [	        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     Mu     U$ )N)setvalueslistrangelenadd)r   type_a_related_to_bss_listidx_0idx_1s         V/home/james-whalen/.local/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3v;'Euc&k2#''(FG#''(FG 3 ( 7     c            	         / n [        [        5       5      nSnU 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   
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=   4   s%    /1G ;;T;VW+
 }e,,M"a'=+U33M!$%* +1-}Q/?/BMRSDTUVDWXM
 mU++NNM?@ !"23C-//"3]3"2NNK=> 4/ ,F 
 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!  [        S[        UR                  5       35      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Ot[        U[        5      (       d  [        S[        U5       35      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_modulezExpected str, got call_methodr!   zExpected tuple, got r   T)r   r   opr$   strtypetargetargsr   AssertionErrorr
   r%   )r>   r?   r@   rA   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      se    HC01
!'3;;/)$.}c$B B  }d33   ??m3 x}}%)jq9I4.P.P'}}Q/H [[M))->!(//377(+=d8??>S=T)UVV-b//B
!-66 !*<< x}}%)jq9I4.P.P'}}Q/H [[M)=%0LS5G15L . .8s-K*-1R1-7$ 8 &mU;;,243F2GH   -*::$X]]+a/$!q)]1-==$x}}%)jq9I4.P.P'}}Q/H E 2H r   ))collections.abcr   typingr   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dictrG   r   r%   r   NSFusionElTypeNSFusionTyper   intr=   boolrT    r   r   <module>rj      s$   $      @ J W 8    & ii&*34D0E+E&F!11	23& 	S 	..
()	....
HIKPd5s):#;< PfOO!O 	O D		O
 
Or   