U
    KcC                     @   s  d dl Z d dlmZmZ d dlmZmZ d dlmZm	Z	m
Z
 d dlmZmZ ddlmZ dd	lmZ dd
lmZ d dlmZmZmZmZ dd Zeeee
f dddZeeeef dddZdeee
f eee
f dddZde_de_de_dddgZdS )    N)get_default_quant_patternssorted_patterns_dict)get_native_backend_configObservationType)PatternNodePatternQuantizerCls)activation_dtypeget_combined_dict   )BackendConfig   QuantizeHandler)DefaultFuseHandler)DictAnyCallableOptionalc                    s"   G  fdddt }|S )Nc                	       sn   e Zd Zdeeeejjf e	d fddZ
edddZeeeee	 dd	d
Zdd Z  ZS )z=get_quantize_handler_cls.<locals>.ConfigurableQuantizeHandlerN)node_patternmodulesroot_node_getterc                    sd   t  ||| rB| jks4td| j d| | j | _n| _| _| _| _| _d S )Nz7Must provide observation_type config for tensor number z, in num_tensor_args_to_observation_type for )	super__init__Znum_tensor_argsAssertionErrorobservation_typedtype_configsoverwrite_output_fake_quantizeroverwrite_output_observerinput_output_observed_)selfr   r   r   )	__class__r   input_output_observed#num_tensor_args_to_observation_typer   r   r    Q/tmp/pip-unpacked-wheel-gikjz4vx/torch/ao/quantization/fx/backend_config_utils.pyr       s    zFget_quantize_handler_cls.<locals>.ConfigurableQuantizeHandler.__init__)returnc                 S   s   | j tjkS N)r   r   Z OUTPUT_SHARE_OBSERVER_WITH_INPUTr    r$   r$   r%   is_general_tensor_value_op2   s    zXget_quantize_handler_cls.<locals>.ConfigurableQuantizeHandler.is_general_tensor_value_op)qconfigpatternis_trainingr&   c                 S   sH   t |}|r(|tjkrB| jdk	rB| jS n|tjkrB| jdk	rB| jS |jS )z
            Returns the constructor for the activation observer which should be
            used for the pattern matched to this handler. Some handlers override
            this to a different value than what is specified in the qconfig.
            N)r	   torchZquint8r   r   Z
activation)r    r*   r+   r,   Z	act_dtyper$   r$   r%   get_activation_ctr6   s    zPget_quantize_handler_cls.<locals>.ConfigurableQuantizeHandler.get_activation_ctrc                 S   s   | j S r'   )r   r(   r$   r$   r%   r"   L   s    zSget_quantize_handler_cls.<locals>.ConfigurableQuantizeHandler.input_output_observed)N)__name__
__module____qualname__r   r   strr-   nnModuler   r   boolr)   r   r   r   r.   r"   __classcell__r$   r   r"   r#   r   r   r   )r!   r%   ConfigurableQuantizeHandler   s    r8   r   )r   r   r#   r   r   r"   r8   r$   r7   r%   get_quantize_handler_cls   s    1r9   )backend_configr&   c           
      C   sb   i }| j  D ]N\}}|j}|j}|j}|j}|j}|j}	|	dkrFd}	t||||||	||< q|S )a#  
    Note: Quantize handler is just a holder for some check methods like
    (should_insert_observer_for_output), maybe this can be a enum as well,
    we can refactor this after we convert the path for fbgemm/qnnpack fully to the
    new path, this is not exposed to backend developers
    NT)	configsitemsr   r   Z$_num_tensor_args_to_observation_typeZ_overwrite_output_fake_quantizeZ_overwrite_output_observerZ_input_output_observedr9   )
r:   Zpattern_to_quantize_handlersr+   configr   r   r#   Zoverwrite_fake_quantizerZoverwrite_observerr"   r$   r$   r%    get_pattern_to_quantize_handlersR   s(    	r>   c                 C   s.   i }| j  D ]\}}|jd k	rt||< q|S r'   )r;   r<   Zfuser_methodr   )r:   Zfusion_pattern_to_fuse_handlersr+   r=   r$   r$   r%   &get_fusion_pattern_to_fuse_handler_clso   s
    

r?   )additional_quant_patternsr&   c                 C   s@   t  }| dk	rt|| }tt  D ]\}}|||< q&t|S )z
    Return a map from pattern to quantize handlers based on the default patterns and the native backend_config.
    The returned map is sorted such that longer patterns will be encountered first when iterating through it.
    N)r   r
   r>   r   r<   r   )r@   patternsr+   Zquantize_handlerr$   r$   r%   get_native_quant_patternsz   s    

rB   z-torch.ao.quantization.fx.backend_config_utils)N)r-   Z&torch.ao.quantization.fx.pattern_utilsr   r   Z$torch.ao.quantization.backend_configr   r   Z(torch.ao.quantization.quantization_typesr   r   r   Ztorch.ao.quantization.utilsr	   r
   r:   r   Zquantization_patternsr   Zfusion_patternsr   typingr   r   r   r   r9   r>   r?   rB   r0   __all__r$   r$   r$   r%   <module>   s*   ; 
"