o
    h66                     @   s  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 g d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	< i e)ej3e*ej4ejDejDejEejEejFejFej6ej6ej7ej7ej8ej8ej9ej9ej:ej:ej;ej;ejGejGej<ej<ej=ej=ejHejHejIejIejJejJi ejKejKejLejLejMejMejNejNejOjPjQej5ej5ej5ejRejRejFejFejSejSejTejUe	jVejVe	jWejWe	jXejXe	jYejYe	jZejZe	j[ej[e	j\ej\i e	j]ej]e	j^ej^e	j_ej_ej`ej6ejaej7ejbej8ejcejXejdejYejeejZejYejYejZejZej]ej]ejfej5ej5ej5ej7ej7ej8ej8Zge!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< de"e$ 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Zde!e#e$f fddZ		d<de#dee!e#e$f  d ede$fd!d"Z	d=de#d#ee!e#e$f  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e"e# fd.d/Zde"e# fd0d1Zde!e 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)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_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,   a/var/www/html/ai/venv/lib/python3.10/site-packages/torch/ao/quantization/quantization_mappings.pyr      s   r   c                   C   
   t tS )z> Get module mapping for post training static quantization
    )copydeepcopyr   r,   r,   r,   r-   r         
r   c                   C   r.   )zH Get reference module mapping for post training static quantization
    )r/   r0   r   r,   r,   r,   r-   r      r1   r   c                  C   &   t t} tj| tj< tj| tj< | S )z= Get module mapping, including mapping for embedding QAT
    )r/   r0   r   nnqEmbeddingBagnnqat	Embeddingmappingr,   r,   r-   r      s   
r   c                   C   r.   )zE Get module mapping for post training static sparse quantization
    )r/   r0   r   r,   r,   r,   r-   r      r1   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/   r0   )r9   r:   r;   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/   r0   )r9   rB   r@   dynamic_quant_module_classr,   r,   r-   r     s   


r   c                   C   r.   )z@ Get default module mapping for quantization aware training
    )r/   r0   r   r,   r,   r,   r-   r     r1   r   c                  C   r2   )z Get module mapping for quantization aware training
        This is includes default values in addition to
        enabling qat for embeddings.
    )r/   r0   r   r5   r4   r   r6   r7   r,   r,   r-   r     s   
r   c                   C      t S )z? Get module mapping for post training dynamic quantization
    )r   r,   r,   r,   r-   r          r    c                   C   rD   )zF Get module mapping for post training dynamic sparse quantization
    )r   r,   r,   r,   r-   r!   %  rE   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/   r0   )#QCONFIG_PROPAGATE_MODULE_CLASS_LISTr,   r,   r-   r"   *  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 )zT Get list of module class types that we will record output
    in numeric suite
    )	rF   r   valuesr   r   rG   r&   r/   r0   ).NUMERIC_SUITE_COMPARE_MODEL_OUTPUT_MODULE_LISTr,   r,   r-   r#   6  s   






	r#   c                   C   r.   N)r/   r0   r   r,   r,   r,   r-   r$   E  s   
r$   float_opc                 C   s,   t | d}|dusJ dt|  d|S )zD Get the quantized operator corresponding to the float operator
    Nz	Operator z) does not have corresponding quantized op)r   r>   r?   )rL   quantized_opr,   r,   r-   r%   J  s
   
r%   modulec                 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   r>   r   rN   r,   r,   r-   _get_special_act_post_processR  s   rP   c                 C   s   | j ot| tv S rK   )trainingtyper   rO   r,   r,   r-   _has_special_act_post_process[  s   rS   )NFrK   )r/   torchr   torch.nn.functional
functionalFtorch.ao.nn.intrinsicao	intrinsicnnitorch.ao.nn.intrinsic.quantized	quantizednniq'torch.ao.nn.intrinsic.quantized.dynamicdynamicnniqdtorch.ao.nn.intrinsic.qatqatnniqattorch.ao.nn.quantizedr3   torch.ao.nn.quantized.reference	referencennqrtorch.ao.nn.quantized.dynamicnnqdtorch.ao.nn.qatr5   torch.ao.nn.qat.dynamicnnqatdtypingr   r   r   r   r   r   torch.ao.nn.sparsetorch.ao.nnao_nntorch.ao.quantization.stubsr	   r
   #torch.ao.quantization.fake_quantizer   r   torch.ao.quantization.utilsr   torch.nn.utils.parametrizer   __all__Quantize
DeQuantizeLinearConv1dConv2dConv3dConvTranspose1dConvTranspose2dConvTranspose3dr6   r4   GRUCellLSTMCellRNNCellr)   r   __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&   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%   ModulerP   rS   r,   r,   r,   r-   <module>   s  
 $ 	
 !"#$%&'()*+,-./01345:	



	