Ë
    M²j@&  ã                   óì   — d dl Z d dlZd dlmZ d dlmZ d dlmZmZm	Z	 d dl
mZmZmZmZ d dl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)ÚAny)ÚMetadataÚMetadataIndexÚStorageMeta)ÚLoadPlanÚLoadPlannerÚSavePlanÚSavePlanner)ÚFuture)Ú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   © ó    úc/var/www/html/ia-enhanced/venv/lib/python3.12/site-packages/torch/distributed/checkpoint/storage.pyr   r      s   … àÓàÓØÔr   r   c            	       óÞ  — e Zd ZdZej
                  ddeej                  z  dz  ddfd„«       Z	ej
                  de
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ee      fd„«       Zej
                  dedeee      ddfd„«       Zeej
                  deej                  z  de
fd„«       «       Zdedz  fd„Zy)r   a8  
    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.

    0) (all ranks) set checkpoint_id if users pass a valid checkpoint_id.
    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()
    NÚcheckpoint_idÚreturnc                  ó   — y)a©  
        Calls to indicates a brand new checkpoint write is going to happen.
        A checkpoint_id may be present if users set the checkpoint_id for
        this checkpoint write. The meaning of the checkpiont_id is
        storage-dependent. It can be a path to a folder/file or a key for
        a key-value storage.

        Args:
            checkpoint_id (Union[str, os.PathLike, None]):
                The ID of this checkpoint instance. The meaning of the checkpoint_id
                depends on the storage. It can be a path to a folder or to a file.
                It can also be a key if the storage is a key-value store.
                (Default: ``None``)
        Nr   ©Úselfr   s     r   ÚresetzStorageWriter.reset-   ó   € ð  	r   Úis_coordinatorÚargsÚkwargsc                  ó   — y)z±
        Initialize this instance.

        Args:
            is_coordinator (bool): Whether this instance is responsible for coordinating
              the checkpoint.
        Nr   )r"   r%   r&   r'   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_planK   r*   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_planZ   r*   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+   r3   s      r   Ú
write_datazStorageWriter.write_datak   r*   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"   r7   r8   s      r   ÚfinishzStorageWriter.finishƒ   r*   r   c                  ó   — y©zŒ
        Check if the given checkpoint_id is supported by the storage. This allow
        us to enable automatic storage selection.
        Nr   ©Úclsr   s     r   Úvalidate_checkpoint_idz$StorageWriter.validate_checkpoint_id”   ó   € ð 	r   c                  ó   — y)a=  
        Return the storage-specific metadata. This is used to store additional information
        in a checkpoint that can be useful for providing request-level observability. StorageMeta
        is passed to the ``SavePlanner`` during save calls. Returns None by default.

        TODO: provide an example
        Nr   )r"   s    r   Ústorage_metazStorageWriter.storage_meta�   s   € ð r   ©N)r   r   r   Ú__doc__ÚabcÚabstractmethodÚstrÚosÚPathLiker#   Úboolr   r)   r
   r.   Úlistr2   r   r   r   r6   r   r:   Úclassmethodr?   r   rB   r   r   r   r   r      sŽ  „ ñð" 	×Ññ 3¨¯©Ñ#4°tÑ#;ð Àtò ó ðð" 	×Ñð	Ø"ð	Ø+.ð	Ø:=ð	à	ò	ó ð	ð 	×Ñð xð °Hò ó ðð 	×Ñð¨¨h©ð ¸DÀ¹Nò ó ðð  	×ÑðØðØ'2ðà	��[Ñ!Ñ	"òó ðð. 	×Ñð˜xð °$°t¸KÑ7HÑ2Ið Èdò ó ðð  Ø×Ñð°3¸¿¹Ñ3Dð Èò ó ó ðð˜k¨DÑ0ô r   r   c                   ó¾  — e Zd ZdZej
                  ddeej                  z  dz  ddfd„«       Z	ej
                  de
de
defd„«       Zej
                  d	ed
e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eej
                  deej                  z  defd„«       «       Zy)r   aV  
    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``:

    0) (all ranks) set checkpoint_id if users pass a valid checkpoint_id.
    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()
    Nr   r   c                  ó   — y)a±  
        Calls to indicates a brand new checkpoint read is going to happen.
        A checkpoint_id may be present if users set the checkpoint_id for
        this checkpoint read. The meaning of the checkpiont_id is
        storage-dependent. It can be a path to a folder/file or a key for
        a key-value storage.

        Args:
            checkpoint_id (Union[str, os.PathLike, None]):
                The ID of this checkpoint instance. The meaning of the checkpoint_id
                depends on the storage. It can be a path to a folder or to a file.
                It can also be a key if the storage is more like a key-value store.
                (Default: ``None``)
        Nr   r!   s     r   r#   zStorageReader.resetº   r$   r   r&   r'   c                  ó   — y)z�
        Read the checkpoint metadata.

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

        Nr   )r"   r&   r'   s      r   Úread_metadatazStorageReader.read_metadataÌ   r*   r   r7   r%   c                  ó   — 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"   r7   r%   r&   r'   s        r   Úset_up_storage_readerz#StorageReader.set_up_storage_readerÖ   r*   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   r1   s     r   r2   z!StorageReader.prepare_global_planò   r*   r   r3   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   r5   s      r   Ú	read_datazStorageReader.read_data  r*   r   c                  ó   — yr<   r   r=   s     r   r?   z$StorageReader.validate_checkpoint_id  r@   r   rC   )r   r   r   rD   rE   rF   rG   rH   rI   r#   r   r   rP   rJ   rR   r   r.   rK   r2   r	   r   rV   rL   r?   r   r   r   r   r   ¨   sr  „ ñð" 	×Ññ 3¨¯©Ñ#4°tÑ#;ð Àtò ó ðð" 	×Ñð 3ð °#ð ¸(ò ó ðð 	×Ñð
Ø ð
Ø26ð
Ø?Bð
ØNQð
à	ò
ó ð
ð 	×Ñð xð °Hò ó ðð 	×Ñð¨¨h©ð ¸DÀ¹Nò ó ðð  	×Ñð˜hð °ð ÀÈÁò ó ðð* Ø×Ñð°3¸¿¹Ñ3Dð Èò ó ó ñr   r   )rE   rH   Údataclassesr   Útypingr   Ú%torch.distributed.checkpoint.metadatar   r   r   Ú$torch.distributed.checkpoint.plannerr   r	   r
   r   Útorch.futuresr   Ú__all__r   ÚABCr   r   r   r   r   ú<module>r_      sp   ðÛ 
Û 	Ý !Ý ç VÑ V÷ó õ !ò <€ñ �$Ô÷ð ó ðôJ�C—G‘Gô JôZx�C—G‘Gõ xr   