
    qi?                       d dl mZ d dlZd dlZd dlZd dl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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       ejp                  d       G d de                    Z9y)#    )annotationsN)Any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)Callable)"_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       w/home/ubuntu/crypto_trading_bot/.venv/lib/python3.12/site-packages/torch/ao/quantization/quantizer/xnnpack_quantizer.py_get_dynamo_graphr(   2   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_channelsr2   r3   r1   r4   pattern_w_biaspattern_wo_biass	            r'   _get_linear_patternsr<   8   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)conv2dr0   addadaptive_avg_pool2d)r5   nnConv2dReLUr/   relur>   Linearr0   r?   AdaptiveAvgPool2dr@   copydeepcopy)supported_operatorss    r'   (_supported_symmetric_quantized_operatorsrJ   G   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)rM   rL   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>d   s!      .
 .=.
s   )r   rJ   extendvaluesrG   rH   )supported_config_and_operatorsopsrQ   s     @r'   -_get_supported_symmetric_config_and_operatorsrW   [   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_   r`   ch_axisra   rb    )r   r
   	with_argsr   r   r   r   r5   int8per_tensor_affineper_channel_symmetricper_tensor_symmetricr	   r   r   r   )rM   rL   ra   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_specrQ   s                   r'   r   r   k   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.   )rW   rd   r)   r'   #_get_supported_config_and_operatorsrw      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_stackry   )metagetrT   
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.   rd   )rO   fr   s     r'   rR   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_compatiblezXNNPACKQuantizer is deprecated! Please use xnnpack quantizer in ExecuTorch (https://github.com/pytorch/executorch/tree/main/backends/xnnpack/quantizer) instead.c                  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 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_relur0   	conv_reluconvconv_transpose_relur@   gru_io_onlyadd_relur?   mul_relumulcatr0   c                Z    t         |           d | _        i | _        i | _        i | _        y r.   )super__init__global_configoperator_type_configmodule_type_configmodule_name_config)self	__class__s    r'   r   zXNNPACKQuantizer.__init__  s7    8<  	! NPHJr)   c                `    | j                   D ch c]  \  }}|	 }}}t        |      S c c}}w r.   )rU   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.   )rU   rS   )r   rQ   all_opsr&   rV   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   rQ   s     r'   
set_globalzXNNPACKQuantizer.set_globalA  s    0r)   c                $    || j                   |<   | S r.   )r   )r   operator_typerQ   s      r'   set_operator_typez"XNNPACKQuantizer.set_operator_typeE  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_typerQ   s      r'   set_module_typez XNNPACKQuantizer.set_module_typeM  s     0C,r)   c                >    |t        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`
        z0quantization_config == None is not supported yet)AssertionErrorr   )r   module_namerQ   s      r'   set_module_namez XNNPACKQuantizer.set_module_nameW  s+     & !STT/B,r)   c                    t        |      S )z-Transforms scalar values to tensor attributes)r   r   models     r'   transform_for_annotationz)XNNPACKQuantizer.transform_for_annotationc  s     )//r)   c                    | j                   r2| j                   j                  j                  r| j                  |      }n| j	                  |      }t        |       |S )z!just handling global spec for now)r   input_activationra   )_annotate_for_dynamic_quantization_config(_annotate_for_static_quantization_configr   r   s     r'   annotatezXNNPACKQuantizer.annotatei  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.   )rL   STATIC_QAT_ONLY_OPSr   
STATIC_OPSr   r   rQ   	filter_fnops        r'   _annotate_all_static_patternsz.XNNPACKQuantizer._annotate_all_static_patternss  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.   rd   r   s     r'   validatezXNNPACKQuantizer.validate  s    r)   c                    | j                   S r.   )rU   )r   s    r'   get_supported_operatorsz(XNNPACKQuantizer.get_supported_operators  s    111r)   )r   None)r   zlist[QuantizationConfig])rQ   QuantizationConfig | Noner   zlist[OperatorPatternType])rQ   r   r   r   )r   ztorch._ops.OpOverloadPacketrQ   r   r   r   )r   r   rQ   r   )r   strrQ   r   )r   torch.fx.GraphModuler   r   r.   )r   r   rQ   r   r   zCallable[[Node], bool] | Noner   r   )r   r   r   r   r   zlist[OperatorConfig])__name__r   r   __doc__rw   rU   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K     ";	" &2 0 
	#:L

5N
0)0	0 48	# 7 1	
 
* 48	# 7 1	
 
)	,)	, 2 2r)   )r#   r   r   ztorch.fx.Graph)r7   z	list[int])r   z$dict[str, list[OperatorPatternType]]r   )FFFi   ir   )rM   r   rL   r   ra   r   rj   intrk   r   rl   r   rm   r   )r   r   )r   zlist[Callable]r   z	list[str]r   zCallable[[Node], bool]):
__future__r   rG   	functoolstyping_extensionstypingr   r   r5   torch._dynamo_dynamor   torch.nn.functionalrA   
functionalr/   #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   collections.abcr   torch.ao.quantization.qconfigr   torch.fxr   __all__r(   r<   rJ   rW   	lru_cacher   rw   r   r   
deprecatedr   rd   r)   r'   <module>r     sZ   "    %  #    H I  2 (P '-.(9   RRR R 	R
 R R R Rj;B	*	*/8	*	* e,gL2y L2	 -
L2r)   