
    ^j"                        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     a/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/torch/_dynamo/profiler.py__iadd__zProfileMetrics.__iadd__"   sB    U///%//)%    c                    t        |t              st        dt        |             t        | j                  |j                  z   | j
                  |j
                  z   | j                  |j                  z         S )NzExpected ProfileMetrics, got )
isinstancer	   AssertionErrortyper   r   r   r   s     r   __add__zProfileMetrics.__add__(   sd    %0 #@e!NOO 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__1   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__<   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   r3   r4   r5   s       r   __init__zProfileResult.__init__D   s*     )1(DN4D%*%>n.>
"/r   c                    | xj                   |j                   z  c_         | xj                  |j                  z  c_        | xj                  |j                  z  c_        | S r   r3   r4   r5   r   s     r   r   zProfileResult.__iadd__K   sB    '

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

 LLN  "# 	#r   N)r3   r	   r4   r	   r5   r   r(   Noner'   )r(   r	   r)   )r(   	list[Any])r+   r,   r-   r6   r   r:   r$   r&   r/   r   r   r1   r1   C   s;    0&0/=0NQ0	0*
#r   r1   c                 F    t         j                  j                  d      dk(  S )NTORCHDYNAMO_PRINT_MISSING1)osenvirongetr/   r   r   should_print_missingrH   d   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>i   s     
=!(A-
=s   z	<built-inzsite-packages/torch/MISSINGz >> )anyr   join)stackrL   s     r   print_missingrS   h   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   r5   c                    t         j                  j                  t         j                  j                  j                  gt                     | _        y )N)
activities
with_stack)torchprofilerprofileProfilerActivityCPUrH   profr#   s    r   r6   zProfiler.__init__t   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)rL   s    r   <lambda>z"Profiler.results.<locals>.<lambda>   s    !,,:L:L r   )keyTORCHDYNAMOr   )r   r   r   r   r   r8   )sortedr^   eventsnamerb   endrc   
elapsed_usrH   rS   rR   rU   r5   r1   r	   )r   captured_regionscaptured_opscaptured_microseconds	total_opstotal_microsecondslast_op_end_timecaptured_region_end_timerh   er5   s              r   resultszProfiler.resultsz   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(   r1   )r+   r,   r-   r5   r.   r6   rt   r/   r   r   rU   rU   q   s    M3
.
r   rU   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)Nrf   )rY   rZ   record_functionforward)argsgms    r   _wrappedz%fx_insert_profiling.<locals>._wrapped   s8    ^^++M: 	%2::t$	% 	% 	%s	   :Ar   )ry   r   r(   r   )rU   r5   )rz   example_inputsr{   s   `  r   fx_insert_profilingr}      s     % aOr   )r(   bool)rR   z	list[str]r(   r@   )rz   ztorch.fx.GraphModuler|   rA   r(   r   )__doc__
__future__r   dataclassesrE   typingr   typing_extensionsr   rY   utilsr   	dataclassr	   r1   rH   rS   rU   r}   r/   r   r   <module>r      sf    #  	  "   $3 $3 $3N# #B>37
 7
tr   