o
    oi                  	   @   s>  d Z ddl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ZddlmZ ddlmZ ddlmZ ddlmZmZ eeZ			d d
e	eef dededefddZdededefddZdeeef de	eef ddfddZdedefddZd!dede	eef dedefddZdedefddZ dS )"z)Utilities related to data saving/loading.    N)Path)IOAnyDictUnion)	url_to_fs)AbstractFileSystem)module_available)_MAP_LOCATION_TYPE_PATHFpath_or_urlmap_locationweights_only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 s>w   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   r   httprbN)

isinstancestrr   torchload
startswithhubload_state_dict_from_urlget_filesystemopen)r   r   r   fsf r   W/home/ubuntu/.local/lib/python3.10/site-packages/lightning/fabric/utilities/cloud_io.py_load!   s(   $r   pathkwargsc                 K   s   t t| fi |\}}|S )N)r   r   )r    r!   r   _r   r   r   r   C   s   r   
checkpointfilepathc              	   C   s   t  }td|  t| | tjt	|\}}|j
. ||d}||  W d   n1 s8w   Y  W d   dS W d   dS 1 sPw   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.

    zSaving checkpoint: wbN)ioBytesIOlogdebugr   savefsspeccorer   r   transactionr   writegetvalue)r#   r$   bytesbufferr   urlpathr   r   r   r   _atomic_saveH   s   Pr2   r   c                 C   sj   t drddlm} t| |rdS t dr"ddlm} t| |r"dS t dr3ddlm} t| |r3dS d	S )
Nadlfsr   )AzureBlobFileSystemTgcsfs)GCSFileSystems3fs)S3FileSystemF)r	   r3   r4   r   r5   r6   r7   r8   )r   r4   r6   r8   r   r   r   _is_object_storage]   s   


r9   strictc                 C   s,   t | r|r| |S | | S | |S )a  Check if a path is directory-like.

    This function determines if a given path is considered directory-like, taking into account the behavior
    specific to object storage platforms. For other filesystems, it behaves similarly to the standard `fs.isdir`
    method.

    Args:
        fs: The filesystem to check the path against.
        path: The path or URL to be checked.
        strict: A flag specific to Object Storage platforms. If set to ``False``, any non-existing path is considered
            as a valid directory-like path. In such cases, the directory (and any non-existing parent directories)
            will be created on the fly. Defaults to False.

    )r9   isdirisfile)r   r    r:   r   r   r   _is_dirs   s
   

r=   c                 C   s   t jt| dkS )Nfile)r+   utilsget_protocolr   )r    r   r   r   _is_local_file_protocol   s   rA   )NF)F)!__doc__r&   loggingpathlibr   typingr   r   r   r   r+   fsspec.utilsr   fsspec.corer   fsspec.implementations.localr    lightning_utilities.core.importsr	    lightning.fabric.utilities.typesr
   r   	getLogger__name__r(   boolr   r   r   r2   r9   r=   rA   r   r   r   r   <module>   s:   


"&$