
      iE              	       X   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	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j.                  e      Z	 	 dd	e
eef   d
ede	e   defdZdededefdZdeeef   deddfdZdedefdZ ddede
eef   dedefdZ!dedefdZ"y)z)Utilities related to data saving/loading.    N)Path)IOAnyOptionalUnion)	url_to_fs)AbstractFileSystem)module_available)_MAP_LOCATION_TYPE_PATHpath_or_urlmap_locationweights_onlyreturnc                    t        | t        t        f      st        j                  | ||      S t        |       j                  d      rH|d}t        j                  d|  d       t        j                  j                  t        |       ||      S t        |       }|j                  | d      5 }t        j                  |||      cddd       S # 1 sw Y   yxY w)a  Loads 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.
        weights_only: If ``True``, restricts loading to ``state_dicts`` of plain ``torch.Tensor`` and other primitive
            types. If loading a checkpoint from a trusted source that contains an ``nn.Module``, use
            ``weights_only=False``. If loading checkpoint from an untrusted source, we recommend using
            ``weights_only=True``. For more information, please refer to the
            `PyTorch Developer Notes on Serialization Semantics <https://docs.pytorch.org/docs/main/notes/serialization.html#id3>`_.

    )r   r   httpNFz:Defaulting to `weights_only=False` for remote checkpoint: z\. If loading a checkpoint from an untrustted source, we recommend using `weights_only=True`.rb)
isinstancestrr   torchload
startswithlogdebughubload_state_dict_from_urlget_filesystemopen)r   r   r   fsfs        x/Volumes/fast/ai/experiments/voice-extract-mac/.venv/lib/python3.12/site-packages/lightning_fabric/utilities/cloud_io.py_loadr"   "   s    " kC;/zz%%
 	

 ;""6* LIIL[M Zn o
 yy11%% 2 
 	

 
	$B	d	#qzz%%
 
$	#	#s   .CCpathkwargsc                 6    t        t        |       fi |\  }}|S )N)r   r   )r#   r$   r   _s       r!   r   r   P   s    c$i*6*EBI    
checkpointfilepathc                    t        j                         }t        j                  d|        t	        j
                  | |       	 t        j                  j                  t        |            \  }}|j                  5  |j                  |d      5 }|j                  |j                                ddd       ddd       y# 1 sw Y   xY w# 1 sw Y   yxY w# t        $ r]}t        |j                   t"              r9t%        |j                   dd      t&        j(                  k(  rt+        d      |Y d}~yY d}~yd}~ww xY 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BytesIOr   r   r   savefsspeccorer   r   transactionr   writegetvaluePermissionErrorr   __context__OSErrorgetattrr,   EXDEVRuntimeError)r(   r)   bytesbufferr   urlpathr    es          r!   _atomic_saver>   U   s     **,KII#H:./	JJz;'	kk++CM:G^^RWWWd3qGGK((*+ 4^^33^^ ammW-'!--RV2W[`[f[f2fo 3g-sO   7C ;C B?.C6C ?C	CCC C 	D= AD88D=r   c                     t        d      rddlm} t        | |      ryt        d      rddlm} t        | |      ryt        d      rddlm} t        | |      ryy	)
Nadlfsr   )AzureBlobFileSystemTgcsfs)GCSFileSystems3fs)S3FileSystemF)r
   r@   rA   r   rB   rC   rD   rE   )r   rA   rC   rE   s       r!   _is_object_storagerF   p   sN     -b-. 'b-(%b,'r'   strictc                     t        |       r%|r| j                  |      S | j                  |       S | j                  |      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.

    )rF   isdirisfile)r   r#   rG   s      r!   _is_dirrK      s>    & "88D>! 99T?""88D>r'   c                 X    t         j                  j                  t        |             dk(  S )Nfile)r0   utilsget_protocolr   )r#   s    r!   _is_local_file_protocolrP      s     <<$$SY/699r'   )NN)F)#__doc__r,   r-   loggingpathlibr   typingr   r   r   r   r0   fsspec.utilsr   fsspec.corer   fsspec.implementations.localr	    lightning_utilities.core.importsr
    lightning_fabric.utilities.typesr   r   	getLogger__name__r   boolr"   r   dictr   r>   rF   rK   rP    r'   r!   <module>r_      s   0  	   + +    ! ; = Fg!
 (,#'+
r5y!+
$+
 4.+
 		+
\ # 2D 
T#s(^ u  6- $ ," %T	*: D UY <:% :D :r'   