o
    i e                     @   sl   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 Z
d	d
 Zdd Zdd Zdd ZdS )z3Decorator to overrides the gradient for a function.    )pywrap_tf_session)cpp_shape_inference_pb2)dtypes)core)compatc                 C   sl   t | tjrt | tjrJ | jj }t|| 	 }W d    n1 s'w   Y  t
jjt|S )N)
isinstancer   SymbolValuegraph_c_graphgetr   GetHandleShapeAndType_as_tf_outputr   CppShapeInferenceResult
HandleData
FromStringr   as_bytes)graph_opc_graphhandle_data r   U/var/www/myenv/lib/python3.10/site-packages/tensorflow/python/ops/handle_data_util.pyget_resource_handle_data   s   
r   c                 C   s   t | tjr	| jS t| S )z!Obtains HandleData from a tensor.)r   r   r	   _handle_datar   )source_tr   r   r   get_handle_data$   s   r   c                 C   s2   |j tjks|j tjkrt| }t|| dS dS )a  Copies HandleData for variant and resource type tensors if available.

  The CppShapeInferenceResult::HandleData proto contains information about the
  shapes and types of the element tensors of resource/variant type tensors.
  We need to copy this across function boundaries, i.e., when capturing a
  placeholder or when returning a function tensor as output. If we don't do this
  the element tensors will have unknown shapes, e.g., if a TensorList variant
  tensor is captured as a placeholder, elements popped from that list would have
  unknown shape.

  Args:
    source_t: The tensor to copy HandleData from.
    target_t: The tensor to copy HandleData to.
  N)dtyper   resourcevariantr   set_handle_data)r   target_tr   r   r   r   copy_handle_data+   s
   r!   c                 C   sv   |du s
|j r
|jsdS t| tjr|| _dS | jj }t	
||  |  W d   dS 1 s4w   Y  dS )z%Sets handle data on the giver tensor.N)is_setshape_and_typer   r   r	   r   r
   r   r   r   SetHandleShapeAndTyper   SerializeToString)r    r   r   r   r   r   r   @   s   "r   c                 C   s2   t j }d|_|jt jj|  |jd |S )NT)shaper   )	r   r   r   r"   r#   appendHandleShapeAndTypeas_protoas_datatype_enum)r&   r   r   r   r   r   create_handle_dataS   s   

r+   N)__doc__tensorflow.python.clientr   tensorflow.python.frameworkr   r   tensorflow.python.typesr   tensorflow.python.utilr   r   r   r!   r   r+   r   r   r   r   <module>   s   