
    ^jj3                     0   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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j0                   G d d	             Z G d
 dej4                  j6                  j8                        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)Sequence)Any)patch)_as_posix_path)report_compile_source_on_errorc                   \    e Zd ZU eed<   eed<   dedefdZdedz  fdZdefdZdefd	Zy)
MinifierTestResultminifier_code
repro_codetreturnc                    t        j                  d|      }|t        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AssertionErrorgroupsub	MULTILINEstrip)selfr   matchrs       m/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/torch/_dynamo/test_minifier_common.py_get_modulezMinifierTestResult._get_module*   sh    		PRST= !899KKNFF7D!2<<8FF9fa(wwy    Nc                 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   )	__name__
__module____qualname__str__annotations__r   r"   r&   r(    r   r   r	   r	   %   sH    OS S 
3: 
4 41c 1r   r	   c                       e Zd Z ej                         Zed fd       Zedd       Zde	de	de	fdZ
ddd	ee   d
ede	dz  dej                  e   fdZde	d
edeej                  e   e	ez  f   fdZdddde	d
edee   de	dz  deej                  e   e	f   f
dZddde	d
edeej                  e   e	f   fdZde	de	dede	fdZddde	de	de	dz  d
edee   dedz  fdZ xZS )MinifierTestBaser   Nc                    t         |           t        j                  j	                  | j
                        st        j                         | _        | 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ospathexists	DEBUG_DIRtempfilemkdtemp_exit_stackenter_contexttorch_dynamoconfigr   	_inductor)cls	__class__s    r   r6   zMinifierTestBase.setUpClassI   s    ww~~cmm,$,,.CM%%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: )r7   getenvshutilrmtreer:   printr=   close)rC   s    r   tearDownClasszMinifierTestBase.tearDownClass`   sJ    99*C0C7MM#--(9#--IJr   devicebug_typec                     |dvrt        d|      t        j                  j                  j	                          dt        j
                  j                  j	                          d|dk(  rdnd d|dS )	N)compile_errorruntime_erroraccuracyzDbug_type must be one of compile_error, runtime_error, accuracy, got r   z
torch._inductor.config.cpucpptritonz .inject_relu_bug_TESTING_ONLY = )r   r?   r@   rA   codegen_configrB   )r   rO   rP   s      r   _gen_codegen_fn_patch_codez+MinifierTestBase._gen_codegen_fn_patch_codeh   s    II VW_Vbc  $$& ' (&&( ) *!'5h??_`h_k l 	r   )cwdargsisolaterZ   c                j   ddl m} |st        |      dk  rt        d|       |d   dk7  rt        d|       |d   dk(  r%t        |      d	k7  rt        d
|       |d   }dg}nNt        |      dk  rt        d|       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}|j7                  |
       |t%        j(                         t        j                  j9                          	 t        j                  j                  j;                  |       t        j                  j                  j;                  |       t=        j>                  ||d|	jA                         jC                  d            S |t#        |      }t=        jD                  |d|d      S # 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY w# t0        $ r d}t3        j4                  |	       Y ?w xY w# |j7                  |
       |t%        j(                         t        j                  j9                          w xY w# t        j                  j                  j;                  |       t        j                  j                  j;                  |       w xY w)Nr   )normalize_path_separator   zexpected at least 2 args, got python3z&expected args[0] to be 'python3', got r   -c   z)expected exactly 3 args for -c mode, got ztorch._dynamozsys.argv__main__)r)   __compile_source__filer   utf-8TF)capture_outputrZ   check)#torch._inductor.cpp_builderr^   lenr   openreadr?   r@   rA   get_config_copyrB   ioStringIOloggingStreamHandler	getLogger
addHandlerr   r7   getcwdchdirr   r   exec	Exception	traceback	print_excremoveHandlerresetload_config
subprocessCompletedProcessgetvalueencoderun)r   r[   r\   rZ   r^   codefdynamo_configinductor_configstderrlog_handlerlogprev_cwdrcs                 r   _maybe_subprocess_runz&MinifierTestBase._maybe_subprocess_runs   s!    	I4y1}$'EdV%LMMAw)#$'MdV%TUUAw$t9>(CD6J  Awvt9q=(+I$)PQQ$q'] >a3AFFH=D> 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Rg> >0Y Y Y Y ! 5B''V445 %%k2* MM'')$$00?&&22?Cs}   KAM AK+ #K.K>K
K+ AM KK	KK(#K+ +!LL LL AMM AN2r   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 )	Nr`   ra   r\   rZ   ztest stdout:rg   ztest stderr:z(\S+)minifier_launcher.pyr   )	r   r:   rL   stdoutdecoder   r   r   r   )r   r   r\   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_after	repro_dirr   r   c                \   | 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.pyr`   minifyaot_inductorz--no-isolater   zminifier stdout:rg   zminifier stderr:z#Input graph did not fail the tester)assertIsNotNoner   r7   r8   joinrl   rm   
assertTruer9   appendr   rL   r   r   r   assertNotIn)r   r   r\   r   r   launch_filer   launch_coder[   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r\   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.pyr`   r   zrepro stdout:rg   zrepro stderr:)r   r   r7   r8   r   rl   rm   r   r9   r   rL   r   r   r   )r   r   r\   
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:run_coderepro_levelc                 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   r?   r@   rA   rX   rB   r:   )r   r   r   r   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   expected_errorc                   |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 )Nrb   AccuracyError   r_   zrunning testre   r   r   rg   zrunning minifier)r\   r   r   zrunning repro)r
   r   )r   rL   sysr   r   assertEqual
returncodeassertIsNoneassertInr   r   r   r   assertNotEqualr	   )r   r   r   r   r\   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   N)r)   r*   r+   r;   r<   r:   classmethodr6   rN   r,   rY   r   r   boolr~   r   bytesr   tupler   r   r   intr   r	   r   __classcell__)rD   s   @r   r0   r0   F   s      "I
 
,    	 	 	 	 HLGSSMGS/3GS:=*GS		$	$U	+GSX%)	z**5139<	=, (*"&(( (
  }( 4Z( 
z**5136	7(> 26&&*.&	z**5136	7&*s  3 SV N (**V*V *V d
	*V *V  }*V 
d	"*Vr   r0   )__doc__dataclassesro   rq   r7   r   rJ   r~   r   r;   ry   collections.abcr   typingr   unittest.mockr   r?   torch._dynamotorch._dynamo.test_casetorch._dynamo.trace_rulesr   torch.utils._tracebackr   	dataclassr	   r@   	test_caseTestCaser0   r.   r   r   <module>r      s     	  	 	   
   $      4 A 1 1 1@GVu}}..77 GVr   