o
    Zh                     @   s   d 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Zddl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 eeZdd	eeeed
f f ddfddZG dd deZdS )a  Testing utilities for Dynamo, providing a specialized TestCase class and test running functionality.

This module extends PyTorch's testing framework with Dynamo-specific testing capabilities.
It includes:
- A custom TestCase class that handles Dynamo-specific setup/teardown
- Test running utilities with dependency checking
- Automatic reset of Dynamo state between tests
- Proper handling of gradient mode state
    N)Union)	trace_log)
IS_WINDOWSTEST_WITH_CROSSREFTEST_WITH_TORCHDYNAMOTestCase   )configresetutils needs.returnc              	   C   s   ddl m} ts
trd S tj strtj	
dddkrd S t| tr&| f} | D ]!}|dkr7tj s6 d S q(zt| W q( tyI   Y  d S w |  d S )Nr   )	run_testsZTORCHINDUCTOR_WINDOWS_TESTS0cuda)$torch.testing._internal.common_utilsr   r   r   torchZxpuZis_availabler   osenvironget
isinstancestrr   	importlibimport_moduleImportError)r   r   Zneedr   r   F/var/www/auris/lib/python3.10/site-packages/torch/_dynamo/test_case.pyr   !   s,   


r   c                       s\   e Zd ZU ejed< ed fddZed fddZd fdd	Z	d fd
dZ
  ZS )r   _exit_stackr   Nc                    s   | j   t   d S N)r   closesupertearDownClasscls	__class__r   r   r!   ?   s   
zTestCase.tearDownClassc                    s0   t    t | _| jtjdddd d S )NTF)Zraise_on_ctx_manager_usageZsuppress_errorsZlog_compilation_metrics)r    
setUpClass
contextlib	ExitStackr   enter_contextr	   patchr"   r$   r   r   r&   D   s   

zTestCase.setUpClassc                    s>   t  | _t   t  tj  t	
 | _t| j d S r   )r   is_grad_enabled_prior_is_grad_enabledr    setUpr
   r   countersclearloggingNullHandlerhandlerr   
addHandler)selfr$   r   r   r-   P   s   



zTestCase.setUpc                    st   t | j tj D ]\}}t||  qt  tj	  t
   | jt ur8td t| j d S d S )NzRunning test changed grad mode)r   removeHandlerr2   r   r.   itemsprintmost_commonr
   r/   r    tearDownr,   r   r+   logwarningZset_grad_enabled)r4   kvr$   r   r   r9   X   s   


zTestCase.tearDown)r   N)__name__
__module____qualname__r'   r(   __annotations__classmethodr!   r&   r-   r9   __classcell__r   r   r$   r   r   <   s   
 
r   )r   )__doc__r'   r   r0   r   typingr   r   Ztorch.testingZtorch._logging._internalr   r   r   r   r   r   ZTorchTestCase r	   r
   r   	getLoggerr>   r:   r   tupler   r   r   r   r   <module>   s    

$