o
    Ihd                     @   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)StrobelightCLIFunctionProfiler!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 )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e r   \/var/www/vscode/kcb/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 ),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_urlr   r   r   get_strobelight_url*   sj   
!rU   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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                 C   s   ddl m} t| S )Nr   )CompileContext)torch._guardsrf   strcurrent_trace_id)clsrf   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   strobeclientznstrobeclient not found, cant enable compile time strobelight profiling, seemslike you are not on a FB machine.Tzpt2-profiler/USERUSERNAME )re   max_profile_duration_secstack_max_lenasync_stack_max_lenrun_user_namer)   )r[   r   infor   shutilwhich	_cls_initre   rc   ra   osenvirongetr   r_   )rj   rl   rv   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   r0   strftimery   getpidr   r   r   ru   rU   rj   r   r   r   rx      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   ru   rW   rX   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[   r_   r   errorrk   rZ   rY   ru   r^   r]   rematchprofileprofile_resultrW   rX   r   )	rj   r   r   r   r   r   frame_id
should_runwork_resultr   r   r   profile_compile_time   sH   


z3StrobelightCompileTimeProfiler.profile_compile_time)r	   N) __name__
__module____qualname__rW   int__annotations__rX   rY   rZ   boolr[   ry   rz   r{   r]   r   rh   r   r^   r_   r   ra   rc   floatre   classmethodrk   r   r|   rx   r   r   r   r   r   r   rV   R   sN   
 !rV   )rO   loggingry   r   r   r   socketr   typingr   r   (torch._strobelight.cli_function_profilerr   	getLoggerr   StreamHandlerconsole_handler	Formatter	formattersetFormatter
addHandlersetLevelINFO	propagaterh   r   rU   rV   r   r   r   r   <module>   s*   


(