U
    yh                     @   s   d dl Z d dlZd dlmZ d dlmZ d dlmZmZm	Z	 d dl
Z
d dl
mZ d dlmZ d dlmZmZ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)contextmanager)Path)	GeneratorOptionalUnion)AbstractFileSystem)	url_to_fs)FileSystemBaseFileSystemReaderFileSystemWriterFsspecWriterFsspecReaderc                   @   s  e Zd ZddddZeeeejf ee	e
jddf dddZeeejf eeeejf dd	d
Zeeejf eeejf dddZeeejf eeejf ddddZeejgddddZeeeejf edddZeeejf edddZeeejf ddddZdS )
FileSystemN)returnc                 C   s
   d | _ d S N)fs)self r   a/var/www/html/venv/lib/python3.8/site-packages/torch/distributed/checkpoint/_fsspec_filesystem.py__init__   s    zFileSystem.__init__)pathmoder   c              
   c   sH   | j d k	st| j j( tt||}|V  W 5 Q R X W 5 Q R X d S r   )r   AssertionErrorZtransactionfsspecopenstr)r   r   r   streamr   r   r   create_stream   s    
zFileSystem.create_stream)r   suffixr   c                 C   s   t j||S r   )osr   join)r   r   r   r   r   r   concat_path'   s    zFileSystem.concat_pathr   r   c                 C   s   t |\| _}|S r   )r   r   )r   r   _r   r   r   	init_path,   s    zFileSystem.init_path)r   new_pathr   c                 C   s   | j || d S r   )r   rename)r   r   r%   r   r   r   r&   0   s    zFileSystem.renamec                 C   s   | j j|dd d S )NT)exist_ok)r   makedirsr   r   r   r   r   mkdir5   s    zFileSystem.mkdircheckpoint_idr   c                 C   s6   t |trdS zt| W n tk
r0   Y dS X dS )NFT)
isinstancer   r   
ValueErrorclsr,   r   r   r   validate_checkpoint_id8   s    
z!FileSystem.validate_checkpoint_idc                 C   s   | j |S r   )r   existsr)   r   r   r   r2   D   s    zFileSystem.existsc                 C   s   | j | d S r   )r   Zrmr)   r   r   r   rm_fileG   s    zFileSystem.rm_file)__name__
__module____qualname__r   r   r   r   r   PathLiker   ioIOBaser   r!   r$   r&   r*   classmethodboolr1   r2   r3   r   r   r   r   r      s(    	 $ r   c                	       s\   e Zd ZdZdeeejf eee	e	edd fddZ
eeeejf ed	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   逖 N)r   single_file_per_rank
sync_filesthread_countper_thread_copy_ahead	overwriter   c                    s2   t  j||||||d t | _| j|| _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.

        N. B. If sync_files is disabled, there's no guarantee that the checkpoint will be consistent in the case of a failure.
        )rB   Nsuperr   r   r   r$   r   )r   r   r>   r?   r@   rA   rB   	__class__r   r   r   Z   s    zFsspecWriter.__init__r+   c                 C   s
   t |S r   r   r1   r/   r   r   r   r1   {   s    z#FsspecWriter.validate_checkpoint_id)TTr<   r=   T)r4   r5   r6   __doc__r   r   r   r7   r;   intr   r:   r1   __classcell__r   r   rE   r   r   L   s"        !c                       sL   e Zd Zeeejf dd fddZeeeejf e	dddZ
  ZS )r   Nr"   c                    s&   t  | t | _| j|| _d S r   rC   r)   rE   r   r   r      s    zFsspecReader.__init__r+   c                 C   s
   t |S r   rG   r/   r   r   r   r1      s    z#FsspecReader.validate_checkpoint_id)r4   r5   r6   r   r   r   r7   r   r:   r;   r1   rJ   r   r   rE   r   r      s   )r8   r   
contextlibr   pathlibr   typingr   r   r   r   r   Zfsspec.corer   Z'torch.distributed.checkpoint.filesystemr	   r
   r   __all__r   r   r   r   r   r   r   <module>   s   24