
    [ThO                        S r SSKrSSKrSSKJr  SSKJr  SSKrSSKJ	r	  \R                   " S S5      5       r " S	 S
5      rS\4S jrS\\   SS4S jr " S S5      rS\R&                  R(                  S\\   S\4S jrg)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                       \ rS rSr% Sr\\S'   Sr\\S'   Sr	\\S'   Sr
\\S'   S\S	\4S
 jrSS jrS\S	S 4S jrS	\4S jrS	\\   4S jrSrg)ProfileMetrics   g        microsecondsr   	operatorsfusionsgraphsotherreturnc                     U =R                   UR                   -  sl         U =R                  UR                  -  sl        U =R                  UR                  -  sl        U $ Nr
   r   r   selfr   s     N/var/www/auris/envauris/lib/python3.13/site-packages/torch/_dynamo/profiler.py__iadd__ProfileMetrics.__iadd__    sB    U///%//)%    c                     [        U[        5      (       d   e[        U R                  UR                  -   U R                  UR                  -   U R                  UR                  -   5      $ r   )
isinstancer   r
   r   r   r   s     r   __add__ProfileMetrics.__add__&   sV    %0000 2 22NNU__,LL5==(
 	
r   c           	      $   [        U[        5      (       a  [        XU5      n[        U R                  [	        SUR                  5      -  U R
                  [	        SUR
                  5      -  U R                  [	        SUR                  5      -  5      $ )Nr   )r   intr   r
   maxr   r   r   s     r   __truediv__ProfileMetrics.__truediv__.   sq    eS!!"57EAu'9'9 ::NNSEOO44LL3q%--00
 	
r   c                 >    U R                   S SU R                  S S3$ )Nz4.0%z ops z timer   r
   r   s    r   __str__ProfileMetrics.__str__7   s%    ..&eD,=,=d+C5IIr   c                 2    U R                   U R                  /$ r   r#   r$   s    r   tocsvProfileMetrics.tocsv:   s     1 122r    N)r   r   r   r   )__name__
__module____qualname____firstlineno__r
   float__annotations__r   r   r   r   r   r   r   r   r    strr%   listr(   __static_attributes__r*   r   r   r   r      ss    L%IsGSFCOd t 

 
)9 
J J3tE{ 3r   r   c                   p    \ rS rSrS\S\S\SS4S jrS\S\4S	 jrS\4S
 jr	S\
4S jrS\\   4S jrSrg)ProfileResult>   capturedtotalunique_graphsr   Nc                 p    U=(       d
    [        5       U l        U=(       d
    [        5       U l        X0l        g r   )r   r7   r8   r9   )r   r7   r8   r9   s       r   __init__ProfileResult.__init__?   s(     )1(DN4D%*%>n.>
"/r   r   c                     U =R                   UR                   -  sl         U =R                  UR                  -  sl        U =R                  UR                  -  sl        U $ r   r7   r8   r9   r   s     r   r   ProfileResult.__iadd__F   sB    '

ekk!
e111r   c                 4    U R                   U R                  -  $ r   )r7   r8   r$   s    r   percentProfileResult.percentL   s    }}tzz))r   c                     U R                   S SU R                  R                  S SU R                  R                  S SU R                  R                  S S3[        U R                  5       5      -   $ )N2z graphs z graph calls 4/z = )r9   r7   r   r   r8   r1   rA   r$   s    r   r%   ProfileResult.__str__O   sj    !!!$HT]]-A-A!,DM}}&&q)4::+?+?*B#G$,,.!"	
r   c                     U R                   U R                  R                  U R                  R                  U R                  R                  /U R                  5       R                  5       -   $ r   )r9   r7   r   r   r8   rA   r(   r$   s    r   r(   ProfileResult.tocsvV   sU    MM  MM##JJ  	

 LLN  "# 	#r   r>   )r+   r,   r-   r.   r   r   r;   r   r   rA   r1   r%   r2   r   r(   r3   r*   r   r   r5   r5   >   sb    0&0/=0NQ0	0d t * *
 
#tCy #r   r5   r   c                  F    [         R                  R                  S5      S:H  $ )NTORCHDYNAMO_PRINT_MISSING1)osenvirongetr*   r   r   should_print_missingrP   _   s    ::>>56#==r   stackc                     [        S U  5       5      (       a  g U  Vs/ s H  nSU;  d  M  SU;  d  M  UPM     n n[        SSR                  U SS  5      5        g s  snf )Nc              3   ,   #    U  H
  nS U;   v   M     g7f)z/torch/autograd/profiler.pyNr*   ).0xs     r   	<genexpr> print_missing.<locals>.<genexpr>d   s     
=u!(A-us   z	<built-inzsite-packages/torch/MISSINGz >> )anyr   join)rQ   rU   s     r   print_missingr\   c   sd    

=u
===a[16LTU6U5 
  y&++eBCj12s   
AAAc                   <    \ rS rSr% Sr\\S'   S	S jrS\4S jr	Sr
g)
Profilerl   r   r9   r   Nc                     [         R                  R                  [         R                  R                  R                  /[        5       S9U l        g )N)
activities
with_stack)torchprofilerprofileProfilerActivityCPUrP   profr$   s    r   r;   Profiler.__init__o   s9    NN**77;;<+- + 
	r   c           	         SnSnSnSnSnSnSn[        U R                  R                  5       S S9nU H  n	U	R                  S:X  a"  U	R                  R
                  nUS-  nUS-  nM5  U	R                  R                  U:  a  U	R                  R
                  nU	R                  R
                  U::  a"  US-  nX9R                  R                  5       -  nO$[        5       (       a  [        U	R                  5        US-  nXYR                  R                  5       -  nM  M     [        R                  n
S[        l        US-  n[        [        UUX!-
  US9[        UUUS-
  S9U
S	9$ )
Nr   c                 .    U R                   R                  $ r   )
time_rangestart)rU   s    r   <lambda>"Profiler.results.<locals>.<lambda>~   s    !,,:L:Lr   )keyTORCHDYNAMOr   )r
   r   r   r   r   r>   )sortedrh   eventsnamerm   endrn   
elapsed_usrP   r\   rQ   r^   r9   r5   r   )r   captured_regionscaptured_opscaptured_microseconds	total_opstotal_microsecondslast_op_end_timecaptured_region_end_timert   er9   s              r   resultsProfiler.resultsu   si    !	#% 		((*0LMAvv&+,<<+;+;( A% Q	##'77#$<<#3#3 <<##'?? A%L)\\-D-D-FF))++!!''*Q	"ll&=&=&??"! $ !..!"Q	#2&$7'	 !/#!A
 (
 	
r   )rh   )r   N)r+   r,   r-   r.   r9   r   r0   r;   r5   r   r3   r*   r   r   r^   r^   l   s    M3
.
 .
r   r^   gmexample_inputsc                 b   ^  S[         S[         4U 4S jjn[        =R                  S-  sl        U$ )Nargsr   c                     > [         R                  R                  S5         TR                  " U 6 sS S S 5        $ ! , (       d  f       g = f)Nrr   )rc   rd   record_functionforward)r   r   s    r   _wrapped%fx_insert_profiling.<locals>._wrapped   s-    ^^++M:::t$ ;::s	   :
Ar   )r   r^   r9   )r   r   r   s   `  r   fx_insert_profilingr      s.    % % % aOr   )__doc__dataclassesrM   typingr   typing_extensionsr   rc   utilsr   	dataclassr   r5   boolrP   r2   r1   r\   r^   fxGraphModuler   r*   r   r   <module>r      s     	  "   !3 !3 !3H# #B>d >3c 3t 37
 7
tEHH00 $s) PS r   