o
    2h                     @   sp   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 dejejdZdS )z?Utilities for including Python state in TensorFlow checkpoints.    N)constant_op)dtypes)ops)base)	tf_exportpy_stateztrain.experimental.PythonStatec                   @   s4   e Zd ZdZejdd Zejdd Zdd ZdS )	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                 C      dS )z3Callback to serialize the object. Returns a string.N )selfr
   r
   c/var/www/html/chatgem/venv/lib/python3.10/site-packages/tensorflow/python/trackable/python_state.py	serializeK       zPythonState.serializec                 C   r	   )z#Callback to deserialize the object.Nr
   )r   string_valuer
   r
   r   deserializeO   r   zPythonState.deserializec                 C   sJ   t   tj|  tjd}W d   t|iS 1 sw   Y  t|iS )z+Implements Trackable._serialize_to_tensors.)dtypeN)r   
init_scoper   constantr   r   stringPYTHON_STATE)r   valuer
   r
   r   _serialize_to_tensorsS   s   

z!PythonState._serialize_to_tensorsN)	__name__
__module____qualname____doc__abcabstractmethodr   r   r   r
   r
   r
   r   r      s    -

r   )	metaclass)r   r   tensorflow.python.frameworkr   r   r   tensorflow.python.trackabler    tensorflow.python.util.tf_exportr   r   	TrackableABCMetar   r
   r
   r
   r   <module>   s    