
    ȅi1                     8   S r SSKrSSKrSSKrSSKrSSKrSSKrSSKrSSKrSSK	r	SSK
r
SSKJr  SSKJrJrJr  SSKJr  SSKrSSKrSSKrSSKJr  SSKJr  \R4                   " S S	5      5       r " S
 S\R8                  R:                  R<                  5      rg)au  Common utilities for testing Dynamo's minifier functionality.

This module provides the base infrastructure for running minification tests in Dynamo.
It includes:
- MinifierTestResult: A dataclass for storing and processing minifier test results
- MinifierTestBase: A base test class with utilities for:
  - Running tests in isolated environments
  - Managing temporary directories and configurations
  - Executing minifier launcher scripts
  - Running and validating reproduction scripts
  - Supporting both compile-time and runtime error testing

The minifier helps reduce failing Dynamo compilations to minimal reproductions.
    N)Sequence)AnyOptionalUnion)patch)_as_posix_path)report_compile_source_on_errorc                   l    \ rS rSr% \\S'   \\S'   S\S\4S jrS\\   4S jrS\4S jr	S\4S	 jr
S
rg)MinifierTestResult%   minifier_code
repro_codetreturnc                     [         R                  " SU5      nUc   S5       eUR                  S5      n[         R                  " SSU[         R                  S9n[         R                  " SSU5      nUR                  5       $ )	Nz3class Repro\(torch\.nn\.Module\):\s+([ ].*\n| *\n)+zfailed to find moduler   z\s+$
)flagsz\n{3,}z

)researchgroupsub	MULTILINEstrip)selfr   matchrs       \/home/james-whalen/.local/lib/python3.13/site-packages/torch/_dynamo/test_minifier_common.py_get_moduleMinifierTestResult._get_module*   si    		PRST 9"99 KKNFF7D!2<<8FF9fa(wwy    c                 |    Sn[         R                  " XR                  5      nU(       a  UR                  S5      nU$ g )Nz,torch\.export\.load\(\s*["\'](.*?)["\']\s*\)   )r   r   r   r   )r   patternr   	file_paths       r   get_exported_program_path,MinifierTestResult.get_exported_program_path2   s5     B		'??3AIr    c                 8    U R                  U R                  5      $ N)r   r   r   s    r   minifier_module"MinifierTestResult.minifier_module>   s     2 233r    c                 8    U R                  U R                  5      $ r(   )r   r   r)   s    r   repro_moduleMinifierTestResult.repro_moduleA   s    00r     N)__name__
__module____qualname____firstlineno__str__annotations__r   r   r%   r*   r-   __static_attributes__r/   r    r   r   r   %   sH    OS S 
8C= 
4 41c 1r    r   c                     ^  \ rS rSr\R
                  " 5       r\SU 4S jj5       r\SS j5       r	S\
S\
S\
4S jrSS	.S
\\   S\S\\
   S\R"                  \   4S jjrS\
S\S\\R"                  \   \\
\4   4   4S jrSSS.S\
S\S\\   S\\
   S\\R"                  \   \
4   4
S jjrSS.S\
S\S\\R"                  \   \
4   4S jjrS\
S\
S\S\
4S jrSS.S\
S\
S\\
   S\S\\   S\\   4S jjrSrU =r$ ) MinifierTestBaseE   r   Nc                   > [         TU ]  5         [        R                  R	                  U R
                  5      (       d  [        R                  " 5       U l        U R                  R                  [        R                  R                  R                  U R
                  S95        U R                  R                  [        R                  R                  R                  SSSS.5      5        g )N)debug_dir_rootFr"   )pattern_matchercompile_threadszcpp.vec_isa_ok)super
setUpClassospathexists	DEBUG_DIRtempfilemkdtemp_exit_stackenter_contexttorch_dynamoconfigr   	_inductor)cls	__class__s    r   r?   MinifierTestBase.setUpClassH   s    ww~~cmm,,$,,.CM%%MM  &&cmm&D	

 	%%OO""(( (-'(&+		
r    c                     [         R                  " SS5      S:w  a!  [        R                  " U R                  5        O[        SU R                   35        U R                  R                  5         g )NPYTORCH_KEEP_TMPDIR01z%test_minifier_common tmpdir kept at: )r@   getenvshutilrmtreerC   printrF   close)rL   s    r   tearDownClassMinifierTestBase.tearDownClass_   sJ    99*C0C7MM#--(9#--IJr    devicebug_typec                     US;   d   e[         R                  R                  R                  5        S[         R                  R                  R                  5        SUS:X  a  SOS SU< S3$ )N)compile_errorruntime_erroraccuracyr   z
torch._inductor.config.cpucpptritonz .inject_relu_bug_TESTING_ONLY = )rH   rI   rJ   codegen_configrK   )r   rZ   r[   s      r   _gen_codegen_fn_patch_code+MinifierTestBase._gen_codegen_fn_patch_codeg   st    IIII$$& ' (&&( ) *!'5h??_`h_k l 	r    )cwdargsisolaterf   c                v   SSK Jn  U(       Gd  [        U5      S:  d   U5       eUS   S:X  d   U5       eUS   S:X  a  [        U5      S:X  d   U5       eUS   nS/nOH[        U5      S:  d   U5       e[        US   5       nU" UR	                  5       5      nS S S 5        USS  n[
        R                  R                  R                  5       n[
        R                  R                  R                  5       n [        R                  " 5       n	[        R                  " U	5      n
[        R                  " S5      nUR                  U
5         [!        ["        R$                  " 5       5      nUb!  [!        U5      n["        R&                  " U5        [)        S	U5         [+        5          [-        WS
US.5        S S S 5        S S S 5        SnUR5                  U
5        Ub  ["        R&                  " W5        [
        R                  R7                  5          [
        R                  R                  R9                  U5        [
        R                  R                  R9                  U5        [:        R<                  " UUSU	R?                  5       RA                  S5      5      $ Ub  [!        U5      n[:        RB                  " USUSS9$ ! , (       d  f       GN)= f! , (       d  f       GN!= f! , (       d  f       GN+= f! [.         a    Sn[0        R2                  " U	S9   GNOf = f! UR5                  U
5        Ub  ["        R&                  " W5        [
        R                  R7                  5         f = f! [
        R                  R                  R9                  U5        [
        R                  R                  R9                  U5        f = f)Nr   )normalize_path_separator   python3r"   -c   ztorch._dynamozsys.argv__main__)r0   __compile_source__filer    utf-8TF)capture_outputrf   check)"torch._inductor.cpp_builderrj   lenopenreadrH   rI   rJ   get_config_copyrK   ioStringIOloggingStreamHandler	getLogger
addHandlerr   r@   getcwdchdirr   r	   exec	Exception	traceback	print_excremoveHandlerresetload_config
subprocessCompletedProcessgetvalueencoderun)r   rg   rh   rf   rj   codefdynamo_configinductor_configstderrlog_handlerlogprev_cwdrcs                 r   _maybe_subprocess_run&MinifierTestBase._maybe_subprocess_runo   s    	It9>'4'>7i'--'Aw$4yA~+t+~Awv4yA~+t+~$q']a3AFFH=D # ABx "MM00@@BM#oo44DDFOD%33F;''8{+*-biik:H,S1z402P2RT
RV#WX 3S0B
 %%k2* MM'')$$00?&&22?C ..!((1	  $S)>>$tERRg #]0 3S2R00 ! 5B''V45 %%k2* MM'')$$00?&&22?Cs}   J:6AM$ 	AK0 K"K2K:
K0 AM$ :
K	
K	K
K-(K0 0 LL LL A
M!!M$ $AN8r   c                b   U R                  SSU/X R                  S9n[        SUR                  R	                  S5      5        [        SUR
                  R	                  S5      5        [        R                  " SUR
                  R	                  S5      5      nUb  X4R                  S5      4$ US 4$ )	Nrl   rm   rh   rf   ztest stdout:rs   ztest stderr:z(\S+)minifier_launcher.pyr"   )	r   rC   rV   stdoutdecoder   r   r   r   )r   r   rh   procrepro_dir_matchs        r   _run_test_codeMinifierTestBase._run_test_code   s     ))d#W.. * 
 	ndkk009:ndkk009:))($++*<*<W*E
 &..q111Tzr    r/   )minifier_argsrepro_after	repro_dirr   r   c                j   U R                  U5        [        [        R                  R	                  US5      5      n[        U5       nUR                  5       nS S S 5        U R                  [        R                  R                  U5      5        SUS/UQnU(       d  US:w  a  UR                  S5        U R                  XUS9n	[        SU	R                  R                  S5      5        U	R                  R                  S5      n
[        S	U
5        U R                  S
U
5        U	W4$ ! , (       d  f       N= f)Nzminifier_launcher.pyrl   minifyaot_inductorz--no-isolater   zminifier stdout:rs   zminifier stderr:z#Input graph did not fail the tester)assertIsNotNoner   r@   rA   joinrx   ry   
assertTruerB   appendr   rV   r   r   r   assertNotIn)r   r   rh   r   r   launch_filer   launch_coderg   launch_procr   s              r   _run_minifier_launcher'MinifierTestBase._run_minifier_launcher   s    	Y'$RWW\\)=S%TU+!&&(K  	{34;A=A;.8 KK'00I0V +"4"4";";G"DE##**73 &)>GK''# s   D$$
D2Trh   c                   U R                  U5        [        [        R                  R	                  US5      5      n[        U5       nUR                  5       nS S S 5        U R                  [        R                  R                  U5      5        U R                  SU/X!S9n[        SUR                  R                  S5      5        [        SUR                  R                  S5      5        UW4$ ! , (       d  f       N= f)Nzrepro.pyrl   r   zrepro stdout:rs   zrepro stderr:)r   r   r@   rA   r   rx   ry   r   rB   r   rV   r   r   r   )r   r   rh   
repro_filer   r   
repro_procs          r   
_run_reproMinifierTestBase._run_repro   s     	Y'#BGGLLJ$GH
*J  	z23//
#W 0 

 	oz0077@Aoz0077@A:%% s   C--
C;run_coderepro_levelc                 D   SnUS:X  a  SnOU(       a  SU S3nS[        [        R                  R                  R	                  5       5       S[        [        R
                  R                  R	                  5       5       SU SU S	U S
[        U R                  5       SU S3$ )N r   z=torch._inductor.config.aot_inductor.dump_aoti_minifier = Truez$torch._dynamo.config.repro_after = "z
"
        z9import torch
import torch._dynamo
import torch._inductor
r   z$
torch._dynamo.config.repro_level = z3
torch._inductor.config.aot_inductor.repro_level = z(
torch._dynamo.config.debug_dir_root = "z"
)r   rH   rI   rJ   rc   rK   rC   )r   r   r   r   repro_after_lines        r   _gen_test_codeMinifierTestBase._gen_test_code   s    .(O  $%%0M 2	  $$3356 7 8&&5578 9 :  $$/= 133>- @((6t~~(F'G H	
  	r    )r   expected_errorc                   U(       a  SnOUb  US:X  a  SnOSnU R                  XU5      n[        S[        R                  S9  U R	                  XtS9u  pUc.  U R                  UR                  S5        U R                  U	5        g U R                  X8R                  R                  S	5      5        U R                  U	5        [        S
[        R                  S9  U R                  U	UUUS9u  p[        S[        R                  S9  U R                  XS9u  pU R                  X<R                  R                  S	5      5        U R                  UR                  S5        [        XS9$ )Nrn   AccuracyError   rk   zrunning testrq   r   r   rs   zrunning minifier)rh   r   r   zrunning repro)r   r   )r   rV   sysr   r   assertEqual
returncodeassertIsNoneassertInr   r   r   r   assertNotEqualr   )r   r   r   r   rh   r   r   	test_code	test_procr   _minifier_procr   r   r   s                 r   _run_full_testMinifierTestBase._run_full_test  sH    K#~'HKK''{K	n3::.#2292N	!Y1115i( 	n&6&6&=&=g&FGY' szz2(,(C(C'#	 )D )
% 	oCJJ/!%!L
n&7&7&>&>w&GHJ1115!UUr    )r   N)r0   r1   r2   r3   rD   rE   rC   classmethodr?   rX   r4   rd   r   r   boolr   r   r   bytesr   tupler   r   r   r   intr   r   r   r6   __classcell__)rM   s   @r   r8   r8   E   s     "I
 
,        KOASSMAS/3AS:B3-AS		$	$U	+ASL%)	z**515c?B	C, (*%)(( (
  }( c]( 
z**5136	7(> 26&&*.&	z**5136	7&*s  3 SV N (**V*V *V !	*V *V  }*V 
$	%*V *Vr    r8   ) __doc__dataclassesr{   r}   r@   r   rT   r   r   rD   r   collections.abcr   typingr   r   r   unittest.mockr   rH   torch._dynamotorch._dynamo.test_casetorch._dynamo.trace_rulesr   torch.utils._tracebackr	   	dataclassr   rI   	test_caseTestCaser8   r/   r    r   <module>r      s     	  	 	   
   $ ' '     4 A 1 1 1>~Vu}}..77 ~Vr    