
    sh-                         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  \(       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)AbstractFileSystemFsspecWriterFsspecReaderc            
       8   \ 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g)
FileSystem   returnNc                     S U l         g Nfs)selfs    ڃ/Users/tiagomarins/Projetos/claudeai/copy_bank/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>1   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   r0   s      r   concat_pathFileSystem.concat_path8   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.renameC   s     	t&r   c                 8    U R                   R                  USS9  g )NT)exist_ok)r   makedirsr   r   s     r   mkdirFileSystem.mkdirH   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_idK   s8    mT**	m$   		s   $ 
11c                 8    U R                   R                  U5      $ r   )r   existsrA   s     r   rM   FileSystem.existsW   s    ww~~d##r   c                 :    U R                   R                  U5        g r   )r   rmrA   s     r   r,   FileSystem.rm_fileZ   s    

4r   r   )r   N)__name__
__module____qualname____firstlineno__r   r   r	   strr(   PathLiker   ioIOBaser.   r3   r8   r<   rB   classmethodboolrJ   rM   r,   __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  r   r   c                      ^  \ rS rSrSr      SS\\\R                  4   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   _   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.

Nr   single_file_per_rank
sync_filesthread_countper_thread_copy_ahead	overwrite_extensionsr   c           
         > [         T	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.
)rc   rd   Nsuperr   r   r   r8   r   )
r   r   r_   r`   ra   rb   rc   rd   r6   	__class__s
            r   r   FsspecWriter.__init__m   sR    2 	 !# 	 	
 ,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   )TT   i TN)rR   rS   rT   rU   __doc__r	   rV   r(   rW   r[   intr   r   r   r   rZ   rJ   r\   __classcell__rh   s   @r   r   r   _   s      &*%/DH#6C$%#6 ##6 	#6
 #6  ##6 #6 h'?@A#6 
#6 #6J @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   rf   )r   r   r6   rh   s      r   r   FsspecReader.__init__   s4    ,GG%%d5f5	r   rD   c                 ,    [         R                  U5      $ r   rk   rH   s     r   rJ   #FsspecReader.validate_checkpoint_id   rm   r   rn   )rR   rS   rT   rU   r	   rV   r(   rW   r   rZ   r[   rJ   r\   rr   rs   s   @r   r   r      sX    6U3#34 64 6
 @5bkk9I3J @t @ @r   )rX   r(   collections.abcr   r   
contextlibr   pathlibr   typingr   r   r	   fsspec.corer
   'torch.distributed.checkpoint._extensionr   'torch.distributed.checkpoint.filesystemr   r   r   fsspecr   __all__r   r   r   r#   r   r   <module>r      so    
 	 / %  1 1 ! L  ) < <@5@# 5@p@# @r   