o
    ίi/                     @   s   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Zd dl	Z	d dl
Zd dlmZ d dl
mZmZ d dlmZ eG dd dZe Zd	d
 ZG dd deZG dd deZe jdefddZdS )    N)	dataclass)Path)profile)MemSnapshotsProfilerPyTorchProfiler)get_is_masterc                   @   sV   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
< dZe	ed< dS )ProfilerArgsFrun	profilingtrace_folderd   
mem_warmup   	mem_stepsf   profile_warmupprofile_stepsN)__name__
__module____qualname__r	   bool__annotations__r   strr   intr   r   r    r   r   B/home/ubuntu/.local/lib/python3.10/site-packages/core/profiling.pyr      s   
 r   c                 C   sb  dd l }dd l}dd l}tj|j}i }dt| v r || nt| } ttj	|ddd}|
 }W d    n1 s>w   Y  ttj	|ddd}|
 |d< W d    n1 s_w   Y  | }	|	
 }
t|
tru|
d}
|
dd	|d
< W d    n1 sw   Y  t|ddd}|||| W d    d S 1 sw   Y  d S )Nr   z.gzzhtml/trace_viewer_embedder.htmlzutf-8)encodingzhtml/trace_viewer_full.htmltrace_viewer_fullz	</script>z
<\/script>	json_datazw+)gzipstring	viztracerospathdirname__file__r   openjoinread
isinstancebytesdecodereplacewriteTemplate
substitute)	json_file	html_filer   r    r!   rootsubftmpljcontentoutput_filer   r   r   perfetto_to_html    s0   


"r9   c                       sH   e Zd Zd
ddZdef fddZdejjjddf fdd	Z  Z	S )PyTorchProfilerWandbreturnNc                 C   s.   || _ d| _tjj| jdddd| jd| _d S )Nr   TF)on_trace_readyprofile_memoryrecord_shapes
with_stack
with_flops
activities)main_profiler	num_stepstorchprofilerr   	_on_trace
ACTIVITIESpytorch_profiler)selfrB   r   r   r   __init__9   s   zPyTorchProfilerWandb.__init__profc                    s$   t d t | t d d S )NzBegin analyze tracezEnd analyze trace)loggerinfosuper_analyze_trace)rI   rK   	__class__r   r   rO   H   s   
z#PyTorchProfilerWandb._analyze_tracec                    sp   t  | t r4tjd ur6tt| jj	dd }t
|dd}t|| tdt|i d S d S d S )Nz"profile_CPU_CUDA*/*.pt.trace.json*r   z.jsonz.htmlprofile_trace)rN   rF   r   wandbr	   listr   rB   
output_dirglobr   r,   r9   logHtml)rI   rK   filename	html_pathrP   r   r   rF   M   s   
zPyTorchProfilerWandb._on_trace)r;   N)
r   r   r   rJ   r   rO   rD   rE   rF   __classcell__r   r   rP   r   r:   8   s    
$r:   c                       s   e Zd Z fddZ  ZS )MemSnapshotsProfilerWandbc                    sb   t  ||| t r-tjd ur/tt| jj	dd }t
dtjt|ddi d S d S d S )Nzmemory_trace_plot/*.htmlr   memory_traceF)inject)rN   __exit__r   rS   r	   rT   r   rB   rU   rV   rW   rX   r&   )rI   exc_typeexc_valexc_tbrY   rP   r   r   r_   [   s    z"MemSnapshotsProfilerWandb.__exit__)r   r   r   r_   r[   r   r   rP   r   r\   Z   s    r\   configc                 c   s    |j r]tj| |j}td|  t r"tj|s"t	| t
j r,t
j  tjj||t|j|j|j ft|j|j|j fgd}|V  W d    d S 1 sVw   Y  d S t }d V  d S )Nz+Profiling active.  Traces will be saved at )rU   moduleschedule)r	   r"   r#   r'   r   rL   rM   r   existsmakedirsrD   distributedis_initializedbarrierxformersrE   r   r\   r   r   r:   r   r   
contextlibnullcontext)dump_dirrd   rc   	trace_dirrE   torch_profilerr   r   r   maybe_run_profilerd   s4   




"
rq   )rl   loggingr"   dataclassesr   pathlibr   torch.distributedrD   rS   xformers.profilerrk   torch.profiler.profilerr   r   r   core.distributedr   r   	getLoggerrL   r9   r:   r\   contextmanagerrq   r   r   r   r   <module>   s&   	"
