
    Diim                        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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                  p    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dS )ProfileMetricsg        floatmicrosecondsr   int	operatorsfusionsgraphsotherr   returnc                    | xj         |j         z  c_         | xj        |j        z  c_        | xj        |j        z  c_        | S Nr   r   r   selfr   s     \/var/www/html/bet.cuttalo.com/ml/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   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   s{    eS!! 	8"5%77EAu'9 : ::NSEO444L3q%-000
 
 	
r   strc                *    | 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$    .IIID,=IIIIIr   list[float]c                    | j         | j        gS r   r"   r#   s    r   tocsvzProfileMetrics.tocsv<   s     122r   N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	      s         LIGFOOOO   
 
 
 

 
 
 
J J J J3 3 3 3 3 3r   r	   c                  6    e Zd ZddZddZddZddZddZdS )ProfileResultcapturedr	   totalunique_graphsr   r   Nonec                h    |pt                      | _        |pt                      | _        || _        d S r   )r	   r1   r2   r3   )r   r1   r2   r3   s       r   __init__zProfileResult.__init__A   s6     )1(DN4D4D%*%>n.>.>
"/r   r   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    '

ek!

e11r   c                     | j         | j        z  S r   )r1   r2   r#   s    r   percentzProfileResult.percentN   s    }tz))r   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 = )r3   r1   r   r   r2   r    r:   r#   s    r   r$   zProfileResult.__str__Q   s~    !R G GT]-AR G G}&FG G+/:+?FG G G$,,..!!"	
r   	list[Any]c                    | j         | j        j        | j        j        | j        j        g|                                                                 z   S r   )r3   r1   r   r   r2   r:   r'   r#   s    r   r'   zProfileResult.tocsvX   sH    M M#J 	

 LLNN  ""# 	#r   N)r1   r	   r2   r	   r3   r   r   r4   r(   )r   r	   r)   )r   r?   )r*   r+   r,   r6   r   r:   r$   r'   r.   r   r   r0   r0   @   sx        0 0 0 0   * * * *
 
 
 
# # # # # #r   r0   r   boolc                 H    t           j                            d          dk    S )NTORCHDYNAMO_PRINT_MISSING1)osenvirongetr.   r   r   should_print_missingrH   a   s    :>>566#==r   stack	list[str]r4   c                    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>f   s(      
=
=!(A-
=
=
=
=
=
=r   c                "    g | ]}d |vd|v
|S )z	<built-inzsite-packages/torch/r.   rM   s     r   
<listcomp>z!print_missing.<locals>.<listcomp>h   s5       [116LTU6U6U6U6U6Ur   MISSINGz >> )anyr   join)rI   s    r   print_missingrW   e   sp    

=
=u
=
=
===    E y&++eBCCj1122222r   c                  .    e Zd ZU dZded<   d
dZddZd	S )Profilerr   r   r3   r   r4   c                    t           j                            t           j        j        j        gt                                | _        d S )N)
activities
with_stack)torchprofilerprofileProfilerActivityCPUrH   profr#   s    r   r6   zProfiler.__init__q   s;    N**7;<+-- + 
 
			r   r0   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)rO   s    r   <lambda>z"Profiler.results.<locals>.<lambda>   s    !,:L r   )keyTORCHDYNAMOr   )r   r   r   r   r   r8   )sortedrb   eventsnamerf   endrg   
elapsed_usrH   rW   rI   rY   r3   r0   r	   )r   captured_regionscaptured_opscaptured_microseconds	total_opstotal_microsecondslast_op_end_timecaptured_region_end_timerl   er3   s              r   resultszProfiler.resultsw   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   N)r   r4   )r   r0   )r*   r+   r,   r3   r-   r6   rx   r.   r   r   rY   rY   n   sO         M
 
 
 
.
 .
 .
 .
 .
 .
r   rY   gmtorch.fx.GraphModuleexample_inputsr?   r   c                >     d fd}t           xj        dz  c_        |S )Nargsr   r   c                     t           j                            d          5   j        |  cd d d            S # 1 swxY w Y   d S )Nrj   )r]   r^   record_functionforward)r}   ry   s    r   _wrappedz%fx_insert_profiling.<locals>._wrapped   s    ^++M:: 	% 	%2:t$	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	%s   
8<<r   )r}   r   r   r   )rY   r3   )ry   r{   r   s   `  r   fx_insert_profilingr      s<    % % % % % % aOr   )r   rA   )rI   rJ   r   r4   )ry   rz   r{   r?   r   r   )__doc__
__future__r   dataclassesrE   typingr   typing_extensionsr   r]   utilsr   	dataclassr	   r0   rH   rW   rY   r   r.   r   r   <module>r      sH    # " " " " "     				       " " " " " "        !3 !3 !3 !3 !3 !3 !3 !3H# # # # # # # #B> > > >3 3 3 37
 7
 7
 7
 7
 7
 7
 7
t     r   