
    `i?                       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.d4dZ/d5dZ0d6dZ1d7dZ2ej3        	 	 	 	 	 	 	 d8d9d'            Z4d7d(Z5d:d*Z6d;d0Z7 e)d1           ej8        d2           G d3 de                                  Z9dS )<    )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_configfunctionr   returntorch.fx.Graphc                z     t          j        | d          | \  }}|j                                         |j        S )NT)
aten_graph)torchdynamoexportgrapheliminate_dead_code)r   inputsgm_s       /home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/torch/ao/quantization/quantizer/xnnpack_quantizer.py_get_dynamo_graphr,   0   s=    9KxD9996BEBH  """8O    
input_size	list[int]c                    | d         }d}t          j        ||f          }t          j        |f          }t          j        |           }dd}t          ||||f          }t          |||f          }||gS )N   c                .    t          j        | ||          S N)Flinear)actweightbiass      r+   	linear_opz'_get_linear_patterns.<locals>.linear_op=   s    xVT***r-   r4   )torchonesr,   )	r.   in_channelsout_channelsr8   r9   r7   r:   pattern_w_biaspattern_wo_biass	            r+   _get_linear_patternsrA   6   s    R.KLZ{344F:|o&&D
*Z
 
 C+ + + + 'y32EFFN'	C=AAOO,,r-   $dict[str, list[OperatorPatternType]]c                    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)conv2dr6   addadaptive_avg_pool2d)r;   nnConv2dReLUr5   relurD   Linearr6   rE   AdaptiveAvgPool2drF   copydeepcopy)supported_operatorss    r+   (_supported_symmetric_quantized_operatorsrP   E   s    
 X_ehm,X_af%Xux}%Xqv	
 HO$qxj1}X'("# 
A A  =,---r-   list[OperatorConfig]c                 2   g } t                      t          d          t          d          t          dd          fD ]Ct                      }|                     fd|                                D                        Dt	          j        |           S )NT)is_qat)is_per_channel)rT   rS   c              3  8   K   | ]}t          |          V  d S r4   )r   ).0pattern_listquantization_configs     r+   	<genexpr>z@_get_supported_symmetric_config_and_operators.<locals>.<genexpr>b   sB       .
 .
 .==.
 .
 .
 .
 .
 .
r-   )r   rP   extendvaluesrM   rN   )supported_config_and_operatorsopsrX   s     @r+   -_get_supported_symmetric_config_and_operatorsr^   Y   s    ;=")++)666)>>>)dKKK	  

 

 788&-- .
 .
 .
 .
 #

.
 .
 .
 	
 	
 	
 	
 =7888r-   F   rT   boolrS   
is_dynamicact_qminintact_qmaxweight_qminweight_qmaxc                ^   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 |||          }nt          |
|
|||          }|S )Nepsg      0?   )averaging_constantobserver)dtype	quant_min	quant_maxqschemerc   observer_or_fake_quant_ctrr   F)rn   ro   rp   rq   ch_axisrc   rr    )r   r   	with_argsr	   r   r
   r   r;   int8per_tensor_affineper_channel_symmetricper_tensor_symmetricr   r   r   r   )rT   rS   rc   rd   rf   rg   rh   
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_specrX   s                   r+   r   r   i   s    #(J ? 	K-9*%@%J#$& & &" &<Jz""-J** 	?-@**->*,j'#K#A#K $
 $
$
 $
	 	 	 (6U##5;U  	 &  E,I))	 E,D)"'J KU777%@Jz""%JJz"/j#N#D#N $
 $
$
 $

  
  
  " 
0!$"
 
 1!!$"
 
 r-   c                     t                      S r4   )r^   rt   r-   r+   #_get_supported_config_and_operatorsr      s    8:::r-   tpc                8    | 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)
    .nr   r    rb   c                    | j                             di           }g }|                                D ]A\  }}t          |t                    r|j        dz   |j        z   }|                    |           B|v S )Nnn_module_stackr   )metagetr[   
isinstancetype
__module____qualname__append)r   r   typesr*   ttp_strs        r+   module_type_filterz3_get_module_type_filter.<locals>.module_type_filter   s    
 &**%6;;#**,, 	 	DAq !T"" 8L3&7LLOOOOr-   r   r   r    rb   )r   r   )r   r   r   s     @r+   _get_module_type_filterr      s>     ]S 2?2F      r-   tp_listlist[Callable]module_name_list	list[str]Callable[[Node], bool]c                H    d | D             d |D             dfd}|S )	Nc                ,    g | ]}t          |          S rt   )r   )rV   r   s     r+   
<listcomp>z7_get_not_module_type_or_name_filter.<locals>.<listcomp>   s!    III22266IIIr-   c                ,    g | ]}t          |          S rt   r   )rV   ms     r+   r   z7_get_not_module_type_or_name_filter.<locals>.<listcomp>   s!    UUUq 7 : :UUUr-   r   r   r    rb   c                D     t           fdz   D                        S )Nc              3  .   K   | ]} |          V  d S r4   rt   )rV   fr   s     r+   rY   z^_get_not_module_type_or_name_filter.<locals>.not_module_type_or_name_filter.<locals>.<genexpr>   s+      TTqqttTTTTTTr-   )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   s1    TTTT%8;S%STTTTTTTr-   r   rt   )r   r   r   r   r   s      @@r+   #_get_not_module_type_or_name_filterr      sb     JIIIIUUDTUUUU U U U U U U *)r-   )is_backward_compatiblezXNNPACKQuantizer is deprecated! Please use xnnpack quantizer in ExecuTorch (https://github.com/pytorch/executorch/tree/main/backends/xnnpack/quantizer) instead.c                       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/d0d Z	 d/d0d!Zd.d"Zd.d#Zd1d$Ze
d2d&            Z xZS )3r   z
    !!! DEPRECATED !!!
    XNNPACKQuantizer is a marked as deprecated. 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_relur6   	conv_reluconvconv_transpose_relurF   gru_io_onlyadd_relurE   mul_relumulcatr6   r    Nonec                    t                                                       d | _        i | _        i | _        i | _        d S r4   )super__init__global_configoperator_type_configmodule_type_configmodule_name_config)self	__class__s    r+   r   zXNNPACKQuantizer.__init__  sH    ;?  	! QSKMr-   list[QuantizationConfig]c                B    d | j         D             }t          |          S )Nc                    h | ]\  }}|S rt   rt   )rV   specr*   s      r+   	<setcomp>zFXNNPACKQuantizer.get_supported_quantization_configs.<locals>.<setcomp>&  s)     /
 /
 /
T1D/
 /
 /
r-   )r\   list)cls
op_configss     r+   "get_supported_quantization_configsz3XNNPACKQuantizer.get_supported_quantization_configs$  s3    /
 /
 # B/
 /
 /

 Jr-   rX   Optional[QuantizationConfig]list[OperatorPatternType]c                    |&g }| j         D ]\  }}|                    |           |S | j         D ]\  }}||k    r|c S g S r4   )r\   rZ   )r   rX   all_opsr*   r]   configs         r+   .get_supported_operator_for_quantization_configz?XNNPACKQuantizer.get_supported_operator_for_quantization_config+  sv     &G< $ $3s####N= 	 	KFC ,,,


 -	r-   r   c                    || _         | S r4   )r   )r   rX   s     r+   
set_globalzXNNPACKQuantizer.set_global?  s    0r-   operator_typetorch._ops.OpOverloadPacketc                    || j         |<   | S r4   )r   )r   r   rX   s      r+   set_operator_typez"XNNPACKQuantizer.set_operator_typeC  s    
 4G!-0r-   module_typer   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   r   rX   s      r+   set_module_typez XNNPACKQuantizer.set_module_typeK  s     0C,r-   module_namestrc                2    |
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`
        Nz1 quantization_config == None is not supported yet)r   )r   r   rX   s      r+   set_module_namez XNNPACKQuantizer.set_module_nameU  s0     #..? /.. 0C,r-   modeltorch.fx.GraphModulec                     t          |          S )z-Transforms scalar values to tensor attributes)r   r   r   s     r+   transform_for_annotationz)XNNPACKQuantizer.transform_for_annotationb  s     )///r-   c                    | j         r'| j         j        j        r|                     |          }n|                     |          }t          |           |S )z!just handling global spec for now)r   input_activationrc   )_annotate_for_dynamic_quantization_config(_annotate_for_static_quantization_configr   r   s     r+   annotatezXNNPACKQuantizer.annotateh  s\      	I$"4"E"P 	IBB5IIEEAA%HHEU###r-   N	filter_fn Optional[Callable[[Node], bool]]c                    ||S |j         r!| j        D ]}t          |         |||           | j        D ]}t          |         |||           |S r4   )rS   STATIC_QAT_ONLY_OPSr   
STATIC_OPSr   r   rX   r   ops        r+   _annotate_all_static_patternsz.XNNPACKQuantizer._annotate_all_static_patternsr  s}     &L% 	K. K K#E+>	JJJJ/ 	G 	GBB':IFFFFr-   c                P    ||S | j         D ]}t          |         |||           |S r4   )DYNAMIC_OPSr   r   s        r+   _annotate_all_dynamic_patternsz/XNNPACKQuantizer._annotate_all_dynamic_patterns  sC     &L" 	G 	GBB':IFFFFr-   c                   t          | j                                                  }| j                                        D ])\  }}|                     ||t          |                     *t          | j                                                  }| j                                        D ])\  }}|                     ||t          |                     *|                     || j        t          ||                     |S r4   )
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 < < > >??#'#:#@#@#B#B 	 	K..v6{CC    t.335566#'#:#@#@#B#B 	 	K..v6{CC    	**/9IJJ	
 	
 	

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

 r-   c                    d S r4   rt   r   s     r+   validatezXNNPACKQuantizer.validate  s    r-   rQ   c                    | j         S r4   )r\   )r   s    r+   get_supported_operatorsz(XNNPACKQuantizer.get_supported_operators  s    11r-   )r    r   )r    r   )rX   r   r    r   )rX   r   r    r   )r   r   rX   r   r    r   )r   r   rX   r   )r   r   rX   r   )r   r   r    r   r4   )r   r   rX   r   r   r   r    r   )r   r   r    r   r    rQ   )__name__r   r   __doc__r   r\   r   r   r   r   classmethodr   r   r   r   r   r   r   r   r   r   r   r   r   r   __classcell__)r   s   @r+   r   r      s         &I%H%J%J"    J" 	KN N N N N N       [     [&            0 0 0 0    7;	    * 7;	       ,   ,    2 2 2 [2 2 2 2 2r-   )r   r   r    r!   )r.   r/   )r    rB   r   )FFFr_   r`   ra   r`   )rT   rb   rS   rb   rc   rb   rd   re   rf   re   rg   re   rh   re   )r   r   )r   r   r   r   r    r   ):
__future__r   rM   	functoolstyping_extensionstypingr   r   r   r   r;   torch._dynamo_dynamor$   torch.nn.functionalrG   
functionalr5   #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,   rA   rP   r^   	lru_cacher   r   r   r   
deprecatedr   rt   r-   r+   <module>r     s$   " " " " " "          9 9 9 9 9 9 9 9 9 9 9 9  # # # # # #                                H G G G G G G G I I I I I I                2 1 1 1 1 1  PPPPPP '   - - - -. . . .(9 9 9 9   R R R R Rj; ; ; ;   B	* 	* 	* 	* e,,,g M2 M2 M2 M2 M2y M2 M2	  -,
M2 M2 M2r-   