
    BVh'A              	       J   d Z 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	 ddl
mZ ddlmZ ddlmZ  ej                   d	d
      Z eej$                  ej&                  ej(                         eej*                  ej,                  ej.                         eej0                  ej2                  ej4                        gZ eej8                  ej:                  ej<                         eej>                  ej@                  ejB                         eejD                  ejF                  ejH                        gZ% eejL                  ejN                  ejP                         eejR                  ejT                  ejV                         eejX                  ejZ                  ej\                         eej^                  ej`                  ejb                         eejd                  ejf                  ejh                        gZ5 eejl                  ejn                  ejp                         eejr                  ejt                  ejv                         eejx                  ejz                  ej|                        gZ? eej                  ej                  ej                         eej                  ej                  ej                         eej                  ej                  ej                        gZI G d deJ      ZK G d deJ      ZL G d deK      ZM G d deJ      ZN G d deK      ZO G d deJ      ZP G d deK      ZQ G d deJ      ZR edg        G d deK             ZS G d  d!eJ      ZT G d" d#eJ      ZU G d$ d%eU      ZV G d& d'eK      ZWg ZX G d( d)eJ      ZYd* ZZy)+zTensorFlow monitoring APIs.    N)summary_pb2)
pywrap_tfe)pywrap_tf_session)
c_api_util)compat)	tf_exportMetricMethodzcreate delete get_cell)createdeleteget_cellc                   *    e Zd ZdZg dZd Zd Zd Zy)MetriczThe base class of metric.)_metric_metric_name_metric_methods_label_lengthc                 $   || _         || _        || _        |t        | j                        k\  r8t	        dj                  | j                   t        | j                                     | j                  | j                     j                  | | _        y)zCreates a new metric.

    Args:
      metric_name: name of the metric class.
      metric_methods: list of swig metric methods.
      label_length: length of label args.
      *args: the arguments to call create method.
    z(Cannot create {} metric with label >= {}N)r   r   r   len
ValueErrorformatr
   r   )selfmetric_namemetric_methodslabel_lengthargss        R/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/eager/monitoring.py__init__zMetric.__init__r   s     $D)D%Ds4//00AHH


S!5!568 9 9 C4''(:(:;BBDIDL    c                     	 | j                   | j                     j                  }| j                  }|	 ||       y y # t        $ r Y y w xY wN)r   r   r   r   AttributeError)r   deletermetrics      r   __del__zMetric.__del__   sT    $$T%7%78??g||f fo   s   /= 	A	A	c                     t        |      | j                  k7  r/t        dj                  | j                  | j                               | j
                  | j                     j                  | j                  g| S )Retrieves the cell.zThe {} expects taking {} labels)r   r   r   r   r   r   r   r   )r   labelss     r   r   zMetric.get_cell   su    
6{d(((8??


T//1 2 2<4 2 23<< r   N)__name__
__module____qualname____doc__	__slots__r   r$   r    r   r   r   r   m   s    !M)J&r   r   c                   (    e Zd ZdZdgZd Zd Zd Zy)CounterCellz+CounterCell stores each value of a Counter._cellc                     || _         y)z`Creates a new CounterCell.

    Args:
      cell: A c pointer of TFE_MonitoringCounterCell.
    Nr0   r   cells     r   r   zCounterCell.__init__        DJr   c                 D    t        j                  | j                  |       y)zQAtomically increments the value.

    Args:
      value: non-negative value.
    N)r   $TFE_MonitoringCounterCellIncrementByr0   r   values     r   increase_byzCounterCell.increase_by   s     33DJJFr   c                 @    t        j                  | j                        S Retrieves the current value.)r   TFE_MonitoringCounterCellValuer0   r   s    r   r9   zCounterCell.value   s    44TZZ@@r   N)r(   r)   r*   r+   r,   r   r:   r9   r-   r   r   r/   r/      s    3i)GAr   r/   c                   0     e Zd ZdZg Z fdZ fdZ xZS )CounterzA stateful class for updating a cumulative integer metric.

  This class encapsulates a set of values (or a single value for a label-less
  metric). Each value is identified by a tuple of labels. The class allows the
  user to increment each value.
  c                 P    t        t        | 
  dt        t	        |      ||g|  y)zCreates a new Counter.

    Args:
      name: name of the new metric.
      description: description of the new metric.
      *labels: The label list of the new metric.
    rA   N)superrA   r   _counter_methodsr   r   namedescriptionr'   	__class__s       r   r   zCounter.__init__   s,     
'4!)-=s6{"&>6<>r   c                 6    t        t        t        |   |       S r&   )r/   rC   rA   r   r   r'   rH   s     r   r   zCounter.get_cell       uWd4f=>>r   r(   r)   r*   r+   r,   r   r   __classcell__rH   s   @r   rA   rA      s     )	>? ?r   rA   c                   (    e Zd ZdZdgZd Zd Zd Zy)IntGaugeCellz/A single integer value stored in an `IntGauge`.r0   c                     || _         y)zbCreates a new IntGaugeCell.

    Args:
      cell: A c pointer of TFE_MonitoringIntGaugeCell.
    Nr2   r3   s     r   r   zIntGaugeCell.__init__   r5   r   c                 D    t        j                  | j                  |       y)zEAtomically set the value.

    Args:
      value: integer value.
    N)r   TFE_MonitoringIntGaugeCellSetr0   r8   s     r   setzIntGaugeCell.set   s     ,,TZZ?r   c                 @    t        j                  | j                        S r<   )r   TFE_MonitoringIntGaugeCellValuer0   r?   s    r   r9   zIntGaugeCell.value   s    55djjAAr   Nr(   r)   r*   r+   r,   r   rU   r9   r-   r   r   rQ   rQ      s    7i)@Br   rQ   c                   0     e Zd ZdZg Z fdZ fdZ xZS )IntGaugezA stateful class for updating a gauge-like integer metric.

  This class encapsulates a set of integer values (or a single value for a
  label-less metric). Each value is identified by a tuple of labels. The class
  allows the user to set each value.
  c                 P    t        t        | 
  dt        t	        |      ||g|  y)zCreates a new IntGauge.

    Args:
      name: name of the new metric.
      description: description of the new metric.
      *labels: The label list of the new metric.
    rZ   N)rC   rZ   r   _int_gauge_methodsr   rE   s       r   r   zIntGauge.__init__   s,     
(D":/A3v;#'?7=?r   c                 6    t        t        t        |   |       S rJ   )rQ   rC   rZ   r   rK   s     r   r   zIntGauge.get_cell   s    h6?@@r   rM   rO   s   @r   rZ   rZ      s      )	?A Ar   rZ   c                   (    e Zd ZdZdgZd Zd Zd Zy)StringGaugeCellz1A single string value stored in an `StringGauge`.r0   c                     || _         y)zhCreates a new StringGaugeCell.

    Args:
      cell: A c pointer of TFE_MonitoringStringGaugeCell.
    Nr2   r3   s     r   r   zStringGaugeCell.__init__  r5   r   c                 D    t        j                  | j                  |       y)zDAtomically set the value.

    Args:
      value: string value.
    N)r    TFE_MonitoringStringGaugeCellSetr0   r8   s     r   rU   zStringGaugeCell.set  s     //

EBr   c                     t        j                         5 }t        j                  | j                  |       t        j                  |      j                  d      }ddd       |S # 1 sw Y   S xY w)r=   zutf-8N)r   	tf_bufferr   "TFE_MonitoringStringGaugeCellValuer0   r   TF_GetBufferdecode)r   buffer_r9   s      r   r9   zStringGaugeCell.value  s`    				 F733DJJH,,W5<<WEeF LF Ls   AA$$A.NrX   r-   r   r   r_   r_      s    9i)Cr   r_   c                   0     e Zd ZdZg Z fdZ fdZ xZS )StringGaugezA stateful class for updating a gauge-like string metric.

  This class encapsulates a set of string values (or a single value for a
  label-less metric). Each value is identified by a tuple of labels. The class
  allows the user to set each value.
  c                 P    t        t        | 
  dt        t	        |      ||g|  y)zCreates a new StringGauge.

    Args:
      name: name of the new metric.
      description: description of the new metric.
      *labels: The label list of the new metric.
    rj   N)rC   rj   r   _string_gauge_methodsr   rE   s       r   r   zStringGauge.__init__'  s.     
+t%m5J&)&k4OGMOr   c                 6    t        t        t        |   |       S rJ   )r_   rC   rj   r   rK   s     r   r   zStringGauge.get_cell2  s    5d<fEFFr   rM   rO   s   @r   rj   rj     s!     )	OG Gr   rj   c                   (    e Zd ZdZdgZd Zd Zd Zy)BoolGaugeCellz0A single boolean value stored in an `BoolGauge`.r0   c                     || _         y)zdCreates a new BoolGaugeCell.

    Args:
      cell: A c pointer of TFE_MonitoringBoolGaugeCell.
    Nr2   r3   s     r   r   zBoolGaugeCell.__init__<  r5   r   c                 D    t        j                  | j                  |       y)zBAtomically set the value.

    Args:
      value: bool value.
    N)r   TFE_MonitoringBoolGaugeCellSetr0   r8   s     r   rU   zBoolGaugeCell.setD  s     --djj%@r   c                 @    t        j                  | j                        S r<   )r    TFE_MonitoringBoolGaugeCellValuer0   r?   s    r   r9   zBoolGaugeCell.valueL  s    66tzzBBr   NrX   r-   r   r   ro   ro   7  s    8i)ACr   ro   z!__internal__.monitoring.BoolGauge)v1c                   0     e Zd ZdZg Z fdZ fdZ xZS )	BoolGaugezA stateful class for updating a gauge-like bool metric.

  This class encapsulates a set of boolean values (or a single value for a
  label-less metric). Each value is identified by a tuple of labels. The class
  allows the user to set each value.
  c                 P    t        t        | 
  dt        t	        |      ||g|  y)zCreates a new BoolGauge.

    Args:
      name: name of the new metric.
      description: description of the new metric.
      *labels: The label list of the new metric.
    rw   N)rC   rw   r   _bool_gauge_methodsr   rE   s       r   r   zBoolGauge.__init__\  s.     
)T#K1D$'K{MEKMr   c                 6    t        t        t        |   |       S rJ   )ro   rC   rw   r   rK   s     r   r   zBoolGauge.get_cellg  s    y$8&ABBr   rM   rO   s   @r   rw   rw   Q  s!     )	MC Cr   rw   c                   (    e Zd ZdZdgZd Zd Zd Zy)SamplerCellz+SamplerCell stores each value of a Sampler.r0   c                     || _         y)z`Creates a new SamplerCell.

    Args:
      cell: A c pointer of TFE_MonitoringSamplerCell.
    Nr2   r3   s     r   r   zSamplerCell.__init__q  r5   r   c                 D    t        j                  | j                  |       y)zBAtomically add a sample.

    Args:
      value: float value.
    N)r   TFE_MonitoringSamplerCellAddr0   r8   s     r   addzSamplerCell.addy  s     ++DJJ>r   c                 2   t        j                         5 }t        j                  | j                  |       t        j                  |      }ddd       t        j                         }|j                  t        j                               |S # 1 sw Y   CxY w)zRetrieves the current distribution of samples.

    Returns:
      A HistogramProto describing the distribution of samples.
    N)r   rd   r   TFE_MonitoringSamplerCellValuer0   r   rf   r   HistogramProtoParseFromStringr   as_bytes)r   rh   
proto_datahistogram_protos       r   r9   zSamplerCell.value  sx     
			 ;7//

GD$11':j; "002O##FOOJ$?@; ;s   6BBN)r(   r)   r*   r+   r,   r   r   r9   r-   r   r   r|   r|   l  s    3i)?r   r|   c                   "    e Zd ZdZdgZd Zd Zy)Bucketsz&Bucketing strategies for the samplers.bucketsc                     || _         y)z[Creates a new Buckets.

    Args:
      buckets: A c pointer of TFE_MonitoringBuckets.
    N)r   )r   r   s     r   r   zBuckets.__init__  s     DLr   c                 B    t        j                  | j                         y r    )r   TFE_MonitoringDeleteBucketsr   r?   s    r   r$   zBuckets.__del__  s    **4<<8r   N)r(   r)   r*   r+   r,   r   r$   r-   r   r   r   r     s    .k)9r   r   c                   &     e Zd ZdZg Z fdZ xZS )ExponentialBucketszExponential bucketing strategy.

  Sets up buckets of the form:
      [-DBL_MAX, ..., scale * growth^i,
       scale * growth_factor^(i + 1), ..., DBL_MAX].
  c                 V    t         t        |   t        j                  |||             y)z|Creates a new exponential Buckets.

    Args:
      scale: float
      growth_factor: float
      bucket_count: integer
    N)rC   r   r   r   #TFE_MonitoringNewExponentialBuckets)r   scalegrowth_factorbucket_countrH   s       r   r   zExponentialBuckets.__init__  s,     

d,66um7C	EFr   )r(   r)   r*   r+   r,   r   rN   rO   s   @r   r   r     s     )
F 
Fr   r   c                   0     e Zd ZdZg Z fdZ fdZ xZS )SampleraR  A stateful class for updating a cumulative histogram metric.

  This class encapsulates a set of histograms (or a single histogram for a
  label-less metric) configured with a list of increasing bucket boundaries.
  Each histogram is identified by a tuple of labels. The class allows the
  user to add a sample to each histogram value.
  c                 f    t        t        | 
  dt        t	        |      ||j
                  |g|  y)zCreates a new Sampler.

    Args:
      name: name of the new metric.
      buckets: bucketing strategy of the new metric.
      description: description of the new metric.
      *labels: The label list of the new metric.
    r   N)rC   r   r   _sampler_methodsr   r   )r   rF   r   rG   r'   rH   s        r   r   zSampler.__init__  s4     
'4!)-=s6{"&OGMOr   c                 6    t        t        t        |   |       S rJ   )r|   rC   r   r   rK   s     r   r   zSampler.get_cell  rL   r   rM   rO   s   @r   r   r     s     )
O? ?r   r   c                   .    e Zd ZdZg dZ	 ddZd Zd Zy)MonitoredTimerzGA context manager to measure the walltime and increment a Counter cell.)r4   tmonitored_section_name	_counting_avoid_repetitive_countingNc                 <    || _         || _        || _        d| _        y)aV  Creates a new MonitoredTimer.

    Args:
      cell: the cell associated with the time metric that will be inremented.
      monitored_section_name: name of action being monitored here.
      avoid_repetitive_counting: when set to True, if already in a monitored
        timer section with the same monitored_section_name, skip counting.
    TN)r4   r   r   r   )r   r4   r   avoid_repetitive_countings       r   r   zMonitoredTimer.__init__  s"     DI"8D&?D#DNr   c                     | j                   r'| j                  r| j                  t        v r	d| _        | S t	        j                         | _        | j                  rt        j                  | j                         | S NF)r   r   MonitoredTimerSectionsr   timer   appendr?   s    r   	__enter__zMonitoredTimer.__enter__  s^    ''''''+AAdnkYY[DF""##D$?$?@Kr   c                    ~~~| j                   rut        j                         | j                  z
  dz  }| j                  j	                  t        |             | j                  r t        j                  | j                         y y y )Ni@B )	r   r   r   r4   r:   intr   r   remove)r   exception_typeexception_value	tracebackmicro_secondss        r   __exit__zMonitoredTimer.__exit__  sg    ~~yy{TVV+w6m
iiC./		$	$%%d&A&AB 
% r   r   )r(   r)   r*   r+   r,   r   r   r   r-   r   r   r   r     s#    O) JO Cr   r   c                       fd}|S )zA function decorator for adding MonitoredTimer support.

  Args:
    cell: the cell associated with the time metric that will be inremented.
  Returns:
    A decorator that measure the function runtime and increment the specified
    counter cell.
  c                 F     t        j                          fd       }|S )Nc                  V    t              5   | i |cd d d        S # 1 sw Y   y xY wr    )r   )r   kwargsr4   funcs     r   wrapperz:monitored_timer.<locals>.actual_decorator.<locals>.wrapper  s-    $ %T$V$% % %s   ()	functoolswraps)r   r   r4   s   ` r   actual_decoratorz)monitored_timer.<locals>.actual_decorator  s%    __T% % Nr   r-   )r4   r   s   ` r   monitored_timerr     s     
r   )[r+   collectionsr   r   tensorflow.core.frameworkr   tensorflow.pythonr   tensorflow.python.clientr   tensorflow.python.frameworkr   tensorflow.python.utilr    tensorflow.python.util.tf_exportr   
namedtuple_MetricMethodTFE_MonitoringNewCounter0TFE_MonitoringDeleteCounter0TFE_MonitoringGetCellCounter0TFE_MonitoringNewCounter1TFE_MonitoringDeleteCounter1TFE_MonitoringGetCellCounter1TFE_MonitoringNewCounter2TFE_MonitoringDeleteCounter2TFE_MonitoringGetCellCounter2rD   TFE_MonitoringNewIntGauge0TFE_MonitoringDeleteIntGauge0TFE_MonitoringGetCellIntGauge0TFE_MonitoringNewIntGauge1TFE_MonitoringDeleteIntGauge1TFE_MonitoringGetCellIntGauge1TFE_MonitoringNewIntGauge2TFE_MonitoringDeleteIntGauge2TFE_MonitoringGetCellIntGauge2r\   TFE_MonitoringNewStringGauge0 TFE_MonitoringDeleteStringGauge0!TFE_MonitoringGetCellStringGauge0TFE_MonitoringNewStringGauge1 TFE_MonitoringDeleteStringGauge1!TFE_MonitoringGetCellStringGauge1TFE_MonitoringNewStringGauge2 TFE_MonitoringDeleteStringGauge2!TFE_MonitoringGetCellStringGauge2TFE_MonitoringNewStringGauge3 TFE_MonitoringDeleteStringGauge3!TFE_MonitoringGetCellStringGauge3TFE_MonitoringNewStringGauge4 TFE_MonitoringDeleteStringGauge4!TFE_MonitoringGetCellStringGauge4rl   TFE_MonitoringNewBoolGauge0TFE_MonitoringDeleteBoolGauge0TFE_MonitoringGetCellBoolGauge0TFE_MonitoringNewBoolGauge1TFE_MonitoringDeleteBoolGauge1TFE_MonitoringGetCellBoolGauge1TFE_MonitoringNewBoolGauge2TFE_MonitoringDeleteBoolGauge2TFE_MonitoringGetCellBoolGauge2ry   TFE_MonitoringNewSampler0TFE_MonitoringDeleteSampler0TFE_MonitoringGetCellSampler0TFE_MonitoringNewSampler1TFE_MonitoringDeleteSampler1TFE_MonitoringGetCellSampler1TFE_MonitoringNewSampler2TFE_MonitoringDeleteSampler2TFE_MonitoringGetCellSampler2r   objectr   r/   rA   rQ   rZ   r_   rj   ro   rw   r|   r   r   r   r   r   r   r-   r   r   <module>r      s   "    1 ( 6 2 ) 6&&&~7OP336699; 336699; 336699;  4477::< 4477::< 4477::<  77::==? 77::==? 77::==? 77::==? 77::==?# . 5588;;= 5588;;= 5588;;=  336699; 336699; 336699;  (V (VA& A4?f ?4B6 B4Av A4f :G& G4CF C4 .26C C 7C4 &  F9f 9"F F.?f ?<  0CV 0Cfr   