
    Vhd                        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	m
Z
 d dlmZ  ej                  d      Z ej                         Z ej"                  d      Zej'                  e       ej)                  e       ej+                  ej,                         de_        d	ed
efdZded
efdZ G d d      Zy)    N)datetime)gethostname)AnyOptional)StrobelightCLIFunctionProfiler!strobelight_compile_time_profilerzB%(name)s, line %(lineno)d, %(asctime)s, %(levelname)s: %(message)sFurlreturnc                    | }	 t        j                  d| gdt         j                        }|j                  dk(  r|j                  j                  d      }|S # t        $ r*}t        j                  dt        |             Y d }~|S d }~ww xY w)NfburlT)capture_outputstdinr   zutf-8z)URL shortening failed: %s, using long URL)

subprocessrunDEVNULL
returncodestdoutdecode	Exceptionloggerwarningrepr)r	   	short_urlresultes       X/home/dcms/DCMS/lib/python3.12/site-packages/torch/_strobelight/compile_time_profiler.py	get_fburlr      s    IMcN4z7I7I
 !,,W5I   MBDGLLMs   AA 	B!BB
identifierc                    i dg dddg gdg gdddgd	d
dddd|  dgdggdg dddg dddddddddg dddg dd d!d"d#d#d$gg dd%dd&d'd(d)
}d*}d+}|t        j                  |      z   |z   }t        |      S ),NaggregateListaggregation_fieldasync_stack_completeb_constraintsc_constraintscolsnamespace_idnamespace_process_idcomparenoneconstraintssample_tagsallz["z"])columnopvaluederivedColsendnowenumCols
filterModeDEFAULThideEmptyColumnsfalseignoreGroupByInComparisonis_timeseries
mappedColsmetriccount	modifiersweighttruepy_async_stackedge0)dimr.   paramanchor1z-30 dayszAmerica/Los_Angelesi'  )
order
order_descparam_dimensionspurposesreturn_remaindersamplingRatioshould_pivotstarttimezonetopz`https://www.internalfb.com/intern/scuba/query/?dataset=pyperf_experimental/on_demand&drillstate=z8&view=GraphProfilerView&&normalized=1726332703&pool=uber)jsondumpsr   )r   
scuba_jsonscuba_url_prefixscuba_url_sufflong_urls        r   get_strobelight_urlrW   *   s_     3  	"  	"	 
 	!78  	6  	%U:,b>Q=RST
  	r  	u  	B  	i  	G  	$W   	! " 	b# $ 	'% & 	R' ( $FSCP
 #)? JB zON$**Z"88>IHX    c                   J   e Zd ZU dZeed<   dZeed<   dZeed<   dZe	ed<   dZ
e	ed<   ej                  j                  d      Zee   ed	<   d
Zee   ed<   d
Zee   ed<   d
Zee   ed<    eej                  j                  dd            Zeed<    eej                  j                  dd            Zeed<    e eej                  j                  dd                  Zeed<   edefd       Zeefdedd
fd       Zed"d       Zed"d       Zedededed edef
d!       Zy
)#StrobelightCompileTimeProfilerr   success_profile_countfailed_profile_countignored_profile_runsFinside_profile_compile_timeenabled COMPILE_STROBELIGHT_FRAME_FILTERframe_id_filterNr   current_phaseprofiler$COMPILE_STROBELIGHT_MAX_STACK_LENGTHi  max_stack_length$COMPILE_STROBELIGHT_MAX_PROFILE_TIMEi  max_profile_timeCOMPILE_STROBELIGHT_SAMPLE_RATEg    cAsample_eachr
   c                 @    ddl m} t        |j                               S )Nr   )CompileContext)torch._guardsrk   strcurrent_trace_id)clsrk   s     r   	get_framez(StrobelightCompileTimeProfiler.get_frameo   s    0^44677rX   profiler_classc                    | j                   rt        j                  d       y t        j                  d       |t        u r+dd l}|j                  d      st        j                  d       y d| _         | j                           || j                  | j                  | j                  | j                  dt        j                  j                  dt        j                  j                  d	d
            z   | j                  h      | _        y )Nz2compile time strobelight profiling already enabledz*compile time strobelight profiling enabledr   strobeclientznstrobeclient not found, cant enable compile time strobelight profiling, seemslike you are not on a FB machine.Tzpt2-profiler/USERUSERNAME )ri   max_profile_duration_secstack_max_lenasync_stack_max_lenrun_user_namer+   )r_   r   infor   shutilwhich	_cls_initri   rg   re   osenvirongetr   rc   )ro   rq   r|   s      r   enablez%StrobelightCompileTimeProfiler.enableu   s    ;;KKLM@A;;<</8 
 &%(%9%9.. # 4 4)jjnnVRZZ^^J%CDE(
rX   c                 >   dj                  t        j                         j                  d      t	        j
                         t                     | _        t        j                  d| j                         t        j                  dt        | j                               y )Nz{date}{pid}{hostname}z%Y-%m-%d-%H:%M:%S)datepidhostnamez%Unique sample tag for this run is: %sz?URL to access the strobelight profile at the end of the run: %s)formatr   r2   strftimer   getpidr   r   r   r{   rW   ro   s    r   r~   z(StrobelightCompileTimeProfiler._cls_init   sl    077(()<=		 ] 8 
 	;S^^LM/	
rX   c                 t    t         j                  d| j                  | j                  | j                  z          y )NzG%s strobelight success runs out of %s non-recursive compilation events.)r   r{   r[   r\   r   s    r   
_log_statsz)StrobelightCompileTimeProfiler._log_stats   s.    U%%%%(@(@@	
rX   func
phase_nameargskwargsc                 0   dt         ffd}| j                  s |       S | j                  t        j	                  d       y | j                         }| j                  r?| xj                  dz  c_        t        j                  d||| j                  |        |       S | j                  Lt        j                  | j                  |      d u}|s(t        j                  d|| j                          |       S d| _        || _	        t        j                  d|        | j                  j                  gi }| j                  j                  | xj                  dz  c_        n| xj                   dz  c_        | j#                          d	| _        |S )
Nr
   c                        i S )N )r   r   r   s   r   skipzAStrobelightCompileTimeProfiler.profile_compile_time.<locals>.skip   s    (((rX   zprofiler is not set   z~profile_compile_time is requested for phase: %s, frame %s, while already in running phase: %s,frame %s, recursive call ignoredz7profiling frame %s is skipped due to frame_id_filter %sTzprofiling frame %sF)r   r_   rc   r   errorrp   r^   r]   r{   rb   ra   rematchprofileprofile_resultr[   r\   r   )	ro   r   r   r   r   r   frame_id
should_runwork_results	    ` ``    r   profile_compile_timez3StrobelightCompileTimeProfiler.profile_compile_time   sa   	)c 	) {{6M<<LL./==?**$$)$KK3!! 6M*#"5"5x@LJM''
 v*.'&((3*cll**4A$A&A<<&&2%%*%$$)$*/'rX   )r
   N) __name__
__module____qualname__r[   int__annotations__r\   r]   r^   boolr_   r   r   r   ra   r   rm   r   rb   rc   r   re   rg   floatri   classmethodrp   r   r   r~   r   r   r   rX   r   rZ   rZ   R   s   !"3" !#! !#!(--GT &(ZZ^^4V%WOXc]W !%J$#'M8C='"Hhsm"


=sCc   


=wGc  bjjnn>DEK  8# 8 8
 *H 
C 
T 
 
B 
 
 
 
 11$'1031?B1	1 1rX   rZ   )rQ   loggingr   r   r   r   socketr   typingr   r   (torch._strobelight.cli_function_profilerr   	getLoggerr   StreamHandlerconsole_handler	Formatter	formattersetFormatter
addHandlersetLevelINFO	propagaterm   r   rW   rZ   r   rX   r   <module>r      s      	 	      S 
		>	?'''')GH	   Y '   / "   3 3 %C %C %PN NrX   