o
    ^im                     @  s   d Z ddlm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dddZdddZG dd dZ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.
    )annotationsN)Any)Self   )
print_oncec                   @  sp   e Zd ZU dZded< dZded< dZded< dZded< dddZdddZ	dddZ
dddZdddZdS )ProfileMetricsg        floatmicrosecondsr   int	operatorsfusionsgraphsotherr   returnc                 C  4   |  j |j 7  _ |  j|j7  _|  j|j7  _| S Nr	   r   r   selfr    r   Q/var/www/html/RAG/RAG_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__r   c                 C  sL   t |trt|||}t| jtd|j | jtd|j | jtd|j S )Nr   )r   r
   r   r	   maxr   r   r   r   r   r   __truediv__0   s   
zProfileMetrics.__truediv__strc                 C  s   | j dd| jddS )Nz4.0%z ops z timer   r	   r   r   r   r   __str__9   s   zProfileMetrics.__str__list[float]c                 C  s   | j | jgS r   r   r   r   r   r   tocsv<      zProfileMetrics.tocsvNr   r   r   r   )r   r   r   r   )r   r   r   r   r   r   )r   r!   )__name__
__module____qualname__r	   __annotations__r   r   r   r   r   r   r    r"   r   r   r   r   r      s   
 



	r   c                   @  s>   e Zd Zddd	ZdddZdddZdddZdddZdS )ProfileResultcapturedr   totalunique_graphsr
   r   Nonec                 C  s"   |pt  | _|p
t  | _|| _d S r   )r   r+   r,   r-   )r   r+   r,   r-   r   r   r   __init__A   s   
zProfileResult.__init__r   r   c                 C  r   r   r+   r,   r-   r   r   r   r   r   H   r   zProfileResult.__iadd__c                 C  s   | j | j S r   )r+   r,   r   r   r   r   percentN   r#   zProfileResult.percentr   c                 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   r1   r   r   r   r   r    Q   s   
zProfileResult.__str__	list[Any]c                 C  s&   | j | jj| jj| jjg|    S r   )r-   r+   r   r   r,   r1   r"   r   r   r   r   r"   X   s   
zProfileResult.tocsvN)r+   r   r,   r   r-   r
   r   r.   r$   )r   r   r%   )r   r5   )r&   r'   r(   r/   r   r1   r    r"   r   r   r   r   r*   @   s    



r*   r   boolc                   C  s   t jddkS )NTORCHDYNAMO_PRINT_MISSING1)osenvirongetr   r   r   r   should_print_missinga   s   r<   stack	list[str]r.   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>f   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   r?   r   r   r   
<listcomp>h   s    z!print_missing.<locals>.<listcomp>MISSINGz >> )anyr   join)r=   r   r   r   print_missinge   s   rH   c                   @  s.   e Zd ZU dZded< dddZdd	d
ZdS )Profilerr   r
   r-   r   r.   c                 C  s    t jjt jjjgt d| _d S )N)
activities
with_stack)torchprofilerprofileProfilerActivityCPUr<   profr   r   r   r   r/   q   s   
zProfiler.__init__r*   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)rA   r   r   r   <lambda>   s    z"Profiler.results.<locals>.<lambda>)keyTORCHDYNAMOr   )r	   r   r   r   r   r0   )sortedrQ   eventsnamerS   endrT   
elapsed_usr<   rH   r=   rI   r-   r*   r   )r   captured_regionscaptured_opscaptured_microseconds	total_opstotal_microsecondslast_op_end_timecaptured_region_end_timerY   er-   r   r   r   resultsw   sP   


zProfiler.resultsN)r   r.   )r   r*   )r&   r'   r(   r-   r)   r/   re   r   r   r   r   rI   n   s   
 
rI   gmtorch.fx.GraphModuleexample_inputsr5   r   c                   s    d fdd}t  jd7  _|S )Nargsr   r   c                    s:   t jd  j|  W  d    S 1 sw   Y  d S )NrW   )rL   rM   record_functionforward)ri   rf   r   r   _wrapped   s   $z%fx_insert_profiling.<locals>._wrappedr   )ri   r   r   r   )rI   r-   )rf   rh   rm   r   rl   r   fx_insert_profiling   s   rn   )r   r6   )r=   r>   r   r.   )rf   rg   rh   r5   r   r   )__doc__
__future__r   dataclassesr9   typingr   typing_extensionsr   rL   utilsr   	dataclassr   r*   r<   rH   rI   rn   r   r   r   r   <module>   s    $
!
	: