
    9ja                         d dl Z d dlZd dlmZmZ d dlmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZ d dlmZmZmZmZ e
rd d	lmZ d
dgZ G d de      Z G d d
e      Z G d de      Zy)    N)	GeneratorSequence)contextmanager)Path)TYPE_CHECKING)	url_to_fs)StreamTransformExtension)FileSystemBaseFileSystemReaderFileSystemWriterSerializationFormat)AbstractFileSystemFsspecWriterFsspecReaderc            
       "   e Zd ZddZedeej                  z  dedee	j                  ddf   fd       Zdeej                  z  dedeej                  z  fdZdeej                  z  deej                  z  fd	Zdeej                  z  d
eej                  z  ddfdZdeej                  z  ddfdZedeej                  z  defd       Zdeej                  z  defdZdeej                  z  ddfdZdeej                  z  dee   fdZy)
FileSystemreturnNc                     d | _         y N)fs)selfs    o/media/conek/DATA/Code/OCR/venv/lib/python3.12/site-packages/torch/distributed/checkpoint/_fsspec_filesystem.py__init__zFileSystem.__init__!   s	    -1    pathmodec              #   <  K   | j                   t        d      t        j                  |      }| j                   j	                  |      5 }	 | 	 d d d        y #  t        fddD              r	 | j                  |        #  Y  xY w xY w# 1 sw Y   y xY ww)Nzfs should not be Nonec              3   &   K   | ]  }|v  
 y wr    ).0chr   s     r   	<genexpr>z+FileSystem.create_stream.<locals>.<genexpr>3   s     2brTz2s   zw+a)r   AssertionErrorosfspathopenanyrm_file)r   r   r   streams     ` r   create_streamzFileSystem.create_stream$   s      77? !899yy
 WW\\$% 				 		2E22T* 		 		sH   A	BBA	BB3BBB	BBBBsuffixc                 B    t         j                  j                  ||      S r   )r$   r   join)r   r   r+   s      r   concat_pathzFileSystem.concat_path:   s    ww||D&))r   c                 .    t        |fi |\  | _        }|S r   )r   r   )r   r   kwargs_s       r   	init_pathzFileSystem.init_path=   s    t.v.
r   new_pathc                 <    | j                   j                  ||       y r   )r   rename)r   r   r3   s      r   r5   zFileSystem.renameA   s    tX&r   c                 >    | j                   j                  |d       y )NT)exist_ok)r   makedirsr   r   s     r   mkdirzFileSystem.mkdirD   s    -r   checkpoint_idc                 \    t        |t              ry	 t        |       y# t        $ r Y yw xY w)NFT)
isinstancer   r   
ValueErrorclsr;   s     r   validate_checkpoint_idz!FileSystem.validate_checkpoint_idG   s5    mT*	m$   		s    	++c                 8    | j                   j                  |      S r   )r   existsr9   s     r   rC   zFileSystem.existsS   s    ww~~d##r   c                 :    | j                   j                  |       y r   )r   rmr9   s     r   r(   zFileSystem.rm_fileV   s    

4r   c                 <    | j                   j                  |d      S )NF)detail)r   lsr9   s     r   rH   zFileSystem.lsY   s     wwzz$uz--r   )r   N)__name__
__module____qualname__r   r   strr$   PathLiker   ioIOBaser*   r.   r2   r5   r:   classmethodboolrA   rC   r(   listrH   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                        e Zd ZdZddddddej
                  fdeej                  z  de	de	d	e
d
e
de	dee   dz  deddf fdZedeej                  z  de	fd       Z xZS )r   a`  
    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         
|   ||||||||       t               | _         | j                  j                  |fi |	| _        y)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.
        )rY   rZ   r[   Nsuperr   r   r   r2   r   )r   r   rU   rV   rW   rX   rY   rZ   r[   r0   	__class__s             r   r   zFsspecWriter.__init__n   sW    4 	 !#!5 	 		
 ,%DGG%%d5f5	r   r;   c                 ,    t         j                  |      S r   r   rA   r?   s     r   rA   z#FsspecWriter.validate_checkpoint_id       00??r   )rI   rJ   rK   __doc__r   
TORCH_SAVErL   r$   rM   rQ   intr   r	   r   rP   rA   __classcell__r_   s   @r   r   r   `   s      &*%/AE4G4R4R%6BKK%6 #%6 	%6
 %6  #%6 %6 67$>%6 2%6 
%6N @33D @ @ @r   c                   v     e Zd Zdeej
                  z  ddf fdZedeej
                  z  defd       Z	 xZ
S )r   r   r   Nc                     t         |   |       t               | _         | j                  j                  |fi || _        y r   r]   )r   r   r0   r_   s      r   r   zFsspecReader.__init__   s6    ,%DGG%%d5f5	r   r;   c                 ,    t         j                  |      S r   ra   r?   s     r   rA   z#FsspecReader.validate_checkpoint_id   rb   r   )rI   rJ   rK   rL   r$   rM   r   rP   rQ   rA   rf   rg   s   @r   r   r      sN    6S2;;. 6T 6
 @33D @ @ @r   )rN   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>rt      sl    
 	 / %    ! L  ) <. <.@7@# 7@t@# @r   