
    igD                        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 ddlmZ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ej                  j                  dedededeEe   f
dZFdedeGfdZHedej|                  dej|                  fd        ZIedeeEe   eJed!f   eKeef   f   deLfd"       ZIdedeej|                  eLf   fd#ZIde@fd$ZMdmde@d%e
j                  d&e@ddfd'ZOdd(d)e.d*ePd+ed,ed-ePde-fd.ZQ G d/ d0      ZR G d1 d2      ZS G d3 d4      ZT G d5 d6      ZU G d7 d8      ZVd%e@de@fd9ZWd%e@de@fd:ZXd;e@de@fd<ZYd%e@de@fd=ZZ	 	 	 dnd>ed?ed!ef   d@ePdAeeP   dBeeP   dCePddfdDZ[dEej                  deEej|                     ded!ef   fdFZ]	 	 dodGeLdHeLdIeGdJeLde@f
dKZ^ej                  dLdfdMeeP   dNeeP   dOej                  dPee@ej                  f   dQePdej|                  fdRZb edS      ZcdeGfdTZdd?ee=ecf   dUedee=ecf   fdVZeddW dXdYefdZe@d[e@dUed\ee@   d]eed!ef   ged!ef   f   deffd^Zgd?ee=ecf   dee=ecf   fd_Zhd?ee=ecf   dee=ecf   fd`Zid?ee=ecf   dee=ecf   fdaZjd?ee=ecf   dee=ecf   fdbZkd?ee=ecf   dee=ecf   fdcZld?ee=ecf   dee=ecf   fddZmd?ee=ecf   dee=ecf   fdeZnd?ee=ecf   dee=ecf   fdfZodpdgeGddfdhZpdiee=ecf   dje=j                  dke=j                  decfdlZsy# e7$ r dZ3Y w xY w)qa1  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)Sequence)AnyCallable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.   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_parametersrP   r   OptimizedModuler5   rR   rE   
zeros_likenamed_bufferstuplelistextendrQ   )rJ   rK   rL   rM   resultsgradsparamsr/   param
param_copyrR   buffersbufferexamples                 r,   collect_resultsrf   Z   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rS   r#   s     r,   rU   z$requires_bwd_pass.<locals>.<genexpr>   s     5A$Q'5   FDon't know how to reduce)
rP   rE   rQ   r*   r\   r[   anyintNotImplementedErrortyperg   s    r,   rj   rj      sb    #u||$   	C$	'5555		C	
8$s)
DDr.   c                      y r&    rr   s    r,   reduce_to_scalar_lossru      s    >Ar.   .c                      y r&   rt   rr   s    r,   ru   ru      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&   ru   rk   s     r,   rU   z(reduce_to_scalar_loss.<locals>.<genexpr>   s     9(+9rl   )MaskedLMOutputSeq2SeqLMOutput!CausalLMOutputWithCrossAttentionsSquashedNormalc              3   2   K   | ]  }t        |        y wr&   ry   )rS   values     r,   rU   z(reduce_to_scalar_loss.<locals>.<genexpr>   s     JE(/Jrl   rm   )rP   rE   rQ   sumnumelr\   r[   lenrq   __name__ru   logitsmeandictvalueskeysrp   rr   s    r,   ru   ru      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)r\   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@   rM   .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@   rM   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   GraphModuler\   rQ   r   r   r   r   rt   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   graphsr   )r   rB   s     r,   r   z"CompileCounterWithBackend.__init__   s!    :;24

r.   r@   rM   .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   r   rV   rB   )r   r@   rM   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   r   r   s    r,   r   zCompileCounterWithBackend.clear  s.    //03D Dr.   r   )r   r   r   strr   rE   r   r   r\   rQ   r   r   r   r   rt   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&   )r   r   s    r,   r   zEagerAndRecordGraphs.__init__  s	    24r.   r@   rM   .c                 P    | j                   j                  |       |j                  S r&   )r   rV   r   )r   r@   rM   s      r,   r   zEagerAndRecordGraphs.__call__  s      	2zzr.   r   r   r   r   r   rE   r   r   r\   rQ   r   r   r   rt   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)	AotEagerAndRecordGraphsr$   Nc                 .    g | _         g | _        g | _        y r&   )r   	fw_graphs	bw_graphsr   s    r,   r   z AotEagerAndRecordGraphs.__init__$  s    245757r.   r@   rM   .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@   rM   r$   .c                 R    j                   j                  |        | j                  S r&   )r   rV   r   r@   rM   r   s     r,   fw_compilerz5AotEagerAndRecordGraphs.__call__.<locals>.fw_compiler.  !     NN!!"%::r.   c                 R    j                   j                  |        | j                  S r&   )r   rV   r   r   s     r,   bw_compilerz5AotEagerAndRecordGraphs.__call__.<locals>.bw_compiler4  r   r.   )r   r   )
r   rV   rE   r   r   r\   rQ   r   r   r   )r   r@   rM   r   r   s   `    r,   r   z AotEagerAndRecordGraphs.__call__)  s     	2	$$	6:5<<6H	c3h		$$	6:5<<6H	c3h	 ##	
 	
r.   r   r   rt   r.   r,   r   r   #  s@    8

((&&
8<U\\8J
	#s(	
r.   r   c                       e Zd ZddZd Zy)InductorAndRecordGraphsNc                      g | _         g | _        y r&   )r   inductor_graphsr   s    r,   r   z InductorAndRecordGraphs.__init__C  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   rV   )r>   r?   old_compile_fx_innerr   s     r,   patchedz1InductorAndRecordGraphs.__call__.<locals>.patchedN  s+      ''Q0'888r.   _compile_fx_inner)new)torch._inductor.compile_fx	_inductor
compile_fxr   rV   r   r   object)r   r@   rM   compile_fx_modr   r   s   `    @r,   r   z InductorAndRecordGraphs.__call__G  sh    ;;2-??	9 \\.*=7K 	A!,,R@	A 	A 	As   A--A6r   )r   r   r   r   r   rt   r.   r,   r   r   B  s    >Ar.   r   c                 0    t        j                  dd|       S )Nz(?m)^ *#.*\n?r1   r2   )r   s    r,   strip_commentr  V  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  Z  s.    99

40@AdkkmABBAs   =gm_strc                 *    t        t        |             S r&   )r  r  )r  s    r,   normalize_gmr	  ^  s     !v!677r.   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  d  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"  l  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@   rM   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.2frt   )r%  r&  r'  r(  s       r,   format_speedupr+    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   Nrt   )rS   ss     r,   rU   zrand_strided.<locals>.<genexpr>  s     
Q1q5
s   c              3   2   K   | ]  \  }}|d z
  |z    yw)r   Nrt   )rS   shaper.  s      r,   rU   zrand_strided.<locals>.<genexpr>  s     Lf$Lrl   r   )r/  r0  )r/  )r-  r/  r0  )allr   zipis_floating_pointitemsizerE   r  float16tozeros
as_strided)r-  r.  r/  r0  r1  needed_sizerd   s          r,   rand_stridedr@    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  rt   r.   r,   check_dynamic_shape_capturerC    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   rD  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?   rA  )rH   rD  rN  s   `` r,   _make_fn_with_patchesrQ    sB    __R'277 'bii 'B ' ' Jr.   c                     | S r&   rt   r+   s    r,   <lambda>rS    s    a r.   )
xfail_prop	decoratorcls
cls_prefix	fn_suffixrT  rU  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_)rq   r   	__bases__r   dir
startswithgetattrcallablesetattrrQ  hasattrunittestexpectedFailure)rV  rW  rX  rT  rU  rD  DummyTestClassr/   rH   new_namenew_fns              r,   make_test_cls_with_patchesrg    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_inforb  r   rH   s    r,   skipIfNotPy311rn    s$    
7"	==r.   c                 b    t         j                  dk\  r| S  t        j                  d      |       S )Nr!      zRequires Python 3.12+rj  rm  s    r,   skipIfNotPy312rr    s,    
7"	18==01"55r.   c                 V    t         j                  dk\  rt        j                  |       S | S )Nrp  )rk  rl  rb  rc  rm  s    r,   xfailIfPy312rt    s&    
7"''++Ir.   c                 b    t         j                  dk\  r t        j                  d      |       S | S )Nrp  zNot supported in Python 3.12+rj  rm  s    r,   skipIfPy312rv    s,    
7"=x}}<=bAAIr.   c                 b    t         j                  dk\  r| S  t        j                  d      |       S )N)r!   r  zRequires Python 3.10+rj  rm  s    r,   requiresPy310rx    s,    
7"	5x}}45b99r.   c                     d| _         | S NT)_expected_failure_dynamicrm  s    r,   expectedFailureDynamicr|    s    #'B Ir.   c                     d| _         | S rz  )!_expected_failure_codegen_dynamicrm  s    r,   expectedFailureCodegenDynamicr        +/B(Ir.   c                     d| _         | S rz  )!_expected_failure_dynamic_wrapperrm  s    r,   expectedFailureDynamicWrapperr    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  "  sX    	d
KK	
		t--
s2==?34 r.   fr>   r?   c                      | |i |S r&   rt   )r  r>   r?   s      r,   &_skipped_function_for_test_reconstructr  -  s     dfr.   )r1   )NNr   )Tg?)F)t__doc__rG  r   rO  loggingos.pathr   r  r3   rk  typesrb  collections.abcr   typingr   r   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"   rQ   r-   r   r5   rI   nnModuler\   rf   boolrj   ru   r[   r   floatr   CodeTyper   ro   r   r   r   r   r   r   r  r  r	  r  r"  r   r$  r+  float32r/  r0  r@  rA  rC  rQ  rq   rg  rn  rr  rt  rv  rx  r|  r  r  r  r>   r?   r  rt   r.   r,   <module>r     s4    
     	 
   $ D D '    6 2 8 8  A @ I I * "&HU %
 $$	g!t_>& >8ELL+A >. . .$"%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8 8 8   #'*. !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Xb"f% (2r6*: HRV$ "b&)9 :hr2v& :8BF+; :xB/ HRV4D hr2v&6 8BF;K hr2v&6 8BF;K 
5T 5d 5B "3599u  	Bs   -N7 7OO