
    קg1                        U 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 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e         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)Dict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                   >    e Zd ZdgZddeeeef                  fdZdS )r	   paramsNc                 4    || _         | j         	i | _         d S d S N)r   )selfr   s     a/var/www/html/ai-engine/env/lib/python3.11/site-packages/torch/distributed/elastic/metrics/api.py__init__zMetricsConfig.__init__(   s$    ;DKKK     r   )__name__
__module____qualname__	__slots__r   r   strr     r!   r   r	   r	   %   sH        
I xS#X7      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.emit5   sN    j%jj)?jjKDTjjWbWhjj	
 	
 	
 	
 	
r!   Nr"   r#   r$   r   r,   r'   r!   r   r   r   4   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!   Nr4   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   r8   )r   r   r8   s      r   r    zMetricStream.__init__A   s    $r!   metric_namemetric_valuec                     | j                             t          t          j                    | j        ||                     d S r   )r8   r,   r   timer   )r   r:   r;   s      r   	add_valuezMetricStream.add_valueE   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_handlerr8   groupc                 &    || a d S | t          |<   d S r   )rA   r@   )r8   rB   s     r   r   r   P   s%    } $+   %Ur!   c                 ^    | t           v rt           |          }nt          }t          | |          S r   )r@   rA   r   )rB   r8   s     r   r   r   Z   s.    u%*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   b   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frB   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[   rB   s   ` r   wrapzprof.<locals>.wrap   s9    	q	 	 	 	 	 
	 r!   r'   )rK   rB   r\   s    ` r   r   r   o   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   funcrB   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[   rB   s   ` r   r\   zprofile.<locals>.wrap   s9    	t	 	 	 	 	 
	  r!   r'   )rB   r\   s   ` r   r   r      s#        ( Kr!   r:   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>   )r:   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   r:   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   typingr   r   typing_extensionsr   __all__r   r	   ABCr
   r   r   r   r@   r&   __annotations__rA   r   r   rN   r   FutureWarningr   r?   r   r   floatr   r'   r!   r   <module>rt      s   



  " " " " " "       ! ! ! ! ! ! ! ! ( ( ( ( ( (    Z&R&R&RSS
           CG   
 
 
 
 
= 
 
 
       

 
 
 
 
 
 
 
 *,d3%& + + +*;*;*=*= - = = =& &} &Xc] & & & &(S ( ( ( (
 
 
( ( ( ( ( (V -FFF      GF FA AC As A# A A A A S  7 73 7c 7 7 7	 7
:u : : : : : :r!   