o
    Zh                   
   @   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   Ztype_a_related_to_bsZs_listZidx_0Zidx_1 r   K/var/www/auris/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   toZ
dequantize)r   r   keys
isinstancetupler   appendr   r   torchZfloat16FZreluZlinearnnZBatchNorm1dZConv1dZBatchNorm2dZConv2dZBatchNorm3dZConv3dZReLU)	resultsZall_quant_patternsZdefault_base_op_idxZquant_patternclsZnew_patternZfp16_em_base_op_idxZ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.
    FZcall_functionr   Zcall_moduleZcall_methodr   r   T)r   r   opr   strtypetargetargsr   r	   r   )r'   r(   r)   r*   Zcur_nodeZ
fusion_idxZcur_fusion_elZfusion_el_is_funZfusion_el_is_modZ
target_modZ!fusion_el_is_meth_with_second_argZfusion_el_is_meth_without_argsr   r   r    end_node_matches_reversed_fusion   sf   










r0   )(typingr   r   r   r    Ztorch.nnr"   Ztorch.nn.functionalZ
functionalr!   Ztorch.ao.quantizationr   r   Z$torch.ao.quantization.backend_configr   Z)torch.ao.quantization.fx.quantize_handlerr   Ztorch.ao.quantization.utilsr	   Ztorch.fxr
   Ztorch.fx.graphr   Zns_typesr   opsZ	quantizedZtoqdictr,   r   r   r   ZNSFusionElTypeZNSFusionTyper   intr&   boolr0   r   r   r   r   <module>   sT    

S