
    BVh                     v    d Z ddlZddlmZmZ ddlmZ ddlmZ  G d d      Z	 G d d	ej                        Zy)
z(Experimental API for checkpoint adapter.    N)ListOptional)tensor)basec                       e Zd ZdZdefdZdeej                     dee   dej                  fdZ	de
ee   ee   f   fdZy)	ReshardCallbackzAPI to reshard a checkpoint value during restore.

  When a ReshardCallback is attached to a CheckpointPosition, the restored value
  of the checkpoint position is resharded based on this callback.
  returnc                      y)zReturns the local name of the object being restored.

    Override this method when the local name of object is different than in the
    checkpoint.
    N )selfs    _/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/checkpoint/checkpoint_adapter.pyobject_namezReshardCallback.object_name   s         checkpoint_valuesshape_and_slice_specc                 @    ~t        |      dk7  rt        d      |d   S )az  Reshards the checkpoint values as read from the checkpoint file.

    Override this to reshard/modify the restored values
    Args:
      checkpoint_values: The values returned by the restore op, as read from
        file.
      shape_and_slice_spec: The shape and slice spec required by the caller.

    Returns:
      List of restored Tensor values after being resharded.
       z2Default reshard expects a single checkpoint value.r   )len
ValueError)r   r   r   s      r   reshardzReshardCallback.reshard&   s,      	
"KLLQr   c                     |g|gfS )a<  Updates the specs to restore op.

    Override this method if the arguments to restore op need to be updated as
    per the resharding required.
    Args:
      checkpoint_key: The cehckpopoint key as requested by the caller
      shape_and_slice_spec: The shape and slice spec as requested by caller

    Returns:
    Tuple of list of checkpoint_keys and specs that the restore op should fetch
    as per the resharding requirement. The length of checkpoint keys returned by
    this method will match the length of checkpoint_values that are input to
    `reshard`.
    r   )r   checkpoint_keyr   s      r   update_restore_inputsz%ReshardCallback.update_restore_inputs<   s    " 3455r   N)__name__
__module____qualname____doc__strr   r   r   Tensorr   tupler   r   r   r   r   r      sd    3  fmm,  !I  }}	 ,6T#YS	!"6r   r   c                       e Zd ZdZeej                  defd              Zej                  de	j                  defd       Zej                  dedee   fd       Zdedeeee   f   fd	Zy
)AbstractCheckpointAdapterzAbstract API for checkpoint adapter.

  This is an experimental API that specifies how checkpoint restore should be
  adapted for specific trackable objects.
  pathc                      y)zdCreate factory to create an Adapter from checkpoint.

    Args:
      path: Path to checkpoint.
    Nr   )clsr#   s     r   create_from_checkpointz0AbstractCheckpointAdapter.create_from_checkpointW       r   	trackabler	   c                      y)a  Returns whether the adapter is applicable to trackable for resharding.

    Args:
      trackable: A Trackable object that is being restored.

    Returns:
      A Boolean indicating if the checkpoint value for this Trackable should be
      resharded.
    Nr   )r   r(   s     r   is_applicablez'AbstractCheckpointAdapter.is_applicable`   r'   r   namec                      y)z;Returns the reshard callback for the trackable with `name`.Nr   )r   r+   s     r   get_reshard_callbackz.AbstractCheckpointAdapter.get_reshard_callbackl   r'   r   c                 |    | j                  |      }||dfS |j                         r|j                         |fS ||fS )z>Returns the updated name and ReshardCallback applicable to it.N)r-   r   )r   r+   callbacks      r   maybe_reshardz'AbstractCheckpointAdapter.maybe_reshardp   sL    ((.H4Z!!#X-->r   N)r   r   r   r   classmethodabcabstractmethodr   r&   r   	Trackableboolr*   r   r   r-   r    r0   r   r   r   r"   r"   P   s         	T^^ 	 	 	 Fs Fx/H F F c8O3L.L(M r   r"   )r   r2   typingr   r   tensorflow.python.frameworkr   tensorflow.python.trackabler   r   ABCr"   r   r   r   <module>r:      s1    / 
 ! . ,66 66r' 'r   