o
    oi%                  	   @   s  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mZmZmZmZmZ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 d d	lmZ d d
lmZ d dlmZ d dl m!Z!m"Z" dZ#erpd dl$m%Z% G dd dZ&G dd dej'Z(de!defddZ)dedefddZ*	d"dee+ef dee+eee"f f deee+  ddfddZ,dedee+ef fd d!Z-dS )#    N)partial)BytesIO)Path)
IOTYPE_CHECKINGAnyCallableDictOptionalOrderedDictSequenceSetUnion)apply_to_collection)Tensor)_TensorMeta)	Parameter)override)_TORCH_GREATER_EQUAL_2_3)_PATH	_Statefulzmeta.ptTypedStoragec                   @   s&  e Zd Zdedddededdf
dd	Zedd
dededede	de
d defddZedd
dededede
d deed f f
ddZe	d*dddededededede
e dddd fddZdefddZe	 	d+ded!edee d"e
e def
d#d$Zd%edefd&d'Zdefd(d)ZdS ),_NotYetLoadedTensor
metatensorarchiveinfo_LazyLoadingUnpicklerstorageinforebuild_argsreturnNc                 C   s   || _ || _|| _|| _d S N)r   r   r   r   )selfr   r   r   r    r"   S/home/ubuntu/.local/lib/python3.10/site-packages/lightning/fabric/utilities/load.py__init__(   s   
z_NotYetLoadedTensor.__init__r   funcnew_typeargsstatec                   sJ   || }t |tr|jdtf fdd}||_|S tj| |S )Nr   c                      s      t j fdddS )Nc                      s    S r    r"   r"   tr"   r#   <lambda>D   s    zP_NotYetLoadedTensor.rebuild_from_type_v2.<locals>._load_tensor.<locals>.<lambda>r"   )torch_tensor_rebuild_from_type_v2r"   r'   old_ltr)   r*   r#   _load_tensorB   s   z>_NotYetLoadedTensor.rebuild_from_type_v2.<locals>._load_tensor)
isinstancer   r2   r   r-   r.   r/   )clsr&   r'   r(   r)   r   retr2   r"   r0   r#   rebuild_from_type_v24   s   

z(_NotYetLoadedTensor.rebuild_from_type_v2datarequires_gradbackward_hooksc                   s@   t |tr|jdtf fdd}||_|S tj| S )Nr   c                     s    } t j|  S r    )r-   _utils_rebuild_parameterr*   r9   r1   r8   r"   r#   r2   V   s   z;_NotYetLoadedTensor.rebuild_parameter.<locals>._load_tensor)r3   r   r2   r   r-   r:   r;   )r4   r7   r8   r9   r   r2   r"   r<   r#   rebuild_parameterJ   s   
	z%_NotYetLoadedTensor.rebuild_parameterstorager   storage_offsetsizestridemetadatac             	   C   s<   ||||||f}	t j|||||||}
|j}t|
|||	S r    )r-   r:   _rebuild_tensor_v2r   r   )r4   r>   r?   r@   rA   r8   r9   rB   r   r   r   r   r"   r"   r#   rebuild_tensor_v2^   s   z%_NotYetLoadedTensor.rebuild_tensor_v2c           	      C   s   ddl m}m} | j\}}}}}| jj}| jjd| |t	j
| |}| j}t  td |||dd}W d    n1 sFw   Y  t	j
j|g| jR  S )Nr   )r   UntypedStoragezdata/ignoreT)wrap_storagedtype	_internal)torch.storager   rE   r   r   rH   r   file_readerget_storage_from_recordr-   r:   _element_size_typed_storage_untyped_storagewarningscatch_warningssimplefilterrC   r   )	r!   r   rE   _fnr@   rH   r>   utsr"   r"   r#   r2   r   s   


z _NotYetLoadedTensor._load_tensorr"   typeskwargsc                 C   s$   |pi }dd |D }||i |S )Nc                 S   s"   g | ]}t |tr| n|qS r"   )r3   r   r2   ).0argr"   r"   r#   
<listcomp>   s   " z:_NotYetLoadedTensor.__torch_function__.<locals>.<listcomp>r"   )r4   r&   rV   r(   rW   loaded_argsr"   r"   r#   __torch_function__   s   z&_NotYetLoadedTensor.__torch_function__namec                 C   sF   |dv r
t | j|S |dv rt |  |S tdt| j d| d)N>   gradndimr@   rH   namesshapelayoutgrad_fnis_metavolatile	output_nrretains_gradr8   >   cudar7   half
contiguous'z' object has no attribute ')getattrr   r2   AttributeErrortype__name__)r!   r]   r"   r"   r#   __getattr__   s
   z_NotYetLoadedTensor.__getattr__c                 C   s   | j j dt| j dS )N())	__class__ro   reprr   )r!   r"   r"   r#   __repr__   s   z_NotYetLoadedTensor.__repr__r    )r"   N)ro   
__module____qualname__r   tupler$   classmethodr   r   dictr
   r   r6   boolr   r   r=   intrD   r2   r   r	   r\   strrp   ru   r"   r"   r"   r#   r   '   s    

	
r   c                       s`   e Zd Zdedejddf fddZededede	f fd	d
Z
ededdfddZ  ZS )r   filerK   r   Nc                    s   t  | || _d S r    )superr$   rK   )r!   r~   rK   rs   r"   r#   r$      s   
z_LazyLoadingUnpickler.__init__moduler]   c                    sh   |dkr|dkrt tj| dS |dkr|dkrt tj| dS |dkr-|dkr-t tj| dS t ||S )Nztorch._utilsrC   r%   ztorch._tensorr/   r;   )r   r   rD   r6   r=   r   
find_class)r!   r   r]   r   r"   r#   r      s   z _LazyLoadingUnpickler.find_classpidr   c                 C   sf   ddl m} |\}}}}}t  td || jdd}W d    n1 s)w   Y  ||_|S )Nr   r   rF   meta)rH   device)rJ   r   rP   rQ   rR   rH   r   )r!   r   r   rS   r4   r>   r"   r"   r#   persistent_load   s   

z%_LazyLoadingUnpickler.persistent_load)ro   rv   rw   r   r-   PyTorchFileReaderr$   r   r}   r   r   rx   r   __classcell__r"   r"   r   r#   r      s    	r   filenamer   c                 C   sr   t j| stdt| dtt| }t|d}t	||}|
 W  d    S 1 s2w   Y  d S )NzPath z! does not exist or is not a file.zdata.pkl)ospathisfileFileNotFoundErrorr}   r-   r   r   
get_recordr   load)r   rK   pklmupr"   r"   r#   
_lazy_load   s   
$r   
collectionc                 C   s    dt dtfdd}t| t |dS )Nr+   r   c                 S   s   |   S r    )r2   r*   r"   r"   r#   r2      s   z*_materialize_tensors.<locals>._load_tensor)rH   function)r   r   r   )r   r2   r"   r"   r#   _materialize_tensors   s   r   sourcedestinationkeysc                 C   sb   |du rt | n|t | @ }|D ]}| |}||v r*t|| tr*|| | q|||< qdS )zTakes the state from the source destination and moves it into the destination dictionary.

    If an object in the destination follows the stateful protocol, it loads the source state via ``load_state_dict``.

    N)setpopr3   r   load_state_dict)r   r   r   keyr)   r"   r"   r#   _move_state_into   s   

r   checkpoint_folderc                 C   sz   t stdddlm} ddlm} ddlm} i }|||| | dd | t }|	 r4t
j|dd	ni }|| |S )
zLoads a sharded checkpoint saved with the `torch.distributed.checkpoint` into a full state dict.

    The current implementation assumes that the entire checkpoint fits in CPU memory.

    z;Processing distributed checkpoints requires PyTorch >= 2.3.r   )FileSystemReader)_EmptyStateDictLoadPlanner)_load_state_dictT)storage_readerplannerno_distcpu)map_location)r   ImportErrortorch.distributed.checkpointr   )torch.distributed.checkpoint.format_utilsr   .torch.distributed.checkpoint.state_dict_loaderr   _METADATA_FILENAMEis_filer-   r   update)r   r   r   r   
checkpoint
extra_fileextrar"   r"   r#   _load_distributed_checkpoint   s    
r   r    ).r   picklerP   	functoolsr   ior   pathlibr   typingr   r   r   r   r	   r
   r   r   r   r   r-   #lightning_utilities.core.apply_funcr   r   torch._Cr   torch.nnr   typing_extensionsr   "lightning.fabric.utilities.importsr    lightning.fabric.utilities.typesr   r   r   rJ   r   r   	Unpicklerr   r   r   r}   r   r   r"   r"   r"   r#   <module>   sD   0 	


