
    sh!?                       S SK Jr  S SKrS SKrS SKrS SKJrJrJrJ	r	  S SK
r
S SKJr  S SKJs  Jr  S SKJrJr  S SKJrJrJrJrJrJr  S SKJrJr  S SKJ r   S SK!J"r"J#r#J$r$J%r%J&r&J'r'  S S	K(J)r)  \	(       a  S S
K*J+r+  S SK,J-r-  SS/r.SS jr/SS jr0SS jr1SS jr2\Rf                         S             SS jj5       r4SS jr5SS jr6      S S jr7\)" SS9 " S S\5      5       r8g)!    )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                    [         R                  " U SS9" U6 u  p#UR                  R                  5         UR                  $ )NT)
aten_graph)torchdynamoexportgrapheliminate_dead_code)functioninputsgm_s       څ/Users/tiagomarins/Projetos/claudeai/copy_bank/venv/lib/python3.13/site-packages/torch/ao/quantization/quantizer/xnnpack_quantizer.py_get_dynamo_graphr)   0   s4    xD96BEBHH  "88O    c                    U S   nSn[         R                  " X!45      n[         R                  " U45      n[         R                  " U 5      nSS jn[        XeX445      n[        XeU45      nXx/$ )N   c                0    [         R                  " XU5      $ N)Flinear)actweightbiass      r(   	linear_op'_get_linear_patterns.<locals>.linear_op=   s    xxT**r*   r/   )torchonesr)   )	
input_sizein_channelsout_channelsr3   r4   r2   r5   pattern_w_biaspattern_wo_biass	            r(   _get_linear_patternsr>   6   sl    R.KLZZ34F::|o&D
**Z
 C+ 'y2EFN'	=AO,,r*   c                 L   [         R                  R                  [         R                  R                  /[         R                  R                  [        R
                  /[        R                  [         R                  R                  /[        R                  [        R
                  //[         R                  R                  /[        R                  //[         R                  //[         R                  R                  /[        R                  //S.n [        R                  " U 5      $ )N)conv2dr1   addadaptive_avg_pool2d)r7   nnConv2dReLUr0   relur@   Linearr1   rA   AdaptiveAvgPool2drB   copydeepcopy)supported_operatorss    r(   (_supported_symmetric_quantized_operatorsrL   E   s    
 XX__ehhmm,XX__aff%XXuxx}}%XXqvv	
 HHOO$qxxj1}XX''(""# 
A  ==,--r*   c                    ^ / n [        5       [        SS9[        SS9[        SSS94 H6  m[        5       nU R                  U4S jUR                  5        5       5        M8     [        R
                  " U 5      $ )NT)is_qat)is_per_channel)rO   rN   c              3  <   >#    U  H  n[        TU5      v   M     g 7fr/   )r   ).0pattern_listquantization_configs     r(   	<genexpr>@_get_supported_symmetric_config_and_operators.<locals>.<genexpr>b   s#      .
 , .== ,s   )r   rL   extendvaluesrI   rJ   )supported_config_and_operatorsopsrS   s     @r(   -_get_supported_symmetric_config_and_operatorsrZ   Y   ss    ;=")+)6)>)dK	  78&-- .
 #

.
 	
  ==788r*   Fc                   SS0nU(       a-  U(       a  [         n[        R                  " SS9n	XS'   O[        nOU(       a  [        nO[
        n[        [        R                  UU[        R                  UUR                  " S
0 UD6S9n
U (       a  [        R                  O[        R                  n[        nU(       a  [        nOU (       a  [        nSS0nU(       a'  U[        R                  :X  a
  [        US'   O	[        US'   [        [        R                  UUUSSUR                  " S
0 UD6S	9nS nU(       a  [        U
S UUU5      nU$ [        U
U
UUU5      nU$ )Nepsg      0?   )averaging_constantobserver)dtype	quant_min	quant_maxqscheme
is_dynamicobserver_or_fake_quant_ctrr   F)r`   ra   rb   rc   ch_axisrd   re    )r   r   	with_argsr	   r   r
   r   r7   int8per_tensor_affineper_channel_symmetricper_tensor_symmetricr   r   r   r   )rO   rN   rd   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_specrS   s                   r(   r   r   i   st    #(J-9*%@%J%J#$&" &<z"-J*-@*->*,jj''#A#K#K $
$
	 (6##5;U;U  	 & ,I)	,D)"'JU777%@Jz"%JJz"/jj#D#N#N $
$

  "0!$"
  1!!$"
 r*   c                     [        5       $ r/   )rZ   rg   r*   r(   #_get_supported_config_and_operatorsrz      s    8::r*   c                P   ^ U R                   S-   U R                  -   mSU4S jjnU$ )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                   > U R                   R                  S0 5      n/ nUR                  5        HG  u  p4[        U[        5      (       a  UR
                  S-   UR                  -   nUR                  U5        MI     TU;   $ )Nnn_module_stackr|   )metagetrW   
isinstancetype
__module____qualname__append)nr~   typesr'   ttp_strs        r(   module_type_filter3_get_module_type_filter.<locals>.module_type_filter   sq    
 &&**%6;#**,DA !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                   ^^ U  Vs/ s H  n[        U5      PM     snmU Vs/ s H  n[        U5      PM     snmSUU4S jjnU$ s  snf s  snf )Nc                @   >^  [        U 4S jTT-    5       5      (       + $ )Nc              3  2   >#    U  H  o" T5      v   M     g 7fr/   rg   )rQ   fr   s     r(   rT   ^_get_not_module_type_or_name_filter.<locals>.not_module_type_or_name_filter.<locals>.<genexpr>   s     T%Sqtt%Ss   )any)r   module_name_list_filtersmodule_type_filterss   `r(   not_module_type_or_name_filterK_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      s\     BII2226IDTUDTq 7 :DTUU U *) JUs
   AA)is_backward_compatiblec                  b  ^  \ rS rSrSr\" 5       r/ SQr/ SQrS/r	SU 4S jjr
\SS j5       r\    SS j5       rSS	 jr      SS
 jr    SS jr    SS jr    SS jrSS jr S       SS jjr S       SS jjr    SS jr    SS jrS S jr\S!S j5       rSrU =r$ )"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_relurB   gru_io_onlyadd_relurA   mul_relumulcatr1   c                   > [         TU ]  5         [        R                  " U R                  R
                   S35        S U l        0 U l        0 U l        0 U l	        g )Nz is deprecated!)
super__init__warningswarn	__class____name__global_configoperator_type_configmodule_type_configmodule_name_config)selfr   s    r(   r   XNNPACKQuantizer.__init__  sU    001AB;?  	! QSKMr*   c                d    U R                    VVs1 s H  u  pUiM	     nnn[        U5      $ s  snnf r/   )rX   list)clsspecr'   
op_configss       r(   "get_supported_quantization_configs3XNNPACKQuantizer.get_supported_quantization_configs!  s;     !$ B B/
 BWTD B 	 /
 J/
s   ,c                    Uc*  / nU R                    H  u  p4UR                  U5        M     U$ U R                    H  u  pTXQ:X  d  M  Us  $    / $ r/   )rX   rV   )r   rS   all_opsr'   rY   configs         r(   .get_supported_operator_for_quantization_config?XNNPACKQuantizer.get_supported_operator_for_quantization_config(  sX     &G<<s# =N==KF ,
 > 	r*   c                    Xl         U $ r/   )r   )r   rS   s     r(   
set_globalXNNPACKQuantizer.set_global<  s    0r*   c                "    X R                   U'   U $ r/   )r   )r   operator_typerS   s      r(   set_operator_type"XNNPACKQuantizer.set_operator_type@  s    
 4G!!-0r*   c                "    X R                   U'   U $ )a  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_typerS   s      r(   set_module_type XNNPACKQuantizer.set_module_typeH  s     0C,r*   c                6    Uc   S5       eX R                   U'   U $ )zSet 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_namerS   s      r(   set_module_name XNNPACKQuantizer.set_module_nameR  s-      +	?>	?+/B,r*   c                    [        U5      $ )z-Transforms scalar values to tensor attributes)r   r   models     r(   transform_for_annotation)XNNPACKQuantizer.transform_for_annotation_  s     )//r*   c                    U R                   (       a7  U R                   R                  R                  (       a  U R                  U5      nOU R	                  U5      n[        U5        U$ )z!just handling global spec for now)r   input_activationrd   )_annotate_for_dynamic_quantization_config(_annotate_for_static_quantization_configr   r   s     r(   annotateXNNPACKQuantizer.annotatee  sN     $"4"4"E"E"P"PBB5IEAA%HEU#r*   c                    Uc  U$ UR                   (       a#  U R                   H  n[        U   " XU5        M     U R                   H  n[        U   " XU5        M     U$ r/   )rN   STATIC_QAT_ONLY_OPSr   
STATIC_OPSr   r   rS   	filter_fnops        r(   _annotate_all_static_patterns.XNNPACKQuantizer._annotate_all_static_patternso  sY     &L%%..#E	J ///BBIF "r*   c                V    Uc  U$ U R                    H  n[        U   " XU5        M     U$ r/   )DYNAMIC_OPSr   r   s        r(   _annotate_all_dynamic_patterns/XNNPACKQuantizer._annotate_all_dynamic_patterns  s3     &L""BBIF #r*   c                   [        U R                  R                  5       5      nU R                  R                  5        H   u  p4U R	                  X[        U5      5        M"     [        U R                  R                  5       5      nU R                  R                  5        H   u  pdU R	                  X[        U5      5        M"     U R	                  UU R                  [        XR5      5        U$ r/   )
r   r   keysitemsr   r   r   r   r   r   r   r   r   r   r   r   r   s          r(   r   9XNNPACKQuantizer._annotate_for_static_quantization_config  s       7 7 < < >?#'#:#:#@#@#BK..6{C $C
 t..3356#'#:#:#@#@#BK..6{C $C
 	**/J	

 r*   c                   [        U R                  R                  5       5      nU R                  R                  5        H   u  p4U R	                  X[        U5      5        M"     [        U R                  R                  5       5      nU R                  R                  5        H   u  pdU R	                  X[        U5      5        M"     U R	                  UU R                  [        XR5      5        U$ r/   )
r   r   r   r   r   r   r   r   r   r   r   s          r(   r   :XNNPACKQuantizer._annotate_for_dynamic_quantization_config  s       7 7 < < >?#'#:#:#@#@#BK//6{C $C
 t..3356#'#:#:#@#@#BK//6{C $C
 	++/J	

 r*   c                    g r/   rg   r   s     r(   validateXNNPACKQuantizer.validate  s    r*   c                    U R                   $ r/   )rX   )r   s    r(   get_supported_operators(XNNPACKQuantizer.get_supported_operators  s    111r*   )r   r   r   r   )r   None)r   zlist[QuantizationConfig])rS   Optional[QuantizationConfig]r   zlist[OperatorPatternType])rS   r   r   r   )r   ztorch._ops.OpOverloadPacketrS   r   r   r   )r   r   rS   r   )r   strrS   r   )r   torch.fx.GraphModuler   r   r/   )r   r   rS   r   r   z Optional[Callable[[Node], bool]]r   r   )r   r   r   r   r   zlist[OperatorConfig])r   r   r   __firstlineno____doc__rz   rX   r   r   r   r   classmethodr   r   r   r   r   r   r   r   r   r   r   r   r   r   __static_attributes____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)r9   z	list[int])r   z$dict[str, list[OperatorPatternType]]r   )FFFi   ir  )rO   r   rN   r   rd   r   rm   intrn   r  ro   r  rp   r  )r   r   )r   zlist[Callable]r   z	list[str]r   zCallable[[Node], bool])9
__future__r   rI   	functoolsr   typingr   r   r   r   r7   torch._dynamo_dynamor    torch.nn.functionalrC   
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>   rL   rZ   	lru_cacher   rz   r   r   r   rg   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*   