
    i]G                        U d Z ddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlmZmZ ddlmZmZmZmZmZ ddlmZ ddlmZ ddlZddlmZ ddlmZ dd	lmZ d
dlm Z m!Z!m"Z"m#Z# d
dl$m%Z%m&Z&m'Z'm(Z( d
dl)m*Z*m+Z+m,Z, d
dl
m-Z-m.Z.m/Z/ d
dl0m1Z1m2Z2 dZ3ee
jh                     e5d<   	 ddl6Z3e!jp                  Z8dZ9 ejt                  e;      Z< ed      Z=deej|                     deej|                     fdZ?de@de@fdZAd ZBd ZCdej                  j                  dedededeFe   f
dZGdedeHfd ZIedej|                  dej|                  fd!       ZJedeeFe   eKed"f   eLeef   f   deMfd#       ZJdedeej|                  eMf   fd$ZJde@fd%ZNdode@d&e
j                  d'e@ddfd(ZPdd)d*e.d+eQd,ed-ed.eQde-fd/ZR G d0 d1      ZS G d2 d3      ZT G d4 d5      ZU G d6 d7      ZV G d8 d9      ZWd&e@de@fd:ZXd&e@de@fd;ZYd&e@de@fd<ZZd=e@de@fd>Z[d&e@de@fd?Z\	 	 	 dpd@edAed"ef   dBeQdCeeQ   dDeeQ   dEeQddfdFZ]dGej                  deFej|                     ded"ef   fdHZ_	 	 dqdIeMdJeMdKeHdLeMde@f
dMZ`ej                  dNdfdOeeQ   dPeeQ   dQej                  dRee@ej                  f   dSeQdej|                  fdTZd edU      ZedeHfdVZfdAee=eef   dWedee=eef   fdXZgddY dZd[ehd\e@d]e@dWed^ee@   d_eed"ef   ged"ef   f   dehfd`ZidAee=eef   dee=eef   fdaZjdAee=eef   dee=eef   fdbZkdAee=eef   dee=eef   fdcZldAee=eef   dee=eef   fddZmdAee=eef   dee=eef   fdeZndAee=eef   dee=eef   fdfZodAee=eef   dee=eef   fdgZpdAee=eef   dee=eef   fdhZqdrdieHddfdjZrdkee=eef   dle=j                  dme=j                  deefdnZuy# e7$ r dZ3Y w xY w)sa1  Testing utilities and infrastructure for Dynamo.

This module provides a comprehensive set of testing utilities including:
- Test result collection and validation
- Graph manipulation and comparison tools
- Test case management and execution helpers
- Specialized test decorators for different Python versions and features
- RNG state management
- Compilation counting and monitoring
- Debug utilities for bytecode transformation

The utilities in this module are used across Dynamo's test suite to ensure
consistent testing patterns and proper test isolation.
    N)CallableSequence)AnyOptionaloverloadTypeVarUnion)	ParamSpec)patch)fx)	aot_eager)OutputGraph   )config
eval_frameoptimize_assertreset)create_instructiondebug_checksis_generatortransform_code_object)CheckFunctionManager	CompileIdGuardedCode)ConvertFrameReturnDynamoFrameTypewrap_guarded_code)CompileCounterIntsamenp   _Pxreturnc                 v    | y | j                         j                         j                  | j                        S N)detachclonerequires_grad_requires_gradr#   s    O/var/www/html/engine/venv/lib/python3.12/site-packages/torch/_dynamo/testing.pyclone_mer-   ?   s.    y88:,,Q__==    namec                 0    t        j                  dd|       S )Nz^_orig_mod[.] resub)r/   s    r,   remove_optimized_module_prefixr5   E       66"B--r.   c                     ddl m d d fd}  t        j                  |d      |       |i | j                  fS )Nr   )InstructionTranslatorc                 T    | j                         j                  j                  | S r&   )
current_txoutputregion_tracker)_gmargskwargsr8   gmr<   s      r,   extract_graph_backendz8extract_graph_and_tracker.<locals>.extract_graph_backendO   s)     .99;BBQQ
r.   T)backend	fullgraph)torch._dynamo.symbolic_convertr8   torchcompilegraph)fnr>   r?   rA   r8   r@   r<   s       @@@r,   extract_graph_and_trackerrI   I   sJ    D	BN E@EMM/4@DdUfU88^##r.   c                     t               }  t        j                  |      |       |i |}||j                  |j                  |j
                  fS )N)rB   )AotEagerAndRecordGraphsrE   rF   graphs	fw_graphs	bw_graphs)rH   r>   r?   rB   results        r,   extract_graphrP   Z   sK    %'G/+U]]7+B/@@F7>>7#4#4g6G6GGGr.   model
predictionlossexample_inputsc                    g }|j                  |       |j                  |       i }i }| j                         D ]f  \  }}t        | t        j                        rt        |      }|}	|j                  }
|j                  t        j                  |      }
|
||dz   <   |	||<   h |j                  |       |j                  |       i }| j                         D ]/  \  }}t        | t        j                        rt        |      }|||<   1 |j                  |       |D ]g  }t        |t        t        f      r|j                  d |D               2t        |t        j                        sM|j                  |j                         i |S )Nz.gradc              3   j   K   | ]+  }t        |t        j                        s|j                   - y wr&   )
isinstancerE   Tensorgrad).0inps     r,   	<genexpr>z"collect_results.<locals>.<genexpr>   s!     X*S%,,:W388Xs   33)appendnamed_parametersrW   r   OptimizedModuler5   rY   rE   
zeros_likenamed_bufferstuplelistextendrX   )rQ   rR   rS   rT   resultsgradsparamsr/   param
param_copyrY   buffersbufferexamples                 r,   collect_resultsrm   `   sb    GNN:NN4 EF--/ 	"eeZ7781$7D
zz::##E*D $dWn!t	" NN5NN6G++- feZ7781$7D NN7! -gt}-NNXwXX'5<<0w||,- Nr.   outc                     t        | t        j                        r| j                  S t        | t        t
        f      rt        d | D              S | yt        | t              ryt        dt        |             )Nc              3   2   K   | ]  }t        |        y wr&   )requires_bwd_passrZ   r#   s     r,   r\   z$requires_bwd_pass.<locals>.<genexpr>   s     5A$Q'5   FDon't know how to reduce)
rW   rE   rX   r*   rc   rb   anyintNotImplementedErrortypern   s    r,   rq   rq      sb    #u||$   	C$	'5555		C	
8$s)
DDr.   c                      y r&    ry   s    r,   reduce_to_scalar_lossr|      s    >Ar.   .c                      y r&   r{   ry   s    r,   r|   r|      s     r.   c                 Z   t        | t        j                        r!| j                         | j	                         z  S t        | t
        t        f      rt        d | D              t        |       z  S t        |       j                  dv rt        | j                        S t        |       j                  dk(  r| j                  j                         S t        | t              r:t        d | j                         D              t        | j                               z  S t!        dt        |             )z/Reduce the output of a model to get scalar lossc              3   2   K   | ]  }t        |        y wr&   r|   rr   s     r,   r\   z(reduce_to_scalar_loss.<locals>.<genexpr>   s     9(+9rs   )MaskedLMOutputSeq2SeqLMOutput!CausalLMOutputWithCrossAttentionsSquashedNormalc              3   2   K   | ]  }t        |        y wr&   r   )rZ   values     r,   r\   z(reduce_to_scalar_loss.<locals>.<genexpr>   s     JE(/Jrs   rt   )rW   rE   rX   sumnumelrc   rb   lenrx   __name__r|   logitsmeandictvalueskeysrw   ry   s    r,   r|   r|      s    #u||$wwy399;&&	C$	'9S99CHDD	c		   

 %SZZ00	c		/	/xx||~	C	JSZZ\JJSHHJN
 
 	
 8$s)
DDr.   c                      t         j                  j                  t         j                  j                  t              d      } t         j                  j                  |       st        j                  |        | S )Nz../debug)ospathjoindirname__file__existsmkdir)r   s    r,   	debug_dirr      sC    77<<1:>D77>>$
Kr.   codeextrac           	      J   t        t        j                  j                  t	               |       d      5 }|j                  t        j                  |      j                          dt        j                  |      j                          d| d       d d d        y # 1 sw Y   y xY w)Nwz


)	openr   r   r   r   writedisBytecodeinfo)r/   r   r   fds       r,   
debug_dumpr      s}    	bggll9;-s	3 
r
||D!&&()cll4.@.D.D.F-GtE7RTU	

 
 
s   ABB")skipframe
cache_sizehooks_r   c                   dt         t           dt        ddfd}t        j                  j                  j                         }t        j                  j                  j                  d      5  |5  t        | j                        rt               cddd       cddd       S t        | j                         t        | j                  |      \  }}t        i dddg dd	it               t               | j                  g d
      }t        t!        |t#        | j                  |      j$                  t'        d	d	                  cddd       cddd       S # 1 sw Y   nxY wddd       y# 1 sw Y   yxY w)zused to debug jump updatesinstructionscode_optionsr$   Nc                 p    | j                  dt        d             | j                  dt        d             y )Nr   NOP)insertr   )r   r   s     r,   insert_nopsz&debug_insert_nops.<locals>.insert_nops   s.    A1%89A1%89r.   debug_insert_nopsF_idr   )r   compiler_fnroot_txexportexport_constraintsframe_statelocal_scopeglobal_scopef_codetorch_function_mode_stackpackage)frame_idframe_compile_id)rc   r   rE   _dynamoutilsget_metrics_contextdynamo_timedr   r   r   r   r   r   localsglobalsr   r   r   guard_managerr   )	r   r   r   r   r   r   metrics_contextr   rG   s	            r,   r   r      s1   
:$s) :3 :4 : mm))==?O				)	)*=	> 
 
%%'
 
 
 	U\\"'kBa!
 <<&(
 !$U\\59GG1q9
+
 
 
 
 
 
 
s1   +E&.E	E& BE>	E&E	E&&E/c                   |    e Zd Zd	dZdej
                  j                  deej                     de	de
f   fdZd	dZy)
CompileCounterr$   Nc                 2    d| _         | j                          y Nr   )frame_countclearselfs    r,   __init__zCompileCounter.__init__   s    :;

r.   r@   rT   .c                     | xj                   dz  c_         |j                  j                  D ]&  }d|j                  v s| xj                  dz  c_        ( |j
                  S )Nr   call)r   rG   nodesopop_countforward)r   r@   rT   nodes       r,   __call__zCompileCounter.__call__   sR     	AHHNN 	#D "	# zzr.   c                 p    t         j                  rt        d      | _        d| _        y d| _        d| _        y r   )r   debug_disable_compile_counterr   r   r   r   s    r,   r   zCompileCounter.clear   s2    //03D   !Dr.   r$   N)r   
__module____qualname__r   rE   r   GraphModulerc   rX   r   r   r   r   r{   r.   r,   r   r      sE    ((&&8<U\\8J	#s(	r.   r   c                       e Zd ZdeddfdZdej                  j                  deej                     de
def   fdZd
d	Zy)CompileCounterWithBackendrB   r$   Nc                 N    d| _         || _        g | _        | j                          y r   )r   rB   rL   r   )r   rB   s     r,   r   z"CompileCounterWithBackend.__init__  s!    :;24

r.   r@   rT   .c                    ddl m} | xj                  dz  c_        |j                  j                  D ]&  }d|j
                  v s| xj                  dz  c_        ( | j                  j                  |         || j                        ||      S )Nr   )lookup_backendr   )
backends.registryr   r   rG   r   r   r   rL   r]   rB   )r   r@   rT   r   r   s        r,   r   z"CompileCounterWithBackend.__call__  su     	6AHHNN 	#D "	# 	2+~dll+B??r.   c                 p    t         j                  rt        d      | _        nd| _        d| _        g | _        y r   )r   r   r   r   r   rL   r   s    r,   r   zCompileCounterWithBackend.clear  s.    //03D Dr.   r   )r   r   r   strr   rE   r   r   rc   rX   r   r   r   r   r{   r.   r,   r   r      sW      
@((&&
@8<U\\8J
@	#s(	
@r.   r   c                   t    e Zd ZddZdej
                  j                  deej                     de	de
f   fdZy)	EagerAndRecordGraphsr$   Nc                     g | _         y r&   )rL   r   s    r,   r   zEagerAndRecordGraphs.__init__  s	    24r.   r@   rT   .c                 P    | j                   j                  |       |j                  S r&   )rL   r]   r   )r   r@   rT   s      r,   r   zEagerAndRecordGraphs.__call__"  s      	2zzr.   r   r   r   r   r   rE   r   r   rc   rX   r   r   r   r{   r.   r,   r   r     s@    5((&&8<U\\8J	#s(	r.   r   c                   t    e Zd ZddZdej
                  j                  deej                     de	de
f   fdZy)	rK   r$   Nc                 .    g | _         g | _        g | _        y r&   )rL   rM   rN   r   s    r,   r   z AotEagerAndRecordGraphs.__init__*  s    245757r.   r@   rT   .c                 p     j                   j                  |       dt        j                  j                  dt
        t        j                     dt        dt        f   f fd}dt        j                  j                  dt
        t        j                     dt        dt        f   f fd}t        ||||      S )Nr@   rT   r$   .c                 R    j                   j                  |        | j                  S r&   )rM   r]   r   r@   rT   r   s     r,   fw_compilerz5AotEagerAndRecordGraphs.__call__.<locals>.fw_compiler4  !     NN!!"%::r.   c                 R    j                   j                  |        | j                  S r&   )rN   r]   r   r   s     r,   bw_compilerz5AotEagerAndRecordGraphs.__call__.<locals>.bw_compiler:  r   r.   )r   r   )
rL   r]   rE   r   r   rc   rX   r   r   r   )r   r@   rT   r   r   s   `    r,   r   z AotEagerAndRecordGraphs.__call__/  s     	2	$$	6:5<<6H	c3h		$$	6:5<<6H	c3h	 ##	
 	
r.   r   r   r{   r.   r,   rK   rK   )  s@    8

((&&
8<U\\8J
	#s(	
r.   rK   c                       e Zd ZddZd Zy)InductorAndRecordGraphsNc                      g | _         g | _        y r&   )rL   inductor_graphsr   s    r,   r   z InductorAndRecordGraphs.__init__I  s    24;=r.   c                      dd l mc m}  j                  j	                  |       |j
                   fd}t        j                  |d|      5  |j                  ||      cd d d        S # 1 sw Y   y xY w)Nr   c                  P    j                   j                  | d           | i |S r   )r   r]   )r>   r?   old_compile_fx_innerr   s     r,   patchedz1InductorAndRecordGraphs.__call__.<locals>.patchedT  s+      ''Q0'888r.   _compile_fx_inner)new)torch._inductor.compile_fx	_inductor
compile_fxrL   r]   r   r   object)r   r@   rT   compile_fx_modr   r   s   `    @r,   r   z InductorAndRecordGraphs.__call__M  sh    ;;2-??	9 \\.*=7K 	A!,,R@	A 	A 	As   A--A6r   )r   r   r   r   r   r{   r.   r,   r   r   H  s    >Ar.   r   c                 0    t        j                  dd|       S )Nz(?m)^ *#.*\n?r1   r2   )r   s    r,   strip_commentr  \  r6   r.   c                     dj                  | j                  d      D cg c]  }|j                          c}      S c c}w )Nr   )r   splitrstrip)r   lines     r,   remove_trailing_spacer	  `  s.    99

40@AdkkmABBAs   =c                     | j                  d      }g }d}|D ].  }|j                         dk(  r|rd}nd}|j                  |       0 dj                  |      S )Nr   Fr1   T)r  stripr]   r   )r   linesrO   	saw_blankr  s        r,   _squash_blank_linesr  d  sd    JJtEFI ::<2IId 99Vr.   gm_strc                 D    t        |       }t        |      }t        |      S r&   )r  r	  r  )r  strippedno_trailings      r,   normalize_gmr  s  s#     V$H'1K{++r.   c                 4    t        j                  dd|       }|S )z-
    Normalize code: remove empty lines.
    z[\r\n]+r   r2   )r   normal_codes     r,   empty_line_normalizerr  {  s     &&T40Kr.   r   rH   nargsexpected_opsexpected_ops_dynamicexpected_frame_countc                    t         j                  s||}t               }t        |      D cg c]  }t	        j
                  dd       }}t        |      D cg c]  }t	        j
                  dd       }	} || }
 ||	 }t                 t        |      |      } || } ||	 } || } ||	 }t                | j                  t        ||
             | j                  t        ||
             | j                  t        ||             | j                  t        ||             | j                  |j                  |       || j                  |j                  |       y y c c}w c c}w )N
   )r   assume_static_by_defaultr   rangerE   randnr   r   
assertTruer   assertEqualr   r   )r   rH   r  r  r  r  actualr   args1args2correct1correct2opt_fnval1aval2aval1bval2bs                    r,   standard_testr,    s?    **/C/O+F*/,7QU[[R 7E7*/,7QU[[R 7E75zH5zH	G$_V$R(FENEENEENEENE	GOOD)*OOD)*OOD)*OOD)*V'')=>,7  ! 87s   E"E'r@   c                     | j                   S r&   )r   )r@   rT   s     r,   dummy_fx_compiler.    s     ::r.   speeduppvalue
is_correctpvalue_thresholdc                 0    |sy||kD  r| ddS | dd|dS )NERRORz.3fzx SAMEzx p=z.2fr{   )r/  r0  r1  r2  s       r,   format_speedupr5    s7       #f%%c]$vcl++r.   cpusizestridedtypedevice
extra_sizec                    |}t        d | D              r"|t        d t        | |      D              dz   z  }|j                  r`|j                  dk(  r8	 t        j                  |t
        j                  |      j                  |      }n2t        j                  |||      }nt        j                  |g||      }t        j                  || |      S )Nc              3   &   K   | ]	  }|d kD    yw)r   Nr{   )rZ   ss     r,   r\   zrand_strided.<locals>.<genexpr>  s     
Q1q5
s   c              3   2   K   | ]  \  }}|d z
  |z    yw)r   Nr{   )rZ   shaper8  s      r,   r\   zrand_strided.<locals>.<genexpr>  s     Lf$Lrs   r   )r9  r:  )r9  )r7  r9  r:  )allr   zipis_floating_pointitemsizerE   r  float16tozeros
as_strided)r7  r8  r9  r:  r;  needed_sizerk   s          r,   rand_stridedrJ    s     K

$
L#dF:KLLqP	
 >>Q [[EMM&QTT U F [[E&IF;-uVLFD&11r.   _Tc                  $    t         j                   S r&   )r   r  r{   r.   r,   check_dynamic_shape_capturerM    s    ....r.   patchesc                      t        j                         dt        j                  dt        j                  dt
        f fd       }|S )Nr>   r?   r$   c            	          t        j                         5 }D ],  \  }}}|j                  t        j                  |||             .  | i |cd d d        S # 1 sw Y   y xY wr&   )
contextlib	ExitStackenter_contextr   r  )r>   r?   stackmoduleattrvalrH   rN  s         r,   _fnz"_make_fn_with_patches.<locals>._fn  sh    !!# 	'u%, E!c##ELLs$CDE t&v&		' 	' 	's   9AA")	functoolswrapsr"   r>   r?   rK  )rH   rN  rX  s   `` r,   _make_fn_with_patchesr[    sB    __R'277 'bii 'B ' ' Jr.   c                     | S r&   r{   r+   s    r,   <lambda>r]    s    a r.   )
xfail_prop	decoratorcls
cls_prefix	fn_suffixr^  r_  c          	         t        | | j                   | j                  i       }|j                  |_        t	        |       D ]  }|j                  d      rt        | |      }t        |      st        ||t        | |             C| | }	t        |g| }
|	|
_        |!t        ||      rt        j                  |
      }
t        ||	 ||
             t        ||      rt        ||t        | |              |S )Ntest_)rx   r   	__bases__r   dir
startswithgetattrcallablesetattrr[  hasattrunittestexpectedFailure)r`  ra  rb  r^  r_  rN  DummyTestClassr/   rH   new_namenew_fns              r,   make_test_cls_with_patchesrq    s     ZL7KN"0"9"9NC >??7#d#BB<gc4.@A	{+H*288F&FO%'"j*A!11&9NHi.?@.ND'#t*<=>  r.   c                 V    t         j                  dk\  r| S t        j                  |       S )N)r!      sysversion_inforl  r   rH   s    r,   skipIfNotPy311rx    s$    
7"	==r.   c                 b    t         j                  dk\  r| S  t        j                  d      |       S )Nr!      zRequires Python 3.12+rt  rw  s    r,   skipIfNotPy312r|    s,    
7"	18==01"55r.   c                     t         j                  dk\  st         j                  dk  r t        j                  d      |       S | S )N)r!      rz  zRequires Python 3.12rt  rw  s    r,   skipIfOnlyNotPy312r    s9    
7"c&6&6&@4x}}34R88Ir.   c                 V    t         j                  dk\  rt        j                  |       S | S )Nrz  )ru  rv  rl  rm  rw  s    r,   xfailIfPy312r    s&    
7"''++Ir.   c                 b    t         j                  dk\  r t        j                  d      |       S | S )Nrz  zNot supported in Python 3.12+rt  rw  s    r,   skipIfPy312r     s,    
7"=x}}<=bAAIr.   c                     d| _         | S NT)_expected_failure_dynamicrw  s    r,   expectedFailureDynamicr  (  s    #'B Ir.   c                     d| _         | S r  )!_expected_failure_codegen_dynamicrw  s    r,   expectedFailureCodegenDynamicr  .      +/B(Ir.   c                     d| _         | S r  )!_expected_failure_dynamic_wrapperrw  s    r,   expectedFailureDynamicWrapperr  4  r  r.   use_xlac                    t        j                  d       t        j                  d       t        rt        j                  j                  d       | r3dd lmc m} |j                  dt        |j                                      y y )Ni9  r   )rE   manual_seedrandomseedr    torch_xla.core.xla_modelcore	xla_modelset_rng_stater   
xla_device)r  xms     r,   reset_rng_stater  9  sX    	d
KK	
		t--
s2==?34 r.   fr>   r?   c                      | |i |S r&   r{   )r  r>   r?   s      r,   &_skipped_function_for_test_reconstructr  D  s     dfr.   )r1   )NNr   )Tg?)F)v__doc__rQ  r   rY  loggingos.pathr   r  r3   ru  typesrl  collections.abcr   r   typingr   r   r   r   r	   typing_extensionsr
   unittest.mockr   rE   r    torch._dynamo.backends.debuggingr   torch._dynamo.output_graphr   r1   r   r   r   r   bytecode_transformationr   r   r   r   guardsr   r   r   r   r   r   r   r   r   r    
ModuleType__annotations__numpyModuleNotFoundErrorunsupportedthree	getLoggerr   logr"   rX   r-   r   r5   rI   rP   nnModulerc   rm   boolrq   r|   rb   r   floatr   CodeTyper   rv   r   r   r   r   rK   r   r  r	  r  r  r  r,  r   r.  r5  float32r9  r:  rJ  rK  rM  r[  rx   rq  rx  r|  r  r  r  r  r  r  r  r>   r?   r  r{   r.   r,   <module>r     sM    
     	 
   . : : '    6 2 8 8  A @ I I * "&HU %
 $$	g!t_>& >8ELL+A >. . .$"H%88??%(+%36%HK%	#Y%P	E3 	E4 	E 
 Au|| A A 
 A 
	tCy%S/4S>9	:
 

Es EuU\\5-@'A E,3 
S 
 
s 
D 
 QR%
%
(+%
47%
<?%
JM%
%
P , < 
 
>A A(. . .C C Cc c , , ,   #'*. !8
8c8 8 3-	8
 #3-8 8 
8B
(,U\\(:c3h !	
,
,
, 
, 	
,
 	
,  ',2
3-2SM2 ;;2 #u||#$	2
 2 \\2: T]/T /
	hr2v. 	# 	(2r6BR 	" !%DO	  	
  #s(+,hsCx.@@A 
>xB' HRV,< 6xB' 6HRV,< 68BF+ R0@ Xb"f% (2r6*: HRV$ "b&)9 xB/ HRV4D hr2v&6 8BF;K hr2v&6 8BF;K 
5T 5d 5B "3599c  	Bs   -O OO