o
    Ih                   
   @   s:  d dl mZmZmZ d dlZd dlmZ d dlm  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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dZ%dede"dedee de&f
d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                 C   st   t  }|  D ]0}t|}tdt|D ]"}t|t|D ]}||| || f ||| || f qqq|S )Nr   )setvalueslistrangelenadd)r   type_a_related_to_bss_listidx_0idx_1 r   P/var/www/vscode/kcb/lib/python3.10/site-packages/torch/ao/ns/fx/pattern_utils.pyget_type_a_related_to_b   s   r   c            	   	   C   s  g } t t }d}| D ]U}t|tr8t|dkr8t|d tr8t|d dkr8|d |d d |d d f}t|trD| ||f ttfD ]}t|trV|g|R }n||f}| ||f qHq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 ]%}| | | tg|d R |d f | tg|d R |d f q| 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pr   r   r   get_reversed_fusions3   sF   



 r;   end_nodereversed_fusiongm
seen_nodesc                 C   s  | }t t|D ]}||v r dS || }|jdkrKt|t o%t|t }|rH|j|kr0 dS t|jdkrEt|jd trE|jd }q dS  dS |jdkrt|t}|rt|jts_J t	||j}	t|tsm dS t|	|su dS t|jdkrt|jd tr|jd }q dS  dS |jdkrt|t
ot|dk}
t|t}|s|
r|r|j|kr dS n't|t
sJ |j|d kr dS t|jdk r dS |jd |d kr dS t|jdkrt|jd tr|jd }q 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=   r>   r?   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_argsr   r   r    end_node_matches_reversed_fusion   sf   










rP   )(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dictrD   r   r#   r   NSFusionElTypeNSFusionTyper   intr;   boolrP   r   r   r   r   <module>   sT    

S