
    pi1                     8   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mZ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j4                   G d d	             Z G d
 dej8                  j:                  j<                        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OptionalUnion)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e   fdZdefdZdefdZ	y	)
MinifierTestResultminifier_code
repro_codetreturnc                     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)selfr   matchrs       d/opt/services/ai/voice_agent/venv/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_path2   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_moduleA   s    00r   N)
__name__
__module____qualname__str__annotations__r   r   r#   r'   r)    r   r   r   r   %   sH    OS S 
8C= 
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e	   dej                   e   fdZde	d
edeej                   e   ee	ef   f   fdZdddde	d
edee   dee	   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e	   d
edee   dee   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   r7   zMinifierTestBase.setUpClassH   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: )r8   getenvshutilrmtreer;   printr>   close)rD   s    r   tearDownClasszMinifierTestBase.tearDownClass_   sJ    99*C0C7MM#--(9#--IJr   devicebug_typec                     |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 = )r@   rA   rB   codegen_configrC   )r   rP   rQ   s      r   _gen_codegen_fn_patch_codez+MinifierTestBase._gen_codegen_fn_patch_codeg   st    IIII$$& ' (&&( ) *!'5h??_`h_k l 	r   )cwdargsisolater[   c                2   ddl m} |sxt        |      dk\  sJ |       |d   dk(  sJ |       |d   dk(  rt        |      dk(  sJ |       |d   }dg}nGt        |      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}|j5                  |
       |t#        j&                         t
        j                  j7                          	 t
        j                  j                  j9                  |       t
        j                  j                  j9                  |       t;        j<                  ||d|	j?                         jA                  d            S |t!        |      }t;        jB                  |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}t1        j2                  |	       Y ?w xY w# |j5                  |
       |t#        j&                         t
        j                  j7                          w xY w# t
        j                  j                  j9                  |       t
        j                  j                  j9                  |       w xY w)Nr   )normalize_path_separator   python3r    -c   ztorch._dynamozsys.argv__main__)r*   __compile_source__filer   utf-8TF)capture_outputr[   check)"torch._inductor.cpp_builderr_   lenopenreadr@   rA   rB   get_config_copyrC   ioStringIOloggingStreamHandler	getLogger
addHandlerr   r8   getcwdchdirr   r	   exec	Exception	traceback	print_excremoveHandlerresetload_config
subprocessCompletedProcessgetvalueencoderun)r   r\   r]   r[   r_   codefdynamo_configinductor_configstderrlog_handlerlogprev_cwdrcs                 r   _maybe_subprocess_runz&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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}   7J(,AM <AK KJ5"K*
K 4AM (J25J?	:KKK !K40K7 3K44K7 7AL??M AN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 )	Nra   rb   r]   r[   ztest stdout:rh   ztest stderr:z(\S+)minifier_launcher.pyr    )	r   r;   rM   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.pyra   minifyaot_inductorz--no-isolater   zminifier stdout:rh   zminifier stderr:z#Input graph did not fail the tester)assertIsNotNoner   r8   r9   joinrm   rn   
assertTruer:   appendr   rM   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.pyra   r   zrepro stdout:rh   zrepro stderr:)r   r   r8   r9   r   rm   rn   r   r:   r   rM   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@   rA   rB   rY   rC   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 )Nrc   AccuracyError   r`   zrunning testrf   r   r   rh   zrunning minifier)r]   r   r   zrunning repro)r   r   )r   rM   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;   classmethodr7   rO   r-   rZ   r   r   boolr   r   r   bytesr   tupler   r   r   r   intr   r   r   __classcell__)rE   s   @r   r1   r1   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r   r1   ) __doc__dataclassesrp   rr   r8   r   rK   r   r   r<   rz   collections.abcr   typingr   r   r   unittest.mockr   r@   torch._dynamotorch._dynamo.test_casetorch._dynamo.trace_rulesr   torch.utils._tracebackr	   	dataclassr   rA   	test_caseTestCaser1   r/   r   r   <module>r      s     	  	 	   
   $ ' '     4 A 1 1 1>wVu}}..77 wVr   