o
    i e!                     @   s0  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dlmZ ddlmZ ddlmZ ddlmZ dZeddgdededd Zdd Zdd Zdd Zeddgdeded&d d!Zd&d"d#Z d$d% Z!dS )'z,SavedModel utility functions implementation.    )	types_pb2)meta_graph_pb2)
struct_pb2)context)byte_swap_tensor)composite_tensor)dtypes)ops)sparse_tensor)tensor_shape)resource_variable_ops)nested_structure_coder)deprecation)nest)	tf_exportzThis API was designed for TensorFlow v1. See https://www.tensorflow.org/guide/migrate for instructions on how to migrate your code to TensorFlow v2.zsaved_model.build_tensor_infoz#saved_model.utils.build_tensor_info)v1Nc                 C   s   t  rtdt| S )a  Utility function to build TensorInfo proto from a Tensor.

  Args:
    tensor: Tensor or SparseTensor whose name, dtype and shape are used to
        build the TensorInfo. For SparseTensors, the names of the three
        constituent Tensors are used.

  Returns:
    A TensorInfo protocol buffer constructed based on the supplied argument.

  Raises:
    RuntimeError: If eager execution is enabled.

  @compatibility(TF2)
  This API is not compatible with eager execution as `tensor` needs to be a
  graph tensor, and there is no replacement for it in TensorFlow 2.x. To start
  writing programs using TensorFlow 2.x, please refer to the [Effective
  TensorFlow 2](https://www.tensorflow.org/guide/effective_tf2) guide.
  @end_compatibility
  z8`build_tensor_info` is not supported in eager execution.)r   executing_eagerlyRuntimeErrorbuild_tensor_info_internal)tensor r   W/var/www/myenv/lib/python3.10/site-packages/tensorflow/python/saved_model/utils_impl.pybuild_tensor_info)   s   r   c                 C   s   t | tjrt | tjst | tjst| S tj	t
| jj|   d}t | tjr@| jj|j_| jj|j_| jj|j_|S | j|_|S )z9Utility function to build TensorInfo proto from a Tensor.)dtyper   )
isinstancer   CompositeTensorr
   SparseTensorr   ResourceVariable%_build_composite_tensor_info_internalr   
TensorInfor   as_dtyper   as_datatype_enum	get_shapeas_protovaluesname
coo_sparsevalues_tensor_nameindicesindices_tensor_namedense_shapedense_shape_tensor_name)r   tensor_infor   r   r   r   G   s"   


r   c                 C   sV   | j }t }t|}|jj|j t	j
| ddD ]}|jj t| q|S )zBUtility function to build TensorInfo proto from a CompositeTensor.Texpand_composites)
_type_specr   r   r   encode_structurer   	type_specCopyFromtype_spec_valuer   flatten
componentsaddr   )r   specr,   
spec_proto	componentr   r   r   r   Z   s   
r   c                 C   s,   t  rtdtjtjt 	 | j
dS )a:  Utility function to build TensorInfo proto from an Op.

  Note that this function should be used with caution. It is strictly restricted
  to TensorFlow internal use-cases only. Please make sure you do need it before
  using it.

  This utility function overloads the TensorInfo proto by setting the name to
  the Op's name, dtype to DT_INVALID and tensor_shape as None. One typical usage
  is for the Op of the call site for the defunned function:
  ```python
    @function.defun
    def some_variable_initialization_fn(value_a, value_b):
      a = value_a
      b = value_b

    value_a = constant_op.constant(1, name="a")
    value_b = constant_op.constant(2, name="b")
    op_info = utils.build_op_info(
        some_variable_initialization_fn(value_a, value_b))
  ```

  Args:
    op: An Op whose name is used to build the TensorInfo. The name that points
        to the Op could be fetched at run time in the Loader session.

  Returns:
    A TensorInfo protocol buffer constructed based on the supplied argument.

  Raises:
    RuntimeError: If eager execution is enabled.
  z@`build_tensor_info_from_op` is not supported in eager execution.)r   r   r%   )r   r   r   r   r   r   
DT_INVALIDr   unknown_shaper#   r%   )opr   r   r   build_tensor_info_from_opf   s    
r=   z'saved_model.get_tensor_from_tensor_infoz-saved_model.utils.get_tensor_from_tensor_infoc                    s   pt  fdd | d}|dkr | jS |dkr2t | jj | jj | jj	S |dkrVt
j| jjd}t|} fdd	| jjD }tj||d
dS td| d)ay  Returns the Tensor or CompositeTensor described by a TensorInfo proto.

  Args:
    tensor_info: A TensorInfo proto describing a Tensor or SparseTensor or
      CompositeTensor.
    graph: The tf.Graph in which tensors are looked up. If None, the
        current default graph is used.
    import_scope: If not None, names in `tensor_info` are prefixed with this
        string before lookup.

  Returns:
    The Tensor or SparseTensor or CompositeTensor in `graph` described by
    `tensor_info`.

  Raises:
    KeyError: If `tensor_info` does not correspond to a tensor in `graph`.
    ValueError: If `tensor_info` is malformed.
  c                    s     tj| dS )Nimport_scope)get_tensor_by_namer	   prepend_name_scoper%   )graphr?   r   r   _get_tensor   s   z0get_tensor_from_tensor_info.<locals>._get_tensorencodingr%   r&   r   )r3   c                    s   g | ]} |j qS r   rB   ).0r9   )rD   r   r   
<listcomp>   s    z/get_tensor_from_tensor_info.<locals>.<listcomp>Tr-   zInvalid TensorInfo.encoding: zJ. Expected `coo_sparse`, `composite_tensor`, or `name` for a dense tensor.)r	   get_default_graph
WhichOneofr%   r
   r   r&   r)   r'   r+   r   StructuredValuer   r1   r   decode_protor5   r   pack_sequence_as
ValueError)r,   rC   r?   rE   r8   r7   r5   r   )rD   rC   r?   r   get_tensor_from_tensor_info   s*   






rN   c                 C   s"   |pt  }|t j| j|dS )a  Returns the element in the graph described by a TensorInfo proto.

  Args:
    tensor_info: A TensorInfo proto describing an Op or Tensor by name.
    graph: The tf.Graph in which tensors are looked up. If None, the current
      default graph is used.
    import_scope: If not None, names in `tensor_info` are prefixed with this
      string before lookup.

  Returns:
    Op or tensor in `graph` described by `tensor_info`.

  Raises:
    KeyError: If `tensor_info` does not correspond to an op or tensor in `graph`
  r>   )r	   rH   as_graph_elementrA   r%   )r,   rC   r?   r   r   r   get_element_from_tensor_info   s   rP   c                 C   s   t | || d S )N)bst%swap_tensor_content_in_graph_function)meta_graph_deffrom_endinessto_endinessr   r   r   swap_function_tensor_content   s   rV   )NN)"__doc__tensorflow.core.frameworkr   tensorflow.core.protobufr   r   tensorflow.python.eagerr   tensorflow.python.frameworkr   rQ   r   r   r	   r
   r   tensorflow.python.opsr   tensorflow.python.saved_modelr   tensorflow.python.utilr   r    tensorflow.python.util.tf_exportr   _DEPRECATION_MSG
deprecatedr   r   r   r=   rN   rP   rV   r   r   r   r   <module>   sB   
)

,