
    8h                         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
Jr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)OptionalTYPE_CHECKINGUnion)	url_to_fs)StreamTransformExtension)FileSystemBaseFileSystemReaderFileSystemWriterSerializationFormat)AbstractFileSystemFsspecWriterFsspecReaderc            
       n   \ rS rSrSS jr\S\\\R                  4   S\S\
\R                  SS4   4S j5       rS\\\R                  4   S\S\\\R                  4   4S	 jrS\\\R                  4   S\\\R                  4   4S
 jrS\\\R                  4   S\\\R                  4   SS4S jrS\\\R                  4   SS4S jr\S\\\R                  4   S\4S j5       rS\\\R                  4   S\4S jrS\\\R                  4   SS4S jrS\\\R                  4   S\\   4S jrSrg)
FileSystem    returnNc                     S U l         g Nfs)selfs    i/var/www/fran/franai/venv/lib/python3.13/site-packages/torch/distributed/checkpoint/_fsspec_filesystem.py__init__FileSystem.__init__!   s	    04    pathmodec              #   D  ^#    U R                   c   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)Nc              3   ,   >#    U  H	  oT;   v   M     g 7fr    ).0chr!   s     r   	<genexpr>+FileSystem.create_stream.<locals>.<genexpr>2   s     2EbTzEs   zw+a)r   osfspathopenanyrm_file)r   r    r!   streams     ` r   create_streamFileSystem.create_stream$   s      ww"""yy
 WW\\$% &%2E222T*  &%sH   AB BA	B B2BBBBB
BB suffixc                 @    [         R                  R                  X5      $ r   )r)   r    join)r   r    r1   s      r   concat_pathFileSystem.concat_path9   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.renameD   s     	t&r   c                 8    U R                   R                  USS9  g )NT)exist_ok)r   makedirsr   r    s     r   mkdirFileSystem.mkdirI   s    -r   checkpoint_idc                 h    [        U[        5      (       a  g [        U5        g! [         a     gf = f)NFT)
isinstancer   r
   
ValueErrorclsrE   s     r   validate_checkpoint_id!FileSystem.validate_checkpoint_idL   s8    mT**	m$   		s   $ 
11c                 8    U R                   R                  U5      $ r   )r   existsrB   s     r   rN   FileSystem.existsX   s    ww~~d##r   c                 :    U R                   R                  U5        g r   )r   rmrB   s     r   r-   FileSystem.rm_file[   s    

4r   c                 6    U R                   R                  USS9$ )NF)detail)r   lsrB   s     r   rU   FileSystem.ls^   s     wwzz$uz--r   r   )r   N)__name__
__module____qualname____firstlineno__r   r   r	   strr)   PathLiker   ioIOBaser/   r4   r9   r=   rC   classmethodboolrK   rN   r-   listrU   __static_attributes__r$   r   r   r   r       s   5 #r{{*+36	299dD(	) (*#r{{*+*58*	sBKK	 *
#r{{*+	sBKK	 '#r{{*+'7<S"++=M7N'	'
.%R[[ 01 .d . 	5bkk9I3J 	t 	 	$5bkk!12 $t $E#r{{"23  .uS"++-. .49 .r   r   c                      ^  \ rS rSrSrSSSSSS\R                  4S\\\	R                  4   S\S	\S
\S\S\S\\\      S\SS4U 4S jjjr\S\\\	R                  4   S\4S j5       rSrU =r$ )r   e   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.
)rj   rk   rl   Nsuperr   r   r   r9   r    )r   r    rf   rg   rh   ri   rj   rk   rl   r7   	__class__s             r   r   FsspecWriter.__init__s   sU    4 	 !#!5 	 		
 ,GG%%d5f5	r   rE   c                 ,    [         R                  U5      $ r   r   rK   rI   s     r   rK   #FsspecWriter.validate_checkpoint_id       00??r   r   r    )rW   rX   rY   rZ   __doc__r   
TORCH_SAVEr	   r[   r)   r\   r`   intr   r   r   r   r_   rK   rb   __classcell__rp   s   @r   r   r   e   s      &*%/DH4G4R4R%6C$%%6 #%6 	%6
 %6  #%6 %6 h'?@A%6 2%6 
%6 %6N @5bkk9I3J @t @ @r   c                      ^  \ rS rSrS\\\R                  4   SS4U 4S jjr\	S\\\R                  4   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   rn   )r   r    r7   rp   s      r   r   FsspecReader.__init__   s4    ,GG%%d5f5	r   rE   c                 ,    [         R                  U5      $ r   rs   rI   s     r   rK   #FsspecReader.validate_checkpoint_id   ru   r   rv   )rW   rX   rY   rZ   r	   r[   r)   r\   r   r_   r`   rK   rb   rz   r{   s   @r   r   r      sX    6U3#34 64 6
 @5bkk9I3J @t @ @r   )r]   r)   collections.abcr   r   
contextlibr   pathlibr   typingr   r   r	   fsspec.corer
   'torch.distributed.checkpoint._extensionr   'torch.distributed.checkpoint.filesystemr   r   r   r   fsspecr   __all__r   r   r   r$   r   r   <module>r      sq    
 	 / %  1 1 ! L  ) A. A.J7@# 7@t@# @r   