
    Pi?                       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 d dlmZmZmZmZmZmZmZmZ d dlmZmZm Z  d dl!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z' erd dl(m)Z) d d	lm*Z* d
dgZ+d1dZ,d2dZ-d3dZ.d4dZ/ej0        	 	 	 	 	 	 	 d5d6d%            Z1d4d&Z2d7d(Z3d8d.Z4 ed/           G d0 d
e                      Z5dS )9    )annotationsN)TYPE_CHECKINGAnyCallableOptional)compatibility)FakeQuantizeFusedMovingAvgObsFakeQuantizeHistogramObserverMinMaxObserverMovingAverageMinMaxObserver%MovingAveragePerChannelMinMaxObserverPerChannelMinMaxObserverPlaceholderObserver)QuantizationSpec	Quantizerget_module_name_filter)OP_TO_ANNOTATOROperatorConfigOperatorPatternTypeQuantizationConfig_convert_scalars_to_attrspropagate_annotation)Node)"_ObserverOrFakeQuantizeConstructor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/torchao/testing/pt2e/_xnnpack_quantizer.py_get_dynamo_graphr+   8   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_opE   s    xVT***r,   r3   )torchonesr+   )	r-   in_channelsout_channelsr7   r8   r6   r9   pattern_w_biaspattern_wo_biass	            r*   _get_linear_patternsr@   >   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)conv2dr5   addadaptive_avg_pool2d)r:   nnConv2dReLUr4   relurC   Linearr5   rD   AdaptiveAvgPool2drE   copydeepcopy)supported_operatorss    r*   (_supported_symmetric_quantized_operatorsrO   M   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)rS   rR   c              3  8   K   | ]}t          |          V  d S r3   )r   ).0pattern_listquantization_configs     r*   	<genexpr>z@_get_supported_symmetric_config_and_operators.<locals>.<genexpr>j   sB       .
 .
 .==.
 .
 .
 .
 .
 .
r,   )r   rO   extendvaluesrL   rM   )supported_config_and_operatorsopsrW   s     @r*   -_get_supported_symmetric_config_and_operatorsr]   a   s    ;=")++)666)>>>)dKKK	  

 

 788&-- .
 .
 .
 .
 #

.
 .
 .
 	
 	
 	
 	
 =7888r,   F   rS   boolrR   
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qschemerb   observer_or_fake_quant_ctrr   F)rm   rn   ro   rp   ch_axisrb   rq    )r	   r   	with_argsr
   r   r   r   r:   int8per_tensor_affineper_channel_symmetricper_tensor_symmetricr   r   r   r   )rS   rR   rb   rc   re   rf   rg   
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_specrW   s                   r*   r   r   q   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 r3   )r]   rs   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   ra   c                    | j                             di           }g }|                                D ]A\  }}t          |t                    r|j        dz   |j        z   }|                    |           B|v S )Nnn_module_stackr   )metagetrZ   
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   ra   )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 rs   )r   )rU   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 rs   )r   )rU   ms     r*   r   z7_get_not_module_type_or_name_filter.<locals>.<listcomp>   s!    TTTa 6q 9 9TTTr,   r   r   r   ra   c                D     t           fdz   D                        S )Nc              3  .   K   | ]} |          V  d S r3   rs   )rU   fr   s     r*   rX   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   rs   )r   r   r   r   r   s      @@r*   #_get_not_module_type_or_name_filterr      sb     JIIIITTCSTTTU U U U U U U *)r,   )is_backward_compatiblec                       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 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_relur5   	conv_reluconvconv_transpose_relurE   gru_io_onlyadd_relurD   mul_relumulcatr5   r   Nonec                    t                                                       t          j        | j        j         d           d | _        i | _        i | _        i | _	        d S )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__  sf    0AAABBB;?  	! QSKMr,   list[QuantizationConfig]c                B    d | j         D             }t          |          S )Nc                    h | ]\  }}|S rs   rs   )rU   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,   rW   Optional[QuantizationConfig]list[OperatorPatternType]c                    |&g }| j         D ]\  }}|                    |           |S | j         D ]\  }}||k    r|c S g S r3   )r[   rY   )r   rW   all_opsr)   r\   configs         r*   .get_supported_operator_for_quantization_configz?XNNPACKQuantizer.get_supported_operator_for_quantization_config0  sv     &G< $ $3s####N= 	 	KFC ,,,


 -	r,   r   c                    || _         | S r3   )r   )r   rW   s     r*   
set_globalzXNNPACKQuantizer.set_globalD  s    0r,   operator_typetorch._ops.OpOverloadPacketc                    || j         |<   | S r3   )r   )r   r   rW   s      r*   set_operator_typez"XNNPACKQuantizer.set_operator_typeH  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   rW   s      r*   set_module_typez XNNPACKQuantizer.set_module_typeP  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   rW   s      r*   set_module_namez XNNPACKQuantizer.set_module_nameZ  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_annotationg  s     )///r,   c                    | j         r'| j         j        j        r|                     |          }n|                     |          }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.annotatem  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 r3   )rR   STATIC_QAT_ONLY_OPSr   
STATIC_OPSr   r   rW   r   ops        r*   _annotate_all_static_patternsz.XNNPACKQuantizer._annotate_all_static_patternsw  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 r3   )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 r3   )
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5kBB    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 r3   )
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5kBB    t.335566#'#:#@#@#B#B 	 	K//v6{CC    	++/9IJJ	
 	
 	

 r,   c                    d S r3   rs   r   s     r*   validatezXNNPACKQuantizer.validate  s    r,   rP   c                    | j         S r3   )r[   )r   s    r*   get_supported_operatorsz(XNNPACKQuantizer.get_supported_operators  s    11r,   )r   r   )r   r   )rW   r   r   r   )rW   r   r   r   )r   r   rW   r   r   r   )r   r   rW   r   )r   r   rW   r   )r   r   r   r   r3   )r   r   rW   r   r   r   r   r   )r   r   r   r   r   rP   )r   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   rA   r   )FFFr^   r_   r`   r_   )rS   ra   rR   ra   rb   ra   rc   rd   re   rd   rf   rd   rg   rd   )r   r   )r   r   r   r   r   r   )6
__future__r   rL   	functoolsr   typingr   r   r   r   r:   torch._dynamo_dynamor#   torch.nn.functionalrF   
functionalr4   torch.fx._compatibilityr   torchao.quantization.pt2er	   r
   r   r   r   r   r   r   #torchao.quantization.pt2e.quantizerr   r   r   -torchao.testing.pt2e._xnnpack_quantizer_utilsr   r   r   r   r   r   torch.fxr   r   __all__r+   r@   rO   r]   	lru_cacher   r   r   r   r   rs   r,   r*   <module>r     s   # " " " " "       9 9 9 9 9 9 9 9 9 9 9 9  # # # # # #          1 1 1 1 1 1	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	         
                 MLLLLLL '   - - - -. . . .(9 9 9 9   R R R R Rj; ; ; ;   B	* 	* 	* 	* e,,,N2 N2 N2 N2 N2y N2 N2 -,N2 N2 N2r,   