
    BVh                         d Z ddl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	       G d
 dej                  ej                               Zy)z?Utilities for including Python state in TensorFlow checkpoints.    N)constant_op)dtypes)ops)base)	tf_exportpy_stateztrain.experimental.PythonStatec                   ^    e Zd ZdZej
                  d        Zej
                  d        Zd Zy)PythonStatea  A mixin for putting Python state in an object-based checkpoint.

  This is an abstract class which allows extensions to TensorFlow's object-based
  checkpointing (see `tf.train.Checkpoint`). For example a wrapper for NumPy
  arrays:

  ```python
  import io
  import numpy

  class NumpyWrapper(tf.train.experimental.PythonState):

    def __init__(self, array):
      self.array = array

    def serialize(self):
      string_file = io.BytesIO()
      try:
        numpy.save(string_file, self.array, allow_pickle=False)
        serialized = string_file.getvalue()
      finally:
        string_file.close()
      return serialized

    def deserialize(self, string_value):
      string_file = io.BytesIO(string_value)
      try:
        self.array = numpy.load(string_file, allow_pickle=False)
      finally:
        string_file.close()
  ```

  Instances of `NumpyWrapper` are checkpointable objects, and will be saved and
  restored from checkpoints along with TensorFlow state like variables.

  ```python
  root = tf.train.Checkpoint(numpy=NumpyWrapper(numpy.array([1.])))
  save_path = root.save(prefix)
  root.numpy.array *= 2.
  assert [2.] == root.numpy.array
  root.restore(save_path)
  assert [1.] == root.numpy.array
  ```
  c                      y)z3Callback to serialize the object. Returns a string.N )selfs    X/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/trackable/python_state.py	serializezPythonState.serializeK           c                      y)z#Callback to deserialize the object.Nr   )r   string_values     r   deserializezPythonState.deserializeO   r   r   c                     t        j                         5  t        j                  | j	                         t
        j                        }ddd       t        |iS # 1 sw Y   t        iS xY w)z+Implements Trackable._serialize_to_tensors.)dtypeN)r   
init_scoper   constantr   r   stringPYTHON_STATE)r   values     r   _serialize_to_tensorsz!PythonState._serialize_to_tensorsS   sR    		 J""4>>#36==IeJ%  J%  s   4AA)N)	__name__
__module____qualname____doc__abcabstractmethodr   r   r   r   r   r   r
   r
      sA    +Z > > . .!r   r
   )	metaclass)r    r!   tensorflow.python.frameworkr   r   r   tensorflow.python.trackabler    tensorflow.python.util.tf_exportr   r   	TrackableABCMetar
   r   r   r   <module>r)      sN    E  3 . + , 6  +,:!$..CKK :! -:!r   