o
    Zh,                     @   sh  U d dl Z d dlZd dlm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
 d
ZG dd de jZG dd deZG dd deZG dd dZi Zeeef ed< e aeed< d/dedee fddZdefddZdd Zd0defddZe	d ed!d/d"d#Zd1d$ed%ed&efd'd(Ze	d)ed!d&ed$ed%efd*d+Zd,e fd-d.Z!dS )2    N)
namedtuplewraps)Optional)
deprecated)MetricsConfigMetricHandlerConsoleMetricHandlerNullMetricHandlerMetricStream	configure	getStreamprofprofile
put_metricpublish_metricget_elapsed_time_ms
MetricDatar   )	timestamp
group_namenamevaluec                   @   s.   e Zd ZdgZddeeeef  fddZdS )r   paramsNc                 C   s   || _ | j d u ri | _ d S d S N)r   )selfr    r   T/var/www/auris/lib/python3.10/site-packages/torch/distributed/elastic/metrics/api.py__init__(   s   

zMetricsConfig.__init__r   )__name__
__module____qualname__	__slots__r   dictstrr   r   r   r   r   r   %   s     r   c                   @   s    e Zd ZejdefddZdS )r   metric_datac                 C      d S r   r   r   r$   r   r   r   emit/   s   zMetricHandler.emitN)r   r   r    abcabstractmethodr   r'   r   r   r   r   r   .   s    r   c                   @      e Zd ZdefddZdS )r	   r$   c              	   C   s,   t d|j d|j d|j d|j  d S )N[z][z]: =)printr   r   r   r   r&   r   r   r   r'   5   s   "zConsoleMetricHandler.emitNr   r   r    r   r'   r   r   r   r   r	   4       r	   c                   @   r*   )r
   r$   c                 C   r%   r   r   r&   r   r   r   r'   <   s   zNullMetricHandler.emitNr.   r   r   r   r   r
   ;   r/   r
   c                   @   s0   e Zd ZdedefddZdedefddZd	S )
r   r   handlerc                 C   s   || _ || _d S r   )r   r0   )r   r   r0   r   r   r   r   A   s   
zMetricStream.__init__metric_namemetric_valuec                 C   s    | j tt | j|| d S r   )r0   r'   r   timer   )r   r1   r2   r   r   r   	add_valueE   s   zMetricStream.add_valueN)r   r   r    r#   r   r   intr4   r   r   r   r   r   @   s    r   _metrics_map_default_metrics_handlerr0   groupc                 C   s   |d u r| a d S | t|< d S r   )r7   r6   )r0   r8   r   r   r   r   P   s   r   c                 C   s    | t v r	t |  }nt}t| |S r   )r6   r7   r   )r8   r0   r   r   r   r   Z   s   

r   c                 C   sL   | j }|d}t|dkr$| j}|r |dd d |d  S |d S |S )N.   r   )r    splitlenr   )fnqualnamer<   moduler   r   r   _get_metric_nameb   s   
rA   torchelasticc                    s    fd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d}|S )Nc               
      s   t  }z6zt } | i |}t| dd W n ty,   t| dd  w W t| dt| |S t| dt| w Nz.successr:   z.failurez.duration.ms)rA   r3   r   	Exceptionr   )argskwargskeystartresult)fr8   r   r   wrapper   s   z#prof.<locals>.wrap.<locals>.wrapperr   )rK   rL   r8   )rK   r   wrap   s   zprof.<locals>.wrapr   )r>   r8   rN   r   rM   r   r   o   s   r   zDeprecated, use `@prof` instead)categoryc                    s    fd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                    rC   )Nc                     s   z9zt   } | i |}t j dd W n ty*   t j dd  w W t j dt| |S t j dt| w rD   )r3   r   r   rE   r   )rF   rG   
start_timerJ   )funcr8   r   r   rL      s(   

z&profile.<locals>.wrap.<locals>.wrapperr   )rQ   rL   rM   )rQ   r   rN      s   zprofile.<locals>.wrapr   )r8   rN   r   rM   r   r      s   r   r1   r2   metric_groupc                 C   s   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   r4   )r1   r2   rR   r   r   r   r      s   r   zMDeprecated, use `put_metric(metric_group)(metric_name, metric_value)` insteadc                 C   s   t | }||| d S r   rS   )rR   r1   r2   Zmetric_streamr   r   r   r      s   r   start_time_in_secondsc                 C   s   t   }t||  d S )z<Return the elapsed time in millis from the given start time.i  )r3   r5   )rT   end_timer   r   r   r      s   r   r   )NrB   )rB   )"r(   r3   collectionsr   	functoolsr   typingr   Ztyping_extensionsr   __all__r   r   ABCr   r	   r
   r   r6   r"   r#   __annotations__r7   r   r   rA   r   FutureWarningr   r5   r   r   floatr   r   r   r   r   <module>   s8   
		

,#