U
    ªÊ¹jA  ã                   @   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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   úU/var/www/html/bwave/python/venv/lib/python3.8/site-packages/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e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__0   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   5   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   E   s    ü
ûzProfileResult.tocsvN)	r   r   r    r'   r   r   r)   r   r   r   r   r   r   r#   /   s
   r#   c                   C   s   t j d¡dkS )NZTORCHDYNAMO_PRINT_MISSINGÚ1)ÚosÚenvironÚgetr   r   r   r   Úshould_print_missingN   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>S   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>U   s      z!print_missing.<locals>.<listcomp>ÚMISSINGz >> éýÿÿÿ)Úanyr   Újoin)Ústackr   r   r   Úprint_missingR   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>m   ó    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   Úresultsd   sP    


üýôzProfiler.resultsN)r   r   r    r&   r'   rN   r   r   r   r   r>   [   s   r>   c                 C   s   | rdd„ | D ƒS d S )Nc                 S   s   g | ]}t t|d g ƒƒ‘qS )Úshape)ÚtupleÚgetattrr3   r   r   r   r7   —   s     zshapes_of.<locals>.<listcomp>r   )Úitr   r   r   Ú	shapes_of•   s    rS   )ÚgmÚexample_inputsc                    s@   t |ƒ‰d ‰‡‡‡fdd„‰ ‡ ‡‡‡fdd„}t jd7  _|S )Nc                    s    ˆ j  ¡  dˆ› dˆ› d| › �S )Nzshape mismatch in=z out=z got=)ÚgraphZprint_tabular)Úextra)rT   Úinput_shapesÚoutput_shapesr   r   Údebug_printž   s    
z(fx_insert_profiling.<locals>.debug_printc               
      s    t j d¡�Š t| ƒˆks<tdd„ ˆD ƒƒs<tˆ t| ƒƒƒ‚ˆj| Ž }ˆd krXt|ƒ‰n.t|ƒˆks†tdd„ ˆD ƒƒs†tˆ t|ƒƒƒ‚|W  5 Q R £ S Q R X d S )NrH   c                 s   s   | ]}t |ƒV  qd S r   ©Zfree_symbols©r4   Úsr   r   r   r6   ©   s    z8fx_insert_profiling.<locals>._wrapped.<locals>.<genexpr>c                 s   s   | ]}t |ƒV  qd S r   r[   r\   r   r   r   r6   °   s    )r?   r@   Zrecord_functionrS   r:   r   Úforward)ÚargsÚresult©rZ   rT   rX   rY   r   r   Ú_wrapped¢   s    ÿ

þ

ÿ

þz%fx_insert_profiling.<locals>._wrappedr   )rS   r>   r&   )rT   rU   rb   r   ra   r   Úfx_insert_profilingš   s    rc   )Údataclassesr/   Útypingr   r   r?   Úutilsr   Ú	dataclassr   r#   r2   r=   r>   rS   ZfxZGraphModulerc   r   r   r   r   Ú<module>   s   $	: