a
    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rt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/var/www/auris/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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 u 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 u 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 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   s0   |    | jd usJ | 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   table)r!   r;   r<   r=   r>   r?   r@   rA   r"   r"   r#   rB      s    
zprofile.tablec                 C   s"   |    | jd usJ | j|S r%   )r6   r   export_chrome_trace)r!   pathr"   r"   r#   rC      s    zprofile.export_chrome_traceself_cpu_time_total)rD   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)r6   r   r   export_stacks)r!   rD   rF   r"   r"   r#   rH      s    zprofile.export_stacksr   c                 C   s(   |    | jd usJ d| j||S NrG   )r6   r   key_averages)r!   Zgroup_by_input_shapeZgroup_by_stack_nr"   r"   r#   rJ      s    zprofile.key_averagesc                 C   s$   |    | jd usJ d| j S rI   )r6   r   total_averager&   r"   r"   r#   rK      s    zprofile.total_averagec                 C   s   |    | jdusJ | jjS )z;Return CPU time as the sum of self times across all events.N)r6   r   rE   r&   r"   r"   r#   rE      s    zprofile.self_cpu_time_total)T)Nr7   r8   r9   r:   NF)rE   )Fr   )__name__
__module____qualname____doc__r$   r'   r*   r)   r1   r3   r5   r6   rB   r   rC   r4   rH   rJ   rK   propertyrE   r"   r"   r"   r#   r      sF    $	       





c                 C   s.  dd }d }g }t j| D ] }| }|d u r|dkr|}q|d urN| rRJ | D ]}i }i }i }	t }
d }|D ]}||}t| s||
v r|
| qv| dkr|d ur| | ko| | ko|	 |	 k}|r|
| qv||	|< d||< d||< n| dkrf||	v s>J d| d|	| }|| }|| }|
 pn| | k}| }| }t| |	 t| d	d
t| dd
| ||||| | dd | D | | rdnd ||||| tjd	|d}|sH| rH||}|dkrH|| | | || |	|= ||= ||= n| dkrt|}t|}||ksJ | D ]}||  | 7  < q| D ]}||  |  7  < q|dkrtdt!d dddg | |  d	d
}|| |}qvqV|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_keyZ__start_profilepushr   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   )idrR   rW   
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[   rW   r\   r]   r^   r_   r`   rb   rc   rg   c                 S   s   | j j| j j gS r%   )Z
time_rangestartend)Zevtr"   r"   r#   <lambda>7      z'_parse_legacy_records.<locals>.<lambda>)key)#	itertoolschainfrom_iterablerW   re   setr   addkindrR   rd   Z	thread_idrh   r   rQ   r
   Zcpu_elapsed_usZfwd_thread_idZshapesr`   ra   Zhas_cudarf   r   r    Zcuda_elapsed_usZappend_kernelZdeviceappendlenkeysrb   cuda_memory_usager   sort)Zthread_recordsrT   Zstart_recordZ	functionsrS   rW   Zthread_record_listZcpu_memory_allocsZcuda_memory_allocsZrange_startsZfiltered_handlesZprev_recordZ
record_keyZ	duplicateri   rb   rw   rd   Zis_remote_eventZstart_flopsZfedurationZnum_open_handles_cpuZnum_open_handles_cudarQ   r"   r"   r#   r,      s    









r,   )rn   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    
 