o
    ?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   i/home/www/facesmatcher.com/pyenv/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   ZCompositeTensorr
   SparseTensorr   ZResourceVariable%_build_composite_tensor_info_internalr   
TensorInfor   Zas_dtyper   Zas_datatype_enum	get_shapeas_protovaluesname
coo_sparsevalues_tensor_nameindicesindices_tensor_nameZ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Zexpand_composites)Z
_type_specr   r   r   Zencode_structurer   	type_specZ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   Z
DT_INVALIDr   Zunknown_shaper   r!   )opr   r   r   build_tensor_info_from_opf   s    
r2   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)Zget_tensor_by_namer	   prepend_name_scoper!   )graphr4   r   r   _get_tensor   s   z0get_tensor_from_tensor_info.<locals>._get_tensorencodingr!   r"   r   )r*   c                    s   g | ]} |j qS r   r6   ).0r0   )r8   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_graphZ
WhichOneofr!   r
   r   r"   r%   r#   r&   r   ZStructuredValuer   r)   r   Zdecode_protor,   r   Zpack_sequence_as
ValueError)r'   r7   r4   r9   r/   r.   r,   r   )r8   r7   r4   r   get_tensor_from_tensor_info   s*   






r>   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`
  r3   )r	   r<   Zas_graph_elementr5   r!   )r'   r7   r4   r   r   r   get_element_from_tensor_info   s   r?   c                 C   s   t | || d S )N)bstZ%swap_tensor_content_in_graph_function)Zmeta_graph_defZfrom_endinessZto_endinessr   r   r   swap_function_tensor_content   s   rA   )NN)"__doc__Ztensorflow.core.frameworkr   Ztensorflow.core.protobufr   r   Ztensorflow.python.eagerr   Ztensorflow.python.frameworkr   r@   r   r   r	   r
   r   Ztensorflow.python.opsr   Ztensorflow.python.saved_modelr   Ztensorflow.python.utilr   r   Z tensorflow.python.util.tf_exportr   Z_DEPRECATION_MSG
deprecatedr   r   r   r2   r>   r?   rA   r   r   r   r   <module>   sB   
)

,