a
    –¥»a…†  ã                   @   s(  d dl Z d dlZd dlmZ d dlmZmZ d dlmZ d dl	m
Z
mZmZmZ d dlZd dlZG dd„ deƒZdd	„ Zd
d„ Zdd„ Zdd„ ZG dd„ deƒZG dd„ deƒZedg d¢ƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ dƒZdd„ Zd Zd!d"„ Z d*d$d%„Z!d+d(d)„Z"dS ),é    N)Ú
DeviceType)ÚdefaultdictÚ
namedtuple)Ú
attrgetter)ÚDictÚListÚTupleÚOptionalc                       s�   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	e
dd„ ƒZd"dd„Zdd„ Zdd„ Zeedœdd„Zd#dd„Zd d!„ Z‡  ZS )$Ú	EventListz&A list of Events (for pretty printing)c                    sV   |  dd¡}|  dd¡}|  dd¡}tt| ƒj|i |¤Ž || _|| _d| _|| _d S )NÚuse_cudaTÚprofile_memoryFÚ
with_flops)ÚpopÚsuperr
   Ú__init__Ú	_use_cudaÚ_profile_memoryÚ_tree_builtÚ_with_flops)ÚselfÚargsÚkwargsr   r   r   ©Ú	__class__© úV/home/yuhw/wc/pochoir/venv/lib/python3.9/site-packages/torch/autograd/profiler_util.pyr      s    zEventList.__init__c                 C   s"   |   ¡  |  ¡  |  ¡  d| _d S )NT)Ú_populate_cpu_childrenÚ_remove_dup_nodesÚ_set_backward_stacktracesr   ©r   r   r   r   Ú_build_tree   s    zEventList._build_treec                 C   s   |   ¡ S ©N)Útabler   r   r   r   Ú__str__    s    zEventList.__str__c                    sÖ   t ƒ ‰ tt| ƒƒD ]†}| | jd ur| | jj| | jkrt| | jjƒdkr| | j| | j_| | j| | j_| | jD ]}| | j|_q|ˆ  |¡ qtˆ ƒdkr¨qÒ‡ fdd„t| ƒD ƒ}|  	¡  |  
|¡ q d S )Né   r   c                    s   g | ]\}}|ˆ vr|‘qS r   r   )Ú.0ÚindZev©Z	to_deleter   r   Ú
<listcomp>1   ó    z/EventList._remove_dup_nodes.<locals>.<listcomp>)ÚsetÚrangeÚlenÚ
cpu_parentÚnameÚcpu_childrenÚkernelsÚaddÚ	enumerateÚclearÚextend)r   ÚidxÚchZnew_evtsr   r'   r   r   #   s"    ÿþzEventList._remove_dup_nodesc                 C   sÞ   dd„ | D ƒ}t |tdƒd�}tj|dd„ d�}|D ]¤\}}t |dd„ d�}g }d}|D ]~}	t|ƒdkrÌ|d	 }
|	jj|
jjks�|	jj|
jjkrš| ¡  q\|
 	|	¡ |	j
d
u s¾J d |	j¡ƒ‚|	 |
¡ qÌq\| |	¡ qXq4d
S )a4  Populates child events into each underlying FunctionEvent object.
        One event is a child of another if [s1, e1) is inside [s2, e2). Where
        s1 and e1 would be start and end of the child event's interval. And
        s2 and e2 start and end of the parent event's interval

        Example: In event list [[0, 10], [1, 3], [3, 4]] would have make [0, 10]
        be a parent of two other intervals.

        If for any reason two intervals intersect only partially, this function
        will not record a parent child relationship between then.
        c                 S   s"   g | ]}|j s|jtjkr|‘qS r   )Úis_asyncÚdevice_typer   ÚCPU©r%   Úevtr   r   r   r(   E   r)   z4EventList._populate_cpu_children.<locals>.<listcomp>Úthread)Úkeyc                 S   s   | j | jfS r!   )r<   Únode_id©Úeventr   r   r   Ú<lambda>N   r)   z2EventList._populate_cpu_children.<locals>.<lambda>c                 S   s   | j j| j j gS r!   )Ú
time_rangeÚstartÚendr?   r   r   r   rA   `   r)   r   éÿÿÿÿNz*There is already a CPU parent event for {})Úsortedr   Ú	itertoolsÚgroupbyr,   rB   rC   rD   r   Úappend_cpu_childr-   Úformatr=   Úset_cpu_parentÚappend)r   Zsync_eventsÚeventsÚthreadsZ	thread_idZthread_eventsZthread_events_Zcurrent_eventsZcur_endr@   Úparentr   r   r   r   5   s@    þÿþÿ

ÿÿþ
z EventList._populate_cpu_childrenc                    s    ‡ fdd„‰ i }| D ]8}ˆ |ƒd u r|j d ur|j|jf}||vr|j ||< q| D ]H}ˆ |ƒ}|d urR|jd ustJ ‚|j|jf}||v r”|| |_ qRg |_ qRd S )Nc                    s(   | d u rd S | j dkr| S ˆ | jƒS d S ©Nr$   )Úscoper-   ©r;   ©Ú	bw_parentr   r   rT   x   s
    
z6EventList._set_backward_stacktraces.<locals>.bw_parent)ÚstackÚsequence_nrr<   Ú
fwd_thread)r   Z
fwd_stacksr;   ÚtÚpr   rS   r   r   w   s    z#EventList._set_backward_stacktracesc                 C   s   t dd„ | D ƒƒS )Nc                 S   s   g | ]
}|j ‘qS r   ©Úself_cpu_time_total©r%   r@   r   r   r   r(   “   r)   z1EventList.self_cpu_time_total.<locals>.<listcomp>)Úsumr   r   r   r   r[   ‘   s    zEventList.self_cpu_time_totalNéd   éK   Fc              
   C   s   t | ||||| j| j|d�S )a²  Prints an EventList as a nicely formatted table.

        Args:
            sort_by (str, optional): Attribute used to sort entries. By default
                they are printed in the same order as they were registered.
                Valid keys include: ``cpu_time``, ``cuda_time``, ``cpu_time_total``,
                ``cuda_time_total``, ``cpu_memory_usage``, ``cuda_memory_usage``,
                ``self_cpu_memory_usage``, ``self_cuda_memory_usage``, ``count``.
            top_level_events_only(bool, optional): Boolean flag to determine the
                selection of events to display. If true, the profiler will only
                display events at top level like top-level invocation of python
                `lstm`, python `add` or other functions, nested events like low-level
                cpu/cuda ops events are omitted for profiler result readability.

        Returns:
            A string containing the table.
        )Úsort_byÚ	row_limitÚmax_src_column_widthÚheaderr   r   Útop_level_events_only)Ú_build_tabler   r   )r   r`   ra   rb   rc   rd   r   r   r   r"   •   s    øzEventList.tablec                 C   s  ddl }t|dƒ�Þ}g }d}| d¡ | D ]‚}|jdu r:q*| d|j|jj|j ¡ |js^|jnd|j	› d|j› d�f ¡ |j
D ]*}| d	|j|jj|j|f ¡ |d
7 }q€q*t| ƒdkrØ| | ¡ d |j¡ | ¡  | d¡ W d  ƒ n1 sö0    Y  dS )zäExports an EventList as a Chrome tracing tools file.

        The checkpoint can be later loaded and inspected under ``chrome://tracing`` URL.

        Args:
            path (str): Path where the trace will be written.
        r   NÚwú[z_{"name": "%s", "ph": "X", "ts": %s, "dur": %s, "tid": %s, "pid": "CPU functions", "args": {}}, z
" node_id:z, thread_id:z "zt{"name": "%s", "ph": "s", "ts": %s, "tid": %s, "pid": "CPU functions", "id": %s, "cat": "cpu_to_cuda", "args": {}}, r$   é   ú])ÚosÚopenÚwriteÚ
trace_namerB   rC   Ú
elapsed_usÚ	is_remoter<   r>   r0   r,   ÚseekÚtellÚSEEK_SETÚtruncate)r   Úpathrj   ÚfZchrome_eventsZnext_idr;   Úkr   r   r   Úexport_chrome_trace±   s<    

ÿúùÿ

ÿù
zEventList.export_chrome_tracec                 C   s   ddgS )Nr[   Úself_cuda_time_totalr   r   r   r   r   Úsupported_export_stacks_metricsè   s    z)EventList.supported_export_stacks_metrics)rt   Úmetricc           	      C   sâ   ||   ¡ vr tdt|   ¡ ƒ ƒ‚t dd¡}t|dƒ�˜}| D ]‚}|jr<t|jƒdkr<t||ƒ}t|ƒdkr<d}t	|jƒD ]}|| 
|¡7 }|d7 }qx|d d… d	 tt|ƒƒ }| |d
 ¡ q<W d   ƒ n1 sÔ0    Y  d S )Nzmetric should be one of: z ;	
Z____rf   r   Ú ú;rE   ú Ú
)ry   Ú
ValueErrorÚstrÚ	maketransrk   rU   r,   ÚgetattrÚintÚreversedÚ	translaterl   )	r   rt   rz   Ztranslate_tableru   r;   Zmetric_valueZ	stack_strÚentryr   r   r   Úexport_stacksë   s    

zEventList.export_stacksr   c                 C   sŠ   | j s
J ‚ttƒ}ttdf dœdd„}| D ]}|||||ƒ  |¡ q,t| ¡ | j| j	| j
d�}|D ]}|jd|… |_|sfd|_qf|S )aH  Averages all function events over their keys.

        Args:
            group_by_input_shapes: group entries by
                (event name, input shapes) rather than just event name.
                This is useful to see which input shapes contribute to the runtime
                the most and may help with size-specific optimizations or
                choosing the best candidates for quantization (aka fitting a roof line)

            group_by_stack_n: group by top n stack trace entries

        Returns:
            An EventList containing FunctionEventAvg objects.
        .)Úreturnc                 S   sZ   t | jƒt | jƒt | jƒt | jƒg}|r8| t | jƒ¡ |dkrR|| jd |… 7 }t|ƒS ©Nr   )	r€   r=   r>   r8   Ú	is_legacyrL   Úinput_shapesrU   Útuple)r@   Úgroup_by_input_shapesÚgroup_by_stack_nr=   r   r   r   Úget_key  s    $z'EventList.key_averages.<locals>.get_key©r   r   r   Nr{   )r   r   ÚFunctionEventAvgr   r€   r1   r
   Úvaluesr   r   r   rU   r‹   )r   r�   rŽ   Ústatsr�   r;   Zavg_listr   r   r   Úkey_averagesû   s     
üzEventList.key_averagesc                 C   s(   t ƒ }| D ]}||7 }d|_q
d|_|S )zVAverages all events.

        Returns:
            A FunctionEventAvg object.
        NZTotal)r‘   r=   )r   Z
total_statr;   r   r   r   Útotal_average"  s    zEventList.total_average)Nr^   r_   NF)Fr   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r    r#   r   r   r   Úpropertyr[   r"   rw   ry   r€   r‡   r”   r•   Ú__classcell__r   r   r   r   r
      s   
B

7
'r
   c                 C   s>   d}d}| |krd  | | ¡S | |kr4d  | | ¡S d  | ¡S )ú+Defines how to format time in FunctionEventg    €„.Ag     @�@z{:.3f}sz{:.3f}msz{:.3f}us©rJ   )Útime_usZUS_IN_SECONDZUS_IN_MSr   r   r   Ú_format_time0  s    rŸ   c                 C   s4   |dkr"| dksJ d  | ¡ƒ‚dS d  | d | ¡S )rœ   r   z Expected time_us == 0 but got {}ÚNaNz{:.2f}%g      Y@r�   )rž   Ztotal_time_usr   r   r   Ú_format_time_share:  s    r¡   c                 C   s~   d}d| }d| }t | ƒ|kr2d | d | ¡S t | ƒ|krPd | d | ¡S t | ƒ|krnd | d | ¡S t| ƒd S dS )z&Returns a formatted memory size stringi   z	{:.2f} Gbç      ð?z	{:.2f} Mbz	{:.2f} Kbz bN)ÚabsrJ   r€   )ÚnbytesZKBZMBZGBr   r   r   Ú_format_memoryA  s    r¥   c                    s   t ‡ fdd„ƒS )Nc                    s   t t| ˆ ƒƒS r!   )rŸ   r‚   r   ©r.   r   r   rA   P  r)   z!_attr_formatter.<locals>.<lambda>)rš   r¦   r   r¦   r   Ú_attr_formatterO  s    r§   c                   @   sX   e Zd ZdZedƒZedƒZedƒZedƒZedƒZ	edƒZ
edd	„ ƒZed
d„ ƒZdS )ÚFormattedTimesMixinz{Helpers for FunctionEvent and FunctionEventAvg.

    The subclass should define `*_time_total` and `count` attributes.
    Úcpu_timeÚ	cuda_timeÚcpu_time_totalÚcuda_time_totalr[   rx   c                 C   s   | j dkrdS d| j | j  S ©Nr   g        r¢   )Úcountr«   r   r   r   r   r©   _  s    zFormattedTimesMixin.cpu_timec                 C   s   | j dkrdS d| j | j  S r­   )r®   r¬   r   r   r   r   rª   c  s    zFormattedTimesMixin.cuda_timeN)r–   r—   r˜   r™   r§   Úcpu_time_strÚcuda_time_strÚcpu_time_total_strÚcuda_time_total_strÚself_cpu_time_total_strÚself_cuda_time_total_strrš   r©   rª   r   r   r   r   r¨   S  s   
r¨   c                   @   s   e Zd Zdd„ Zdd„ ZdS )ÚIntervalc                 C   s   || _ || _d S r!   )rC   rD   )r   rC   rD   r   r   r   r   i  s    zInterval.__init__c                 C   s   | j | j S r!   )rD   rC   r   r   r   r   rn   m  s    zInterval.elapsed_usN)r–   r—   r˜   r   rn   r   r   r   r   rµ   h  s   rµ   ÚKernel)r.   ÚdeviceÚdurationc                   @   s®   e Zd ZdZddddddddddejddddfdd„Zdd	„ Zd
d„ Zdd„ Z	e
dd„ ƒZe
dd„ ƒZe
dd„ ƒZe
dd„ ƒZe
dd„ ƒZe
dd„ ƒZe
dd„ ƒZdd„ ZdS )ÚFunctionEventz.Profiling information about a single function.Nr   FrE   c                 C   s”   || _ || _|| _|| _t||ƒ| _|| _|| _g | _d| _	g | _
d | _|| _|| _|	| _|
| _|| _|| _|| _|| _|| _|| _|| _|| _d S rP   )Úidr>   r.   rm   rµ   rB   r<   rW   r0   r®   r/   r-   r‹   rU   rQ   Úcpu_memory_usageÚcuda_memory_usager7   ro   rV   r8   Údevice_indexrŠ   Úflops)r   rº   r.   r<   Ústart_usÚend_usrW   r‹   rU   rQ   r»   r¼   r7   ro   rV   r>   r8   r½   rŠ   r¾   rm   r   r   r   r   v  s.    zFunctionEvent.__init__c                 C   s(   | j tjksJ ‚| j t|||ƒ¡ d S r!   )r8   r   r9   r0   rL   r¶   )r   r.   r·   r¸   r   r   r   Úappend_kernel“  s    zFunctionEvent.append_kernelc                 C   s>   | j tjksJ ‚t|tƒsJ ‚|j tjks.J ‚| j |¡ dS )z­Append a CPU child of type FunctionEvent.

        One is supposed to append only direct children to the event to have
        correct self cpu time being reported.
        N)r8   r   r9   Ú
isinstancer¹   r/   rL   )r   Úchildr   r   r   rI   —  s    zFunctionEvent.append_cpu_childc                 C   s8   | j tjksJ ‚t|tƒsJ ‚|j tjks.J ‚|| _dS )a#  Set the immediate CPU parent of type FunctionEvent

        One profiling FunctionEvent should have only one CPU parent such that
        the child's range interval is completely inside the parent's. We use
        this connection to determine the event is from top-level op or not.
        N)r8   r   r9   rÂ   r¹   r-   )r   rO   r   r   r   rK   ¢  s    zFunctionEvent.set_cpu_parentc                 C   s0   | j s| jtjkrdS | jtdd„ | jD ƒƒ S )Nr   c                 S   s   g | ]
}|j ‘qS r   )r»   ©r%   rÃ   r   r   r   r(   µ  r)   z7FunctionEvent.self_cpu_memory_usage.<locals>.<listcomp>)r7   r8   r   r9   r»   r]   r/   r   r   r   r   Úself_cpu_memory_usage°  s
    ÿz#FunctionEvent.self_cpu_memory_usagec                 C   s0   | j s| jtjkrdS | jtdd„ | jD ƒƒ S )Nr   c                 S   s   g | ]
}|j ‘qS r   )r¼   rÄ   r   r   r   r(   ½  r)   z8FunctionEvent.self_cuda_memory_usage.<locals>.<listcomp>)r7   r8   r   r9   r¼   r]   r/   r   r   r   r   Úself_cuda_memory_usage¸  s
    ÿz$FunctionEvent.self_cuda_memory_usagec                 C   s0   | j s| jtjkrdS | jtdd„ | jD ƒƒ S )Nr   c                 S   s   g | ]
}|j ‘qS r   )r«   rÄ   r   r   r   r(   Å  r)   z5FunctionEvent.self_cpu_time_total.<locals>.<listcomp>)r7   r8   r   r9   r«   r]   r/   r   r   r   r   r[   À  s
    ÿz!FunctionEvent.self_cpu_time_totalc                 C   sx   | j r
dS | jtjkrZ| jsDtdd„ | jD ƒƒtdd„ | jD ƒƒ S tdd„ | jD ƒƒS n| jtjksjJ ‚| j	 
¡ S d S )Nr   c                 s   s   | ]}|j V  qd S r!   ©r¸   ©r%   Zkinfor   r   r   Ú	<genexpr>Ï  r)   z0FunctionEvent.cuda_time_total.<locals>.<genexpr>c                 s   s   | ]}|j V  qd S r!   ©r¬   )r%   r6   r   r   r   rÉ   Ð  r)   c                 s   s   | ]}|j V  qd S r!   rÇ   rÈ   r   r   r   rÉ   Ó  r)   )r7   r8   r   r9   rŠ   r]   r0   r/   ÚCUDArB   rn   r   r   r   r   r¬   È  s    ÿzFunctionEvent.cuda_time_totalc                 C   sJ   | j r
dS | jtjkr0| jtdd„ | jD ƒƒ S | jtjks@J ‚| jS d S )Nr   c                 S   s   g | ]
}|j ‘qS r   rÊ   rÄ   r   r   r   r(   Þ  r)   z6FunctionEvent.self_cuda_time_total.<locals>.<listcomp>)r7   r8   r   r9   r¬   r]   r/   rË   r   r   r   r   rx   Ø  s    ÿz"FunctionEvent.self_cuda_time_totalc                 C   s   | j tjkr| j ¡ S dS d S r‰   )r8   r   r9   rB   rn   r   r   r   r   r«   ã  s    
zFunctionEvent.cpu_time_totalc                 C   s   | j S r!   r¦   r   r   r   r   r=   ê  s    zFunctionEvent.keyc                 C   sf   d  | j| j| j| j| j| jj| jjt	dd„ | j
D ƒƒ| j| j| jt	| jƒ| j| j| j| j| j| j¡S )Nzñ<FunctionEvent id={} name={} device_type={} node_id={} cpu_time={} start_us={} end_us={} cpu_children={} cuda_time={} name={} thread={} input_shapes={} cpu_memory_usage={} cuda_memory_usage={} is_async={} is_remote={} seq_nr={} is_legacy={}>c                 S   s   g | ]
}|j ‘qS r   )rº   rÄ   r   r   r   r(   ú  r)   z*FunctionEvent.__repr__.<locals>.<listcomp>)rJ   rº   r.   r8   r>   r¯   rB   rC   rD   r€   r/   r°   r<   r‹   r»   r¼   r7   ro   rV   rŠ   r   r   r   r   Ú__repr__î  s*    ìÿzFunctionEvent.__repr__)r–   r—   r˜   r™   r   r9   r   rÁ   rI   rK   rš   rÅ   rÆ   r[   r¬   rx   r«   r=   rÌ   r   r   r   r   r¹   t  s0   
ü








r¹   c                   @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )r‘   z:Used to average stats over multiple FunctionEvent objects.c                 C   s„   d | _ d| _d| _d| _d| _d| _d| _d| _d| _d | _	d | _
d | _d| _d| _d| _d| _d | _d | _tj| _d| _d| _d S )Nr   F)r=   r®   r>   r7   ro   r«   r¬   r[   rx   r‹   rU   rQ   r»   r¼   rÅ   rÆ   r/   r-   r   r9   r8   rŠ   r¾   r   r   r   r   r     s*    zFunctionEventAvg.__init__c                 C   sJ  | j d u rb|j | _ |j| _|j| _|j| _|j| _|j| _|j| _|j| _|j| _|j	| _	|j
| _
t|ttfƒstJ ‚|j | j ks„J ‚|  j|j7  _|  j|j7  _|  j|j7  _|  j|j7  _|  j|j7  _|  j|j7  _|  j|j7  _|  j|j7  _|  j|j7  _| jd u �r*|j| _n|jd u�rF|  j|j7  _| S r!   )r=   r>   r7   ro   r-   r/   r‹   rU   rQ   r8   rŠ   rÂ   r¹   r‘   r«   r¬   r[   rx   r»   r¼   rÅ   rÆ   r®   r¾   ©r   Úotherr   r   r   r1   "  s8    

zFunctionEventAvg.addc                 C   s
   |   |¡S r!   )r1   rÍ   r   r   r   Ú__iadd__D  s    zFunctionEventAvg.__iadd__c              
   C   s,   d  | j| j| j| j| jt| jƒ| j| j	¡S )Nz�<FunctionEventAvg key={} self_cpu_time={} cpu_time={}  self_cuda_time={} cuda_time={} input_shapes={} cpu_memory_usage={} cuda_memory_usage={}>)
rJ   r=   r³   r¯   r´   r°   r€   r‹   r»   r¼   r   r   r   r   rÌ   G  s    öÿzFunctionEventAvg.__repr__N)r–   r—   r˜   r™   r   r1   rÏ   rÌ   r   r   r   r   r‘   	  s
   "r‘   c                   @   s   e Zd Zdd„ ZdS )ÚStringTablec                 C   s(   t |ƒdkrtj |¡n|| |< | | S rP   )r,   ÚtorchZ_CZ	_demangle)r   r=   r   r   r   Ú__missing__Y  s     zStringTable.__missing__N)r–   r—   r˜   rÒ   r   r   r   r   rÐ   X  s   rÐ   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚMemRecordsAccz<Acceleration structure for accessing mem_records in intervalc                 C   sH   || _ g | _g | _t|ƒdkrDtdd„ t|ƒD ƒƒ}t|Ž \| _| _d S )Nr   c                 S   s    g | ]\}}|d    ¡ |f‘qS ©r   )r¿   )r%   ÚiÚrr   r   r   r(   i  r)   z*MemRecordsAcc.__init__.<locals>.<listcomp>)Ú_mem_recordsÚ_start_usesÚ_indicesr,   rF   r2   Úzip)r   Zmem_recordsÚtmpr   r   r   r   d  s    zMemRecordsAcc.__init__c                 c   sB   t  | j|¡}t  | j|¡}t||ƒD ]}| j| j|  V  q&d S r!   )ÚbisectÚbisect_leftrØ   Úbisect_rightr+   r×   rÙ   )r   r¿   rÀ   Z	start_idxZend_idxrÕ   r   r   r   Úin_intervall  s    zMemRecordsAcc.in_intervalN)r–   r—   r˜   r™   r   rß   r   r   r   r   rÓ   a  s   rÓ   c                    s   g d¢}t ‡ fdd„|D ƒƒS )N))úautograd/__init__Z_make_grads)rà   Úbackward)ztorch/tensorrá   )ú_internal/common_utilsZprof_callable)râ   Zprof_func_call)râ   Zprof_meth_callc                    s&   g | ]}|d  ˆ v o|d ˆ v  ‘qS )r   r$   r   )r%   ru   ©r†   r   r   r(   |  r)   z'_filter_stack_entry.<locals>.<listcomp>)Úall)r†   Zfiltered_entriesr   rã   r   Ú_filter_stack_entrys  s    rå   z[memory]c                 C   s   t dddddg}| |v S )Nz profiler::_record_function_enterzprofiler::_record_function_exitzaten::is_leafzaten::output_nrzaten::_version)ÚMEMORY_EVENT_NAME)r.   Zfiltered_out_namesr   r   r   Ú_filter_name€  s    úrç   Fc                 C   s$   t ƒ }||  } |r |  d¡r d} | S )NzProfilerStep#zProfilerStep*)rÐ   Ú
startswith)r.   Zwith_wildcardZstring_tabler   r   r   Ú_rewrite_name�  s    
ré   r^   r_   c           +         sÌ  t | ƒdkrdS tdd„ | D ƒƒ}tdd„ | D ƒƒ}	tdd„ | D ƒƒ}
ˆdurptt| ‡fdd	„d
d�|||d�} d}tdd„ | D ƒƒd }t||ƒ}d}tdd„ | D ƒƒd }t||ƒ}d}|}d}g }| D ](}|jdurÌt |jƒdkrÌ| |j¡ qÌt |ƒdk}|�r(tdd„ |D ƒƒd }t||ƒ}g d¢}|�rD| g d¢¡ |�rl| ddg¡ |	�rl| ddg¡ | d¡ tdd„ | D ƒƒ}|�r˜| d¡ d‰ dg‰dg‰ˆ  g‰d}d8‡ ‡‡‡fd d!„	}d"d#„ }||ƒ |d$d… D ]}||ƒ �qä|
�r| d%¡ ||ƒ |�r(| d&¡ ||d'd(� |�r’g }| D ]}|j	dk�r6| |j	¡ �q6t |ƒdk�rŽ|t|ƒƒ\}}| d) 
|¡¡ ||ƒ nd*}ˆd }ˆd }ˆd } d}g ‰‡fd+d,„}!td-d„ | D ƒƒ}"d}#| D ]@}|jtjk�rþ|j�r|#|j7 }#n|jtjk�rØ|#|j7 }#�qØ|du�r8|!d.|  ƒ |!|ƒ |�rR|!d.|  ƒ |!d/ƒ |!|ƒ |!|j
|Ž ƒ |!|ƒ d0d1„ }$d}%| D �]}|%|k�r– �q�|�r®|jdu�r®�q€n|%d$7 }%|j}&t |&ƒ|d2 k�râ|&d|d2 … d3 }&|&t|j|"ƒ|j|j�st|j|"ƒnd|j|jg}'|�r:|' |jt|j|#ƒ|j|jg¡ |�rz|' t|jƒt|j ƒg¡ |	�rz|' t|j!ƒt|j"ƒg¡ |' |j#¡ |�r˜|' |j$¡ |
�r¶|' t%|j&ƒd|… ¡ |�rê|j	dk�rÔ|' d4¡ n|' d5 
|j	| ¡¡ |�rd}(t |jƒdk�r|$|jd |ƒ}(|' |(¡ |!|j
|'Ž ƒ |�r€dgt |ƒd$  })|jd$|… D ] }*|!|j
|)|$|*|ƒg Ž ƒ �qR|) d¡ |!|j
|)Ž ƒ �q€|!|ƒ |!d6 
t'|"ƒ¡ƒ |�rÂ|!d7 
t'|#ƒ¡ƒ d (ˆ¡S )9zVPrints a summary of events (which can be a list of FunctionEvent or FunctionEventAvg).r   r{   c                 S   s   g | ]}|j d k‘qS rÔ   )rx   r\   r   r   r   r(   ¥  r)   z _build_table.<locals>.<listcomp>c                 S   s   g | ]}|j d k‘qS rÔ   )rÆ   r\   r   r   r   r(   ¦  r)   c                 S   s$   g | ]}|j d uot|j ƒdk‘qS r‰   )r‹   r,   r\   r   r   r   r(   ¨  r)   Nc                    s
   t | ˆ ƒS r!   )r‚   rR   )r`   r   r   rA   ¬  r)   z_build_table.<locals>.<lambda>T)r=   Úreverser�   é7   c                 S   s   g | ]}t |jƒ‘qS r   )r,   r=   r:   r   r   r   r(   °  r)   é   éP   c                 S   s   g | ]}t t|jƒƒ‘qS r   )r,   r€   r‹   r:   r   r   r   r(   ´  r)   é   c                 S   s   g | ]}t d d„ |D ƒƒ‘qS )c                 S   s   g | ]}t |ƒ‘qS r   ©r,   )r%   r†   r   r   r   r(   Á  r)   z+_build_table.<locals>.<listcomp>.<listcomp>)Úmax)r%   rU   r   r   r   r(   Á  r)   )ÚNamez
Self CPU %zSelf CPUzCPU total %z	CPU totalzCPU time avg)z	Self CUDAzSelf CUDA %z
CUDA totalzCUDA time avgzCPU MemzSelf CPU MemzCUDA MemzSelf CUDA Memz
# of Callsc                 S   s   g | ]}|j d k‘qS )rE   )r>   r:   r   r   r   r(   á  r)   zNode IDrh   é   ú>c                    s\   ˆd  d| t | ƒ d dˆ   7  < ˆd  d|  dˆ   7  < ˆd  | ˆ  7  < d S )Nr   z{: Ú}r}   ú-)r€   )ÚpaddingÚtext_dir)ÚSPACING_SIZEÚheader_sep_lstÚline_length_lstÚrow_format_lstr   r   Ú
add_columnì  s    (z _build_table.<locals>.add_columnc                 S   sr   g d¢}| dksJ ‚t dtt | ¡d tt|ƒd ƒƒƒ}|dkrN|t|ƒk sRJ ‚tdt |¡d ƒ|t|ƒ fS )N)ZFLOPsZKFLOPsZMFLOPsZGFLOPsZTFLOPsZPFLOPsr   é   r$   é
   g      À)	rð   ÚminÚmathÚlog10Úfloatr,   ÚpowÚfloorrƒ   )r¾   Zflop_headersZ	log_flopsr   r   r   Úauto_scale_flopsñ  s
    &z&_build_table.<locals>.auto_scale_flopsr$   zInput ShapeszSource Locationú<)r÷   zTotal {}Fc                    s   ˆ   | ¡ ˆ   d¡ d S )Nr~   )rL   )Ús)Úresultr   r   rL      s    
z_build_table.<locals>.appendc                 S   s   g | ]
}|j ‘qS r   rZ   r\   r   r   r   r(   $  r)   ú=z1This report only display top-level ops statisticsc                 S   sD   t | ƒ|kr@t | ƒ| }| |d … } t | ƒdkr@d| dd …  } | S )Nrý   ú...rï   )rt   Úsrc_column_widthÚoffsetr   r   r   Ú	trim_path;  s    z_build_table.<locals>.trim_pathrý   r
  z--z{0:8.3f}zSelf CPU time total: {}zSelf CUDA time total: {})ró   ))r,   Úanyr
   rF   rð   rÿ   rU   rL   r4   r¾   rJ   r]   r8   r   r9   rŠ   rx   rË   r-   r=   r¡   r[   r³   r7   r«   r±   r¯   r´   r²   r°   r¥   r»   rÅ   r¼   rÆ   r®   r>   r€   r‹   rŸ   Újoin)+rM   r`   rc   ra   rb   r   r   rd   Zhas_cuda_timeZhas_cuda_memZhas_input_shapesZMAX_NAME_COLUMN_WIDTHZname_column_widthZMAX_SHAPES_COLUMN_WIDTHZshapes_column_widthZDEFAULT_COLUMN_WIDTHZflops_column_widthr  Zstacksr;   Z	has_stackÚheadersZappend_node_idZMAX_STACK_ENTRYrü   r  Ú_Z	raw_flopsZflops_scaleZflops_headerZ
row_formatZ
header_sepZline_lengthrL   Zsum_self_cpu_time_totalZsum_self_cuda_time_totalr  Zevent_limitr.   Z
row_valuesZ	src_fieldZempty_headersr†   r   )rø   rù   rú   r  rû   r`   r   re   ˜  sB   
ÿÿþ


þþÿ







ø

ûüüÿ

re   )F)NNr^   r_   FFF)#rG   rÑ   Ztorch.autogradr   Úcollectionsr   r   Úoperatorr   Útypingr   r   r   r	   rÜ   r   Úlistr
   rŸ   r¡   r¥   r§   Úobjectr¨   rµ   r¶   r¹   r‘   rÐ   rÓ   rå   ræ   rç   ré   re   r   r   r   r   Ú<module>   sD     $
	 O	

       ø