U
    KcJ                      @   sb  d dl Z d dlmZ d dlZd dlm  mZ d dlmZ d dl	m  m
Z d dlm  m  mZ ddlmZmZmZmZ ddlmZ ddlmZ dgZeejejejejd	Zeejejd
ZeejejejejddZeej ejej ejddZ!ee dddZ"ee dddZ#ee dddZ$ee dddZ%ee dddZ&edddZ'dS )    N)List   )BackendConfigBackendPatternConfigDTypeConfigObservationType)_Conv2dMetadata   )reverse_sequential_wrapper2get_executorch_backend_config)input_dtypeoutput_dtypeweight_dtype
bias_dtype)r   r   T)r   r   r   r   Z
is_dynamic)returnc                  C   s|   t j} tttg}g }|ttjj	
| |tjj	tj	tj	 |ttjjj
| |ddd |S )z?
    Return all configs related to linear modules and ops.
    r   r	   ZweightZbias)r   &OUTPUT_USE_DIFFERENT_OBSERVER_AS_INPUT(executorch_weighted_op_int8_dtype_config,executorch_default_dynamic_int8_dtype_config/executorch_default_dynamic_float16_dtype_configappendr   torchnnZLinearset_observation_typeset_dtype_configsset_root_moduleset_reference_quantized_modulennqrset_qat_modulennqat
functionalZlinear_set_input_type_to_index)observation_typedtype_configsZlinear_configs r$   S/tmp/pip-unpacked-wheel-gikjz4vx/torch/ao/quantization/backend_config/executorch.py_get_linear_configs7   s:    r&   c                  C   sZ  t j} tg}g }tfD ]<}|t|j| |	|j
|j|j |t|j| |ddd |ttjj|jf|t|j|j |ttj|jf|t|j|j |t|j| |	|j
|j|j |ttjj|jf| | |ttj|jf| | q|S )z=
    Return all configs related to conv modules and ops.
    r   r	   r   )r   r   r   r   r   r   rootr   r   r   r   	referencer   qatfuncr!   r   r   ZReLUZset_fuser_methodr
   Zfused_conv_reluZset_fused_moduleFreluZrelu_qat)r"   r#   Zconv_configsZconvsr$   r$   r%   _get_conv_configsR   s    


r-   c                  C   sL   t g} tjtjtjd}g }tjtjfD ]}|t|	| 
| q(|S )z3
    Return all configs related to binary ops.
    )r   r   r	   )r   r   r    OUTPUT_SHARE_OBSERVER_WITH_INPUToperatoraddr   r   r   r   Z(_set_num_tensor_args_to_observation_type)r#   Z+num_tensor_args_to_observation_type_mappingZbinary_op_configsopr$   r$   r%   _get_binary_ops_configs   s    r2   c                  C   s^   t j} tg}tjtjtjtjj	tj
ddddddg}g }|D ]}|t|| | q:|S )ar  
    Return the operator configs for the operators that works for both float and quantized
    input if input is quantized, the output Tensor shares the same quantization parameter
    with input.

    Example operator: avgpool2d, reshape, transpose, maxpool2d
    Example observed operator:
    observer_0 - avgpool2d - observer_0 (same observer instance as input)
    ZpermuteZreshaper,   Zrelu_squeezeZsqueeze_)r   r.   )executorch_default_op_quint8_dtype_configr+   Zadaptive_avg_pool2dr,   Zrelu6r   r   ZAdaptiveAvgPool2dr3   r   r   r   r   )r"   r#   Zshare_qparams_opsZshare_qparams_op_configsr1   r$   r$   r%   _get_share_qparams_ops_configs   s0    
r5   c                  C   s0   t j} tg}g }|ttj| | |S )z2
    Return all configs related to batchnorm.
    )	r   r   r4   r   r   r   ZBatchNorm2dr   r   )r"   r#   Z
bn_configsr$   r$   r%   _get_bn_configs   s    
r6   c                   C   s0   t dt t t t t S )za
    Return the `BackendConfig` for backends PyTorch lowers to through the Executorch stack.
    Z
executorch)r   Zset_backend_pattern_configsr&   r-   r2   r5   r6   r$   r$   r$   r%   r      s    )(r/   typingr   r   Ztorch.nn.functionalr   r    r+   Ztorch.nnZtorch.nn.qatr)   r   Ztorch.nn.quantized._referenceZ	quantizedZ
_referencer   Zbackend_configr   r   r   r   Z_common_operator_config_utilsr   Zfuser_method_mappingsr
   __all__Zquint8Zqint8floatr   r4   r   Zfloat16r   r&   r-   r2   r5   r6   r   r$   r$   r$   r%   <module>   sR   	6!