
    קg"6                        U d dl Z d dlmZmZmZmZmZmZ d dlZd dl	m
c mZ d dlm
c mc mZ d dlm
c mc mc mZ d dlm
c mc mc mZ d dlm
c mc mc mc mZ d dlm
c mc mZ d dlm
c mc mc mZ d dlm
c mc mZ d dlm
c mc mc mZ  d dl!m
c mc mc m"Z# d dl$Zd dl%mc 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l0m1Z1 g dZ2e-ej3        e,ej4        ej5        e#j5        ej6        e#j6        ej7        e#j7        ej8        e#j8        ej9        e#j9        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#j@        ejA        e#jA        iZBeeef         eCd	<   i e-ej3        e,ej4        ejD        ejD        ejE        ejE        ejF        ejF        ej6        ej6        ej7        ej7        ej8        ej8        ej9        ej9        ej:        ej:        ej;        ej;        ejG        ejG        ej<        ej<        ej=        ej=        ejH        ejH        ejI        ejI        ejJ        ejJ        i ejK        ejK        ejL        ejL        ejM        ejM        ejN        ejN        ejO        jP        jQ        ej5        ej5        ej5        ejR        ejR        ejF        ejF        ejS        ejS        ejT        ejU        ejV        ejV        ejW        ejW        ejX        ejX        ejY        ejY        ejZ        e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ja        ej7        ejb        ej8        ejc        ejX        ejd        ejY        eje        ejZ        ejY        ejY        ejZ        ejZ        ej]        ej]        ejf        ej5        ej5        ej5        ej7        ej7        ej8        ej8        Zgeeef         eCd
<   ej7        ej7        ej8        ej8        ej5        ej5        ejO        jP        jQ        ej5        ej`        ej`        eja        eja        ejb        ejb        ejc        ejc        ejd        ejd        eje        eje        ejY        ejY        ejZ        ejZ        ej]        ej]        ejf        ejf        iZheeef         eCd<   ej>        e j>        ej5        e j5        ej5        e j5        ejO        jP        jQ        e j5        ejA        e jA        eji        e ji        e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jk        hZlee         eCd<   e'jm        ejn        j        jm        e'jo        ejn        j        jo        e'jp        ejn        j        jp        e'jq        ejn        j        jq        e'jr        ejn        j        jr        e'js        ejn        j        js        iZteeeeuf         ef         eCd<   ejv        e)ejw        e)ejx        e)ejy        e*iZzeeef         eCd<   ej5        ej{        j        j5        iZ|eeef         eCd<   ej5        ej{        j        j        j5        iZ}eeef         eCd<   dee         fdZ~deeef         fdZdeeef         fdZdeeef         fdZdeeef         fdZ	 	 d+dedeeeef                  dedefdZ	 d,dedeeeef                  defdZdeeef         fdZdeeef         fd Zdeeef         fd!Zdeeef         fd"Zdee         fd#Zdee         fd$Zdeeeeuf         ef         fd%Zd&eeeuf         defd'Zd(ej        j        dee         fd)Zd(ej        j        defd*ZdS )-    N)AnyCallableDictOptionalSet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                  J    t           j        j        t           j        j        h} | S )z8These modules cannot have observers inserted by default.)r	   quantizableLSTMMultiheadAttention)no_observerss    g/var/www/html/ai-engine/env/lib/python3.11/site-packages/torch/ao/quantization/quantization_mappings.pyr   r      s    N')JKL    c                  4    t          j        t                    S )z8Get module mapping for post training static quantization)copydeepcopyr    r/   r.   r   r      s    ==>>>r/   c                  4    t          j        t                    S )zBGet reference module mapping for post training static quantization)r1   r2   r   r3   r/   r.   r   r      s    =GHHHr/   c                      t          j        t                    } t          j        | t
          j        <   t          j        | t
          j        <   | S )z7Get module mapping, including mapping for embedding QAT)r1   r2   r   nnqEmbeddingBagnnqat	Embeddingmappings    r.   r   r      s5    m@AAG"%"2GE"}GEONr/   c                  4    t          j        t                    S )z?Get module mapping for post training static sparse quantization)r1   r2   r   r3   r/   r.   r   r      s    =DEEEr/   Ffloat_module_classadditional_static_quant_mappingis_referencec                     |i }t          |rt          nt          |          }|                    | d          }|J dt	          |            dz               t          j        |          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strr1   r2   )r=   r>   r?   all_mappingsstatic_quant_module_classs        r.   r   r      s     '.*,'$	2661'	 L !- 0 01CT J J$00@s+='>'>@@
A	B 100 =2333r/    additional_dynamic_quant_mappingc                     |i }t          t          |          }|                    | d          }|J dt          |            dz               t	          j        |          S )zfn Get the dynamically quantized module class corresponding to
    the floating point module class
    NrA   rB   )r   r   rC   rD   r1   r2   )r=   rG   rE   dynamic_quant_module_classs       r.   r   r     s     (/+-($-/O L ".!1!12Dd!K!K%11@s+='>'>@@
A	B 211 =3444r/   c                  4    t          j        t                    S )z:Get default module mapping for quantization aware training)r1   r2   r   r3   r/   r.   r   r     s    =4555r/   c                      t          j        t                    } t          j        | t
          j        <   t          j        | t
          j        <   | S )zGet module mapping for quantization aware training
    This is includes default values in addition to
    enabling qat for embeddings.
    )r1   r2   r   r8   r7   r	   r9   r:   s    r.   r    r      s6    
 m788G$1GBO!OGBLNr/   c                      t           S )z9Get module mapping for post training dynamic quantization)r   r3   r/   r.   r!   r!   *  s    00r/   c                      t           S )z@Get module mapping for post training dynamic sparse quantization)r   r3   r/   r.   r"   r"   /  s    77r/   c                  "   t          t                                                    t          t                                                    z  t          t                                                    z  t
          z  } t          j        |           S )z_Get the default list of module types that we'll attach qconfig
    attribute to in prepare
    )setr   keysr   r   r'   r1   r2   )#QCONFIG_PROPAGATE_MODULE_CLASS_LISTs    r.   r#   r#   4  sx    
 	0557788
)..00
1
1	2
388::
;
;	< *	* ( =<===r/   c                     t          t                                                    t          t                                                    z  t          t                                                    z  t          t                                                    z  t          t                                                    z  t          t                                                    z  t          z  } t          j        |           S )zSGet list of module class types that we will record output
    in numeric suite
    )	rO   r   valuesr   r   rP   r'   r1   r2   ).NUMERIC_SUITE_COMPARE_MODEL_OUTPUT_MODULE_LISTs    r.   r$   r$   A  s    
 	07799::
)0022
3
3	4
3::<<
=
=	> 27799
:
:	; )..00
1
1		2
 388::
;
;	< *	* 3 =GHHHr/   c                  4    t          j        t                    S N)r1   r2   r   r3   r/   r.   r%   r%   Q  s     =EFFFr/   float_opc                 v    t                               | d          }|J dt          |            d            |S )z>Get the quantized operator corresponding to the float operatorNz	Operator z) does not have corresponding quantized op)r   rC   rD   )rW   quantized_ops     r.   r&   r&   X  sE    ?CCHdSSL  K3x==KKK 	!  r/   modulec                 R    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   rC   r   rZ   s    r.   _get_special_act_post_processr]   a  s)     .11$V,,d  r/   c                 <    | j         ot          |           t          v S rV   )trainingtyper   r\   s    r.   _has_special_act_post_processra   m  s    ?QtF||/QQQr/   )NFrV   )r1   typingr   r   r   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.qatr8   torch.ao.nn.qat.dynamicnnqatdtorch.ao.nn.quantizedr6   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ConvTranspose3dr9   r7   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   rD   HardsigmoidSigmoidSoftmaxTanhr   sparser   r   r   r   r   r   r   boolr   r   r   r    r!   r"   r#   r$   r%   r&   Moduler]   ra   r3   r/   r.   <module>r      sj    < < < < < < < < < < < < < < < <           # # # # # # # # # # # # * * * * * * * * * * * * * * * . . . . . . . . . . . . . . . 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7             ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( # # # # # # # # # # # # , , , , , , , , , , , , , , , . . . . . . . . . . . . . . .                           ? > > > > > > > 9 9 9 9 9 9 C C C C C C  8 s|It{It{It{It{,,,L$.OT&JKJGTYG .Xs]0C   &6=s|6=6= NCO6= NCO	6=
 J6= Isz6= Isz6= Isz6= +6= +6= +6= FCG6= L#-6= OS%6= L#-6=  L#-!6=" s)#6= 6=$ s)%6=& s)'6=( L#-)6=* L#-+6=, J5sz-6=. Isz/6=0 Hci16=2 J36=4 Hci56=8 96=< L$-=6=> L$-?6=@ NDOA6=B NDOC6=D NDOE6=F M4>G6=H t)I6= 6= 6=J NDOK6=L -M6=N NDOO6=P OSZQ6=R OSZS6=T OSZU6=V W6=X Y6=Z [6=\ t]6=^ t_6=` ta6=b szc6=f 
L#*g6=h 
L#*i6=j 
L#*k6= 6= $d8S=&9 6 6 6t Iu|Iu|Iu|J5u|L&/L&/L&/f)f)f)NF%NF%NF%NF%4 T(C-0   ( JIt{
M4;J5t{GTYFDHKJNE$OS%L#-> %tHcM':   0 M2 X    E59"K$.OUY(6L%)%0L%)%0Iuy"*V ,d533G3Q.R    N?J;J;G;	@ "D8);$<    Iu|%,D +T(C--@    Iu|%-4E ,d8S=.A   
S    ?$x}2E ? ? ? ?
ID3<O I I I I
D34G    Fhm9L F F F F FJ4 4 4%-d8S=.A%B4 4 		4 4 4 44 GK5 5 5&.tHcM/B&C5 	5 5 5 5(6hm)< 6 6 6 6
4#+>    14#3F 1 1 1 1
8$x}:M 8 8 8 8

>c(m 
> 
> 
> 
>IH I I I I Gx}	x	'(G G G GU8S=%9 h    	%(/ 	hx>P 	 	 	 	R%(/ Rd R R R R R Rr/   