
    Wjw                        U d dl Z d dlZd dlmZ d dlmZ d dlmZ g dZ edg d          Z	 G d d	          Z
 G d
 de j                  Z G d de          Z G d de          Z G d d          Zi Zeeef         ed<    e            aeed<   d&dededz  fdZdefdZd Zd'defdZ ede          d&d            Zd(deded efd!Z ed"e          d ededefd#            Zd$efd%ZdS ))    N)
namedtuplewraps)
deprecated)MetricsConfigMetricHandlerConsoleMetricHandlerNullMetricHandlerMetricStream	configure	getStreamprofprofile
put_metricpublish_metricget_elapsed_time_ms
MetricDatar   )	timestamp
group_namenamevaluec                   8    e Zd ZdgZddeeef         dz  fdZdS )r   paramsNc                 4    || _         | j         	i | _         d S d S N)r   )selfr   s     j/home/longshao/multi-rider-rag/.venv/lib/python3.11/site-packages/torch/distributed/elastic/metrics/api.py__init__zMetricsConfig.__init__'   s$    ;DKKK     r   )__name__
__module____qualname__	__slots__dictstrr    r   r   r   r   $   sH        
I tCH~4      r   r   c                   4    e Zd Zej        defd            ZdS )r   metric_datac                     d S r   r&   r   r(   s     r   emitzMetricHandler.emit.   s    r   N)r    r!   r"   abcabstractmethodr   r+   r&   r   r   r   r   -   s=        
      r   r   c                       e Zd ZdefdZdS )r	   r(   c           
      d    t          d|j         d|j         d|j         d|j                    d S )N[z][z]: =)printr   r   r   r   r*   s     r   r+   zConsoleMetricHandler.emit4   sN    j%jj)?jjKDTjjWbWhjj	
 	
 	
 	
 	
r   Nr    r!   r"   r   r+   r&   r   r   r	   r	   3   s/        

 
 
 
 
 
 
r   r	   c                       e Zd ZdefdZdS )r
   r(   c                     d S r   r&   r*   s     r   r+   zNullMetricHandler.emit;   s    r   Nr3   r&   r   r   r
   r
   :   s/        
      r   r
   c                   .    e Zd ZdedefdZdedefdZdS )r   r   handlerc                 "    || _         || _        d S r   )r   r7   )r   r   r7   s      r   r   zMetricStream.__init__@   s    $r   metric_namemetric_valuec                     | j                             t          t          j                    | j        ||                     d S r   )r7   r+   r   timer   )r   r9   r:   s      r   	add_valuezMetricStream.add_valueD   s?    ty{{DO[,OO	
 	
 	
 	
 	
r   N)r    r!   r"   r%   r   r   intr=   r&   r   r   r   r   ?   sX        3     
S 
 
 
 
 
 
 
r   r   _metrics_map_default_metrics_handlerr7   groupc                 &    || a d S | t          |<   d S r   )r@   r?   )r7   rA   s     r   r   r   O   s%    } $+   %Ur   c                 b    t                               | t                    }t          | |          S r   )r?   getr@   r   )rA   r7   s     r   r   r   Y   s(    u&>??Gw'''r   c                     | j         }|                    d          }t          |          dk    r8| j        }|r'|                    d          d         dz   |d         z   S |d         S |S )N.   r   )r"   splitlenr!   )fnqualnamerI   modules       r   _get_metric_namerN   ^   sk    HNN3E
5zzQ 	<<$$R(3.q998Or   torchelasticc                 ,    fd}| r ||           S |S )a  
    @profile decorator publishes duration.ms, count, success, failure metrics for the function that it decorates.

    The metric name defaults to the qualified name (``class_name.def_name``) of the function.
    If the function does not belong to a class, it uses the leaf module name instead.

    Usage

    ::

     @metrics.prof
     def x():
         pass


     @metrics.prof(group="agent")
     def y():
         pass
    c                 @     t                      fd            }|S )Nc                  `   t                    }	 t          j                    } | i |}t          | dd           n## t          $ r t          | dd            w xY w	 t          | dt	          |                     n&# t          | dt	          |                     w xY w|S Nz.successrG   z.failurez.duration.ms)rN   r<   r   	Exceptionr   )argskwargskeystartresultfrA   s        r   wrapperz#prof.<locals>.wrap.<locals>.wrapper   s    "1%%CT	D+F++c+++Q6666   c+++Q666 7
 c///1DU1K1KUSSSS
c///1DU1K1KUSSSSMs   /A B  A""B #B+r   )rZ   r[   rA   s   ` r   wrapzprof.<locals>.wrap   s9    	q	 	 	 	 	 
	 r   r&   )rK   rA   r\   s    ` r   r   r   k   s8    *    " 
 tBxxr   zDeprecated, use `@prof` instead)categoryc                       fd}|S )z
    @profile decorator adds latency and success/failure metrics to any given function.

    Usage

    ::

     @metrics.profile("my_metric_group")
     def some_function(<arguments>):
    c                 @     t                      fd            }|S )Nc            	      j   	 t          j                     } | i |}t          j         dd           n(# t          $ r t          j         dd            w xY w	 t          j         dt	          |                     n+# t          j         dt	          |                     w xY w|S rS   )r<   r   r    rT   r   )rU   rV   
start_timerY   funcrA   s       r   r[   z&profile.<locals>.wrap.<locals>.wrapper   s    !Y[[
t.v..u&@&@&@!DDDD   u&@&@&@!DDD E }222'
33	   }222'
33	    Ms   48 B %AB (B0r   )rb   r[   rA   s   ` r   r\   zprofile.<locals>.wrap   s9    	t	 	 	 	 	 
	& r   r&   )rA   r\   s   ` r   r   r      s#        . Kr   r9   r:   metric_groupc                 L    t          |                              | |           dS )z
    Publish a metric data point.

    Usage

    ::

     put_metric("metric_name", 1)
     put_metric("metric_name", 1, "metric_group_name")
    Nr   r=   )r9   r:   rc   s      r   r   r      s(     l%%k<@@@@@r   zMDeprecated, use `put_metric(metric_group)(metric_name, metric_value)` insteadc                 P    t          |           }|                    ||           d S r   re   )rc   r9   r:   metric_streams       r   r   r      s-    
 l++MK66666r   start_time_in_secondsc                 R    t          j                     }t          || z
  dz            S )z<Return the elapsed time in millis from the given start time.i  )r<   r>   )rh   end_times     r   r   r      s&    y{{H00D8999r   r   )NrO   )rO   ) r,   r<   collectionsr   	functoolsr   typing_extensionsr   __all__r   r   ABCr   r	   r
   r   r?   r$   r%   __annotations__r@   r   r   rN   r   FutureWarningr   r>   r   r   floatr   r&   r   r   <module>rs      s   



  " " " " " "       ( ( ( ( ( (    Z&R&R&RSS
           CG   
 
 
 
 
= 
 
 
       

 
 
 
 
 
 
 
 *,d3%& + + +*;*;*=*= - = = =& &} &S4Z & & & &(S ( ( ( (

 
 
) ) ) ) ) )X -FFF# # # GF#LA AC As A# A A A A S  7 73 7c 7 7 7	 7
:u : : : : : :r   