o
    Ih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   quantizableLSTMMultiheadAttention)no_observers r*   _/var/www/vscode/kcb/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.   )r7   r8   r9   all_mappings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
    Nr:   r;   )r   r   r<   r=   r-   r.   )r7   r@   r>   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   r0   )zGet module mapping for quantization aware training
    This is includes default values in addition to
    enabling qat for embeddings.
    )r-   r.   r   r3   r2   r   r4   r5   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   rB   )z@Get module mapping for post training dynamic sparse quantization)r   r*   r*   r*   r+   r   /  rC   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.   )#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
    )	rD   r   valuesr   r   rE   r$   r-   r.   ).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   r<   r=   )rJ   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   r<   r   rL   r*   r*   r+   _get_special_act_post_processa  s   rN   c                 C   s   | j ot| tv S rI   )trainingtyper   rM   r*   r*   r+   _has_special_act_post_processm  s   rQ   )NFrI   )r-   typingr   r   r   r   torchtorch.ao.nnaor   ao_nntorch.ao.nn.intrinsic	intrinsicnnitorch.ao.nn.intrinsic.qatqatnniqattorch.ao.nn.intrinsic.quantized	quantizednniq'torch.ao.nn.intrinsic.quantized.dynamicdynamicnniqdtorch.ao.nn.qatr3   torch.ao.nn.qat.dynamicnnqatdtorch.ao.nn.quantizedr1   torch.ao.nn.quantized.dynamicnnqdtorch.ao.nn.quantized.reference	referencennqrtorch.ao.nn.sparsetorch.nn.functional
functionalF#torch.ao.quantization.fake_quantizer   r   torch.ao.quantization.stubsr	   r
   torch.ao.quantization.utilsr   torch.nn.utils.parametrizer   __all__Quantize
DeQuantizeLinearConv1dConv2dConv3dConvTranspose1dConvTranspose2dConvTranspose3dr4   r2   GRUCellLSTMCellRNNCellr'   r   dict__annotations__BatchNorm2dBatchNorm3dDropoutELU	GroupNorm	HardswishInstanceNorm1dInstanceNorm2dInstanceNorm3d	LayerNorm	LeakyReLUmoduleslinearNonDynamicallyQuantizableLinearReLU6PReLUFloatFunctionalQFunctionalBNReLU2dBNReLU3d
ConvReLU1d
ConvReLU2d
ConvReLU3d	ConvAdd2dConvAddReLU2d
LinearReLULinearLeakyReLU
LinearTanhConvBn1dConvBn2dConvBn3dConvBnReLU1dConvBnReLU2dConvBnReLU3d
LinearBn1dr   r   GRUr   
Sequentialr$   rD   eluops	hardswishinstance_norm
layer_norm
leaky_reludropoutr   r=   HardsigmoidSigmoidSoftmaxTanhr   sparser   r   r   r   r   r   r   boolr   r   r   r   r   r   r    r!   r"   r#   ModulerN   rQ   r*   r*   r*   r+   <module>   s  
 $	
 !"#$%&'()*+,-./01345:	


	