
    9j                    $   d Z ddl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Z ddlmZ ddlmc mZ ddlmZ ddlmZ ddlmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2 ddl3m4Z4m5Z5m6Z6 ddl7m8Z8 ddl9m:Z: dd	l;m<Z<m=Z= 	 dd
l>m?Z?m@Z@ ddlAmBZBmCZCmDZDmEZE ddlFmGZG ddlHmIZI dZJddlLZLddlMZMddlNZNddlOZOddlPZPddlQmRZR ddlSmTZT ddlUZVddlWmXZY ddlZm[Z[  G d d      Z\d Z]d Z^ej                  j                         Z`e`fdZa G d d      ZbddZcd Zdd Zed Zfd Zgd Zhdd Zid! Zjd" Zkd# Zld$ Zmd% Znd& Zod' Zpd( Zqd) Zrd* Zs	 ddltZtdZu ePj                  eu d+      Zwd, ZxeVj                  dfd-Zzdd.Z{dd/Z|d0 Z} G d1 d2e=      Z~ G d3 d4e~      Z G d5 d6ej                  j                         Z G d7 d8ej                  j                         Z G d9 d:ej                  j                         Z G d; d<ej                         Z G d= d>ej                  j                         Z G d? d@ej                  j                         Z G dA dBej                  j                         Z G dC dDej                  j                         Z G dE dFej                  j                         Z G dG dHej                  j                         Z G dI dJej                  j                         Z G dK dLej                  j                         Z G dM dNej                  j                         Z G dO dPej                  j                         Z G dQ dRej                  j                         Z G dS dTej                  j                         Z G dU dVej                  j                         Z G dW dXej                         Z G dY dZej                  j                         Z G d[ d\ej                  j                         Z G d] d^ej                  j                         Z G d_ d`ej                  j                         Z G da dbej                  j                         Z G dc ddej                  j                         Z G de dfej                  j                         Z G dg dhej                  j                         Z G di djej                  j                         Z G dk dlej                  j                         Z G dm dnej                  j                         Z G do dpej                  j                         Z G dq drej                  j                         Z G ds dtej                  j                         Z G du dvej                  j                         Z G dw dxej                  j                         Z G dy dzej                  j                         Z G d{ d|ej                  j                         Z G d} d~ej                  j                         Z G d dej                  j                         Z G d dej                  j                         Z G d dej                  j                         Z G d dej                         Z G d dej                         Z G d dej                  j                         Z G d dej                  j                         Z G d dej                  j                         Z G d dej                         Z G d dej                         Z G d dej                  j                         Z G d dej                  j                         Z G d dej                  j                         Z G d dej                  j                         Z G d dej                  j                         Z G d dej                  j                         Z G d dej                  j                         Z G d de      Z G d dej                         Z G d dej                         Z G d dej                         Z G d dej                         Z G d dej                         Z G d dejz                        Z G d dej                         Z G d dej                         Z G d dej                         Z G d dej                  j                         Z G d dej                         Z G d dej                  j                         Z G d dej                  j                         Z G d dej                  j                         Z G d dej                  j                         Z G d dej                  j                         Z G dÄ dej                  j                         Z G dń dej                  j                         Z G dǄ dej                         Z G dɄ dej                         Z G d˄ dej                         Z G d̈́ dΫ      Zdτ Zy# eK$ r dZJY 	w xY w# eK$ r dZuY 	w xY w)zImporting this file includes common utility methods and base classes for
checking quantization api and properties of resulting modules.
    N)control_flow)_FusedModule)convertdefault_dynamic_qat_qconfigdefault_dynamic_qconfigdefault_dynamic_quant_observerdefault_embedding_qat_qconfigdefault_observerdefault_per_channel_qconfigdefault_qconfig%default_symmetric_qnnpack_qat_qconfigdefault_weight_observerDeQuantStub!float_qparams_weight_only_qconfigget_default_qat_qconfigget_default_qat_qconfig_mappingget_default_qconfigget_default_qconfig_mappingPerChannelMinMaxObserverpropagate_qconfig_QConfigQConfigMappingquantizequantize_dynamic_jitquantize_jit	QuantStub	QuantTypeQuantWrapper))get_default_dynamic_quant_module_mappingsget_default_qat_module_mappings$get_default_qconfig_propagation_list)_load_for_lite_interpreter)override_quantized_engine)TEST_WITH_ROCMTestCase)NSSingleResultValuesType
NSSubgraph)
convert_fxconvert_to_reference_fx
prepare_fxprepare_qat_fx)GraphModule)NodeTF)Any)Callable)	FileCheckc                   X    e Zd ZdZd Zed        Zed        Zed        Zd Z	d Z
d Zy	)
NodeSpecz"Used for checking GraphModule Nodec                      || _         || _        y)z
        op: call_function | call_module
        target:
          for call_function, target would be a function
          for call_module, target would be the type of PyTorch module
        N)optarget)selfr4   r5   s      k/media/conek/DATA/Code/OCR/venv/lib/python3.12/site-packages/torch/testing/_internal/common_quantization.py__init__zNodeSpec.__init__X   s         c                     t        d|      S )Ncall_functionr2   clsr5   s     r7   r;   zNodeSpec.call_functionb   s    00r9   c                     t        d|      S )Ncall_methodr<   r=   s     r7   r@   zNodeSpec.call_methodf       v..r9   c                     t        d|      S )Ncall_moduler<   r=   s     r7   rC   zNodeSpec.call_modulej   rA   r9   c                 D    t        | j                  | j                  f      S N)hashr4   r5   r6   s    r7   __hash__zNodeSpec.__hash__n   s    TWWdkk*++r9   c                     t        |t              st        S | j                  |j                  k(  xr | j                  |j                  k(  S rE   )
isinstancer2   NotImplementedr4   r5   )r6   others     r7   __eq__zNodeSpec.__eq__q   s7    %*!!ww%(("Bt{{ell'BBr9   c                 ^    t        | j                        dz   t        | j                        z   S )N )reprr4   r5   rG   s    r7   __repr__zNodeSpec.__repr__w   s#    DGG}s"T$++%666r9   N)__name__
__module____qualname____doc__r8   classmethodr;   r@   rC   rH   rM   rQ    r9   r7   r2   r2   U   sV    , 1 1 / / / /,C7r9   r2   c                  X    t         j                  j                         r
t        sddgS dgS )Ncpucuda)torchrZ   is_availabler$   rW   r9   r7   get_supported_device_typesr]   {   s*     ::224^RWQXr9   c                     |D ]  } | |  	 y)z
    Default evaluation function takes a torch.utils.data.Dataset or a list of
    input Tensors and run the model on the dataset
    NrW   )model
calib_datainps      r7   test_only_eval_fnrb      s    
  sr9   c                    t         j                  j                  | j                         d      }d\  }}}t	        d      D ]  }| j                          |D ]  \  }}	|j                           | |      }
 ||
|	      }|j                          |j                          ||j                         z  }t        j                  |
d      \  }}||	j                  d      z  }|||	k(  j                         j                         z  }  |||fS )zh
    Default train function takes a torch.utils.data.Dataset and train the model
    on the dataset
    gMbP?lr)r   r   r   
      r   )r[   optimAdam
parametersrangetrain	zero_gradbackwardstepitemmaxsizesum)r_   
train_dataloss_fn	optimizer
train_losscorrecttotal_datar5   outputloss	predicteds                r7   test_only_train_fnr      s    
   !1!1!3 >I!(J2Y :& 		:LD&!4[F66*DMMONN$))+%J 99VQ/LAyV[[^#E	V+0027799G		:: w%%r9   c                   ,    e Zd ZdZddZd ZddZd Zy)	AverageMeterz1Computes and stores the average and current valuec                 @    || _         || _        | j                          y rE   )namefmtreset)r6   r   r   s      r7   r8   zAverageMeter.__init__   s    	

r9   c                 <    d| _         d| _        d| _        d| _        y Nr   )valavgrs   countrG   s    r7   r   zAverageMeter.reset   s    
r9   c                     || _         | xj                  ||z  z  c_        | xj                  |z  c_        | j                  | j                  z  | _        y rE   )r   rs   r   r   )r6   r   ns      r7   updatezAverageMeter.update   s<    C!G

a
88djj(r9   c                 ~    d| j                   z   dz   | j                   z   dz   } |j                  di | j                  S )Nz{name} {valz} ({avgz})rW   )r   format__dict__)r6   fmtstrs     r7   __str__zAverageMeter.__str__   s;    )I5@4Gv}}-t}}--r9   N)z:frg   )rR   rS   rT   rU   r8   r   r   r   rW   r9   r7   r   r      s    ;
).r9   r   c                    t        j                         5  t        |      }|j                  d      }| j	                  |ddd      \  }}|j                         }|j                  |j                  dd      j                  |            }g }|D ]X  }	|d|	 j                  d      j                         j                  dd      }
|j                  |
j                  d|z               Z |cddd       S # 1 sw Y   yxY w)zNComputes the accuracy over the k top predictions for the specified values of kr   rg   TN)keepdimg      Y@)r[   no_gradrq   rr   topkteqview	expand_asfloatrs   appendmul_)r|   r5   r   maxk
batch_sizerz   predrx   resk	correct_ks              r7   accuracyr      s    	 4y[[^
++dAtT24vvx''&++a,66t<= 	;A((,22488D8IIJJy~~ej&89:	;   s   CC33C<c                 ^   | j                          t        |d      D ]  \  }\  }}t        dd       |j                  |      |j                  |      }} | |      }	 ||	|      }
|j	                          |
j                          |j                          t        |	|d       ||k\  s y  y )Nrg   )start. )end)rg      )r   )rl   	enumerateprinttorm   rn   ro   r   )r_   	criterionrv   data_loaderdevicentrain_batchescntimager5   r|   r}   s              r7   train_one_epochr      s    	KKM )+Q ? 
_eVcr(&))F*;vu(f-. 
 r9   c                     dt         j                  d<   dt         j                  d<   t        j                  d| |       y )N	localhostMASTER_ADDR12355MASTER_PORTgloorank
world_size)osenvirondistinit_process_groupr   s     r7   	ddp_setupr      s2     +BJJ} 'BJJ} 	F*Er9   c                  ,    t        j                          y rE   )r   destroy_process_grouprW   r9   r7   ddp_cleanupr      s     r9   c                 \   t        | |       |j                          t        j                  j                  j                  || g      }|j                  |        |}t        j                  j                  |j                         d      }t        |t        |t        | d       t                y )N)
device_idsg-C6?rd   rg   )r   rZ   r[   nnparallelDistributedDataParallelr   rh   SGDrj   r   r   datasetr   )r   r   preparedmodel_with_ddprv   s        r7   run_ddpr      s}    dJMMOxx  88tf8UHKKN 9 9 ;GINIy'4KMr9   c                 0    t        | t               d       y )NTinplace)r   r   )modules    r7   convert_dynamicr      s    F=?Nr9   c                     t        | |       y rE   )r   )r_   qconfig_dicts     r7   prepare_dynamicr      s    ul+r9   c                 4   ||z  }||z  }d\  }}t        j                  ||| |f|z         }|||z
  j                         z  }t        j                  |||t         j                        }||z  }|	|z  }	|d | }|	d | }	d\  }}t        j                  ||||f|z         }t        j                  dd|f      }|rddt        |      z  z   }t        j                  |t         j                        }t        j                  |	t         j                        } |j                  | |j                          |j                  | z
  j                         z  }||z  |j                         z  }t        j                  ||j                         |j                         dt         j                        }nb|d   ||	d   z
  j                         z  }||d   z  |j                         z  }t        j                  ||d   |	d   t         j                        }|||||
r|fS d fS )	Nr      scale
zero_pointdtype)r   r   rf   )r   rg   r   r   )r[   randintr   quantize_per_tensorquint8lentensorreshapequantize_per_channeldoublelongqint8)r   in_channels_per_groupinput_feature_map_sizeout_channels_per_groupgroupskernel_sizeX_scaleX_zero_pointW_scaleW_zero_pointuse_biasuse_channelwisein_channelsout_channelsX_value_minX_value_maxX_initXX_qW_value_minW_value_maxW_initb_initW_shapeW_scales_tensorW_zero_points_tensorWbW_qs                                r7   _make_conv_test_inputr	     sG    (&0K)F2L!'[+]]	
 !		!F 	6L(//11A

#
#	\C $G,.Lm|$G.L ")[+ ]]!	
 		F ]]1b</2FD3{#333,,wekkB$||LL#O##W-||~ < 4 < <g FFMMOP 	
 o%6((""$ %%'++
 AJ&<?299;;gaj 6<<>1''WQZLO5;;
 sAsA44t44r9   c                     d\  }}t        j                  |||      }| ||z
  j                         z  }t        j                  || |t         j                        }||fS )Nr   r   )r[   r   r   r   r   )r   r   sizesr   r   r   r   r   s           r7   !_make_conv_add_extra_input_tensorr  P  se    !'[+]]F
 	*$++--A

#
#	:U\\C c6Mr9   c                      dt         t              r6dt        j                  j                  j
                  vrd _         _         S t        j                          fd       }|S )NzrQuantized operations require FBGEMM. FBGEMM is only optimized for CPUs with instruction set support AVX2 or newer.fbgemmTc                      dt         j                  j                  j                  vrt	        j
                         | i | y )Nr  r[   backends	quantizedsupported_enginesunittestSkipTestargskwargsfnreasons     r7   wrapperzskipIfNoFBGEMM.<locals>.wrapperf  9    5>>33EEE##F++r9   
rJ   typer[   r  r  r  __unittest_skip____unittest_skip_why__	functoolswrapsr  r  r  s   ` @r7   skipIfNoFBGEMMr$  ^  sd     BF"d5>>33EEE#'B '-B$	__R    Nr9   c                      dt         t              r6dt        j                  j                  j
                  vrd _         _         S t        j                          fd       }|S )N%Quantized operations require QNNPACK.qnnpackTc                      dt         j                  j                  j                  vrt	        j
                         | i | y Nr'  r  r  s     r7   r  z skipIfNoQNNPACK.<locals>.wrapperx  s9    ENN44FFF##F++r9   r  r#  s   ` @r7   skipIfNoQNNPACKr*  p  sa    4F"dENN44FFF#'B '-B$	__R    Nr9   c                      dt         t              r6dt        j                  j                  j
                  vrd _         _         S t        j                          fd       }|S )Nr&  r'  Tc                      dt         j                  j                  j                  vrt	        j
                        t        d      5   | i | d d d        y # 1 sw Y   y xY wr)  )r[   r  r  r  r  r  r#   r  s     r7   r  z#withQNNPACKBackend.<locals>.wrapper  sV    ENN44FFF##F++&y1 	 	  	  	 s   	AA#r  r#  s   ` @r7   withQNNPACKBackendr-    sc     5F"dENN44FFF#'B '-B$	__R    Nr9   c                      dt         t              r6dt        j                  j                  j
                  vrd _         _         S t        j                          fd       }|S )Nz$Quantized operations require ONEDNN.onednnTc                      dt         j                  j                  j                  vrt	        j
                         | i | y )Nr/  r  r  s     r7   r  zskipIfNoONEDNN.<locals>.wrapper  r  r9   r  r#  s   ` @r7   skipIfNoONEDNNr1    sa    3F"d5>>33EEE#'B '-B$	__R    Nr9   c                      dt         t              r8t        j                  j                  j                         sd _         _         S t        j                          fd       }|S )Nz*Quantized operations require BF16 support.Tc                      t         j                  j                  j                         st	        j
                         | i | y rE   )r[   opsmkldnn_is_mkldnn_bf16_supportedr  r  r  s     r7   r  z#skipIfNoONEDNNBF16.<locals>.wrapper  s7    yy99;##F++r9   )
rJ   r  r[   r4  r5  r6  r  r   r!  r"  r#  s   ` @r7   skipIfNoONEDNNBF16r7    s_    9F"dyy99;#'B '-B$	__R    Nr9   c                      dt         t              r6dt        j                  j                  j
                  vrd _         _         S t        j                          fd       }|S )Nz!Quantized operations require X86.x86Tc                      dt         j                  j                  j                  vrt	        j
                         | i | y )Nr9  r  r  s     r7   r  zskipIfNoX86.<locals>.wrapper  s9    00BBB##F++r9   r  r#  s   ` @r7   skipIfNoX86r;    sa    0F"d00BBB#'B '-B$	__R    Nr9   c                      dt         t              r$t        j                         sd _         _         S t        j                          fd       }|S )Nzdynamo doesn't support.Tc                  h    t        j                         st        j                         | i | y rE   )torchdynamois_dynamo_supportedr  r  r  s     r7   r  z&skipIfNoDynamoSupport.<locals>.wrapper  s-    ..0##F++r9   )rJ   r  r>  r?  r  r   r!  r"  r#  s   ` @r7   skipIfNoDynamoSupportr@    sU    &F"d..0#'B '-B$	__R    Nr9   c                      dt         t              r$t        j                         sd _         _         S t        j                          fd       }|S )Nzinductor doesn't support.Tc                  h    t        j                         st        j                         | i | y rE   )r>  is_inductor_supportedr  r  r  s     r7   r  z(skipIfNoInductorSupport.<locals>.wrapper  s-    002##F++r9   )rJ   r  r>  rC  r  r   r!  r"  r#  s   ` @r7   skipIfNoInductorSupportrD    sU    (F"d002#'B '-B$	__R    Nr9   zno torchvisionc                     |r t         j                  j                  | |      S t         j                  j                  |       S rE   )r[   jittracescript)r_   tracingr{   s      r7   get_script_modulerJ    s-    +2599??5$'O		8H8H8OOr9   c                     t        j                  | j                  d   dz   f|      }| |dd t        j                  t        j
                  ||            }|r|dd S |dd S )z6
    Convert lengths to offsets for embedding_bag
    r   rg   r   Nr   )npzerosshaper[   
from_numpycumsum)r   offset_typeuse_begin_offsettts       r7   lengths_to_offsetsrT    sb     
1771:>#;	7BBqrF			"))Bk:	;B#2wab6Mr9   c           	         | j                         dk7  rt        d| j                                | j                  dd      j                         } |dk  rt        d|       | j                  d   |z  dk7  rt        d| j                  d    d|       | j                  d|      }t        j                  |      j                         dk7  rt        d	      |j                  dd
      }|j                  dd
      }d|z  dz
  }d}||z
  j                  d      |z  }t        j                  |      j                         dk7  rt        d      ||d|dz
  z  z  z   }	t        j                  |	      j                         dk7  rt        d      |j                  |      j                  |      j                         j                  ||      }
t        j                  |
      j                         dk7  rt        d      |
j!                  t        j"                        j                  | j                        }
|
j$                  t        j$                  d      k7  r9|
d d d d df   dz  |
d d dd df   z  j!                  t        j&                        }
|j)                  | j                  d   d      }|	j)                  | j                  d   d      }	t        j*                  |j                  |j-                  d      |j-                  d      d      |	j                  |	j-                  d      |	j-                  d      d      gd      j                  dd      j                         }|
|fS )N   zexpected w.dim() == 2, got r   rg   zexpected q_group_size > 1, got r   z:expected w.shape[-1] % q_group_size == 0, got w.shape[-1]=z, q_group_size=zto_quant contains NaN valuesTdimr   gư>minzscales contains NaN valueszzeros contains NaN valueszout contains NaN valuesr   rY   r   )rX  AssertionError	transpose
contiguousrN  r   r[   isnanrs   amaxaminclampsubdivroundclamp_r   int32r   uint8r   catrr   )wn_bitq_group_sizeto_quantmax_valmin_valmax_intmin_intscalesrM  outscales_and_zeross               r7   _group_quantize_tensorrt  	  s   uuw!|:1557)DEE	Aq$$&Aq>|nMNNwwr{\!Q&HQSUdeqdrs
 	
 yy\*H{{8  "a';<<mm4m0Gmm4m0GhlGG&&4&07:F{{6 A%9::feai 011E{{51$899
,,w

#
#F
+
1
1
3
:
:7G
LC{{3"677
&&u{{&
#
+
+AGG
4C
zzU\\%((2ss7|q 3r14a4x=044U[[A [[R(FJJqwwqz2&E		v{{1~v{{1~qAejjmUZZ]A> 	
 
1a	     r9   c                    | j                  dd      j                         } | j                         dk7  rt        d| j                                |dk  rt        d|       | j                  d   |z  dk7  rt        d| j                  d    d|       | j                  d|      }|j                         j                  dd	
      }t        j                  |j                        j                  }d|dz
  z  dz
  }|j                  |      |z  }t        j                  |      }|j                  t        j                        j                  | j                  d   d      }|j                  t        j                        j                  | j                  d   d      }|j                  dd      }|j                  dd      }d|z  dz
  }|j!                  |      j#                  d      j                  t        j$                        j                  |      }	|	d d dd df   dz  |	d d d d df   z  j                  t        j&                        }
|j)                         j                         }|
|fS )Nr   rg   rV  zExpected w.dim() == 2, got zExpected groupsize > 1, got r   z+Expected w.shape[-1] % groupsize == 0, got z % TrW  rY  g      !@)rq   r   )r\  r]  rX  r[  rN  r   absr_  r[   finfor   epsra  
zeros_liker   float32rc  addint8rg  squeeze)ri  rj  	groupsizerl  rm  rx  ro  rq  rM  w_int8	out_uint8rs  s               r7    _group_quantize_tensor_symmetricr  ?  s"    	
Aq$$&Auuw!|:1557)DEEA~;I;GHHwwr{Y!#J177SU;-WZ[dZefggyyY'Hlln!!a!6G
++gmm
$
(
(CEAI"G]]s]#g-FV$E YYu}}%--aggaj"=FHHU]]#++AGGAJ;E^^B"FMM"a EhlG\\&!%%c*--ejj9??G?LF ADqD!Q&CaC8<<U[[II~~'224&&&r9   c                    | j                   }| j                         } t        j                  t        j                        j
                  }t        j                  | d      \  }}t        j                  |t        j                  |            }t        j                  |t        j                  |            }	|j                  }
t        j                  | |	      }	|	t        ||z
        dz  z  }t        j                  ||      j                  | j                         }t        j                  |j                         t        j                  |
      }| |j!                  d      z  }t        j"                  |      }||j!                  d      z   }t        j                  |||      j                  |      }||j                  |      |fS )Nrg   rX  rV  rY  )r   r   r   )r   r   r[   rw  rz  rx  aminmaxrZ  ry  rq   r   ra  r   rM  rr   int64	unsqueezerd  )x	quant_min	quant_maxtarget_dtypex_dtyperx  rn  rm  min_val_negmax_val_posr   rq  zero_pointsx_divx_roundx_zpquants                    r7   !_dynamically_quantize_per_channelr  a  sb    ggG		A
++emm
$
(
(C }}QA.GW ))GU%5%5g%>?K))GU%5%5g%>?KF ))[L+6KE)i"781<=F[[S),,QWW5F++k..0FSK   $$Ekk% G[**2..DKKi366|DE&))G$k11r9   c            
       z    e Zd Z fdZd Zd Zd Z	 d!dZd Zd Z	d	 Z
d
 Zd Zd Zd Zd Zd Zd Zd"dZd Zd Z	 	 	 	 	 	 d#dZ	 	 	 d$dZd%dZerRdeeeeef   f   deeeeeef   eeef   f   f   dededdf
dZdeeeeeee f   f   f   ddfdZ!	 	 	 	 	 	 	 	 	 	 	 d&dZ"e#jH                  fd Z% xZ&S )'QuantizationTestCasec                    t         |           t        d      D cg c])  }t        j                  ddt        j
                        g+ c}| _        t        d      D cg c]O  }t        j                  ddt        j
                        t        j                  dddt        j                        gQ c}| _	        t        d      D cg c]*  }t        j                  dddt        j
                        g, c}| _
        t        d      D cg c]+  }t        j                  ddddt        j
                        g- c}| _        t        d      D cg c],  }t        j                  dddddt        j
                        g. c}| _        t        d      D cg c]P  }t        j                  dddt        j
                        t        j                  ddd	t        j                        gR c}| _        t        d      D cg c]Q  }t        j                  ddddt        j
                        t        j                  ddd	t        j                        gS c}| _        t        d      D cg c]R  }t        j                  dddddt        j
                        t        j                  ddd	t        j                        gT c}| _        | j                  | j                  | j                  d
| _        t"        j$                  t"        j&                  g| _        t"        j*                  t"        j$                  t"        j&                  g| _        y c c}w c c}w c c}w c c}w c c}w c c}w c c}w c c}w )NrV  r   r   r   rg   )rV     rf   r   rg   rV  r  )supersetUprk   r[   randr   r`   r   r   rt   img_data_1dimg_data_2dimg_data_3dimg_data_1d_trainimg_data_2d_trainimg_data_3d_trainimg_data_dictr   STATICQATstatic_quant_typesDYNAMICall_quant_types)r6   rz   	__class__s     r7   r  zQuantizationTestCase.setUp  s   JOPQ(SQEJJq!5;;?@S 1X

  

1au{{3aD

;
 PUUVxX!UZZ1bDEXCH8
>?UZZ1b"EKK89
 EJ!H
?@UZZ1aAU[[9:
 1X"

  

1a5;;7aD

;"
 1X"

  

1aRu{{;aD

;"
 1X"

  

1aAq<aD

;"
 
 $-#3#3Y]]"C ) 1 193C3CY]]S] T
 Y

"
"
"
s3   .M AM	/M0M1M#AM(AM--AM2c                 p    | j                  t        |d             | j                  t        |d             y)zChecks the module does not contain child
        modules for quantization preparation, e.g.
        quant, dequant and observer
        r  dequantN)assertFalsehasattrr6   r   s     r7   checkNoPrepModulesz'QuantizationTestCase.checkNoPrepModules  s.    
 	1234r9   c                     | j                  t        |d             |j                         D ]  }| j                  |        y)z*Checks the module does not contain qconfigqconfigN)r  r  childrencheckNoQconfig)r6   r   childs      r7   r  z#QuantizationTestCase.checkNoQconfig  s;    34__& 	'E&	'r9   c                     | j                  t        |d             | j                  t        |d             | j                  t        |d             y)zChecks the module contains child
        modules for quantization preparation, e.g.
        quant, dequant and observer
        r   r  r  N)
assertTruer  r  s     r7   checkHasPrepModulesz(QuantizationTestCase.checkHasPrepModules  s=    
 	1201	23r9   Nc           	         |
t               }|i }|j                  di       }d }t        |d      r|j                   ||      r1t	        |t
        j                  j                        st        |      |v st        |      |v rbt	        |t
        j                  j                  j                        s4| j                  t        |d      dt        t        |            z   dz          t        |      t               j                         vrrt        |      |j                         vrVt	        |t               sE|j#                         D ]1  }t        |      t        j$                  u r| j'                  |||       3 yyyy)znChecks the module or module's leaf descendants
        have observers in preparation for quantization
        N%float_to_observed_custom_module_classc                 V    d}| j                         D ]  \  }}|dk7  s|dz  } |dk(  S )Nr   activation_post_processrg   )named_children)r   submodule_name_countr   rz   s       r7   is_leaf_modulez;QuantizationTestCase.checkObservers.<locals>.is_leaf_module  sE    #$ !002 .a44(A-(. (1,,r9   r  r  zmodule: z do not have observer)r!   getr  r  rJ   r[   r   
Sequentialr  aoquantizationr   r  strr    valuesr   r  DropoutcheckObservers)r6   r   propagate_qconfig_listprepare_custom_config_dict&float_to_observed_module_class_mappingr  r  s          r7   r  z#QuantizationTestCase.checkObservers  s\    ")%I%K"%-)+&1K1O1O3R2
.
	- FI&* #6*&vuxx/B/BCV(>><#IIvuxx'<'<'H'HIOO 9:Sf..1HH L ? A H H JJV$J$Q$Q$SSv|4* ;"**,##13M 5 T Kr9   c                     | j                  t        |j                        t        j                         | j                  t        |j
                        t        j                         y)zVChecks that mod has nn.Quantize and
        nn.DeQuantize submodules inserted
        N)assertEqualr  r  nnqQuantizer  
DeQuantizer6   mods     r7   checkQuantDequantz&QuantizationTestCase.checkQuantDequant  s=     	cii#,,7ckk*CNN;r9   c                     | j                  t        |j                        t        j                         | j                  |       y)zChecks that mod has been swapped for an nnq.Linear
        module, the bias is qint32, and that the module
        has Quantize and DeQuantize submodules
        N)r  r  r   r  Linearr  r  s     r7   checkWrappedQuantizedLinearz0QuantizationTestCase.checkWrappedQuantizedLinear	  s/    
 	cjj)3::6s#r9   c                 V    | j                  t        |      t        j                         y rE   )r  r  r  r  r  s     r7   checkQuantizedLinearz)QuantizationTestCase.checkQuantizedLinear  s    cCJJ/r9   c                     | j                  t        |      t        j                         | j                  |j                  j
                  |       y_Checks that mod has been swapped for an nnqd.Linear
        module, the bias is float.
        N)r  r  nnqdr  _packed_paramsr   r6   r  r   s      r7   checkDynamicQuantizedLinearz0QuantizationTestCase.checkDynamicQuantizedLinear  s8     	cDKK0++1159r9   c                     | j                  t        |      t        j                         | j                  |j                  j
                  |       yr  )r  r  nniqd
LinearReLUr  r   r  s      r7   checkDynamicQuantizedLinearReluz4QuantizationTestCase.checkDynamicQuantizedLinearRelu  s:     	cE$4$45++1159r9   c                     |j                         }t        j                         }t        j                  ||       |j                  d       t        j                  |d      }|j                  |        || } || } fd}	 |	||       t        j                         }t        j                  ||       |j                  d       t        j                  |d      }
 |
| } |	||       y )Nr   F)weights_onlyc                    j                  | d   |d          t        | d   t              r=j                  | d   d   |d   d          j                  | d   d   |d   d          y j                  | d   |d          y )Nr   rg   )r  rJ   tuple)ref_outload_outr6   s     r7   check_outputszEQuantizationTestCase.check_eager_serialization.<locals>.check_outputs.  s    WQZ!5'!*e,  AA?  AA?  Xa[9r9   )
state_dictioBytesIOr[   saveseekloadload_state_dict)r6   	ref_modelloaded_modelr  
model_dictr  loaded_dictr  r  r  loadeds   `          r7   check_eager_serializationz.QuantizationTestCase.check_eager_serialization"  s    ))+
JJL

:q!	q	jj7$$[1Q-#	: 	gx(JJL

9a 	q	AE21:gx(r9   c                     |j                         }|j                         }| j                  ||j                         z  t	                      | j                  ||j                         z  t	                      y rE   )
get_weightget_biasr  keysset)r6   r  weight_keys	bias_keysweightbiass         r7   check_weight_bias_apiz*QuantizationTestCase.check_weight_bias_api?  sX    %%'!!#v{{}4ce<TYY[0#%8r9   c                    t         j                  dt         j                  di}| j                  t	        |      |       |j
                  D ]5  }| j                  |j                  j                         d   d   ||          7 y)zbChecks that mod has been swapped for an nnqd.LSTM type
        module, the bias is float.
        quantized_dynamicquantized_fp16r   N)r[   r   float16r  r  _all_weight_valuesparam__getstate__r6   r  reference_module_typer   wt_dtype_mappacked_paramss         r7   checkDynamicQuantizedLSTMz.QuantizationTestCase.checkDynamicQuantizedLSTME  s|    
 KK,MM+
 	c$9: 33 	M##00215a8,u:M	r9   c                 j    | j                  t        |      t        j                  j                         y rE   )r  r  r[   r   r  r  s     r7   checkLinearz QuantizationTestCase.checkLinearS  s    cEHHOO4r9   c                     t         j                  dt         j                  di}| j                  t	        |      |       t        |d      rE|j                  D ]5  }| j                  |j                  j                         d   d   ||          7 yy)r  r   r  r  r   N)	r[   r   r  r  r  r  r  r  r  r  s         r7   checkDynamicQuantizedModulez0QuantizationTestCase.checkDynamicQuantizedModuleV  s    
 KK,MM+
 	c$9:3,-!$!7!7   !''446q9!<l5>Q .r9   c                     t         j                  j                  |      }| j                  ||||       t         j                  j	                  ||d         }| j                  ||||       y r   )r[   rF  rH  _checkScriptablerG  )r6   orig_modr`   check_save_loadscriptedtraceds         r7   checkScriptablez$QuantizationTestCase.checkScriptablee  sX    99##H-h*oN :a=9h
OLr9   c                    | j                  |||       t        j                         }t        j                  j                  ||       |j                  d       t        j                  j                  |      }|r| j                  |||       y y r   )"_checkModuleCorrectnessAgainstOrigr  r  r[   rF  r  r  r  )r6   r  
script_modr`   r  buffer
loaded_mods          r7   r  z%QuantizationTestCase._checkScriptablen  sl    //*jQ 		z6*AYY^^F+
 33Hj*U r9   c                 J    |D ]  } || } || }| j                  ||         y rE   )r  )r6   r  test_modr`   ra   
ref_outputscripted_outputs          r7   r  z7QuantizationTestCase._checkModuleCorrectnessAgainstOrig|  s3     	:C!3J&nO_j9	:r9   c
           	         |rt        dt        |             dt        t        j                  j
                  j                        i}
|r|j                         }|rd|	t        n|	i}
t        |||d         j                         }|rt        d|j                         i }i }dD ]`  }|r t        ||
|      ||<    ||   |      ||<   %t        j                  |      }t        ||
t        |gd|      ||<    ||   |d    ||<   b |r2t        d	|d
   j                         t        d|d   j                         |rM| j!                  |d
   |d          t#               j%                  |      j'                  |d   j                         |d   S )NzTesting:r   r   zinput graph:)TF)debugF)r   r   zdebug graph:Tznon debug graph:)r   r  r   r[   r  r  engineevalr   rJ  graphr   copydeepcopyr   rb   r  r0   checkrun)r6   r   inputsquantized_oprI  r   r&  	eval_modedynamicr  r   r_   modelsoutputsinputs_copys                  r7   checkGraphModeOpz%QuantizationTestCase.checkGraphModeOp  s    *c&k*/0H0H0O0OPQ[[]F7? 7PWXL!&'6!9=BBD.%++." 	;E 4ULPU Vu!.v!6 #mmF3 , % M!!u "/q	!:%	;( .&,"4"45$fUm&9&9:WT]GEN; Kl+//u0C0CDe}r9   c           	      <   i }g }t        |j                  d            }|j                  j                  D ]  }d}	|j                  dk(  s|j                  dk(  r!t        |j                  |j                        }	n;|j                  dk(  r,t        |j                  t        ||j                                 }	|	|j                  |	       |	|v r||	xx   dz  cc<   d||	<    |#| j                  ||v dt        |      z   d	z          ||j                         D ]  \  }}
|
d
k7  ri| j                  ||v dt        |      z   dz          | j                  ||   |
k(  dt        |      z   dz   t        |
      z   dz   t        ||         z          t| j                  ||vdt        |      z   dz           |id
}|D ]   }	|t        |      k(  r y|	||   k(  s|dz  }" | j                  |t        |      k(  d| j                  |d      z   dz   t        |      z          yy)a   Check if GraphModule contains the target node
        Args:
            graph_module: the GraphModule instance we want to check
            expected_node, expected_node_occurrence, expected_node_list:
               see docs for checkGraphModeFxOp
        Fremove_duplicateNr;   r@   rC   rg   znode:z not found in the graph moduler   zCheck failed for node:z
 not foundz Expected occurrence:z Found occurrence:z! expected no occurrence but foundzCheck failed for graph:)	print_strzExpected ordered list:)dictnamed_modulesr#  nodesr4   r2   r5   r  r   r  r  itemsr   printGraphModule)r6   graph_moduleexpected_nodeexpected_node_occurrenceexpected_node_listnodes_in_graph	node_listmodulesnoder   
occurrence	cur_indexs               r7   checkGraphModuleNodesz*QuantizationTestCase.checkGraphModuleNodes  sw    	|1151IJ &&,, 	*DAww/)TWW-ETWWdkk2M)TWWd74;;+?&@A}  #&"1%*%()N1%	* $OO/#m,,/OO
 $/-E-K-K-M )z?OO%703}3EET OO&}5C0m,-12 j/* /	/
 n];<= OO%^;0m,-=>!. )I #$6 77*955NI	#
 OOS!344)'''FG*+ ()* *r9   c                    t        |j                  d            }g }|j                  j                  D ]  }dj	                  t        t        |j                  |j                  |j                  |j                  |j                  g            }|j                  dk(  r'|dt        t        ||j                                 z   z  }|j                  |        dj	                  |      }|rt        |       |S )NFr1  rO   rC   z module type: 
)r4  r5  r#  r6  joinmaprP   r4   r   r5   r  r  r  r   r   )r6   r9  r3  r?  
node_infosr   	node_infostr_to_prints           r7   r8  z%QuantizationTestCase.printGraphModule  s    |1151IJ
##)) 	)ATADD!&&!((AFFAHH+U!VWItt}$-T'!((:K5L0MMM	i(		)
 yy,,r9   matched_subgraph_pairsexpected_typesgm_agm_breturnc                 $   dt         dt        dt        t        z  fd}| j	                  t        |      t        |      k(  dt        |       dt        |              |j                         D ]  \  }}|\  }}	|\  }
}|	\  }}||   \  }} ||j                  |      } ||j                  |      } ||j                  |      } ||j                  |      }|
|u xr ||u xr
 ||u xr ||u }| j	                  |d| d|
|||f d	||||f         y
)a  
            Verifies that the types specified in expected_types match
            the underlying objects pointed to by the nodes in matched_subgraph_pairs.

            An example successful test case:

              matched_subgraph_pairs = {'x0': (graph_a_conv_0_node, graph_b_conv_0_node)}
              expected_types = {'x0': (nn.Conv2d, nnq.Conv2d)}

            The function tests for key equivalence, and verifies types with
            instance checks.
            r@  gmrO  c                     | j                   dk(  r!t        || j                        }t        |      S | j                   dvrt	        d| j                         | j                  S )NrC   )r;   r@   z:Expected node.op in ('call_function', 'call_method'), got )r4   getattrr5   r  r[  )r@  rQ  r  s      r7   _get_underlying_op_typez]QuantizationTestCase.assert_types_for_matched_subgraph_pairs.<locals>._get_underlying_op_type,  s\     77m+!"dkk2C9$ww&FF,/ijnjqjqit-uvv;;&r9   z-Expected length of results to match, but got  and zType mismatch at z: expected z, got N)	r-   r,   r/   r  r  r   r7  
start_nodeend_node)r6   rK  rL  rM  rN  rT  r   vexpected_types_aexpected_types_bexp_type_start_aexp_type_end_aexp_type_start_bexp_type_end_b
subgraph_a
subgraph_bact_type_start_aact_type_start_bact_type_end_aact_type_end_btypes_matchs                        r7   'assert_types_for_matched_subgraph_pairsz<QuantizationTestCase.assert_types_for_matched_subgraph_pairs  s   ,	'	' +	'C	' OO*+s>/BB?DZ@[?\\abeftbuavw ',,. 1562 "23C0 .3C0 .)?)B&
J#::;P;PRV#W #::;P;PRV#W !89L9Ld!S!89L9Ld!S%)99 ;'>9;)-==; (>9	  's+7GYiky6z5{ |,n>NP^_`b!r9   act_compare_dictc                 (   |j                         D ]  \  }}|j                         D ]  \  }}| j                  t        |      dk(  d| d       |j                         \  }}t	        t        ||               D ]  }||   |   }	||   |   }
| j                  |	d   |	d   k(  d| d| d| d       | j                  t        |	d         t        |
d         k(  d| d| d| d	       |t
        j                  j                  k(  xr d
|	d   v xs d
|
d   v }|sEt	        t        |	d               D ]*  }|	d   |   }|
d   |   }t        |t        j                        r;| j                  |j                  |j                  k(  d| d| d| dd| dz          it        |t              rE|d   }|d   }| j                  |j                  |j                  k(  d| d| d| dd| dz          t        |t              st        dt        |             t        |      dk7  rt        dt        |             t        |d         dk7  rt        dt        |d                |d   j                  |d   j                  k7  r+t        d|d   j                   d|d   j                         |d   d   j                  |d   d   j                  k7  r1t        d|d   d   j                   d|d   d   j                         |d   d   j                  |d   d   j                  k7  st        d|d   d   j                   d|d   d   j                          |	d   }|
d   }|	d   }|
d   }|	d   t
        j                   j                  k(  r*| j                  ||k(         | j                  ||k(         E|	d   t
        j"                  j                  k(  sg| j                  ||k7         | j                  ||k7            y)a  
            Verifies that the act_compare_dict (output of Numeric Suite APIs) is valid:
            1. for each layer, results are recorded for two models
            2. number of seen tensors match
            3. shapes of each pair of seen tensors match
            rV  zLayer z) does not have exactly two model results.r  z, rU  z do not have the same type.r  z- do not have the same number of seen Tensors.conv1dprev_node_target_typerO   zhave a shape mismatch at idx r   r   zunhandled type z!Expected len(values_0) == 2, got rg   z$Expected len(values_0[1]) == 2, got z5Expected values_0[0].shape == values_1[0].shape, got z != z;Expected values_0[1][0].shape == values_1[1][0].shape, got z;Expected values_0[1][1].shape == values_1[1][1].shape, got ref_node_nameprev_node_nameN)r7  r  r   r  rk   r&   WEIGHTvaluerJ   r[   TensorrN  listr  r[  r  NODE_OUTPUT
NODE_INPUT)r6   rg  
layer_nameresult_type_to_dataresult_type
layer_datamodel_name_0model_name_1res_idxlayer_data_0layer_data_1is_weight_functional_conv1didxvalues_0values_1ref_node_name_0ref_node_name_1prev_node_name_0prev_node_name_1s                      r7   assert_ns_compare_dict_validz1QuantizationTestCase.assert_ns_compare_dict_validQ  s    4D3I3I3K VQ/
//B/H/H/J UQ+KOOJ1, ,UV 2<1B.L,#(Z-E)F#G OQ'1,'?'H'1,'?'H(0L4HH$ZL<.l^Sno
 X 673|H?U;VV$ZL<.l^  TA  B (+C+J+J+P+PP  (L9P,Q Q !U#+|<S/T#T	 4  ;',Sh1G-H'I &*+7+A#+F+7+A#+F#-h#E$(OO(0(..(H*0B|nER^Q__`(a,I#a*P)Q%&
 &0$%?/7{H/7{H$(OO(0(..(H*0B|nER^Q__`(a,I#a*P)Q%& ,6h+F.<tT\~N^=_.`(`'*8}'9.<?`ademan`o=p.q(q'*8A;'71'<.<?cdghpqrhsdtcu=v.w(w'/{'8'8HQK<M<M'M.</33;A;3D3D2ET(ST+J[J[I\-^/* )* (0{1~';';x{1~?S?S'S.</33;A;q>3G3G2HXVW[YZ^MaMaLb-d/* )* (0{1~';';x{1~?S?S'S.</33;A;q>3G3G2HXVW[YZ^MaMaLb-d/* )*G&*R +7*G*6*G+78H+I(+78H+I((07CCIIJ !OOO?O,OP OOO?O,OP(07BBHHI !OOO?O,OP OOO?O,OP_OQUQVQr9   c                 B   t        |      t        u r|d   }|t        j                  k(  r>t	        t
        j                  j                  j                        }|j                          n|t        j                  k(  r>t        t
        j                  j                  j                        }|j                          n/t        }t               j                  |      }|j                          |t        j                  k(  rt         }nt"        }|	Jt        |	      t        t$        fvrt'        d      t)        |	t              r|	}nt        j*                  |	      } ||||||      }|t        j,                  k7  r ||  |r8t/                t/        d|       t/        d|       t/                t/        d|       | j1                  ||
||       t3        j4                  |      }t7        t3        j4                  |            }t9        t3        j4                  |            } || } || }t3        j4                  |      }t3        j4                  |      }|r|n|}|r1t/                t/        d|       | j;                  |       t/                | j1                  ||||       |||||dS )	a0  Quantizes model with graph mode quantization on fx and check if the
            quantized model contains the quantized_node

            Args:
                model: floating point torch.nn.Module
                inputs: one positional sample input arguments for model
                expected_node: NodeSpec
                    e.g. NodeSpec.call_function(torch.quantize_per_tensor)
                expected_node_occurrence: a dict from NodeSpec to
                    expected number of occurrences (int)
                    e.g. {NodeSpec.call_function(torch.quantize_per_tensor) : 1,
                            NodeSpec.call_method('dequantize'): 1}
                expected_node_list: a list of NodeSpec, used to check the order
                    of the occurrence of Node
                    e.g. [NodeSpec.call_function(torch.quantize_per_tensor),
                            NodeSpec.call_module(nnq.Conv2d),
                            NodeSpec.call_function(F.hardtanh_),
                            NodeSpec.call_method('dequantize')]
                is_reference: if True, enables reference mode
                print_debug_info: if True, prints debug info
                custom_qconfig_dict: overrides default qconfig_dict
                prepare_expected_node: same as expected_node, but for prepare
                prepare_expected_node_occurrence: same as
                    expected_node_occurrence, but for prepare
                prepare_expected_node_list: same as expected_node_list, but
                    for prepare

            Returns:
                A dictionary with the following structure:
               {
                   "prepared": ...,  # the prepared model
                   "quantized": ...,  # the quantized non-reference model
                   "quantized_reference": ...,  # the quantized reference model
                   "result": ...,  # the result for either quantized or
                                   # quantized_reference model depending on the
                                   # is_reference argument
               }
            r   z8custom_qconfig_dict should be a QConfigMapping or a dict)example_inputsprepare_custom_configbackend_configzquant type:
zoriginal model:
zprepared model:
zquantized model:
)r   r  quantized_referencequantized_outputquantized_reference_output)r  rp  r   r  r   r[   r  r  r!  rl   r  r   r"  r   r   
set_globalr+   r*   r4  r[  rJ   	from_dictr  r   rC  r$  r%  r(   r)   r8  )r6   r_   r(  
quant_typer:  r;  r<  is_referenceprint_debug_infocustom_qconfig_dictprepare_expected_node prepare_expected_node_occurrenceprepare_expected_node_listr  r  qconfig_mappingr  preparer   prepared_copyqgraphqgraph_referenceresultresult_referenceqgraph_copyqgraph_reference_copyqgraph_to_checks                              r7   checkGraphModeFxOpz'QuantizationTestCase.checkGraphModeFxOp  sI   p F|t#Y]]*"ANN,,33# y///"=NN,,33# 

1"0"2"="=g"F

Y]]*($ #.+,^T4JJ()cdd1>B&9O&4&>&>?R&SO%&;-H Y...&!oz2)51)84&&%0*	 !MM(3Mh 78F6t}}X7NOV_F/8--/K$(MM2B$C!2>.FO*O<%%o6&&("	 *('<$*.> r9   c	                    |r||g}	n|g}	|j                         }
t        j                         }t        j                  |
|       |j                  d       t        j                  |      }t        j                  j                  j                  }|
D ]  }t        |
|   t        j                  j                        s+t        ||   t        j                  j                        st        d|d       ||
|         } |||         }| j                  ||        |rt        j                   ||dd|      }nt        j"                  |||      }| j%                  |||	       |j'                  |       | j                   ||j(                  j*                         ||j(                  j*                               | j-                  ||	gd       |r%t        j.                  j!                  ||dd	d
      }n!t        j.                  j#                  ||      }|r<t1        j2                  |t        j4                  d      }t7        t8        |      |_        t=        |        ||	  |r"t        j                   j?                  |      }d}n!t        j"                  j?                  |      }d} ||	  | jA                  |tC        |      v        y )Nr   zExpected loaded_dict[z] to be ScriptObjectTrs   )num_embeddingsembedding_diminclude_last_offsetmoder   )r  r  r   )r  Fr  r  r  scale_grad_by_freqr  r  r  )r   qschemech_axis
activationr  QuantizedEmbeddingBagQuantizedEmbedding)"r  r  r  r[   r  r  r  r4  r  embedding_bag_unpackrJ   _CScriptObjectr[  r  r  EmbeddingBag	Embeddingr  r  r  _packed_weightr  r   r   	with_args per_channel_affine_float_qparamsr   r   r  r   
from_floatr  r  )r6   qembr  r  indicesoffsetsset_qconfig
is_emb_bagr   r(  emb_dictr  r  embedding_unpackkey
emb_weightloaded_weightloaded_qembfloat_embeddingfloat_qparams_observerq_embeddingbagexpected_names                         r7   checkEmbeddingSerializationz0QuantizationTestCase.checkEmbeddingSerialization>  s    w'FYF??$JJL

8Q	q	jjm 99..CC 	<C(3-)>)>?!+c"2EHH4I4IJ(+@G[)\]]-hsm<
 0S1A B  ];	< **-+$(K ---]RWK 	&&t[&A##K0T00??@[77FFG	
 	TF8TB #hh33-+$(#( 4 O $hh00-] 1 O %=%G%GU%K%KUV&" '.9BX'O# 	(  --88IN3M ]]55oFN0M^)<<=r9   NN)F)FFTTFN)NNNT)NNNFFNNNNNN)'rR   rS   rT   r  r  r  r  r  r  r  r  r  r  r  r  r
  r  r  r  r  r  r/  rC  r8  HAS_FXr4  r  r  r'   r/   r,   rf  r.   r  r  r[   r   r  __classcell__r  s   @r7   r  r    s   0Td5'4 OS3j<$0::):95MV: 8z !%IV 9	$(eJ
4J.K)K$L9	 !U58!34eHh<N6OOPP9	 9	 9	 9	v`	Q"3S$sCx.-@(A#AB`	Q `	QN %)#" $"&-1'+"&I	h llY>r9   r  c                       e Zd Zdeej
                  j                     fdZdej
                  j                  dej                  fdZ	y)QuantizationLiteTestCasemodel_classc                     d}t        |      5  t        j                  j                  j	                  |      } |di |}t        |t        | j                  g      }d d d        |S # 1 sw Y   S xY w)Nr'  rW   )r#   r[   r  r  r   r   rb   r`   )r6   r  r  qenginer  r_   s         r7   _create_quantized_modelz0QuantizationLiteTestCase._create_quantized_model  sm    &w/ 	Jhh++??HG)&)EU$57HIE		J 	J s   AA&&A0r_   inputc                    d}t        |      5  t        j                  j                  |      } ||      }d}t	        d|dz         D ]  }	 t        j                  |j                               }|j                  d       t        |      }	 |	|      }
t        j                  j                  ||
       |	j                  |      }t        j                  j                  ||       |	j                  d|      }t        j                  j                  ||        n d d d        y # t        $ r}||k(  r|Y d }~d }~ww xY w# 1 sw Y   y xY w)Nr'  r   rg   r   forward)r#   r[   rF  rH  rk   r  r  $_save_to_buffer_for_lite_interpreterr  r"   testingassert_closer  
run_methodr[  )r6   r_   r  r  script_modulescript_module_result	max_retryretryr  mobile_modulemobile_module_resultmobile_module_forward_resultmobile_module_run_method_resultes                 r7   _compare_script_and_mobilez3QuantizationLiteTestCase._compare_script_and_mobile  sF   &w/ #	!II,,U3M#0#7 Iq)a-0 !ZZ%JJLF KKN$>v$FM+8+?(MM..,.B 4A3H3H3O0MM..,.J 7D6N6N!573 MM..,.M =#	 #	< & !	) 	!=#	 #	s6   <D>C
D!D>!	D;*D61D>6D;;D>>EN)
rR   rS   rT   r  r[   r   Moduler  ro  r  rW   r9   r7   r  r    s:    	43H 	& & &r9   r  c                   <     e Zd Zd fdZd Zdeedf   fdZ xZS )SingleLayerLinearModelrO  c                     t         |           t        j                  j	                  dd      j                  t        j                        | _        y Nr   r   )r  r8   r[   r   r  r   r   fc1r6   r  s    r7   r8   zSingleLayerLinearModel.__init__  s6    88??1a(++%+++>r9   c                 (    | j                  |      }|S rE   r  r6   r  s     r7   r  zSingleLayerLinearModel.forward      HHQKr9   .c                 0    t        j                  dd      fS Nrg   r   r[   r  rG   s    r7   get_example_inputsz)SingleLayerLinearModel.get_example_inputs      

1a ""r9   rO  N	rR   rS   rT   r8   r  r  r.   r  r  r  s   @r7   r  r    s!    ?#E#s(O #r9   r  c                   <     e Zd Zd fd	Zd Zdeedf   fdZ xZS )AnnotatedSingleLayerLinearModelc                    t         |           t        j                  j                  j                  |      | _        t        t        j                  j                  dd      j                  t        j                              | _        y r  )r  r8   r[   r  r  r   r  r   r   r  r   r   r  r6   r  r  s     r7   r8   z(AnnotatedSingleLayerLinearModel.__init__  sW    xx,,@@I1 5 8 8u{{ 8 KLr9   c                 (    | j                  |      }|S rE   r  r  s     r7   r  z'AnnotatedSingleLayerLinearModel.forward  r  r9   rO  .c                 0    t        j                  dd      fS r  r  rG   s    r7   r  z2AnnotatedSingleLayerLinearModel.get_example_inputs  r  r9   r  r  r  s   @r7   r  r    s"    M
#E#s(O #r9   r  c                   <     e Zd Zd fd	Zd Zdeedf   fdZ xZS )SingleLayerLinearDynamicModelc                    t         |           t        j                  j                  j                  |      | _        t        j                  j                  dd      j                  t        j                        | _        y r  )r  r8   r[   r  r  r   r  r   r  r   r   r  r  s     r7   r8   z&SingleLayerLinearDynamicModel.__init__  sR    xx,,@@I88??1a(++%+++>r9   c                 (    | j                  |      }|S rE   r  r  s     r7   r  z%SingleLayerLinearDynamicModel.forward  r  r9   rO  .c                 0    t        j                  dd      fS r  r  rG   s    r7   r  z0SingleLayerLinearDynamicModel.get_example_inputs  r  r9   r  r  r  s   @r7   r  r    s!    ?
#E#s(O #r9   r  c                   <     e Zd Zd fdZd Zdeedf   fdZ xZS )LinearAddModelrO  c                 .   t         |           t        j                  j	                  dd      j                  t        j                        | _        t        j                  j	                  dd      j                  t        j                        | _        y Nr      r   	r  r8   r[   r   r  r   r   r  fc2r  s    r7   r8   zLinearAddModel.__init__  ^    88??1a(++%+++>88??1a(++%+++>r9   c                 v    | j                  |      }t        j                  |d      }| j                  |      }|S Nr   )r  r[   r{  r  r  s     r7   r  zLinearAddModel.forward  s0    HHQKIIaOHHQKr9   .c                 0    t        j                  dd      fS r  r  rG   s    r7   r  z!LinearAddModel.get_example_inputs  r  r9   r  r  r  s   @r7   r  r    s!    ?
#E#s(O #r9   r  c                   $     e Zd Z fdZd Z xZS )RNNDynamicModelc                 Z   t         |           t        | _        |dk(  rCt        j
                  j                  dd      j                  t        j                        | _	        |dk(  rDt        j
                  j                  dd      j                  t        j                        | _	        y y )NGRUrV  r   LSTM)r  r8   r   r  r[   r   r  r   r   r  r  r6   mod_typer  s     r7   r8   zRNNDynamicModel.__init__  sz    .uxx||Aq),,5;;,?DHvxx}}Q*--EKK-@DH r9   c                 (    | j                  |      }|S rE   r  r  s     r7   r  zRNNDynamicModel.forward  r  r9   rR   rS   rT   r8   r  r  r  s   @r7   r  r    s    Ar9   r  c                   $     e Zd Z fdZd Z xZS )RNNCellDynamicModelc                    t         |           t        | _        |dk(  rCt        j
                  j                  dd      j                  t        j                        | _	        |dk(  rCt        j
                  j                  dd      j                  t        j                        | _	        |dk(  rEt        j
                  j                  ddd      j                  t        j                        | _	        |dk(  rFt        j
                  j                  ddd	      j                  t        j                        | _	        y y )
NGRUCellrV  r   LSTMCellRNNReLUrelu)nonlinearityRNNTanhtanh)r  r8   r   r  r[   r   r  r   r   r  r  RNNCellr  s     r7   r8   zRNNCellDynamicModel.__init__  s    .y xx''1-00u{{0CDHz!xx((A.111DDHy xx''16'BEEEKKEXDHy xx''16'BEEEKKEXDH !r9   c                 (    | j                  |      }|S rE   r  r  s     r7   r  zRNNCellDynamicModel.forward&  r  r9   r  r  s   @r7   r  r    s    
Yr9   r  c                   &     e Zd Zd fd	Zd Z xZS )LSTMwithHiddenDynamicModelc                    t         |           t        j                  j                  j                  |      | _        t        j                  j                  dd      j                  t        j                        | _        y )NrV  r   )r  r8   r[   r  r  r   r  r   r  r   r   lstmr  s     r7   r8   z#LSTMwithHiddenDynamicModel.__init__,  sR    xx,,@@IHHMM!Q'***=	r9   c                 4    | j                  ||      \  }}||fS rE   )r%  )r6   r  hids      r7   r  z"LSTMwithHiddenDynamicModel.forward1  s    1c"3#vr9   r  r  r  s   @r7   r#  r#  +  s    >
r9   r#  c                   <     e Zd Zd fdZd Zdeedf   fdZ xZS )	ConvModelrO  c                     t         |           t        j                  j	                  dddd      j                  t        j                        | _        y Nr  r   Fr  r   )r  r8   r[   r   Conv2dr   r   convr  s    r7   r8   zConvModel.__init__7  s<    HHOOAq!%O8;;%++;N	r9   c                 (    | j                  |      }|S rE   r.  r  s     r7   r  zConvModel.forward;      IIaLr9   .c                 4    t        j                  dddd      fS Nrg   r  r   r  rG   s    r7   r  zConvModel.get_example_inputs?      

1aA&((r9   r  r  r  s   @r7   r)  r)  6  s"    O)E#s(O )r9   r)  c                   <     e Zd Zd fdZd Zdeedf   fdZ xZS )ConvTransposeModelrO  c                     t         |           t        j                  j	                  dddd      j                  t        j                        | _        y r+  )r  r8   r[   r   ConvTranspose2dr   r   r.  r  s    r7   r8   zConvTransposeModel.__init__D  s?    HH,,Q15,ADD5;;DW	r9   c                 (    | j                  |      }|S rE   r0  r  s     r7   r  zConvTransposeModel.forwardH  r1  r9   .c                 4    t        j                  dddd      fS r3  r  rG   s    r7   r  z%ConvTransposeModel.get_example_inputsL  r4  r9   r  r  r  s   @r7   r6  r6  C  s"    X)E#s(O )r9   r6  c                   :     e Zd Z fdZd Zdeedf   fdZ xZS )AnnotatedConvModelc                 F   t         |           t        j                  j                  j                  |      | _        t        j                  j                  dddd      j                  t        j                        | _        t               | _        t               | _        y r+  )r  r8   r[   r  r  r   r  r   r-  r   r   r.  r   r  r   r  r  s     r7   r8   zAnnotatedConvModel.__init__Q  sj    xx,,@@IHHOOAq!%O8;;%++;N	[
"}r9   c                 l    | j                  |      }| j                  |      }| j                  |      }|S rE   r  r.  r  r  s     r7   r  zAnnotatedConvModel.forwardX  .    JJqMIIaLLLOr9   rO  .c                 4    t        j                  dddd      fS r3  r  rG   s    r7   r  z%AnnotatedConvModel.get_example_inputs^  r4  r9   r  r  s   @r7   r<  r<  P  !    %)E#s(O )r9   r<  c                   :     e Zd Z fdZd Zdeedf   fdZ xZS )AnnotatedConvTransposeModelc                 F   t         |           t        j                  j                  j                  |      | _        t        j                  j                  dddd      j                  t        j                        | _        t               | _        t               | _        y r+  )r  r8   r[   r  r  r   r  r   r8  r   r   r.  r   r  r   r  r  s     r7   r8   z$AnnotatedConvTransposeModel.__init__c  sm    xx,,@@IHH,,Q15,ADD5;;DW	[
"}r9   c                 l    | j                  |      }| j                  |      }| j                  |      }|S rE   r?  r  s     r7   r  z#AnnotatedConvTransposeModel.forwardj  r@  r9   rO  .c                 4    t        j                  dddd      fS r3  r  rG   s    r7   r  z.AnnotatedConvTransposeModel.get_example_inputsp  r4  r9   r  r  s   @r7   rD  rD  b  rB  r9   rD  c                   <     e Zd Zd fdZd Zdeedf   fdZ xZS )ConvBnModelrO  c                 2   t         |           t        j                  j	                  dddd      j                  t        j                        | _        t        j                  j                  d      j                  t        j                        | _	        y r+  )
r  r8   r[   r   r-  r   r   r.  BatchNorm2dbnr  s    r7   r8   zConvBnModel.__init__u  sd    HHOOAq!%O8;;%++;N	((&&q),,5;;,?r9   c                 J    | j                  |      }| j                  |      }|S rE   r.  rL  r  s     r7   r  zConvBnModel.forwardz  !    IIaLGGAJr9   .c                 4    t        j                  dddd      fS r3  r  rG   s    r7   r  zConvBnModel.get_example_inputs  r4  r9   r  r  r  s   @r7   rI  rI  t  s"    @

)E#s(O )r9   rI  c                   <     e Zd Zd fdZd Zdeedf   fdZ xZS )AnnotatedConvBnModelrO  c                    t         |           t        | _        t        j
                  j                  dddd      j                  t        j                        | _	        t        j
                  j                  d      j                  t        j                        | _        t               | _        t               | _        y r+  )r  r8   r   r  r[   r   r-  r   r   r.  rK  rL  r   r  r   r  r  s    r7   r8   zAnnotatedConvBnModel.__init__  s}    &HHOOAq!%O8;;%++;N	((&&q),,5;;,?[
"}r9   c                     | j                  |      }| j                  |      }| j                  |      }| j                  |      }|S rE   )r  r.  rL  r  r  s     r7   r  zAnnotatedConvBnModel.forward  s;    JJqMIIaLGGAJLLOr9   .c                 4    t        j                  dddd      fS r3  r  rG   s    r7   r  z'AnnotatedConvBnModel.get_example_inputs  r4  r9   r  r  r  s   @r7   rR  rR    s!    %)E#s(O )r9   rR  c                   <     e Zd Zd fdZd Zdeedf   fdZ xZS )ConvBnReLUModelrO  c                 h   t         |           t        j                  j	                  dddd      j                  t        j                        | _        t        j                  j                  d      j                  t        j                        | _	        t        j                  d      | _        y Nr  r   Fr,  r   Tr   )r  r8   r[   r   r-  r   r   r.  rK  rL  ReLUr  r  s    r7   r8   zConvBnReLUModel.__init__  st    HHOOAq!%O8;;%++;N	((&&q),,5;;,?GGD)	r9   c                 l    | j                  |      }| j                  |      }| j                  |      }|S rE   r.  rL  r  r  s     r7   r  zConvBnReLUModel.forward  s.    IIaLGGAJIIaLr9   .c                 4    t        j                  dddd      fS r3  r  rG   s    r7   r  z"ConvBnReLUModel.get_example_inputs  r4  r9   r  r  r  s   @r7   rW  rW    s!    *)E#s(O )r9   rW  c                   B     e Zd Zd fd	Zd Zd Zdeedf   fdZ xZ	S )AnnotatedConvBnReLUModelc                     t         |           t        j                  j                  j                  |      | _        t        j                  j                  dddd      j                  t        j                        | _        t        j                  j                  d      j                  t        j                        | _        t        j                  d      | _        t!               | _        t%               | _        y rY  )r  r8   r[   r  r  r   r  r   r-  r   r   r.  rK  rL  rZ  r  r   r  r   r  r  s     r7   r8   z!AnnotatedConvBnReLUModel.__init__  s    xx,,@@IHHOOAq!%O8;;%++;N	((&&q),,5;;,?GGD)	[
"}r9   c                     | j                  |      }| j                  |      }| j                  |      }| j                  |      }| j	                  |      }|S rE   )r  r.  rL  r  r  r  s     r7   r  z AnnotatedConvBnReLUModel.forward  sH    JJqMIIaLGGAJIIaLLLOr9   c                     | j                   r0t        j                  j                  j	                  | g dgd       y t        j                  j                  j                  | g dgd       y )Nr\  Tr   trainingr[   r  r  fuse_modules_qatfuse_modulesrG   s    r7   
fuse_modelz#AnnotatedConvBnReLUModel.fuse_model  s[    ==HH!!22-. 3  HH!!..-. / r9   rO  .c                 4    t        j                  dddd      fS r3  r  rG   s    r7   r  z+AnnotatedConvBnReLUModel.get_example_inputs  r4  r9   r  )
rR   rS   rT   r8   r  rg  r  r.   r  r  r  s   @r7   r_  r_    s&    %	)E#s(O )r9   r_  c                   <     e Zd Zd fdZd Zdeedf   fdZ xZS )TwoLayerConvModelrO  c                 :   t         |           t        j                  j	                  dddd      j                  t        j                        | _        t        j                  j	                  dddd      j                  t        j                        | _        y )Nr  r   Fr,  r   rg   )	r  r8   r[   r   r-  r   r   conv1conv2r  s    r7   r8   zTwoLayerConvModel.__init__  sj    XX__Q15_9<<5;;<O
XX__Q15_9<<5;;<O
r9   c                 J    | j                  |      }| j                  |      }|S rE   rl  rm  r  s     r7   r  zTwoLayerConvModel.forward  !    JJqMJJqMr9   .c                 4    t        j                  dddd      fS r3  r  rG   s    r7   r  z$TwoLayerConvModel.get_example_inputs  r4  r9   r  r  r  s   @r7   rj  rj    s"    P

)E#s(O )r9   rj  c                   <     e Zd Zd fdZd Zdeedf   fdZ xZS )TwoLayerLinearModelrO  c                 .   t         |           t        j                  j	                  dd      j                  t        j                        | _        t        j                  j	                  dd      j                  t        j                        | _        y r  r  r  s    r7   r8   zTwoLayerLinearModel.__init__  r  r9   c                 J    | j                  |      }| j                  |      }|S rE   r  r  r  s     r7   r  zTwoLayerLinearModel.forward  !    HHQKHHQKr9   .c                 0    t        j                  dd      fS r  r  rG   s    r7   r  z&TwoLayerLinearModel.get_example_inputs  r  r9   r  r  r  s   @r7   rs  rs    s!    ?

#E#s(O #r9   rs  c                   <     e Zd Zd fdZd Zdeedf   fdZ xZS )LinearModelWithSubmodulerO  c                 v    t         |           t               | _        t	        j
                  dd      | _        y r
  )r  r8   rs  submr   r  fcr  s    r7   r8   z!LinearModelWithSubmodule.__init__  s)    ')	))Aq/r9   c                 J    | j                  |      }| j                  |      }|S rE   )r|  r}  r  s     r7   r  z LinearModelWithSubmodule.forward  rO  r9   .c                 6    | j                   j                         S rE   )r|  r  rG   s    r7   r  z+LinearModelWithSubmodule.get_example_inputs      yy++--r9   r  r  r  s   @r7   rz  rz    s!    "

.E#s(O .r9   rz  c                   <     e Zd Zd fdZd Zdeedf   fdZ xZS )AnnotatedTwoLayerLinearModelrO  c                    t         |           t        j                  j	                  dd      j                  t        j                        | _        t        t        j                  j	                  dd      j                  t        j                              | _	        t        j                  j                  j                  d      | j                  _        y )Nr   r  r   r  )r  r8   r[   r   r  r   r   r  r   r  r  r  r   r  r  s    r7   r8   z%AnnotatedTwoLayerLinearModel.__init__  s    88??1a(++%+++>1 5 8 8u{{ 8 KL 8800DDXNr9   c                 J    | j                  |      }| j                  |      }|S rE   rv  r  s     r7   r  z$AnnotatedTwoLayerLinearModel.forward  rw  r9   .c                 0    t        j                  dd      fS r  r  rG   s    r7   r  z/AnnotatedTwoLayerLinearModel.get_example_inputs  r  r9   r  r  r  s   @r7   r  r    s"    O
#E#s(O #r9   r  c                   &     e Zd Zd fdZd Z xZS )ActivationsTestModelc                 6   t         |           t        j                  j                  j                  d      | _        t        j                  j                  j                         | _        t        j                  j                         j                  t        j                        | _        t        j                  j                         j                  t        j                        | _        t        j                  j                  j!                         | _        y )Nr  r   )r  r8   r[   r  r  r   r  r   r  r   	Hardswishr   r   	hardswishELUelur   r  r  s    r7   r8   zActivationsTestModel.__init__  s    xx,,@@JXX**446
++-00u{{0C88<<>$$5;;$7xx,,88:r9   c                     | j                  |      }| j                  |      }| j                  |      }| j                  |      }|S rE   )r  r  r  r  r  s     r7   r  zActivationsTestModel.forward  s<    JJqMNN1HHQKLLOr9   r  r  r  s   @r7   r  r    s    ;r9   r  c                   <     e Zd Zd fdZd Zdeedf   fdZ xZS )LinearReluModelrO  c                     t         |           t        j                  j	                  dd      j                  t        j                        | _        t        j                  j                         | _	        y r  
r  r8   r[   r   r  r   r   r}  rZ  r  r  s    r7   r8   zLinearReluModel.__init__  sG    ((//!Q'***=HHMMO	r9   c                 F    | j                  | j                  |            }|S rE   r  r}  r  s     r7   r  zLinearReluModel.forward      IIdggaj!r9   .c                 0    t        j                  dd      fS r  r  rG   s    r7   r  z"LinearReluModel.get_example_inputs!  r  r9   r  r  r  s   @r7   r  r    s!    $
#E#s(O #r9   r  c                   <     e Zd Zd fdZd Zdeedf   fdZ xZS )LinearReluLinearModelrO  c                 t   t         |           t        j                  j	                  dd      j                  t        j                        | _        t        j                  j                         | _	        t        j                  j	                  dd      j                  t        j                        | _
        y r  r  r8   r[   r   r  r   r   r  rZ  r  r  r  s    r7   r8   zLinearReluLinearModel.__init__&  o    88??1a(++%+++>HHMMO	88??1a(++%+++>r9   c                 l    | j                  |      }| j                  |      }| j                  |      }|S rE   r  r  r  r  s     r7   r  zLinearReluLinearModel.forward,  .    HHQKIIaLHHQKr9   .c                 0    t        j                  dd      fS r  r  rG   s    r7   r  z(LinearReluLinearModel.get_example_inputs2  r  r9   r  r  r  s   @r7   r  r  %  s!    ?#E#s(O #r9   r  c                   <     e Zd Zd fdZd Zdeedf   fdZ xZS )LinearReluAddModelrO  c                 t   t         |           t        j                  j	                  dd      j                  t        j                        | _        t        j                  j                         | _	        t        j                  j	                  dd      j                  t        j                        | _
        y r  r  r  s    r7   r8   zLinearReluAddModel.__init__7  r  r9   c                     | j                  |      }| j                  |      }t        j                  |d      }| j	                  |      }t        j
                  j                         | _        |S r
  r  r  r[   r{  r  r   rZ  r  s     r7   r  zLinearReluAddModel.forward=  N    HHQKIIaLIIaOHHQKHHMMO	r9   .c                 0    t        j                  dd      fS r  r  rG   s    r7   r  z%LinearReluAddModel.get_example_inputsE  r  r9   r  r  r  s   @r7   r  r  6  s!    ?#E#s(O #r9   r  c                   <     e Zd Zd fd	Zd Zdeedf   fdZ xZS )LinearBnLeakyReluModelc                     t         |           t        j                  dd      | _        t        j
                  d      | _        t        j                  d      | _        || _	        y )Nr   g{Gz?)
r  r8   r   r  linearBatchNorm1dbn1d	LeakyReLU
leaky_reluwith_bn)r6   r  r  s     r7   r8   zLinearBnLeakyReluModel.__init__J  sF    ii1oNN1%	,,t,r9   c                     | j                  |      }| j                  r| j                  |      }| j                  |      }|S rE   )r  r  r  r  r  s     r7   r  zLinearBnLeakyReluModel.forwardQ  s6    KKN<<		!AOOAr9   rO  .c                 0    t        j                  dd      fS r  r  rG   s    r7   r  z)LinearBnLeakyReluModel.get_example_inputsX  r  r9   r  r  r  s   @r7   r  r  I  s!    #E#s(O #r9   r  c                   <     e Zd Zd fdZd Zdeedf   fdZ xZS )LinearTanhModelrO  c                     t         |           t        j                  dd      | _        t        j
                         | _        y r
  )r  r8   r   r  r  Tanhr  r  s    r7   r8   zLinearTanhModel.__init__]  s,    ii1oGGI	r9   c                 J    | j                  |      }| j                  |      }|S rE   )r  r  r  s     r7   r  zLinearTanhModel.forwardb  s!    KKNIIaLr9   .c                 0    t        j                  dd      fS r  r  rG   s    r7   r  z"LinearTanhModel.get_example_inputsg  r  r9   r  r  r  s   @r7   r  r  \  s!    

#E#s(O #r9   r  c                   F     e Zd Z	 	 	 	 	 d fd	Zd Zdeedf   fdZ xZS )ConvBnAddReluModelc                 >   t         |           t        j                  ddd      | _        t        j                  ddd      | _        t        j                  d      | _        t        j                         | _	        || _
        || _        || _        || _        || _        y )Nr   )rV  rV  )r  r8   r   r-  r.  rm  rK  rL  rZ  r  r  	with_relutwo_conv	left_convuse_torch_add)r6   r  r  r  r  r  r  s         r7   r8   zConvBnAddReluModel.__init__l  sy     	IIaF+	YYq!V,
..#GGI	" "*r9   c                 @   | j                   r| j                  r| j                  rEt        j                  | j                  | j                  |            | j                  |            }nt        j                  | j                  |      | j                  |            }n| j                  r4| j                  | j                  |            | j                  |      z   }n| j                  |      | j                  |      z   }n|| j                  r| j                  rh| j                  r6t        j                  | j                  | j                  |            |      }n"t        j                  | j                  |      |      }n| j                  r5t        j                  || j                  | j                  |                  }nt        j                  || j                  |            }n| j                  rE| j                  r$| j                  | j                  |            |z   }nY| j                  |      |z   }nD| j                  r$|| j                  | j                  |            z   }n|| j                  |      z   }| j                  r| j                  |      }|S rE   )r  r  r  r[   r{  rL  r.  rm  r  r  r  )r6   x1x2r  s       r7   r  zConvBnAddReluModel.forward  s   ==!!<<		$''$))B-"8$**R.IA		$))B-B@A<<		".B?A		"

26A!!>>||!IIdggdiim&<bA!IIdiimR8||!IIb$''$))B-*@A!IIb$))B-8>>|| GGDIIbM2R7 IIbMB.||2!772.>>		!Ar9   rO  .c                 b    t        j                  dddd      t        j                  dddd      fS )Nrg   r   r  rV  r  rG   s    r7   r  z%ConvBnAddReluModel.get_example_inputs  s+    

1aA&

1aA(>??r9   )TTTTTr  r  s   @r7   r  r  k  s5     +&%N@E#s(O @r9   r  c                   <     e Zd Zd fdZd Zdeedf   fdZ xZS )ConvReluModelrO  c                     t         |           t        j                  j	                  ddd      j                  t        j                        | _        t        j                  j                         | _	        y )Nr  r   r   )
r  r8   r[   r   r-  r   r   r}  rZ  r  r  s    r7   r8   zConvReluModel.__init__  sI    ((//!Q*--EKK-@HHMMO	r9   c                 F    | j                  | j                  |            }|S rE   r  r  s     r7   r  zConvReluModel.forward  r  r9   .c                 4    t        j                  dddd      fS r3  r  rG   s    r7   r  z ConvReluModel.get_example_inputs  r4  r9   r  r  r  s   @r7   r  r    s!    $
)E#s(O )r9   r  c                   <     e Zd Zd fdZd Zdeedf   fdZ xZS )ConvReluConvModelrO  c                 x   t         |           t        j                  j	                  ddd      j                  t        j                        | _        t        j                  j                         | _	        t        j                  j	                  ddd      j                  t        j                        | _
        y Nr  r   r   rg   r  r8   r[   r   r-  r   r   r  rZ  r  r  r  s    r7   r8   zConvReluConvModel.__init__  s    88??1a+..U[[.AHHMMO	88??1a+..U[[.Ar9   c                 l    | j                  |      }| j                  |      }| j                  |      }|S rE   r  r  s     r7   r  zConvReluConvModel.forward  r  r9   .c                 4    t        j                  dddd      fS r3  r  rG   s    r7   r  z$ConvReluConvModel.get_example_inputs  r4  r9   r  r  r  s   @r7   r  r    s"    B)E#s(O )r9   r  c                   <     e Zd Zd fdZd Zdeedf   fdZ xZS )ConvReluAddModelrO  c                 x   t         |           t        j                  j	                  ddd      j                  t        j                        | _        t        j                  j                         | _	        t        j                  j	                  ddd      j                  t        j                        | _
        y r  r  r  s    r7   r8   zConvReluAddModel.__init__  r  r9   c                     | j                  |      }| j                  |      }t        j                  |d      }| j	                  |      }t        j
                  j                         | _        |S r
  r  r  s     r7   r  zConvReluAddModel.forward  r  r9   .c                 4    t        j                  dddd      fS r3  r  rG   s    r7   r  z#ConvReluAddModel.get_example_inputs  r4  r9   r  r  r  s   @r7   r  r    s"    B)E#s(O )r9   r  c                   &     e Zd Zd fdZd Z xZS )NormalizationTestModelc                 l   t         |           t        j                  j                  j                         | _        t        j                  j                  dd      j                  t        j                        | _        t        j                  j                  d      | _        t        j                  j                  dd      | _        t        j                  j!                  d      | _        t        j                  j%                  d      | _        t        j                  j)                  d      | _        y )Nr   r  r   rV  )r  r8   r[   r  r  r   r  r   r  r   r   r  	LayerNorm
layer_norm	GroupNorm
group_normInstanceNorm1dinstance_norm1dInstanceNorm2dinstance_norm2dInstanceNorm3dinstance_norm3dr  s    r7   r8   zNormalizationTestModel.__init__  s    XX**446
88??1a(++%+++>((,,Q/((,,Q2$xx66q9$xx66q9$xx66q9r9   c                 p   | j                  |      }| j                  |      }| j                  |      }| j                  |j	                  d      j                  ddd            }| j                  |      }| j                  |j	                  d            }| j                  |j	                  d            }|S )Nr   rg   r  )	r  r  r  r  r  repeatr  r  r  r  s     r7   r  zNormalizationTestModel.forward  s    JJqMHHQKOOAOOAKKO221a;<  #  R1  R1r9   r  r  r  s   @r7   r  r    s    :r9   r  c                   &     e Zd Zd fdZd Z xZS )NestedModelc                     t         |           t               | _        t	               | _        t        j                  j                  dd      j                  t        j                        | _        y r  )r  r8   r  sub1rs  sub2r[   r   r  r   r   fc3r  s    r7   r8   zNestedModel.__init__  sJ    #%	')	88??1a(++%+++>r9   c                 l    | j                  |      }| j                  |      }| j                  |      }|S rE   r  r  r  r  s     r7   r  zNestedModel.forward  .    IIaLIIaLHHQKr9   r  r  r  s   @r7   r  r    s    ?r9   r  c                   $     e Zd Z fdZd Z xZS )AnnotatedNestedModelc                    t         |           t               | _        t	               | _        t        t        j                  j                  dd      j                  t        j                              | _        t        | j                  _        t        | j
                  j                        | j
                  _        |dk(  r t         | j
                  j                  _        y t        | j
                  j                  _        y )Nr   r   r  )r  r8   r  r  rs  r  r   r[   r   r  r   r   r  r   r  r  r   r  s     r7   r8   zAnnotatedNestedModel.__init__  s    #%	')	1 5 8 8u{{ 8 KL*$TYY]]3		h$?DIIMM!$3DIIMM!r9   c                 l    | j                  |      }| j                  |      }| j                  |      }|S rE   r  r  s     r7   r  zAnnotatedNestedModel.forward  r  r9   r  r  s   @r7   r  r    s    
4r9   r  c                   &     e Zd Zd fdZd Z xZS )AnnotatedSubNestedModelc                 \   t         |           t               | _        t	        t                     | _        t	        t        j                  j                  dd      j                  t        j                              | _        t        | j                  _        t        | j                  _        y r  )r  r8   r  r  r   rs  r  r[   r   r  r   r   r  r   r  r  s    r7   r8   z AnnotatedSubNestedModel.__init__  sm    #%	 !4!67	1 5 8 8u{{ 8 KL*+		r9   c                 l    | j                  |      }| j                  |      }| j                  |      }|S rE   r  r  s     r7   r  zAnnotatedSubNestedModel.forward   r  r9   r  r  r  s   @r7   r  r    s    ,r9   r  c                   &     e Zd Zd fdZd Z xZS ) AnnotatedCustomConfigNestedModelc                    t         |           t               | _        t	               | _        t        t        j                  j                  dd      j                  t        j                              | _        t        | j                  _        t        | j
                  _        t        j                  t        j                   d}t#        t%        j&                  di |t(              }|| j
                  j*                  _        t        | j
                  j*                        | j
                  _        t        | j
                  j,                        | j
                  _        y )Nr   r   )r   r  r  rW   )r  r8   r  r  rs  r  r   r[   r   r  r   r   r  r   r  r   per_tensor_affiner   r
   r  r   r  r  )r6   custom_optionscustom_qconfigr  s      r7   r8   z)AnnotatedCustomConfigNestedModel.__init__(  s    #%	')	1 5 8 8u{{ 8 KL*+		#(<<E<S<ST '11CNC*
 !/		$TYY]]3		$TYY]]3		r9   c                 l    | j                  |      }| j                  |      }| j                  |      }|S rE   r  r  s     r7   r  z(AnnotatedCustomConfigNestedModel.forward:  r  r9   r  r  r  s   @r7   r  r  '  s    4$r9   r  c                   &     e Zd Zd fdZd Z xZS )QuantSubModelc                 J   t         |           t               | _        t	        t                     | _        t        | j                  _        t        j                  j                  dd      j                  t        j                        | _        t        | j                  _        y r  )r  r8   r  r  r   rs  r  r   r  r[   r   r  r   r   r  r  s    r7   r8   zQuantSubModel.__init__B  sh    #%	 !4!67	+		88??1a(++%+++>*r9   c                 l    | j                  |      }| j                  |      }| j                  |      }|S rE   r  r  s     r7   r  zQuantSubModel.forwardJ  r  r9   r  r  r  s   @r7   r  r  A  s    +r9   r  c                   ,     e Zd Zd fdZd Zd Z xZS )InnerModulec                    t         |           t        j                  j	                  dd      j                  t        j                        | _        t        j                  j                         | _	        t        j                  j	                  dd      j                  t        j                        | _
        t        j                  j                         | _        y r  )r  r8   r[   r   r  r   r   r  rZ  relu1r  relu2r  s    r7   r8   zInnerModule.__init__R  s    88??1a(++%+++>XX]]_
88??1a(++%+++>XX]]_
r9   c           	      ~    | j                  | j                  | j                  | j                  |                        S rE   )r  r  r  r  r  s     r7   r  zInnerModule.forwardY  s+    zz$((4::dhhqk#:;<<r9   c                 4   g }t        | j                               }t        |      D ]  \  }\  }}t        |t        j
                  j                        s.|t        |      dz
  k\  r nLt        ||dz      d   t        j
                  j                        so|j                  |||dz      d   g        | j                  r-t        j                  j                  j                  | |d       y t        j                  j                  j                  | |d       y )Nrg   r   Tr   )rp  r  r   rJ   r[   r   r  r   rZ  r   rd  r  r  re  rf  )r6   fusable_layersr  r}  current_namelayers         r7   rf  zInnerModule.fuse_modules\  s    d1134*3N*C 	V&C&,%1#n-11nS1W5a8%((--H"))<a9PQR9S*TU	V ==HH!!224QU2VHH!!..t^T.Rr9   r  )rR   rS   rT   r8   r  rf  r  r  s   @r7   r   r   Q  s    %=Sr9   r   c                   <     e Zd Zd fdZd Zdeedf   fdZ xZS )FunctionalLinearrO  c                     t         |           t        j                  d      | _        t        j
                  d      | _        y )N)r   r   r   )r  r8   r[   r  r  rM  r  r  s    r7   r8   zFunctionalLinear.__init__m  s-    jj(KKN	r9   c                 X    t        j                  || j                  | j                        S rE   )Fr  r  r  r  s     r7   r  zFunctionalLinear.forwardr  s    xx4;;		22r9   .c                 0    t        j                  dd      fS r  r  rG   s    r7   r  z#FunctionalLinear.get_example_inputsu  r  r9   r  r  r  s   @r7   r
  r
  l  s!    #
3#E#s(O #r9   r
  c                   <     e Zd Zd fdZd Zdeedf   fdZ xZS ) SingleLayerFunctionalLinearModelrO  c                 @    t         |           t               | _        y rE   )r  r8   r
  linear1r  s    r7   r8   z)SingleLayerFunctionalLinearModel.__init__z  s    ')r9   c                 (    | j                  |      }|S rE   )r  r  s     r7   r  z(SingleLayerFunctionalLinearModel.forward~  s    LLOr9   .c                 6    | j                   j                         S rE   r  r  rG   s    r7   r  z3SingleLayerFunctionalLinearModel.get_example_inputs      ||..00r9   r  r  r  s   @r7   r  r  y  s!    *1E#s(O 1r9   r  c                   <     e Zd Zd fdZd Zdeedf   fdZ xZS )TwoLayerFunctionalLinearModelrO  c                 ^    t         |           t               | _        t               | _        y rE   r  r8   r
  r  linear2r  s    r7   r8   z&TwoLayerFunctionalLinearModel.__init__  "    ')')r9   c                 J    | j                  |      }| j                  |      }|S rE   )r  r  r  s     r7   r  z%TwoLayerFunctionalLinearModel.forward  s!    LLOLLOr9   .c                 6    | j                   j                         S rE   r  rG   s    r7   r  z0TwoLayerFunctionalLinearModel.get_example_inputs  r  r9   r  r  r  s   @r7   r  r    s!    *

1E#s(O 1r9   r  c                   <     e Zd Zd fdZd Zdeedf   fdZ xZS )FunctionalLinearAddModelrO  c                 ^    t         |           t               | _        t               | _        y rE   r  r  s    r7   r8   z!FunctionalLinearAddModel.__init__  r  r9   c                 v    | j                  |      }t        j                  |d      }| j                  |      }|S r
  )r  r[   r{  r  r  s     r7   r  z FunctionalLinearAddModel.forward  s0    LLOIIaOLLOr9   .c                 6    | j                   j                         S rE   r  rG   s    r7   r  z+FunctionalLinearAddModel.get_example_inputs  r  r9   r  r  r  s   @r7   r   r     s!    *
1E#s(O 1r9   r   c                   <     e Zd Zd fdZd Zdeedf   fdZ xZS )FunctionalLinearReluModelrO  c                 @    t         |           t               | _        y rE   )r  r8   r
  r  r  s    r7   r8   z"FunctionalLinearReluModel.__init__  s    &(r9   c                 R    | j                  |      }t        j                  |      }|S rE   )r  r  r  r  s     r7   r  z!FunctionalLinearReluModel.forward  s!    KKNFF1Ir9   .c                 6    | j                   j                         S rE   )r  r  rG   s    r7   r  z,FunctionalLinearReluModel.get_example_inputs  s    {{--//r9   r  r  r  s   @r7   r%  r%    s!    )
0E#s(O 0r9   r%  c                   <     e Zd Zd fdZd Zdeedf   fdZ xZS )FunctionalLinearReluLinearModelrO  c                     t         |           t               | _        t	        j
                         | _        t               | _        y rE   )r  r8   r
  r  r   rZ  r  r  r  s    r7   r8   z(FunctionalLinearReluLinearModel.__init__  s/    ')GGI	')r9   c                 l    | j                  |      }| j                  |      }| j                  |      }|S rE   )r  r  r  r  s     r7   r  z'FunctionalLinearReluLinearModel.forward  s.    LLOIIaLLLOr9   .c                 6    | j                   j                         S rE   r  rG   s    r7   r  z2FunctionalLinearReluLinearModel.get_example_inputs  r  r9   r  r  r  s   @r7   r*  r*    s!    *1E#s(O 1r9   r*  c                   <     e Zd Zd fdZd Zdeedf   fdZ xZS )FunctionalConv2drO  c                     t         |           t        j                  dddd      | _        t        j                  d      | _        d| _        d| _        d| _        d| _	        y )Nr  rg   rg   )r   r   rg   )
r  r8   r[   r  r  r  stridepaddingdilationr   r  s    r7   r8   zFunctionalConv2d.__init__  sO    jjAq!,JJqM	r9   c           	          t        j                  || j                  | j                  | j                  | j
                  | j                  | j                        S rE   )r  conv2dr  r  r2  r3  r4  r   r  s     r7   r  zFunctionalConv2d.forward  s@    xxKKIIKKLLMMKK
 	
r9   .c                 4    t        j                  dddd      fS r3  r  rG   s    r7   r  z#FunctionalConv2d.get_example_inputs  r4  r9   r  r  r  s   @r7   r/  r/    s!    	
)E#s(O )r9   r/  c                   <     e Zd Zd fdZd Zdeedf   fdZ xZS )SingleLayerFunctionalConvModelrO  c                 @    t         |           t               | _        y rE   )r  r8   r/  rl  r  s    r7   r8   z'SingleLayerFunctionalConvModel.__init__  s    %'
r9   c                 (    | j                  |      }|S rE   )rl  r  s     r7   r  z&SingleLayerFunctionalConvModel.forward  s    JJqMr9   .c                 6    | j                   j                         S rE   rl  r  rG   s    r7   r  z1SingleLayerFunctionalConvModel.get_example_inputs      zz,,..r9   r  r  r  s   @r7   r9  r9    s!    (/E#s(O /r9   r9  c                   <     e Zd Zd fdZd Zdeedf   fdZ xZS )TwoLayerFunctionalConvModelrO  c                 ^    t         |           t               | _        t               | _        y rE   )r  r8   r/  rl  rm  r  s    r7   r8   z$TwoLayerFunctionalConvModel.__init__  s"    %'
%'
r9   c                 J    | j                  |      }| j                  |      }|S rE   ro  r  s     r7   r  z#TwoLayerFunctionalConvModel.forward  rp  r9   .c                 6    | j                   j                         S rE   r=  rG   s    r7   r  z.TwoLayerFunctionalConvModel.get_example_inputs  r>  r9   r  r  r  s   @r7   r@  r@    s!    (

/E#s(O /r9   r@  c                   <     e Zd Zd fdZd Zdeedf   fdZ xZS )FunctionalConvReluModelrO  c                 @    t         |           t               | _        y rE   )r  r8   r/  r.  r  s    r7   r8   z FunctionalConvReluModel.__init__  s    $&	r9   c                 R    | j                  |      }t        j                  |      }|S rE   )r.  r  r  r  s     r7   r  zFunctionalConvReluModel.forward  s!    IIaLFF1Ir9   .c                 6    | j                   j                         S rE   )r.  r  rG   s    r7   r  z*FunctionalConvReluModel.get_example_inputs	  r  r9   r  r  r  s   @r7   rE  rE    s!    '
.E#s(O .r9   rE  c                   <     e Zd Zd fdZd Zdeedf   fdZ xZS )FunctionalConvReluConvModelrO  c                     t         |           t               | _        t	        j
                         | _        t               | _        y rE   )r  r8   r/  rl  r   rZ  r  rm  r  s    r7   r8   z$FunctionalConvReluConvModel.__init__	  s/    %'
GGI	%'
r9   c                 l    | j                  |      }| j                  |      }| j                  |      }|S rE   )rl  r  rm  r  s     r7   r  z#FunctionalConvReluConvModel.forward	  s.    JJqMIIaLJJqMr9   .c                 6    | j                   j                         S rE   r=  rG   s    r7   r  z.FunctionalConvReluConvModel.get_example_inputs	  r>  r9   r  r  r  s   @r7   rJ  rJ  	  s!    (/E#s(O /r9   rJ  c                   0     e Zd ZdZd fdZd Zd Z xZS )SkipQuantModelVWe can skip quantization by explicitly
    setting qconfig of a submodule to None
    c                     t         |           t               | _        t        j
                  j                  dd      j                  t        j                        | _	        y r  )
r  r8   r   rb  r[   r   r  r   r   r}  r  s    r7   r8   zSkipQuantModel.__init__	  s?    =((//!Q'***=r9   c                 B    | j                  | j                  |            S rE   r}  rb  r  s     r7   r  zSkipQuantModel.forward"	      wwtxx{##r9   c                 8    | j                   j                          y rE   )rb  rf  rG   s    r7   rf  zSkipQuantModel.fuse_modules%	  s    r9   r  rR   rS   rT   rU   r8   r  rf  r  r  s   @r7   rO  rO  	  s    >
$ r9   rO  c                   .     e Zd ZdZ fdZd Zd Z xZS )AnnotatedSkipQuantModelrP  c                 V   t         |           t        j                  j                  j                  |      | _        t        t                     | _	        t        j                  j                  dd      j                  t        j                        | _        d | j                  _        y r  )r  r8   r[   r  r  r   r  r   r   rb  r   r  r   r   r}  r  s     r7   r8   z AnnotatedSkipQuantModel.__init__.	  sk    xx,,@@I.((//!Q'***=r9   c                 B    | j                  | j                  |            S rE   rS  r  s     r7   r  zAnnotatedSkipQuantModel.forward6	  rT  r9   c                 L    | j                   j                  j                          y rE   )rb  r   rf  rG   s    r7   rf  z$AnnotatedSkipQuantModel.fuse_modules9	  s    $$&r9   rV  r  s   @r7   rX  rX  )	  s    $'r9   rX  c                   *     e Zd ZdZd fdZd Z xZS )QuantStubModel=A Module with manually inserted `QuantStub` and `DeQuantStub`c                 @   t         |           t        j                  j                  j                  d      | _        t               | _        t               | _
        t        j                  j                  dd      j                  t        j                        | _        y )Nr'  r   r   )r  r8   r[   r  r  r   r  r   r  r   r  r   r  r   r   r}  r  s    r7   r8   zQuantStubModel.__init__@	  sd    xx,,@@K[
"}((//!Q'***=r9   c                 h    | j                  |      }| j                  |      }| j                  |      S rE   )r  r}  r  r  s     r7   r  zQuantStubModel.forwardG	  s*    JJqMGGAJ||Ar9   r  rR   rS   rT   rU   r8   r  r  r  s   @r7   r]  r]  =	  s    H>r9   r]  c                   (     e Zd ZdZ fdZd Z xZS )ManualLinearQATModelr^  c                    t         |           t        j                  j                  j                  |      | _        t               | _        t               | _
        t        j                  j                  dd      j                  t        j                        | _        t        j                  j                  dd      j                  t        j                        | _        y Nr   rg   r   rf   )r  r8   r[   r  r  r   r  r   r  r   r  r   r  r   r   r  r  r  s     r7   r8   zManualLinearQATModel.__init__P	  s    xx,,DDWM[
"}88??1a(++%+++>88??1b),,5;;,?r9   c                     | j                  |      }| j                  |      }| j                  |      }| j                  |      S rE   )r  r  r  r  r  s     r7   r  zManualLinearQATModel.forwardX	  s7    JJqMHHQKHHQK||Ar9   ra  r  s   @r7   rc  rc  M	  s    H@r9   rc  c                   (     e Zd ZdZ fdZd Z xZS )ManualDropoutQATModelr^  c                    t         |           t        j                  j                  j                  |      | _        t               | _        t               | _
        t        j                  j                  dd      j                  t        j                        | _        t        j                  j!                  d      | _        y )Nr   rg   r         ?)r  r8   r[   r  r  r   r  r   r  r   r  r   r  r   r   r  r  dropoutr  s     r7   r8   zManualDropoutQATModel.__init__b	  sz    xx,,DDWM[
"}88??1a(++%+++>xx'',r9   c                     | j                  |      }| j                  |      }| j                  |      }| j                  |      S rE   )r  r  rk  r  r  s     r7   r  zManualDropoutQATModel.forwardj	  s7    JJqMHHQKLLO||Ar9   ra  r  s   @r7   rh  rh  _	  s    H-r9   rh  c                   *     e Zd ZdZd fd	Zd Z xZS )ManualLinearDynamicQATModelz,A Module that uses a dynamic QAT by default.c                 L   t         |           |xs t        | _        t        j
                  j                  dd      j                  t        j                        | _	        t        j
                  j                  dd      j                  t        j                        | _
        y re  )r  r8   r   r  r[   r   r  r   r   r  r  r6   r  r  s     r7   r8   z$ManualLinearDynamicQATModel.__init__t	  sk    ="=88??1a(++%+++>88??1b),,5;;,?r9   c                 J    | j                  |      }| j                  |      }|S rE   rv  r  s     r7   r  z#ManualLinearDynamicQATModel.forwardz	  rw  r9   rE   ra  r  s   @r7   rn  rn  q	  s    7@r9   rn  c                   *     e Zd ZdZd fd	Zd Z xZS )ManualConvLinearQATModelzpA module with manually inserted `QuantStub` and `DeQuantStub`
    and contains both linear and conv modules
    c                 X   t         |           |r|n(t        j                  j                  j                  d      | _        t               | _        t               | _
        t        j                  j                  ddd      j                  t        j                        | _        t        j                  j!                  dd      j                  t        j                        | _        t        j                  j!                  dd      j                  t        j                        | _        y )Nr'  r  rg   )r   r   @   rf   )r  r8   r[   r  r  r   r  r   r  r   r  r   r-  r   r   r.  r  r  r  rp  s     r7   r8   z!ManualConvLinearQATModel.__init__	  s      &&>>yI 	
 [
"}HHOOAqaO8;;%++;N	88??2r*--EKK-@88??2r*--EKK-@r9   c                     | j                  |      }| j                  |      }|j                  dd      j                         }| j	                  |      }| j                  |      }| j                  |      S )Nr   ru  )r  r.  r   r]  r  r  r  r  s     r7   r  z ManualConvLinearQATModel.forward	  s\    JJqMIIaLFF2rN%%'HHQKHHQK||Ar9   rE   ra  r  s   @r7   rs  rs  	  s    Ar9   rs  c                   $     e Zd ZdZd fdZ xZS )ManualConvLinearSymmQATModelzhSame as ManualConvLinearQATModule but with Symmetric Quantization.
    Supported only with qnnpack.
    c                 ,    t         |   t               y rE   )r  r8   r   r  s    r7   r8   z%ManualConvLinearSymmQATModel.__init__	  s    >?r9   r  )rR   rS   rT   rU   r8   r  r  s   @r7   rx  rx  	  s    @ @r9   rx  c                        e Zd Zd fdZ	 	 ddej
                  dej
                  dz  dej
                  dz  fdZ xZS )	ManualEmbeddingBagLinearNc                 T   t         |           t        j                  ddd      | _        t
        | j                  _        t               | _        t               | _
        t        j                  dd      j                  t        j                        | _        t!        d      | _        y )Nrf      rs   )r  r  r  rg   r   r'  )r  r8   r   r  embr	   r  r   r  r   r  r  r   r[   r   r  r   r  s    r7   r8   z!ManualEmbeddingBagLinear.__init__	  sp    ??"BUS8[
"}iiA&)))<.y9r9   r  r  per_sample_weightsc                     | j                  |||      }| j                  |      }| j                  |      }| j                  |      S rE   )r~  r  r  r  )r6   r  r  r  r  s        r7   r  z ManualEmbeddingBagLinear.forward	  s?     HHUG%78JJqMKKN||Ar9   r  r  rR   rS   rT   r8   r[   ro  r  r  r  s   @r7   r{  r{  	  sG    : (,26		||	 $	 "LL4/		r9   r{  c                   \     e Zd ZdZd fdZdej                  dej                  fdZ xZS )DeFusedEmbeddingBagLineara  A module to simulate QAT embedding bag with a linear layer,
    this module uses a separate embedding and bagging op, similar
    to that which is described in the EmbeddingBag documentation.

    https://pytorch.org/docs/stable/generated/torch.nn.EmbeddingBag.html
    rO  c                 |   t         |           t        j                  dd      | _        t
        | j                  _        t        j                  | _	        t               | _        t               | _        t        j                  dd      j                  t        j                         | _        t%        d      | _        y )Nrf   r}  r  rg   r   r'  )r  r8   r   r  r~  r	   r  r[   rs   
bagging_opr   r  r   r  r  r   r   r  r   r  s    r7   r8   z"DeFusedEmbeddingBagLinear.__init__	  sy    <<rD8))[
"}iiA&)))<.y9r9   r  c                     | j                  | j                  |      d      }| j                  |      }| j                  |      }| j	                  |      S Nrg   r  )r  r~  r  r  r  )r6   r  r  s      r7   r  z!DeFusedEmbeddingBagLinear.forward	  sD    OODHHUOO3JJqMKKN||Ar9   r  )	rR   rS   rT   rU   r8   r[   ro  r  r  r  s   @r7   r  r  	  s(    :U\\ ell r9   r  c                   &     e Zd Zd fdZd Z xZS )SubModelForFusionc                 
   t         |           t        j                  dddd       j	                  t
        j                        | _        t        j                  d      j	                  t
        j                        | _	        y )NrV  rg   r,  r   )
r  r8   r   r-  r   r[   r   r.  rK  rL  r  s    r7   r8   zSubModelForFusion.__init__	  sX    IIaAD1445;;4G	..#&&U[[&9r9   c                 J    | j                  |      }| j                  |      }|S rE   rN  r  s     r7   r  zSubModelForFusion.forward	  rO  r9   r  r  r  s   @r7   r  r  	  s    :
r9   r  c                   &     e Zd Zd fdZd Z xZS )SubModelWithoutFusionc                    t         |           t        j                  dddd       j	                  t
        j                        | _        t        j                  d      j	                  t
        j                        | _	        y )NrV  rg   r,  r   Fr   )
r  r8   r   r-  r   r[   r   r.  rZ  r  r  s    r7   r8   zSubModelWithoutFusion.__init__	  sX    IIaAD1445;;4G	GGE*--EKK-@	r9   c                 B    | j                  | j                  |            S rE   )r  r.  r  s     r7   r  zSubModelWithoutFusion.forward	  s    yy1&&r9   r  r  r  s   @r7   r  r  	  s    A
'r9   r  c                   $     e Zd Z fdZd Z xZS )ModelForFusionc                    t         |           t        j                  dddd       j	                  t
        j                        | _        t        j                  d      j	                  t
        j                        | _	        t        j                  d      j	                  t
        j                        | _        t               | _        t               | _        t        j                   dd	      j	                  t
        j                        | _        t%               | _        t)               | _        || _        t        j.                  ddd
d       j	                  t
        j                        | _        t        j                  d      j	                  t
        j                        | _        t        j4                  d      j	                  t
        j                        | _        t        j                  d      j	                  t
        j                        | _        t        j:                  ddd      j	                  t
        j                        | _        t        j>                  d      j	                  t
        j                        | _         t        j                  d      j	                  t
        j                        | _!        d | j                  _        d | j"                  _        y )Nr  rV  rg   r,  r   Tr   $   rf   )rg   rg   rg   F)"r  r8   r   r-  r   r[   r   rl  rK  bn1rZ  r  r  r  r  r  r  r}  r   r  r   r  r  Conv3drm  r  BatchNorm3dbn2relu3Conv1dconv3r  bn3relu4rp  s     r7   r8   zModelForFusion.__init__	  s   YYq!QT255EKK5H
>>!$''ekk':WWT*--EKK-@
%'	)+	))B#&&U[[&9[
"}YYq!YT:==EKK=P
WWU+..U[[.A
>>!$''ekk':WWT*--EKK-@
YYq!Q'***=
>>!$''ekk':WWT*--EKK-@
 		r9   c                    |j                  d      }| j                  |      }| j                  |      }| j                  |      }| j	                  |      }|j                  d      }|j                  d      }| j                  |      }| j                  |      }| j                  |      }| j                  |      }| j                  |      }| j                  |      }|j                  dd      j                         }| j                  |      }| j                  |      }| j!                  |      }| j#                  |      }| j%                  |      }| j                  |      }|S )NrV  r   r  )r}  r  r  r  r  r  rl  r  r  r  r  r  r   r]  r}  rm  r  r  r  r6   r  ys      r7   r  zModelForFusion.forward 
  s   IIaLJJqMJJqMHHQKJJqMKKNKKNJJqMHHQKJJqMIIaLLLOIIaLIIb"((*GGAJJJqMJJqMHHQKJJqMLLOr9   r  r  s   @r7   r  r  	  s    ,r9   r  c                         e Zd Zd fdZ xZS )
ConvBNReLUc           	          t         |   t        j                  ddddd      t        j                  d      t        j
                  d             y )Nr  rg   Fr,  r   )r  r8   r   r-  rK  rZ  r  s    r7   r8   zConvBNReLU.__init__
  s;    IIaAqu-r~~a/@"''RWBX	
r9   r  )rR   rS   rT   r8   r  r  s   @r7   r  r  
  s    
 
r9   r  c                   &     e Zd Zd fdZd Z xZS )ModelWithSequentialFusionc                    t         |           t        j                  ddd      | _        t        j
                  d      | _        t        d      D cg c]  }t                }}t        j                  | | _
        t        j                  dd      t        j
                  d      g}t        j                  | | _        t        j                         | _        t               | _        t!               | _        y c c}w )Nr  rg   Fr   ,  rf   )r  r8   r   r-  rl  rZ  r  rk   r  r  featuresr  
classifierseqr   r  r   r  )r6   rz   layersheadr  s       r7   r8   z"ModelWithSequentialFusion.__init__ 
  s    YYq!Q'
WWU+
(-a11*,11v.		#r"BGGE$:;--.==?[
"} 2s   C9c                     | j                  |      }| j                  |      }| j                  |      }| j                  |      }t	        j
                  |d      }| j                  |      }| j                  |      }| j                  |      }|S )N)r   r  )	r  rl  r  r  r[   r   r  r  r  r  s     r7   r  z!ModelWithSequentialFusion.forward,
  su    JJqMJJqMJJqMMM!MM!./OOAHHQKLLOr9   r  r  r  s   @r7   r  r  
  s    
%	r9   r  c                   &     e Zd Zd fdZd Z xZS )ModelForFusionWithBiasc                    t         |           t        j                  dddd      j	                  t
        j                        | _        t        j                  d      j	                  t
        j                        | _	        t        j                  d      j	                  t
        j                        | _        t        j                  dddd      j	                  t
        j                        | _        t        j                  d      j	                  t
        j                        | _        t               | _        t!               | _        y )	Nr  rV  r   Tr,  r   r   rg   )r  r8   r   r-  r   r[   r   rl  rK  r  rZ  r  rm  r  r   r  r   r  r  s    r7   r8   zModelForFusionWithBias.__init__9
  s    YYq!QT255EKK5H
>>!$''ekk':WWT*--EKK-@
YYq!QT255EKK5H
>>!$''ekk':[
"}r9   c                     | j                  |      }| j                  |      }| j                  |      }| j                  |      }| j	                  |      }| j                  |      }| j                  |      }|S rE   )r  rl  r  r  rm  r  r  r  s     r7   r  zModelForFusionWithBias.forwardC
  sb    JJqMJJqMHHQKJJqMJJqMHHQKLLOr9   r  r  r  s   @r7   r  r  8
  s    %r9   r  c                   &     e Zd Zd fdZd Z xZS )ModelForLinearBNFusionc                 X   t         |           t        j                  dd      | _        t        j
                  d      | _        t        j                  j                  | j                  j                         t        j                  j                  | j                  j                         y )N   rf   )r  r8   r   r  r}  r  rL  inituniform_r  r  r  s    r7   r8   zModelForLinearBNFusion.__init__O
  sd    ))B#..$
(
&r9   c                 B    | j                  | j                  |            S rE   )rL  r}  r  s     r7   r  zModelForLinearBNFusion.forwardV
  s    wwtwwqz""r9   r  r  r  s   @r7   r  r  N
  s    '#r9   r  c                       e Zd Zd Zd Zy)DummyObserverc                      y)N)g      ?r   rW   rG   s    r7   calculate_qparamszDummyObserver.calculate_qparams[
  s    r9   c                     |S rE   rW   r  s     r7   r  zDummyObserver.forward^
  s    r9   N)rR   rS   rT   r  r  rW   r9   r7   r  r  Z
  s    r9   r  c                   &     e Zd Zd fdZd Z xZS )ModelForConvTransposeBNFusionc                 f   t         |           t        j                  ddd      | _        t        j
                  d      | _        t        j                  ddd      | _        t        j                  d      | _
        t        j                  ddd      | _        t        j                  d      | _        y )Nr  rg   )r  r8   r   ConvTranspose1drl  r  r  r8  rm  rK  r  ConvTranspose3dr  r  r  r  s    r7   r8   z&ModelForConvTransposeBNFusion.__init__c
  s    ''1a0
>>!$''1a0
>>!$''1a0
>>!$r9   c                    | j                  |      }| j                  |      }|j                  d      }| j                  |      }| j	                  |      }|j                  d      }| j                  |      }| j                  |      }|S )NrV  )rl  r  r  rm  r  r  r  r  s     r7   r  z%ModelForConvTransposeBNFusion.forwardl
  so    JJqMHHQKKKNJJqMHHQKKKNJJqMHHQKr9   r  r  r  s   @r7   r  r  b
  s    %	r9   r  c                   &     e Zd Zd fdZd Z xZS )ModelWithFunctionalsc                     t         |           t        j                         | _        t        j                         | _        t        j                         | _        t        j                         | _        y rE   )r  r8   r  FloatFunctionalmycatmyadd
myadd_relumymatmulr  s    r7   r8   zModelWithFunctionals.__init__y
  sN    ((*
((*
--/++-r9   c                     | j                   j                  |||g      }| j                  j                  ||      }| j                  j                  ||      }| j                  j                  ||j                        }|S rE   )	r  rh  r  r{  r  add_relur  matmulT)r6   r  r  zri  us         r7   r  zModelWithFunctionals.forward
  se    JJNNAq!9%JJNN1a OO$$Q*MM  ACC(
 r9   r  r  r  s   @r7   r  r  x
  s    .	r9   r  c                   ,     e Zd Zd fdZd Zd Z xZS )
ResNetBasec                 
   t         |           t        j                  }d}t        j                  ||dd      | _         ||      | _        t        j                         | _        t        j                         | _	        t        j                  j                         | _        t        j                  j                         | _        t        j                   d      | _        t        j                  j%                  |d      | _        y )Nr  r1  Fr,  rg   )r  r8   r   rK  r-  rl  r  rZ  r  r  r[   Identity
downsampler  r  myopAdaptiveAvgPool2davgpoolr  r}  r6   
norm_layerinplanesr  s      r7   r8   zResNetBase.__init__
  s    ^^
YYx6F
h'WWY
WWY
((++-LL002	++F3((//(A.r9   c                 X   | j                  |      }| j                  |      }| j                  |      }| j                  |      }| j                  j                  ||      }| j                  |      }| j                  |      }t        j                  |d      }| j                  |      }|S )Nrg   )rl  r  r  r  r  r{  r  r  r[   flattenr}  r6   r  rr  identitys       r7   r  zResNetBase.forward
  s    jjmhhsmjjo??1%iimmC*jjoll3mmC#ggcl
r9   c                     | j                   r0t        j                  j                  j	                  | g dgd       y t        j                  j                  j                  | g dgd       y )N)rl  r  r  Tr   rc  rG   s    r7   rg  zResNetBase.fuse_model
  s[    ==HH!!22014 3  HH!!..014 / r9   r  )rR   rS   rT   r8   r  rg  r  r  s   @r7   r  r  
  s    /
	r9   r  c                   &     e Zd Zd fdZd Z xZS )ModelMultipleOpsc                 x   t         |           t        j                  }d}t        j                  ||dd      | _        t        j                  ||dd      | _         ||      | _        t        j                         | _	        t        j                         | _
        t        j                  j                         | _        t        j                  j                         | _        t        j                  j                         | _        t        j$                  d      | _        t        j(                  dd      | _        y Nr  r1  Fr,  )r   r   r}     )r  r8   r   rK  r-  rl  rm  r  rZ  r  r  r[   r  r  r  r  skip_addrh  r  r  r  r}  r  s      r7   r8   zModelMultipleOps.__init__
  s    ^^
YYx6F
YYx6F
h'WWY
WWY
((++-446<<//1++F3))B"r9   c                    | j                  |      }| j                  |      }| j                  |      }| j                  |      }| j                  j                  ||      }| j                  |      }| j                  |      }| j                  |      }t        j                  j                  j                  |dd      }| j                  j                  ||g      }|j                  dd      }| j                  |      }|S NrV  r   r}  )rl  r  r  r  r  r{  r  r  rm  r[   r   
functional
max_pool2drh  r   r}  r  s       r7   r  zModelMultipleOps.forward
  s    jjmhhsmjjo??1%mmX.jjoll3jjohh!!,,S!Q7hhllC:&kk"i(ggcl
r9   r  r  r  s   @r7   r  r  
  s    #r9   r  c                   &     e Zd Zd fdZd Z xZS )ModelMultipleOpsNoAvgPoolc                 2   t         |           t        j                  }d}t        j                  ||dd      | _        t        j                  ||dd      | _         ||      | _        t        j                         | _	        t        j                         | _
        t        j                  j                         | _        t        j                  j                         | _        t        j                  d      | _        t        j"                  dd      | _        y r  )r  r8   r   rK  r-  rl  rm  r  rZ  r  r  r  r  r  rh  	MaxPool2dmaxpoolr  r}  r  s      r7   r8   z"ModelMultipleOpsNoAvgPool.__init__
  s    ^^
YYx6F
YYx6F
h'WWY
WWY
446<<//1||F+))B"r9   c                    | j                  |      }| j                  |      }| j                  |      }| j                  |      }| j                  j                  ||      }| j                  |      }| j                  |      }| j                  |      }t        j                  j                  j                  |dd      }| j                  j                  ||g      }|j                  dd      }| j                  |      }|S r  )rl  r  r  rm  r  r{  r  r  r[   r   r  r  rh  r   r}  )r6   r  rr  skips       r7   r  z!ModelMultipleOpsNoAvgPool.forward
  s    jjmhhsmjjozz!}mmT*jjoll3jjohh!!,,S!Q7hhllC:&kk"i(ggcl
r9   r  r  r  s   @r7   r  r  
  s    #r9   r  c                   &     e Zd Zd fdZd Z xZS )EmbeddingBagModulec                 t    t         |           t        j                  j	                  ddddd      | _        y )Nrf   r}  TFrs   r  )r  r8   r[   r   r  r~  r  s    r7   r8   zEmbeddingBagModule.__init__
  s8    88(( $$ ) 
r9   c                 (    | j                  |||      S rE   r~  )r6   r  r  r  s       r7   r  zEmbeddingBagModule.forward  s    xx*<==r9   r  r  r  s   @r7   r  r  
  s    
>r9   r  c                   &     e Zd Zd fdZd Z xZS )EmbeddingModulec                 n    t         |           t        j                  j	                  dd      | _        y Nrf   r}  r  r  r8   r[   r   r  r~  r  s    r7   r8   zEmbeddingModule.__init__	  s)    88%%Rr%Jr9   c                 $    | j                  |      S rE   r  r6   r  s     r7   r  zEmbeddingModule.forward  s    xx  r9   r  r  r  s   @r7   r  r    s    K!r9   r  c                   &     e Zd Zd fdZd Z xZS )EmbeddingWithStaticLinearc                 4   t         |           t        j                  j	                  dd      | _        t        j                  j                  dd      | _        t        | j
                  _	        t        | _	        t               | _        t               | _        y )Nrf   r}  r  r   rV  )r  r8   r[   r   r  r~  r  r}  r   r  r   r   r  r   r  r  s    r7   r8   z"EmbeddingWithStaticLinear.__init__  sd    88(("(M((//!Q'<&[
"}r9   c                     | j                  ||      }| j                  |      }| j                  |      }| j                  |      }t	        j
                  |g|gz   d      }|S r  )r~  r  r}  r  r[   rh  )r6   r  r  	linear_inr~  q_xr}  r  s           r7   r  z!EmbeddingWithStaticLinear.forward  sX    hhw(jj#WWS\\\"99bTSE\q1r9   r  r  r  s   @r7   r  r    s    %r9   r  c                   t     e Zd Z	 	 d fdZdej
                  dej
                  dej
                  fdZ xZS )DenseTopMLPrO  c                 
   t         |           t        j                  t        j                  ||            | _        t        j                  t        j                  ||z   |      t        j                  ||            | _        y rE   )r  r8   r   r  r  	dense_mlptop_mlp)r6   	dense_dim	dense_outr  
top_out_intop_out_outr  s         r7   r8   zDenseTopMLP.__init__%  sa     	IIi+
 }}IIi-/<IIj+.
r9   sparse_featuredensec                     | j                  |      }t        j                  |g|gz   d      }| j                  |      }|S r  )r  r[   rh  r  )r6   r  r  dense_featurer  rr  s         r7   r  zDenseTopMLP.forward2  s?    
 u-99m_/??QGll8$
r9   r  r  r  s   @r7   r  r  $  s=    
	
		 ||	 
		r9   r  c                   $     e Zd Z fdZd Z xZS )EmbBagWrapperc                 \    t         |           t        j                  ||d      | _        y )Nrs   )r  )r  r8   r   r  emb_bag)r6   r  r  r  s      r7   r8   zEmbBagWrapper.__init__A  s"    ~}5Qr9   c                 &    | j                  ||      S rE   )r  )r6   r  r  s      r7   r  zEmbBagWrapper.forwardE  s    ||GW--r9   r  r  s   @r7   r  r  @  s    R.r9   r  c                        e Zd ZdZdZdZdZdZdZdZ	d fdZ
dej                  d	ej                  d
ej                  dej                  fdZ xZS )SparseNNModelrf   r   r   rV  rg   rO  c                     t         |           t        | j                  | j                        | _        t        | j                  | j                  | j                  | j                  | j                        | _        y rE   )r  r8   r  _NUM_EMBEDDINGS_EMBEDDING_DIMmodel_sparser  
_DENSE_DIM_DENSE_OUTPUT_TOP_OUT_IN_TOP_OUT_OUT	dense_topr  s    r7   r8   zSparseNNModel.__init__R  s`    )$*>*>@S@ST$OO
r9   sparse_indicessparse_offsetsr  c                 N    | j                  ||      }| j                  ||      }|S rE   )r  r   )r6   r!  r"  r  r  rr  s         r7   r  zSparseNNModel.forward^  s+     **>>Jnn^U3
r9   r  )rR   rS   rT   r  r  r  r  r  r  _TOP_MLP_DIMr8   r[   ro  r  r  r  s   @r7   r  r  I  sf    ONJMKLL

		 	 ||		
 
	r9   r  c                      e Zd Z G d dej                  j
                        Z G d dej                  j
                        Z G d dej                  j
                        Z G d dej                  j
                        Z	 G d	 d
ej                  j
                        Z
 G d dej                  j
                        Z G d dej                  j
                        Z G d dej                  j
                        Z G d dej                  j
                        Z G d dej                  j
                        Z G d dej                  j
                        Z G d dej                  j
                        Z G d dej                  j
                        Z G d dej                  j
                        Z G d dej                  j
                        Z G d d ej                  j
                        Z G d! d"ej                  j
                        Z G d# d$ej                  j
                        Z G d% d&ej                  j
                        Z G d' d(ej                  j
                        Z G d) d*ej                  j
                        Z G d+ d,ej                  j
                        Z G d- d.ej                  j
                        Z G d/ d0ej                  j
                        Zy1)2TestHelperModulesc            
           e Zd Zdej                  dej                  dej                  dej                  dej                  f
dZd Zy)	TestHelperModules.ControlFlowxspred1pred2r  rO  c           
      \  	 dt         j                  dt         j                  fd	dt         j                  dt         j                  fddt         j                  dt         j                  dt         j                  f	fddt         j                  dt         j                  fddt         j                  d	t         j                  dt         j                  dt         j                  dt         j                  f
fd
}t        j                  ||      }t        j                  |||||      S )Nr  rO  c                 <    | | z   } t        j                  | |       } | S rE   r[   mmr  s    r7   true_nestedz:TestHelperModules.ControlFlow.forward.<locals>.true_nesteds  s    EHHQNr9   c                 .    t        j                  | |       S rE   r.  r0  s    r7   false_nestedz;TestHelperModules.ControlFlow.forward.<locals>.false_nestedx  s    xx1~%r9   r  r+  c                 @    t        j                  || g      }| |z   S rE   )r   cond)r  r+  r  r3  r1  s      r7   true_fnz6TestHelperModules.ControlFlow.forward.<locals>.true_fn{  s$     %%e[,L1ur9   c                 "    | j                         S rE   )cos)r  rz   s     r7   false_fnz7TestHelperModules.ControlFlow.forward.<locals>.false_fn  s    uuwr9   r*  c                     | j                         } t        j                  |||g      }| |z   } | j                         S rE   )r8  r   r5  sin)r  r*  r+  r  r9  r6  s       r7   map_fnz5TestHelperModules.ControlFlow.forward.<locals>.map_fn  s>     EEG %%eWhE
KEuuwr9   )r[   ro  r/  r   rG  )
r6   r)  r*  r+  r  r<  r9  r3  r6  r1  s
         @@@@r7   r  z%TestHelperModules.ControlFlow.forwardl  s    u||  
& & &5<<   ELL  	<<	||	 ||	 <<		
 	 AA##FBua@@r9   c                     t        j                  dd      t        j                  dg      t        j                  dg      t        j                  dd      fS )NrV  F)r[   onesr   rG   s    r7   r  z,TestHelperModules.ControlFlow.example_inputs  sD    

1a eW%eW%

1a 	 r9   N)rR   rS   rT   r[   ro  r  r  rW   r9   r7   ControlFlowr(  k  sW    "	A"	A <<"	A <<	"	A
 ||"	A \\"	AH	r9   r?  c                   &     e Zd Zd fdZd Z xZS )%TestHelperModules.Conv2dPropAnnotatonc                     t         |           t        j                  j	                  ddd      | _        t        j                  j                  dd      | _        y Nr  )r  r8   r[   r   r-  r.  r  r  r  s    r7   r8   z.TestHelperModules.Conv2dPropAnnotaton.__init__  s<    G1a0DI((//!Q/DKr9   c                     | j                  |      }|j                  dd      }t        j                  j                  j                  |dd      }| j                  |      }|S )Nr   r  g      rj  )r.  r   r[   r   r  hardtanhr  r  s     r7   r  z-TestHelperModules.Conv2dPropAnnotaton.forward  sN    		!Ar1A##,,Qc:AAAHr9   r  r  r  s   @r7   Conv2dPropAnnotatonrA    s    	0
	r9   rF  c                   &     e Zd Zd fdZd Z xZS ))TestHelperModules.Conv2dWithObsSharingOpsc                     t         |           t        j                  j	                  ddd      | _        t        j                  j                         | _        t        j                  j                  d      | _	        y Nr  r1  )
r  r8   r[   r   r-  r.  HardtanhrE  r  adaptive_avg_pool2dr  s    r7   r8   z2TestHelperModules.Conv2dWithObsSharingOps.__init__  sQ    G1a0DI!HH--/DM',xx'A'A&'ID$r9   c                     | j                  |      }| j                  |      }| j                  |      }t        j                  |      }|S rE   )r.  rL  rE  r[   meanr  s     r7   r  z1TestHelperModules.Conv2dWithObsSharingOps.forward  s?    		!A((+Aa A

1AHr9   r  r  r  s   @r7   Conv2dWithObsSharingOpsrH    s    	J	r9   rO  c                   &     e Zd Zd fdZd Z xZS ),TestHelperModules.Conv2dWithTwoLinearPermutec                    t         |           t        j                  j	                  ddd      | _        t        j                  j                  ddd      | _        t        j                  j                  dd      | _        y )Nr     r  Fr,  	r  r8   r[   r   r-  r.  r  r  r  r  s    r7   r8   z5TestHelperModules.Conv2dWithTwoLinearPermute.__init__  V    G2q1DI 88??2qu?=DL 88??1a0DLr9   c                     | j                  |      }t        j                  |d      }| j                  | j	                  |            S Nr   rV  r  rg   )r.  r[   permuter  r  r6   r  conv_outpermute_outs       r7   r  z4TestHelperModules.Conv2dWithTwoLinearPermute.forward  s7    yy|H--,?K<<[ 9::r9   r  r  r  s   @r7   Conv2dWithTwoLinearPermuterQ        	1	;r9   r]  c                   &     e Zd Zd fdZd Z xZS )%TestHelperModules.Conv2dWithTwoLinearc                    t         |           t        j                  j	                  ddd      | _        t        j                  j                  ddd      | _        t        j                  j                  dd      | _        y )Nr  rS  ru  r  Fr,  rT  r  s    r7   r8   z.TestHelperModules.Conv2dWithTwoLinear.__init__  rU  r9   c                     | j                  |      }t        j                  |d      }| j                  | j	                  |            S )N)rV  ru  )r.  r[   r   r  r  )r6   r  r[  reshape_outs       r7   r  z-TestHelperModules.Conv2dWithTwoLinear.forward  s7    yy|H--':K<<[ 9::r9   r  r  r  s   @r7   Conv2dWithTwoLinearr`    r^  r9   rd  c                   &     e Zd Zd fdZd Z xZS )$TestHelperModules.ConvLinearWPermutec                     t         |           t        j                  j	                  ddd      | _        t        j                  j                  dd      | _        y )Nr  r  )r  r8   r[   r   r-  r.  r  r  r  s    r7   r8   z-TestHelperModules.ConvLinearWPermute.__init__  s<    G1a0DI 88??1a0DLr9   c                 r    | j                  |      }t        j                  |d      }| j                  |      S rW  )r.  r[   rY  r  rZ  s       r7   r  z,TestHelperModules.ConvLinearWPermute.forward  s.    yy|H--,?K<<,,r9   r  r  r  s   @r7   ConvLinearWPermuterf    s    	1
	-r9   ri  c                   ,     e Zd Zd fdZd Zd Z xZS )!TestHelperModules.TwoLinearModulec                     t         |           t        j                  j	                  ddd      | _        t        j                  j	                  dd      | _        y )Nr  rS  Fr,  )r  r8   r[   r   r  r  r  r  s    r7   r8   z*TestHelperModules.TwoLinearModule.__init__  s>    G 88??1bu?=DL 88??2q1DLr9   c                 B    | j                  | j                  |            S rE   )r  r  r  s     r7   r  z)TestHelperModules.TwoLinearModule.forward  s    <<Q00r9   c                 0    t        j                  dd      fS )NrV  r  r[   randnrG   s    r7   r  z0TestHelperModules.TwoLinearModule.example_inputs  s    KK1%''r9   r  rR   rS   rT   r8   r  r  r  r  s   @r7   TwoLinearModulerk    s    	2
	1	(r9   rr  c                   &     e Zd Zd fdZd Z xZS )TestHelperModules.ConvMaxPool2dc                     t         |           t        j                  j	                  ddd      | _        t        j                  j                  dd      | _        y )NrV  rg   )r  r8   r[   r   r-  r.  r  poolr  s    r7   r8   z(TestHelperModules.ConvMaxPool2d.__init__  s>    G1a0DI**1a0DIr9   c                 J    | j                  |      }| j                  |      }|S rE   )r.  rv  r  s     r7   r  z'TestHelperModules.ConvMaxPool2d.forward  s!    		!A		!AHr9   r  r  r  s   @r7   ConvMaxPool2drt    s    	1
	r9   rx  c                   &     e Zd Zd fdZd Z xZS )+TestHelperModules.ConvWithAdaptiveAvgPool2dc                     t         |           t        j                  j	                  ddd      | _        t        j                  j                  d      | _        y rJ  )r  r8   r[   r   r-  r.  r  rL  r  s    r7   r8   z4TestHelperModules.ConvWithAdaptiveAvgPool2d.__init__  s=    G1a0DI',xx'A'A&'ID$r9   c                 J    | j                  |      }| j                  |      }|S rE   )r.  rL  r  s     r7   r  z3TestHelperModules.ConvWithAdaptiveAvgPool2d.forward  s$    		!A((+AHr9   r  r  r  s   @r7   ConvWithAdaptiveAvgPool2drz    s    	J
	r9   r}  c                   &     e Zd Zd fd	Zd Z xZS ) TestHelperModules.ConvWithBNReluc                    t         |           t        j                  j                  t        j                  j
                  t        j                  j                  d}t        j                  j                  t        j                  j                  t        j                  j                  d} ||   ddd||      | _
        |r ||   d      | _        n#t        j                  j                         | _        |r$t        j                  j                         | _        y t        j                  j                         | _        y )Nr  r  )r  r3  )r  r8   r[   r   r  r-  r  r  rK  r  r.  rL  r  rZ  r  )	r6   r  rX  rL  r  r3  convsbnsr  s	           r7   r8   z)TestHelperModules.ConvWithBNRelu.__init__  s    GEHHOOPE88''88''88''C
 #c
1awGDI"#c(1+((++-!HHMMO	!HH--/	r9   c                 h    | j                  |      }| j                  |      }| j                  |      S rE   r\  r  s     r7   r  z(TestHelperModules.ConvWithBNRelu.forward  s*    		!A
A99Q<r9   )rV  TTr   r  r  s   @r7   ConvWithBNRelur    s    	0&	 r9   r  c                   &     e Zd Zd fd	Zd Z xZS )!TestHelperModules.ConvTWithBNReluc                    t         |           t        j                  j                  t        j                  j
                  d}t        j                  j                  t        j                  j                  d} ||   ddd|      | _        |r ||   d      | _	        n#t        j                  j                         | _	        |r$t        j                  j                         | _        y t        j                  j                         | _        y )N)rg   rV  r  r,  )r  r8   r[   r   r  r8  r  rK  convtrL  r  rZ  r  )r6   r  rX  rL  r  convtsr  r  s          r7   r8   z*TestHelperModules.ConvTWithBNRelu.__init__  s    G11ehh6N6NOFhh**uxx/C/CDC$Q148DJ"#c(1+((++-!HHMMO	!HH--/	r9   c                 h    | j                  |      }| j                  |      }| j                  |      S rE   )r  rL  r  r  s     r7   r  z)TestHelperModules.ConvTWithBNRelu.forward!  s*    

1A
A99Q<r9   )rV  TTr  r  s   @r7   ConvTWithBNRelur    s    	0	 r9   r  c                   ,     e Zd Zd fdZd Zd Z xZS )"TestHelperModules.Conv2dThenConv1dc                     t         |           t        j                  j	                  ddd      | _        t        j                  j                  ddd      | _        y rC  )r  r8   r[   r   r  ri  r-  r6  r  s    r7   r8   z+TestHelperModules.Conv2dThenConv1d.__init__'  s>    G((//!Q2DK((//!Q2DKr9   c                 l    | j                  |      }|j                  d      }| j                  |      }|S r   )r6  r}  ri  r  s     r7   r  z*TestHelperModules.Conv2dThenConv1d.forward,  s.    AA		!AAAHr9   c                 4    t        j                  dddd      fS r3  ro  rG   s    r7   r  z1TestHelperModules.Conv2dThenConv1d.example_inputs2  s    KK1a+--r9   r  rq  r  s   @r7   Conv2dThenConv1dr  &  s    	3
		.r9   r  c                   &     e Zd Zd fdZd Z xZS )TestHelperModules.Conv2dWithCatc                     t         |           t        j                  j	                  ddd      | _        t        j                  j	                  ddd      | _        y rC  r  r8   r[   r   r-  rl  rm  r  s    r7   r8   z(TestHelperModules.Conv2dWithCat.__init__6  >    GAq1DJAq1DJr9   c                 |    | j                  |      }| j                  |      }t        j                  ||gd      }|S r  rl  rm  r[   rh  )r6   r  r  r  s       r7   r  z'TestHelperModules.Conv2dWithCat.forward;  s5    

1A

1A		1a&a(AHr9   r  r  r  s   @r7   Conv2dWithCatr  5  s    	2
	r9   r  c                   &     e Zd Zd fdZd Z xZS )"TestHelperModules.Conv2dWithTwoCatc                     t         |           t        j                  j	                  ddd      | _        t        j                  j	                  ddd      | _        y rC  r  r  s    r7   r8   z+TestHelperModules.Conv2dWithTwoCat.__init__B  r  r9   c                     | j                  |      }| j                  |      }t        j                  ||gd      }||z   }t        j                  ||g      }|S r  r  r6   r  r  x3x4r  r  ri  s           r7   r  z*TestHelperModules.Conv2dWithTwoCat.forwardG  sP    BBBB		2r(*ARA		1a&!AHr9   r  r  r  s   @r7   Conv2dWithTwoCatr  A  s    	2
	r9   r  c                   ,     e Zd Zd fdZd Zd Z xZS )!TestHelperModules.Conv2dWithSplitc                     t         |           t        j                  j	                  ddd      | _        t        j                  j	                  ddd      | _        y rC  r  r  s    r7   r8   z*TestHelperModules.Conv2dWithSplit.__init__P  r  r9   c                     | j                  |      }t        j                  |dd      \  }}t        j                  ||gd      }|S )NrV  rg   r  )rl  r[   splitrh  )r6   r  r  r  r  s        r7   r  z)TestHelperModules.Conv2dWithSplit.forwardU  s>    

1A[[A1-FB		2r(*AHr9   c                 4    t        j                  dddd      fS )Nrg   r  rS  ro  rG   s    r7   r  z0TestHelperModules.Conv2dWithSplit.example_inputs\      KK1b"-//r9   r  rq  r  s   @r7   Conv2dWithSplitr  O  s    	2
		0r9   r  c                       e Zd Zd Zy)TestHelperModules.ThreeAddc                 $    ||z   }||z   }||z   }|S rE   rW   r  s           r7   r  z"TestHelperModules.ThreeAdd.forward`  s"    RARAAAHr9   NrR   rS   rT   r  rW   r9   r7   ThreeAddr  _  s    	r9   r  c                   &     e Zd Zd fdZd Z xZS )!TestHelperModules.EmbeddingModulec                 n    t         |           t        j                  j	                  dd      | _        y r  r  r  s    r7   r8   z*TestHelperModules.EmbeddingModule.__init__g  s)    Gxx))2)NDHr9   c                 $    | j                  |      S rE   r  r  s     r7   r  z)TestHelperModules.EmbeddingModule.forwardk  s    88G$$r9   r  r  r  s   @r7   r  r  f  s    	O	%r9   r  c                   &     e Zd Zd fdZd Z xZS )+TestHelperModules.EmbeddingConvLinearModulec                    t         |           t        j                  j	                  dd      | _        t        j                  j                  ddd      | _        t        j                  j                  dd      | _	        y )Nrf   r  r  rS  )rg   r  )
r  r8   r[   r   r  r~  r-  r.  r  r  r  s    r7   r8   z4TestHelperModules.EmbeddingConvLinearModule.__init__o  sW    Gxx))1)MDH2v6DI((//"a0DKr9   c                    | j                  |      }t        j                  |d      }t        j                  |d      }| j	                  |      }t        j                  |d      }t        j
                  |d      }| j                  |      S )Nr   r  )r   r  rg   rV  rX  )r~  r[   r  rY  r.  r}  r  )r6   r  
embeddingsr[  s       r7   r  z3TestHelperModules.EmbeddingConvLinearModule.forwardu  sm    '*J;Jz<@Jyy,H}}X|<H}}X15H;;x((r9   r  r  r  s   @r7   EmbeddingConvLinearModuler  n  s    	1	)r9   r  c                       e Zd Zd Zy)TestHelperModules.AddInplaceAddc                     ||z   }||z  }|S rE   rW   r  s      r7   r  z'TestHelperModules.AddInplaceAdd.forward      AAFAHr9   Nr  rW   r9   r7   AddInplaceAddr  ~      	r9   r  c                       e Zd Zd Zy)TestHelperModules.MulInplaceMulc                     ||z  }||z  }|S rE   rW   r  s      r7   r  z'TestHelperModules.MulInplaceMul.forward  r  r9   Nr  rW   r9   r7   MulInplaceMulr    r  r9   r  c                       e Zd Zd Zy)TestHelperModules.AddMulScalarc                 .    |dz   }|dz  }|dz  }|dz  }|S rC  rW   r  s     r7   r  z&TestHelperModules.AddMulScalar.forward  s+    AAAAFAFAHr9   Nr  rW   r9   r7   AddMulScalarr    s    	r9   r  c                   &     e Zd Zd fdZd Z xZS )+TestHelperModules.ConvBnReLU2dAndLinearReLUc                     t         |           t        j                  d      | _        t
        j                  j                  ddd      | _        t
        j                  j                         | _
        y )NT)r  r  r  Fr,  )r  r8   r&  r  conv_bn_relur[   r   r  r  rZ  r  r  s    r7   r8   z4TestHelperModules.ConvBnReLU2dAndLinearReLU.__init__  sO    G 1 @ @d @ KD((//!QU/;DKDIr9   c                 v    | j                  |      }t        j                  |d      }| j                  |      }|S rW  )r  r[   rY  r  )r6   r  r\  
linear_outs       r7   r  z3TestHelperModules.ConvBnReLU2dAndLinearReLU.forward  s6    !!!$A--<8K[1Jr9   r  r  r  s   @r7   ConvBnReLU2dAndLinearReLUr    s    	(	r9   r  c                   ,     e Zd Zd fdZd Zd Z xZS )!TestHelperModules.GroupwiseConv2dc                 r    t         |           t        j                  j	                  dddd      | _        y )Nr   r  rV  )r   )r  r8   r[   r   r-  r.  r  s    r7   r8   z*TestHelperModules.GroupwiseConv2d.__init__  s*    G1a:DIr9   c                 $    | j                  |      S rE   r0  r  s     r7   r  z)TestHelperModules.GroupwiseConv2d.forward  s    99Q<r9   c                 4    t        j                  dddd      fS )NrV  r   rf   ro  rG   s    r7   r  z0TestHelperModules.GroupwiseConv2d.example_inputs  r  r9   r  rq  r  s   @r7   GroupwiseConv2dr    s    	;	 	0r9   r  c                   &     e Zd Zd fdZd Z xZS )!TestHelperModules.LinearReluModelc                     t         |           t        j                  j	                  dd      j                  t        j                        | _        t        j                  j                         | _	        y r  r  r  s    r7   r8   z*TestHelperModules.LinearReluModel.__init__  sG    Ghhooa+..U[[.ADGDIr9   c                 F    | j                  | j                  |            }|S rE   r  r  s     r7   r  z)TestHelperModules.LinearReluModel.forward  s    		$''!*%AHr9   r  r  r  s   @r7   r  r    s    	(
	r9   r  N)rR   rS   rT   r[   r   r  r?  rF  rO  r]  rd  ri  rr  rx  r}  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  rW   r9   r7   r&  r&  j  s   +ehhoo +Zehhoo %((// 
;UXX__ 
;
;ehhoo 
;	-UXX__ 	-
(%((// 
(	 		EHHOO 	   2 %((//  *.588?? .
 
588?? 0%((// 0 588?? %%((// %)EHHOO )   uxx EHHOO 	0%((// 	0%((// r9   r&  c                 z     G d dt         j                  j                        } || |||      j                         S )a  
    Generate a linear module with quantize-dequantize (reference quantized)
    with static quantization parameters (no choose_qparams at runtime).
    A simulation to PT2E quantization in Torchao.
    It is used to test fusion and lowering passes in Inductor for X86 CPU.
    Input quantization limit is 0-127 to avoid overflow on old platforms.
    Params:
        N: output feature dimension
        K: input feature dimension
        bias: boolean flag to indicate whether linear module has bias
        example_input: example input tensor to get scale/zero point
    Return:
        An instance of the reference quantized linear module
    c                   $     e Zd Z fdZd Z xZS )8_static_reference_quantized_linear_module.<locals>.Modelc           	         t         |           t        j                  j                  j
                  j                  |ddt        j                  t        j                        j                  t        j                        \  | _        | _        | j                  j                         j                         | j                  j                         j                         c| _        | _        t        j                  j!                  |||      | _        t        j                  j                  j%                  | j"                  j&                  t        j(                        \  | _        | _        | j*                  j                         j/                  t        j                        j1                         | _        | j,                  j                         j/                  t        j2                        j1                         | _        t        j                  j                  j4                  j7                  | j"                  j&                  | j*                  | j,                  dddt        j(                        | _        y )Nr      )r  r  rx  r   r   axisr  r  r   )r  r8   r[   r4  quantized_decomposedchoose_qparamsr   rw  rz  rx  rg  x_scaler  detachrp   r   r  r  choose_qparams_per_tokenr  r|  w_scalesw_zpsr   r}  r  r   defaultqw)r6   NKr  example_inputr  s        r7   r8   zA_static_reference_quantized_linear_module.<locals>.Model.__init__  s   G&+ii&D&D&S&S&Z&ZKK.22kk '[ '#DL$) '+ll&9&9&;&@&@&BDIIDTDTDVD[D[D]#DL$)((//!Q5DK(-		(F(F(_(_""%** )` )%DM4: !MM00255emmDLLNDM**,//<DDFDJii44IIQQ""

jj R DGr9   c           	         t         j                  j                  j                  j	                  | j
                  | j                  | j                  dddt         j                        }t         j                  j                  j                  j	                  || j                  | j                  ddt         j                        }t         j                  j                  j                  j	                  || j                  | j                  ddt         j                        }t         j                  j                  j                  j	                  ||| j                  j                         }|S )Nr   r  r  r  )r  r  r   )r[   r4  r  dequantize_per_channelr  r  r  r  r|  r   r  r  rg  dequantize_per_tensoratenr  r  )r6   r  dqwquantize_per_tensor_defaultdequantize_per_tensor_defaultr  s         r7   r  z@_static_reference_quantized_linear_module.<locals>.Model.forward  s   ))00GGOO

jj P C +0))*H*H*\*\*d*d		kk +e +' -2II,J,J,`,`,h,h+		kk -i -) YY^^**223PRUW[WbWbWgWghFMr9   r  r  s   @r7   Modelr    s    	4	r9   r  )r[   r   r  r"  )r  r  r  r  r  s        r7   )_static_reference_quantized_linear_moduler    s3    6 6p At]+0022r9   )r   rE   )r   rS  )r       )rU   r[   'torch.ao.nn.intrinsic.quantized.dynamicr  r   	intrinsicr  r+  r  torch.ao.nn.quantizedr  torch.ao.nn.quantized.dynamicr  torch.distributeddistributedr   torch.nntorch.nn.functionalr  r  functorch.experimentalr   torch.ao.nn.intrinsicr   torch.ao.quantizationr   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   +torch.ao.quantization.quantization_mappingsr   r    r!   torch.jit.mobiler"   (torch.testing._internal.common_quantizedr#   $torch.testing._internal.common_utilsr$   r%   torch.ao.ns.fx.ns_typesr&   r'   !torch.ao.quantization.quantize_fxr(   r)   r*   r+   torch.fxr,   torch.fx.graphr-   r  ImportErrorr$  r!  r  r   r  typingr.   collections.abcr/   numpyrL  torch._dynamo_dynamor>  torch.testingr0   r2   r]   rb   CrossEntropyLoss_default_loss_fnr   r   r   r   r   r   r   r   r   r	  r  r$  r*  r-  r1  r7  r;  r@  rD  torchvisionHAS_TORCHVISIONskipIfskip_if_no_torchvisionrJ  r  rT  rt  r  r  r  r  r  r  r  r  r  r  r  r#  r)  r6  r<  rD  rI  rR  rW  r_  rj  rs  rz  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r
  r  r  r   r%  r*  r/  r9  r@  rE  rJ  rO  rX  r]  rc  rh  rn  rs  rx  r{  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r&  r  rW   r9   r7   <module>r     s    7 7 7 # # , ,      / .      8  8 N IL  %#F   	 	   $  # ##7 #7L 88,,.  3C &.. .2" F!O,P5f$$($$$$$O )_)<>NO P ')hh 	3!l'D2BU>8 U>p23 2p
#UXX__ 
##ehhoo ##EHHOO ##RYY # ehhoo %((// $ 
) 
)
) 
)) )$)%((// )$)%((// ))588?? )()ehhoo )")uxx )B) )#%((// #.ryy .#588?? # 588?? "#ehhoo ##EHHOO #"# #&#UXX__ #&#ehhoo #<@ <@@)EHHOO )) )$)uxx )&UXX__ ,%((// 588?? (ehhoo  uxx 4EHHOO  S%((// S6
#uxx 
#
1uxx 
11EHHOO 11uxx 1 0		 01bii 1")uxx )2
/UXX__ 
//%((// /.bii ./")) /" UXX__  "'ehhoo '(UXX__  588?? $EHHOO $%((// uxx 6@#; @ryy ,		 2			 	'BII ',RYY ,^
 
		 2RYY ,	#RYY 	#EHHOO BII ,588?? 0# #Luxx H >> >!ehhoo ! &")) 8.BII .BII BH HT
G3ac  F`  Os$   ,"i6 -j 6j jjj