o
    z“©iß  ã                   @   sX   d Z ddlZddlmZ ddlmZ ddlZddlm	Z	 ddl
mZ G dd„ deƒZdS )	zg
On exception checkpointing
==========================

Automatically save a checkpoints on exception.
é    N)ÚAny)Úoverride)Ú_PATH)Ú
Checkpointc                	       s‚   e Zd ZdZdZddededdf‡ fdd	„Zedefd
d„ƒZ	e
dddededdfdd„ƒZe
dddededdfdd„ƒZ‡  ZS )ÚOnExceptionCheckpointaÔ  Used to save a checkpoint on exception.

    Args:
        dirpath: directory to save the checkpoint file.
        filename: checkpoint filename. This must not include the extension.

    Raises:
        ValueError:
            If ``filename`` is empty.


    Example:
        >>> from pytorch_lightning import Trainer
        >>> from pytorch_lightning.callbacks import OnExceptionCheckpoint
        >>> trainer = Trainer(callbacks=[OnExceptionCheckpoint(".")])

    z.ckptÚon_exceptionÚdirpathÚfilenameÚreturnNc                    s&   t ƒ  ¡  |stdƒ‚|| _|| _d S )NzThe filename cannot be empty)ÚsuperÚ__init__Ú
ValueErrorr   r	   )Úselfr   r	   ©Ú	__class__© úg/home/ubuntu/.local/lib/python3.10/site-packages/pytorch_lightning/callbacks/on_exception_checkpoint.pyr   4   s
   

zOnExceptionCheckpoint.__init__c                 C   s   t j | j| j| j ¡S ©N)ÚosÚpathÚjoinr   r	   ÚFILE_EXTENSION)r   r   r   r   Ú	ckpt_path<   s   zOnExceptionCheckpoint.ckpt_pathÚtrainerz
pl.TrainerÚ_Ú__c                 O   s   |  | j¡ d S r   )Úsave_checkpointr   ©r   r   r   r   r   r   r   r   @   s   z"OnExceptionCheckpoint.on_exceptionc                 O   s   |j  | j¡ d S r   )ÚstrategyÚremove_checkpointr   r   r   r   r   ÚteardownE   s   zOnExceptionCheckpoint.teardown)r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   Ústrr   Úpropertyr   r   r   r   r    Ú__classcell__r   r   r   r   r      s    $r   )r$   r   Útypingr   Útyping_extensionsr   Úpytorch_lightningÚplÚ lightning_fabric.utilities.typesr   Úpytorch_lightning.callbacksr   r   r   r   r   r   Ú<module>   s   