o
    ei)6                     @   s  U d dl Z d dlmZ d dl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	Z/e*ej0e)ej1ej2e 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>iZ?e@eef eAd
< i e*ej0e)ej1ejBejBejCejCejDejDej3ej3ej4ej4ej5ej5ej6ej6ej7ej7ej8ej8ejEejEej9ej9ej:ej:ejFejFejGejGejHejHi ejIejIejJejJejKejKejLejLejMjNjOej2ej2ej2ejPejPejQejQejRejSejTejTejUejUejVejVejWejWejXejXejYejYejZejZej[ej[ej\ej\ej]ej]ej^ej3ej_ej4ej`ej5ejaejVejbejWejcejXejWejWejXejXej[ej[ejdej2ej2ej2ej4ej4ej5ej5iZee@eef eAd< ej4ej4ej5ej5ej2ej2ejMjNjOej2ej^ej^ej_ej_ej`ej`ejaejaejbejbejcejcejWejWejXejXej[ej[ejdejdiZfe@eef eAd< ej;ej;ej2ej2ej2ej2ejMjNjOej2ej>ej>ejgejgej<ej<ej=ej=ej[ej[ej:ej:ej9ej9iZhe@eef eAd< ejihZjeke eAd< e$jlejmjjle$jnejmjjne$joejmjjoe$jpejmjjpe$jqejmjjqe$jrejmjjriZse@eetB ef eAd< ejue&ejve&ejwe&ejxe'iZye@eef eAd< ej2e	jzjj2iZ{e@eef eAd< ej2e	jzjjj2iZ|e@eef eAd< deke fddZ}de@eef fddZ~de@eef fddZde@eef fddZde@eef fddZ		d=ded e@eef dB d!edefd"d#Z	d>ded$e@eef dB defd%d&Zde@eef fd'd(Zde@eef fd)d*Zde@eef fd+d,Zde@eef fd-d.Zdeke fd/d0Zdeke fd1d2Zde@eetB ef fd3d4Zd5eetB defd6d7Zd8ejjdedB fd9d:Zd8ejjdefd;d<ZdS )?    N)Callable)Any)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(   u/var/www/addictedbytheproject.nl/epg/venv/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 r$t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,   )r5   r6   r7   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 r tdt|  d t|S )zfn Get the dynamically quantized module class corresponding to
    the floating point module class
    Nr8   r9   )r	   r   r:   r;   r<   r+   r,   )r5   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   r.   )zGet module mapping for quantization aware training
    This is includes default values in addition to
    enabling qat for embeddings.
    )r+   r,   r   r1   r0   r   r2   r3   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   rA   )z@Get module mapping for post training dynamic sparse quantization)r   r(   r(   r(   r)   r   1  rB   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   6  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
    )	rC   r   valuesr   r   rD   r"   r+   r,   ).NUMERIC_SUITE_COMPARE_MODEL_OUTPUT_MODULE_LISTr(   r(   r)   r   C  s   






	r   c                   C   r*   N)r+   r,   r   r(   r(   r(   r)   r    S  s   
r    float_opc                 C   s*   t | }|du rt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;   r<   )rI   quantized_opr(   r(   r)   r!   Z  s   
r!   modulec                 C   s   t t| 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
    )r   r:   r
   rK   r(   r(   r)   _get_special_act_post_processd  s   rM   c                 C   s   | j ot| tv S rH   )trainingtyper   rL   r(   r(   r)   _has_special_act_post_processn  s   rP   )NFrH   )r+   collections.abcr   typingr   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.qatr1   torch.ao.nn.qat.dynamicnnqatdtorch.ao.nn.quantizedr/   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ConvTranspose3dr2   r0   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"   rC   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!   ModulerM   rP   r(   r(   r(   r)   <module>   s  
 $	
 !"#$%9	




