ó
    Eñi;  ã                  ó–   • S r SSKJr  SSKJr  SSKJrJr  \(       a  SSKr\ " S S5      5       r	\ " S S	5      5       r
 " S
 S5      rg)a  
Dynamo Profiler - tracks where Dynamo spends time during compilation.

This module provides profiling functionality for Dynamo tracing, showing per-function
cumtime (inclusive) and tottime (exclusive) in a cProfile-compatible format.
The output can be visualized with tools like snakeviz.

Usage:
    # Enable via config (prints pstats output):
    torch._dynamo.config.dynamo_profiler = True

    # Or save to file for snakeviz:
    torch._dynamo.config.dynamo_profiler = "/tmp/dynamo.prof"
    # Then: snakeviz /tmp/dynamo.prof
é    )Úannotations)Ú	dataclass)ÚAnyÚTYPE_CHECKINGNc                  ó(  • \ rS rSr% SrS\S'   S\S'   S\S'   S\S'   S\S	'   S\S
'   S\S'   SrS\S'   SrS\S'   SrS\S'   Sr	S\S'   Sr
S\S'   \SS j5       r\S S j5       r\S S j5       r\S S j5       r\S!S j5       r\S"S j5       rS#S jrSrg)$ÚFunctionTraceTimingé   a  
Timing data for a single inlined function trace.

Follows cProfile conventions:
- cumtime: total time in function including all subcalls (inclusive)
- tottime: time in function excluding subcalls (exclusive)
- caller info: who called this function (for building call graph)
ÚstrÚ	func_nameÚfilenameÚintÚfirstlinenoÚ
cumtime_nsÚ
tottime_nsÚbytecode_countÚinline_depthNú
str | NoneÚcaller_func_nameÚcaller_filenamez
int | NoneÚcaller_firstlinenoTÚboolÚis_primitive_call© ú tuple[tuple[str, str, int], ...]Ú
call_stackc                ó   • U R                   $ ©N©r   ©Úselfs    ÚZ/home/mande/repo/quber/.venv/lib/python3.13/site-packages/torch/_dynamo/dynamo_profiler.pyÚtrace_time_nsÚ!FunctionTraceTiming.trace_time_ns=   s   € à�‰Ðó    c                ó    • U R                   S-  $ ©Ng    €„.Ar   r   s    r!   Útrace_time_msÚ!FunctionTraceTiming.trace_time_msA   ó   € à�‰ Ñ$Ð$r$   c                ó    • U R                   S-  $ r&   r   r   s    r!   Ú
cumtime_msÚFunctionTraceTiming.cumtime_msE   r)   r$   c                ó    • U R                   S-  $ r&   )r   r   s    r!   Ú
tottime_msÚFunctionTraceTiming.tottime_msI   r)   r$   c                óˆ   • U R                   b5  U R                  =(       d    SU R                  =(       d    SU R                   4$ g)z/Return caller as a pstats-compatible key tuple.NÚ r   )r   r   r   r   s    r!   Ú
caller_keyÚFunctionTraceTiming.caller_keyM   sD   € ð × Ñ Ñ,à×$Ñ$×*¨Ø×'Ñ'×,¨1Ø×%Ñ%ðð ð
 r$   c                óH   • U R                   U R                  U R                  4$ )z6Return this function as a pstats-compatible key tuple.)r   r   r   r   s    r!   Úfunc_keyÚFunctionTraceTiming.func_keyX   s   € ð —‘˜t×/Ñ/°·±Ð@Ð@r$   c                óÂ   • SU R                    SU R                   SU R                   SU R                  S SU R                  S SU R
                   SU R                   S	3$ )
NzFunctionTraceTiming(z at Ú:z
, cumtime=ú.2fzms, tottime=zms, bytecode=z, depth=Ú))r   r   r   r+   r.   r   r   r   s    r!   Ú__repr__ÚFunctionTraceTiming.__repr__]   sp   € à" 4§>¡>Ð"2°$°t·}±}°oÀQÀt×GWÑGWÐFXð YØ—‘ sÐ+¨<¸¿¹ÈÐ7Lð MØ×+Ñ+Ð,¨H°T×5FÑ5FÐ4GÀqðJð	
r$   )Úreturnr   )r=   Úfloat)r=   ztuple[str, int, str] | None)r=   ztuple[str, int, str])r=   r
   )Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__Ú__annotations__r   r   r   r   r   Úpropertyr"   r'   r+   r.   r2   r5   r;   Ú__static_attributes__r   r$   r!   r   r      sè   ‡ ñð ƒNØƒMØÓàƒOØƒOàÓàÓà#'Ð�jÓ'Ø"&€O�ZÓ&Ø%)Ð˜
Ó)ð #Ð�tÓ"ð 46€JÐ0Ó5ð óó ðð ó%ó ð%ð ó%ó ð%ð ó%ó ð%ð óó ðð óAó ðA÷
r$   r   c                  óZ   • \ rS rSr% SrS\S'   S\S'   S\S'   S\S'   S\S	'   S
rS\S'   Srg)ÚProfilerStackEntryée   z@Stack entry for tracking function timing in the Dynamo profiler.r
   r   r   r   r   Ústart_time_nsÚchild_time_nsTr   r   r   N)r?   r@   rA   rB   rC   rD   r   rF   r   r$   r!   rH   rH   e   s,   ‡ áJàƒNØƒMØÓØÓØÓØ"Ð�tÖ"r$   rH   c                  óÊ   • \ rS 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S jr
SS	 jrSS
 jr S     SS jjr S     SS jjr S     SS jjrSrg)ÚDynamoProfilerStateéq   z:State for Dynamo profiler tracking function trace timings.c                ó    • / U l         / U l        g r   )ÚtimingsÚstackr   s    r!   Ú__init__ÚDynamoProfilerState.__init__t   s   € Ø24ˆŒØ/1ˆ�
r$   c                ó:   • U R                   R                  U5        g)z)Record timing data for a traced function.N)rP   Úappend)r    Útimings     r!   Úrecord_timingÚ!DynamoProfilerState.record_timingx   s   € à�‰×Ñ˜FÕ#r$   c                ó   • U R                   $ )zGet all recorded timings.)rP   r   s    r!   Úget_timingsÚDynamoProfilerState.get_timings|   s   € à�|‰|Ðr$   c                ó¤   ^^^• [        UUU4S jU R                   5       5      (       + nU R                  R                  [        TTTUSUS95        g)z'Push a new entry onto the timing stack.c              3  óš   >#   • U  H@  nUR                   T:H  =(       a%    UR                  T:H  =(       a    UR                  T:H  v •  MB     g 7fr   ©r   r   r   )Ú.0Úentryr   r   r   s     €€€r!   Ú	<genexpr>Ú+DynamoProfilerState.push.<locals>.<genexpr>…   sN   øé € ð 
ò $�ð �O‰O˜yÑ(÷ 1Ø—‘ (Ñ*÷1à×!Ñ! [Ñ0ô1ò $ùs   ƒAAr   )r   r   r   rJ   rK   r   N)ÚanyrQ   rU   rH   )r    r   r   r   rJ   Úis_primitives    ```  r!   ÚpushÚDynamoProfilerState.push€   sU   ú€ ô
 ö 
ð Ÿšó	
ó 
ô 
ˆð 	�
‰
×ÑÜØ#Ø!Ø'Ø+ØØ".ñõ		
r$   c                óZ   • U R                   (       a  U R                   R                  5       $ g)z(Pop the top entry from the timing stack.N)rQ   Úpopr   s    r!   rh   ÚDynamoProfilerState.pop–   s   € à�:�:Ø—:‘:—>‘>Ó#Ð#Ør$   c                ól   • U R                   (       a#  U R                   S   =R                  U-  sl        gg)zGAdd the child's cumulative time to the parent's child_time accumulator.éÿÿÿÿN)rQ   rK   )r    Úchild_cumtime_nss     r!   Úadd_child_timeÚ"DynamoProfilerState.add_child_timeœ   s(   € à�:�:Ø�J‰J�r‰N×(Ò(Ð,<Ñ<Ö(ð r$   c                óŠ   • U R                   (       a2  U R                   S   nUR                  UR                  UR                  4$ g)zLGet the current caller (top of stack) as (func_name, filename, firstlineno).rk   N)rQ   r   r   r   )r    r`   s     r!   Úget_current_callerÚ&DynamoProfilerState.get_current_caller¡   s4   € à�:�:Ø—J‘J˜r‘NˆEØ—O‘O U§^¡^°U×5FÑ5FÐGÐGØr$   c                ó:   • [        S U R                   5       5      $ )zOGet the full current call stack as tuple of (func_name, filename, firstlineno).c              3  óf   #   • U  H'  oR                   UR                  UR                  4v •  M)     g 7fr   r^   )r_   r`   s     r!   ra   Ú5DynamoProfilerState.get_call_stack.<locals>.<genexpr>ª   s&   é € ð 
ÚNXÀU�_‰_˜eŸn™n¨e×.?Ñ.?Õ@Êjùs   ‚/1)ÚtuplerQ   r   s    r!   Úget_call_stackÚ"DynamoProfilerState.get_call_stack¨   s    € äñ 
ØNRÏjÊjó
ó 
ð 	
r$   Nc                ó†  • SSK nSSKnSSKnSSKnUR	                  [
        5      n0 n0 n	U R                   GH]  n
U
R                  U
R                  U
R                  4nX¸;  a  SSSSS.X‹'   0 X›'   X‹   nUS==   S-  ss'   US==   U
R                  S-  -  ss'   U
R                  (       a'  US	==   S-  ss'   US
==   U
R                  S-  -  ss'   U
R                  c  M«  U
R                  U
R                  =(       d    SU
R                  =(       d    S4nXÙU   ;  a  SSSSS.X›   U'   X›   U   nUS==   S-  ss'   US==   U
R                  S-  -  ss'   US
==   U
R                  S-  -  ss'   U
R                  (       d  GMP  US	==   S-  ss'   GM`     U(       Ga  [!        UR#                  5       S SS9n[%        S5        [%        SS SS	S SSS SS
S S35        [%        S5        SnSnU Hn  u  u  nnnnUS   nUS	   nUS   S-  nUS
   S-  nUU-  nUU-  nSU;   a  UR'                  S5      S   OUn[%        US SUS SUS SUS SU SU SU S35        Mp     [%        S5        [%        S[)        U R                  5       S[)        U5       35        [%        S US! S"US! S#35        [+        U	R-                  5       5       H"  nX›    H  nXØ;  d  M
  SSSSS.X�'   0 X�'   M     M$     0 nUR#                  5        HM  u  p¼0 nX›   R#                  5        H  u  pÞUS   US	   US   US
   4UU'   M     US	   US   US   US
   U4UU'   MO     UR/                  5       nUR1                  5         UR3                  5         UR4                  " UUR7                  5       S$9nUUl        [;        S% UR=                  5        5       5      Ul        [;        S& UR=                  5        5       5      Ul         [;        S' UR=                  5        5       5      Ul!        U(       a$  URE                  U5        URG                  S(UU5        U$ ))z¸Generate pstats.Stats object from recorded timings.

Args:
    output_file: Optional file path to save the stats.
    print_raw: If True, print raw aggregate timings before returning.
r   Ng        )ÚncallsÚpcallsÚtottimeÚcumtimery   é   r{   g    eÍÍArz   r|   r1   c                ó   • U S   S   $ )Nr}   r|   r   )Úxs    r!   Ú<lambda>Ú5DynamoProfilerState.generate_pstats.<locals>.<lambda>ò   s   € °!°A±$°y²/r$   T)ÚkeyÚreversez 
=== Aggregate Timings (raw) ===z>8Ú z>12z
  functionzP--------------------------------------------------------------------------------iè  Ú/rk   z>10.2fzms zms  z (r8   r:   zTotal timings: z, unique functions: zSum tottime: r9   zms, Sum cumtime: Úms)Ústreamc              3  ó*   #   • U  H	  oS    v •  M     g7f)r}   Nr   ©r_   Úss     r!   ra   Ú6DynamoProfilerState.generate_pstats.<locals>.<genexpr>;  s   é € ÐBÒ.A¨ !¦Ò.Aùó   ‚c              3  ó*   #   • U  H	  oS    v •  M     g7f)r   Nr   r‰   s     r!   ra   r‹   <  s   é € ÐAÒ-@¨ žtÒ-@ùrŒ   c              3  ó*   #   • U  H	  oS    v •  M     g7f)é   Nr   r‰   s     r!   ra   r‹   =  s   é € Ð?Ò+> a˜qžTÒ+>ùrŒ   z/Saved pstats to %s. Visualize with: snakeviz %s)$ÚcProfileÚioÚloggingÚpstatsÚ	getLoggerr?   rP   r   r   r   r   r   r   r   r   r   ÚsortedÚitemsÚprintÚsplitÚlenÚlistÚkeysÚProfileÚenableÚdisableÚStatsÚStringIOÚstatsÚsumÚvaluesÚtotal_callsÚ
prim_callsÚtotal_ttÚ
dump_statsÚinfo)r    Úoutput_fileÚ	print_rawr�   r‘   r’   r“   ÚlogÚ
aggregatedÚcaller_edgesÚtr‚   Úaggr2   ÚedgeÚsorted_itemsÚtotal_cumtimeÚtotal_tottimer   Úlinenor   ry   rz   r{   r|   Ú
short_fileÚ
stats_dictÚcallersÚdummy_profiler¡   s                                 r!   Úgenerate_pstatsÚ#DynamoProfilerState.generate_pstats®   sÂ  € ó 	ÛÛÛà×Ñ¤Ó)ˆð BDˆ
ð ð 	ð —•ˆAØ—:‘:˜qŸ}™}¨a¯k©kÐ:ˆCàÓ$àØØ"Ø"ñ	#�
‘ð %'�Ñ!à‘/ˆCØ�‹M˜QÑ‹MØ�	‹N˜aŸl™l¨SÑ0Ñ0‹Nà×"×"Ø�H“ Ñ"“Ø�I“ !§,¡,°Ñ"4Ñ4“ð × Ñ Ó,à×%Ñ%Ø×(Ñ(×-¨AØ×&Ñ&×,¨"ð�
ð
 °#Ñ%6Ó6à"#Ø"#Ø#&Ø#&ñ	5�LÑ% jÑ1ð $Ñ(¨Ñ4�Ø�X“ !Ñ#“Ø�Y“ 1§<¡<°#Ñ#5Ñ5“ð �Y“ 1§<¡<°#Ñ#5Ñ5“Ø×&×&Ò&Ø˜“N aÑ'–NñS ÷V Ü!Ø× Ñ Ó"Ñ(AÈ4ñˆLô Ð5Ô6ÜØ˜B�-˜q ¨" ¨Q¨y¸¨o¸Q¸yÈ¸oÈZÐXôô �(ŒOØˆMØˆMÛ6BÑ2Ñ-�˜6 9¨sØ˜X™�Ø˜X™�Ø˜i™.¨4Ñ/�Ø˜i™.¨4Ñ/�Ø Ñ(�Ø Ñ(�Ø8;¸x»˜XŸ^™^¨CÓ0°Ò4ÈX�
ÜØ˜b�k  6¨" +¨Q¨w°vÐ.>¸cÀ'È&ÐAQÐQUØ �k  J <¨q°°¸ð;öñ 7Cô �(ŒOÜØ!¤# d§l¡lÓ"3Ð!4Ð4HÌÈZËÐHYÐZôô Ø ¨cÐ2Ð2CÀMÐRUÐCVÐVXÐYôô ˜×)Ñ)Ó+Ö,ˆCØ*Ô/�
ØÕ/à"#Ø"#Ø#&Ø#&ñ	.�JÑ*ð 02�LÓ,ó 0ñ -ð ð 	ð #×(Ñ(Ö*‰HˆCØQSˆGØ$0Ñ$5×$;Ñ$;Ö$=Ñ �
à˜‘NØ˜‘NØ˜‘OØ˜‘Oð	'�˜
Ó#ñ %>ð �H‘Ø�H‘Ø�I‘Ø�I‘ØðˆJ�s‹Oñ +ð& !×(Ñ(Ó*ˆØ×ÑÔØ×ÑÔØ—’˜]°2·;±;³=ÑAˆà ˆŒÜÑB¨j×.?Ñ.?Ô.AÓBÓBˆÔÜÑA¨Z×->Ñ->Ô-@ÓAÓAˆÔÜÑ?¨:×+<Ñ+<Ô+>Ó?Ó?ˆŒæØ×Ñ˜[Ô)Ø�H‰HØAØØôð ˆr$   c           	     ó’  • SSK nSSKnSSKnUR                  S5      (       d  [	        S5        gUR                  S5      (       d  [	        S5        gUc  UR                  SS5      S   S	-   n UR                  SS
SSSSU/UR                  UR                  S9nUR                  SSSU/UR                  UR                  UR                  S9nUR                  R                  5         UR                  5       u  p‰UR                  5       u  pŠUR                  S:w  a  [	        SU
R                  5        35        gUR                  S:w  a  [	        SU	R                  5        35        gUR                  R                  U5      (       d  [	        SU 35        g[	        SU 35        U$ ! [         a  n[	        SU 35         SnAgSnAff = f)aC  Generate an SVG call graph from a profile file using gprof2dot and graphviz.

Args:
    profile_file: Path to the pstats profile file.
    svg_file: Optional path for the output SVG. If not provided, uses
        profile_file with .svg extension.

Returns:
    Path to the generated SVG file, or None if generation failed.
r   NÚ	gprof2dotz8gprof2dot not found. Install with: pip install gprof2dotÚdotz3graphviz 'dot' not found. Install graphviz package.Ú.r}   z.svgz-fr“   z"--node-label=total-time-percentagez!--node-label=self-time-percentagez--node-label=total-time)ÚstdoutÚstderrz-Tsvgz-o)Ústdinr¿   rÀ   zgprof2dot failed: zgraphviz dot failed: zSVG file was not created: zSVG call graph saved to: zFailed to generate SVG: )ÚosÚshutilÚ
subprocessÚwhichr—   ÚrsplitÚPopenÚPIPEr¿   ÚcloseÚcommunicateÚ
returncodeÚdecodeÚpathÚisfileÚ	Exception)r    Úprofile_fileÚsvg_filerÂ   rÃ   rÄ   r¼   r½   Ú_Údot_errÚgprof2dot_errÚes               r!   Úgenerate_svgÚ DynamoProfilerState.generate_svgI  sÊ  € ó 	ÛÛà�|‰|˜K×(Ñ(ÜÐLÔMØà�|‰|˜E×"Ñ"ÜÐGÔHØàÑØ#×*Ñ*¨3°Ó2°1Ñ5¸Ñ>ˆHð,	à"×(Ñ(àØØØ8Ø7Ø-Ø ðð "—‘Ø!—‘ð )ð ˆIð ×"Ñ"Ø˜  xÐ0Ø×&Ñ&Ø!—‘Ø!—‘ð	 #ð ˆCð ×Ñ×"Ñ"Ô$ØŸ™Ó*‰JˆAØ(×4Ñ4Ó6ÑˆAà×#Ñ# qÓ(ÜØ(¨×)=Ñ)=Ó)?Ð(@ÐAôð à�~‰~ Ó"ÜÐ-¨g¯n©nÓ.>Ð-?Ð@ÔAØà—7‘7—>‘> (×+Ñ+ÜÐ2°8°*Ð=Ô>ØäÐ-¨h¨ZÐ8Ô9ØˆOøäó 	ÜÐ,¨Q¨CÐ0Ô1Üûð	ús+   Á-C
F$ Ä8,F$ Å%.F$ ÆF$ Æ$
GÆ.GÇGc                ó@  • SSK nU R                  (       d  gU R                  USS9n[        S5        UR                  Ul        UR                  S5      R                  5         U(       a6  [        SU 35        [        SU 35        U(       a  U R                  U5        ggg)	zûPrint profiler stats to stdout and optionally save to file.

Args:
    output_file: Optional path to save the pstats profile.
    generate_svg: If True and output_file is provided, also generate an SVG
        call graph using gprof2dot and graphviz.
r   NT)rª   z!
=== Dynamo Profiler (pstats) ===Ú
cumulativez
Profile saved to: zVisualize with: snakeviz )	ÚsysrP   r¹   r—   r¿   r‡   Ú
sort_statsÚprint_statsrÖ   )r    r©   rÖ   rÚ   r¡   s        r!   r§   ÚDynamoProfilerState.dump_stats“  s�   € ó 	à�|�|Øà×$Ñ$ [¸DÐ$ÐAˆÜÐ2Ô3Ø—z‘zˆŒØ×Ñ˜Ó&×2Ñ2Ô4æÜÐ(¨¨Ð6Ô7ÜÐ-¨k¨]Ð;Ô<æØ×!Ñ! +Õ.ð ð	 r$   )rQ   rP   )r=   ÚNone)rV   r   r=   rÞ   )r=   zlist[FunctionTraceTiming])
r   r
   r   r
   r   r   rJ   r   r=   rÞ   )r=   zProfilerStackEntry | None)rl   r   r=   rÞ   )r=   ztuple[str, str, int] | None)r=   r   )NF)r©   r   rª   r   r=   zpstats.Statsr   )rÐ   r
   rÑ   r   r=   r   )NT)r©   r   rÖ   r   r=   rÞ   )r?   r@   rA   rB   rC   rR   rW   rZ   re   rh   rm   rp   rv   r¹   rÖ   r§   rF   r   r$   r!   rM   rM   q   sÎ   † ÙDô2ô$ôð
Øð
Ø(+ð
Ø:=ð
ØNQð
à	ô
ô,ô=ô
ô
ð AFðYØ%ðYØ9=ðYà	õYðx 9=ðHØðHØ+5ðHà	õHðV DHð/Ø%ð/Ø<@ð/à	÷/ð /r$   rM   )rC   Ú
__future__r   Údataclassesr   Útypingr   r   r“   r   rH   rM   r   r$   r!   Ú<module>râ      s_   ðñõ  #å !ß %ö Ûð ÷F
ð F
ó ðF
ðR ÷#ð #ó ð#÷{/ò {/r$   