o
    2h'A                  	   @   s  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d	d
ZeejejejdeejejejdeejejejdgZeejejejdeejej ej!deej"ej#ej$dgZ%eej&ej'ej(deej)ej*ej+deej,ej-ej.deej/ej0ej1deej2ej3ej4dgZ5eej6ej7ej8deej9ej:ej;deej<ej=ej>dgZ?eej@ejAejBdeejCejDejEdeejFejGejHdgZIG dd deJZKG dd deJZLG dd deKZMG dd deJZNG dd deKZOG dd deJZPG dd deKZQG dd deJZRedg dG dd deKZSG d d! d!eJZTG d"d# d#eJZUG d$d% d%eUZVG d&d' d'eKZWg ZXG d(d) d)eJZYd*d+ ZZdS ),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                   @   s0   e Zd ZdZg dZdd Zdd Zdd Zd	S )
MetriczThe base class of metric.)_metric_metric_name_metric_methods_label_lengthc                 G   sP   || _ || _|| _|t| jkrtd| j t| j| j| j j| | _dS )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args r   ]/var/www/html/chatgem/venv/lib/python3.10/site-packages/tensorflow/python/eager/monitoring.py__init__r   s   	zMetric.__init__c                 C   sF   z| j | j j}| j}W n
 ty   Y d S w |d ur!|| d S d S N)r   r   r
   r   AttributeError)r   deletermetricr   r   r   __del__   s   
zMetric.__del__c                 G   s>   t || jkrtd| j| j| j| j j| jg|R  S )Retrieves the cell.zThe {} expects taking {} labels)r   r   r   r   r   r   r   r   r   labelsr   r   r   r      s   zMetric.get_cellN)__name__
__module____qualname____doc__	__slots__r   r    r   r   r   r   r   r   m   s    
r   c                   @   .   e Zd ZdZdgZdd Zdd Zdd Zd	S )
CounterCellz+CounterCell stores each value of a Counter._cellc                 C   
   || _ dS )z`Creates a new CounterCell.

    Args:
      cell: A c pointer of TFE_MonitoringCounterCell.
    Nr+   r   cellr   r   r   r         
zCounterCell.__init__c                 C      t | j| dS )zQAtomically increments the value.

    Args:
      value: non-negative value.
    N)r   $TFE_MonitoringCounterCellIncrementByr+   r   valuer   r   r   increase_by      zCounterCell.increase_byc                 C      t | jS Retrieves the current value.)r   TFE_MonitoringCounterCellValuer+   r   r   r   r   r4         zCounterCell.valueN)r$   r%   r&   r'   r(   r   r5   r4   r   r   r   r   r*          r*   c                       0   e Zd ZdZg Z fddZ fddZ  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                    (   t t| jdtt|||g|R   dS )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.
    r?   N)superr?   r   _counter_methodsr   r   namedescriptionr#   	__class__r   r   r      
   zCounter.__init__c                       t tt| j| S r!   )r*   rA   r?   r   r"   rF   r   r   r         zCounter.get_cellr$   r%   r&   r'   r(   r   r   __classcell__r   r   rF   r   r?      
    r?   c                   @   r)   )
IntGaugeCellz/A single integer value stored in an `IntGauge`.r+   c                 C   r,   )zbCreates a new IntGaugeCell.

    Args:
      cell: A c pointer of TFE_MonitoringIntGaugeCell.
    Nr-   r.   r   r   r   r      r0   zIntGaugeCell.__init__c                 C   r1   )zEAtomically set the value.

    Args:
      value: integer value.
    N)r   TFE_MonitoringIntGaugeCellSetr+   r3   r   r   r   set   r6   zIntGaugeCell.setc                 C   r7   r8   )r   TFE_MonitoringIntGaugeCellValuer+   r;   r   r   r   r4      r<   zIntGaugeCell.valueNr$   r%   r&   r'   r(   r   rQ   r4   r   r   r   r   rO      r=   rO   c                       r>   )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                    r@   )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.
    rT   N)rA   rT   r   _int_gauge_methodsr   rC   rF   r   r   r      rH   zIntGauge.__init__c                    rI   rJ   )rO   rA   rT   r   r"   rF   r   r   r      rK   zIntGauge.get_cellrL   r   r   rF   r   rT      rN   rT   c                   @   r)   )
StringGaugeCellz1A single string value stored in an `StringGauge`.r+   c                 C   r,   )zhCreates a new StringGaugeCell.

    Args:
      cell: A c pointer of TFE_MonitoringStringGaugeCell.
    Nr-   r.   r   r   r   r     r0   zStringGaugeCell.__init__c                 C   r1   )zDAtomically set the value.

    Args:
      value: string value.
    N)r    TFE_MonitoringStringGaugeCellSetr+   r3   r   r   r   rQ     r6   zStringGaugeCell.setc                 C   sL   t  }t| j| t|d}W d   |S 1 sw   Y  |S )r9   zutf-8N)r   	tf_bufferr   "TFE_MonitoringStringGaugeCellValuer+   r   TF_GetBufferdecode)r   buffer_r4   r   r   r   r4     s   

zStringGaugeCell.valueNrS   r   r   r   r   rV      r=   rV   c                       r>   )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                    r@   )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.
    r]   N)rA   r]   r   _string_gauge_methodsr   rC   rF   r   r   r   '  
   
zStringGauge.__init__c                    rI   rJ   )rV   rA   r]   r   r"   rF   r   r   r   2  rK   zStringGauge.get_cellrL   r   r   rF   r   r]     rN   r]   c                   @   r)   )
BoolGaugeCellz0A single boolean value stored in an `BoolGauge`.r+   c                 C   r,   )zdCreates a new BoolGaugeCell.

    Args:
      cell: A c pointer of TFE_MonitoringBoolGaugeCell.
    Nr-   r.   r   r   r   r   <  r0   zBoolGaugeCell.__init__c                 C   r1   )zBAtomically set the value.

    Args:
      value: bool value.
    N)r   TFE_MonitoringBoolGaugeCellSetr+   r3   r   r   r   rQ   D  r6   zBoolGaugeCell.setc                 C   r7   r8   )r    TFE_MonitoringBoolGaugeCellValuer+   r;   r   r   r   r4   L  r<   zBoolGaugeCell.valueNrS   r   r   r   r   r`   7  r=   r`   z!__internal__.monitoring.BoolGauge)v1c                       r>   )	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                    r@   )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.
    rd   N)rA   rd   r   _bool_gauge_methodsr   rC   rF   r   r   r   \  r_   zBoolGauge.__init__c                    rI   rJ   )r`   rA   rd   r   r"   rF   r   r   r   g  rK   zBoolGauge.get_cellrL   r   r   rF   r   rd   Q  s
    rd   c                   @   r)   )
SamplerCellz+SamplerCell stores each value of a Sampler.r+   c                 C   r,   )z`Creates a new SamplerCell.

    Args:
      cell: A c pointer of TFE_MonitoringSamplerCell.
    Nr-   r.   r   r   r   r   q  r0   zSamplerCell.__init__c                 C   r1   )zBAtomically add a sample.

    Args:
      value: float value.
    N)r   TFE_MonitoringSamplerCellAddr+   r3   r   r   r   addy  r6   zSamplerCell.addc                 C   s\   t  }t| j| t|}W d   n1 sw   Y  t }|	t
| |S )zRetrieves the current distribution of samples.

    Returns:
      A HistogramProto describing the distribution of samples.
    N)r   rX   r   TFE_MonitoringSamplerCellValuer+   r   rZ   r   HistogramProtoParseFromStringr   as_bytes)r   r\   
proto_datahistogram_protor   r   r   r4     s   
zSamplerCell.valueN)r$   r%   r&   r'   r(   r   rh   r4   r   r   r   r   rf   l  r=   rf   c                   @   s&   e Zd ZdZdgZdd Zdd ZdS )Bucketsz&Bucketing strategies for the samplers.bucketsc                 C   r,   )z[Creates a new Buckets.

    Args:
      buckets: A c pointer of TFE_MonitoringBuckets.
    N)rp   )r   rp   r   r   r   r     r0   zBuckets.__init__c                 C   s   t | j d S r   )r   TFE_MonitoringDeleteBucketsrp   r;   r   r   r   r      s   zBuckets.__del__N)r$   r%   r&   r'   r(   r   r    r   r   r   r   ro     s
    ro   c                       s$   e Zd ZdZg Z fddZ  ZS )ExponentialBucketszExponential bucketing strategy.

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

    Args:
      scale: float
      growth_factor: float
      bucket_count: integer
    N)rA   rr   r   r   #TFE_MonitoringNewExponentialBuckets)r   scalegrowth_factorbucket_countrF   r   r   r     s
   
zExponentialBuckets.__init__)r$   r%   r&   r'   r(   r   rM   r   r   rF   r   rr     s    rr   c                       r>   )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                    s,   t t| jdtt|||j|g|R   dS )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.
    rw   N)rA   rw   r   _sampler_methodsr   rp   )r   rD   rp   rE   r#   rF   r   r   r     s
   	zSampler.__init__c                    rI   rJ   )rf   rA   rw   r   r"   rF   r   r   r     rK   zSampler.get_cellrL   r   r   rF   r   rw     s
    rw   c                   @   s4   e Zd ZdZg dZ	dddZdd Zd	d
 ZdS )MonitoredTimerzGA context manager to measure the walltime and increment a Counter cell.)r/   tmonitored_section_name	_counting_avoid_repetitive_countingNFc                 C   s   || _ || _|| _d| _dS )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)r/   r{   r}   r|   )r   r/   r{   avoid_repetitive_countingr   r   r   r     s   
zMonitoredTimer.__init__c                 C   s@   | j r| jr| jtv rd| _| S t | _| jrt| j | S NF)r}   r{   MonitoredTimerSectionsr|   timerz   appendr;   r   r   r   	__enter__  s   

zMonitoredTimer.__enter__c                 C   sL   ~~~| j r"t | j d }| jt| | jr$t| j d S d S d S )Ni@B )	r|   r   rz   r/   r5   intr{   r   remove)r   exception_typeexception_value	tracebackmicro_secondsr   r   r   __exit__  s   zMonitoredTimer.__exit__r   )r$   r%   r&   r'   r(   r   r   r   r   r   r   r   ry     s    	
ry   c                    s    fd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                    s   t   fdd}|S )Nc                     s:   t   | i |W  d    S 1 sw   Y  d S r   )ry   )r   kwargs)r/   funcr   r   wrapper  s   
$z:monitored_timer.<locals>.actual_decorator.<locals>.wrapper)	functoolswraps)r   r   r/   )r   r   actual_decorator  s   z)monitored_timer.<locals>.actual_decoratorr   )r/   r   r   r   r   monitored_timer  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_MonitoringGetCellCounter2rB   TFE_MonitoringNewIntGauge0TFE_MonitoringDeleteIntGauge0TFE_MonitoringGetCellIntGauge0TFE_MonitoringNewIntGauge1TFE_MonitoringDeleteIntGauge1TFE_MonitoringGetCellIntGauge1TFE_MonitoringNewIntGauge2TFE_MonitoringDeleteIntGauge2TFE_MonitoringGetCellIntGauge2rU   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_MonitoringGetCellStringGauge4r^   TFE_MonitoringNewBoolGauge0TFE_MonitoringDeleteBoolGauge0TFE_MonitoringGetCellBoolGauge0TFE_MonitoringNewBoolGauge1TFE_MonitoringDeleteBoolGauge1TFE_MonitoringGetCellBoolGauge1TFE_MonitoringNewBoolGauge2TFE_MonitoringDeleteBoolGauge2TFE_MonitoringGetCellBoolGauge2re   TFE_MonitoringNewSampler0TFE_MonitoringDeleteSampler0TFE_MonitoringGetCellSampler0TFE_MonitoringNewSampler1TFE_MonitoringDeleteSampler1TFE_MonitoringGetCellSampler1TFE_MonitoringNewSampler2TFE_MonitoringDeleteSampler2TFE_MonitoringGetCellSampler2rx   objectr   r*   r?   rO   rT   rV   r]   r`   rd   rf   ro   rr   rw   r   ry   r   r   r   r   r   <module>   s   +
#3