
    AVh	(                     *   d Z ddlmZ ddlmZ ddlmZ ddlmZ ddlm	Z	 ddlm
Z ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ d Z G d d      Z edg      dd       Z edg      dd       Z edg      dd       Zd Zd Zd Zd Zd Zy)zTensor Handle Operations.    )resource_handle_pb2)pywrap_tf_session)device)dtypes)ops)tensor)	array_ops)gen_data_flow_ops)compat)numpy_compat)	tf_exportc                 z    t        j                  t        | j                               t        j
                        S )z:Encode a ResourceHandle proto as custom numpy struct type.)dtype)r   
np_asarray	bytearraySerializeToStringr   np_resource)resource_handles    Q/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/ops/session_ops.pyencode_resource_handler   !   s.    		 	 1134F<N<N
     c                       e Zd ZdZd Zd Zd Zd Zd Ze	d        Z
d Zd	 Zd
 Zed        Zed        Zed        Zy)TensorHandlez3Represents a handle for a live tensor in a session.c                 p    t        j                  |      | _        d| _        || _        || _        d| _        y)aJ  Constructs a new tensor handle.

    A tensor handle for a persistent tensor is a python string
    that has the form of "tensor_name;unique_id;device_name".

    Args:
      handle: A tensor handle.
      dtype: The data type of the tensor represented by `handle`.
      session: The session in which the tensor is produced.
    NT)r   
as_str_any_handle_resource_handle_dtype_session_auto_gc_enabled)selfhandler   sessions       r   __init__zTensorHandle.__init__+   s4     $$V,DL DDKDM Dr   c                 h    | j                   r&| j                  j                  | j                         y y N)r    r   _register_dead_handler"   r!   s    r   __del__zTensorHandle.__del__<   s&    
mm))$++6 r   c                     | j                   S r&   r   r(   s    r   __str__zTensorHandle.__str__@   s    <<r   c                 2   | j                   st        j                         | _         | j                  j	                  d      d   | j                   _        t        j                  | j                   _        | j                  | j                   _	        | j                   S )z1The ResourceHandle representation of this handle.;)
r   r   ResourceHandleProtor   splitr   r   TENSOR_HANDLE_KEY	containernamer(   s    r   _get_resource_handlez!TensorHandle._get_resource_handleC   sp      1EEGd%)\\%7%7%<R%@d"):)L)Ld%#'<<d    r   c                 4    t        | j                               S )zConvert a TensorHandle object to a feedable numpy value.

    Returns:
      A numpy array of a custom struct type that can be used as a feed value
      to run().
    )r   r5   r(   s    r   to_numpy_arrayzTensorHandle.to_numpy_arrayL   s     "$";";"=>>r   c                     | j                   S )z)The string representation of this handle.r+   r(   s    r   r"   zTensorHandle.handleU   s     <<r   c                    | j                   st        d| j                  z        t        | j                  j
                  | j                  | j                        \  }}| j                  j                  ||| j                  i      S )z:Return the value of the tensor represented by this handle.3Persistent tensor %s may have already been deleted.	feed_dict)	r    	TypeErrorr"   _get_handle_readerr   graphr   r   run)r!   holderreaders      r   evalzTensorHandle.evalZ   sq      K$ % %'(;(;T\\(,5NFF==V/EFFr   c                    | j                   st        d| j                  z        d| _         t        | j                  j
                  d| j                        \  }}| j                  j                  ||| j                  i       y)z-Force the deletion of this persistent tensor.r:   Fr   r;   N)r    r=   r"   _get_handle_deleterr   r?   r   r@   )r!   rA   deleters      r   deletezTensorHandle.deletec   so      K$ % %!D)$--*=*=q$,,OOFGMMg&$++)>?r   c                 (    d| _         | j                  S )zReturn the raw handle of the tensor.

    Note that the method disables the automatic garbage collection of this
    persistent tensor. The caller is now responsible for managing the life
    time of the tensor.
    F)r    r   r(   s    r   get_raw_handlezTensorHandle.get_raw_handlel   s     "D<<r   c                 z    t        j                  |       }t        j                  |j	                  d      d         S )z&The device name encoded in the handle.r.   r/   )r   r   pydevcanonical_namer1   )r"   
handle_strs     r   _get_device_namezTensorHandle._get_device_namev   s5     ""6*J
 0 0 5b 9::r   c                 R    t        |       j                  d      }|d   dz   |d   z   S )zThe graph key for reader.r.   r   r/   )strr1   )r"   handle_partss     r   _get_reader_keyzTensorHandle._get_reader_key|   s1     v;$$S)L?S <#333r   c                 `    | j                   j                  dz   t        j                  |      z   S )zThe graph key for mover.r.   )opr4   r   rR   )feederr"   s     r   _get_mover_keyzTensorHandle._get_mover_key   s'     99>>C,">">v"FFFr   N)__name__
__module____qualname____doc__r$   r)   r,   r5   r7   propertyr"   rC   rG   rI   staticmethodrN   rR   rV    r   r   r   r   (   s    ;!"7!?  G@ ; ;
 4 4
 G Gr   r   get_session_handle)v1Nc                     t        | t        j                        st        d      t	        j
                  |       5  t        j                  | |      cddd       S # 1 sw Y   yxY w)aB  Return the handle of `data`.

  This is EXPERIMENTAL and subject to change.

  Keep `data` "in-place" in the runtime and create a handle that can be
  used to retrieve `data` in a subsequent run().

  Combined with `get_session_tensor`, we can keep a tensor produced in
  one run call in place, and use it as the input in a future run call.

  Args:
    data: A tensor to be stored in the session.
    name: Optional name prefix for the return tensor.

  Returns:
    A scalar string tensor representing a unique handle for `data`.

  Raises:
    TypeError: if `data` is not a Tensor.

  Example:

  ```python
  c = tf.multiply(a, b)
  h = tf.compat.v1.get_session_handle(c)
  h = sess.run(h)

  p, a = tf.compat.v1.get_session_tensor(h.handle, tf.float32)
  b = tf.multiply(a, 10)
  c = sess.run(b, feed_dict={p: h.handle})
  ```

  z`data` must be of type Tensor.r4   N)
isinstance
tensor_libTensorr=   r   colocate_withr
   r^   )datar4   s     r   r^   r^      sZ    F 
D*++	,
4
55  A//4@A A As   AA%get_session_tensorc                 2   t         j                  |       }t        j                  |      5  t	        j
                  t        j                        }t        |j                  ||       t        j                  |||      }ddd       ||fS # 1 sw Y   fS xY w)aM  Get the tensor of type `dtype` by feeding a tensor handle.

  This is EXPERIMENTAL and subject to change.

  Get the value of the tensor from a tensor handle. The tensor
  is produced in a previous run() and stored in the state of the
  session.

  Args:
    handle: The string representation of a persistent tensor handle.
    dtype: The type of the output tensor.
    name: Optional name prefix for the return tensor.

  Returns:
    A pair of tensors. The first is a placeholder for feeding a
    tensor handle and the second is the tensor in the session state
    keyed by the tensor handle.

  Example:

  ```python
  c = tf.multiply(a, b)
  h = tf.compat.v1.get_session_handle(c)
  h = sess.run(h)

  p, a = tf.compat.v1.get_session_tensor(h.handle, tf.float32)
  b = tf.multiply(a, 10)
  c = sess.run(b, feed_dict={p: h.handle})
  ```

  ra   N)r   rN   r   r   r	   placeholderr   string_register_handle_feederr?   r
   rg   )r"   r   r4   handle_devicerA   r   s         r   rg   rg      s    B //7-
zz-  L""6==1FFLL&%811&%dKFL &		L &	s   AB

Bdelete_session_tensorc                    t         j                  |       }t        j                  |      5  t	        j
                  t        j                        }t        j                  ||      }ddd       ||fS # 1 sw Y   fS xY w)a  Delete the tensor for the given tensor handle.

  This is EXPERIMENTAL and subject to change.

  Delete the tensor of a given tensor handle. The tensor is produced
  in a previous run() and stored in the state of the session.

  Args:
    handle: The string representation of a persistent tensor handle.
    name: Optional name prefix for the return tensor.

  Returns:
    A pair of graph elements. The first is a placeholder for feeding a
    tensor handle and the second is a deletion operation.
  ra   N)
r   rN   r   r   r	   ri   r   rj   r
   rm   )r"   r4   rl   rA   rF   s        r   rm   rm      ss    " //7-
zz-  I""6==1F55f4HGI '	I '	s   ;A22A>c                 J    || j                   |j                  j                  <   y r&   )_handle_feedersrT   r4   )r?   rU   r   s      r   rk   rk      s    */%		'r   c                 `    | j                   j                  |j                  j                        S r&   )rp   getrT   r4   )r?   rU   s     r   _get_handle_feederrs      s!    				"	"699>>	22r   c                    t         j                  |      }| j                  j                  |      }|t         j	                  |      }| j                         5  | j                  |      5  t        j                  t        j                        }t        |j                  ||       t        j                  ||      }ddd       ddd       f}|| j                  |<   |S # 1 sw Y   &xY w# 1 sw Y   *xY w)z'Return a read subgraph for this handle.N)r   rR   _handle_readersrr   rN   
as_defaultr   r	   ri   r   rj   rk   r?   r
   rg   )r?   r"   r   	graph_keyresultrl   rA   rB   s           r   r>   r>      s    **62)  $$Y/&^ 11&9M				 CU\\-8 C$$V]]3ffllFE: 33FEBfC C fF'-E)$	-C C C Cs%   C,*AC ;C, C)	%C,,C5c                    t        | |      }|yt        j                  |      }|j                  j                  |k(  ryt        j                  ||      }| j                  j                  |      }|t        | ||      \  }}| j                         5  | j	                  |j                  j                        5  t        j                  |      }	ddd       ddd       |	f}|| j                  |<   |S # 1 sw Y   &xY w# 1 sw Y   *xY w)z:Return a move subgraph for this pair of feeder and handle.N)rs   r   rN   rT   r   rV   _handle_moversrr   r>   rv   r
   r^   )
r?   rU   r"   r   rl   rw   rx   rA   rB   movers
             r   _get_handle_moverr|     s    
UF
+%
]//7-YY&))&&9)##I.&^'vu=NFF				 ;U\\&))*:*:; ;226:e; ;e_F&,E#	-	; ; ; ;s$   &C?8C3C?3C<	8C??Dc                    | j                   j                  |      }|t        j                  |      }| j	                         5  | j                  |      5  t        j                  t        j                        }t        j                  |      }ddd       ddd       f}|| j                   |<   |S # 1 sw Y   &xY w# 1 sw Y   *xY w)z+Return a deletion subgraph for this handle.N)_handle_deletersrr   r   rN   rv   r   r	   ri   r   rj   r
   rm   )r?   deleter_keyr"   rx   rl   rA   rF   s          r   rE   rE   !  s    !!%%k2&^ 11&9M				 @U\\-8 @$$V]]3f!77?g@ @ gF*0E;'	-@ @ @ @s$   B?9B3B?3B<	8B??Cr&   )rZ   tensorflow.core.frameworkr   tensorflow.python.clientr   tensorflow.python.frameworkr   rK   r   r   r   rc   tensorflow.python.opsr	   r
   tensorflow.python.utilr   r    tensorflow.python.util.tf_exportr   r   r   r^   rg   rm   rk   rs   r>   r|   rE   r]   r   r   <module>r      s       : 6 7 . + < + 3 ) / 6]G ]G@ #$%'A &'AT #$%% &%P &'( ).03 *r   