a
    d	                     @   s   d Z ddlZddlmZ ddlmZmZmZmZ 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 deeef eedd	d
ZeeedddZeeef eeef ddddZdS )z)Utilities related to data saving/loading.    N)Path)AnyDictIOUnion)	url_to_fs)AbstractFileSystem)_MAP_LOCATION_TYPE_PATH)path_or_urlmap_locationreturnc                 C   s   t | ttfstj| |dS t| dr>tjjt| |dS t| }|	| d}tj||dW  d   S 1 sv0    Y  dS )zLoads a checkpoint.

    Args:
        path_or_url: Path or URL of the checkpoint.
        map_location: a function, ``torch.device``, string or a dict specifying how to remap storage locations.
    )r   httprbN)

isinstancestrr   torchload
startswithZhubZload_state_dict_from_urlget_filesystemopen)r   r   fsf r   l/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/lightning_fabric/utilities/cloud_io.py_load   s    
r   )pathkwargsr   c                 K   s   t t| fi |\}}|S )N)r   r   )r   r   r   _r   r   r   r   3   s    r   )
checkpointfilepathr   c                 C   sR   t  }t| | t|d}||  W d   n1 sD0    Y  dS )a  Saves a checkpoint atomically, avoiding the creation of incomplete checkpoints.

    Args:
        checkpoint: The object to save.
            Built to be used with the ``dump_checkpoint`` method, but can deal with anything which ``torch.save``
            accepts.
        filepath: The path to which the checkpoint will be saved.
            This points to the file that the checkpoint will be stored in.
    wbN)ioBytesIOr   savefsspecr   writegetvalue)r   r    Zbytesbufferr   r   r   r   _atomic_save8   s    
r(   )N)__doc__r"   pathlibr   typingr   r   r   r   r%   r   Zfsspec.corer   Zfsspec.implementations.localr   Z lightning_fabric.utilities.typesr	   r
   r   r   r   r(   r   r   r   r   <module>   s     
