o
    2h                     @   s   d Z ddlZG dd d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    d | _t | _d S N)super__init___session	threadingLock_initialization_lockself	__class__ X/var/www/html/chatgem/venv/lib/python3.10/site-packages/tensorboard/util/op_evaluator.pyr   >   s   
zPersistentOpEvaluator.__init__c              	   C   s   ddl m  m} | jA | jr	 W d   dS | }|  |   W d   n1 s0w   Y  |jddid}|j	||d| _W d   dS 1 sPw   Y  dS )z@Initialize the graph and session, if this has not yet been done.r   NGPU)device_count)graphconfig)
tensorflow.compat.v1compatv1r	   r   Graph
as_defaultinitialize_graphConfigProtoSession)r   tfr   r   r   r   r   _lazily_initializeC   s   

"z(PersistentOpEvaluator._lazily_initializec                 C      t d)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".NotImplementedErrorr
   r   r   r   r   R   s   z&PersistentOpEvaluator.initialize_graphc                 O   r   )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".r   r   argskwargsr   r   r   run[   s   	zPersistentOpEvaluator.runc                 O   sF   |    | j  | j|i |W  d    S 1 sw   Y  d S 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   r   r   r   r   r   <module>   s   