
    Vh).                     $   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 ddlZddlZddlZddlmZ ddlmZ ej,                   G d d             Z G d	 d
ej0                  j2                  j4                        Zy)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)Optional)patch)_as_posix_path)report_compile_source_on_errorc                   :    e Zd ZU eed<   eed<   d Zd Zd Zd Zy)MinifierTestResultminifier_code
repro_codec                     t        j                  d|      }|J d       |j                  d      }t        j                  dd|t         j                        }t        j                  dd|      }|j                         S )	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)selftmatchrs       R/home/dcms/DCMS/lib/python3.12/site-packages/torch/_dynamo/test_minifier_common.py_get_modulezMinifierTestResult._get_module+   si    		PRST 9"99 KKNFF7D!2<<8FF9fa(wwy    c                 r    d}t        j                  || j                        }|r|j                  d      }|S y )Nz,torch\.export\.load\(\s*["\'](.*?)["\']\s*\)   )r   r   r
   r   )r   patternr   	file_paths       r   get_exported_program_pathz,MinifierTestResult.get_exported_program_path3   s7     B		'4??3AIr   c                 8    | j                  | j                        S N)r   r	   r   s    r   minifier_modulez"MinifierTestResult.minifier_module?   s     2 233r   c                 8    | j                  | j                        S r!   )r   r
   r"   s    r   repro_modulezMinifierTestResult.repro_moduleB   s    00r   N)	__name__
__module____qualname__str__annotations__r   r   r#   r%    r   r   r   r   &   s!    O
41r   r   c                        e Zd Z ej                         Ze fd       Zed        Zd Z	dddZ
d Zddd	d
ZdddZd Zdddee   fdZ xZS )MinifierTestBasec                 H   t         |           | j                  j                  t        j
                  j                  j                  | j                               | j                  j                  t        j                  j                  j                  dddd             y )N)debug_dir_rootFr   )pattern_matchercompile_threadszcpp.vec_isa_ok)
super
setUpClass_exit_stackenter_contexttorch_dynamoconfigr   	DEBUG_DIR	_inductor)cls	__class__s    r   r3   zMinifierTestBase.setUpClassI   s{    %%MM  &&cmm&D	

 	%%OO""(( (-'(&+		
r   c                     t        j                  dd      dk7  r t        j                  | j                         nt        d| j                          | j                  j                          y )NPYTORCH_KEEP_TMPDIR01z%test_minifier_common tmpdir kept at: )osgetenvshutilrmtreer9   printr4   close)r;   s    r   tearDownClasszMinifierTestBase.tearDownClass^   sJ    99*C0C7MM#--(9#--IJr   c                     |dv sJ t         j                  j                  j                          dt         j                  j                  j                          d|dk(  rdnd d|dS )N)compile_errorruntime_erroraccuracyr   z
torch._inductor.config.cpucpptritonz .inject_relu_bug_TESTING_ONLY = )r6   r7   r8   codegen_configr:   )r   devicebug_types      r   _gen_codegen_fn_patch_codez+MinifierTestBase._gen_codegen_fn_patch_codef   st    IIII$$& ' (&&( ) *!'5h??_`h_k l 	r   N)cwdc                   |srt        |      dk\  sJ |       |d   dk(  sJ |       |d   dk(  rt        |      dk(  sJ |       |d   }dg}nAt        |      dk\  sJ |       t        |d         5 }|j                         }d d d        |dd  }t        j                  j
                  j                         }t        j                  j
                  j                         }	 t        j                         }t        j                  |      }	t        j                  d      }
|
j                  |	       	 t        t        j                                }| t        |      }t        j"                  |       t%        d|      5  t'               5  t)        d	|d
       d d d        d d d        d}|
j1                  |	       |t        j"                         t        j                  j3                          	 t        j                  j
                  j5                  |       t        j                  j
                  j5                  |       t7        j8                  ||d|j;                         j=                  d            S |t        |      }t7        j>                  |d|d      S # 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY w# t*        $ r d}t-        j.                  |       Y ?w xY w# |
j1                  |	       |t        j"                         t        j                  j3                          w xY w# t        j                  j
                  j5                  |       t        j                  j
                  j5                  |       w xY w)N   r   python3r   -c   ztorch._dynamozsys.argv__main__)r&   __compile_source__filer   utf-8TF)capture_outputrS   check) lenopenreadr6   r7   r8   get_config_copyr:   ioStringIOloggingStreamHandler	getLogger
addHandlerr   rA   getcwdchdirr   r   exec	Exception	traceback	print_excremoveHandlerresetload_config
subprocessCompletedProcessgetvalueencoderun)r   argsisolaterS   codefdynamo_configinductor_configstderrlog_handlerlogprev_cwdrcs                r   _maybe_subprocess_runz&MinifierTestBase._maybe_subprocess_runn   s   t9>'4'>7i'--'Aw$4yA~+t+~Awv4yA~+t+~$q'] $a668D$ABx "MM00@@BM#oo44DDFOD%33F;''8{+*-biik:H,S1z40 Y2P2R YT
RV#WXY YB
 %%k2* MM'')$$00?&&22?C ..!((1	  $S)>>$tERRe$ $.Y Y Y Y ! 5B''V445 %%k2* MM'')$$00?&&22?Cs}   1J AL6 0AK ;J6J)J6
K (AL6 J&)J3	.J66K ;K !K($K+ 'K((K+ +AL33L6 6AN
c                h   | j                  dd|g|| j                        }t        d|j                  j	                  d             t        d|j
                  j	                  d             t        j                  d|j
                  j	                  d            }|||j                  d      fS |d fS )	NrV   rW   ry   rS   ztest stdout:r]   ztest stderr:z(\S+)minifier_launcher.pyr   )	r   r9   rE   stdoutdecoder~   r   r   r   )r   rz   ry   procrepro_dir_matchs        r   _run_test_codez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_afterc                \   | j                  |       t        t        j                  j	                  |d            }t        |      5 }|j                         }d d d        | j                  t        j                  j                  |             d|dg|}|s|dk7  r|j                  d       | j                  |||      }	t        d|	j                  j                  d             |	j                  j                  d      }
t        d	|
       | j                  d
|
       |	fS # 1 sw Y   xY w)Nzminifier_launcher.pyrV   minifyaot_inductorz--no-isolater   zminifier stdout:r]   zminifier stderr:z#Input graph did not fail the tester)assertIsNotNoner   rA   pathjoinra   rb   
assertTrueexistsappendr   rE   r   r   r~   assertNotIn)r   	repro_dirry   r   r   launch_filer{   launch_coderx   launch_procr~   s              r   _run_minifier_launcherz'MinifierTestBase._run_minifier_launcher   s    	Y'$RWW\\)=S%TU+ 	#!&&(K	#{34;A=A;.8 KK'00wI0V +"4"4";";G"DE##**73 &)>GK''	# 	#s   D""D+Try   c                   | j                  |       t        t        j                  j	                  |d            }t        |      5 }|j                         }d d d        | j                  t        j                  j                  |             | j                  d|g||      }t        d|j                  j                  d             t        d|j                  j                  d             |fS # 1 sw Y   xY w)Nzrepro.pyrV   r   zrepro stdout:r]   zrepro stderr:)r   r   rA   r   r   ra   rb   r   r   r   rE   r   r   r~   )r   r   ry   
repro_filer{   r
   
repro_procs          r   
_run_reproz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   C11C:c                 8   d}|dk(  rd}n|rd| d}dt        t        j                  j                  j	                                dt        t        j
                  j                  j	                                d| d| d	| d
t        | j                         d| dS )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   r6   r7   r8   rO   r:   r9   )r   run_coder   repro_levelrepro_after_lines        r   _gen_test_codez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   returnc                   |rd}n||dk(  rd}nd}| j                  |||      }t        dt        j                         | j	                  ||      \  }}	|.| j                  |j                  d       | j                  |	       y | j                  ||j                  j                  d	             | j                  |	       t        d
t        j                         | j                  |	|||      \  }
}t        dt        j                         | j                  |	|      \  }}| j                  ||j                  j                  d	             | j                  |j                  d       t        ||      S )NrX   AccuracyError   rU   zrunning testr[   r   r   r]   zrunning minifier)ry   r   r   zrunning repro)r	   r
   )r   rE   sysr~   r   assertEqual
returncodeassertIsNoneassertInr   r   r   r   assertNotEqualr   )r   r   r   expected_errorry   r   r   	test_code	test_procr   _minifier_procr	   r   r
   s                 r   _run_full_testzMinifierTestBase._run_full_test  sZ    K#~'HKK''+{K	n3::.#229g2N	9!Y1115i( 	ni&6&6&=&=g&FGY' szz2(,(C(C'#	 )D )
% 	oCJJ/!%G!L
Jnj&7&7&>&>w&GHJ1115!*UUr   )r&   r'   r(   tempfilemkdtempr9   classmethodr3   rG   rR   r   r   r   r   r   r   r   r   __classcell__)r<   s   @r   r-   r-   F   s       "I
 
(     ;? <SB  464(. 04 &$B PR V	$	% Vr   r-   )__doc__dataclassesrd   rf   rA   r   rC   rs   r   r   rn   typingr   unittest.mockr   r6   torch._dynamotorch._dynamo.test_casetorch._dynamo.trace_rulesr   torch.utils._tracebackr   	dataclassr   r7   	test_caseTestCaser-   r+   r   r   <module>r      s~     	  	 	   
        4 A 1 1 1>aVu}}..77 aVr   