
    ȅi@&                         S SK r S SKrS SKJr  S SKJr  S SKJrJrJ	r	  S SK
JrJrJrJr  S SKJr  / SQr\" SS	9 " S
 S5      5       r " S S\ R&                  5      r " S S\ R&                  5      rg)    N)	dataclass)Any)MetadataMetadataIndexStorageMeta)LoadPlanLoadPlannerSavePlanSavePlanner)Future)WriteResultStorageWriterStorageReaderT)frozenc                   4    \ rS rSr% \\S'   \\S'   \\S'   Srg)r      indexsize_in_bytesstorage_data N)	__name__
__module____qualname____firstlineno__r   __annotations__intr   __static_attributes__r       ^/home/james-whalen/.local/lib/python3.13/site-packages/torch/distributed/checkpoint/storage.pyr   r      s    r   r   c            	          \ rS rSrSr\R                  SS\\R                  -  S-  SS4S jj5       r
\R                  S\S\S	\SS4S
 j5       r\R                  S\S\4S j5       r\R                  S\\   S\\   4S j5       r\R                  S\S\S\\\      4S j5       r\R                  S\S\\\      SS4S j5       r\\R                  S\\R                  -  S\4S j5       5       rS\S-  4S jrSrg)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.

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                     g)aI  
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   resetStorageWriter.reset-         	r   is_coordinatorargskwargsc                     g)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_writer#StorageWriter.set_up_storage_writer?       r   planc                     g)aI  
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&   r1   s     r   prepare_local_plan StorageWriter.prepare_local_planK   r0   r   plansc                     g)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&   r6   s     r   prepare_global_plan!StorageWriter.prepare_global_planZ   r0   r   plannerc                     g)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&   r1   r;   s      r   
write_dataStorageWriter.write_datak   r0   r   metadataresultsc                     g)am  
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&   r@   rA   s      r   finishStorageWriter.finish   r0   r   c                     gzt
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_id$StorageWriter.validate_checkpoint_id        	r   c                     g)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_metaStorageWriter.storage_meta   s     r   r   N)r   r   r   r   __doc__abcabstractmethodstrosPathLiker'   boolr   r.   r
   r4   listr9   r   r   r   r>   r   rC   classmethodrI   r   rM   r   r   r   r   r   r      s   " 	3#4t#; t  " 		"	+.	:=			 	 	x H   	h DN    	'2	[!	" . 	x $tK7H2I d    33D    kD0 r   r   c                      \ rS rSrSr\R                  SS\\R                  -  S-  SS4S jj5       r
\R                  S\S\S\4S	 j5       r\R                  S
\S\S\S\SS4
S j5       r\R                  S\S\4S j5       r\R                  S\\   S\\   4S j5       r\R                  S\S\S\S   4S j5       r\\R                  S\\R                  -  S\4S j5       5       rSrg)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``:

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                     g)aQ  
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'   StorageReader.reset   r)   r   r+   r,   c                     g)zo
Read the checkpoint metadata.

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

Nr   )r&   r+   r,   s      r   read_metadataStorageReader.read_metadata   r0   r   r@   r*   c                     g)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&   r@   r*   r+   r,   s        r   set_up_storage_reader#StorageReader.set_up_storage_reader   r0   r   r1   c                     g)aF  
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   r3   s     r   r4    StorageReader.prepare_local_plan   r0   r   r6   c                     g)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   r8   s     r   r9   !StorageReader.prepare_global_plan   r0   r   r;   c                     g)aC  
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   r=   s      r   	read_dataStorageReader.read_data  r0   r   c                     grF   r   rG   s     r   rI   $StorageReader.validate_checkpoint_id  rK   r   r   rO   )r   r   r   r   rP   rQ   rR   rS   rT   rU   r'   r   r   r^   rV   ra   r   r4   rW   r9   r	   r   rh   rX   rI   r   r   r   r   r   r      sr   " 	3#4t#; t  " 	3 # (   	
 
26
?B
NQ
	
 
 	x H   	h DN    	h    * 33D    r   r   )rQ   rT   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>rs      sp    
 	 !  V V  ! < $  JCGG JZxCGG xr   