
    Vh               
       p   d dl mZmZmZ d dlZd dlmZ d dlmc mZ	 d dl
mZmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d	d
lmZ ej2                  j4                  Zdeeee   f   deeeef      fdZ eeeeeef   f   Z!eee!e!f   ee!e!e!e!f   f   Z"de#ee"e$f      fdZ%dede"dedee   de&f
dZ'y)    )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                 &   t               }| j                         D ]s  }t        |      }t        dt	        |            D ]N  }t        |t	        |            D ]4  }|j                  ||   ||   f       |j                  ||   ||   f       6 P u |S )Nr   )setvalueslistrangelenadd)r   type_a_related_to_bss_listidx_0idx_1s         L/home/dcms/DCMS/lib/python3.12/site-packages/torch/ao/ns/fx/pattern_utils.pyget_type_a_related_to_br      s     KN%-446 Ha1c&k* 	HEuc&k2 H#''u(FG#''u(FGH	HH     c            	         g } t        t                     }d}|j                         D ]  }t        |t              rFt        |      dk(  r8t        |d   t              r%t        |d         dk(  r|d   |d   d   |d   d   f}t        |t              r| j                  ||f       t        t        fD ]0  }t        |t              r|g|}n||f}| j                  ||f       2  d}dt        j                  ft        j                  t        j                  df|ft        j                  t        j                   f|ft        j"                  t        j$                  f|ft        j&                  t        j(                  f|ft        j*                  t        j                  t        j                   f|ft        j*                  t        j"                  t        j$                  f|ft        j*                  t        j&                  t        j(                  f|fg}|D ]U  }| j                  |       | j                  t        g|d   |d   f       | j                  t        g|d   |d   f       W | S )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   s9    /1G ;;T;VW+002 ?
 }e,M"a'=+U3M!$%* +1-}Q/?/BMRSDTUVDWXM
 mU+NNM+>?@ !"23 	?C-/"3]3"M2NNK)<=>	?/?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                    | }t        t        |            D ]Q  }||v r y||   }|j                  dk(  rt        |t               xr t        |t
               }|rX|j                  |k7  r yt        |j                        dkD  r-t        |j                  d   t              r|j                  d   } y y|j                  dk(  rt        |t
              }|rt        |j                  t              sJ t        ||j                        }	t        |t
              s yt        |	|      s yt        |j                        dkD  r.t        |j                  d   t              r|j                  d   }W y y|j                  dk(  rt        |t              xr t        |      dk(  }
t        |t              }|s|
r|r|j                  |k7  rY yt        |t              sJ |j                  |d   k7  r yt        |j                        dk  r y|j                  d   |d   k7  r yt        |j                        dkD  r.t        |j                  d   t              r|j                  d   }N y y y y)	zZ
    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      s&    HC01 >
z!'
3;;/)$.}c$B B  }d33   ??m3 x}}%)jq9I4.P'}}Q/H [[M))->!(//3777-b(//B
!-6 !*m< x}}%)jq9I4.P'}}Q/H [[M)=%0LS5G15L . .8s-K*-1R1-7$%mU;;;-*::$X]]+a/$!q)]1-==$x}}%)jq9I4.P'}}Q/H }>@ 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   