a
    d                     @   s2  U d dl Z d dlZd dlZd dlmZ d dlmZ d dlmZ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 aeed< d)eedddZedddZdd Zd*edddZd+ddZd,eeed d!d"Zeeed#d$d%Zed&d'd(ZdS )-    N)
namedtuplewraps)DictOptional)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eeeef  dddZdS )r   paramsNr   c                 C   s   || _ | j d u ri | _ d S Nr   )selfr    r   n/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/torch/distributed/elastic/metrics/api.py__init__   s    
zMetricsConfig.__init__)N)__name__
__module____qualname__	__slots__r   r   strr   r   r   r   r   r      s   r   c                   @   s    e Zd ZejedddZdS )r   metric_datac                 C   s   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                   @   s   e Zd ZedddZdS )r	   r$   c                 C   s    t d|j|j|j|j d S )Nz[{}][{}]: {}={})printformatr   r   r   r   r&   r   r   r   r'   '   s    zConsoleMetricHandler.emitNr   r    r!   r   r'   r   r   r   r   r	   &   s   r	   c                   @   s   e Zd ZedddZdS )r
   r$   c                 C   s   d S r   r   r&   r   r   r   r'   3   s    zNullMetricHandler.emitNr,   r   r   r   r   r
   2   s   r
   c                   @   s,   e Zd ZeedddZeedddZdS )r   r   handlerc                 C   s   || _ || _d S r   r-   )r   r   r.   r   r   r   r   8   s    zMetricStream.__init__)metric_namemetric_valuec                 C   s    | j tt | j|| d S r   )r.   r'   r   timer   )r   r/   r0   r   r   r   	add_value<   s    zMetricStream.add_valueN)r   r    r!   r#   r   r   intr2   r   r   r   r   r   7   s   r   _default_metrics_handlerr.   groupc                 C   s   |d u r| a n| t|< d S r   )r4   _metrics_mapr5   r   r   r   r   G   s    r   r6   c                 C   s    | t v rt |  }nt}t| |S r   )r7   r4   r   )r6   r.   r   r   r   r   Q   s    
r   c                 C   sR   | j }|d}t|dkrJ| j}|r@|dd d |d  S |d S n|S d S )N.   r   )r!   splitlenr    )fnqualnamer<   moduler   r   r   _get_metric_nameY   s    

rA   torchelasticc                    s     fdd}| r|| S |S d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                    s   t   fdd}|S )Nc               
      s   t  }znz,t } | i |}t| dd W n& ty\   t| dd  Y n0 W t| dt| nt| dt| 0 |S )Nz.successr:   z.failurez.duration.ms)rA   r1   r   	Exceptionr   )argskwargskeystartresult)fr6   r   r   wrapper|   s    
0z#prof.<locals>.wrap.<locals>.wrapperr   )rI   rJ   r8   )rI   r   wrap{   s    zprof.<locals>.wrapNr   )r>   r6   rK   r   r8   r   r   f   s    r   c                    s   t dt  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>):
    zDeprecated, use @prof insteadc                    s   t   fdd}|S )Nc                     s   ztz.t   } | i |}td jd W n( tyX   td jd  Y n0 W td jt| ntd jt| 0 |S )Nz
{}.successr:   z
{}.failurez{}.duration.ms)r1   r   r+   r   rC   r   )rD   rE   
start_timerH   )funcr6   r   r   rJ      s"    


z&profile.<locals>.wrap.<locals>.wrapperr   )rM   rJ   r8   )rM   r   rK      s    zprofile.<locals>.wrap)warningswarnDeprecationWarning)r6   rK   r   r8   r   r      s    r   r/   r0   metric_groupc                 C   s   t || | dS )z
    Publishes a metric data point.

    Usage

    ::

     put_metric("metric_name", 1)
     put_metric("metric_name", 1, "metric_group_name")
    N)r   r2   rQ   r   r   r   r      s    r   )rR   r/   r0   c                 C   s"   t d t| }||| d S )NzKDeprecated, use put_metric(metric_group)(metric_name, metric_value) instead)rN   rO   r   r2   )rR   r/   r0   Zmetric_streamr   r   r   r      s
    r   )start_time_in_secondsc                 C   s   t   }t||  d S )zG
    Returns the elapsed time in millis from the given start time.
    i  )r1   r3   )rS   Zend_timer   r   r   r      s    r   )N)NrB   )N)rB   ) r(   r1   rN   collectionsr   	functoolsr   typingr   r   __all__r   r   ABCr   r	   r
   r   r7   r4   __annotations__r#   r   r   rA   r   r   r3   r   r   floatr   r   r   r   r   <module>	   s,   
	
,
$