o
    oi                     @   s:   d dl mZmZ d dlmZ d dlmZ G dd dZdS )    )DictOptionalN)_BaseProgressc                   @   s   e Zd ZdZdddZedefdd	Zejd
eddfdd	Zde	fddZ
de	ddfddZddee	 dede	fddZ	dde	deddfddZde	deddfddZdS )_LoopzBasic Loops interface.trainer
pl.TrainerreturnNc                 C   s   d| _ || _d S )NF)_restartingr   )selfr    r   P/home/ubuntu/.local/lib/python3.10/site-packages/lightning/pytorch/loops/loop.py__init__   s   
z_Loop.__init__c                 C   s   | j S )zDWhether the state of this loop was reloaded and it needs to restart.)r	   r
   r   r   r   
restarting   s   z_Loop.restartingr   c                 C   s,   || _ t|  D ]
}t|tr||_q	dS )z7Connects this loop's restarting value and its children.N)r	   varsvalues
isinstancer   r   )r
   r   loopr   r   r   r       s   
c                 C   s   i S )zCalled when saving a model checkpoint, use to persist loop state.

        Returns:
            The current loop state.

        r   r   r   r   r   on_save_checkpoint(   s   z_Loop.on_save_checkpoint
state_dictc                 C   s   dS )zACalled when loading a model checkpoint, use to reload loop state.Nr   )r
   r   r   r   r   on_load_checkpoint1   s    z_Loop.on_load_checkpoint destinationprefixc                 C   sn   |du ri }|   ||d < | j D ]!\}}|| }t|tr'| ||< qt|tr4|||d  q|S )a?  The state dict is determined by the state and progress of this loop and all its children.

        Args:
            destination: An existing dictionary to update with this loop's state. By default a new dictionary
                is returned.
            prefix: A prefix for each key in the state dictionary

        Nr   .)r   __dict__itemsr   r   r   r   )r
   r   r   kvkeyr   r   r   r   4   s   	

z_Loop.state_dictc                 C   sP   |  | | | j D ]\}}t|tr"|| || d  qd| _dS )z2Loads the state of this loop and all its children.r   TN)_load_from_state_dictcopyr   r   r   r   load_state_dictr   )r
   r   r   r   r   r   r   r   r"   J   s   

z_Loop.load_state_dictc                 C   sd   | j  D ]\}}|| }||vrqt|tr|||  q|d |v r0| ||d   d S d S )Nr   )r   r   r   r   r"   r   )r
   r   r   r   r   r   r   r   r   r    V   s   
z_Loop._load_from_state_dict)r   r   r   N)Nr   )r   )__name__
__module____qualname____doc__r   propertyboolr   setterr   r   r   r   strr   r"   r    r   r   r   r   r      s&    
	
r   )	typingr   r   lightning.pytorchpytorchpl lightning.pytorch.loops.progressr   r   r   r   r   r   <module>   s   