a
    h5                  b   @   s4  U d dl Z d dlmZmZmZmZ d dl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  mZ d dlm  m	  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Z d dlm  m	  m  mZ d dlm  m	  m  m Z! d dl"Zd dl#m	  m$Z% d dlm	Z	 d dl&m'Z'm(Z( d dl)m*Z*m+Z+ d dl,m-Z- d dl.m/Z/ g dZ0e+ej1e*ej2e	j3e!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?iZ@eAeef eBd	< e+ej1e*ej2e	jCejCe	jDejDe	jEejEe	j4ej4e	j5ej5e	j6ej6e	j7ej7e	j8ej8e	j9ej9e	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j3e	j3ej3e	jQejQe	jRejRejSejTejUejUejVejVejWejWejXejXejYejYejZejZej[ej[ej\ej\ej]ej]ej^ej^ej_ej4ej`ej5ejaej6ejbejWejcejXejdejYejXejXejYejYej\ej\ejeej3ej3ej3ej5ej5ej6ej6i1ZfeAeef eBd
< e	j5ej5e	j6ej6e	j3ej3e	jNjOjPej3ej_ej_ej`ej`ejaejaejbejbejcejcejdejdejXejXejYejYej\ej\ejeejeiZgeAeef eBd< e	j<ej<e	j3ej3ej3ej3e	jNjOjPej3e	j?ej?e	jhejhe	j=ej=e	j>ej>ej\ej\e	j;ej;e	j:ej:iZieAeef eBd< e	jjhZkele eBd< e%jmejnjjme%joejnjjoe%jpejnjjpe%jqejnjjqe%jrejnjjre%jsejnjjsiZteAeeeuf ef eBd< e	jve'e	jwe'e	jxe'e	jye(iZzeAeef eBd< e	j3e
j{jj3iZ|eAeef eBd< e	j3e
j{jjj3iZ}eAeef eBd< ele dddZ~eAeef dddZeAeef dddZeAeef dddZeAeef dddZd:eeeAeef  eeddd Zd;eeeAeef  ed!d"d#ZeAeef dd$d%ZeAeef dd&d'ZeAeef dd(d)ZeAeef dd*d+Zele dd,d-Zele dd.d/ZeAeeeuf ef dd0d1Zeeeuf ed2d3d4Zej	jee d5d6d7Zej	jed5d8d9ZdS )<    N)AnyCallableOptionalUnion)nn)+default_fixed_qparams_range_0to1_fake_quant.default_fixed_qparams_range_neg1to1_fake_quant)DeQuantStub	QuantStub)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,DEFAULT_FLOAT_TO_QUANTIZED_OPERATOR_MAPPINGS"DEFAULT_MODULE_TO_ACT_POST_PROCESS+DEFAULT_STATIC_SPARSE_QUANT_MODULE_MAPPINGS,DEFAULT_DYNAMIC_SPARSE_QUANT_MODULE_MAPPINGSno_observer_set(get_default_static_quant_module_mappings2get_default_static_quant_reference_module_mappings*get_embedding_static_quant_module_mappings/get_default_static_sparse_quant_module_mappingsget_static_quant_module_classget_dynamic_quant_module_classget_default_qat_module_mappings!get_embedding_qat_module_mappings)get_default_dynamic_quant_module_mappings0get_default_dynamic_sparse_quant_module_mappings$get_default_qconfig_propagation_list&get_default_compare_output_module_list0get_default_float_to_quantized_operator_mappingsget_quantized_operatorr   r   r   r   _INCLUDE_QCONFIG_PROPAGATE_LISTr   r   r   r   )returnc                  C   s   t jjt jjh} | S )z8These modules cannot have observers inserted by default.)r   ZquantizableLSTMZMultiheadAttention)Zno_observers r'   Y/var/www/auris/lib/python3.9/site-packages/torch/ao/quantization/quantization_mappings.pyr      s    r   c                   C   s
   t tS )z8Get module mapping for post training static quantization)copydeepcopyr   r'   r'   r'   r(   r      s    r   c                   C   s
   t tS )zBGet reference module mapping for post training static quantization)r)   r*   r   r'   r'   r'   r(   r      s    r   c                  C   s&   t t} tj| tj< tj| tj< | S )z7Get module mapping, including mapping for embedding QAT)r)   r*   r   nnqEmbeddingBagnnqat	Embeddingmappingr'   r'   r(   r      s    
r   c                   C   s
   t tS )z?Get module mapping for post training static sparse quantization)r)   r*   r   r'   r'   r'   r(   r      s    r   F)float_module_classadditional_static_quant_mappingis_referencer%   c                 C   sR   |du ri }t |rtnt|}|| d}|dusHJ 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strr)   r*   )r1   r2   r3   all_mappingsZstatic_quant_module_classr'   r'   r(   r      s    
r   )r1    additional_dynamic_quant_mappingr%   c                 C   sJ   |du ri }t t|}|| d}|dus@J dt|  d t|S )zfn Get the dynamically quantized module class corresponding to
    the floating point module class
    Nr4   r5   )r   r   r6   r7   r)   r*   )r1   r9   r8   Zdynamic_quant_module_classr'   r'   r(   r     s    
r   c                   C   s
   t tS )z:Get default module mapping for quantization aware training)r)   r*   r   r'   r'   r'   r(   r     s    r   c                  C   s&   t t} tj| tj< tj| tj< | S )zGet 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(   r     s    
r   c                   C   s   t S )z9Get module mapping for post training dynamic quantization)r   r'   r'   r'   r(   r   )  s    r   c                   C   s   t S )z@Get module mapping for post training dynamic sparse quantization)r   r'   r'   r'   r(   r   .  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
    )setr   keysr   r   r$   r)   r*   )Z#QCONFIG_PROPAGATE_MODULE_CLASS_LISTr'   r'   r(   r    3  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 )zSGet 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(   r!   @  s    





	r!   c                   C   s
   t tS N)r)   r*   r   r'   r'   r'   r(   r"   P  s    r"   )float_opr%   c                 C   s,   t | d}|dus(J dt|  d|S )z>Get the quantized operator corresponding to the float operatorNz	Operator z) does not have corresponding quantized op)r   r6   r7   )r>   Zquantized_opr'   r'   r(   r#   W  s
    
r#   )moduler%   c                 C   s   t t| dS )zGet 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   r6   r   r?   r'   r'   r(   _get_special_act_post_process`  s    rA   c                 C   s   | j ot| tv S r=   )Ztrainingtyper   r@   r'   r'   r(   _has_special_act_post_processl  s    rC   )NF)N)r)   typingr   r   r   r   ZtorchZtorch.ao.nnZaor   Zao_nnZtorch.ao.nn.intrinsicZ	intrinsicZnniZtorch.ao.nn.intrinsic.qatZqatZnniqatZtorch.ao.nn.intrinsic.quantizedZ	quantizedZnniqZ'torch.ao.nn.intrinsic.quantized.dynamicZdynamicZnniqdZtorch.ao.nn.qatr-   Ztorch.ao.nn.qat.dynamicZnnqatdZtorch.ao.nn.quantizedr+   Ztorch.ao.nn.quantized.dynamicZnnqdZtorch.ao.nn.quantized.reference	referenceZnnqrZtorch.ao.nn.sparseZtorch.nn.functionalZ
functionalFZ#torch.ao.quantization.fake_quantizer   r   Ztorch.ao.quantization.stubsr	   r
   Ztorch.ao.quantization.utilsr   Ztorch.nn.utils.parametrizer   __all__ZQuantizeZ
DeQuantizeZLinearZConv1dZConv2dZConv3dZConvTranspose1dZConvTranspose2dZConvTranspose3dr.   r,   ZGRUCellZLSTMCellZRNNCellr&   r   dict__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	ConvAdd2dZConvAddReLU2dZ
LinearReLUZLinearLeakyReLUZ
LinearTanhZConvBn1dZConvBn2dZConvBn3dZConvBnReLU1dZConvBnReLU2dZConvBnReLU3dZ
LinearBn1dr   r   ZGRUr   Z
Sequentialr$   r:   ZeluopsZ	hardswishZinstance_normZ
layer_normZ
leaky_reluZdropoutr   r7   ZHardsigmoidZSigmoidZSoftmaxZTanhr   sparser   r   r   r   r   r   r   boolr   r   r   r   r   r   r    r!   r"   r#   ModulerA   rC   r'   r'   r'   r(   <module>   sD  
$9	   	