o
    Zh6                     @   s  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	< i e+ej1e*ej2e	jCejCe	jDejDe	jEejEe	j4ej4e	j5ej5e	j6ej6e	j7ej7e	j8ej8e	j9ej9e	jFejFe	j:ej:e	j;ej;e	jGejGe	jHejHe	jIejIi e	jJejJe	jKejKe	jLejLe	jMejMe	jNjOjPej3e	j3ej3e	jQejQe	jEejEe	jRejRejSejTejUejUejVejVejWejWejXejXejYejYejZejZej[ej[i ej\ej\ej]ej]ej^ej^ej_ej4ej`ej5ejaej6ejbejWejcejXejdejYejXejXejYejYej\ej\ejeej3ej3ej3ej5ej5ej6ej6ZfeAe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< dele fddZ~deAeef fddZdeAeef fddZdeAeef fddZdeAeef fddZ		d<dedeeAeef  d edefd!d"Z	d=ded#eeAeef  defd$d%ZdeAeef fd&d'ZdeAeef fd(d)ZdeAeef fd*d+ZdeAeef fd,d-Zdele fd.d/Zdele fd0d1ZdeAeeeuf ef fd2d3Zd4eeeuf defd5d6Zd7ej	jdee fd8d9Zd7ej	jdefd:d;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'   Z/var/www/auris/lib/python3.10/site-packages/torch/ao/quantization/quantization_mappings.pyr      s   r   c                   C   
   t tS )z8Get module mapping for post training static quantization)copydeepcopyr   r'   r'   r'   r(   r         
r   c                   C   r)   )zBGet reference module mapping for post training static quantization)r*   r+   r   r'   r'   r'   r(   r      r,   r   c                  C   &   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   r)   )z?Get module mapping for post training static sparse quantization)r*   r+   r   r'   r'   r'   r(   r      r,   r   Ffloat_module_classadditional_static_quant_mappingis_referencec                 C   sR   |du ri }t |rtnt|}|| d}|dus$J 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+   )r4   r5   r6   all_mappingsZstatic_quant_module_classr'   r'   r(   r      s   

r    additional_dynamic_quant_mappingc                 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
    Nr7   r8   )r   r   r9   r:   r*   r+   )r4   r<   r;   Zdynamic_quant_module_classr'   r'   r(   r     s   

r   c                   C   r)   )z:Get default module mapping for quantization aware training)r*   r+   r   r'   r'   r'   r(   r     r,   r   c                  C   r-   )zGet module mapping for quantization aware training
    This is includes default values in addition to
    enabling qat for embeddings.
    )r*   r+   r   r0   r/   r   r1   r2   r'   r'   r(   r     s   
r   c                   C      t S )z9Get module mapping for post training dynamic quantization)r   r'   r'   r'   r(   r   *     r   c                   C   r=   )z@Get module mapping for post training dynamic sparse quantization)r   r'   r'   r'   r(   r   /  r>   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    4  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!   A  s   






	r!   c                   C   r)   N)r*   r+   r   r'   r'   r'   r(   r"   Q  s   
r"   float_opc                 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   r9   r:   )rC   Zquantized_opr'   r'   r(   r#   X  s
   
r#   modulec                 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   r9   r   rD   r'   r'   r(   _get_special_act_post_processa  s   rF   c                 C   s   | j ot| tv S rB   )Ztrainingtyper   rE   r'   r'   r(   _has_special_act_post_processm  s   rH   )NFrB   )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.qatr0   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ConvTranspose3dr1   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   r:   ZHardsigmoidZSigmoidZSoftmaxZTanhr   sparser   r   r   r   r   r   r   boolr   r   r   r   r   r   r    r!   r"   r#   ModulerF   rH   r'   r'   r'   r(   <module>   s  
 $	
 !"#$%&'()*+,-./01345:	


	