U
    ,-e66                  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Ze!e#e$f d d$dZe!e#e$f d d%dZd9e#ee!e#e$f  ee$d'd(dZd:e#ee!e#e$f  e$d)d*dZe!e#e$f d d+dZe!e#e$f d d,dZe!e#e$f d d-dZe!e#e$f d d.dZe"e# d d/dZe"e# d d0dZe!e e#euf e#f d d1dZe e#euf e#d2d3dZejjee# d4d5d6Zejjed4d7d8Zd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_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_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)   l/var/www/html/Darija-Ai-Train/env/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@   $                                                                  :                                   	  
     
	