U
    ‰dõ  ã                   @   s.  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	 eddddd	gƒ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Ú
MetricDataÚ	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 )ÚMetricsConfigÚparamsN©r   c                 C   s   || _ | j d kri | _ d S ©Nr   )Úselfr   © r   úI/tmp/pip-unpacked-wheel-ua33x9lu/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 )ÚMetricHandler©Ú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 )ÚConsoleMetricHandlerr   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 )ÚNullMetricHandlerr   c                 C   s   d S r   r   r   r   r   r   r   /   s    zNullMetricHandler.emitNr#   r   r   r   r   r$   .   s   r$   c                   @   s,   e Zd Zeedœdd„Zeedœdd„ZdS )ÚMetricStream©r	   Úhandlerc                 C   s   || _ || _d S r   r&   )r   r	   r'   r   r   r   r   4   s    zMetricStream.__init__)Úmetric_nameÚmetric_valuec                 C   s    | j  tt ¡ | j||ƒ¡ d S r   )r'   r   r   Útimer	   )r   r(   r)   r   r   r   Ú	add_value8   s    ÿzMetricStream.add_valueN)r   r   r   r   r   r   Úintr+   r   r   r   r   r%   3   s   r%   Ú_default_metrics_handler©r'   Úgroupc                 C   s   |d kr| a n| t|< d S r   )r-   Ú_metrics_mapr.   r   r   r   Ú	configureC   s    r1   ©r/   c                 C   s    | t krt |  }nt}t| |ƒS r   )r0   r-   r%   )r/   r'   r   r   r   Ú	getStreamM   s    
r3   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Úqualnamer7   Úmoduler   r   r   Ú_get_metric_nameU   s    

r<   Ú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 ˆ ƒ}zVz(t ¡ }ˆ | |Ž}t|› ddˆƒ W n( tk
rZ   t|› ddˆƒ ‚ Y nX W 5 t|› dt|ƒˆƒ X |S )Nz.duration.msz.successr5   z.failure)r<   Ú
put_metricÚget_elapsed_time_msr*   Ú	Exception)ÚargsÚkwargsÚkeyÚstartÚresult)Úfr/   r   r   Úwrapperx   s    
z#prof.<locals>.wrap.<locals>.wrapperr   )rF   rG   r2   )rF   r   Úwrapw   s    zprof.<locals>.wrapNr   )r9   r/   rH   r   r2   r   Úprofb   s    rI   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                     sz   zZz*t ¡ }ˆ | |Ž}t ˆd ˆ j¡dƒ W n* tk
rV   t ˆd ˆ j¡dƒ ‚ Y nX W 5 t ˆd ˆ j¡t|ƒƒ X |S )Nz{}.duration.msz
{}.successr5   z
{}.failure)Úpublish_metricr"   r   r?   r*   r@   )rA   rB   Ú
start_timerE   )Úfuncr/   r   r   rG   œ   s    

ýz&profile.<locals>.wrap.<locals>.wrapperr   )rL   rG   r2   )rL   r   rH   ›   s    zprofile.<locals>.wrap)ÚwarningsÚwarnÚDeprecationWarning)r/   rH   r   r2   r   ÚprofileŽ   s    rP   ©r(   r)   Ú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)r3   r+   rQ   r   r   r   r>   ²   s    r>   )rR   r(   r)   c                 C   s"   t  d¡ t| ƒ}| ||¡ d S )NzKDeprecated, use put_metric(metric_group)(metric_name, metric_value) instead)rM   rN   r3   r+   )rR   r(   r)   Zmetric_streamr   r   r   rJ   Á   s
    ÿrJ   )Ústart_time_in_secondsc                 C   s   t   ¡ }t||  d ƒS )zG
    Returns the elapsed time in millis from the given start time.
    iè  )r*   r,   )rS   Zend_timer   r   r   r?   É   s    r?   )N)Nr=   )N)r=   )r   r*   rM   Úcollectionsr   Ú	functoolsr   Útypingr   r   r   r   ÚABCr   r    r$   r%   r0   r-   Ú__annotations__r   r1   r3   r<   rI   rP   r,   r>   rJ   Úfloatr?   r   r   r   r   Ú<module>	   s,    	
,
$