
    `i                      J   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mZ d dlZd dlZej        d             Zd Zd Zd	 Zd
 Zd Z G d dej        j        j                  Zd Zdededee ej!                 e
g ed         f         f         fdZ"dS )    N)AbstractContextManager)Callable)
LazyString)configc               #   
  K   t           j        j                                        } t           j        j                            t           j        j                                                   	 d V  t           j        j                            |            t           j        j                                         d S # t           j        j                            |            t           j        j                                         w xY wN)torch_logging	_internal_get_log_state_set_log_stateLogState
_init_logs)
prev_states    y/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/torch/testing/_internal/logging_utils.pypreserve_log_stater      s      )88::J	N++EN,D,M,M,O,OPPP. //
;;; ++----- 	 //
;;; ++----s   ,B9 9A	Dc                 @   t          j                    }t          j        j                            t          j        d| i          }|                    t                                 |                    |           t          j        j                                         |S )N
TORCH_LOGS)
contextlib	ExitStackunittestmockpatchdictosenvironenter_contextr   r	   r
   r   r   )settings
exit_stacksettings_patchs      r   log_settingsr!      s}    %''J](--bj<:RSSN/11222^,,,	N'')))    c                      t          j                    }|                    t                                 t	          j        j        di |  |S )N )r   r   r   r   r	   r
   set_logs)kwargsr   s     r   log_apir'   "   sJ    %''J/11222	N%%f%%%r"   c                     ddddg fd}|                                  D ]\  }}t          |t                    r                    |           0t          |t                    r |||           Rt          |t
                    r-|dk    r'|                                 D ]\  }} |||           t          d          d                              S )	N+ -)
      (   c                     t          | t                    r9t          |t                    r$|v r                     |         | z              d S t	          d          )NInvalid value for setting)
isinstancestrintappend
ValueError)namelevelINT_TO_VERBOSITYr   s     r   append_settingz*kwargs_to_settings.<locals>.append_setting.   sb    dC   	:Zs%;%; 	:IY@Y@YOO,U3d:;;;F8999r"   modulesr0   ,)itemsr1   boolr4   r3   r   r5   join)r&   r9   r6   valmodule_qnamer7   r8   r   s         @@r   kwargs_to_settingsrA   )   s   RS11H: : : : : : \\^^ 	: 	:	cc4   	:OOD!!!!S!! 	:N4%%%%T"" 	:ty'8'8'*yy{{ 4 4#e|U33334 899988Hr"   c                        fd}|S )Nc                 N     t          j        ddi           fd            }|S )Nfx_graph_cacheFc                    t           j                                         g }t                    dk    r:|                     |          5   | |           d d d            n# 1 swxY w Y   njt          t          di           5  |                     |          5   | |           d d d            n# 1 swxY w Y   d d d            n# 1 swxY w Y   t           j                                         |                                 t          di 5  |                     |          5   | |           d d d            n# 1 swxY w Y   d d d            d S # 1 swxY w Y   d S )Nr   r$   )	r	   _dynamoresetlen_handler_watcherr!   rA   clearr'   )selfrecordsfnr&   s     r   test_fnz3make_logging_test.<locals>.wrapper.<locals>.test_fnP   s    M!!!G6{{a**733 & &BtW%%%& & & & & & & & & & & & & & & ""4">">v">">?? & &AVAVW^A_A_ & &BtW%%%& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & M!!!MMOOO""6"" " "D$9$9'$B$B " "4!!!" " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " "s~   
A##A'*A'CB7+C7B;	;C>B;	?CCCE-E:EE
	
EE
	EE"%E")inductor_configr   )rM   rN   r&   s   ` r   wrapperz"make_logging_test.<locals>.wrapperO   sE    		 0%8	9	9	" 	" 	" 	" 	" 
:	9	"& r"   r$   )r&   rP   s   ` r   make_logging_testrQ   N   s#        . Nr"   c                       fd}|S )Nc                       fd}|S )Nc                 
   t           j                                         g }t                    5  |                     |          5   | |           d d d            n# 1 swxY w Y   d d d            d S # 1 swxY w Y   d S r   )r	   rF   rG   r!   rI   )rK   rL   rM   r   s     r   rN   z4make_settings_test.<locals>.wrapper.<locals>.test_fnj   s   M!!!Gh'' " ")>)>w)G)G " "4!!!" " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " "s5   A8A A8 A$	$A8'A$	(A88A<?A<r$   )rM   rN   r   s   ` r   rP   z#make_settings_test.<locals>.wrapperi   s)    	" 	" 	" 	" 	" 	" r"   r$   )r   rP   s   ` r   make_settings_testrU   h   s#         Nr"   c                   V     e Zd Ze fd            Zed             Zd Zd Zd Z xZ	S )LoggingTestCasec                    t                                                       | j                            t          j        j                            t          j	        ddi                     | j                            t          j                            dd                     | j                            t          j                            dd                     d S )N___LOG_TESTINGr*   z$torch._dynamo.config.suppress_errorsTztorch._dynamo.config.verboseF)
super
setUpClass_exit_stackr   r   r   r   r   r   r   )cls	__class__s    r   r[   zLoggingTestCase.setUpClassv   s    %%M$$RZ2BB1GHH	
 	
 	
 	%%M FMM	
 	
 	
 	%%M >FF	
 	
 	
 	
 	
r"   c                     | j                                          t          j        j        j                                         t          j                                         d S r   )r\   closer	   r
   r   	log_staterJ   r   )r]   s    r   tearDownClasszLoggingTestCase.tearDownClass   sJ     *00222!!#####r"   c                 :    t          fd|D                       S )Nc              3   D   K   | ]}|                                 v V  d S r   )
getMessage).0rms     r   	<genexpr>z,LoggingTestCase.hasRecord.<locals>.<genexpr>   s0      8811&888888r"   )any)rK   rL   rh   s     `r   	hasRecordzLoggingTestCase.hasRecord   s&    8888888888r"   c           	          d D ]C|                                 v r+|                     t          fd                     D|                     d| d            S )Nc                      d d  d S )Nzmultiple matching records: z and  among r$   )rg   recordrL   s   r   <lambda>z+LoggingTestCase.getRecord.<locals>.<lambda>   s!     ^f ^ ^1 ^ ^U\ ^ ^ r"   )msgzdid not find record with rn   )re   assertIsNoner   fail)rK   rL   rh   rg   ro   s    ` @@r   	getRecordzLoggingTestCase.getRecord   s     
	 
	A ALLNN""!!"^^^^^^  "    >IIE!EEGEEFFFr"   c           	        	 t          j                    }fdt          j        j        j                                        D ]}t          j        |          }t          |j
                  }|                     |dd           |                     |dd           |j
        D ]H}|j        		fd}|                    t          j        j                            |d|                     I|S )Nc                 2                         |            d S r   )r4   )ro   record_lists    r   emit_post_hookz8LoggingTestCase._handler_watcher.<locals>.emit_post_hook   s    v&&&&&r"      zgAll pt2 loggers should only have at most two handlers (debug artifacts and messages above debug level).r   z3All pt2 loggers should have more than zero handlersc                 4     |             |            d S r   r$   )ro   rx   old_emits    r   new_emitz2LoggingTestCase._handler_watcher.<locals>.new_emit   s)    HV$$$"N6*****r"   emit)r   r   r	   r
   r   log_registryget_log_qnameslogging	getLoggerrH   handlersassertLessEqualassertGreaterr}   r   r   r   r   object)
rK   rw   r   	log_qnameloggernum_handlershandlerr|   rx   r{   s
    `      @@r   rI   z LoggingTestCase._handler_watcher   s(   )++
	' 	' 	' 	' 	'
 1>MMOO 	 	I&y11Fv//L  y   |Q0efff!? 	 	"<+ + + + + + ((M'..wII   	 r"   )
__name__
__module____qualname__classmethodr[   rb   rk   rt   rI   __classcell__)r^   s   @r   rW   rW   u   s        

 

 

 

 [

 $ $ [$
9 9 9  &      r"   rW   c                      t          j                    }t          j        |          t          j         fd            fd}||fS )zExample:
    logs_to_string("torch._inductor.compile_fx", "post_grad_graphs")
    returns the output of TORCH_LOGS="post_grad_graphs" from the
    torch._inductor.compile_fx module.
    streamc               3      K   	 t           j                                      } |                                d V  |                                d S # |                                w xY wr   )r	   r
   getArtifactLogger
addHandlerremoveHandler)r   r   
log_optionmodules    r   tmp_redirect_logsz)logs_to_string.<locals>.tmp_redirect_logs   st      	*^55fjIIFg&&&EEE  )))))F  ))))s   9A A,c                  `    t                    } |                                             | S r   )r!   r   )r   r   r   s    r   ctx_managerz#logs_to_string.<locals>.ctx_manager   s4    !*--
  !2!2!4!4555r"   )ioStringIOr   StreamHandlerr   contextmanager)r   r   
log_streamr   r   r   s   ``  @@r   logs_to_stringr      s     J#:666G* * * * * * *     
 {""r"   r   log_optionsreturnc                      d t          t                              D             }d |D             t          j         fd            dt          d         ffd}||fS )zExample:
    multiple_logs_to_string("torch._inductor.compile_fx", "pre_grad_graphs", "post_grad_graphs")
    returns the output of TORCH_LOGS="pre_graph_graphs, post_grad_graphs" from the
    torch._inductor.compile_fx module.
    c                 4    g | ]}t          j                    S r$   )r   r   )rf   _s     r   
<listcomp>z+multiple_logs_to_string.<locals>.<listcomp>   s    BBBQ2;==BBBr"   c                 8    g | ]}t          j        |           S )r   )r   r   )rf   r   s     r   r   z+multiple_logs_to_string.<locals>.<listcomp>   s&    WWWZ%Z888WWWr"   c               3   >  K   fdD             } 	 t          |           D ]\  }}|                    |           d V  t          |           D ]\  }}|                    |           d S # t          |           D ]\  }}|                    |           w xY w)Nc                 P    g | ]"}t           j                            |          #S r$   )r	   r
   r   )rf   optionr   s     r   r   zFmultiple_logs_to_string.<locals>.tmp_redirect_logs.<locals>.<listcomp>   s+    ^^^5>33FFCC^^^r"   )zipr   r   )loggersr   r   r   r   r   s      r   r   z2multiple_logs_to_string.<locals>.tmp_redirect_logs   s      ^^^^R]^^^	.#&w#9#9 + +!!'****EEE#&w#9#9 . .$$W----. .3w#9#9 . .$$W----.s   /A/ /-Br   Nc                      t          d                                        } |                                             | S )Nz, )r!   r>   r   )r   r   r   s    r   r   z,multiple_logs_to_string.<locals>.ctx_manager   s@    !$))K"8"899
  !2!2!4!4555r"   )rangerH   r   r   r   )r   r   log_streamsr   r   r   s   ``  @@r   multiple_logs_to_stringr      s     CB%K0@0@*A*ABBBKWW;WWWH. . . . . . ./5       
 ##r"   )#torch._dynamo.test_caser	   unittest.mockr   r   r   torch._loggingtorch._logging._internalr   typingr   torch._dynamo.utilsr   torch._inductorr   rO   r   r   r   r   r!   r'   rA   rQ   rU   rF   	test_caseTestCaserW   r   r2   tuplelistr   r   r$   r"   r   <module>r      s           				             - - - - - -       * * * * * * 5 5 5 5 5 5  				. . .      J  4  H H H H Hem-6 H H HV# # #4$C $s $uT"+EVX`ace{  }A  fB  bB  YC  FC  @D $ $ $ $ $ $r"   