a
    –¥»ay*  ã                   @   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   úX/home/yuhw/wc/pochoir/venv/lib/python3.9/site-packages/torch/autograd/profiler_legacy.pyÚ__init__   s$    

zprofile.__init__c                 C   s   t | j| j| j| j| j| jƒS ©N)r	   r   r   r   r   r   r   ©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$   E   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__H   s    
ü
zprofile.__exit__c                 C   s   | j d u rdS t| j ƒS )Nz$<unfinished profiler_legacy.profile>)r   Úreprr!   r   r   r   Ú__repr__X   s    
zprofile.__repr__c                 C   s   | j d u 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 u rtdƒ‚d S )NzProfiler didn't finish running)r   r#   r!   r   r   r   Ú_check_finishb   s    
zprofile._check_finishNéd   éK   c                 C   s,   |   ¡  | jd usJ ‚| jj|||||d�S )N)Úsort_byÚ	row_limitÚmax_src_column_widthÚheaderÚtop_level_events_only)r0   r   Útable)r   r3   r4   r5   r6   r7   r   r   r   r8   f   s    þzprofile.tablec                 C   s"   |   ¡  | jd usJ ‚| j |¡S r    )r0   r   Úexport_chrome_trace)r   Úpathr   r   r   r9   o   s    zprofile.export_chrome_traceÚself_cpu_time_total)r:   Úmetricc                 C   s6   |   ¡  | jd usJ dƒ‚| js(J dƒ‚| j ||¡S )NúExpected profiling resultsz(export_stacks() requires with_stack=True)r0   r   r   Úexport_stacks)r   r:   r<   r   r   r   r>   u   s    zprofile.export_stacksr   c                 C   s(   |   ¡  | jd usJ dƒ‚| j ||¡S ©Nr=   )r0   r   Úkey_averages)r   Zgroup_by_input_shapeZgroup_by_stack_nr   r   r   r@   {   s    zprofile.key_averagesc                 C   s$   |   ¡  | jd usJ dƒ‚| j ¡ S r?   )r0   r   Útotal_averager!   r   r   r   rA   �   s    zprofile.total_averagec                 C   s   |   ¡  | jdusJ ‚| 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   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   r8   r   r9   r.   r>   r@   rA   Úpropertyr;   r   r   r   r   r      s8    þ÷ 		





r   c                 C   s&  dd„ }d}d }g }g }t j| Ž D ] }| ¡ }|d u r"|dkr"|}q"|d urT| ¡ rXJ ‚| D �]²}i }	i }
i }tƒ }d }|D �]�}||ƒ}t| ¡ ƒsž||v rª| |¡ q|| ¡ dk�r |d u�r| ¡ | ¡ koð| ¡ | ¡ koð| ¡ | ¡ k}|�r| |¡ q||||< d|	|< d|
|< �nê| ¡ dk�r^||v �sFJ 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�sŠJ ‚|	 ¡ 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ÚpopzŠExpected 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>â   ó    z)_parse_legacy_records.<locals>.<listcomp>)ÚidrH   rM   Ú
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   rM   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>  rQ   z'_parse_legacy_records.<locals>.<lambda>)Úkey)#Ú	itertoolsÚchainrM   r\   Úsetr   ÚaddÚkindrH   Úformatr[   Z	thread_idr_   r   rG   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_recordsrJ   Znext_idZstart_recordZ	functionsZrecord_stackrI   rM   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_cudarG   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   rd   Úwarningsr   Úobjectr   r&   r   r   r   r   Ú<module>   s     