ó
    EñiÑ"  ã                  ó"  • % S r SSKJr  SSKrSSKrSSKrSSKJr  SSKJ	r	J
r
JrJr  SSKJr  \(       a  SSKJr  SSKJr  \R$                  " \5      r " S	 S
5      r\\\\\\	4   \
\\      \
\   /S4   rS\S'    " S S5      r " S S5      rg)a[  Metrics collection and management system for Dynamo.

This module provides context managers for gathering and reporting metrics during
compilation and runtime.

It includes two main components:
- MetricsContext: A context manager for collecting metrics during compilation, supporting
  nested contexts and various metric types (counters, sets, key-value pairs)
- RuntimeMetricsContext: A specialized context for runtime metrics collection that doesn't
  require explicit context management

The metrics system enables comprehensive monitoring and analysis of both compilation and
execution performance.
é    )ÚannotationsN)ÚCallable)ÚAnyÚOptionalÚTYPE_CHECKINGÚ	TypeAlias)ÚSelf)ÚIterator)ÚCapturedTracebackc                  óD   • \ rS rSrSrS	S
S jjrSS jrSS jrSS jrSr	g)ÚTopNé#   zW
Helper to record a list of metrics, keeping only the top N "most expensive" elements.
c                ó   • Xl         / U l        g ©N©Úat_mostÚheap)Úselfr   s     ÚZ/home/mande/repo/quber/.venv/lib/python3.13/site-packages/torch/_dynamo/metrics_context.pyÚ__init__ÚTopN.__init__(   s   € ØŒØ+-ˆ�	ó    c                ó²   • [        U R                  5      U R                  :  a  [        R                  O[        R
                  nU" U R                  X!45        g r   )Úlenr   r   ÚheapqÚheappushÚheappushpop)r   ÚkeyÚvalÚfns       r   ÚaddÚTopN.add,   s6   € ä" 4§9¡9›~°·±Ó<ŒU�^Š^Ä%×BSÑBSˆÙ
ˆ4�9‰9�s�jÕ!r   c                ó,   • [        U R                  5      $ r   )r   r   ©r   s    r   Ú__len__ÚTopN.__len__1   s   € Ü�4—9‘9‹~Ðr   c                ó8   • S [        U R                  SS9 5       $ )Nc              3  ó,   #   • U  H
  u  pX!4v •  M     g 7fr   © )Ú.0r   r   s      r   Ú	<genexpr>Ú TopN.__iter__.<locals>.<genexpr>5   s   é € ÐKÒ+J™x˜s�•
Ò+Jùs   ‚T)Úreverse)Úsortedr   r$   s    r   Ú__iter__ÚTopN.__iter__4   s   € ÙK¬6°$·)±)ÀTÒ+JÓKÐKr   r   N)é   )r   ÚintÚreturnÚNone)r   r   r   r2   r3   r4   )r3   r2   )r3   zIterator[tuple[Any, int]])
Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r   r!   r%   r/   Ú__static_attributes__r)   r   r   r   r   #   s   † ñö.ô"ô
÷Lr   r   r   Ú
OnExitTypec                  ó¤   • \ 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S jjr
SS	 jrSSS
 jjrSS jrSS jrSS jrSrg)ÚMetricsContexté>   c                óH   • Xl         0 U l        SU l        SU l        / U l        g)a)  
Use this class as a contextmanager to create a context under which to accumulate
a set of metrics, e.g., metrics gathered during a compilation. On exit of the
contextmanager, call the provided 'on_exit' function and pass a dictionary of
all metrics set during the lifetime of the contextmanager.
r   N)Ú_on_exitÚ_metricsÚ_start_time_nsÚ_levelÚ_edits©r   Úon_exits     r   r   ÚMetricsContext.__init__?   s&   € ð  ŒØ(*ˆŒØ#$ˆÔØˆŒØ@Bˆ�r   c                ó’   • U R                   S:X  a!  0 U l        [        R                  " 5       U l        U =R                   S-  sl         U $ )z
Initialize metrics recording.
r   é   )rC   rA   ÚtimeÚtime_nsrB   r$   s    r   Ú	__enter__ÚMetricsContext.__enter__L   s8   € ð �;‰;˜!ÓàˆDŒMÜ"&§,¢,£.ˆDÔà�Š�qÑ�Øˆr   c                ó8  • U =R                   S-  sl         U R                   S:¼  d   eU R                   S:X  a>   [        R                  " 5       nU R                  U R                  X@R
                  X5        gg! [         a    [        R                  S5         gf = f)z.
At exit, call the provided on_exit function.
rI   r   z0Unexpected exception logging compilation metricsN)	rC   rJ   rK   r@   rB   rA   Ú	ExceptionÚlogÚ	exception)r   Úexc_typeÚ	exc_valueÚ
_tracebackÚend_time_nss        r   Ú__exit__ÚMetricsContext.__exit__X   sƒ   € ð 	�Š�qÑ�Ø�{‰{˜aÓÐÐØ�;‰;˜!ÓðRÜ"Ÿlšl›n�Ø—‘Ø×'Ñ'¨·m±mÀXõð øô ó RÜ—‘ÐPÖQðRús   ¹<A7 Á7BÂBc                ó    • U R                   S:„  $ )z$
True if we've entered the context.
r   )rC   r$   s    r   Úin_progressÚMetricsContext.in_progressl   s   € ð �{‰{˜Q‰Ðr   c                ó¬   • U R                   S:X  a  [        SU S35      eXR                  ;  a  SU R                  U'   U R                  U==   U-  ss'   g)ú'
Increment a metric by a given amount.
r   zCannot increment ú outside of a MetricsContextN©rC   ÚRuntimeErrorrA   ©r   ÚmetricÚvalues      r   Ú	incrementÚMetricsContext.incrementr   sR   € ð �;‰;˜!ÓÜÐ!2°6°(Ð:VÐWÓXÐXØŸ™Ó&Ø$%ˆD�M‰M˜&Ñ!Ø�‰�fÓ Ñ&Ôr   c                óT   ^• SSR                  U4S jU R                   5       5      -   $ )Nz

c              3  ó„   >#   • U  H5  u  pUT-  (       d  M  S SR                  UR                  5       5      -   v •  M7     g7f)zPrevious Traceback:
Ú N)ÚjoinÚformat)r*   ÚeÚkÚpreds      €r   r+   Ú/MetricsContext._render_edits.<locals>.<genexpr>}   s8   øé € ð $
â#‘�Ø�4�xó :Ð# b§g¡g¨a¯h©h«jÓ&9Ö9Ú#ùs
   ƒA —)A )rh   rD   )r   rl   s    `r   Ú_render_editsÚMetricsContext._render_edits|   s,   ø€ Ø˜Ÿ™ô $
àŸšó$
ó 
ñ 
ð 	
r   c                ó.  • U R                   S:X  a  [        SU S35      eXR                  ;   a)  U(       d"  [        U R                  U15      SU S3-   5      eU R                  R                  [        R                  " SS9U145        X R                  U'   g)	zj
Set a metric to a given value. Raises if the metric has been assigned previously
in the current context.
r   úCannot set r]   z

RuntimeError: Metric 'z]' has already been set in the current context (see above for current and previous traceback).rI   ©ÚskipN)rC   r_   rA   rn   rD   Úappendr   Úextract)r   ra   rb   Ú	overwrites       r   ÚsetÚMetricsContext.setƒ   sš   € ð
 �;‰;˜!ÓÜ ¨V¨HÐ4PÐQÓRÐRØ—]‘]Ó"®9ÜØ×"Ñ" F 8Ó,Ø.¨v¨hð 7Bð BñBóð ð
 	�‰×ÑÔ-×5Ò5¸1Ñ=À¸xÐHÔIØ %�‰�fÒr   c                ó    • U R                   S:X  a  [        SU S35      eXR                  ;  a  0 U R                  U'   X0R                  U   U'   g)a  
Treats a give metric as a dictionary and set the k and value within it.
Note that the metric must be a dictionary or not present.

We allow this to be called multiple times (i.e. for features, it's not uncommon
for them to be used multiple times within a single compilation).
r   rq   r]   Nr^   )r   ra   r   rb   s       r   Úset_key_valueÚMetricsContext.set_key_value“   sN   € ð �;‰;˜!ÓÜ ¨V¨HÐ4PÐQÓRÐRØŸ™Ó&Ø$&ˆD�M‰M˜&Ñ!Ø%*�‰�fÑ˜cÒ"r   c                óÞ  • U R                   S:X  a  [        S5      eU R                  R                  5       UR                  5       -  nU(       a?  U(       d8  [        U R	                  [        UR                  5       5      5      SU S3-   5      eU R                  R                  [        R                  " SS9[        UR                  5       5      45        U R                  R                  U5        g)z«
Set multiple metrics directly. This method does NOT increment. Raises if any
metric has been assigned previously in the current context and overwrite is
not set to True.
r   ú1Cannot update metrics outside of a MetricsContextz

RuntimeError: Metric(s) z_ have already been set in the current context.  (see above for current and previous traceback).rI   rr   N)rC   r_   rA   Úkeysrn   rw   rD   rt   r   ru   Úupdate)r   Úvaluesrv   Úexistings       r   r   ÚMetricsContext.update¡   s¿   € ð �;‰;˜!ÓÜÐRÓSÐSØ—=‘=×%Ñ%Ó'¨&¯+©+«-Ñ7ˆÞžIÜØ×"Ñ"¤3 v§{¡{£}Ó#5Ó6Ø0°°
ð ;Bð BñBóð ð
 	�‰×ÑÔ-×5Ò5¸1Ñ=¼sÀ6Ç;Á;Ã=Ó?QÐRÔSØ�‰×Ñ˜VÕ$r   c                ó~   • U R                   S:X  a  [        S5      eU R                   S:X  a  U R                  U5        gg)z1
Update, but only when at the outermost context.
r   r}   rI   N)rC   r_   r   )r   r€   s     r   Úupdate_outerÚMetricsContext.update_outer³   s9   € ð �;‰;˜!ÓÜÐRÓSÐSØ�;‰;˜!ÓØ�K‰K˜Õð r   c                óÊ   • U R                   S:X  a  [        SU S35      eXR                  ;  a  [        5       U R                  U'   U R                  U   R	                  U5        g)z(
Records a metric as a set() of values.
r   zCannot add r]   N)rC   r_   rA   rw   r!   r`   s      r   Ú
add_to_setÚMetricsContext.add_to_set¼   sV   € ð �;‰;˜!ÓÜ ¨V¨HÐ4PÐQÓRÐRØŸ™Ó&Ü$'£EˆD�M‰M˜&Ñ!Ø�‰�fÑ×!Ñ! %Õ(r   c                ó®   • U R                   S:X  a  gXR                  ;  a  [        5       U R                  U'   U R                  U   R                  X#5        g)z+
Records a metric as a TopN set of values.
r   N)rC   rA   r   r!   )r   ra   r   r   s       r   Ú	add_top_nÚMetricsContext.add_top_nÆ   sD   € ð �;‰;˜!ÓØØŸ™Ó&Ü$(£FˆD�M‰M˜&Ñ!Ø�‰�fÑ×!Ñ! #Õ+r   )rD   rC   rA   r@   rB   N©rF   r;   r3   r4   )r3   r	   )rR   zOptional[type[BaseException]]rS   zOptional[BaseException]rT   r   r3   r4   )r3   Úbool)ra   Ústrrb   r2   r3   r4   )rl   zset[str]r3   rŽ   )F)ra   rŽ   rb   r   rv   r�   r3   r4   )ra   rŽ   r   rŽ   rb   r   r3   r4   )r€   údict[str, Any]rv   r�   r3   r4   )r€   r�   r3   r4   )ra   rŽ   rb   r   r3   r4   )ra   rŽ   r   r   r   r2   r3   r4   )r5   r6   r7   r8   r   rL   rV   rY   rc   rn   rw   rz   r   r„   r‡   rŠ   r:   r)   r   r   r=   r=   >   sj   † ôCô
ðRà/ðRð +ðRð ð	Rð
 
ôRô(ô'ô
ö&ô +ö%ô$ ô)÷,r   r=   c                  óF   • \ rS rSrSS jr S       S	S jjrS
S jrSrg)ÚRuntimeMetricsContextéÑ   c                ó,   • Xl         0 U l        SU l        g)z§
Similar to MetricsContext, but used to gather the runtime metrics that are
decoupled from compilation, where there's not a natural place to insert a
context manager.
r   N)r@   rA   rB   rE   s     r   r   ÚRuntimeMetricsContext.__init__Ò   s   € ð  ŒØ(*ˆŒØ#$ˆÕr   Nc                óN  • U R                   (       d  [        R                  " 5       U l        XR                   ;  a  SU R                   U'   U R                   U==   U-  ss'   U(       a>  UR	                  5        H)  u  pEX@R                   ;  d  M  Uc  M  XPR                   U'   M+     gg)r\   r   N)rA   rJ   rK   rB   Úitems)r   ra   rb   Úextrark   Úvs         r   rc   ÚRuntimeMetricsContext.incrementÜ   sy   € ð �}�}ä"&§,¢,£.ˆDÔØŸ™Ó&Ø$%ˆD�M‰M˜&Ñ!Ø�‰�fÓ Ñ&ÓæØŸ™ž‘�ØŸM™MÕ)¨a«mØ'(—M‘M !Ó$ò &ð r   c                ó  • U R                   (       aF   [        R                  " 5       nU R                  U R                  XR                   SS5        0 U l         gg! [
         a    [        R                  S5         N*f = f! 0 U l         f = f)zG
Call the on_exit function with the metrics gathered so far and reset.
Nz,Unexpected exception logging runtime metrics)rA   rJ   rK   r@   rB   rO   rP   rQ   )r   rU   s     r   ÚfinishÚRuntimeMetricsContext.finishî   sr   € ð �=�=ð#Ü"Ÿlšl›n�Ø—‘Ø×'Ñ'¨·m±mÀTÈ4ôð !#�•ð øô ó NÜ—‘ÐLÖMðNûð !#�•ús#   “=A ÁA;Á8A> Á:A;Á;A> Á>	B)rA   r@   rB   rŒ   r   )ra   rŽ   rb   r2   r—   zOptional[dict[str, Any]]r3   r4   )r3   r4   )r5   r6   r7   r8   r   rc   r›   r:   r)   r   r   r‘   r‘   Ñ   s8   † ô%ð JNð)Øð)Ø"%ð)Ø.Fð)à	õ)÷$#r   r‘   )r9   Ú
__future__r   r   ÚloggingrJ   Úcollections.abcr   Útypingr   r   r   r   Útyping_extensionsr	   r
   Útorch.utils._tracebackr   Ú	getLoggerr5   rP   r   r2   ÚdictrŽ   ÚtypeÚBaseExceptionr;   Ú__annotations__r=   r‘   r)   r   r   Ú<module>r¨      s¤   ðòõ #ã Û Û Ý $ß :Ó :Ý "ö Ý(å 4ð ×Ò˜Ó!€÷Lñ Lð* !Øˆ#ˆt�C˜�H‰~˜x¨¨]Ñ(;Ñ<¸hÀ}Ñ>UÐVØð
ñ€
ˆIó ÷P,ñ P,÷f*#ò *#r   