
    BVh!                     ~   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       ej4                  de      d               Zd Zd Zd Z eddg       ej4                  de      dd              ZddZ d Z!y) 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                 V    t        j                         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)tensors    X/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/saved_model/utils_impl.pybuild_tensor_infor   )   s-    0  
 $ % %	#F	++    c                    t        | t        j                        r?t        | t        j                        s%t        | t
        j                        st        |       S t        j                  t        j                  | j                        j                  | j                         j                               }t        | t        j                        rq| 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_infos     r   r   r   G   s    )99:
V]77
8
V2CC
D088))OOFLL)::##%..02+ 223060B0BK-171D1DK.5;5G5G5L5LK2 
 {{K	r   c                 ~   | j                   }t        j                         }t        j                  |      }|j
                  j                  j                  |j                         t        j                  | d      D ]>  }|j
                  j                  j                         j                  t        |             @ |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	components        r   r!   r!   Z   s    			$))++%66t<*((11*2L2LM<<$? /i  ++//1::"9-// 
r   c                     t        j                         rt        d      t        j                  t
        j                  t        j                         j                         | j                        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(   )ops    r   build_tensor_info_from_oprB   f   s[    @  
JL L		"	"  --/88:77
 r   z'saved_model.get_tensor_from_tensor_infoz-saved_model.utils.get_tensor_from_tensor_infoc                    xs t        j                         fd}| j                  d      }|dk(  r || j                        S |dk(  ret	        j
                   || j                  j                         || j                  j                         || j                  j                              S |dk(  rt        j                  | j                  j                        }t        j                  |      }| j                  j                   D cg c]  } ||j                         }}t#        j$                  ||d      S t'        d	| d
      c c}w )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                 P    j                  t        j                  |             S )Nimport_scope)get_tensor_by_namer
   prepend_name_scope)r(   graphrF   s    r   _get_tensorz0get_tensor_from_tensor_info.<locals>._get_tensor   s(    ##t,?A Ar   encodingr(   r)   r   )r7   Tr1   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   r5   r   decode_protor9   r   pack_sequence_as
ValueError)	r/   rI   rF   rJ   rK   r<   r;   r=   r9   s	    ``      r   get_tensor_from_tensor_inforR      sC   , 
*3((*%A ##J/({''((<%%K**>>?K**==>K**BBCE E %%++#44>>@J!..z:D..99;)+inn- ;J ;  zTJJ
4XJ ?     	;s   Ec                     |xs t        j                         }|j                  t        j                  | j                  |            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`
  rE   )r
   rL   as_graph_elementrH   r(   )r/   rI   rF   s      r   get_element_from_tensor_inforU      sB      
*3((*%				[--LI
K Kr   c                 2    t        j                  | ||       y )N)bst%swap_tensor_content_in_graph_function)meta_graph_deffrom_endinessto_endinesss      r   swap_function_tensor_contentr\      s    ++m[r   )NN)"__doc__tensorflow.core.frameworkr   tensorflow.core.protobufr   r   tensorflow.python.eagerr   tensorflow.python.frameworkr   rW   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!   rB   rR   rU   r\    r   r   <module>ri      s    3 / 3 / + ? 8 . + 5 4 7 @ . ' 6
*  ')NOQ./, 0Q,6&	&R 8>@ A./)  0A) XK*r   