
    קg,                         d dl Z d dlZd dlmZmZ d dlZddlmZ e j         G d d                      Z	 G d d          Z
d	 Zd
 Z G d d          Zdej        j        dee         fdZdS )    N)AnyList   )
print_oncec                   p    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dZ
dd	Zd
 ZdefdZd ZdS )ProfileMetricsg        microsecondsr   	operatorsfusionsgraphsotherc                     | xj         |j         z  c_         | xj        |j        z  c_        | xj        |j        z  c_        | S Nr	   r
   r   selfr   s     R/var/www/html/ai-engine/env/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   returnc                 *    | j         dd| j        ddS )Nz4.0%z ops z timer
   r	   r   s    r   __str__zProfileMetrics.__str__)   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   )__name__
__module____qualname__r	   float__annotations__r
   r   r   r   r   r   r   strr!   r#    r   r   r   r      s         L%IsGSFCOOO   
 
 
 

 
 
J J J J J3 3 3 3 3r   r   c                   6    e Zd Zd	dZd
dZd ZdefdZd ZdS )ProfileResultr   Nc                 h    |pt                      | _        |pt                      | _        || _        d S r   )r   capturedtotalunique_graphs)r   r.   r/   r0   s       r   __init__zProfileResult.__init__1   s4    (0(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__6   sB    '

ek!

e11r   c                      | j         | j        z  S r   )r.   r/   r    s    r   percentzProfileResult.percent<   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)   r5   r    s    r   r!   zProfileResult.__str__?   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/   r5   r#   r    s    r   r#   zProfileResult.tocsvF   sH    M M#J 	

 LLNN  ""# 	#r   r   N)r   r,   )	r$   r%   r&   r1   r   r5   r)   r!   r#   r*   r   r   r,   r,   0   su        0 0 0 0
   * * *
 
 
 
 
# # # # #r   r,   c                  H    t           j                            d          dk    S )NTORCHDYNAMO_PRINT_MISSING1)osenvirongetr*   r   r   should_print_missingrB   O   s    :>>566#==r   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>T   s(      
=
=!(A-
=
=
=
=
=
=r   c                 "    g | ]}d |vd|v
|S )z	<built-inzsite-packages/torch/r*   rE   s     r   
<listcomp>z!print_missing.<locals>.<listcomp>V   s5       [116LTU6U6U6U6U6Ur   MISSINGz >> )anyr   join)stacks    r   print_missingrP   S   sp    

=
=u
=
=
===    E y&++eBCCj1122222r   c                        e Zd ZdZddZd ZdS )Profilerr   r   Nc                     t           j                            t           j        j        j        gt                                | _        d S )N)
activities
with_stack)torchprofilerprofileProfilerActivityCPUrB   profr    s    r   r1   zProfiler.__init___   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)rG   s    r   <lambda>z"Profiler.results.<locals>.<lambda>n   s    !,:L r   )keyTORCHDYNAMOr   )r	   r
   r   r   r   r3   )sortedr[   eventsnamer_   endr`   
elapsed_usrB   rP   rO   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.resultse   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;   )r$   r%   r&   r0   r1   rq   r*   r   r   rR   rR   \   s<        M
 
 
 
.
 .
 .
 .
 .
r   rR   gmexample_inputsc                 <      fd}t           xj        dz  c_        |S )Nc                      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)argsrr   s    r   _wrappedz%fx_insert_profiling.<locals>._wrapped   s    ^++M:: 	% 	%2:t$	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	%s   
8<<r   )rR   r0   )rr   rs   ry   s   `  r   fx_insert_profilingrz      s7    % % % % % aOr   )dataclassesr?   typingr   r   rV   utilsr   	dataclassr   r,   rB   rP   rR   fxGraphModulerz   r*   r   r   <module>r      s*       				                !3 !3 !3 !3 !3 !3 !3 !3H# # # # # # # #>> > >3 3 37
 7
 7
 7
 7
 7
 7
 7
tEH0 $s)      r   