
    Vh                      V   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j2                  j4                  j6                        Zd Zdededee ejB                     e
g ed   f   f   fdZ"y)    N)AbstractContextManager)Callable)
LazyString)configc               #   R  K   t         j                  j                  j                         } t         j                  j                  j	                  t         j                  j                  j                                	 d  t         j                  j                  j	                  |        t         j                  j                  j                          y # t         j                  j                  j	                  |        t         j                  j                  j                          w xY wwN)torch_logging	_internal_get_log_state_set_log_stateLogState
_init_logs)
prev_states    U/home/dcms/DCMS/lib/python3.12/site-packages/torch/testing/_internal/logging_utils.pypreserve_log_stater      s     ))88:J	NN++ENN,D,D,M,M,OP.  //
;  ++- 	  //
;  ++-s    A8D';C ?AD'AD$$D'c                 F   t        j                         }t        j                  j                  j                  t        j                  d| i      }|j                  t                      |j                  |       t        j                  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!      sq    %%'J]]((--bjj<:RSN/12^,	NN'')    c                      t        j                         }|j                  t                      t	        j
                  j                  di |  |S )N )r   r   r   r   r	   r
   set_logs)kwargsr   s     r   log_apir'   "   s=    %%'J/12	NN%f%r"   c                  n   ddddg fd}| j                         D ]  \  }}t        |t              rj                  |       (t        |t              r
 |||       Bt        |t
              r'|dk(  r"|j                         D ]  \  }} |||        yt        d       dj                        S )	N+ -)
      (   c                     t        | t              r,t        |t              r|v rj                  |   | z          y 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.   sD    dC Zs%;IY@YOO,U3d:;8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H: \\^ 	:	cc4 OOD!S!4%T"ty'8'*yy{ 4#e|U34 899	: 88Hr"   c                        fd}|S )Nc                 J     t        j                  ddi       fd       }|S )Nfx_graph_cacheFc                 l   t         j                  j                          g }t              dk(  r$| j	                  |      5   | |       d d d        n@t        t        di       5  | j	                  |      5   | |       d d d        d d d        t         j                  j                          |j                          t        di 5  | j	                  |      5   | |       d d d        d d d        y # 1 sw Y   oxY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   6xY w# 1 sw Y   y xY w)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    MM!G6{a**73 &tW%& & ""4">v">? &AVAVW^A_ &tW%& & MM!MMO"6" "D$9$9'$B "4!" " "& && & & &" " " "sT   
C:(D:
DDD*
D)D*:DD	DDD'	#D**D3)inductor_configr   )rM   rN   r&   s   ` r   wrapperz"make_logging_test.<locals>.wrapperO   s-    			 0%8	9	" 
:	"& r"   r$   )r&   rP   s   ` r   make_logging_testrQ   N   s    . Nr"   c                       fd}|S )Nc                       fd}|S )Nc                     t         j                  j                          g }t              5  | j	                  |      5   | |       d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY wr   )r	   rF   rG   r!   rI   )rK   rL   rM   r   s     r   rN   z4make_settings_test.<locals>.wrapper.<locals>.test_fnj   sa    MM!Gh' ")>)>w)G "4!" " " " " "s"   A&
A	A&A#	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                   J     e Zd Ze fd       Zed        Zd Zd Zd Z xZ	S )LoggingTestCasec                    t         |           | j                  j                  t        j
                  j                  j                  t        j                  ddi             | j                  j                  t        j
                  j                  dd             | j                  j                  t        j
                  j                  dd             y )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    %%MM$$RZZ2BB1GH	
 	%%MM FM	
 	%%MM >F	
r"   c                     | j                   j                          t        j                  j                  j
                  j                          t        j                  j                          y r   )r\   closer	   r
   r   	log_staterJ   r   )r]   s    r   tearDownClasszLoggingTestCase.tearDownClass   s>      **002!!#r"   c                 ,    t        fd|D              S )Nc              3   B   K   | ]  }|j                         v   y wr   )
getMessage).0rms     r   	<genexpr>z,LoggingTestCase.hasRecord.<locals>.<genexpr>   s     811&8s   )any)rK   rL   rh   s     `r   	hasRecordzLoggingTestCase.hasRecord   s    8888r"   c           	          d D ]8  |j                         v s| j                  t        fd             : | j                  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    "=fXU1#WU\T] ^ r"   )msgzdid not find record with rn   )re   assertIsNoner   fail)rK   rL   rh   rg   ro   s    ` @@r   	getRecordzLoggingTestCase.getRecord   su     
	A ALLN"!!"^ "  
	 >II1!GG9EFr"   c           	        	 t        j                         }fdt        j                  j                  j
                  j                         D ]  }t        j                  |      }t        |j                        }| j                  |dd       | j                  |dd       |j                  D ]N  }|j                  		fd}|j                  t        j                   j"                  j%                  |d|             P  |S )Nc                 (    j                  |        y 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                 &     |         |        y r   r$   )ro   rx   old_emits    r   new_emitz2LoggingTestCase._handler_watcher.<locals>.new_emit   s    V$"6*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1>>MMO 	I&&y1Fv/L  y |Q0ef!?? 	"<<+ ((MM''..wI		, r"   )
__name__
__module____qualname__classmethodr[   rb   rk   rt   rI   __classcell__)r^   s   @r   rW   rW   u   s8    

 

 $ $
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                  j                        } | j                         d  | j	                         y #  j	                         w xY wwr   )r	   r
   getArtifactLogger
addHandlerremoveHandler)r   r   
log_optionmodules    r   tmp_redirect_logsz)logs_to_string.<locals>.tmp_redirect_logs   sO     	*^^55fjIFg&  )F  )s   A"5A A"AA"c                  J    t              } | j                                 | S r   )r!   r   )r   r   r   s    r   ctx_managerz#logs_to_string.<locals>.ctx_manager   s%    !*-
  !2!45r"   )ioStringIOr   StreamHandlerr   contextmanager)r   r   
log_streamr   r   r   s   ``  @@r   logs_to_stringr      sJ     J##:6G* *
 {""r"   r   log_optionsreturnc                 (    t        t                    D cg c]  }t        j                          }}|D cg c]  }t	        j
                  |       c}t        j                   fd       dt        d   ffd}||fS c c}w c c}w )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.
    r   c               3   d  K   D  cg c]"  } t         j                  j                  |       $ }} 	 t        |      D ]  \  }}|j	                  |        d  t        |      D ]  \  }}|j                  |        y c c} w # t        |      D ]  \  }}|j                  |        w xY wwr   )r	   r
   r   zipr   r   )optionloggersr   r   r   r   r   s       r   r   z2multiple_logs_to_string.<locals>.tmp_redirect_logs   s     R]^5>>33FFC^^	.#&w#9 +!!'*+#&w#9 .$$W-. _ $'w#9 .$$W-.s&   B0'BB0)B +B0'B--B0r   Nc                  h    t        dj                              } | j                                 | S )Nz, )r!   r>   r   )r   r   r   s    r   r   z,multiple_logs_to_string.<locals>.ctx_manager   s.    !$))K"89
  !2!45r"   )	rangerH   r   r   r   r   r   r   r   )r   r   _log_streamsr   r   r   r   s   ``    @@r   multiple_logs_to_stringr      s     +0K0@*ABQ2;;=BKBKVWZ%%Z8WH. ./5 
 ##' CWs
   B
B)#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  	. .J4Hemm--66 HV#4$C $s $uT"++EVX`ace{  }A  fB  bB  YC  FC  @D $r"   