
    {YhO                     
   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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.
    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dS )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     h/var/www/tools.fuzzalab.pt/emblema-extractor/venv/lib/python3.11/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__&   sP    %00000 22NU_,L5=(
 
 	
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__.   s{    eS!! 	8"5%77EAu'9 : ::NSEO444L3q%-0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$    .IIID,=IIIIIr   c                     | j         | j        gS r   r   r    s    r   tocsvzProfileMetrics.tocsv:   s     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      s         L%IsGSFCOOOd t    
 
 
 

 
)9 
 
 
 
J J J J J3tE{ 3 3 3 3 3 3r   r   c                   f    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dS )ProfileResultcapturedtotalunique_graphsr   Nc                 h    |pt                      | _        |pt                      | _        || _        d S r   )r   r.   r/   r0   )r   r.   r/   r0   s       r   __init__zProfileResult.__init__?   s6     )1(DN4D4D%*%>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    '

ek!

e11r   c                      | j         | j        z  S r   )r.   r/   r    s    r   percentzProfileResult.percentL   s    }tz))r   c                     | j         dd| j        j        dd| j        j        dd| j        j        ddt          |                                           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   s~    !R G GT]-AR G G}&FG G+/:+?FG G G$,,..!!"	
r   c                     | j         | j        j        | j        j        | j        j        g|                                                                 z   S r   )r0   r.   r   r
   r/   r6   r#   r    s    r   r#   zProfileResult.tocsvV   sH    M M#J 	

 LLNN  ""# 	#r   )r$   r%   r&   r   r   r2   r   r   r6   r)   r!   r*   r   r#   r+   r   r   r-   r-   >   s        0&0/=0NQ0	0 0 0 0d t    * * * * *
 
 
 
 
#tCy # # # # # #r   r-   r   c                  H    t           j                            d          dk    S )NTORCHDYNAMO_PRINT_MISSING1)osenvirongetr+   r   r   should_print_missingrB   _   s    :>>566#==r   stackc                     t          d | D                       rd S d | D             } t          dd                    | dd                               d S )Nc              3      K   | ]}d |v V  	dS )z/torch/autograd/profiler.pyNr+   .0xs     r   	<genexpr>z print_missing.<locals>.<genexpr>d   s(      
=
=!(A-
=
=
=
=
=
=r   c                 "    g | ]}d |vd|v
|S )z	<built-inzsite-packages/torch/r+   rF   s     r   
<listcomp>z!print_missing.<locals>.<listcomp>f   s5       [116LTU6U6U6U6U6Ur   MISSINGz >> )anyr   join)rC   s    r   print_missingrP   c   sp    

=
=u
=
=
===    E y&++eBCCj1122222r   c                   2    e Zd ZU dZeed<   ddZdefdZdS )Profilerr   r0   r   Nc                     t           j                            t           j        j        j        gt                                | _        d S )N)
activities
with_stack)torchprofilerprofileProfilerActivityCPUrB   profr    s    r   r2   zProfiler.__init__o   s;    N**7;<+-- + 
 
			r   c           	         d}d}d}d}d}d}d}t          | j                                        d           }|D ]}	|	j        dk    r|	j        j        }|dz  }|dz  }$|	j        j        |k    r|	j        j        }|	j        j        |k    r"|dz  }||	j                                        z  }n"t                      rt          |	j
                   |dz  }||	j                                        z  }t          j        }
dt          _        |dz  }t          t          ||||z
  |          t          |||dz
            |
	          S )
Nr   c                     | j         j        S r   )
time_rangestart)rH   s    r   <lambda>z"Profiler.results.<locals>.<lambda>~   s    !,:L r   )keyTORCHDYNAMOr   )r	   r
   r   r   r   r4   )sortedr[   eventsnamer_   endr`   
elapsed_usrB   rP   rC   rR   r0   r-   r   )r   captured_regionscaptured_opscaptured_microseconds	total_opstotal_microsecondslast_op_end_timecaptured_region_end_timere   er0   s              r   resultszProfiler.resultsu   s    !	#% 	((**0L0LMMM 	 	Av&&+,<+;( A% Q		#'777#$<#3 <#'??? A%L)Q\-D-D-F-FF)))++ +!!'***Q	"al&=&=&?&??"" .!"Q	#2&$'77'	   !/#!A  
 (
 
 
 	
r   )r   N)	r$   r%   r&   r0   r   r(   r2   r-   rq   r+   r   r   rR   rR   l   sU         M3
 
 
 
.
 .
 .
 .
 .
 .
 .
r   rR   gmexample_inputsc                 Z     dt           dt           f fd}t          xj        dz  c_        |S )Nargsr   c                      t           j                            d          5   j        |  cd d d            S # 1 swxY w Y   d S )Nrc   )rV   rW   record_functionforward)ru   rr   s    r   _wrappedz%fx_insert_profiling.<locals>._wrapped   s    ^++M:: 	% 	%2:t$	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	%s   
8<<r   )r   rR   r0   )rr   rs   ry   s   `  r   fx_insert_profilingrz      sJ    % % % % % % % % aOr   )__doc__dataclassesr?   typingr   typing_extensionsr   rV   utilsr   	dataclassr   r-   boolrB   r*   r)   rP   rR   fxGraphModulerz   r+   r   r   <module>r      sn        				       " " " " " "        !3 !3 !3 !3 !3 !3 !3 !3H# # # # # # # #B>d > > > >3c 3t 3 3 3 37
 7
 7
 7
 7
 7
 7
 7
tEH0 $s) PS      r   