o
    ?e'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_exportZ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   )selfZmetric_nameZmetric_methodsZlabel_lengthargs r   c/home/www/facesmatcher.com/pyenv/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   deleterZ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   Z$TFE_MonitoringCounterCellIncrementByr&   r   valuer   r   r   increase_by      zCounterCell.increase_byc                 C      t | jS Retrieves the current value.)r   ZTFE_MonitoringCounterCellValuer&   r   r   r   r   r.         zCounterCell.valueN)r   r    r!   r"   r#   r   r/   r.   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.
    r8   N)superr8   r   _counter_methodsr   r   namedescriptionr   	__class__r   r   r      
   zCounter.__init__c                       t tt| j| S r   )r%   r:   r8   r
   r   r?   r   r   r
         zCounter.get_cellr   r    r!   r"   r#   r   r
   __classcell__r   r   r?   r   r8      
    r8   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      r+   zIntGaugeCell.__init__c                 C   r,   )zEAtomically set the value.

    Args:
      value: integer value.
    N)r   ZTFE_MonitoringIntGaugeCellSetr&   r-   r   r   r   set   r0   zIntGaugeCell.setc                 C   r1   r2   )r   ZTFE_MonitoringIntGaugeCellValuer&   r4   r   r   r   r.      r5   zIntGaugeCell.valueNr   r    r!   r"   r#   r   rI   r.   r   r   r   r   rH      r6   rH   c                       r7   )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                    r9   )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.
    rK   N)r:   rK   r   _int_gauge_methodsr   r<   r?   r   r   r      rA   zIntGauge.__init__c                    rB   rC   )rH   r:   rK   r
   r   r?   r   r   r
      rD   zIntGauge.get_cellrE   r   r   r?   r   rK      rG   rK   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     r+   zStringGaugeCell.__init__c                 C   r,   )zDAtomically set the value.

    Args:
      value: string value.
    N)r   Z TFE_MonitoringStringGaugeCellSetr&   r-   r   r   r   rI     r0   zStringGaugeCell.setc                 C   sL   t  }t| j| t|d}W d   |S 1 sw   Y  |S )r3   zutf-8N)r   	tf_bufferr   Z"TFE_MonitoringStringGaugeCellValuer&   r   TF_GetBufferdecode)r   buffer_r.   r   r   r   r.     s   

zStringGaugeCell.valueNrJ   r   r   r   r   rM      r6   rM   c                       r7   )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                    r9   )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.
    rR   N)r:   rR   r   _string_gauge_methodsr   r<   r?   r   r   r   '  
   
zStringGauge.__init__c                    rB   rC   )rM   r:   rR   r
   r   r?   r   r   r
   2  rD   zStringGauge.get_cellrE   r   r   r?   r   rR     rG   rR   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   <  r+   zBoolGaugeCell.__init__c                 C   r,   )zBAtomically set the value.

    Args:
      value: bool value.
    N)r   ZTFE_MonitoringBoolGaugeCellSetr&   r-   r   r   r   rI   D  r0   zBoolGaugeCell.setc                 C   r1   r2   )r   Z TFE_MonitoringBoolGaugeCellValuer&   r4   r   r   r   r.   L  r5   zBoolGaugeCell.valueNrJ   r   r   r   r   rU   7  r6   rU   z!__internal__.monitoring.BoolGauge)v1c                       r7   )	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                    r9   )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)r:   rW   r   _bool_gauge_methodsr   r<   r?   r   r   r   \  rT   zBoolGauge.__init__c                    rB   rC   )rU   r:   rW   r
   r   r?   r   r   r
   g  rD   zBoolGauge.get_cellrE   r   r   r?   r   rW   Q  s
    rW   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  r+   zSamplerCell.__init__c                 C   r,   )zBAtomically add a sample.

    Args:
      value: float value.
    N)r   ZTFE_MonitoringSamplerCellAddr&   r-   r   r   r   addy  r0   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   rN   r   ZTFE_MonitoringSamplerCellValuer&   r   rO   r   ZHistogramProtoZParseFromStringr   as_bytes)r   rQ   Z
proto_dataZhistogram_protor   r   r   r.     s   
zSamplerCell.valueN)r   r    r!   r"   r#   r   rZ   r.   r   r   r   r   rY   l  r6   rY   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)r]   )r   r]   r   r   r   r     r+   zBuckets.__init__c                 C   s   t | j d S r   )r   ZTFE_MonitoringDeleteBucketsr]   r4   r   r   r   r     s   zBuckets.__del__N)r   r    r!   r"   r#   r   r   r   r   r   r   r\     s
    r\   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)r:   r^   r   r   Z#TFE_MonitoringNewExponentialBuckets)r   scaleZgrowth_factorZbucket_countr?   r   r   r     s
   
zExponentialBuckets.__init__)r   r    r!   r"   r#   r   rF   r   r   r?   r   r^     s    r^   c                       r7   )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.
    r`   N)r:   r`   r   _sampler_methodsr   r]   )r   r=   r]   r>   r   r?   r   r   r     s
   	zSampler.__init__c                    rB   rC   )rY   r:   r`   r
   r   r?   r   r   r
     rD   zSampler.get_cellrE   r   r   r?   r   r`     s
    r`   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*   rd   rf   re   )r   r*   rd   Z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)rf   rd   MonitoredTimerSectionsre   timerc   appendr4   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 )	re   ri   rc   r*   r/   intrd   rh   remove)r   Zexception_typeZexception_value	tracebackZmicro_secondsr   r   r   __exit__  s   zMonitoredTimer.__exit__rg   )r   r    r!   r"   r#   r   rk   ro   r   r   r   r   rb     s    	
rb   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   )rb   )r   kwargs)r*   funcr   r   wrapper  s   
$z:monitored_timer.<locals>.actual_decorator.<locals>.wrapper)	functoolswraps)rq   rr   r*   )rq   r   actual_decorator  s   z)monitored_timer.<locals>.actual_decoratorr   )r*   rv   r   ru   r   monitored_timer  s   
	rw   )[r"   collectionsrs   ri   Ztensorflow.core.frameworkr   Ztensorflow.pythonr   Ztensorflow.python.clientr   Ztensorflow.python.frameworkr   Ztensorflow.python.utilr   Z tensorflow.python.util.tf_exportr   
namedtupleZ_MetricMethodZTFE_MonitoringNewCounter0ZTFE_MonitoringDeleteCounter0ZTFE_MonitoringGetCellCounter0ZTFE_MonitoringNewCounter1ZTFE_MonitoringDeleteCounter1ZTFE_MonitoringGetCellCounter1ZTFE_MonitoringNewCounter2ZTFE_MonitoringDeleteCounter2ZTFE_MonitoringGetCellCounter2r;   ZTFE_MonitoringNewIntGauge0ZTFE_MonitoringDeleteIntGauge0ZTFE_MonitoringGetCellIntGauge0ZTFE_MonitoringNewIntGauge1ZTFE_MonitoringDeleteIntGauge1ZTFE_MonitoringGetCellIntGauge1ZTFE_MonitoringNewIntGauge2ZTFE_MonitoringDeleteIntGauge2ZTFE_MonitoringGetCellIntGauge2rL   ZTFE_MonitoringNewStringGauge0Z TFE_MonitoringDeleteStringGauge0Z!TFE_MonitoringGetCellStringGauge0ZTFE_MonitoringNewStringGauge1Z TFE_MonitoringDeleteStringGauge1Z!TFE_MonitoringGetCellStringGauge1ZTFE_MonitoringNewStringGauge2Z TFE_MonitoringDeleteStringGauge2Z!TFE_MonitoringGetCellStringGauge2ZTFE_MonitoringNewStringGauge3Z TFE_MonitoringDeleteStringGauge3Z!TFE_MonitoringGetCellStringGauge3ZTFE_MonitoringNewStringGauge4Z TFE_MonitoringDeleteStringGauge4Z!TFE_MonitoringGetCellStringGauge4rS   ZTFE_MonitoringNewBoolGauge0ZTFE_MonitoringDeleteBoolGauge0ZTFE_MonitoringGetCellBoolGauge0ZTFE_MonitoringNewBoolGauge1ZTFE_MonitoringDeleteBoolGauge1ZTFE_MonitoringGetCellBoolGauge1ZTFE_MonitoringNewBoolGauge2ZTFE_MonitoringDeleteBoolGauge2ZTFE_MonitoringGetCellBoolGauge2rX   ZTFE_MonitoringNewSampler0ZTFE_MonitoringDeleteSampler0ZTFE_MonitoringGetCellSampler0ZTFE_MonitoringNewSampler1ZTFE_MonitoringDeleteSampler1ZTFE_MonitoringGetCellSampler1ZTFE_MonitoringNewSampler2ZTFE_MonitoringDeleteSampler2ZTFE_MonitoringGetCellSampler2ra   objectr   r%   r8   rH   rK   rM   rR   rU   rW   rY   r\   r^   r`   rh   rb   rw   r   r   r   r   <module>   s   +
#3