U
    d+                     @   s|   d dl Z d dlZ d dlmZmZmZmZmZmZ d dl	m
Z
mZmZmZmZ d dlZd dlmZ G dd deZdd ZdS )	    N)	EventListFunctionEventMEMORY_EVENT_NAME_filter_name_filter_stack_entry_rewrite_name)
DeviceTypeProfilerConfigProfilerState_disable_profiler_legacy_enable_profiler_legacy)warnc                   @   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)eedddZd*d!d"Zejje_d#d$ Zejje_ed%d& ZdS )+profilez&DEPRECATED: use torch.profiler insteadTF)use_cudarecord_shapes
with_flopsprofile_memory
with_stackwith_modulesc                C   s   || _ | j sd S || _d | _d| _|| _|| _|  j| jO  _|| _|| _|| _| jrnt	j
 sntd d| _| jr~tj| _ntj| _d S )NFz/CUDA is not available, disabling CUDA profiling)enabledr   function_eventsenteredr   r   r   r   r   torchcudaZis_availabler   r
   ZCUDAprofiler_kindCPU)selfr   r   r   r   r   r   r    r   B/tmp/pip-unpacked-wheel-ua33x9lu/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   Z_CZ	_autogradZ_ExperimentalConfigr   r   r   r   config3   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__?   s    zprofile.__enter__c                 C   s   t |   d S r    )r   r"   r!   r   r   r   r$   H   s    zprofile._start_tracec                 C   sN   | j s
d S | jrtj  t }t|}t|| j| j| j	d| _
| j
  dS )N)r   r   r   F)r   r   r   r   Zsynchronizer   _parse_legacy_recordsr   r   r   r   Z_build_tree)r   exc_typeexc_valexc_tbrecordsZparsed_resultsr   r   r   __exit__K   s    

zprofile.__exit__c                 C   s   | j d krdS t| j S )Nz$<unfinished profiler_legacy.profile>)r   reprr!   r   r   r   __repr__[   s    
zprofile.__repr__c                 C   s   | j d krdS t| j S )Nz,<unfinished profile.profiler_legacy.profile>)r   strr!   r   r   r   __str__`   s    
zprofile.__str__c                 C   s   | j d krtdd S )NzProfiler didn't finish running)r   r#   r!   r   r   r   _check_finishe   s    
zprofile._check_finishNd   K   c                 C   s,   |    | jd k	st| jj|||||dS )N)sort_by	row_limitmax_src_column_widthheadertop_level_events_only)r0   r   AssertionErrortable)r   r3   r4   r5   r6   r7   r   r   r   r9   i   s       zprofile.tablec                 C   s"   |    | jd k	st| j|S r    )r0   r   r8   export_chrome_trace)r   pathr   r   r   r:   r   s    zprofile.export_chrome_traceself_cpu_time_total)r;   metricc                 C   s6   |    | jd k	std| js(td| j||S )NExpected profiling resultsz(export_stacks() requires with_stack=True)r0   r   r8   r   export_stacks)r   r;   r=   r   r   r   r?   x   s    zprofile.export_stacksr   c                 C   s(   |    | jd k	std| j||S Nr>   )r0   r   r8   key_averages)r   Zgroup_by_input_shapeZgroup_by_stack_nr   r   r   rA   ~   s    zprofile.key_averagesc                 C   s$   |    | jd k	std| j S r@   )r0   r   r8   total_averager!   r   r   r   rB      s    zprofile.total_averagec                 C   s   |    | jdk	st| jjS )zl Returns total time spent on CPU obtained as a sum of
        all self times across all the events.
        N)r0   r   r8   r<   r!   r   r   r   r<      s    zprofile.self_cpu_time_total)T)Nr1   r2   NF)r<   )Fr   )__name__
__module____qualname____doc__r   r"   r%   r$   r+   r-   r/   r0   r9   r   r:   r.   r?   rA   rB   propertyr<   r   r   r   r   r      s8     	





r   c                 C   s&  dd }d}d }g }g }t j|  D ] }| }|d kr"|dkr"|}q"|d k	rT| rXt| D ]}i }	i }
i }t }d }|D ]}||}t| s||kr|| q|| dkr |d k	r| | ko| | ko|	 |	 k}|r|| q||||< d|	|< d|
|< n| dkr^||ksFtd
||| }|	| }|
| }| pv| | k}| }| }t| |	 t| dd	t| d
d	| ||||| | dd | D | ||||| tjd|d}|s@| r@||}|dkr@|| | | || ||= |	|= |
|= n| dkr
t|	}t|
}||kst|	 D ]}|	|  |  7  < q|
 D ]}|
|  |! 7  < q|dkr
tdt"d dddg |  |! dd
}|| |}q|q\|j#dd d |S )Nc                 S   s   |   |  fS )zt
        Returns a tuple to be used by _parse_legacy_records for correlating start and
        end records.
        )handlenode_id)recordr   r   r   _get_record_key   s    z._parse_legacy_records.<locals>._get_record_keyr   Z__start_profilepushpopzExpected record with key {} to exist in range_starts.
                    This means that the pop event did not have a corresponding push.T)nameZwith_wildcardFc                 S   s   g | ]}t |r|qS r   )r   ).0entryr   r   r   
<listcomp>   s      z)_parse_legacy_records.<locals>.<listcomp>)idrI   rN   
trace_namethreadstart_usend_usZ
fwd_threadZinput_shapesstackscopecpu_memory_usagecuda_memory_usageis_async	is_remotesequence_nrZdevice_type	is_legacyflopsZmemory_alloc)
rR   rN   rS   rT   rU   rV   rW   rY   rZ   r^   c                 S   s   | j j| j j gS r    )Z
time_rangestartend)Zevtr   r   r   <lambda>      z'_parse_legacy_records.<locals>.<lambda>)key)$	itertoolschainrN   r\   r8   setr   addkindrI   formatr[   Z	thread_idr_   r   rH   r   Zcpu_elapsed_usZfwd_thread_idZshapesrW   rX   r]   r   r   Zhas_cudaZcuda_elapsed_usZappend_kernelZdeviceappendlenkeysrY   rZ   r   sort)Zthread_recordsrK   Znext_idZstart_recordZ	functionsZrecord_stackrJ   rN   Zthread_record_listZcpu_memory_allocsZcuda_memory_allocsZrange_startsZfiltered_handlesZprev_recordZ
record_keyZ	duplicater`   rY   rZ   r[   Zis_remote_eventZstart_flopsZfedurationZnum_open_handles_cpuZnum_open_handles_cudarH   r   r   r   r&      s    










r&   )r   Z
torch.cudaZtorch.autograd.profiler_utilr   r   r   r   r   r   Ztorch.autogradr   r	   r
   r   r   re   warningsr   objectr   r&   r   r   r   r   <module>   s     