B
    d                 @   s    d Z ddlZG dd deZdS )zFTensorBoard helper routine for TF op evaluator.

Requires TensorFlow.
    Nc                   s@   e Zd ZdZ fddZdd Zdd Zdd	 Zd
d Z  Z	S )PersistentOpEvaluatora  Evaluate a fixed TensorFlow graph repeatedly, safely, efficiently.

    Extend this class to create a particular kind of op evaluator, like an
    image encoder. In `initialize_graph`, create an appropriate TensorFlow
    graph with placeholder inputs. In `run`, evaluate this graph and
    return its result. This class will manage a singleton graph and
    session to preserve memory usage, and will ensure that this graph and
    session do not interfere with other concurrent sessions.

    A subclass of this class offers a threadsafe, highly parallel Python
    entry point for evaluating a particular TensorFlow graph.

    Example usage:

        class FluxCapacitanceEvaluator(PersistentOpEvaluator):
          """Compute the flux capacitance required for a system.

          Arguments:
            x: Available power input, as a `float`, in jigawatts.

          Returns:
            A `float`, in nanofarads.
          """

          def initialize_graph(self):
            self._placeholder = tf.placeholder(some_dtype)
            self._op = some_op(self._placeholder)

          def run(self, x):
            return self._op.eval(feed_dict: {self._placeholder: x})

        evaluate_flux_capacitance = FluxCapacitanceEvaluator()

        for x in xs:
          evaluate_flux_capacitance(x)
    c                s"   t t|   d | _t | _d S )N)superr   __init___session	threadingLock_initialization_lock)self)	__class__ O/var/www/html/venv/lib/python3.7/site-packages/tensorboard/util/op_evaluator.pyr   >   s    zPersistentOpEvaluator.__init__c          
   C   sv   ddl m  m} | jT | jr$dS | }|  |   W dQ R X |jddid}|j	||d| _W dQ R X dS )z@Initialize the graph and session, if this has not yet been done.r   NZGPU)Zdevice_count)graphconfig)
Ztensorflow.compat.v1compatZv1r   r   ZGraph
as_defaultinitialize_graphZConfigProtoSession)r	   tfr   r   r   r   r   _lazily_initializeC   s    
z(PersistentOpEvaluator._lazily_initializec             C   s   t ddS )zCreate the TensorFlow graph needed to compute this operation.

        This should write ops to the default graph and return `None`.
        z-Subclasses must implement "initialize_graph".N)NotImplementedError)r	   r   r   r   r   R   s    z&PersistentOpEvaluator.initialize_graphc             O   s   t ddS )a  Evaluate the ops with the given input.

        When this function is called, the default session will have the
        graph defined by a previous call to `initialize_graph`. This
        function should evaluate any ops necessary to compute the result
        of the query for the given *args and **kwargs, likely returning
        the result of a call to `some_op.eval(...)`.
        z Subclasses must implement "run".N)r   )r	   argskwargsr   r   r   run[   s    	zPersistentOpEvaluator.runc          	   O   s*   |    | j  | j||S Q R X d S )N)r   r   r   r   )r	   r   r   r   r   r   __call__f   s    zPersistentOpEvaluator.__call__)
__name__
__module____qualname____doc__r   r   r   r   r   __classcell__r   r   )r
   r   r      s   $	r   )r   r   objectr   r   r   r   r   <module>   s   