o
    ‡ZŽ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   úQ/var/www/auris/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)Zdebug_dir_rootFr   )Zpattern_matcherZ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 )NZPYTORCH_KEEP_TMPDIRÚ0Ú1z%test_minifier_common tmpdir kept at: )ÚosÚgetenvÚshutilÚrmtreer/   Úprintr*   Úcloser1   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)Zcompile_errorZruntime_errorZaccuracyr	   z
torch._inductor.config.ÚcpuÚcppZtritonz .inject_relu_bug_TESTING_ONLY = )r,   r-   r.   Úcodegen_configr0   )r   ZdeviceZ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"   Z__compile_source__©Úfileó    úutf-8TF)Úcapture_outputrB   Úcheck) ÚlenÚopenÚreadr,   r-   r.   Zget_config_copyr0   ÚioÚStringIOÚloggingÚStreamHandlerÚ	getLoggerÚ
addHandlerr   r7   ÚgetcwdÚchdirr   r   ÚexecÚ	ExceptionÚ	tracebackÚ	print_excÚremoveHandlerÚresetZload_configÚ
subprocessÚCompletedProcessÚgetvalueÚencodeÚrun)r   ÚargsÚisolaterB   ÚcodeÚfZdynamo_configZinductor_configÚstderrZlog_handlerÚlogZ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 )	NrD   rE   ©re   rB   ztest stdout:rK   ztest stderr:z(\S+)minifier_launcher.pyr   )	rk   r/   r;   ÚstdoutÚdecoderh   r   r   r   )r   rf   re   ÚprocZ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.pyrD   ZminifyÚaot_inductorz--no-isolaterl   zminifier stdout:rK   zminifier stderr:z#Input graph did not fail the tester)ÚassertIsNotNoner   r7   ÚpathÚjoinrO   rP   Ú
assertTrueÚexistsÚappendrk   r;   rm   rn   rh   ZassertNotIn)r   Ú	repro_dirre   rq   rr   Zlaunch_filerg   Zlaunch_coderd   Zlaunch_procrh   r   r   r   Ú_run_minifier_launcher¾   s   


ÿ

z'MinifierTestBase._run_minifier_launcherT©re   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.pyrD   rl   zrepro stdout:rK   zrepro stderr:)rt   r   r7   ru   rv   rO   rP   rw   rx   rk   r;   rm   rn   rh   )r   rz   re   Z
repro_filerg   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Ú rs   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,   r-   r.   r@   r0   r/   )r   Úrun_coderr   Úrepro_levelZrepro_after_liner   r   r   Ú_gen_test_codeç   s.   ÿÿüûúùø	÷
özMinifierTestBase._gen_test_code)rq   Ú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 )NrF   ZAccuracyErroré   rC   zrunning testrH   r|   r   rK   zrunning minifier)re   rq   rr   zrunning repro)r   r   )r‚   r;   Úsysrh   rp   ÚassertEqualÚ
returncodeZassertIsNoneZassertInrn   rt   r{   r~   ZassertNotEqualr   )r   r€   rr   Zexpected_errorre   rq   r   Z	test_codeZ	test_procrz   Z_minifier_procr   r}   r   r   r   r   Ú_run_full_test  s4   


üzMinifierTestBase._run_full_test)r"   r#   r$   ÚtempfileÚmkdtempr/   Úclassmethodr)   r=   rA   rk   rp   r{   r~   r‚   r   r   rˆ   Ú__classcell__r   r   r3   r   r'   F   s"    
Aÿ!ÿþr'   )Ú__doc__ÚdataclassesrQ   rS   r7   r   r9   r_   r…   r‰   r[   Útypingr   Zunittest.mockr   r,   Ztorch._dynamoZtorch._dynamo.test_caseZtorch._dynamo.trace_rulesr   Ztorch.utils._tracebackr   Ú	dataclassr   r-   Z	test_caseZTestCaser'   r   r   r   r   Ú<module>   s*   