o
    zi+                     @   sV   d Z ddlmZmZmZ ddlmZ ddlmZ ddl	Z
ddlmZ G dd dZdS )	z'Base class used to build new callbacks.    )AnyDictType)Tensor)	OptimizerN)STEP_OUTPUTc                   @   s\  e Zd ZdZedefddZeded  fddZde	defdd	Z
d
ddddeddfddZd
ddddeddfddZdhddZdhddZdhddZdhddZd
dddde	deddf
ddZd
dddd ede	deddfd!d"Zdhd#d$Zdhd%d&Zdhd'd(Zdhd)d*Zdhd+d,Zdhd-d.Zdhd/d0Zdhd1d2Z	3did
dddde	ded4eddfd5d6Z	3did
dddd ede	ded4eddfd7d8Z	3did
dddde	ded4eddfd9d:Z	3did
dddd ede	ded4eddfd;d<Z 	3did
dddde	ded4eddfd=d>Z!	3did
dddd e	de	ded4eddfd?d@Z"dhdAdBZ#dhdCdDZ$dhdEdFZ%dhdGdHZ&dhdIdJZ'dhdKdLZ(dhdMdNZ)dhdOdPZ*d
ddddQe+ddfdRdSZ,de-ee	f fdTdUZ.dVe-ee	f ddfdWdXZ/d
ddddYe-ee	f ddfdZd[Z0d
ddddYe-ee	f ddfd\d]Z1d
dddd^e2ddfd_d`Z3dhdadbZ4d
ddddce5ddfdddeZ6d
ddddce5ddfdfdgZ7dS )jCallbackzvAbstract base class used to build new callbacks.

    Subclass this class and override any of the relevant hooks

    returnc                 C   s   | j jS )au  Identifier for the state of the callback.

        Used to store and retrieve a callback's state from the checkpoint dictionary by
        ``checkpoint["callbacks"][state_key]``. Implementations of a callback need to provide a unique state key if 1)
        the callback has state and 2) it is desired to maintain the state of multiple instances of that callback.

        )	__class____qualname__self r   X/home/ubuntu/.local/lib/python3.10/site-packages/pytorch_lightning/callbacks/callback.py	state_key    s   	zCallback.state_keyc                 C   s   t | S )z7State key for checkpoints saved prior to version 1.5.0.)typer   r   r   r   _legacy_state_key+   s   zCallback._legacy_state_keykwargsc                 K   s   | j j t| S )zFormats a set of key-value pairs into a state key string with the callback class name prefixed. Useful for
        defining a :attr:`state_key`.

        Args:
            **kwargs: A set of key-value pairs. Must be serializable to :class:`str`.

        )r
   r   repr)r   r   r   r   r   _generate_state_key0   s   zCallback._generate_state_keytrainer
pl.Trainer	pl_modulepl.LightningModulestageNc                 C      dS )z9Called when fit, validate, test, predict, or tune begins.Nr   r   r   r   r   r   r   r   setup:       zCallback.setupc                 C   r   )z7Called when fit, validate, test, predict, or tune ends.Nr   r   r   r   r   teardown=   r   zCallback.teardownc                 C   r   )zCalled when fit begins.Nr   r   r   r   r   r   r   on_fit_start@   r   zCallback.on_fit_startc                 C   r   )zCalled when fit ends.Nr   r    r   r   r   
on_fit_endC   r   zCallback.on_fit_endc                 C   r   )z/Called when the validation sanity check starts.Nr   r    r   r   r   on_sanity_check_startF   r   zCallback.on_sanity_check_startc                 C   r   )z-Called when the validation sanity check ends.Nr   r    r   r   r   on_sanity_check_endI   r   zCallback.on_sanity_check_endbatch	batch_idxc                 C   r   )z#Called when the train batch begins.Nr   )r   r   r   r%   r&   r   r   r   on_train_batch_startL   r   zCallback.on_train_batch_startoutputsc                 C   r   )zCalled when the train batch ends.

        Note:
            The value ``outputs["loss"]`` here will be the normalized value w.r.t ``accumulate_grad_batches`` of the
            loss returned from ``training_step``.

        Nr   )r   r   r   r(   r%   r&   r   r   r   on_train_batch_endQ   r   zCallback.on_train_batch_endc                 C   r   )z#Called when the train epoch begins.Nr   r    r   r   r   on_train_epoch_start\   r   zCallback.on_train_epoch_startc                 C   r   )a+  Called when the train epoch ends.

        To access all batch outputs at the end of the epoch, you can cache step outputs as an attribute of the
        :class:`pytorch_lightning.core.LightningModule` and access them in this hook:

        .. code-block:: python

            class MyLightningModule(L.LightningModule):
                def __init__(self):
                    super().__init__()
                    self.training_step_outputs = []

                def training_step(self):
                    loss = ...
                    self.training_step_outputs.append(loss)
                    return loss


            class MyCallback(L.Callback):
                def on_train_epoch_end(self, trainer, pl_module):
                    # do something with all training_step outputs, for example:
                    epoch_mean = torch.stack(pl_module.training_step_outputs).mean()
                    pl_module.log("training_epoch_mean", epoch_mean)
                    # free up the memory
                    pl_module.training_step_outputs.clear()

        Nr   r    r   r   r   on_train_epoch_end_   r   zCallback.on_train_epoch_endc                 C   r   )z!Called when the val epoch begins.Nr   r    r   r   r   on_validation_epoch_start|   r   z"Callback.on_validation_epoch_startc                 C   r   )zCalled when the val epoch ends.Nr   r    r   r   r   on_validation_epoch_end   r   z Callback.on_validation_epoch_endc                 C   r   )z"Called when the test epoch begins.Nr   r    r   r   r   on_test_epoch_start   r   zCallback.on_test_epoch_startc                 C   r   )z Called when the test epoch ends.Nr   r    r   r   r   on_test_epoch_end   r   zCallback.on_test_epoch_endc                 C   r   )z%Called when the predict epoch begins.Nr   r    r   r   r   on_predict_epoch_start   r   zCallback.on_predict_epoch_startc                 C   r   )z#Called when the predict epoch ends.Nr   r    r   r   r   on_predict_epoch_end   r   zCallback.on_predict_epoch_endr   dataloader_idxc                 C   r   )z(Called when the validation batch begins.Nr   r   r   r   r%   r&   r2   r   r   r   on_validation_batch_start   r   z"Callback.on_validation_batch_startc                 C   r   )z&Called when the validation batch ends.Nr   r   r   r   r(   r%   r&   r2   r   r   r   on_validation_batch_end   r   z Callback.on_validation_batch_endc                 C   r   )z"Called when the test batch begins.Nr   r3   r   r   r   on_test_batch_start   r   zCallback.on_test_batch_startc                 C   r   )z Called when the test batch ends.Nr   r5   r   r   r   on_test_batch_end   r   zCallback.on_test_batch_endc                 C   r   )z%Called when the predict batch begins.Nr   r3   r   r   r   on_predict_batch_start   r   zCallback.on_predict_batch_startc                 C   r   )z#Called when the predict batch ends.Nr   r5   r   r   r   on_predict_batch_end   r   zCallback.on_predict_batch_endc                 C   r   )zCalled when the train begins.Nr   r    r   r   r   on_train_start   r   zCallback.on_train_startc                 C   r   )zCalled when the train ends.Nr   r    r   r   r   on_train_end   r   zCallback.on_train_endc                 C   r   )z'Called when the validation loop begins.Nr   r    r   r   r   on_validation_start   r   zCallback.on_validation_startc                 C   r   )z%Called when the validation loop ends.Nr   r    r   r   r   on_validation_end   r   zCallback.on_validation_endc                 C   r   )zCalled when the test begins.Nr   r    r   r   r   on_test_start   r   zCallback.on_test_startc                 C   r   )zCalled when the test ends.Nr   r    r   r   r   on_test_end   r   zCallback.on_test_endc                 C   r   )zCalled when the predict begins.Nr   r    r   r   r   on_predict_start   r   zCallback.on_predict_startc                 C   r   )zCalled when predict ends.Nr   r    r   r   r   on_predict_end   r   zCallback.on_predict_end	exceptionc                 C   r   )zACalled when any trainer execution is interrupted by an exception.Nr   )r   r   r   rC   r   r   r   on_exception   r   zCallback.on_exceptionc                 C   s   i S )zCalled when saving a checkpoint, implement to generate callback's ``state_dict``.

        Returns:
            A dictionary containing callback state.

        r   r   r   r   r   
state_dict      zCallback.state_dictrE   c                 C   r   )zCalled when loading a checkpoint, implement to reload callback state given callback's ``state_dict``.

        Args:
            state_dict: the callback state returned by ``state_dict``.

        Nr   )r   rE   r   r   r   load_state_dict   rF   zCallback.load_state_dict
checkpointc                 C   r   )a  Called when saving a checkpoint to give you a chance to store anything else you might want to save.

        Args:
            trainer: the current :class:`~pytorch_lightning.trainer.trainer.Trainer` instance.
            pl_module: the current :class:`~pytorch_lightning.core.LightningModule` instance.
            checkpoint: the checkpoint dictionary that will be saved.

        Nr   r   r   r   rH   r   r   r   on_save_checkpoint   r   zCallback.on_save_checkpointc                 C   r   )ai  Called when loading a model checkpoint, use to reload state.

        Args:
            trainer: the current :class:`~pytorch_lightning.trainer.trainer.Trainer` instance.
            pl_module: the current :class:`~pytorch_lightning.core.LightningModule` instance.
            checkpoint: the full checkpoint dictionary that got loaded by the Trainer.

        Nr   rI   r   r   r   on_load_checkpoint  r   zCallback.on_load_checkpointlossc                 C   r   )z"Called before ``loss.backward()``.Nr   )r   r   r   rL   r   r   r   on_before_backward  r   zCallback.on_before_backwardc                 C   r   )zCCalled after ``loss.backward()`` and before optimizers are stepped.Nr   r    r   r   r   on_after_backward  r   zCallback.on_after_backward	optimizerc                 C   r   )z#Called before ``optimizer.step()``.Nr   r   r   r   rO   r   r   r   on_before_optimizer_step  r   z!Callback.on_before_optimizer_stepc                 C   r   )z(Called before ``optimizer.zero_grad()``.Nr   rP   r   r   r   on_before_zero_grad  r   zCallback.on_before_zero_grad)r   r   r   r   r	   N)r   )8__name__
__module__r   __doc__propertystrr   r   r   r   r   r   r   r!   r"   r#   r$   intr'   r   r)   r*   r+   r,   r-   r.   r/   r0   r1   r4   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   BaseExceptionrD   r   rE   rG   rJ   rK   r   rM   rN   r   rQ   rR   r   r   r   r   r      s`   















	













		





r   )rU   typingr   r   r   torchr   torch.optimr   pytorch_lightningpl!pytorch_lightning.utilities.typesr   r   r   r   r   r   <module>   s   