o
    8wi                  	   @   sB  d Z ddlZddlZddlZddlmZ ddl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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   `/home/ubuntu/sommelier/.venv/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   D   s   r   
checkpointfilepathc              
   C   s  t  }td|  t| | zHtjt	|\}}|j
0 ||d}||  W d   n1 s9w   Y  W d   W dS W d   W dS 1 sSw   Y  W dS  ty } z#t|jtrwt|jddtjkr~td|W Y d}~dS W Y d}~dS d}~ww )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errnoz]Upgrade fsspec to enable cross-device local checkpoints: pip install "fsspec[http]>=2025.5.0")ioBytesIOlogdebugr   savefsspeccorer   r   transactionr   writegetvaluePermissionErrorr   __context__OSErrorgetattrr%   EXDEVRuntimeError)r"   r#   bytesbufferr   urlpathr   er   r   r   _atomic_saveI   s$   V r9   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   r:   r;   r   r<   r=   r>   r?   )r   r;   r=   r?   r   r   r   _is_object_storaged   s   


r@   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.

    )r@   isdirisfile)r   r   rA   r   r   r   _is_dirz   s
   

rD   c                 C   s   t jt| dkS )Nfile)r+   utilsget_protocolr   )r   r   r   r   _is_local_file_protocol   s   rH   )NF)F)"__doc__r%   r&   loggingpathlibr   typingr   r   r   r+   fsspec.utilsr   fsspec.corer   fsspec.implementations.localr    lightning_utilities.core.importsr    lightning_fabric.utilities.typesr	   r
   	getLogger__name__r(   boolr   r   dictr   r9   r@   rD   rH   r   r   r   r   <module>   s<   


"&$