
    Vh!?                       d dl mZ d dlZd dlZd dlZd dlmZ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mZmZmZmZmZ d dlmZmZ d dlm Z  d dl!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z' d d	l(m)Z) e	rd d
l*m+Z+ d dl,m-Z- ddgZ.ddZ/ddZ0ddZ1ddZ2ejf                  	 	 	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 dd       Z4ddZ5ddZ6	 	 	 	 	 	 d dZ7 e)d       G d de             Z8y)!    )annotationsN)AnyCallableOptionalTYPE_CHECKING)FakeQuantizeFusedMovingAvgObsFakeQuantize)HistogramObserverMinMaxObserverMovingAverageMinMaxObserver%MovingAveragePerChannelMinMaxObserverPerChannelMinMaxObserverPlaceholderObserver)QuantizationSpec	Quantizer)_get_module_name_filter)_convert_scalars_to_attrsOP_TO_ANNOTATOROperatorConfigOperatorPatternTypepropagate_annotationQuantizationConfig)compatibility)"_ObserverOrFakeQuantizeConstructor)NodeXNNPACKQuantizer!get_symmetric_quantization_configc                     t        j                  | d      | \  }}|j                  j                          |j                  S )NT)
aten_graph)torchdynamoexportgrapheliminate_dead_code)functioninputsgm_s       a/home/dcms/DCMS/lib/python3.12/site-packages/torch/ao/quantization/quantizer/xnnpack_quantizer.py_get_dynamo_graphr)   0   s8    9KxD96BEBHH  "88O    c                    | d   }d}t        j                  ||f      }t        j                  |f      }t        j                  |       }dd}t        ||||f      }t        |||f      }||gS )N   c                0    t        j                  | ||      S N)Flinear)actweightbiass      r(   	linear_opz'_get_linear_patterns.<locals>.linear_op=   s    xxVT**r*   r/   )torchonesr)   )	
input_sizein_channelsout_channelsr3   r4   r2   r5   pattern_w_biaspattern_wo_biass	            r(   _get_linear_patternsr=   6   sv    R.KLZZ{34F::|o&D
**Z
 C+ 'y32EFN'	C=AOO,,r*   c                 J   t         j                  j                  t         j                  j                  gt         j                  j                  t        j
                  gt        j                  t         j                  j                  gt        j                  t        j
                  ggt         j                  j                  gt        j                  ggt         j                  ggt         j                  j                  gt        j                  ggd} t        j                  |       S )N)conv2dr1   addadaptive_avg_pool2d)r6   nnConv2dReLUr0   relur?   Linearr1   r@   AdaptiveAvgPool2drA   copydeepcopy)supported_operatorss    r(   (_supported_symmetric_quantized_operatorsrK   E   s    
 XX__ehhmm,XX__aff%XXuxx}}%XXqvv	
 HHOO$qxxj1}XX''(""# 
A  ==,--r*   c                     g } t               t        d      t        d      t        dd      fD ]4  t               }| j                  fd|j                         D               6 t	        j
                  |       S )NT)is_qat)is_per_channel)rN   rM   c              3  6   K   | ]  }t        |        y wr/   )r   ).0pattern_listquantization_configs     r(   	<genexpr>z@_get_supported_symmetric_config_and_operators.<locals>.<genexpr>b   s!      .
 .=.
s   )r   rK   extendvaluesrH   rI   )supported_config_and_operatorsopsrR   s     @r(   -_get_supported_symmetric_config_and_operatorsrX   Y   sx    ;=")+)6)>)dK	  

 78&-- .
 #

.
 	


 ==788r*   Fc                r   ddi}|r+|r"t         }t        j                  d      }	|	|d<   nt        }n|rt        }nt
        }t        t        j                  ||t        j                  | |j                  d
i |      }
| rt        j                  nt        j                  }t        }|rt        }n| rt        }ddi}|r&|t        j                  k(  r
t        |d<   n	t        |d<   t        t        j                  |||dd |j                  d
i |	      }d }|rt        |
d |||      }|S t        |
|
|||      }|S )Nepsg      0?   )averaging_constantobserver)dtype	quant_min	quant_maxqscheme
is_dynamicobserver_or_fake_quant_ctrr   F)r^   r_   r`   ra   ch_axisrb   rc    )r   r   	with_argsr	   r   r
   r   r6   int8per_tensor_affineper_channel_symmetricper_tensor_symmetricr   r   r   r   )rN   rM   rb   act_qminact_qmaxweight_qminweight_qmax
extra_argsact_observer_or_fake_quant_ctrdynamic_quant_observeract_quantization_specweight_qscheme!weight_observer_or_fake_quant_ctrweight_quantization_specbias_quantization_specrR   s                   r(   r   r   i   s|    #(J-9*%@%J%J#$&" &<Jz"-J*-@*->*,jj''#K#A#K#K $
$
	 (6##5;U;U  	 & ,I)	,D)"'JU777%@Jz"%JJz"/jj#N#D#N#N $
$

  "0!$"
  1!!$"
 r*   c                     t               S r/   )rX   re   r*   r(   #_get_supported_config_and_operatorsrx      s    8::r*   c                L    | j                   dz   | j                  z   dfd}|S )a  Get the module_type_filter function for a given module type, the filter accepts
    a node and checks if the node comes from a module that has certain module type

    For example:
        node: linear_op = call_function[...](...)  # comes from a module with type Block -> Sub -> Linear


    >> module_type_filter = _get_module_type_filter(Sub)  # submodule with type `Sub`, under the `Block` submodule
    >> print(module_type_filter(node))
    True  # the node is from the submodule `Sub` (same for `Block` and `Linear` as well)
    .c                    | j                   j                  di       }g }|j                         D ]B  \  }}t        |t              r|j
                  dz   |j                  z   }|j                  |       D |v S )Nnn_module_stackrz   )metagetrU   
isinstancetype
__module____qualname__append)nr|   typesr'   ttp_strs        r(   module_type_filterz3_get_module_type_filter.<locals>.module_type_filter   su    
 &&**%6;#**, 	DAq !T"LL3&7LLO	 r*   r   r   returnbool)r   r   )tpr   r   s     @r(   _get_module_type_filterr      s)     ]]S 2??2F r*   c                    | D cg c]  }t        |       c}|D cg c]  }t        |       c}dfd}|S c c}w c c}w )Nc                6     t         fdz   D               S )Nc              3  .   K   | ]  } |        y wr/   re   )rP   fr   s     r(   rS   z^_get_not_module_type_or_name_filter.<locals>.not_module_type_or_name_filter.<locals>.<genexpr>   s     TqtTs   )any)r   module_name_list_filtersmodule_type_filterss   `r(   not_module_type_or_name_filterzK_get_not_module_type_or_name_filter.<locals>.not_module_type_or_name_filter   s    T%8;S%STTTTr*   r   )r   r   )tp_listmodule_name_listr   mr   r   r   s        @@r(   #_get_not_module_type_or_name_filterr      sL     BII2226IDTUq 7 :UU *) JUs	   >A)is_backward_compatiblec                  6    e Zd ZdZ e       Zg dZg dZdgZd fdZ	e
dd       Ze
	 	 	 	 dd       ZddZ	 	 	 	 	 	 dd	Z	 	 	 	 dd
Z	 	 	 	 ddZ	 	 	 	 ddZddZ	 d	 	 	 	 	 	 	 ddZ	 d	 	 	 	 	 	 	 ddZ	 	 	 	 ddZ	 	 	 	 ddZddZe
dd       Z xZS ) r   z
    !!! DEPRECATED !!!
    XNNPACKQuantizer is a marked as deprected. It will be removed in the future.
    It has been moved to executorch.backends.xnnpack.quantizer.xnnpack_quantizer.XNNPACKQuantizer.
    Please use the new quantizer instead.
    )conv_bn_reluconv_bnconv_transpose_bn_reluconv_transpose_bn)linear_relur1   	conv_reluconvconv_transpose_relurA   gru_io_onlyadd_relur@   mul_relumulcatr1   c                    t         |           t        j                  | j                  j
                   d       d | _        i | _        i | _        i | _	        y )Nz is deprecated!)
super__init__warningswarn	__class____name__global_configoperator_type_configmodule_type_configmodule_name_config)selfr   s    r(   r   zXNNPACKQuantizer.__init__  sU    001AB;?  	! QSKMr*   c                `    | j                   D ch c]  \  }}|	 }}}t        |      S c c}}w r/   )rV   list)clsspecr'   
op_configss       r(   "get_supported_quantization_configsz3XNNPACKQuantizer.get_supported_quantization_configs!  s:     !$ B B/
T1D/

 /
 J/
s   *c                    |)g }| j                   D ]  \  }}|j                  |        |S | j                   D ]  \  }}||k(  s|c S  g S r/   )rV   rT   )r   rR   all_opsr'   rW   configs         r(   .get_supported_operator_for_quantization_configz?XNNPACKQuantizer.get_supported_operator_for_quantization_config(  si     &G<< $3s#$N== 	KFC ,,
	 	r*   c                    || _         | S r/   )r   )r   rR   s     r(   
set_globalzXNNPACKQuantizer.set_global<  s    0r*   c                $    || j                   |<   | S r/   )r   )r   operator_typerR   s      r(   set_operator_typez"XNNPACKQuantizer.set_operator_type@  s    
 4G!!-0r*   c                $    || j                   |<   | S )a5  Set quantization_config for a submodule with type: `module_type`, for example:
        quantizer.set_module_name(Sub) or quantizer.set_module_name(nn.Linear), it will quantize all supported operator/operator
        patterns in the submodule with this module type with the given `quantization_config`
        )r   )r   module_typerR   s      r(   set_module_typez XNNPACKQuantizer.set_module_typeH  s     0C,r*   c                6    |J d       || j                   |<   | S )a  Set quantization_config for a submodule with name: `module_name`, for example:
        quantizer.set_module_name("blocks.sub"), it will quantize all supported operator/operator
        patterns in the submodule with this module name with the given `quantization_config`
        z1 quantization_config == None is not supported yet)r   )r   module_namerR   s      r(   set_module_namez XNNPACKQuantizer.set_module_nameR  s/      +	?>	?+/B,r*   c                    t        |      S )z-Transforms scalar values to tensor attributes)r   r   models     r(   transform_for_annotationz)XNNPACKQuantizer.transform_for_annotation_  s     )//r*   c                    | j                   r2| j                   j                  j                  r| j                  |      }n| j	                  |      }t        |       |S )z!just handling global spec for now)r   input_activationrb   )_annotate_for_dynamic_quantization_config(_annotate_for_static_quantization_configr   r   s     r(   annotatezXNNPACKQuantizer.annotatee  sN     $"4"4"E"E"P"PBB5IEAA%HEU#r*   c                    ||S |j                   r!| j                  D ]  }t        |   |||        | j                  D ]  }t        |   |||        |S r/   )rM   STATIC_QAT_ONLY_OPSr   
STATIC_OPSr   r   rR   	filter_fnops        r(   _annotate_all_static_patternsz.XNNPACKQuantizer._annotate_all_static_patternso  sm     &L%%.. K#E+>	JK// 	GBB':IF	Gr*   c                P    ||S | j                   D ]  }t        |   |||        |S r/   )DYNAMIC_OPSr   r   s        r(   _annotate_all_dynamic_patternsz/XNNPACKQuantizer._annotate_all_dynamic_patterns  s=     &L"" 	GBB':IF	Gr*   c                   t        | j                  j                               }| j                  j                         D ]!  \  }}| j	                  ||t        |             # t        | j                  j                               }| j                  j                         D ]!  \  }}| j	                  ||t        |             # | j	                  || j                  t        ||             |S r/   )
r   r   keysitemsr   r   r   r   r   r   r   r   r   r   r   r   r   s          r(   r   z9XNNPACKQuantizer._annotate_for_static_quantization_config  s       7 7 < < >?#'#:#:#@#@#B 	K..v6{C	
 t..3356#'#:#:#@#@#B 	K..v6{C	
 	**/9IJ	

 r*   c                   t        | j                  j                               }| j                  j                         D ]!  \  }}| j	                  ||t        |             # t        | j                  j                               }| j                  j                         D ]!  \  }}| j	                  ||t        |             # | j	                  || j                  t        ||             |S r/   )
r   r   r   r   r   r   r   r   r   r   r   s          r(   r   z:XNNPACKQuantizer._annotate_for_dynamic_quantization_config  s       7 7 < < >?#'#:#:#@#@#B 	K//v6{C	
 t..3356#'#:#:#@#@#B 	K//v6{C	
 	++/9IJ	

 r*   c                     y r/   re   r   s     r(   validatezXNNPACKQuantizer.validate  s    r*   c                    | j                   S r/   )rV   )r   s    r(   get_supported_operatorsz(XNNPACKQuantizer.get_supported_operators  s    111r*   )r   None)r   zlist[QuantizationConfig])rR   Optional[QuantizationConfig]r   zlist[OperatorPatternType])rR   r   r   r   )r   ztorch._ops.OpOverloadPacketrR   r   r   r   )r   r   rR   r   )r   strrR   r   )r   torch.fx.GraphModuler   r   r/   )r   r   rR   r   r   z Optional[Callable[[Node], bool]]r   r   )r   r   r   r   r   zlist[OperatorConfig])r   r   r   __doc__rx   rV   r   r   r   r   classmethodr   r   r   r   r   r   r   r   r   r   r   r   r   r   __classcell__)r   s   @r(   r   r      sz    &I%J"J" 	KN     ">	" &2 0 
	#:L5Q0)0	0 7;	# : 4	
 
* 7;	# : 4	
 
)	,)	, 2 2r*   )r$   r   r   ztorch.fx.Graph)r8   z	list[int])r   z$dict[str, list[OperatorPatternType]]r   )FFFi   ir   )rN   r   rM   r   rb   r   rk   intrl   r   rm   r   rn   r   )r   r   )r   zlist[Callable]r   z	list[str]r   zCallable[[Node], bool])9
__future__r   rH   	functoolsr   typingr   r   r   r   r6   torch._dynamo_dynamor    torch.nn.functionalrB   
functionalr0   #torch.ao.quantization.fake_quantizer   r	   torch.ao.quantization.observerr
   r   r   r   r   r   torch.ao.quantization.quantizerr   r   %torch.ao.quantization.quantizer.utilsr   7torch.ao.quantization.quantizer.xnnpack_quantizer_utilsr   r   r   r   r   r   torch.fx._compatibilityr   torch.ao.quantization.qconfigr   torch.fxr   __all__r)   r=   rK   rX   	lru_cacher   rx   r   r   r   re   r*   r(   <module>r     s>   "    9 9  #    H I  2 P '-.(9   RRR R 	R
 R R R Rj;B	*	*/8	*	* e,N2y N2 -N2r*   