o
    Iñh).  ã                   @   sÊ   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„ dƒƒZG d	d
„ d
ejjjƒZdS )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                   @   s>   e Zd ZU eed< eed< dd„ Zdd„ Zdd„ Zd	d
„ ZdS )ÚMinifierTestResultÚminifier_codeÚ
repro_codec                 C   sP   t  d|¡}|d usJ dƒ‚| d¡}t jdd|t jd}t  dd|¡}| ¡ 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Úr© r   úV/var/www/vscode/kcb/lib/python3.10/site-packages/torch/_dynamo/test_minifier_common.pyÚ_get_module+   s   
zMinifierTestResult._get_modulec                 C   s(   d}t  || j¡}|r| d¡}|S d S )Nz,torch\.export\.load\(\s*["\'](.*?)["\']\s*\)é   )r   r   r   r   )r   Úpatternr   Ú	file_pathr   r   r   Úget_exported_program_path3   s   
z,MinifierTestResult.get_exported_program_pathc                 C   ó   |   | j¡S ©N)r   r   ©r   r   r   r   Úminifier_module?   ó   z"MinifierTestResult.minifier_modulec                 C   r   r   )r   r   r   r   r   r   Úrepro_moduleB   r    zMinifierTestResult.repro_moduleN)	Ú__name__Ú
__module__Ú__qualname__ÚstrÚ__annotations__r   r   r   r!   r   r   r   r   r   &   s   
 r   c                       s   e Zd Ze ¡ Ze‡ fdd„ƒZedd„ ƒZdd„ Z	ddœd	d
„Z
dd„ Zdddœdd„Zddœdd„Zdd„ Zddœdee fdd„Z‡  ZS )ÚMinifierTestBasec                    sF   t ƒ  ¡  | j tjjj| jd¡ | j tj	j ddddœ¡¡ d S )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__r   r   r,   I   s   
ÿúÿÿzMinifierTestBase.setUpClassc                 C   s<   t  dd¡dkrt | j¡ ntd| j› ƒ | j ¡  d S )NÚPYTORCH_KEEP_TMPDIRÚ0Ú1z%test_minifier_common tmpdir kept at: )ÚosÚgetenvÚshutilÚrmtreer2   Úprintr-   Úcloser4   r   r   r   ÚtearDownClass^   s   zMinifierTestBase.tearDownClassc                 C   sD   |dv sJ ‚t jj ¡ › dt 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 = )r/   r0   r1   Úcodegen_configr3   )r   ÚdeviceÚbug_typer   r   r   Ú_gen_codegen_fn_patch_codef   s   
ÿ
þýýz+MinifierTestBase._gen_codegen_fn_patch_codeN)Úcwdc             	   C   st  |s(t |ƒdksJ |ƒ‚|d dksJ |ƒ‚|d dkr/t |ƒdks'J |ƒ‚|d }dg}n*t |ƒdks9J |ƒ‚t|d ƒ}| ¡ }W d   ƒ n1 sNw   Y  |dd … }tjj ¡ }tjj ¡ }z¦t 	¡ }t
 |¡}	t
 d¡}
|
 |	¡ znzGtt ¡ ƒ}|d urŽt|ƒ}t |¡ td|ƒ# tƒ  t|d	|d
œƒ W d   ƒ n1 sªw   Y  W d   ƒ n1 s¹w   Y  d}W n tyÒ   d}tj|d Y nw W |
 |	¡ |d urât |¡ tj ¡  n|
 |	¡ |d uröt |¡ tj ¡  w W tjj |¡ tjj |¡ ntjj |¡ tjj |¡ w t ||d| ¡  d¡¡S |d ur1t|ƒ}tj|d|ddS )Né   r   Úpython3r   ú-cé   ztorch._dynamozsys.argvÚ__main__)r"   Ú__compile_source__©Úfileó    úutf-8TF)Úcapture_outputrL   Úcheck) ÚlenÚopenÚreadr/   r0   r1   Úget_config_copyr3   Ú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   ÚargsÚisolaterL   ÚcodeÚfÚdynamo_configÚinductor_configÚstderrÚlog_handlerÚlogÚprev_cwdÚrcr   r   r   Ú_maybe_subprocess_runn   sp   
ÿ	



ÿ€ þ€


û
ÿü
z&MinifierTestBase._maybe_subprocess_runc                C   sn   | j dd|g|| jd}td|j d¡ƒ td|j d¡ƒ t d|j d¡¡}|d ur3|| d¡fS |d fS )	NrN   rO   ©rr   rL   ztest stdout:rV   ztest stderr:z(\S+)minifier_launcher.pyr   )	r|   r2   r?   ÚstdoutÚdecoderw   r   r   r   )r   rs   rr   ÚprocÚrepro_dir_matchr   r   r   Ú_run_test_code¯   s   ÿÿzMinifierTestBase._run_test_coder   )Úminifier_argsÚrepro_afterc                C   sÎ   |   |¡ ttj |d¡ƒ}t|ƒ}| ¡ }W d   ƒ n1 s!w   Y  |  tj |¡¡ d|dg|¢}|sA|dkrA| 	d¡ | j
|||d}	td|	j d¡ƒ |	j d¡}
td	|
ƒ |  d
|
¡ |	|fS )Nzminifier_launcher.pyrN   ÚminifyÚaot_inductorz--no-isolater}   zminifier stdout:rV   zminifier stderr:z#Input graph did not fail the tester)ÚassertIsNotNoner   r;   ÚpathÚjoinrZ   r[   Ú
assertTrueÚexistsÚappendr|   r?   r~   r   rw   ÚassertNotIn)r   Ú	repro_dirrr   rƒ   r„   Úlaunch_filert   Úlaunch_coderq   Úlaunch_procrw   r   r   r   Ú_run_minifier_launcher¾   s   


ÿ

z'MinifierTestBase._run_minifier_launcherT©rr   c                C   sž   |   |¡ ttj |d¡ƒ}t|ƒ}| ¡ }W d   ƒ n1 s!w   Y  |  tj |¡¡ | j	d|g||d}t
d|j d¡ƒ t
d|j d¡ƒ ||fS )Nzrepro.pyrN   r}   zrepro stdout:rV   zrepro stderr:)r‡   r   r;   rˆ   r‰   rZ   r[   rŠ   r‹   r|   r?   r~   r   rw   )r   rŽ   rr   Ú
repro_filert   r   Ú
repro_procr   r   r   Ú
_run_reproÕ   s   


ÿ
ÿzMinifierTestBase._run_reproc                 C   sp   d}|dkr	d}n|rd|› d}dt tjj ¡ ƒ› dt t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   r/   r0   r1   rH   r3   r2   )r   Úrun_coder„   Úrepro_levelÚrepro_after_liner   r   r   Ú_gen_test_codeç   s.   ÿÿüûúùø	÷
özMinifierTestBase._gen_test_code)rƒ   Úreturnc                C   s  |rd}n|d u s|dkrd}nd}|   |||¡}tdtjd | j||d\}}	|d u r;|  |jd¡ |  |	¡ d S |  ||j 	d	¡¡ |  
|	¡ td
tjd | j|	|||d\}
}tdtjd | j|	|d\}}|  ||j 	d	¡¡ |  |jd¡ t||dS )NrP   ÚAccuracyErroré   rM   zrunning testrS   r“   r   rV   zrunning minifier)rr   rƒ   r„   zrunning repro)r   r   )r›   r?   Úsysrw   r‚   ÚassertEqualÚ
returncodeÚassertIsNoneÚassertInr   r‡   r’   r–   ÚassertNotEqualr   )r   r˜   r„   Úexpected_errorrr   rƒ   r™   Ú	test_codeÚ	test_procrŽ   Ú_minifier_procr   r•   r   r   r   r   Ú_run_full_test  s4   


üzMinifierTestBase._run_full_test)r"   r#   r$   ÚtempfileÚmkdtempr2   Úclassmethodr,   rA   rK   r|   r‚   r’   r–   r›   r   r   r©   Ú__classcell__r   r   r6   r   r'   F   s"    
Aÿ!ÿþr'   )Ú__doc__Údataclassesr]   r_   r;   r   r=   rl   rŸ   rª   rg   Útypingr   Úunittest.mockr   r/   Útorch._dynamoÚtorch._dynamo.test_caseÚtorch._dynamo.trace_rulesr   Útorch.utils._tracebackr   Ú	dataclassr   r0   Ú	test_caseÚTestCaser'   r   r   r   r   Ú<module>   s*   