
    im                        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	             Z G d
 d      ZddZddZ G d d      ZddZy)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                  n    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	Zdd
Z	ddZ
ddZddZy)ProfileMetricsg        floatmicrosecondsr   int	operatorsfusionsgraphsc                    | xj                   |j                   z  c_         | xj                  |j                  z  c_        | xj                  |j                  z  c_        | S Nr   r   r   selfothers     P/var/www/html/engine/venv/lib/python3.12/site-packages/torch/_dynamo/profiler.py__iadd__zProfileMetrics.__iadd__"   sB    U///%//)%    c                    t        |t              sJ t        | j                  |j                  z   | j                  |j                  z   | j                  |j                  z         S r   )
isinstancer	   r   r   r   r   s     r   __add__zProfileMetrics.__add__(   sS    %000 2 22NNU__,LL5==(
 	
r   c           	        t        |t              rt        |||      }t        | j                  t	        d|j                        z  | j
                  t	        d|j
                        z  | j                  t	        d|j                        z        S )Nr   )r   r   r	   r   maxr   r   r   s     r   __truediv__zProfileMetrics.__truediv__0   sp    eS!"5%7EAu'9'9 ::NNSEOO44LL3q%--00
 	
r   c                >    | j                   dd| j                  ddS )Nz4.0%z ops z timer   r   r   s    r   __str__zProfileMetrics.__str__9   s%    ..&eD,=,=d+C5IIr   c                2    | j                   | j                  gS r   r    r!   s    r   tocsvzProfileMetrics.tocsv<   s     1 122r   Nr   r   returnr   )r   r	   r&   r	   )r   r   r&   r	   r&   str)r&   zlist[float])__name__
__module____qualname__r   __annotations__r   r   r   r   r   r   r"   r$    r   r   r	   r	      sC    L%IsGSFCO

J3r   r	   c                  D    e Zd Z	 	 	 	 	 	 	 	 ddZddZd	dZd
dZddZy)ProfileResultc                ^    |xs
 t               | _        |xs
 t               | _        || _        y r   )r	   capturedtotalunique_graphs)r   r1   r2   r3   s       r   __init__zProfileResult.__init__A   s*     )1(DN4D%*%>n.>
"/r   c                    | xj                   |j                   z  c_         | xj                  |j                  z  c_        | xj                  |j                  z  c_        | S r   r1   r2   r3   r   s     r   r   zProfileResult.__iadd__H   sB    '

ekk!
e111r   c                4    | j                   | j                  z  S r   )r1   r2   r!   s    r   percentzProfileResult.percentN   s    }}tzz))r   c                    | j                   dd| j                  j                  dd| j                  j                  dd| j                  j                  ddt        | j                               z   S )N2z graphs z graph calls 4/z = )r3   r1   r   r   r2   r(   r8   r!   s    r   r"   zProfileResult.__str__Q   sj    !!!$HT]]-A-A!,DM}}&&q)4::+?+?*B#G$,,.!"	
r   c                    | j                   | j                  j                  | j                  j                  | j                  j                  g| j                         j                         z   S r   )r3   r1   r   r   r2   r8   r$   r!   s    r   r$   zProfileResult.tocsvX   sU    MM  MM##JJ  	

 LLN  "# 	#r   N)r1   r	   r2   r	   r3   r   r&   Noner%   )r&   r	   r'   )r&   	list[Any])r)   r*   r+   r4   r   r8   r"   r$   r-   r   r   r/   r/   @   s;    0&0/=0NQ0	0*
#r   r/   c                 F    t         j                  j                  d      dk(  S )NTORCHDYNAMO_PRINT_MISSING1)osenvirongetr-   r   r   should_print_missingrF   a   s    ::>>56#==r   c                    t        d | D              ry | D cg c]  }d|vsd|vs| } }t        ddj                  | dd               y c c}w )Nc              3  $   K   | ]  }d |v  
 yw)z/torch/autograd/profiler.pyNr-   ).0xs     r   	<genexpr>z print_missing.<locals>.<genexpr>f   s     
=!(A-
=s   z	<built-inzsite-packages/torch/MISSINGz >> )anyr   join)stackrJ   s     r   print_missingrQ   e   s]    

=u
==[16LTU6UE  y&++eBCj12s   	AAAc                  ,    e Zd ZU dZded<   ddZddZy)	Profilerr   r   r3   c                    t         j                  j                  t         j                  j                  j                  gt                     | _        y )N)
activities
with_stack)torchprofilerprofileProfilerActivityCPUrF   profr!   s    r   r4   zProfiler.__init__q   s9    NN**77;;<+- + 
	r   c           	        d}d}d}d}d}d}d}t        | j                  j                         d       }|D ]  }	|	j                  dk(  r!|	j                  j
                  }|dz  }|dz  }3|	j                  j                  |k\  r|	j                  j
                  }|	j                  j
                  |k  r#|dz  }||	j                  j                         z  }nt               rt        |	j                         |dz  }||	j                  j                         z  } t        j                  }
dt        _        |dz  }t        t        ||||z
  |      t        |||dz
        |
	      S )
Nr   c                .    | j                   j                  S r   )
time_rangestart)rJ   s    r   <lambda>z"Profiler.results.<locals>.<lambda>   s    !,,:L:L r   )keyTORCHDYNAMOr   )r   r   r   r   r   r6   )sortedr\   eventsnamer`   endra   
elapsed_usrF   rQ   rP   rS   r3   r/   r	   )r   captured_regionscaptured_opscaptured_microseconds	total_opstotal_microsecondslast_op_end_timecaptured_region_end_timerf   er3   s              r   resultszProfiler.resultsw   sr    !	#% 		((*0LM 	Avv&+,<<+;+;( A% Q	##'77#$<<#3#3 <<##'?? A%L)Q\\-D-D-FF))+!!''*Q	"all&=&=&??"!	$ !..!"Q	#2&$'77'	 !/#!A
 (
 	
r   N)r&   r>   )r&   r/   )r)   r*   r+   r3   r,   r4   rr   r-   r   r   rS   rS   n   s    M3
.
r   rS   c                F     d fd}t         xj                  dz  c_        |S )Nc                     t         j                  j                  d      5   j                  |  cd d d        S # 1 sw Y   y xY w)Nrd   )rW   rX   record_functionforward)argsgms    r   _wrappedz%fx_insert_profiling.<locals>._wrapped   s8    ^^++M: 	%2::t$	% 	% 	%s	   :Ar   )rw   r   r&   r   )rS   r3   )rx   example_inputsry   s   `  r   fx_insert_profilingr{      s     % aOr   )r&   bool)rP   z	list[str]r&   r>   )rx   ztorch.fx.GraphModulerz   r?   r&   r   )__doc__
__future__r   dataclassesrC   typingr   typing_extensionsr   rW   utilsr   	dataclassr	   r/   rF   rQ   rS   r{   r-   r   r   <module>r      sf    #  	  "   !3 !3 !3H# #B>37
 7
tr   