
    `i               
          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j        j        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'dS )    )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                 z   t                      }|                                 D ]}t          |          }t          dt	          |                    D ]h}t          |t	          |                    D ]H}|                    ||         ||         f           |                    ||         ||         f           Ii|S )Nr   )setvalueslistrangelenadd)r   type_a_related_to_bss_listidx_0idx_1s         p/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/torch/ao/ns/fx/pattern_utils.pyget_type_a_related_to_br      s     KN%%-4466 H Ha1c&kk** 	H 	HEuc&kk22 H H#''u(FGGG#''u(FGGGGH	H
     c            	          g } t          t                                }d}|                                D ]}t          |t                    rjt          |          dk    rWt          |d         t                    r<t          |d                   dk    r#|d         |d         d         |d         d         f}t          |t                    r|                     ||f           t          t          fD ]9}t          |t                    r|g|R }n||f}|                     ||f           :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 ]o}|                     |           |                     t          g|d         R |d         f           |                     t          g|d         R |d         f           p| 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   s}    /1G ;;T;V;VWW+0022 ? ?
 }e,,	YM""a''=+U33 (M!$%%** +1-}Q/?/BMRSDTUVDWXM
 mU++ 	ANNM+>?@@@ !"23 	? 	?C-// 3"3]33"M2NNK)<=>>>>	? 
 EM"AFAHlC	
 .")	$&9:
.")	$&9:
.")	$&9:
'2>29	-/BC
'2>29	-/BC
'2>29	-/BCO   : :q-!--qt4555)1AaD111Q489999Nr   end_nodereversed_fusiongm
seen_nodesc                 <   | }t          t          |                    D ]{}||v r dS ||         }|j        dk    rt          |t                     ot          |t
                     }|rW|j        |k    r dS t          |j                  dk    r.t          |j        d         t                    r|j        d         } dS  dS |j        dk    rt          |t
                    }|rt          |j        t                    sJ t          ||j                  }	t          |t
                    s dS t          |	|          s dS t          |j                  dk    r/t          |j        d         t                    r|j        d         }j dS  dS |j        dk    rt          |t                    ot          |          dk    }
t          |t                    }|s|
r|r|j        |k    r dS n`t          |t                    sJ |j        |d         k    r dS t          |j                  dk     r dS |j        d         |d         k    r dS t          |j                  dk    r/t          |j        d         t                    r|j        d         }t dS  dS  dS dS )	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011 > >
z!!55'
3;/))$.}c$B$B B  }d333    ?m33 55x}%%))jq9I4.P.P)'}Q/HH 55uu[M)))->> !(/377777-b(/BB
!-66 ! 55!*m<< ! 55x}%%))jq9I4.P.P)'}Q/HH 55uu[M))=%00LS5G5G15L . .8s-K-K*- 1R 1 
%-77$uu 8 &mU;;;;;-*:::$uuX]++a//$uu!q)]1-===$uux}%%))jq9I4.P.P)'}Q/HH 55uu554r   )(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 J J J J J W W W W W W 8 8 8 8 8 8                   & & & & & & i&*34D0E+E&F!11	23   & 	S 	..
()	....
HIKPd5s):#;< P P P PfKK!K 	K D		K
 
K K K K K Kr   