a
    hO                     @   s   d Z ddlZddlZddlmZ ddlmZ ddlZddlm	Z	 ej
G dd dZG d	d
 d
ZedddZee ddddZG dd dZejjee edddZdS )aN  
Dynamo profiling implementation.

This module provides profiling functionality for Dynamo, including:
- ProfileMetrics: Class for collecting and aggregating performance metrics like
  execution time, operator counts, and fusion statistics
- ProfileResult: Class for analyzing and reporting profiling results
- Utilities for tracking missed/uncaptured operations
- Functions for instrumenting FX graphs with profiling capabilities

The profiler helps measure and optimize the performance of Dynamo-compiled code
by tracking both captured and total operations, timing, and graph statistics.
    N)Any)Self   )
print_oncec                   @   s   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< e
e
ddd	Zd d dd
dZed dddZedddZee dddZdS )ProfileMetricsg        microsecondsr   	operatorsfusionsgraphsotherreturnc                 C   s4   |  j |j 7  _ |  j|j7  _|  j|j7  _| S Nr   r   r	   selfr    r   D/var/www/auris/lib/python3.9/site-packages/torch/_dynamo/profiler.py__iadd__    s    zProfileMetrics.__iadd__c                 C   s2   t |tsJ t| j|j | j|j | j|j S r   )
isinstancer   r   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__r   c                 C   s   | j dd| jddS )Nz4.0%z ops z timer   r   r   r   r   r   __str__7   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   strr   listr   r   r   r   r   r      s   
	r   c                   @   s^   e Zd ZeeeddddZeedddZedd	d
Ze	dddZ
ee dddZdS )ProfileResultN)capturedtotalunique_graphsr   c                 C   s"   |pt  | _|pt  | _|| _d S r   )r   r'   r(   r)   )r   r'   r(   r)   r   r   r   __init__?   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   F   s    zProfileResult.__iadd__r   c                 C   s   | j | j S r   )r'   r(   r   r   r   r   percentL   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(   r$   r,   r   r   r   r   r   O   s    
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   V   s    
zProfileResult.tocsv)r   r    r!   r   r   r*   r   r   r,   r$   r   r%   r   r   r   r   r   r   r&   >   s   r&   r   c                   C   s   t jddkS )NZTORCHDYNAMO_PRINT_MISSING1)osenvirongetr   r   r   r   should_print_missing_   s    r4   )stackr   c                 C   s@   t dd | D rd S dd | D } tdd| dd   d S )Nc                 s   s   | ]}d |v V  qdS )z/torch/autograd/profiler.pyNr   .0xr   r   r   	<genexpr>d       z print_missing.<locals>.<genexpr>c                 S   s    g | ]}d |vrd|vr|qS )z	<built-inzsite-packages/torch/r   r6   r   r   r   
<listcomp>f   s   z!print_missing.<locals>.<listcomp>MISSINGz >> )anyr   join)r5   r   r   r   print_missingc   s    r@   c                   @   s6   e Zd ZU dZeed< ddddZedddZdS )	Profilerr   r)   Nr   c                 C   s    t jjt jjjgt d| _d S )N)Z
activitiesZ
with_stack)torchprofilerZprofileZProfilerActivityZCPUr4   profr   r   r   r   r*   o   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 rt	|	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)r8   r   r   r   <lambda>~   r:   z"Profiler.results.<locals>.<lambda>)keyTORCHDYNAMOr   )r   r   r	   r
   r   r+   )sortedrD   eventsnamerF   endrG   Z
elapsed_usr4   r@   r5   rA   r)   r&   r   )r   Zcaptured_regionsZcaptured_opsZcaptured_microsecondsZ	total_opsZtotal_microsecondsZlast_op_end_timeZcaptured_region_end_timerL   er)   r   r   r   resultsu   sP    


zProfiler.results)	r   r    r!   r)   r   r#   r*   r&   rP   r   r   r   r   rA   l   s   
rA   )gmexample_inputsr   c                    s&   t t d fdd}t jd7  _|S )N)argsr   c                     s:   t jd  j|  W  d    S 1 s,0    Y  d S )NrJ   )rB   rC   Zrecord_functionZforward)rS   rQ   r   r   _wrapped   s    z%fx_insert_profiling.<locals>._wrappedr   )r   rA   r)   )rQ   rR   rU   r   rT   r   fx_insert_profiling   s    rV   )__doc__Zdataclassesr1   typingr   Ztyping_extensionsr   rB   utilsr   Z	dataclassr   r&   boolr4   r%   r$   r@   rA   ZfxZGraphModulerV   r   r   r   r   <module>   s   $!	: