U
    Kc1                  \   @   s  U d dl Z d dlZd dlmZ d dlm  mZ d dlm  mZ d dl	m  m  m
Z d dlm  m  m
  mZ d dlm  m  mZ d dlm  m  m
Z d dlm  m  m
  mZ d dlm  m  m
  mZ d dlm  m  mZ d dlm  m  m  mZ d dlmZm Z m!Z!m"Z"m#Z#m$Z$ d dl%Zd dl&m  mZ' d dl(m)Z)m*Z* d dl+m,Z,m-Z- d dl.m/Z/ d dl0m1Z1 e)ej2e*ej3ej4ej4ej5ej5ej6ej6ej7ej7ej8ej8ej9ej9ej:ej:ej;ej;ej<ej<ej=ej=ej>ej>ej?ej?ej@ej@iZAe!e#e$f eBd< e)ej2e*ej3ejCejCejDejDejEejEej5ej5ej6ej6ej7ej7ej8ej8ej9ej9ej:ej:ejFejFej;ej;ej<ej<ejGejGejHejHejIejIejJejJejKejKejLejLejMejMejNjOjPej4ej4ej4ejQejQejEejEejRejRejSejTejUejUejVejVejWejWejXejXejYejYejZejZej[ej5ej\ej6ej]ej7ej^ejWej_ejXej`ejYejXejXejYejYejZejZejaej4ej4ej4ej6ej6ej7ej7i.Zbe!e#e$f eBd	< ej6ej6ej7ej7ej4ej4ejNjOjPej4ej[ej[ej\ej\ej]ej]ej^ej^ej_ej_ej`ej`ejXejXejYejYejZejZejaejaiZce!e#e$f eBd
< ej=ej=ej4ej4ej4ej4ejNjOjPej4ej@ej@ejdejdej>ej>ej?ej?ejZejZej<ej<ej;ej;iZee!e#e$f eBd< ejfhZge"e# eBd< ejhejij
jhejjejij
jjejkejij
jkejlejij
jlejmejij
jmejnejij
jniZoe!e e#epf e#f eBd< ejqe,ejre,ejse,ejte-iZue!e#e#f eBd< ej4e'jvj
j4iZwe!e#e$f eBd< ej4e'jvj
jj4iZxe!e#e$f eBd< e"e$ dddZye!e#e$f dddZze!e#e$f dddZ{e!e#e$f dddZ|e!e#e$f dddZ}d9e#ee!e#e$f  e~e$dddZd:e#ee!e#e$f  e$d d!d"Ze!e#e$f dd#d$Ze!e#e$f dd%d&Ze!e#e$f dd'd(Ze!e#e$f dd)d*Ze"e# dd+d,Ze"e# dd-d.Ze!e e#epf e#f dd/d0Ze e#epf e#d1d2d3Zejjee# d4d5d6Zejje~d4d7d8ZdS );    N)nn)OptionalUnionDictSetCallableAny)	QuantStubDeQuantStub)+default_fixed_qparams_range_0to1_fake_quant.default_fixed_qparams_range_neg1to1_fake_quant)get_combined_dict)type_before_parametrizations.DEFAULT_REFERENCE_STATIC_QUANT_MODULE_MAPPINGS$DEFAULT_STATIC_QUANT_MODULE_MAPPINGSDEFAULT_QAT_MODULE_MAPPINGS%DEFAULT_DYNAMIC_QUANT_MODULE_MAPPINGS_INCLUDE_QCONFIG_PROPAGATE_LIST,DEFAULT_FLOAT_TO_QUANTIZED_OPERATOR_MAPPINGS"DEFAULT_MODULE_TO_ACT_POST_PROCESS+DEFAULT_STATIC_SPARSE_QUANT_MODULE_MAPPINGS,DEFAULT_DYNAMIC_SPARSE_QUANT_MODULE_MAPPINGS)returnc                  C   s   t tjjtjjg} | S )z8These modules cannot have observers inserted by default.)setr   ZquantizableLSTMZMultiheadAttention)Zno_observers r   O/tmp/pip-unpacked-wheel-gikjz4vx/torch/ao/quantization/quantization_mappings.pyno_observer_set   s
    r   c                   C   s
   t tS )z> Get module mapping for post training static quantization
    )copydeepcopyr   r   r   r   r   (get_default_static_quant_module_mappings   s    r    c                   C   s
   t tS )zH Get reference module mapping for post training static quantization
    )r   r   r   r   r   r   r   2get_default_static_quant_reference_module_mappings   s    r!   c                  C   s&   t t} tj| tj< tj| tj< | S )z= Get module mapping, including mapping for embedding QAT
    )r   r   r   nnqEmbeddingBagnnqat	Embeddingmappingr   r   r   *get_embedding_static_quant_module_mappings   s    
r(   c                   C   s
   t tS )zE Get module mapping for post training static sparse quantization
    )r   r   r   r   r   r   r   /get_default_static_sparse_quant_module_mappings   s    r)   F)float_module_classadditional_static_quant_mappingis_referencer   c                 C   sR   |dkri }t |rtnt|}|| d}|dk	sHtdt| d t|S )zen Get the statically quantized module class corresponding to
    the floating point module class
    NFloating point module class {}5 does not have a corresponding quantized module class)	r   r   r   getAssertionErrorformatstrr   r   )r*   r+   r,   all_mappingsZstatic_quant_module_classr   r   r   get_static_quant_module_class   s     
r4   )r*    additional_dynamic_quant_mappingr   c                 C   sJ   |dkri }t t|}|| d}|dk	s@tdt| d t|S )zfn Get the dynamically quantized module class corresponding to
    the floating point module class
    Nr-   r.   )r   r   r/   r0   r1   r2   r   r   )r*   r5   r3   Zdynamic_quant_module_classr   r   r   get_dynamic_quant_module_class   s    

r6   c                   C   s
   t tS )z@ Get default module mapping for quantization aware training
    )r   r   r   r   r   r   r   get_default_qat_module_mappings   s    r7   c                  C   s&   t t} tj| tj< tj| tj< | S )z Get module mapping for quantization aware training
        This is includes default values in addition to
        enabling qat for embeddings.
    )r   r   r   r$   r#   r   r%   r&   r   r   r   !get_embedding_qat_module_mappings   s    
r8   c                   C   s   t S )z? Get module mapping for post training dynamic quantization
    )r   r   r   r   r   )get_default_dynamic_quant_module_mappings  s    r9   c                   C   s   t S )zF Get module mapping for post training dynamic sparse quantization
    )r   r   r   r   r   0get_default_dynamic_sparse_quant_module_mappings  s    r:   c                  C   s2   t t t t B t t B tB } t| S )z` Get the default list of module types that we'll attach qconfig
    attribute to in prepare
    )r   r   keysr   r   r   r   r   )Z#QCONFIG_PROPAGATE_MODULE_CLASS_LISTr   r   r   $get_default_qconfig_propagation_list  s    


r<   c                  C   sV   t t t t B t t B t t B t t B t t B tB } t| S )zT Get list of module class types that we will record output
    in numeric suite
    )	r   r   valuesr   r   r;   r   r   r   )Z.NUMERIC_SUITE_COMPARE_MODEL_OUTPUT_MODULE_LISTr   r   r   &get_default_compare_output_module_list  s    





	r>   c                   C   s
   t tS N)r   r   r   r   r   r   r   0get_default_float_to_quantized_operator_mappings'  s    r@   )float_opr   c                 C   s*   t | d}|dk	s&tdt| |S )zD Get the quantized operator corresponding to the float operator
    Nz4Operator {} does not have corresponding quantized op)r   r/   r0   r1   r2   )rA   Zquantized_opr   r   r   get_quantized_operator,  s
    
rB   )moduler   c                 C   s   t t| dS )z Get the special activation post process for `module`, this has
    higher priority than the activation post process in `qconfig`
    e.g.
    input: torch.nn.Sigmoid
    output: default_affine_fixed_qparam_fake_quant
    N)r   r/   r   rC   r   r   r   _get_special_act_post_process4  s    rE   c                 C   s   | j ot| tkS r?   )Ztrainingtyper   rD   r   r   r   _has_special_act_post_process=  s    rG   )NF)N)r   Ztorchr   Ztorch.nn.functionalZ
functionalFZtorch.nn.intrinsicZ	intrinsicZnniZtorch.nn.intrinsic.quantizedZ	quantizedZnniqZ$torch.nn.intrinsic.quantized.dynamicZdynamicZnniqdZtorch.nn.intrinsic.qatZqatZnniqatZtorch.ao.nn.quantizedZaor"   Ztorch.ao.nn.quantized.reference	referenceZnnqrZtorch.ao.nn.quantized.dynamicZnnqdZtorch.ao.nn.qatr$   Ztorch.ao.nn.qat.dynamicZnnqatdtypingr   r   r   r   r   r   Ztorch.ao.nn.sparseZtorch.ao.nnZao_nnZtorch.ao.quantization.stubsr	   r
   Z#torch.ao.quantization.fake_quantizer   r   Ztorch.ao.quantization.utilsr   Ztorch.nn.utils.parametrizer   ZQuantizeZ
DeQuantizeZLinearZConv1dZConv2dZConv3dZConvTranspose1dZConvTranspose2dZConvTranspose3dr%   r#   ZGRUCellZLSTMCellZRNNCellr   r   __annotations__ZBatchNorm2dZBatchNorm3dZDropoutZELUZ	GroupNormZ	HardswishZInstanceNorm1dZInstanceNorm2dZInstanceNorm3dZ	LayerNormZ	LeakyReLUmodulesZlinearZNonDynamicallyQuantizableLinearZReLU6ZPReLUZFloatFunctionalZQFunctionalZBNReLU2dZBNReLU3dZ
ConvReLU1dZ
ConvReLU2dZ
ConvReLU3dZ
LinearReLUZConvBn1dZConvBn2dZConvBn3dZConvBnReLU1dZConvBnReLU2dZConvBnReLU3dZ
LinearBn1dr   r   ZGRUr   Z
Sequentialr   ZeluopsZ	hardswishZinstance_normZ
layer_normZ
leaky_reluZdropoutr   r2   ZHardsigmoidZSigmoidZSoftmaxZTanhr   sparser   r   r   r    r!   r(   r)   boolr4   r6   r7   r8   r9   r:   r<   r>   r@   rB   ModulerE   rG   r   r   r   r   <module>   s                                                                  6                                   	  
     
	