
    ȅia                         S SK r S SKrS SKJrJr  S SKJr  S SKJr  S SK	J
r
  S SKJr  S SKJr  S SKJrJrJrJr  \
(       a  S S	KJr  S
S/r " S S\5      r " S S
\5      r " S S\5      rg)    N)	GeneratorSequence)contextmanager)Path)TYPE_CHECKING)	url_to_fs)StreamTransformExtension)FileSystemBaseFileSystemReaderFileSystemWriterSerializationFormat)AbstractFileSystemFsspecWriterFsspecReaderc            
       >   \ rS rSrSS jr\S\\R                  -  S\S\	\
R                  SS4   4S j5       rS\\R                  -  S\S\\R                  -  4S	 jrS\\R                  -  S\\R                  -  4S
 jrS\\R                  -  S\\R                  -  SS4S jrS\\R                  -  SS4S jr\S\\R                  -  S\4S j5       rS\\R                  -  S\4S jrS\\R                  -  SS4S jrS\\R                  -  S\\   4S jrSrg)
FileSystem    returnNc                     S U l         g Nfs)selfs    i/home/james-whalen/.local/lib/python3.13/site-packages/torch/distributed/checkpoint/_fsspec_filesystem.py__init__FileSystem.__init__!   s	    -1    pathmodec              #   V  ^#    U R                   c  [        S5      e[        R                  " U5      nU R                   R	                  UT5       n Uv    S S S 5        g !   [        U4S jS 5       5      (       a   U R                  U5        e !    e = fe = f! , (       d  f       g = f7f)Nzfs should not be Nonec              3   ,   >#    U  H	  oT;   v   M     g 7fr    ).0chr   s     r   	<genexpr>+FileSystem.create_stream.<locals>.<genexpr>3   s     2EbTzEs   zw+a)r   AssertionErrorosfspathopenanyrm_file)r   r   r   streams     ` r   create_streamFileSystem.create_stream$   s      77? !899yy
 WW\\$% &%2E222T*  &%sH   AB)BA	B)B;BBBBB
B&"B)suffixc                 @    [         R                  R                  X5      $ r   )r(   r   join)r   r   r0   s      r   concat_pathFileSystem.concat_path:   s    ww||D))r   c                 .    [        U40 UD6u  U l        nU$ r   )r   r   )r   r   kwargs_s       r   	init_pathFileSystem.init_path=   s    t.v.
r   new_pathc                 :    U R                   R                  X5        g r   )r   rename)r   r   r:   s      r   r<   FileSystem.renameA   s    t&r   c                 8    U R                   R                  USS9  g )NT)exist_ok)r   makedirsr   r   s     r   mkdirFileSystem.mkdirD   s    -r   checkpoint_idc                 h    [        U[        5      (       a  g [        U5        g! [         a     gf = f)NFT)
isinstancer   r   
ValueErrorclsrD   s     r   validate_checkpoint_id!FileSystem.validate_checkpoint_idG   s8    mT**	m$   		s   $ 
11c                 8    U R                   R                  U5      $ r   )r   existsrA   s     r   rM   FileSystem.existsS   s    ww~~d##r   c                 :    U R                   R                  U5        g r   )r   rmrA   s     r   r,   FileSystem.rm_fileV   s    

4r   c                 6    U R                   R                  USS9$ )NF)detail)r   lsrA   s     r   rT   FileSystem.lsY   s     wwzz$uz--r   r   )r   N)__name__
__module____qualname____firstlineno__r   r   strr(   PathLiker   ioIOBaser.   r3   r8   r<   rB   classmethodboolrJ   rM   r,   listrT   __static_attributes__r"   r   r   r   r       sh   2 "++%-0	299dD(	) **bkk 1 *3 *3CT *cBKK/ cBKK>O '3, 'bkk8I 'd '.#+ . . 	33D 	 	 	$3, $ $C"++- $ .sR[[( .T#Y .r   r   c                      ^  \ rS rSrSrSSSSSS\R                  4S\\R                  -  S\
S	\
S
\S\S\
S\\   S-  S\SS4U 4S jjjr\S\\R                  -  S\
4S j5       rSrU =r$ )r   `   aD  
Basic implementation of StorageWriter using FFspec.

This implementation makes the following assumptions and simplifications:

* The checkpoint path is an empty or non-existing directory.
* File creation is atomic

The checkpoint consist of one file per write request plus
a `.metadata` file with the serialized metadata.

T   i Nr   single_file_per_rank
sync_filesthread_countper_thread_copy_ahead	overwrite_extensionsserialization_formatr   c	                    > [         T
U ]  UUUUUUUUS9  [        5       U l        U R                  R                  " U40 U	D6U l        g)a  
Initialize the writer pointing to `path`.

Args:
    path: directory where the checkpoint will be written to.
    single_file_per_rank: Produce one file per rank instead of one file per tensor/blob. Default to True.
    sync_files : force files to be synced to permanent storage. Default to True.
    thread_count: Number of IO threads to use to write. Default to 1.
    per_thread_copy_ahead: How many bytes to copy from the GPU ahead of saving then. Default 10Mb.
    overwrite: Whether to allow overwriting existing checkpoints. Defaults to True.
    _extensions: Extensions to apply to output streams (EXPERIMENTAL)

N. B. If sync_files is disabled, there's no guarantee that the checkpoint will be consistent in the case of a failure.
)ri   rj   rk   Nsuperr   r   r   r8   r   )r   r   re   rf   rg   rh   ri   rj   rk   r6   	__class__s             r   r   FsspecWriter.__init__n   sU    4 	 !#!5 	 		
 ,GG%%d5f5	r   rD   c                 ,    [         R                  U5      $ r   r   rJ   rH   s     r   rJ   #FsspecWriter.validate_checkpoint_id       00??r   r   r   )rV   rW   rX   rY   __doc__r   
TORCH_SAVErZ   r(   r[   r_   intr   r	   r   r^   rJ   ra   __classcell__ro   s   @r   r   r   `   s      &*%/AE4G4R4R%6BKK%6 #%6 	%6
 %6  #%6 %6 67$>%6 2%6 
%6 %6N @33D @ @ @r   c                      ^  \ rS rSrS\\R                  -  SS4U 4S jjr\S\\R                  -  S\	4S j5       r
SrU =r$ )	r      r   r   Nc                    > [         TU ]  U5        [        5       U l        U R                  R                  " U40 UD6U l        g r   rm   )r   r   r6   ro   s      r   r   FsspecReader.__init__   s4    ,GG%%d5f5	r   rD   c                 ,    [         R                  U5      $ r   rr   rH   s     r   rJ   #FsspecReader.validate_checkpoint_id   rt   r   ru   )rV   rW   rX   rY   rZ   r(   r[   r   r^   r_   rJ   ra   ry   rz   s   @r   r   r      sN    6S2;;. 6T 6
 @33D @ @ @r   )r\   r(   collections.abcr   r   
contextlibr   pathlibr   typingr   fsspec.corer   'torch.distributed.checkpoint._extensionr	   'torch.distributed.checkpoint.filesystemr
   r   r   r   fsspecr   __all__r   r   r   r"   r   r   <module>r      sl    
 	 / %    ! L  ) <. <.@7@# 7@t@# @r   