
    iE                        S SK r S SKrS SKrS SKrS SKJr  S SKJrJr  S SK	J
r
Jr  S SKJr  S SKJr  S SKrS SKJr  S SKJr  S S	KJr  S S
KJrJrJr  S SKJr   " S S\5      r\R<                  " \" S5      (       + S5       " S S\5      5       rS r  " S S\RB                  RD                  5      r# " S S\RB                  RD                  5      r$S r%S r&    SS jr' SSSSSSSS.S jjr(g)     N)get_executorch_backend_config)#_convert_to_reference_decomposed_fx
prepare_fx)NodeSpecQuantizationTestCase)TestCase)clone_preserve_strides_offset)FROM_NODE_KEY)_extract_node_source_debug_infobfs_trace_with_node_process)convert_pt2eprepare_pt2eprepare_qat_pt2e)torch_version_at_leastc                      \ rS rSrS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                  \R                  R                  R                  R                  \R                  R                  R                  R                  \R                  R                  R                  R                  \R                  R                  R                  R                  \R                  R                  R                  R                  0r       SS jrSrg)PT2EQuantizationTestCase&   z=
Base QuantizationTestCase for PT2 with some helper methods.
Nc                 B   [         R                  R                  5         UR                  5       n[        R
                  " U5      n[        S [        [        U5      5       5       5      n[         R                  R                  UUU(       a  UOS SS9R                  5       nU	(       a  [        X5      nO[        X5      nU
(       a  [        SU5        U" U6   [        U5      nU
(       a  [        SU5        U" U6 n[        nUR!                  5        VVs0 s H  u  nnUR#                  U5      U_M     nnnUc  / nU Vs/ s H  nUR#                  U5      PM     nnU R%                  UUUS9  U(       Ga  Un['        5       n[        R
                  " U5      n[)        UUUUS9nU" U6   [+        UUS9n[         R                  R                  UUU(       a  UOS S9R                  5       n0 n[,        R.                  R!                  5        H%  u  nnUU;   d  M  UU   UUR#                  U5      '   M'     Ub5  UR!                  5        VVs0 s H  u  nnUR#                  U5      U_M     nnnU R%                  UUS	9  U" U6 nU R1                  UU5        U$ s  snnf s  snf s  snnf )
Nc              3   t   #    U  H.  nUS :X  a!  S [         R                  R                  S5      0OSv   M0     g7f)r   dimN)torchexportDim).0is     Y/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/torchao/testing/pt2e/utils.py	<genexpr>;PT2EQuantizationTestCase._test_quantizer.<locals>.<genexpr>I   s6      
/ -.FQ  '(</s   68T)dynamic_shapesstrictzprepared model:zquantized model)expected_node_occurrenceexpected_node_list)backend_config)r    )r"   )r   _dynamoresetevalcopydeepcopytuplerangelenr   moduler   r   printr   r   itemscall_functioncheckGraphModuleNodesr   r   r   r   _MAP_TO_FX_TRACED_OPSassertEqual)selfmodelexample_inputs	quantizerr"   r#   check_against_fx_quantfx_qconfig_mappingexport_with_dynamic_shapeis_qatis_debug_modetraining_ir_node_occurrencem_eagermr    pt2_quant_outputnskvnode_occurrencen	node_listqconfig_mappingr$   m_copym_fxfx_quant_outputs                              r   _test_quantizer(PT2EQuantizationTestCase._test_quantizer4   s     	**, MM'" 
3~./
 
 LL-F>D	   

 &( 	
  .AQ*A#Q'	>O#Q'n-/G/M/M/O
/Otq!BQ"/O 	 
 %!#2DE2DQR%%a(2D	E""I 	# 	
 "0O:<N]]7+FD .!6^D <<&&1J~PT '  fh	 
 !O0FFLLN100;STU;VOB$4$4Q$78 O +6 !< A A C# C1 $$Q'* C   # &&to&V"N3O_.>?K

 F2#s   J=JJ )NFNFFFN)__name__
__module____qualname____firstlineno____doc__r   opsquantized_decomposedquantize_per_tensordefaultdequantize_per_tensorquantize_per_channeldequantize_per_channeltensorr2   rK   __static_attributes__rM       r   r   r   &   sD   
 			&&::EII<Z<Z<n<n<v<v		&&<<eii>\>\>r>r>z>z		&&;;UYY=[=[=p=p=x=x		&&==uyy?]?]?t?t?|?|		&&::AA599CaCaCuCuC|C|		&&<<CCUYYEcEcEyEy  FA  FA  $"'$(Sr\   r   z2.7.0zRequires torch 2.7+c                   X    \ rS rSrSrS
S jrS\\\4   4S jr	S\\\4   4S jr
S rS	rg)PT2ENumericDebuggerTestCase   z
Base test case class for PT2E numeric debugger tests containing common utility functions
for numeric debugging functionality.
returnNc                 (   ^  U 4S jn[        X5        g )Nc                 .  > U R                   S:X  d  U R                   S:X  a  g [        U R                  ;  d  U R                  [           c  g U R                  [           S   R                  S:X  a  g TR	                  [        U R                  SU  S35        g )Nplaceholderoutputz!ExportedProgram.module().unlift()Node z doesn't have from_node info)opr
   meta	pass_nameassertIn)noder4   s    r   !_assert_node_has_from_node_sourcemPT2ENumericDebuggerTestCase._assert_each_node_has_from_node_source.<locals>._assert_node_has_from_node_source   s    ww-'477h+> DII-=1I1Q  		-(,6667  MM		v9:r\   r   )r4   r5   rl   s   `  r   &_assert_each_node_has_from_node_sourceBPT2ENumericDebuggerTestCase._assert_each_node_has_from_node_source   s    	2 	$EMr\   c                 .   ^ 0 mU4S jn[        X5        T$ )Nc                 l   > [        U 5      =nb%  UR                  UR                  4T[        U 5      '   g g N)r   namegraph_idstr)rk   root_node_sourcefrom_node_source_maps     r   #_extract_from_node_source_from_nodebPT2ENumericDebuggerTestCase._extract_from_node_source.<locals>._extract_from_node_source_from_node   s=    $CD$II V$))$--3$SY/ Wr\   r   )r4   r5   rx   rw   s      @r   _extract_from_node_source5PT2ENumericDebuggerTestCase._extract_from_node_source   s    /1	 	$EO##r\   c                 .   ^ 0 mU4S jn[        X5        T$ )Nc                   > [         U R                  ;   aj  U R                  [            bU  [        U R                  R                  S5      5      n[	        U 5      nUT;  a  UTU'   g TU   U:X  d   SU  SU SU 35       eg g g )Nnn_module_stackrf   z has different from_node info z.than previous node sharing the same decomp op )r
   rh   ru   getr   )rk   prev_decomp_opfrom_node_source&prev_decomp_op_to_from_node_source_maps      r   7_extract_from_node_source_with_prev_decomp_op_from_nodeڊPT2ENumericDebuggerTestCase._extract_from_node_source_with_prev_decomp_op.<locals>._extract_from_node_source_with_prev_decomp_op_from_node   s    		)dii.F.R!$TYY]]3D%E!F#B4#H !)OO( ;>J
 ?~N+,  v%CDTCUHHXZ	, /S)r\   r   )r4   r5   r   r   s      @r   -_extract_from_node_source_with_prev_decomp_opIPT2ENumericDebuggerTestCase._extract_from_node_source_with_prev_decomp_op   s#    AC.	$ 	$	
 65r\   c                     U R                  UR                  UR                  :H  =(       a    UR                  UR                  :H  5        g rr   )
assertTruers   rt   )r4   node_source_1node_source_2s      r   assertNodeSourcesEqual2PT2ENumericDebuggerTestCase.assertNodeSourcesEqual   s<    -"4"44 A&&-*@*@@	
r\   rM   )r`   N)rN   rO   rP   rQ   rR   rn   dictru   anyrz   r   r   r[   rM   r\   r   r^   r^      s;    
N8$$sCx. $6d3PS8n 64
r\   r^   c                 z    SSK Js  Js  Js  Jn  SSK Jn  U" 5       nUR                  UR                  XS95        U$ )z^
Create a default X86InductorQuantizer configured for the given mode (QAT and dynamic quant).
r   N)X86InductorQuantizer)r;   
is_dynamic):torchao.quantization.pt2e.quantizer.x86_inductor_quantizerquantizationpt2er7   x86_inductor_quantizerr   
set_global,get_default_x86_inductor_quantization_config)r;   r   xiqr   r7   s        r   get_default_x86_quantizerr      sF     ML %&I88 	9 	

 r\   c                   2   ^  \ rS rSrSrU 4S jrS rSrU =r$ )FP8QDQLinear   zU
Float8 quantize-dequantize (QDQ, a.k.a., fake quant) linear layer used for testing.
c                 (  > [         TU ]  5         [        R                  U l        [        R
                  " X!45      R                  U R                  5      U l        SU l        SU l	        S U l
        U(       a  [        R
                  " U45      U l
        g g Ng       @)super__init__r   float8_e4m3fnqtyperandntoweightweight_scalescalebias)r4   in_featuresout_featureshas_bias	__class__s       r   r   FP8QDQLinear.__init__   sm    ((
kk<"=>AA$**M
	\O4DI r\   c                    [         R                  R                  R                  R	                  U R
                  R                  [         R                  " U R                  /5      [         R                  S9n[         R                  R                  R                  R	                  U[         R                  " U R                  /5      U R                  S9n[         R                  R                  R                  R	                  U[         R                  " U R                  /5      [         R                  S9n[         R                  R                  R                  XBU R                   5      nU$ N)rZ   r   output_dtype)rZ   r   float8_dtype)r   rS   torchao'dequantize_affine_float8_non_decomposedrV   r   datarZ   r   float%quantize_affine_float8_non_decomposedr   r   nn
functionallinearr   )r4   inputr   q_inputdq_inputouts         r   forwardFP8QDQLinear.forward   s    ""JJRR;;##,, 1 123 S 
 ))##IIQQ,,

|, R 

 99$$LLTT,,

|, U 
 hh!!((499E
r\   )r   r   r   r   r   	rN   rO   rP   rQ   rR   r   r   r[   __classcell__r   s   @r   r   r      s    5 r\   r   c                   @   ^  \ rS rSrSr     SU 4S jjrS rSrU =r$ )FP8QDQConv2di  zU
Float8 quantize-dequantize (QDQ, a.k.a., fake quant) conv2d layer used for testing.
c	                 b  > [         T	U ]  5         [        R                  U l        [        R
                  " X!U-  /UQ75      R                  U R                  5      U l        SU l        SU l	        S U l
        U(       a  [        R
                  " U45      U l
        X@l        XPl        X`l        Xpl        g r   )r   r   r   r   r   r   r   r   r   r   r   stridepaddingdilationgroups)
r4   in_channelsout_channelskernel_sizer   r   r   r   r   r   s
            r   r   FP8QDQConv2d.__init__  s     	((
kk&0?;?

"TZZ. 	  
	\O4DI r\   c           	      $   [         R                  R                  R                  R	                  U R
                  R                  [         R                  " U R                  /5      [         R                  S9n[         R                  R                  R                  R	                  U[         R                  " U R                  /5      U R                  S9n[         R                  R                  R                  R	                  U[         R                  " U R                  /5      [         R                  S9n[         R                  R                  R                  UUU R                   U R"                  U R$                  U R&                  U R(                  5      $ r   )r   rS   r   r   rV   r   r   rZ   r   r   r   r   r   r   r   conv2dr   r   r   r   r   )r4   r   r   r   r   s        r   r   FP8QDQConv2d.forward5  s!   ""JJRR;;##,, 1 123 S 

 ))##IIQQ,,

|, R 

 99$$LLTT,,

|, U 
 xx""))IIKKLLMMKK
 	
r\   )	r   r   r   r   r   r   r   r   r   )   r   r   r   Tr   r   s   @r   r   r     s(     4
 
r\   r   c                 b   U R                   n[        R                  R                  R                  R                  U [        R                  " U/5      [        R                  5      n[        R                  R                  R                  R                  U[        R                  " U/5      U5      nU$ )zM
Quantize-dequantize (QDQ, a.k.a., fake quant) pattern for FP8 quantization.
)	dtyper   rS   r   r   rV   rZ   r   r   )r   r   r   r   r   s        r   qdq_fp8r   Q  s     KKEiiEEMMeWG
 yy  HHPPeWH
 Or\   c                    S nU" U 5      nU R                  5        GH6  u  p4UR                  R                  nUS;  a  M$  UR                  n[        R
                  " U5      nU[        R                  " [        R                  5      R
                  -  nXl        [        R                  " Xh-  [        R                  " [        R                  5      R                  [        R                  " [        R                  5      R
                  5      R                  [        R                  5      n	XR                  l        US;   aX  [        UR                  UR                  S5      n
UR                   U
l        UR#                  5       U
l        XR                  l        OUS;   a  [%        UR&                  UR(                  UR*                  UR,                  UR.                  UR0                  UR2                  S5      n
UR                   U
l        UR#                  5       U
l        XR                  l        X$   R4                  nX$   R6                  n[9        XW
5        GM9     g)z\
In-place convert a model to fake-quantized FP8 model by replacing Linear and Conv2d layers
c                 R   ^^^ SSK Jn  U" SSS/5      m0 mUUU4S jmT" U 5        T$ )Nr   )
namedtupleModInstInfors   parentc                 Z   > U R                  5        H  u  pT" XS9TU'   T" U5        M     g )N)rs   r   )named_children)r   rs   modcreate_mod_info_recursionmod_inst_infoparent_child_mod_dicts      r   r   Lfp8_convert_.<locals>.generate_model_info.<locals>.create_mod_info_recursionn  s0    #224	-:-T%c*)#. 5r\   )collectionsr   )r5   r   r   r   r   s     @@@r   generate_model_info)fp8_convert_.<locals>.generate_model_infoh  s2    *"=682DE "	/
 	"%($$r\   )LinearConv2d)r   F)r   N)named_modulesr   rN   r   r   maxfinfor   r   clampminr   r   r   r   r   r   itemr   r   r   r   r   r   r   r   r   rs   setattr)r5   r   r   rs   r   mod_type_strparamxmaxr   q_parampatched_modr   s               r   fp8_convert_r   c  s   
% 06((*	}}--33

yyekk%*=*=>BBB'++!KK++,00KK++,00
 "U  
!	 	
 "

:%&s8H8H%PK"xxK'3'8'8':K$&-#Z'&  



	K  #xxK'3'8'8':K$&-#&+22$)..k*I +r\   Fc                 ,   U(       a  [         R                  " 5       O[        R                  " 5       nU   U(       a(  U(       a   eU(       a   e[	        U 5        U sSSS5        $ [        R
                  R                  XSS9R                  5       nU(       a  UO
[        X#5      nU(       a  [        Xt5      O
[        Xt5      nU" U6   [        R                  R                  R                  U5        [        U5      n	U	sSSS5        $ ! , (       d  f       g= f)z
Generate a reference-quantized model with quant/dequant (fake quant) pattern
For INT8: using the PT2E quantization flow.
For FP8: using `fp8_convert_`.
NT)r!   )
contextlibnullcontextr   no_gradr   r   r-   r   r   r   r   r   r   move_exported_model_to_evalr   )
r   inputsr;   r   r7   is_fp8maybe_no_gradexport_modelprepare_modelconvert_models
             r   _generate_ref_quantized_modelr     s     17J**,EMMOM	!!>: 
 !<<..s4.HOOQL  .vB   !9!,: 
 6"  %%AA-P(7M - 
s   &D$BD
DT)aotiatolrtol	fullgraphinductor_configsr    c          
         U=(       d    0 n[         R                  R                  5         [         R                  " S5        U V	s/ s H  n	[	        U	5      PM     n
n	UnU nU" U
0 UD6n[         R
                  R                  R                  5         U(       a  [         R                  " 5          [         R                  R                  R                  SS9   [         R                  R                  U UUSSS9n[         R
                  R                  XS9n[         R
                  R                  U5      nSSS5        SSS5        O2[         R                  " 5          [         R                  " XS9nSSS5        W" U6 n[         R                  R!                  UUXES9  gs  sn	f ! , (       d  f       Ns= f! , (       d  f       NI= f! , (       d  f       NZ= f)	z
Utility function to check the results of torch compiled model
by comparing the outputs of the original model and the compiled model.
r   T)use_new_tracer_experimental)r    r!   +prefer_deferred_runtime_asserts_over_guards)r  N)r  )r   r  )r   r%   r&   manual_seedr	   	_inductormetricsr   _exportconfigpatchr   aoti_compile_and_packageaoti_load_packagecompiletestingassert_close)r5   r6   kwargsr   r   r  r  r  r    x
ref_inputs
ref_kwargs	ref_modelcorrecteppackage_pathcompiled_modelactuals                     r   check_torch_compiled_modelr    sh     \rF	MM	a<JKNq/2NJKJI2z2G	OO!!#MMOMM  &&4&H $$-<@ % B !??CC D L #__>>|LN I O  ]]_"]]5FN ^,F	MMwTE7 L IH O  _s7   F+(F/AF1F/G 
F,	(F//
F= 
G)FFNFrr   ))r   r(   unittestr   $torch.ao.quantization.backend_configr   !torch.ao.quantization.quantize_fxr   r   +torch.testing._internal.common_quantizationr   r   $torch.testing._internal.common_utilsr   &torch.testing._internal.inductor_utilsr	   r   torchao.quantization.pt2er
   +torchao.quantization.pt2e._numeric_debuggerr   %torchao.quantization.pt2e.graph_utilsr   'torchao.quantization.pt2e.quantize_pt2er   r   r   torchao.utilsr   r   skipIfr^   r   r   Moduler   r   r   r   r   r  rM   r\   r   <module>r*     s        : P  3 W M 
 1a3 aH 
+G446KLO
( O
 MO
d("588?? "J8
588?? 8
v$8+| $!T /F
 
		/Fr\   