o
    Zhd                     @   s   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dZe ZedZee ee eej de_d	ed
efddZded
efddZG dd dZdS )    N)datetime)gethostname)AnyOptional)StrobelightCLIFunctionProfilerZ!strobelight_compile_time_profilerzB%(name)s, line %(lineno)d, %(asctime)s, %(levelname)s: %(message)sFurlreturnc              
   C   st   | }zt jd| gdt jd}|jdkr|jd}W |S W |S  ty9 } ztdt	| W Y d }~|S d }~ww )NZ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   Z	short_urlresulte r   W/var/www/auris/lib/python3.10/site-packages/torch/_strobelight/compile_time_profiler.py	get_fburl   s   
r   
identifierc                 C   s   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 | | }t|S ),NZaggregateListZaggregation_fieldZasync_stack_completeZb_constraintsZc_constraintscolsZnamespace_idZnamespace_process_idcomparenoneconstraintssample_tagsallz["z"])columnopvalueZderivedColsendnowZenumColsZ
filterModeDEFAULTZhideEmptyColumnsfalseZignoreGroupByInComparisonZis_timeseriesZ
mappedColsZmetriccount	modifiersweighttrueZpy_async_stackedge0)dimr"   paramanchor1z-30 dayszAmerica/Los_Angelesi'  )
orderZ
order_descZparam_dimensionsZpurposesZreturn_remainderZsamplingRatioZ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   Z
scuba_jsonZscuba_url_prefixZscuba_url_suffZlong_urlr   r   r   get_strobelight_url*   sj   
!r8   c                   @   sN  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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ddZeed< eejddZeed< eeejddZeed< edefddZeefdedd
fddZed'ddZed'dd Zed!ed"ed#ed$edef
d%d&Zd
S )(StrobelightCompileTimeProfilerr   success_profile_countfailed_profile_countignored_profile_runsFinside_profile_compile_timeenabledZ COMPILE_STROBELIGHT_FRAME_FILTERframe_id_filterNr   current_phaseprofilerZ$COMPILE_STROBELIGHT_MAX_STACK_LENGTHi  max_stack_lengthZ$COMPILE_STROBELIGHT_MAX_PROFILE_TIMEi  max_profile_timeZCOMPILE_STROBELIGHT_SAMPLE_RATEg    cAsample_eachr   c                 C   s   ddl m} t| S )Nr   )CompileContext)Ztorch._guardsrE   strZcurrent_trace_id)clsrE   r   r   r   	get_frameo   s   z(StrobelightCompileTimeProfiler.get_frameprofiler_classc                 C   s   | j r
td d S td |tu r#dd l}|ds#td d S d| _ |   || j| j| j	| j	dt
jdt
jd	d
 | jhd| _d S )Nz2compile time strobelight profiling already enabledz*compile time strobelight profiling enabledr   Zstrobeclientznstrobeclient not found, cant enable compile time strobelight profiling, seemslike you are not on a FB machine.Tzpt2-profiler/USERUSERNAME )rD   Zmax_profile_duration_secZstack_max_lenZasync_stack_max_lenZrun_user_namer   )r>   r   infor   shutilwhich	_cls_initrD   rC   rB   osenvirongetr   rA   )rG   rI   rN   r   r   r   enableu   s.   


z%StrobelightCompileTimeProfiler.enablec                 C   sF   dj t dt t d| _t	d| j t	dt
| j d S )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   r%   strftimerQ   getpidr   r   r   rM   r8   rG   r   r   r   rP      s   z(StrobelightCompileTimeProfiler._cls_initc                 C   s   t d| j| j| j  d S )NzG%s strobelight success runs out of %s non-recursive compilation events.)r   rM   r:   r;   r[   r   r   r   
_log_stats   s
   
z)StrobelightCompileTimeProfiler._log_statsfunc
phase_nameargskwargsc           	         s  dt f fdd}| js| S | jd u rtd d S |  }| jr8|  jd7  _td||| j	| | S | j
d urSt| j
|d u}|sStd|| j
 | S d| _|| _	td	| | jjg R i }| jjd urz|  jd7  _n|  jd7  _|   d
| _|S )Nr   c                      s    i S )Nr   r   r_   r]   r`   r   r   skip   s   zAStrobelightCompileTimeProfiler.profile_compile_time.<locals>.skipz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>   rA   r   errorrH   r=   r<   rM   r@   r?   rematchZprofileZprofile_resultr:   r;   r\   )	rG   r]   r^   r_   r`   rb   Zframe_idZ
should_runZwork_resultr   ra   r   profile_compile_time   sH   


z3StrobelightCompileTimeProfiler.profile_compile_time)r   N) __name__
__module____qualname__r:   int__annotations__r;   r<   r=   boolr>   rQ   rR   rS   r?   r   rF   r   r@   rA   r   rB   rC   floatrD   classmethodrH   r   rT   rP   r\   rg   r   r   r   r   r9   R   sN   
 !r9   )r6   loggingrQ   re   r   r   socketr   typingr   r   Z(torch._strobelight.cli_function_profilerr   	getLoggerr   StreamHandlerZconsole_handler	Formatter	formattersetFormatter
addHandlersetLevelINFO	propagaterF   r   r8   r9   r   r   r   r   <module>   s*   


(