U
    E‹Ñh-  ã                   @   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+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 )-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rtt	j
 ¡ sttjddd� d| _| jr„tj| _ntj| _d S )NFz/CUDA is not available, disabling CUDA profilingé   )Ú
stacklevel)Úenabledr   Úfunction_eventsÚenteredr   r   r   r   r   ÚtorchÚcudaZis_availableÚwarningsÚwarnr   ZCUDAÚprofiler_kindÚCPU)Úselfr   r   r   r   r   r   r   © r"   úL/tmp/user/2202/pip-unpacked-wheel-bf9o3bgk/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	_profilerZ_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r6dnd | j| j	d�| _
| j
 ¡  dS )Nr   )Ú
use_devicer   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__`   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__q   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__v   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_finish{   s    
zprofile._check_finishNéd   éK   é7   éP   c              	   C   s0   |   ¡  | jd k	st‚| jj|||||||d�S )N)Úsort_byÚ	row_limitÚmax_src_column_widthÚmax_name_column_widthÚmax_shapes_column_widthÚheaderÚtop_level_events_only)r6   r   ÚAssertionErrorÚtable)r!   r;   r<   r=   r>   r?   r@   rA   r"   r"   r#   rC      s    
ùzprofile.tablec                 C   s"   |   ¡  | jd k	st‚| j |¡S r%   )r6   r   rB   Úexport_chrome_trace)r!   Úpathr"   r"   r#   rD   —   s    zprofile.export_chrome_traceÚself_cpu_time_total)rE   Ú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)r6   r   rB   r   Úexport_stacks)r!   rE   rG   r"   r"   r#   rI   ž   s    zprofile.export_stacksr   c                 C   s(   |   ¡  | jd k	stdƒ‚| j ||¡S ©NrH   )r6   r   rB   Úkey_averages)r!   Zgroup_by_input_shapeZgroup_by_stack_nr"   r"   r#   rK   ¤   s    zprofile.key_averagesc                 C   s$   |   ¡  | jd k	stdƒ‚| j ¡ S rJ   )r6   r   rB   Útotal_averager&   r"   r"   r#   rL   «   s    zprofile.total_averagec                 C   s   |   ¡  | jdk	st‚| jjS )z;Return CPU time as the sum of self times across all events.N)r6   r   rB   rF   r&   r"   r"   r#   rF   ²   s    zprofile.self_cpu_time_total)T)Nr7   r8   r9   r:   NF)rF   )Fr   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r$   r'   r*   r)   r1   r3   r5   r6   rC   r   rD   r4   rI   rK   rL   ÚpropertyrF   r"   r"   r"   r#   r      sF    þ÷$	       ø





c                 C   s:  dd„ }d}d }g }g }t j | ¡D ] }| ¡ }|d kr$|dkr$|}q$|d k	rV| ¡ rZt‚| 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�rr||k�sJtd|› d�ƒ‚|| }|	| }|
| }| ¡ �pz| ¡ | ¡ k}| ¡ }| ¡ }t| ¡ | 
¡ t| ¡ d	d
�t| ¡ dd
�| ¡ | |¡| |¡| ¡ | ¡ dd„ | ¡ D ƒ| ¡ | ¡ �rüdnd ||||| ¡ tjd	|d�}|�sT| ¡ �rT| |¡}|dk�rT| | ¡ | ¡ |¡ | |¡ ||= |	|= |
|= n¬| 	¡ dk�rt|	ƒ}t|
ƒ}||k�sžt‚|	 ¡ 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 )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_keyr   Z__start_profileÚpushÚpopzExpected record with key zo 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>r   )ÚidrS   rX   Ú
trace_nameÚthreadÚstart_usÚend_usZ
fwd_threadZinput_shapesÚstackÚscoper+   Úcpu_memory_usageÚdevice_memory_usageÚis_asyncÚ	is_remoteÚsequence_nrZdevice_typeÚ	is_legacyÚflopsZmemory_alloc)
r\   rX   r]   r^   r_   r`   ra   rc   rd   rh   c                 S   s   | j j| j j gS r%   )Z
time_rangeÚstartÚend)Zevtr"   r"   r#   Ú<lambda>9  ó    z'_parse_legacy_records.<locals>.<lambda>)Úkey)$Ú	itertoolsÚchainÚfrom_iterablerX   rf   rB   Úsetr   ÚaddÚkindrS   re   Z	thread_idri   r   rR   r
   Zcpu_elapsed_usZfwd_thread_idZshapesra   rb   Zhas_cudarg   r   r    Zcuda_elapsed_usZappend_kernelZdeviceÚappendÚlenÚkeysrc   Úcuda_memory_usager   Úsort)Zthread_recordsrU   Znext_idZstart_recordZ	functionsZrecord_stackrT   rX   Zthread_record_listZcpu_memory_allocsZcuda_memory_allocsZrange_startsZfiltered_handlesZprev_recordZ
record_keyZ	duplicaterj   rc   rx   re   Zis_remote_eventZstart_flopsZfeÚdurationZnum_open_handles_cpuZnum_open_handles_cudarR   r"   r"   r#   r,   º   sÌ    



ÿý
ÿ
þÿê



ö
r,   )ro   r   Ztyping_extensionsr   r   Z
torch.cudaZtorch.autogradr   r   r   r   r   Ztorch.autograd.profiler_utilr   r	   r
   r   r   r   Ú__all__r   r,   r"   r"   r"   r#   Ú<module>   s    	ý 