o
    eiJ                     @   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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/m0Z0 d
dl1m2Z2m3Z3 dZ4ee
j5 e6d< zddl7Z4W n e8y   dZ4Y nw e"j9Z9dZ:e;e<Z=edZ>deej? deej? fddZ@deAdeAfddZBdd ZCdd ZDdejEjFded ed!edeGe f
d"d#ZHd$edeIfd%d&ZJed$ej?dej?fd'd(ZKed$eeGe eLed)f eMeef f deNfd*d(ZKd$edeej?eNf fd+d(ZKdeAfd,d-ZOddeAd/e
jPd0eAddfd1d2ZQdd3d4e/d5eRd6ed7ed8eRde.fd9d:ZSG d;d< d<ZTG d=d> d>ZUG d?d@ d@ZVG dAdB dBZWG dCdD dDZXd/eAdeAfdEdFZYd/eAdeAfdGdHZZd/eAdeAfdIdJZ[dKeAdeAfdLdMZ\d/eAdeAfdNdOZ]			
ddPedQed)ef dReRdSeeR dTeeR dUeRddfdVdWZ^dXej_d!eGej? ded)ef fdYdZZ`	[	\dd]eNd^eNd_eId`eNdeAf
dadbZaejbdcdfddeeR deeeR dfejcdgeeAejdf dheRdej?fdidjZeedkZfdeIfdldmZgdQee>eff dnedee>eff fdodpZhddqdr dsdteidueAdveAdnedweeA dxeed)ef ged)ef f deifdydzZjdQee>eff dee>eff fd{d|ZkdQee>eff dee>eff fd}d~ZldQee>eff dee>eff fddZmdQee>eff dee>eff fddZndQee>eff dee>eff fddZodQee>eff dee>eff fddZpdQee>eff dee>eff fddZqdQee>eff dee>eff fddZrddeIddfddZsdee>eff de>jtde>judeffddZvdawejxdeeGejj_  fddZyd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)Callable	Generator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                 C   s    | d u rd S |    | jS N)detachclonerequires_grad_requires_gradr#    r+   _/var/www/addictedbytheproject.nl/epg/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      r4   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r6   gmr9   r+   r,   extract_graph_backendO   s   z8extract_graph_and_tracker.<locals>.extract_graph_backendT)backend	fullgraph)torch._dynamo.symbolic_convertr6   torchcompilegraph)fnr;   r<   r?   r+   r=   r,   extract_graph_and_trackerI   s   
rG   c                 O   s2   t  }tj|d| |i |}||j|j|jfS )N)r@   )AotEagerAndRecordGraphsrC   rD   graphs	fw_graphs	bw_graphs)rF   r;   r<   r@   resultr+   r+   r,   extract_graphZ   s   rM   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%   )
isinstancerC   Tensorgrad).0inpr+   r+   r,   	<genexpr>   s     z"collect_results.<locals>.<genexpr>)appendnamed_parametersrR   r   OptimizedModuler4   rT   rC   
zeros_likenamed_bufferstuplelistextendrS   )rN   rO   rP   rQ   resultsgradsparamsr.   param
param_copyrT   buffersbufferexampler+   r+   r,   collect_results`   s:   








rh   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rU   r#   r+   r+   r,   rW          z$requires_bwd_pass.<locals>.<genexpr>FDon't know how to reduce)
rR   rC   rS   r)   r^   r]   anyintNotImplementedErrortyperi   r+   r+   r,   rk      s   
rk   c                 C      d S r%   r+   rs   r+   r+   r,   reduce_to_scalar_loss   s   ru   .c                 C   rt   r%   r+   rs   r+   r+   r,   ru      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   rj   r%   ru   rl   r+   r+   r,   rW      rm   z(reduce_to_scalar_loss.<locals>.<genexpr>)MaskedLMOutputSeq2SeqLMOutput!CausalLMOutputWithCrossAttentionsSquashedNormalc                 s   rj   r%   rv   )rU   valuer+   r+   r,   rW      rm   rn   )rR   rC   rS   sumnumelr^   r]   lenrr   __name__ru   logitsmeandictvalueskeysrq   rs   r+   r+   r,   ru      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   r0   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j |W t| jr4t	 W  d   W  d   S t
| j t| j|\}}ti dddg d	d
it t | jg dd}tt|t| j|jtd
d
dW  d   W  d   S 1 sww   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)r^   r   rC   _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   rE   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   s   d| _ |   d S Nr   )frame_countclearselfr+   r+   r,   __init__   s   zCompileCounter.__init__r>   rQ   .c                 C   s:   |  j d7  _ |jjD ]}d|jv r|  jd7  _q|jS )Nr   call)r   rE   nodesopop_countforward)r   r>   rQ   noder+   r+   r,   __call__   s   
zCompileCounter.__call__c                 C   s"   t jr	td| _nd| _d| _d S r   )r   debug_disable_compile_counterr   r   r   r   r+   r+   r,   r      s   
zCompileCounter.clearr$   N)r   
__module____qualname__r   rC   r   GraphModuler^   rS   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| _ || _g | _|   d S r   )r   r@   rI   r   )r   r@   r+   r+   r,   r     s   z"CompileCounterWithBackend.__init__r>   rQ   .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   rE   r   r   r   rI   rX   r@   )r   r>   rQ   r   r   r+   r+   r,   r     s   
z"CompileCounterWithBackend.__call__c                 C   s(   t jr	td| _nd| _d| _g | _d S r   )r   r   r   r   r   rI   r   r+   r+   r,   r     s
   
zCompileCounterWithBackend.clearr   )r   r   r   strr   rC   r   r   r^   rS   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%   )rI   r   r+   r+   r,   r     s   
zEagerAndRecordGraphs.__init__r>   rQ   .c                 C   s   | j | |jS r%   )rI   rX   r   )r   r>   rQ   r+   r+   r,   r   "     zEagerAndRecordGraphs.__call__r   r   r   r   r   rC   r   r   r^   rS   r   r   r   r+   r+   r+   r,   r     s    

r   c                   @   r   )rH   r$   Nc                 C   s   g | _ g | _g | _d S r%   )rI   rJ   rK   r   r+   r+   r,   r   *  s   
z AotEagerAndRecordGraphs.__init__r>   rQ   .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>   rQ   r$   .c                        j |  | jS r%   )rJ   rX   r   r>   rQ   r   r+   r,   fw_compiler4  r   z5AotEagerAndRecordGraphs.__call__.<locals>.fw_compilerc                    r   r%   )rK   rX   r   r   r   r+   r,   bw_compiler:  r   z5AotEagerAndRecordGraphs.__call__.<locals>.bw_compiler)r   r   )
rI   rX   rC   r   r   r^   rS   r   r   r   )r   r>   rQ   r   r   r+   r   r,   r   /  s*   

z AotEagerAndRecordGraphs.__call__r   r   r+   r+   r+   r,   rH   )  s    

rH   c                   @   s   e Zd ZdddZdd ZdS )InductorAndRecordGraphsr$   Nc                 C   s   g | _ g | _d S r%   )rI   inductor_graphsr   r+   r+   r,   r   I  s   
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   rX   )r;   r<   old_compile_fx_innerr   r+   r,   patchedT  s   z1InductorAndRecordGraphs.__call__.<locals>.patched_compile_fx_inner)new)torch._inductor.compile_fx	_inductor
compile_fxrI   rX   r   r   object)r   r>   rQ   compile_fx_modr   r+   r   r,   r   M  s   
$z InductorAndRecordGraphs.__call__r   )r   r   r   r   r   r+   r+   r+   r,   r   H  s    
r   c                 C   r/   )Nz(?m)^ *#.*\n?r0   r1   r   r+   r+   r,   strip_comment\  r5   r   c                 C   s   d dd | dD S )Nr   c                 S   s   g | ]}|  qS r+   )rstrip)rU   liner+   r+   r,   
<listcomp>a  s    z)remove_trailing_space.<locals>.<listcomp>)r   splitr   r+   r+   r,   remove_trailing_space`  s   r   c                 C   sL   |  d}g }d}|D ]}| dkr|rqd}nd}|| qd|S )Nr   Fr0   T)r   striprX   r   )r   linesrL   	saw_blankr   r+   r+   r,   _squash_blank_linesd  s   

r   gm_strc                 C   s   t | }t|}t|S r%   )r   r   r   )r   strippedno_trailingr+   r+   r,   normalize_gms  s   r   c                 C   s   t dd| }|S )z-
    Normalize code: remove empty lines.
    z[\r\n]+r   r1   )r   normal_coder+   r+   r,   empty_line_normalizer{  s   r   r   rF   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 
   rC   randnrU   r   r+   r+   r,   r         z!standard_test.<locals>.<listcomp>c                 S   r   r   r   r   r+   r+   r,   r     r   )r   assume_static_by_defaultr   ranger   r   
assertTruer   assertEqualr   r   )r   rF   r   r   r   r   actualargs1args2correct1correct2opt_fnval1aval2aval1bval2br+   r+   r,   standard_test  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 | D r|tdd t| |D d 7 }|jr;|jdkr2	 tj|tj|dj|d}ntj|||d}n	tj	|g||d}t
|| |S )Nc                 s   s    | ]}|d kV  qdS )r   Nr+   )rU   sr+   r+   r,   rW     rm   zrand_strided.<locals>.<genexpr>c                 s   s     | ]\}}|d  | V  qdS )r   Nr+   )rU   shaper  r+   r+   r,   rW     s    r   )r  r  )r  )r  r  r  )allr|   zipis_floating_pointitemsizerC   r   float16tozeros
as_strided)r  r  r  r  r  needed_sizerf   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 )Nr;   r<   r$   c               	      s^   t  !}D ]\}}}|t||| q | i |W  d    S 1 s(w   Y  d S r%   )
contextlib	ExitStackenter_contextr   r   )r;   r<   stackmoduleattrvalrF   r*  r+   r,   _fn  s
   
$z"_make_fn_with_patches.<locals>._fn)	functoolswrapsr"   r;   r<   r(  )rF   r*  r3  r+   r2  r,   _make_fn_with_patches  s   "r6  c                 C   s   | S r%   r+   r*   r+   r+   r,   <lambda>  s    r7  )
xfail_prop	decoratorcls
cls_prefix	fn_suffixr8  r9  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_)rr   r   	__bases__r   dir
startswithgetattrcallablesetattrr6  hasattrunittestexpectedFailure)r:  r;  r<  r8  r9  r*  DummyTestClassr.   rF   new_namenew_fnr+   r+   r,   make_test_cls_with_patches  s$   



rJ  c                 C   s   t jdkr| S t| S )N)r!      sysversion_inforE  r   rF   r+   r+   r,   skipIfNotPy311  s   

rP  c                 C   s   t jdkr| S td| S )Nr!      zRequires Python 3.12+rL  rO  r+   r+   r,   skipIfNotPy312  s   
rS  c                 C   s&   t jdks
t jdk rtd| S | S )N)r!      rQ  zRequires Python 3.12rL  rO  r+   r+   r,   skipIfOnlyNotPy312  s   rU  c                 C   s   t jdkr
t| S | S )NrQ  )rM  rN  rE  rF  rO  r+   r+   r,   xfailIfPy312  s   

rV  c                 C   s   t jdkrtd| S | S )NrQ  zNot supported in Python 3.12+rL  rO  r+   r+   r,   skipIfPy312   s   
rW  c                 C   
   d| _ | S NT)_expected_failure_dynamicrO  r+   r+   r,   expectedFailureDynamic(     r[  c                 C   rX  rY  )!_expected_failure_codegen_dynamicrO  r+   r+   r,   expectedFailureCodegenDynamic.  r\  r^  c                 C   rX  rY  )!_expected_failure_dynamic_wrapperrO  r+   r+   r,   expectedFailureDynamicWrapper4  r\  r`  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   )rC   manual_seedrandomseedr    torch_xla.core.xla_modelcore	xla_modelset_rng_stater   
xla_device)ra  xmr+   r+   r,   reset_rng_state9  s   

rk  fr;   r<   c                 O   s   | |i |S r%   r+   )rl  r;   r<   r+   r+   r,   &_skipped_function_for_test_reconstructD  s   rm  c                   c   s    g a zt V  W da dS da w )a\  
    Context manager to capture graph modules compiled by invoke_subgraph_inductor_compile.

    Usage:
        with _testing_capture_invoke_subgraph_inductor_compile_gms() as captured_gms:
            # code that triggers invoke_subgraph_inductor_compile
            pass
        # captured_gms will contain the list of captured graph modules
    N)6_testing_invoke_subgraph_inductor_compile_captured_gmsr+   r+   r+   r,   5_testing_capture_invoke_subgraph_inductor_compile_gmsM  s
   ro  )r0   )NNr   )Tr  )F)z__doc__r+  r   r4  loggingos.pathr   rc  r2   rM  typesrE  collections.abcr   r   r   typingr   r   r   r   r	   typing_extensionsr
   unittest.mockr   rC   r    torch._dynamo.backends.debuggingr   torch._dynamo.output_graphr   r0   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"   rS   r-   r   r4   rG   rM   nnModuler^   rh   boolrk   ru   r]   r   floatr   CodeTyper   rp   r   r   r   r   rH   r   r   r   r   r   r   r  r   r  r  float32r  r  r'  r(  r)  r6  rr   rJ  rP  rS  rU  rV  rW  r[  r^  r`  rk  r;   r<   rm  rn  contextmanagerro  r+   r+   r+   r,   <module>   sl   

( 
(

!

	

&
""""""""


