o
    ,hB                     @   s  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l0m1Z1 dZ2ee
j3 e4d< zddl5Z2W n e6y   dZ2Y nw e!j7Z7dZ8e9e:Z;edZ<deej= deej= fddZ>de?de?fddZ@dd ZAdejBjCdedededeDe f
d d!ZEd"edeFfd#d$ZGed"ej=dej=fd%d&ZHed"eeDe eIed'f eJeef f deKfd(d&ZHd"edeej=eKf fd)d&ZHde?fd*d+ZLdde?d-e
jMd.e?ddfd/d0ZNdd1d2e.d3eOd4ed5ed6eOde-fd7d8ZPG d9d: d:ZQG d;d< d<ZRG d=d> d>ZSG d?d@ d@ZTG dAdB dBZUd-e?de?fdCdDZVd-e?de?fdEdFZWdGe?de?fdHdIZXd-e?de?fdJdKZY			
ddLedMed'ef dNeOdOeeO dPeeO dQeOddfdRdSZZdTej[deDej= ded'ef fdUdVZ\	W	XddYeKdZeKd[eFd\eKde?f
d]d^Z]ej^d_dfd`eeO daeeO dbej_dcee?ej`f ddeOdej=fdedfZaedgZbdeFfdhdiZcdMee<ebf djedee<ebf fdkdlZdddmdn dodpeedqe?dre?djedsee? dteed'ef ged'ef f deefdudvZfdMee<ebf dee<ebf fdwdxZgdMee<ebf dee<ebf fdydzZhdMee<ebf dee<ebf fd{d|ZidMee<ebf dee<ebf fd}d~ZjdMee<ebf dee<ebf fddZkdMee<ebf dee<ebf fddZldMee<ebf dee<ebf fddZmdMee<ebf dee<ebf fddZnddeFddfddZodee<ebf de<jpde<jqdebfddZrdS )a1  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)samenp   _Pxreturnc                 C   s    | d u rd S |    | jS N)detachclonerequires_grad_requires_gradr!    r)   P/var/www/html/scripts/venv/lib/python3.10/site-packages/torch/_dynamo/testing.pyclone_me?   s   r+   namec                 C      t dd| S )Nz^_orig_mod[.] resub)r,   r)   r)   r*   remove_optimized_module_prefixE      r2   c                    sJ   ddl m  d d  fdd}tj|dd| |i | jfS )Nr   )InstructionTranslatorc                    s   |    jj| S r#   )
current_txoutputregion_tracker)_gmargskwargsr4   gmr7   r)   r*   extract_graph_backendO   s   z8extract_graph_and_tracker.<locals>.extract_graph_backendT)backend	fullgraph)torch._dynamo.symbolic_convertr4   torchcompilegraph)fnr9   r:   r=   r)   r;   r*   extract_graph_and_trackerI   s   
rE   model
predictionlossexample_inputsc                 C   s  g }| | | | i }i }|  D ]'\}}t| tjr"t|}|}	|j}
|jd u r1t|}
|
||d < |	||< q| | | | i }| 	 D ]\}}t| tjrZt|}|||< qL| | |D ] }t|t
tfrz|dd |D  qft|tjr| |j qf|S )Nz.gradc                 s   s"    | ]}t |tjr|jV  qd S r#   )
isinstancerA   Tensorgrad).0inpr)   r)   r*   	<genexpr>{   s     z"collect_results.<locals>.<genexpr>)appendnamed_parametersrJ   r   OptimizedModuler2   rL   rA   
zeros_likenamed_bufferstuplelistextendrK   )rF   rG   rH   rI   resultsgradsparamsr,   param
param_copyrL   buffersbufferexampler)   r)   r*   collect_resultsZ   s:   








r`   outc                 C   sZ   t | tjr	| jS t | ttfrtdd | D S | d u rdS t | tr&dS tdt	| )Nc                 s       | ]}t |V  qd S r#   )requires_bwd_passrM   r!   r)   r)   r*   rO          z$requires_bwd_pass.<locals>.<genexpr>FDon't know how to reduce)
rJ   rA   rK   r'   rV   rU   anyintNotImplementedErrortypera   r)   r)   r*   rc      s   
rc   c                 C      d S r#   r)   rk   r)   r)   r*   reduce_to_scalar_loss   s   rm   .c                 C   rl   r#   r)   rk   r)   r)   r*   rm      s   c                 C   s   t | tjr|  |   S t | ttfr"tdd | D t|  S t| j	dv r.t
| jS t| j	dkr:| j S t | trPtdd |  D t|   S tdt| )z/Reduce the output of a model to get scalar lossc                 s   rb   r#   rm   rd   r)   r)   r*   rO      re   z(reduce_to_scalar_loss.<locals>.<genexpr>)MaskedLMOutputSeq2SeqLMOutput!CausalLMOutputWithCrossAttentionsSquashedNormalc                 s   rb   r#   rn   )rM   valuer)   r)   r*   rO      re   rf   )rJ   rA   rK   sumnumelrV   rU   lenrj   __name__rm   logitsmeandictvalueskeysri   rk   r)   r)   r*   rm      s   


c                  C   s0   t jt jtd} t j| st |  | S )Nz../debug)ospathjoindirname__file__existsmkdir)r~   r)   r)   r*   	debug_dir   s   
r   r.   codeextrac              	   C   sl   t tjt | d!}|t|  dt|  d| d W d    d S 1 s/w   Y  d S )Nwz


)	openr}   r~   r   r   writedisBytecodeinfo)r,   r   r   fdr)   r)   r*   
debug_dump   s
   ("r   )skipframe
cache_sizehooks_r   c          	      C   s  dt t dtddfdd}tjj }tjjdh |U t| jr4t	 W  d   W  d   S t
| j t| j|}ti ddddd	d
it t | jg dd}tt|t| j|jtd
d
dW  d   W  d   S 1 suw   Y  W d   dS 1 sw   Y  dS )zused to debug jump updatesinstructionscode_optionsr"   Nc                 S   s$   |  dtd |  dtd d S )Nr   NOP)insertr   )r   r   r)   r)   r*   insert_nops   s   z&debug_insert_nops.<locals>.insert_nops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)rV   r   rA   _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   rC   r)   r)   r*   r      s:   


Rr   c                   @   sH   e Zd ZdddZdejjdeej de	de
f fdd	Zdd
dZdS )CompileCounterr"   Nc                 C      d| _ d| _d S Nr   frame_countop_countselfr)   r)   r*   __init__      
zCompileCounter.__init__r<   rI   .c                 C   s:   |  j d7  _ |jjD ]}d|jv r|  jd7  _q|jS )Nr   call)r   rC   nodesopr   forward)r   r<   rI   noder)   r)   r*   __call__   s   
zCompileCounter.__call__c                 C   r   r   r   r   r)   r)   r*   clear   r   zCompileCounter.clearr"   N)rw   
__module____qualname__r   rA   r   GraphModulerV   rK   r   r   r   r   r)   r)   r)   r*   r      s    


	r   c                   @   sP   e Zd ZdeddfddZdejjdeej	 de
def fd	d
ZdddZdS )CompileCounterWithBackendr>   r"   Nc                 C   s   d| _ d| _|| _g | _d S r   )r   r   r>   graphs)r   r>   r)   r)   r*   r      s   
z"CompileCounterWithBackend.__init__r<   rI   .c                 C   s\   ddl m} |  jd7  _|jjD ]}d|jv r|  jd7  _q| j| || j	||S )Nr   )lookup_backendr   )
backends.registryr   r   rC   r   r   r   r   rP   r>   )r   r<   rI   r   r   r)   r)   r*   r      s   
z"CompileCounterWithBackend.__call__c                 C   s   d| _ d| _g | _d S r   )r   r   r   r   r)   r)   r*   r   
     
zCompileCounterWithBackend.clearr   )rw   r   r   strr   rA   r   r   rV   rK   r   r   r   r   r)   r)   r)   r*   r      s    

r   c                   @   >   e Zd Zd
ddZdejjdeej de	de
f fdd	ZdS )EagerAndRecordGraphsr"   Nc                 C   s
   g | _ d S r#   )r   r   r)   r)   r*   r     s   
zEagerAndRecordGraphs.__init__r<   rI   .c                 C   s   | j | |jS r#   )r   rP   r   )r   r<   rI   r)   r)   r*   r        zEagerAndRecordGraphs.__call__r   rw   r   r   r   rA   r   r   rV   rK   r   r   r   r)   r)   r)   r*   r     s    

r   c                   @   r   )AotEagerAndRecordGraphsr"   Nc                 C   s   g | _ g | _g | _d S r#   )r   	fw_graphs	bw_graphsr   r)   r)   r*   r     r   z AotEagerAndRecordGraphs.__init__r<   rI   .c                    st    j | dtjjdttj dtdtf f fdd}dtjjdttj dtdtf f fdd}t	||||d	S )
Nr<   rI   r"   .c                        j |  | jS r#   )r   rP   r   r<   rI   r   r)   r*   fw_compiler(  r   z5AotEagerAndRecordGraphs.__call__.<locals>.fw_compilerc                    r   r#   )r   rP   r   r   r   r)   r*   bw_compiler.  r   z5AotEagerAndRecordGraphs.__call__.<locals>.bw_compiler)r   r   )
r   rP   rA   r   r   rV   rK   r   r   r   )r   r<   rI   r   r   r)   r   r*   r   #  s*   

z AotEagerAndRecordGraphs.__call__r   r   r)   r)   r)   r*   r     s    

r   c                   @   s   e Zd ZdddZdd ZdS )InductorAndRecordGraphsr"   Nc                 C   s   g | _ g | _d S r#   )r   inductor_graphsr   r)   r)   r*   r   =  r   z InductorAndRecordGraphs.__init__c                    sr   dd l m  m} j| |j  fdd}tj|d|d |||W  d    S 1 s2w   Y  d S )Nr   c                     s   j | d   | i |S r   )r   rP   )r9   r:   old_compile_fx_innerr   r)   r*   patchedH  s   z1InductorAndRecordGraphs.__call__.<locals>.patched_compile_fx_inner)new)torch._inductor.compile_fx	_inductor
compile_fxr   rP   r   r
   object)r   r<   rI   compile_fx_modr   r)   r   r*   r   A  s   
$z InductorAndRecordGraphs.__call__r   )rw   r   r   r   r   r)   r)   r)   r*   r   <  s    
r   c                 C   r-   )Nz(?m)^ *#.*\n?r.   r/   r   r)   r)   r*   strip_commentP  r3   r   c                 C   s   d dd | dD S )Nr   c                 S   s   g | ]}|  qS r)   )rstrip)rM   liner)   r)   r*   
<listcomp>U  s    z)remove_trailing_space.<locals>.<listcomp>)r   splitr   r)   r)   r*   remove_trailing_spaceT  s   r   gm_strc                 C   s   t t| S r#   )r   r   )r   r)   r)   r*   normalize_gmX  s   r   c                 C   s   t dd| }|S )z-
    Normalize code: remove empty lines.
    z[\r\n]+r   r/   )r   normal_coder)   r)   r*   empty_line_normalizer^  s   r   r   rD   nargsexpected_opsexpected_ops_dynamicexpected_frame_countc                 C   s   t js	|d ur	|}t }dd t|D }dd t|D }|| }	|| }
t  t||}|| }|| }|| }|| }t  | t||	 | t||	 | t||
 | t||
 | |j	| |d urv| |j
| d S d S )Nc                 S      g | ]}t d d qS 
   rA   randnrM   r   r)   r)   r*   r   s      z!standard_test.<locals>.<listcomp>c                 S   r   r   r   r   r)   r)   r*   r   t  r   )r   assume_static_by_defaultr   ranger   r   
assertTruer   assertEqualr   r   )r   rD   r   r   r   r   actualargs1args2correct1correct2opt_fnval1aval2aval1bval2br)   r)   r*   standard_testf  s,   r  r<   c                 C   s   | j S r#   )r   r   r)   r)   r*   dummy_fx_compile  s   r  T皙?speeduppvalue
is_correctpvalue_thresholdc                 C   s.   |sdS ||kr| ddS | dd|dS )NERRORz.3fzx SAMEzx p=z.2fr)   )r	  r
  r  r  r)   r)   r*   format_speedup  s
   r  cpusizestridedtypedevice
extra_sizec                 C   s   t dd t| |D d | }|jr0|jdkr'	 tj|tj|dj|d}ntj|||d}n	tj|g||d}t	|| |S )Nc                 s   s     | ]\}}|d  | V  qdS )r   Nr)   )rM   shaper  r)   r)   r*   rO     s    zrand_strided.<locals>.<genexpr>r   )r  r  )r  )r  r  r  )
rt   zipis_floating_pointitemsizerA   r   float16tozeros
as_strided)r  r  r  r  r  needed_sizer^   r)   r)   r*   rand_strided  s   
r  _Tc                   C   s   t j S r#   )r   r   r)   r)   r)   r*   check_dynamic_shape_capture  s   r   patchesc                    s.   t  dtjdtjdtf fdd}|S )Nr9   r:   r"   c               	      s^   t  !}D ]\}}}|t||| q | i |W  d    S 1 s(w   Y  d S r#   )
contextlib	ExitStackenter_contextr
   r   )r9   r:   stackmoduleattrvalrD   r!  r)   r*   _fn  s
   
$z"_make_fn_with_patches.<locals>._fn)	functoolswrapsr    r9   r:   r  )rD   r!  r*  r)   r)  r*   _make_fn_with_patches  s   "r-  c                 C   s   | S r#   r)   r(   r)   r)   r*   <lambda>  s    r.  )
xfail_prop	decoratorcls
cls_prefix	fn_suffixr/  r0  c                G   s   t | | j | ji }|j|_t| D ]P}|drVt| |}t|s.t||t| | q| | }	t	|g|R  }
|	|
_|d urMt
||rMt|
}
t||	||
 qt
||sdt||t| | q|S )Ntest_)rj   rw   	__bases__r   dir
startswithgetattrcallablesetattrr-  hasattrunittestexpectedFailure)r1  r2  r3  r/  r0  r!  DummyTestClassr,   rD   new_namenew_fnr)   r)   r*   make_test_cls_with_patches  s$   



rA  c                 C   s   t jdkr| S t| S )N)r      sysversion_infor<  r   rD   r)   r)   r*   skipIfNotPy311  s   

rG  c                 C      t jdkr| S td| S )Nr      zRequires Python 3.12+rC  rF  r)   r)   r*   skipIfNotPy312  s   
rK  c                 C   s   t jdkr
t| S | S )NrI  )rD  rE  r<  r=  rF  r)   r)   r*   xfailIfPy312  s   

rL  c                 C   s   t jdkrtd| S | S )NrI  zNot supported in Python 3.12+rC  rF  r)   r)   r*   skipIfPy312  s   
rM  c                 C   rH  )N)r   r   zRequires Python 3.10+rC  rF  r)   r)   r*   requiresPy310  s   
rN  c                 C   
   d| _ | S NT)_expected_failure_dynamicrF  r)   r)   r*   expectedFailureDynamic
     rR  c                 C   rO  rP  )!_expected_failure_codegen_dynamicrF  r)   r)   r*   expectedFailureCodegenDynamic  rS  rU  c                 C   rO  rP  )!_expected_failure_dynamic_wrapperrF  r)   r)   r*   expectedFailureDynamicWrapper  rS  rW  Fuse_xlac                 C   sV   t d td trtjd | r)dd lm  m} |dt	|
  d S d S )Ni9  r   )rA   manual_seedrandomseedr   torch_xla.core.xla_modelcore	xla_modelset_rng_stater   
xla_device)rX  xmr)   r)   r*   reset_rng_state  s   

rb  fr9   r:   c                 O   s   | |i |S r#   r)   )rc  r9   r:   r)   r)   r*   &_skipped_function_for_test_reconstruct&  s   rd  )r.   )NNr   )Tr  )F)s__doc__r"  r   r+  loggingos.pathr}   rZ  r0   rD  typesr<  collections.abcr   typingr   r   r   r   r   r   typing_extensionsr	   unittest.mockr
   rA   r    torch._dynamo.backends.debuggingr   torch._dynamo.output_graphr   r.   r   r   r   r   bytecode_transformationr   r   r   r   guardsr   r   r   r   r   r   r   r   r   
ModuleType__annotations__numpyModuleNotFoundErrorunsupportedthree	getLoggerrw   logr    rK   r+   r   r2   rE   nnModulerV   r`   boolrc   rm   rU   rz   floatr   CodeTyper   rh   r   r   r   r   r   r   r   r   r   r   r  r   r  r  float32r  r  r  r  r   r-  rj   rA  rG  rK  rL  rM  rN  rR  rU  rW  rb  r9   r:   rd  r)   r)   r)   r*   <module>   s^    

( 
(

!

	

&
""""""	""
