
    VhO                        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             Z G d	 d
      ZdefdZdee   ddfdZ G d d      Zdej&                  j(                  dee   def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.
    N)Any)Self   )
print_oncec                       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e
de
fd	Zdd
Zdedd fdZdefdZdee   fdZy)ProfileMetricsg        microsecondsr   	operatorsfusionsgraphsotherreturnc                     | xj                   |j                   z  c_         | xj                  |j                  z  c_        | xj                  |j                  z  c_        | S Nr	   r
   r   selfr   s     F/home/dcms/DCMS/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   intr   r	   maxr
   r   r   s     r   __truediv__zProfileMetrics.__truediv__.   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__7   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   r   r   )__name__
__module____qualname__r	   float__annotations__r
   r   r   r   r   r   r   r   r   strr!   listr#    r   r   r   r      ss    L%IsGSFCOd t 

 
)9 
J J3tE{ 3r   r   c                   ^    e Zd ZdedededdfdZdedefdZdefd	Zde	fd
Z
dee   fdZy)ProfileResultcapturedtotalunique_graphsr   Nc                 ^    |xs
 t               | _        |xs
 t               | _        || _        y r   )r   r.   r/   r0   )r   r.   r/   r0   s       r   __init__zProfileResult.__init__?   s*     )1(DN4D%*%>n.>
"/r   r   c                     | xj                   |j                   z  c_         | xj                  |j                  z  c_        | xj                  |j                  z  c_        | S r   r.   r/   r0   r   s     r   r   zProfileResult.__iadd__F   sB    '

ekk!
e111r   c                 4    | j                   | j                  z  S r   )r.   r/   r    s    r   percentzProfileResult.percentL   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 = )r0   r.   r   r
   r/   r)   r6   r    s    r   r!   zProfileResult.__str__O   sj    !!!$HT]]-A-A!,DM}}&&q)4::+?+?*B#G$,,.!"	
r   c                     | j                   | j                  j                  | j                  j                  | j                  j                  g| j                         j                         z   S r   )r0   r.   r   r
   r/   r6   r#   r    s    r   r#   zProfileResult.tocsvV   sU    MM  MM##JJ  	

 LLN  "# 	#r   )r$   r%   r&   r   r   r2   r   r   r6   r)   r!   r*   r   r#   r+   r   r   r-   r-   >   sb    0&0/=0NQ0	0d t * *
 
#tCy #r   r-   r   c                  F    t         j                  j                  d      dk(  S )NTORCHDYNAMO_PRINT_MISSING1)osenvirongetr+   r   r   should_print_missingrB   _   s    ::>>56#==r   stackc                     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>d   s     
=!(A-
=s   z	<built-inzsite-packages/torch/MISSINGz >> )anyr   join)rC   rG   s     r   print_missingrM   c   s]    

=u
==[16LTU6UE  y&++eBCj12s   	AAAc                   0    e Zd ZU dZeed<   ddZdefdZy)Profilerr   r0   r   Nc                     t         j                  j                  t         j                  j                  j                  gt                     | _        y )N)
activities
with_stack)torchprofilerprofileProfilerActivityCPUrB   profr    s    r   r2   zProfiler.__init__o   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)rG   s    r   <lambda>z"Profiler.results.<locals>.<lambda>~   s    !,,:L:L r   )keyTORCHDYNAMOr   )r	   r
   r   r   r   r4   )sortedrX   eventsnamer\   endr]   
elapsed_usrB   rM   rC   rO   r0   r-   r   )r   captured_regionscaptured_opscaptured_microseconds	total_opstotal_microsecondslast_op_end_timecaptured_region_end_timerb   er0   s              r   resultszProfiler.resultsu   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   )r   N)	r$   r%   r&   r0   r   r(   r2   r-   rn   r+   r   r   rO   rO   l   s    M3
.
 .
r   rO   gmexample_inputsc                 ^     dt         dt         f fd}t        xj                  dz  c_        |S )Nargsr   c                      t         j                  j                  d      5   j                  |  cd d d        S # 1 sw Y   y xY w)Nr`   )rS   rT   record_functionforward)rr   ro   s    r   _wrappedz%fx_insert_profiling.<locals>._wrapped   s8    ^^++M: 	%2::t$	% 	% 	%s	   :Ar   )r   rO   r0   )ro   rp   rv   s   `  r   fx_insert_profilingrw      s.    % % % aOr   )__doc__dataclassesr?   typingr   typing_extensionsr   rS   utilsr   	dataclassr   r-   boolrB   r*   r)   rM   rO   fxGraphModulerw   r+   r   r   <module>r      s     	  "   !3 !3 !3H# #B>d >3c 3t 37
 7
tEHH00 $s) PS r   