o
    Õ2úh  ã                   @   sX   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„ dƒZ	G dd	„ d	ej
ƒZdS )
z(Experimental API for checkpoint adapter.é    N)ÚListÚOptional)Útensor)Úbasec                   @   s^   e Zd ZdZdefdd„Zdeej dee dejfdd„Z	de
ee ee f fd	d
„ZdS )ÚReshardCallbackzÈAPI 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                 C   ó   dS )zšReturns the local name of the object being restored.

    Override this method when the local name of object is different than in the
    checkpoint.
    N© )Úselfr	   r	   új/var/www/html/chatgem/venv/lib/python3.10/site-packages/tensorflow/python/checkpoint/checkpoint_adapter.pyÚobject_name   s   zReshardCallback.object_nameÚcheckpoint_valuesÚshape_and_slice_specc                 C   s   ~t |ƒdk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   r	   r	   r   Úreshard&   s   zReshardCallback.reshardc                 C   s   |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 checkpoint 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   r	   r	   r   Úupdate_restore_inputs<   s   z%ReshardCallback.update_restore_inputsN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ústrr   r   r   ÚTensorr   Útupler   r	   r	   r	   r   r      s    þý
üþr   c                   @   s|   e Zd ZdZeejdefdd„ƒƒZejde	j
defdd„ƒZejd	edee fd
d„ƒZd	edeeee f fdd„ZdS )ÚAbstractCheckpointAdapterz Abstract API for checkpoint adapter.

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

    Args:
      path: Path to checkpoint.
    Nr	   )Úclsr   r	   r	   r   Úcreate_from_checkpointW   ó    z0AbstractCheckpointAdapter.create_from_checkpointÚ	trackabler   c                 C   r   )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!   r	   r	   r   Úis_applicable`   r    z'AbstractCheckpointAdapter.is_applicableÚnamec                 C   r   )z;Returns the reshard callback for the trackable with `name`.Nr	   )r
   r#   r	   r	   r   Úget_reshard_callbackl   r    z.AbstractCheckpointAdapter.get_reshard_callbackc                 C   s6   |   |¡}|du r|dfS | ¡ r| ¡ |fS ||fS )z>Returns the updated name and ReshardCallback applicable to it.N)r$   r   )r
   r#   Úcallbackr	   r	   r   Úmaybe_reshardp   s   
z'AbstractCheckpointAdapter.maybe_reshardN)r   r   r   r   ÚclassmethodÚabcÚabstractmethodr   r   r   Ú	TrackableÚboolr"   r   r   r$   r   r&   r	   r	   r	   r   r   P   s    "r   )r   r(   Útypingr   r   Útensorflow.python.frameworkr   Útensorflow.python.trackabler   r   ÚABCr   r	   r	   r	   r   Ú<module>   s   9