~L imdZddlmZddlZddlZddlmZddlmZddl Z ddl m Z ejGdd Z Gd d Zdd Zdd ZGddZddZy)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_oncecneZdZUdZded<dZded<dZded<dZded<dd Zdd Z dd Z dd Z dd Z y)ProfileMetricsgfloat microsecondsrint operatorsfusionsgraphsc|xj|jz c_|xj|jz c_|xj|jz c_|SNr r rselfothers \/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/torch/_dynamo/profiler.py__iadd__zProfileMetrics.__iadd__"sB U/// %//)  %  ct|tsJt|j|jz|j|jz|j|jzSr) isinstancer r r rrs r__add__zProfileMetrics.__add__(sS%000    2 2 2 NNU__ , LL5== (  rc t|tr t|||}t|jt d|jz |j t d|j z |j t d|j z S)Nr)rr r r maxr rrs r __truediv__zProfileMetrics.__truediv__0sp eS !"5%7E   Au'9'9 : : NNSEOO4 4 LL3q%--0 0  rc>|jdd|jddS)Nz4.0%z ops z timer r rs r__str__zProfileMetrics.__str__9s%..&eD,=,=d+C5IIrc2|j|jgSrr r!s rtocsvzProfileMetrics.tocsv<s 1 122rNrrreturnr)rr r&r )rrr&r r&str)r&z list[float]) __name__ __module__ __qualname__r __annotations__r rrrrrr"r$rrr r sCL%IsGSFCO   J3rr cDeZdZ ddZddZd dZd dZd dZy) ProfileResultc^|xs t|_|xs t|_||_yr)r capturedtotal unique_graphs)rr1r2r3s r__init__zProfileResult.__init__As*)1(DN4D %*%>n.> "/rc|xj|jz c_|xj|jz c_|xj|jz c_|Srr1r2r3rs rrzProfileResult.__iadd__HsB '  ekk!  e111 rc4|j|jz Sr)r1r2r!s rpercentzProfileResult.percentNs}}tzz))rc|jdd|jjdd|jjdd|jjddt |j zS)N2z graphs z graph calls 4/z = )r3r1rr r2r(r8r!s rr"zProfileResult.__str__Qsj!!!$HT]]-A-A!,DM}}&&q)4::+?+?*B# G$,,.! " rc|j|jj|jj|jjg|j j zSr)r3r1rr r2r8r$r!s rr$zProfileResult.tocsvXsU    MM MM # # JJ  LLN " # #rN)r1r r2r r3r r&Noner%)r&r r')r& list[Any])r)r*r+r4rr8r"r$r-rrr/r/@s;0&0/=0NQ0 0 * #rr/cFtjjddk(S)NTORCHDYNAMO_PRINT_MISSING1)osenvirongetr-rrshould_print_missingrFas ::>>5 6# ==rctd|Dry|Dcgc]}d|vsd|vs |}}tddj|ddycc}w)Nc3$K|]}d|v yw)z/torch/autograd/profiler.pyNr-).0xs r z print_missing..fs =! (A - =sz > )anyrjoin)stackrJs r print_missingrQes] =u == [16LTU6U E y&++eBCj12 s A A A c,eZdZUdZded<ddZddZy) Profilerrr r3ctjjtjjjgt |_y)N) activities with_stack)torchprofilerprofileProfilerActivityCPUrFprofr!s rr4zProfiler.__init__qs9NN**77;;<+-+  rc d}d}d}d}d}d}d}t|jjd}|D]} | jdk(r!| jj }|dz }|dz}3| jj |k\r| jj }| jj |kr#|dz }|| jjz }ntrt| j|dz }|| jjz }tj} dt_ |dz}tt||||z |t|||dz | S) Nrc.|jjSr) time_rangestart)rJs rz"Profiler.results..s!,,:L:Lr)key TORCHDYNAMOr)r r rrrr6)sortedr\eventsnamer`endra elapsed_usrFrQrPrSr3r/r ) rcaptured_regions captured_opscaptured_microseconds total_opstotal_microsecondslast_op_end_timecaptured_region_end_timerfer3s rresultszProfiler.resultswsr ! #%  ((*0LM Avv&+,<<+;+;( A% Q ##'77#$<<#3#3 <<##'?? A%L)Q\\-D-D-FF))+!!''*Q "all&=&=&??"! $!.. !"Q #2&$'77'  !/#!A  (  rN)r&r>)r&r/)r)r*r+r3r,r4rrr-rrrSrSnsM3 . rrScFdfd }txjdz c_|S)Nctjjd5j|cdddS#1swYyxYw)Nrd)rWrXrecord_functionforward)argsgms r_wrappedz%fx_insert_profiling.._wrappeds8 ^^ + +M : %2::t$ % % %s :Ar)rwrr&r)rSr3)rxexample_inputsrys` rfx_insert_profilingr{s % a Or)r&bool)rPz list[str]r&r>)rxztorch.fx.GraphModulerzr?r&r)__doc__ __future__r dataclassesrCtypingrtyping_extensionsrrWutilsr dataclassr r/rFrQrSr{r-rrrsf # "  !3!3!3H##B>37 7 tr