
    1Vh                     r    d Z ddlZddlmZ  e       Z G d d      Zd	dZda	 ej                         Zd Zy)
z6Provides a lazy wrapper for deferring Tensor creation.    N)tf2c                       e Zd ZdZd Zd Zy)LazyTensorCreatora  Lazy auto-converting wrapper for a callable that returns a `tf.Tensor`.

    This class wraps an arbitrary callable that returns a `Tensor` so that it
    will be automatically converted to a `Tensor` by any logic that calls
    `tf.convert_to_tensor()`. This also memoizes the callable so that it is
    called at most once.

    The intended use of this class is to defer the construction of a `Tensor`
    (e.g. to avoid unnecessary wasted computation, or ensure any new ops are
    created in a context only available later on in execution), while remaining
    compatible with APIs that expect to be given an already materialized value
    that can be converted to a `Tensor`.

    This class is thread-safe.
    c                     t        |      st        d|z        || _        d| _        t	        j
                         | _        t                y)zInitializes a LazyTensorCreator object.

        Args:
          tensor_callable: A callable that returns a `tf.Tensor`.
        zNot a callable: %rN)callable
ValueError_tensor_callable_tensor	threadingRLock_tensor_lock"_register_conversion_function_once)selftensor_callables     T/home/dcms/DCMS/lib/python3.12/site-packages/tensorboard/util/lazy_tensor_creator.py__init__zLazyTensorCreator.__init__,   sA     (1OCDD /%OO-*,    c                 X   | j                   | j                   t        u rj| j                  5  | j                   t        u rt        d      | j                    t        | _         | j	                         | _         d d d        | j                   S | j                   S # 1 sw Y   | j                   S xY w)Nz4Cannot use LazyTensorCreator with reentrant callable)r
   _CALL_IN_PROGRESS_SENTINELr   RuntimeErrorr	   )r   s    r   __call__zLazyTensorCreator.__call__9   s    <<4<<3M#M"" ;<<#==&N  \\)#=DL#'#8#8#:DL; ||t||; ||s   A
BB)N)__name__
__module____qualname____doc__r   r    r   r   r   r      s     -
r   r   Fc                     ~t        | t              st        d| z        |rt        d       |        }|d |j                  fvrt        d|j                  d|      |S )Nz"Expected LazyTensorCreator, got %rz1Cannot use LazyTensorCreator to create ref tensorz1Cannot convert LazyTensorCreator returning dtype z
 to dtype )
isinstancer   r   dtype)valuer   nameas_reftensors        r   _lazy_tensor_creator_converterr$   F   sj    e./?%GHHNOOWFT6<<((||U$
 	
 Mr   c                      t         s8t        5  t         s"da t        j                  t        t
        d       ddd       yy# 1 sw Y   yxY w)a  Performs one-time registration of `_lazy_tensor_creator_converter`.

    This helper can be invoked multiple times but only registers the conversion
    function on the first invocation, making it suitable for calling when
    constructing a LazyTensorCreator.

    Deferring the registration is necessary because doing it at at module import
    time would trigger the lazy TensorFlow import to resolve, and that in turn
    would break the delicate `tf.summary` import cycle avoidance scheme.
    Tr   )	base_typeconversion_funcpriorityN)_conversion_registered_conversion_registered_locktf#register_tensor_conversion_functionr   r$   r   r   r   r   r   [   sI     "( 	))-&66/$B	 	 "	 	s   )A  A	)NNF)r   r   tensorboard.compatr   r+   objectr   r   r$   r)   Lockr*   r   r   r   r   <module>r0      sF    =  (
 $X ( (V"  ,inn. r   