
    &Vf                     ,    d Z ddlZ G d d          ZdS )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                     t                                                       d | _        t          j                    | _        d S N)super__init___session	threadingLock_initialization_lock)self	__class__s    Z/var/www/html/software/conda/lib/python3.11/site-packages/tensorboard/util/op_evaluator.pyr   zPersistentOpEvaluator.__init__>   s6    $-N$4$4!!!    c                    ddl mc m} | j        5  | j        r	 ddd           dS |                                }|                                5  |                                  ddd           n# 1 swxY w Y   |                    ddi          }|	                    ||          | _        ddd           dS # 1 swxY w 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   s       r   _lazily_initializez(PersistentOpEvaluator._lazily_initializeC   s    	*))))))))& 	C 	C} 	C 	C 	C 	C 	C 	C 	C 	C HHJJE!!## ( (%%'''( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ^^%^<<FJJU6JBBDM	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	Cs:   	B<(B<A0$B<0A4	4B<7A4	87B<<C C 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EEr   c                     |                                   | j                                        5   | j        |i |cd d d            S # 1 swxY w Y   d S r   )r   r	   r   r'   r$   s      r   __call__zPersistentOpEvaluator.__call__f   s    !!!]%%'' 	- 	-48T,V,,	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	-s   AAA)
__name__
__module____qualname____doc__r   r   r   r'   r)   __classcell__)r   s   @r   r   r      s        # #J5 5 5 5 5
C C C
 
 
	F 	F 	F- - - - - - -r   r   )r-   r
   r    r   r   <module>r0      sY        Q- Q- Q- Q- Q- Q- Q- Q- Q- Q-r   