U
    ©Ê¹j66  ã                d   @   sn  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  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  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 dd	d
ddddddddddddddddddddgZ2e)ej3e*e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@ejAejAiZBe!e#e$f eCd< e)ej3e*ej4ejDejDejEejEejFejFej6ej6ej7ej7ej8ej8ej9ej9ej:ej:ej;ej;ejGejGej<ej<ej=ej=ejHejHejIejIejJejJejKejKejLejLejMejMejNejNejOjPjQej5ej5ej5ejRejRejFejFejSejSejTe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j_ej`ej6ejaej7ejbej8ejcejXejdejYejeejZejYejYejZejZej]ej]ejfej5ej5ej5ej7ej7ej8ej8i2Zge!e#e$f eCd	< ej7ej7ej8ej8ej5ej5ejOjPjQej5e	j`ej`e	jaejae	jbejbe	jcejce	jdejde	jeejee	jYejYe	jZejZe	j]ej]e	jfejfiZhe!e#e$f eCd
< ej>ej>ej5ej5ej5ej5ejOjPjQej5ejAejAejiejiej?ej?ej@ej@e	j]ej]ej=ej=ej<ej<iZje!e#e$f eCd< ejkhZle"e# eCd< ejmejnjjmejoejnjjoejpejnjjpejqejnjjqejrejnjjrejsejnjjsiZte!e e#euf e#f eCd< ejve,ejwe,ejxe,ejye-iZze!e#e#f eCd< ej5e'j{jj5iZ|e!e#e$f eCd< ej5e'j{jjj5iZ}e!e#e$f eCd< e"e$ d œd!d„Z~e!e#e$f d œd"d„Ze!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„Z…e!e#e$f d œd+d„Z†e!e#e$f d œd,d„Z‡e!e#e$f d œd-d„Zˆe!e#e$f d œd.d„Z‰e"e# d œd/d„ZŠe"e# d œd0d„Z‹e!e e#euf e#f d œd1d„ZŒe e#euf e#d2œd3d„Z�ejjŽee# d4œd5d6„Z�ejjŽeƒd4œd7d8„Z�d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Ú,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_mappingsÚ2get_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_mappingsÚ0get_default_dynamic_sparse_quant_module_mappingsÚ$get_default_qconfig_propagation_listÚ&get_default_compare_output_module_listÚ0get_default_float_to_quantized_operator_mappingsÚget_quantized_operatorÚ_INCLUDE_QCONFIG_PROPAGATE_LIST)Ú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)   új/var/www/html/bwave/python/venv/lib/python3.8/site-packages/torch/ao/quantization/quantization_mappings.pyr   Ñ   s    þc                   C   s
   t  t¡S )z> Get module mapping for post training static quantization
    )ÚcopyÚdeepcopyr   r)   r)   r)   r*   r   Ù   s    c                   C   s
   t  t¡S )zH Get reference module mapping for post training static quantization
    )r+   r,   r   r)   r)   r)   r*   r   Þ   s    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*   r   ã   s    
c                   C   s
   t  t¡S )zE Get module mapping for post training static sparse quantization
    )r+   r,   r   r)   r)   r)   r*   r   ë   s    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Ústrr+   r,   )r3   r4   r5   Úall_mappingsZstatic_quant_module_classr)   r)   r*   r   ð   s     þ
ÿÿ)r3   Ú 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
    Nr6   r7   )r   r   r8   r9   r:   r+   r,   )r3   r<   r;   Zdynamic_quant_module_classr)   r)   r*   r     s    

ÿÿc                   C   s
   t  t¡S )z@ Get default module mapping for quantization aware training
    )r+   r,   r   r)   r)   r)   r*   r     s    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   r0   r1   r)   r)   r*   r     s    
c                   C   s   t S )z? Get module mapping for post training dynamic quantization
    )r   r)   r)   r)   r*   r       s    c                   C   s   t S )zF Get module mapping for post training dynamic sparse quantization
    )r   r)   r)   r)   r*   r!   %  s    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"   *  s    

ÿ
þýÿ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*   r#   6  s    

ÿ
þ
ý
ü
ûúÿ	c                   C   s
   t  t¡S ©N)r+   r,   r   r)   r)   r)   r*   r$   E  s    )Úfloat_opr'   c                 C   s,   t  | d¡}|dk	s(tdt| ƒ› d�ƒ‚|S )zD Get the quantized operator corresponding to the float operator
    Nz	Operator z) does not have corresponding quantized op)r   r8   r9   r:   )rA   Zquantized_opr)   r)   r*   r%   J  s
    
ÿ)Ú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   r8   r   ©rB   r)   r)   r*   Ú_get_special_act_post_processR  s    rD   c                 C   s   | j ot| ƒtkS r@   )ZtrainingÚtyper   rC   r)   r)   r*   Ú_has_special_act_post_process[  s    rF   )NF)N)‘r+   Ztorchr   Ztorch.nn.functionalZ
functionalÚFZtorch.ao.nn.intrinsicZaoZ	intrinsicZnniZtorch.ao.nn.intrinsic.quantizedZ	quantizedZnniqZ'torch.ao.nn.intrinsic.quantized.dynamicZdynamicZnniqdZtorch.ao.nn.intrinsic.qatZqatZnniqatZtorch.ao.nn.quantizedr-   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   Ú__all__ZQuantizeZ
DeQuantizeZLinearZConv1dZConv2dZConv3dZConvTranspose1dZConvTranspose2dZConvTranspose3dr0   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	ConvAdd2dZConvAddReLU2dZ
LinearReLUZLinearLeakyReLUZ
LinearTanhZConvBn1dZConvBn2dZConvBn3dZConvBnReLU1dZConvBnReLU2dZConvBnReLU3dZ
LinearBn1dr   r   ZGRUr   Z
Sequentialr&   ZeluZ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%   ÚModulerD   rF   r)   r)   r)   r*   Ú<module>   s@   $ é               ñ                                                  Ë:              ñ           õÿ      ú    ü	 ÿ 
ÿ  ý ý þ þ
ÿ	