
    PhN                         d dl Z d dlmZ d dlmZmZ d dlmZ ddlm	Z	m
Z
 ddlmZmZmZmZ g dZ ed	
       G d d             Z G d de j$                        Z G d de j$                        Zy)    N)	dataclass)ListAny)Future   )MetadataMetadataIndex)LoadPlanSavePlanSavePlannerLoadPlanner)WriteResultStorageWriterStorageReaderT)frozenc                   ,    e Zd ZU eed<   eed<   eed<   y)r   indexsize_in_bytesstorage_dataN)__name__
__module____qualname__r	   __annotations__intr        oC:\Users\daisl\Desktop\realtime-object-detection\venv\Lib\site-packages\torch/distributed/checkpoint/storage.pyr   r      s    r   r   c            	       "   e Zd ZdZej
                  deddfd       Zej
                  dedefd       Z	ej
                  de
e   de
e   fd	       Zej
                  ded
edee
e      fd       Zej
                  dede
e
e      ddfd       Zy)r   a  
    Interface used by ``save_state_dict`` to write to storage.

    One StorageWriter instance acts as both the coordinator and the follower
    in a distributed checkpoint. As part of initialization, each instance
    is told its role.

    A subclass should expect the following sequence of calls.

    1) (all ranks) set_up_storage_writer()
    2) (all ranks) prepare_local_plan()
    3) (coordinator) prepare_global_plan()
    4) (all ranks) write_data()
    5) (coordinator) finish()
    is_coordinatorreturnNc                      y)z
        Initialize this instance.

        Args:
            is_coordinator (bool): Whether this instance is responsible for coordinating
              the checkpoint.
        Nr   )selfr   s     r   set_up_storage_writerz#StorageWriter.set_up_storage_writer/        	r   planc                      y)a  
        Perform storage-specific local planning.

        While this method can produce a completely different plan, the recommended
        way is to store storage specific data in SavePlan::storage_data.

        Args:
            plan (SavePlan): The local plan from the ``SavePlanner`` in use.

        Returns:
            A transformed ``SavePlan`` after storage local planning
        Nr   r"   r%   s     r   prepare_local_planz StorageWriter.prepare_local_plan:        	r   plansc                      y)a  
        Perform centralized planning of storage.

        This method is only called on the coordinator instance.

        While this method can produce a completely different plan, the preferred
        way is to store storage specific data in SavePlan::storage_data.

        Args:
            plans: A list of ``SavePlan`` instances, one for each rank.

        Returns:
            A list of transformed ``SavePlan`` after storage global planning
        Nr   r"   r*   s     r   prepare_global_planz!StorageWriter.prepare_global_planJ         	r   plannerc                      y)a  
        Write all items from ``plan`` using ``planner`` to resolve the data.

        A subclass should call ``SavePlanner::resolve_data`` on each item
        from the plan to get access to the underlying object to write.

        Subclasses should lazily call `resolve_data` as it can allocate memory.
        In case of tensors, make following assumptions:

        - They might be on any device, including not matching the one on ``WriteItem::tensor_data``
        - They might be views or not contiguous. Only the projection needs to be saved.

        Args:
            plan (SavePlan): The save plan to execute.
            planner (SavePlanner): Planner object to be used to resolve items to data.

        Returns:
            A future that completes to a list of WriteResult
        Nr   r"   r%   r/   s      r   
write_datazStorageWriter.write_data\   s    . 	r   metadataresultsc                      y)a  
        Write the metadata and marks the current checkpoint as successful.

        The actual format/schema used for serializing `metadata` is an
        implementation detail. The only requirement is that it's recoverable
        in to the same object graph.

        Args:
            metadata (Metadata): metadata for the new checkpoint
            results: A list of WriteResults from all ranks.

        Returns:
            None
        Nr   )r"   r3   r4   s      r   finishzStorageWriter.finishu   s    $ 	r   )r   r   r   __doc__abcabstractmethodboolr#   r   r(   r   r-   r   r   r   r2   r   r6   r   r   r   r   r      s      	D T   	x H   	h DN  " 	'2	[!	" 0 	 +/[0A+B	 r   r   c                      e Zd ZdZej
                  defd       Zej
                  dededdfd       Z	ej
                  de
de
fd	       Zej
                  d
ee
   dee
   fd       Zej
                  de
deded   fd       Zy)r   a  
    Interface used by ``load_state_dict`` to read from storage.

    One StorageReader instance acts as both the coordinator and the follower
    in a distributed checkpoint. As part of initialization, each instance
    is told its role.

    A subclass should expected the following sequence of calls by ``load_state_dict``:

    1) (all ranks) read_metadata()
    2) (all ranks) set_up_storage_reader()
    3) (all ranks) prepare_local_plan()
    4) (coordinator) prepare_global_plan()
    5) (all ranks) read_data()
    r    c                      y)z
        Read the checkpoint metadata.

        Returns:
            The metadata object associated with the checkpoint being loaded.

        Nr   )r"   s    r   read_metadatazStorageReader.read_metadata   r$   r   r3   r   Nc                      y)z
        Initialize this instance.

        Args:
            metadata (Metadata): The metadata schema to use.
            is_coordinator (bool): Whether this instance is responsible for coordinating
              the checkpoint.
        Nr   )r"   r3   r   s      r   set_up_storage_readerz#StorageReader.set_up_storage_reader   s     	r   r%   c                      y)a  
        Perform storage-specific local planning.

        While this method can produce a completely different plan, the recommended
        way is to store storage specific data in LoadPlan::storage_data.

        Args:
            plan (LoadPlan): The local plan from the ``LoadPlan`` in use.

        Returns:
            A transformed ``LoadPlan`` after storage local planning
        Nr   r'   s     r   r(   z StorageReader.prepare_local_plan   r)   r   r*   c                      y)a  
        Perform centralized planning of storage loading.

        This method is only called on the coordinator instance.

        While this method can produce a completely different plan, the preferred
        way is to store storage specific data in LoadPlan::storage_data.

        Args:
            plans: A list of ``LoadPlan`` instances, one for each rank.

        Returns:
            A list of transformed ``LoadPlan`` after storage global planning
        Nr   r,   s     r   r-   z!StorageReader.prepare_global_plan   r.   r   r/   c                      y)a  
        Read all items from ``plan`` using ``planner`` to resolve the data.

        A subclass should call ``LoadPlanner::load_bytes`` to deserialize a BytesIO
        object into the right place.

        A subclass should call ``LoadPlanner::resolve_tensor`` to get access to the
        tensors that in should load data into.

        It's the StorageLayer responsibility to properly schedule any cross device copies
        required.

        Args:
            plan (LoadPlan): The local plan to execute on
            planner (LoadPlanner): The planner object to use to resolve items.

        Returns:
            A future that completes once all reads are finished.
        Nr   r1   s      r   	read_datazStorageReader.read_data   s    * 	r   )r   r   r   r7   r8   r9   r   r=   r:   r?   r
   r(   r   r-   r   r   rC   r   r   r   r   r      s      	x   		h 	 	QU 	 	 	x H   	h DN  " 	h    r   r   )r8   dataclassesr   typingr   r   torch.futuresr   r3   r   r	   r/   r
   r   r   r   __all__r   ABCr   r   r   r   r   <module>rI      sj    
 !   
  < $  iCGG iX_CGG _r   