
    9jO                        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  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)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       h/media/conek/DATA/Code/OCR/venv/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_urlrV   *   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d	z  ed
<   d	Zed	z  ed<   d	Zed	z  ed<   d	Zed	z  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_FILTERNframe_id_filterr   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._guardsrj   strcurrent_trace_id)clsrj   s     r   	get_framez(StrobelightCompileTimeProfiler.get_frameo   s    0^44677rW   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   strobeclientzostrobeclient not found, can't enable compile time strobelight profiling, seemslike you are not on a FB machine.Tzpt2-profiler/USERUSERNAME )rh   max_profile_duration_secstack_max_lenasync_stack_max_lenrun_user_namer*   )r^   r   infor   shutilwhich	_cls_initrh   rf   rd   osenvirongetr   rb   )rn   rp   r{   s      r   enablez%StrobelightCompileTimeProfiler.enableu   s    ;;KKLM@A;;<</8 
 &%(%9%9.. # 4 4)jjnnVRZZ^^J%CDE(
rW   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   r1   strftimer~   getpidr   r   r   rz   rV   rn   s    r   r}   z(StrobelightCompileTimeProfiler._cls_init   sl    077(()<=		 ] 8 
 	;S^^LM/	
rW   c                 t    t         j                  d| j                  | j                  | j                  z          y )NzG%s strobelight success runs out of %s non-recursive compilation events.)r   rz   rZ   r[   r   s    r   
_log_statsz)StrobelightCompileTimeProfiler._log_stats   s.    U%%%%(@(@@	
rW   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    (((rW   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^   rb   r   errorro   r]   r\   rz   ra   r`   rematchprofileprofile_resultrZ   r[   r   )	rn   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%%*%$$)$*/'rW   )r	   N)__name__
__module____qualname__rZ   int__annotations__r[   r\   r]   boolr^   r~   r   r   r`   rl   r   ra   rb   r   rd   rf   floatrh   classmethodro   r   r   r}   r   r   r   rW   r   rY   rY   R   s   !"3" !#! !#!(--GT #%**..1S"TOS4ZT "Jd
! $M3:$HcDj


=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rW   rY   )rP   loggingr~   r   r   r   socketr   typingr   (torch._strobelight.cli_function_profilerr   	getLoggerr   StreamHandlerconsole_handler	Formatter	formattersetFormatter
addHandlersetLevelINFO	propagaterl   r   rV   rY   r   rW   r   <module>r      s      	 	     S 
		>	?'''')GH	   Y '   / "   3 3 %C %C %PN NrW   