
    1Vh                     $    d Z ddlZ G d d      Zy)zFTensorBoard helper routine for TF op evaluator.

Requires TensorFlow.
    Nc                   :     e Zd ZdZ fdZd Zd Zd Zd Z x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                 b    t         |           d | _        t        j                         | _        y N)super__init___session	threadingLock_initialization_lock)self	__class__s    M/home/dcms/DCMS/lib/python3.12/site-packages/tensorboard/util/op_evaluator.pyr   zPersistentOpEvaluator.__init__>   s$    $-NN$4!    c                 f   ddl mc m} | j                  5  | j                  r
	 ddd       y|j                         }|j                         5  | j                          ddd       |j                  ddi      }|j                  ||      | _        ddd       y# 1 sw Y   >xY w# 1 sw Y   yxY w)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   s       r   _lazily_initializez(PersistentOpEvaluator._lazily_initializeC   s     	*)&& 	C}}	C 	C HHJE!!# (%%'( ^^%^<FJJU6JBDM	C 	C( (		C 	Cs(   B' B'B4B'B$	 B''B0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".NotImplementedError)r   s    r   r   z&PersistentOpEvaluator.initialize_graphR   s    
 ";
 	
r   c                     t        d      )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kwargss      r   runzPersistentOpEvaluator.run[   s     ""DEEr   c                     | j                          | j                  j                         5   | j                  |i |cd d d        S # 1 sw Y   y xY wr   )r   r	   r   r'   r$   s      r   __call__zPersistentOpEvaluator.__call__f   sF    !]]%%' 	-488T,V,	- 	- 	-s   AA)
__name__
__module____qualname____doc__r   r   r   r'   r)   __classcell__)r   s   @r   r   r      s$    #J5
C
	F-r   r   )r-   r
   r    r   r   <module>r0      s    Q- Q-r   