
    9im                        S r SSKJ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S jrSS jr " S S5      rS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.
    )annotationsN)Any)Self   )
print_oncec                      \ rS rSr% SrS\S'   SrS\S'   SrS\S'   SrS\S	'   SS
 jr	SS jr
SS jrSS jrSS jrSrg)ProfileMetrics   g        floatmicrosecondsr   int	operatorsfusionsgraphsc                    U =R                   UR                   -  sl         U =R                  UR                  -  sl        U =R                  UR                  -  sl        U $ Nr   r   r   selfothers     V/var/www/html/land-doc-ocr/venv/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   r   r	   r   maxr   r   r   s     r   __truediv__ProfileMetrics.__truediv__0   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__9   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   returnr   )r   r	   r-   r	   )r   r   r-   r	   r-   str)r-   zlist[float])__name__
__module____qualname____firstlineno__r   __annotations__r   r   r   r   r   r!   r&   r)   __static_attributes__r+   r   r   r	   r	      sC    L%IsGSFCO

J3r   r	   c                  V    \ rS rSr        S	S jrS
S jrSS jrSS jrSS jrSr	g)ProfileResult@   c                p    U=(       d
    [        5       U l        U=(       d
    [        5       U l        X0l        g r   )r	   capturedtotalunique_graphs)r   r:   r;   r<   s       r   __init__ProfileResult.__init__A   s(     )1(DN4D%*%>n.>
"/r   c                    U =R                   UR                   -  sl         U =R                  UR                  -  sl        U =R                  UR                  -  sl        U $ r   r:   r;   r<   r   s     r   r   ProfileResult.__iadd__H   sB    '

ekk!
e111r   c                4    U R                   U R                  -  $ r   )r:   r;   r%   s    r   percentProfileResult.percentN   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 = )r<   r:   r   r   r;   r/   rC   r%   s    r   r&   ProfileResult.__str__Q   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   )r<   r:   r   r   r;   rC   r)   r%   s    r   r)   ProfileResult.tocsvX   sU    MM  MM##JJ  	

 LLN  "# 	#r   r@   N)r:   r	   r;   r	   r<   r   r-   Noner,   )r-   r	   r.   )r-   	list[Any])
r0   r1   r2   r3   r=   r   rC   r&   r)   r5   r+   r   r   r7   r7   @   s;    0&0/=0NQ0	0*
#r   r7   c                 F    [         R                  R                  S5      S:H  $ )NTORCHDYNAMO_PRINT_MISSING1)osenvirongetr+   r   r   should_print_missingrT   a   s    ::>>56#==r   c                    [        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>f   s     
=u!(A-us   z	<built-inzsite-packages/torch/MISSINGz >> )anyr   join)stackrX   s     r   print_missingr`   e   sd    

=u
===a[16LTU6U5 
  y&++eBCj12s   
AAAc                  8    \ rS rSr% SrS\S'   S	S jrS
S jrSrg)Profilern   r   r   r<   c                    [         R                  R                  [         R                  R                  R                  /[        5       S9U l        g )N)
activities
with_stack)torchprofilerprofileProfilerActivityCPUrT   profr%   s    r   r=   Profiler.__init__q   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)rX   s    r   <lambda>"Profiler.results.<locals>.<lambda>   s    !,,:L:Lr   )keyTORCHDYNAMOr   )r   r   r   r   r   r@   )sortedrl   eventsnamerq   endrr   
elapsed_usrT   r`   r_   rb   r<   r7   r	   )r   captured_regionscaptured_opscaptured_microseconds	total_opstotal_microsecondslast_op_end_timecaptured_region_end_timerx   er<   s              r   resultsProfiler.resultsw   si    !	#% 		((*0LMAvv&+,<<+;+;( A% Q	##'77#$<<#3#3 <<##'?? A%L)\\-D-D-FF))++!!''*Q	"ll&=&=&??"! $ !..!"Q	#2&$7'	 !/#!A
 (
 	
r   )rl   N)r-   rL   )r-   r7   )	r0   r1   r2   r3   r<   r4   r=   r   r5   r+   r   r   rb   rb   n   s    M3
.
r   rb   c                J   ^  SU 4S jjn[         =R                  S-  sl        U$ )Nc                    > [         R                  R                  S5         TR                  " U 6 sS S S 5        $ ! , (       d  f       g = f)Nrv   )rg   rh   record_functionforward)argsgms    r   _wrapped%fx_insert_profiling.<locals>._wrapped   s-    ^^++M:::t$ ;::s	   :
Ar   )r   r   r-   r   )rb   r<   )r   example_inputsr   s   `  r   fx_insert_profilingr      s     % aOr   )r-   bool)r_   z	list[str]r-   rL   )r   ztorch.fx.GraphModuler   rM   r-   r   )__doc__
__future__r   dataclassesrQ   typingr   typing_extensionsr   rg   utilsr   	dataclassr	   r7   rT   r`   rb   r   r+   r   r   <module>r      sf    #  	  "   !3 !3 !3H# #B>37
 7
tr   