U
    yhT                     @   s   U d dl mZ d dlmZmZmZmZ d dlmZ	 d dl
m  m  mZ d dlm  m  mZ d dlmZmZ d dlmZmZmZmZ g Zee ed< G dd dZdS )	    )Future)AnyDictListOptionalN)MetadataSTATE_DICT_TYPE)LoadPlannerSavePlannerStorageReaderStorageWriter__all__c                	   @   s~   e Zd ZdZddddddeeeej e	e
ee ee dddZeed	d
dZeed	ddZeeef dd	ddZdS )_Checkpointera  This base class specefies a high level API for saving and loading
    distributed `state_dict` 's. It provides an abstraction over the low-level APIs
    provided by :py:mod:`torch.distributed.checkpoint.storage`, essentially calling
    :py:meth: `torch.distributed.state_dict_saver.save` and
    :py:meth: `torch.distributed.state_dict_loader.load` with the provided storage
    readers and writers.

    .. warning::
        This feature is experimental and subject to removal/change.

    Nr   F)process_groupcoordinator_rankno_distload_plannersave_plannerstorage_writerstorage_readerr   r   r   r   r   c                C   s.   || _ || _|| _|| _|| _|| _|| _dS )a{  Initializes the Checkpointer instance.

        Args:
            storage_writer: Instance of StorageWrite use to perform writes.
            storage_reader: StorageReader used to load data from.
            process_group: ProcessGroup to be used for cross-rank synchronization.
            coordinator_rank: Rank to use to coordinate the checkpoint. rank0 is used by default.
            no_dist: If ``True``, distributed checkpoint will not load in SPMD style. (Default: ``False``)
            loader_planner: Instance of LoadPlanner to use when loading.
            save_planner: Instance of SavePlanner to use when saving.
        Nr   )selfr   r   r   r   r   r   r    r   \/var/www/html/venv/lib/python3.8/site-packages/torch/distributed/checkpoint/_checkpointer.py__init__    s    z_Checkpointer.__init__)
state_dictreturnc                 C   s    t j|| j| j| j| j| jdS )ziCalls :py:meth: `torch.distributed.state_dict_saver.save`. Utilizing values passed during initialization.)r   r   r   planner)saversaver   r   r   r   r   r   r   r   r   r   r   >   s    z_Checkpointer.savec                 C   s   t j|| j| j| jdS )z
        Calls :py:meth: `torch.distributed.state_dict_saver._async_save`. Utilizing values passed during initialization.

        Returns:
            Future: A future holding the resultant Metadata object from `save`.
        )r   r   r   )r   
async_saver   r   r   r    r   r   r   r!   L   s    
z_Checkpointer.async_savec                 C   s   t j|| j| j| jd dS )zjCalls :py:meth: `torch.distributed.state_dict_loader.load`. Utilizing values passed during initialization.)r   r   r   N)loaderloadr   r   r   r    r   r   r   r#   ]   s    z_Checkpointer.load)__name__
__module____qualname____doc__r   r   r   distZProcessGroupintboolr	   r
   r   r   r   r   r   r!   r   strr   r#   r   r   r   r   r      s,    r   )concurrent.futuresr   typingr   r   r   r   Ztorch.distributeddistributedr(   Z.torch.distributed.checkpoint.state_dict_loader
checkpointZstate_dict_loaderr"   Z-torch.distributed.checkpoint.state_dict_saverZstate_dict_saverr   Z%torch.distributed.checkpoint.metadatar   r   Z$torch.distributed.checkpoint.storager	   r
   r   r   r   r+   __annotations__r   r   r   r   r   <module>   s    