U
    E‹Ñh  ã                   @   s�   d dl Z d dlZd dlmZmZ d dlZddlmZ e jG dd„ dƒƒZ	G dd„ dƒZ
d	d
„ Zdd„ ZG dd„ dƒZejjee dœdd„ZdS )é    N)ÚAnyÚListé   )Ú
print_oncec                   @   sr   e Zd ZU dZeed< dZeed< dZeed< dZ	eed< d dœdd	„Z
d dœd
d„Zdd„ Zdd„ Zdd„ ZdS )ÚProfileMetricsg        Úmicrosecondsr   Ú	operatorsÚfusionsÚgraphs©Úotherc                 C   s4   |  j |j 7  _ |  j|j7  _|  j|j7  _| S ©N©r   r   r	   ©Úselfr   © r   úD/tmp/user/2202/pip-unpacked-wheel-bf9o3bgk/torch/_dynamo/profiler.pyÚ__iadd__   s    zProfileMetrics.__iadd__c                 C   s2   t |tƒst‚t| j|j | j|j | j|j ƒS r   )Ú
isinstancer   ÚAssertionErrorr   r   r	   r   r   r   r   Ú__add__   s    


ýzProfileMetrics.__add__c                 C   sL   t |tƒrt|||ƒ}t| jtd|jƒ | jtd|jƒ | jtd|jƒ ƒS )Nr   )r   Úintr   r   Úmaxr   r	   r   r   r   r   Ú__truediv__    s    
ýzProfileMetrics.__truediv__c                 C   s   | j d›d| jd›d�S )Nz4.0%z ops z time©r   r   ©r   r   r   r   Ú__str__)   s    zProfileMetrics.__str__c                 C   s   | j | jgS r   r   r   r   r   r   Útocsv,   s    zProfileMetrics.tocsvN)Ú__name__Ú
__module__Ú__qualname__r   ÚfloatÚ__annotations__r   r   r	   r
   r   r   r   r   r   r   r   r   r   r      s   
	r   c                   @   s:   e Zd Zdd„ Zd dœdd„Zdd„ Zdd	„ Zd
d„ ZdS )ÚProfileResultc                 C   s"   |pt ƒ | _|pt ƒ | _|| _d S r   )r   ÚcapturedÚtotalÚunique_graphs)r   r$   r%   r&   r   r   r   Ú__init__1   s    zProfileResult.__init__r   c                 C   s4   |  j |j 7  _ |  j|j7  _|  j|j7  _| S r   ©r$   r%   r&   r   r   r   r   r   6   s    zProfileResult.__iadd__c                 C   s   | j | j S r   )r$   r%   r   r   r   r   Úpercent<   s    zProfileResult.percentc                 C   s>   | j d›d| jjd›d| jjd›d| jjd›d�t|  ¡ ƒ S )NÚ2z graphs z graph calls Ú4ú/z = )r&   r$   r
   r   r%   Ústrr)   r   r   r   r   r   ?   s    0
þÿzProfileResult.__str__c                 C   s&   | j | jj| jj| jjg|  ¡  ¡  S r   )r&   r$   r
   r   r%   r)   r   r   r   r   r   r   F   s    ü
ûzProfileResult.tocsvN)r   r   r    r'   r   r)   r   r   r   r   r   r   r#   0   s
   r#   c                   C   s   t j d¡dkS )NZTORCHDYNAMO_PRINT_MISSINGÚ1)ÚosÚenvironÚgetr   r   r   r   Úshould_print_missingO   s    r2   c                 C   s@   t dd„ | D ƒƒrd S dd„ | D ƒ} tdd | dd … ¡ƒ d S )Nc                 s   s   | ]}d |kV  qdS )z/torch/autograd/profiler.pyNr   ©Ú.0Úxr   r   r   Ú	<genexpr>T   s     z print_missing.<locals>.<genexpr>c                 S   s    g | ]}d |krd|kr|‘qS )z	<built-inzsite-packages/torch/r   r3   r   r   r   Ú
<listcomp>V   s      z!print_missing.<locals>.<listcomp>ÚMISSINGz >> éýÿÿÿ)Úanyr   Újoin)Ústackr   r   r   Úprint_missingS   s    ÿr=   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚProfilerr   c                 C   s    t jjt jjjgtƒ d�| _d S )N)Z
activitiesZ
with_stack)ÚtorchÚprofilerZprofileZProfilerActivityZCPUr2   Úprofr   r   r   r   r'   _   s    
þzProfiler.__init__c                 C   s   d}d}d}d}d}d}d}t | j ¡ dd„ d�}|D ]ˆ}	|	jdkr^|	jj}|d7 }|d8 }q6|	jj|kr6|	jj}|	jj|kr–|d7 }||	j ¡ 7 }ntƒ r¦t	|	j
ƒ |d7 }||	j ¡ 7 }q6q6tj}
dt_|d8 }tt|||| |d�t|||d d	�|
d
�S )Nr   éÿÿÿÿc                 S   s   | j jS r   )Ú
time_rangeÚstart)r5   r   r   r   Ú<lambda>n   ó    z"Profiler.results.<locals>.<lambda>)ÚkeyÚTORCHDYNAMOr   )r   r   r	   r
   r   r(   )ÚsortedrA   ÚeventsÚnamerC   ÚendrD   Z
elapsed_usr2   r=   r<   r>   r&   r#   r   )r   Zcaptured_regionsZcaptured_opsZcaptured_microsecondsZ	total_opsZtotal_microsecondsZlast_op_end_timeZcaptured_region_end_timerJ   Úer&   r   r   r   Úresultse   sP    


üýôzProfiler.resultsN)r   r   r    r&   r'   rN   r   r   r   r   r>   \   s   r>   )ÚgmÚexample_inputsc                    s   ‡ fdd„}t  jd7  _|S )Nc               
      s.   t j d¡� ˆ j| Ž W  5 Q R £ S Q R X d S )NrH   )r?   r@   Zrecord_functionZforward)Úargs©rO   r   r   Ú_wrapped—   s    z%fx_insert_profiling.<locals>._wrappedr   )r>   r&   )rO   rP   rS   r   rR   r   Úfx_insert_profiling–   s    rT   )Zdataclassesr/   Útypingr   r   r?   Úutilsr   Z	dataclassr   r#   r2   r=   r>   ZfxZGraphModulerT   r   r   r   r   Ú<module>   s   $	: