o
    i/                     @   s   d dl Z d dlZd dlmZ d dlZd dlZd dlmZmZm	Z	m
Z
mZ d dlmZmZmZmZmZmZ dgZedddG dd dZd	d
 ZdS )    N)
deprecated)_disable_profiler_legacy_enable_profiler_legacy
DeviceTypeProfilerConfigProfilerState)_filter_name_filter_stack_entry_rewrite_name	EventListFunctionEventMEMORY_EVENT_NAMEprofilez`torch.autograd.profiler_legacy.profile` is deprecated and will be removed in a future release. Please use `torch.profiler` instead.)categoryc                   @   s   e Zd ZdZ	d*dddddddddZdd Zd	d
 Zdd Zdd Zdd Z	dd Z
dd Z							d+ddZejje_dd Zejje_d,ded efd!d"Zd-d$d%Zejje_d&d' Zejje_ed(d) ZdS ).r   z'DEPRECATED: use torch.profiler instead.TF)use_cudarecord_shapes
with_flopsprofile_memory
with_stackwith_modulesc                C   s   || _ | j sd S || _d | _d| _|| _|| _|  j| jO  _|| _|| _|| _| jr:t	j
 s:tjddd d| _| jrCtj| _d S tj| _d S )NFz/CUDA is not available, disabling CUDA profiling   )
stacklevel)enabledr   function_eventsenteredr   r   r   r   r   torchcudais_availablewarningswarnr   CUDAprofiler_kindCPU)selfr   r   r   r   r   r   r    r$   `/sda-disk/www/egybert/egybert_env/lib/python3.10/site-packages/torch/autograd/profiler_legacy.py__init__$   s*   zprofile.__init__c              	   C   s(   t | j| j| j| j| j| jtjj	
 S N)r   r!   r   r   r   r   r   r   _C	_profiler_ExperimentalConfigr#   r$   r$   r%   configH   s   
zprofile.configc                 C   s*   | j sd S | jrtdd| _|   | S )Nz)Profiler context manager is not reentrantT)r   r   RuntimeError_start_tracer+   r$   r$   r%   	__enter__T   s   zprofile.__enter__c                 C   s   t |   d S r'   )r   r,   r+   r$   r$   r%   r.   ]   s   zprofile._start_tracec                 C   sV   | j sd S | jrtj  t }t|}t|| jrdnd | j| j	d| _
| j
  dS )Nr   )
use_devicer   r   F)r   r   r   r   synchronizer   _parse_legacy_recordsr   r   r   r   _build_tree)r#   exc_typeexc_valexc_tbrecordsparsed_resultsr$   r$   r%   __exit__`   s   

zprofile.__exit__c                 C      | j d u rdS t| j S )Nz$<unfinished profiler_legacy.profile>)r   reprr+   r$   r$   r%   __repr__s      

zprofile.__repr__c                 C   r:   )Nz,<unfinished profile.profiler_legacy.profile>)r   strr+   r$   r$   r%   __str__x   r=   zprofile.__str__c                 C   s   | j d u r	tdd S )NzProfiler didn't finish running)r   r-   r+   r$   r$   r%   _check_finish}   s   
zprofile._check_finishNd   K   7   P   c              	   C   s4   |    | jd u rtd| jj|||||||dS )NExpected profiling results)sort_by	row_limitmax_src_column_widthmax_name_column_widthmax_shapes_column_widthheadertop_level_events_only)r@   r   AssertionErrortable)r#   rF   rG   rH   rI   rJ   rK   rL   r$   r$   r%   rN      s   

zprofile.tablec                 C   s&   |    | jd u rtd| j|S NrE   )r@   r   rM   export_chrome_trace)r#   pathr$   r$   r%   rP      s   
zprofile.export_chrome_traceself_cpu_time_totalrQ   metricc                 C   s6   |    | jd u rtd| jstd| j||S )NrE   z(export_stacks() requires with_stack=True)r@   r   rM   r   export_stacks)r#   rQ   rS   r$   r$   r%   rT      s   
zprofile.export_stacksr   c                 C   s(   |    | jd u rtd| j||S rO   )r@   r   rM   key_averages)r#   group_by_input_shapegroup_by_stack_nr$   r$   r%   rU      s   
zprofile.key_averagesc                 C   s$   |    | jd u rtd| j S rO   )r@   r   rM   total_averager+   r$   r$   r%   rX      s   

zprofile.total_averagec                 C   s"   |    | jdu rtd| jjS )z;Return CPU time as the sum of self times across all events.NrE   )r@   r   rM   rR   r+   r$   r$   r%   rR      s   
zprofile.self_cpu_time_total)T)NrA   rB   rC   rD   NF)rR   )Fr   )__name__
__module____qualname____doc__r&   r,   r/   r.   r9   r<   r?   r@   rN   r   rP   r>   rT   rU   rX   propertyrR   r$   r$   r$   r%   r      sH    $	





c                 C   s  dd }d }g }t j| D ]}| }|d u r|dkr|}q|d u s'| r+td| D ]}i }i }i }	t }
d }|D ]z}||}t| sN||
v rT|
| q=|	 dkr|d ur~| | kou|	 |	 kou|
 |
 k}|r~|
| q=||	|< d||< d||< n*|	 dkr[||	vrtd| d	|	| }|| }|| }| p| | k}| }| }td,i d
| d|
 dt| dddt| ddd| d||d||d| d| ddd | D d| d| rdnd d|d|d|d|d | d!tjd"dd#|}|sL| rL||}|dkrL|| | | || |	|= ||= ||= n[|	 d$krt|}t|}||krztd%| d&| d'|D ]}||  | 7  < q||D ]}||  |  7  < q|dkrtdt!d dddg | |  dd(
}|| |}q=q-|j"d)d* d+ |S )-Nc                 S   s   |   |  fS )zPReturn a tuple for correlating start and end records in `_parse_legacy_records`.)handlenode_id)recordr$   r$   r%   _get_record_key   s   z._parse_legacy_records.<locals>._get_record_key__start_profilez#Expected a valid local start_recordpushr   popzExpected record with key z[ to exist in range_starts. This means that the pop event did not have a corresponding push.idr_   nameT)rf   with_wildcard
trace_nameFthreadstart_usend_us
fwd_threadinput_shapesstackc                 S   s   g | ]}t |r|qS r$   )r	   ).0entryr$   r$   r%   
<listcomp>  s
    z)_parse_legacy_records.<locals>.<listcomp>scoper0   r   cpu_memory_usagedevice_memory_usageis_async	is_remotesequence_nrdevice_type	is_legacyflopsmemory_alloczBExpected CPU and CUDA memory allocation handles to match, but got z	 CPU and z CUDA)
re   rf   rh   ri   rj   rk   rn   rs   rt   ry   c                 S   s   | j j| j j gS r'   )
time_rangestartend)evtr$   r$   r%   <lambda>F  s    z'_parse_legacy_records.<locals>.<lambda>)keyr$   )#	itertoolschainfrom_iterablerf   rv   rM   setr   addkindr_   ru   	thread_idrz   r   r^   r
   cpu_elapsed_usfwd_thread_idshapesrn   rr   has_cudarw   r   r"   cuda_elapsed_usappend_kerneldeviceappendlenrs   cuda_memory_usager   sort)thread_recordsra   start_record	functionsr`   rf   thread_record_listcpu_memory_allocscuda_memory_allocsrange_startsfiltered_handlesprev_record
record_key	duplicater}   rs   r   ru   is_remote_eventstart_flopsfedurationnum_open_handles_cpunum_open_handles_cudar^   r$   r$   r%   r2      s  






	






ir2   )r   r   typing_extensionsr   r   
torch.cudatorch.autogradr   r   r   r   r   torch.autograd.profiler_utilr   r	   r
   r   r   r   __all__r   r2   r$   r$   r$   r%   <module>   s    
 #