o
    Zh-                     @   s   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
mZmZ d dlmZ d dlmZ d dlmZmZmZ erDd d	lmZ d
dgZG dd deZG dd
 d
eZG dd deZdS )    N)	GeneratorSequence)contextmanager)Path)OptionalTYPE_CHECKINGUnion)	url_to_fs)StreamTransformExtension)FileSystemBaseFileSystemReaderFileSystemWriter)AbstractFileSystemFsspecWriterFsspecReaderc                
   @   s4  e Zd ZdddZedeeejf dede	e
jddf fddZdeeejf d	edeeejf fd
dZdeeejf deeejf fddZdeeejf deeejf ddfddZdeeejf ddfddZedeeejf defddZdeeejf defddZdeeejf ddfddZdS )
FileSystemreturnNc                 C   s
   d | _ d S N)fs)self r   ^/var/www/auris/lib/python3.10/site-packages/torch/distributed/checkpoint/_fsspec_filesystem.py__init__    s   
zFileSystem.__init__pathmodec              
   #   s    | j d usJ t|}| j | +}z|V  W n   t fdddD r6z| | W     Y   W d    d S 1 sBw   Y  d S )Nc                 3   s    | ]}| v V  qd S r   r   ).0chr   r   r   	<genexpr>1   s    z+FileSystem.create_stream.<locals>.<genexpr>zw+a)r   osfspathopenanyrm_file)r   r   r   streamr   r   r   create_stream#   s    

"zFileSystem.create_streamsuffixc                 C   s   t j||S r   )r   r   join)r   r   r&   r   r   r   concat_path8   s   zFileSystem.concat_pathc                 K   s   t |fi |\| _}|S r   )r	   r   )r   r   kwargs_r   r   r   	init_path=   s   zFileSystem.init_pathnew_pathc                 C   s   | j || d S r   )r   rename)r   r   r,   r   r   r   r-   C   s   zFileSystem.renamec                 C   s   | j j|dd d S )NT)exist_ok)r   makedirsr   r   r   r   r   mkdirH   s   zFileSystem.mkdircheckpoint_idc                 C   s2   t |trdS zt| W dS  ty   Y dS w )NFT)
isinstancer   r	   
ValueErrorclsr2   r   r   r   validate_checkpoint_idK   s   

z!FileSystem.validate_checkpoint_idc                 C   s   | j |S r   )r   existsr0   r   r   r   r8   W   s   zFileSystem.existsc                 C   s   | j | d S r   )r   Zrmr0   r   r   r   r#   Z   s   zFileSystem.rm_file)r   N)__name__
__module____qualname__r   r   r   strr   PathLiker   ioIOBaser%   r(   r+   r-   r1   classmethodboolr7   r8   r#   r   r   r   r   r      sD    



 r   c                       s   e Zd ZdZ						ddeeejf deded	e	d
e	dede
ee  ddf fddZedeeejf defddZ  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   逖 Nr   single_file_per_rank
sync_filesthread_countper_thread_copy_ahead	overwrite_extensionsr   c           	   	      s<   t  j|||||||d t | _| jj|fi || _dS )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.
        )rH   rI   Nsuperr   r   r   r+   r   )	r   r   rD   rE   rF   rG   rH   rI   r)   	__class__r   r   r   m   s   	zFsspecWriter.__init__r2   c                 C   
   t |S r   r   r7   r5   r   r   r   r7         
z#FsspecWriter.validate_checkpoint_id)TTrB   rC   TN)r9   r:   r;   __doc__r   r<   r   r=   rA   intr   r   r
   r   r@   r7   __classcell__r   r   rL   r   r   _   s6    

%&c                       sP   e Zd Zdeeejf ddf fddZedeeejf de	fddZ
  ZS )	r   r   r   Nc                    s.   t  | t | _| jj|fi || _d S r   rJ   )r   r   r)   rL   r   r   r      s   zFsspecReader.__init__r2   c                 C   rN   r   rO   r5   r   r   r   r7      rP   z#FsspecReader.validate_checkpoint_id)r9   r:   r;   r   r<   r   r=   r   r@   rA   r7   rS   r   r   rL   r   r      s     &)r>   r   collections.abcr   r   
contextlibr   pathlibr   typingr   r   r   Zfsspec.corer	   Z'torch.distributed.checkpoint._extensionr
   Z'torch.distributed.checkpoint.filesystemr   r   r   Zfsspecr   __all__r   r   r   r   r   r   r   <module>   s"   @8