o
    hA                     @   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dejjdee fddZdS )    N)AnyList   )
print_oncec                   @   sj   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	Z
dd
dZdd Zdd Zdd ZdS )ProfileMetricsg        microsecondsr   	operatorsfusionsgraphsotherc                 C   4   |  j |j 7  _ |  j|j7  _|  j|j7  _| S Nr   r   r	   selfr    r   L/var/www/html/ai/venv/lib/python3.10/site-packages/torch/_dynamo/profiler.py__iadd__      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__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+      zProfileMetrics.tocsvN)r   r   )__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ef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   r   r   r%   r&   r'   r   r   r   r   r   5   r   zProfileResult.__iadd__c                 C   s   | j | j S r   )r%   r&   r   r   r   r   percent;   r   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   
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 )NTORCHDYNAMO_PRINT_MISSING1)osenvirongetr   r   r   r   should_print_missingN   s   r4   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>S   s    z print_missing.<locals>.<genexpr>c                 S   s    g | ]}d |vrd|vr|qS )z	<built-inzsite-packages/torch/r   r5   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)
activities
with_stack)torchprofilerprofileProfilerActivityCPUr4   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 ]E}	|	jdkr/|	jj}|d7 }|d8 }q|	jj|kr_|	jj}|	jj|krK|d7 }||	j 7 }nt rSt	|	j
 |d7 }||	j 7 }q	 qtj}
dt_|d8 }tt|||| |dt|||d d	|
d
S )Nr   c                 S   s   | j jS r   )
time_rangestart)r7   r   r   r   <lambda>m   s    z"Profiler.results.<locals>.<lambda>)keyTORCHDYNAMOr   )r   r   r	   r
   r   r)   )sortedrH   eventsnamerJ   endrK   
elapsed_usr4   r?   r>   r@   r'   r$   r   )r   captured_regionscaptured_opscaptured_microseconds	total_opstotal_microsecondslast_op_end_timecaptured_region_end_timerP   er'   r   r   r   resultsd   sP   


zProfiler.resultsN)r   r    r!   r'   r(   r\   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getattrr5   r   r   r   r9      s    zshapes_of.<locals>.<listcomp>r   )itr   r   r   	shapes_of   s   ra   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=)graphprint_tabular)extra)rb   input_shapesoutput_shapesr   r   debug_print   s   
z(fx_insert_profiling.<locals>.debug_printc                     s   t jdF t| kstdd D sJ  t| j|  }d u r,t|nt|ksCtdd D sCJ  t||W  d    S 1 sOw   Y  d S )NrN   c                 s       | ]}t |V  qd S r   free_symbolsr6   sr   r   r   r8          
z8fx_insert_profiling.<locals>._wrapped.<locals>.<genexpr>c                 s   rj   r   rk   rm   r   r   r   r8      ro   )rC   rD   record_functionra   r<   forward)argsresultri   rb   rg   rh   r   r   _wrapped   s    





$z%fx_insert_profiling.<locals>._wrappedr   )ra   r@   r'   )rb   rc   ru   r   rt   r   fx_insert_profiling   s   rv   )dataclassesr1   typingr   r   rC   utilsr   	dataclassr   r$   r4   r?   r@   ra   fxGraphModulerv   r   r   r   r   <module>   s    $	: