
    rhZ                        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	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j2                  e      Zdde
eeed	f   f   d
dfdZ G d de      Z G d de      Zy)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                    ddl m} t        st        ry t        j
                  j                         s*t        r$t        j                  j                  dd      dk(  ry t        | t              r| f} | D ]>  }|dk(  r!t        j                  j                         r' y 	 t        j                  |       @  |        y # t         $ r Y  y w xY w)Nr   )	run_testsTORCHINDUCTOR_WINDOWS_TESTS0cuda)$torch.testing._internal.common_utilsr   r   r   torchxpuis_availabler   osenvironget
isinstancestrr   	importlibimport_moduleImportError)r   r   needs      j/var/www/html/ai-insurance-compliance-backend/venv/lib/python3.12/site-packages/torch/_dynamo/test_case.pyr   r   '   s    > 2 II""$JJNN8#>#E% 6>::**,''- K  s   B::	CCc                   x     e Zd ZU ej                  ed<   ed fd       Zed fd       Zd fdZ	d fdZ
 xZS )r   _exit_stackc                 V    | j                   j                          t        |           y N)r#   closesupertearDownClasscls	__class__s    r!   r(   zTestCase.tearDownClassE   s        c                     t         |           t        j                         | _        | j                  j                  t        j                  ddd             y )NTF)raise_on_ctx_manager_usagesuppress_errorslog_compilation_metrics)r'   
setUpClass
contextlib	ExitStackr#   enter_contextr
   patchr)   s    r!   r1   zTestCase.setUpClassJ   sE    $..0%%LL+/ %(-	
r,   c                    t        j                         | _        t        |           t                t        j                  j                          t        j                         | _        t        j                  | j                         y r%   )r   is_grad_enabled_prior_is_grad_enabledr'   setUpr   r   countersclearloggingNullHandlerhandlerr   
addHandler)selfr+   s    r!   r9   zTestCase.setUpV   sT    &+&;&;&=#**,T\\*r,   c                    t        j                  | j                         t        j                  j                         D ]  \  }}t        ||j                                ! t                t        j                  j                          t        | -          | j                  t        j                         ur5t        j!                  d       t        j"                  | j                         y y )NzRunning test changed grad mode)r   removeHandlerr>   r   r:   itemsprintmost_commonr   r;   r'   tearDownr8   r   r7   logwarningset_grad_enabled)r@   kvr+   s      r!   rF   zTestCase.tearDown^   s    -NN((* 	&DAq!Q]]_%	&&&e.C.C.EEKK89""4#>#>? Fr,   r   N)__name__
__module____qualname__r2   r3   __annotations__classmethodr(   r1   r9   rF   __classcell__r+   s   @r!   r   r   B   sF    %%%    	
 	
+	@ 	@r,   r   c                   P    e Zd ZU dZej
                  ed<   dZej                  j                  Z
ej                  j                  Zej                  j                  Zej                  j                  Zej                  j                  Zej                  j                  Zej                  j                   Zej                  j"                  Zej                  j$                  Zej                  j&                  Zej                  j(                  Zej                  j*                  Zej                  j,                  Zej                  j.                  Zej                  j0                  Zej                  j2                  Zej                  j4                  Zej                  j6                  Zej                  j8                  Zej                  j:                  Zej                  j<                  Zej                  j>                  Zej                  j@                  Z ej                  jB                  Z!ej                  jD                  Z"ej                  jF                  Z#ej                  jH                  Z$ej                  jJ                  Z%ej                  jL                  Z&ej                  jN                  Z'ej                  jP                  Z(ej                  jR                  Z)ej                  jT                  Z*ej                  jV                  Z+d Z, fdZ-e.d fd       Z/e.d fd       Z0 xZ1S )	CPythonTestCasez
    Test class for CPython tests located in "test/dynamo/CPython/Py_version/*".

    This class enables specific features that are disabled by default, such as
    tracing through unittest methods.
    _stackTc                     t        | | j                        }t        j                  j	                  ||      |      }t        | | j                  |       |S )N)nopython)getattr_testMethodNamer   _dynamooptimizesetattr)r@   fnbackendrX   methods        r!   
compile_fnzCPythonTestCase.compile_fn   sK     t334''('CFKd**F3	r,   c                    t         |          }| j                  }t        j                  |      j                  t        j                        d   j                  d      d   }t        j                  dt        j                  |            }|r>|j                         j                  t        j                        j                  dd      }n|S d| d| d| S )	N.r   z
/([\d_]+)/_ CPython-)r'   _dynamo_test_keyr+   inspectgetfilesplitr   sepresearchgroupstripreplace)r@   suffixtest_cls	test_filepy_verr+   s        r!   ri   z CPythonTestCase._dynamo_test_key   s    )+>>OOH-33BFF;B?EEcJ1M	='//(*CD\\^))"&&199#rBFM)AfX66r,   c                 V    | j                   j                          t        |           y r%   )rV   r&   r'   r(   r)   s    r!   r(   zCPythonTestCase.tearDownClass   s    

r,   c                 t   t         j                  j                  dd      t         j                  j                  z   }t	        j
                  |      dz   }t        j                  |       }t	        j                  ||      }|rt        t        t        |j                         j                  |      j                  d                  }t        j                   d d }||k  rdj                  t        t"        |            }dj                  t        t"        |            }t%        j&                  d| d|       t%        j&                  d	t        j                  |              t(        	| U          t-        j.                         | _        | j0                  j3                  t5        j6                  d
             y )Ndynamocpythonz\d_\d{2}re      rd   zTest requires Python z but got Python z>Test requires a specific Python version but not found in path T)enable_trace_unittest)r   pathjoinrm   rn   escaperj   rk   ro   tuplemapintrp   removeprefixrl   sysversion_infor   unittestSkipTestr'   r1   r2   r3   rV   r4   r
   r5   )
r*   prefixregexsearch_pathmtest_py_verrv   expectedgotr+   s
            r!   r1   zCPythonTestCase.setUpClass   sZ    h	2RWW[[@		&!K/ooc*IIe[)C)?)?)G)M)Mc)R STK%%bq)F#88C[$9:hhs3/0''+H:5EcUK  ##PQXQ`Q`adQePfg  	))+


  LL&*	
r,   rL   )2rM   rN   rO   __doc__r2   r3   rP   dynamo_strict_nopythonr   r   assertEqualassertNotEqual
assertTrueassertFalseassertIsassertIsNotassertIsNoneassertIsNotNoneassertInassertNotInassertIsInstanceassertNotIsInstanceassertAlmostEqualassertNotAlmostEqualassertGreaterassertGreaterEqual
assertLessassertLessEqualassertRegexassertNotRegexassertCountEqualassertMultiLineEqualassertSequenceEqualassertListEqualassertTupleEqualassertSetEqualassertDictEqualassertRaisesassertRaisesRegexassertWarnsassertWarnsRegex
assertLogsfailfailureExceptionra   ri   rQ   r(   r1   rR   rS   s   @r!   rU   rU   j   s       ! ##//K&&55N""--J##//K  ))H##//K$$11L''77O  ))H##//K((99"++?? ));;#,,AA%%33M!**==""--J''77O##//K&&55N((99#,,AA"++??''77O((99&&55N''77O$$11L ));;##//K((99""--J!!D((99	7     
 
r,   rU   ) ) r   r2   r   rj   r<   r   rn   r   r   typingr   r   torch.testingtorch._logging._internalr   r   r   r   r   r   TorchTestCaserf   r
   r   r   	getLoggerrM   rG   r   r   r   rU   r   r,   r!   <module>r      s        	 	 
     .  # " g!U3c3h/0 $ 6%@} %@Pb
h b
r,   