a
    d'                     @   s^   d Z ddlmZmZ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ListOptionalType)Tensor)	OptimizerN)STEP_OUTPUTc                   @   st  e Zd ZdZeedddZeed  dddZe	eddd	Z
d
deddddZd
deddddZd
dddddZd
dddddZd
dddddZd
dddddZd
de	eddddZd
dee	edddd Zd
dddd!d"Zd
dddd#d$Zd
dddd%d&Zd
dddd'd(Zd
dddd)d*Zd
dddd+d,Zd
dddd-d.Zd
dee	 dd/d0d1Zd
de	eedd2d3d4Zd
dee e	eedd5d6d7Z d
de	eedd2d8d9Z!d
dee e	eedd5d:d;Z"d
de	eedd2d<d=Z#d
de	e	eedd5d>d?Z$d
dddd@dAZ%d
ddddBdCZ&d
ddddDdEZ'd
ddddFdGZ(d
ddddHdIZ)d
ddddJdKZ*d
ddddLdMZ+d
ddddNdOZ,d
de-ddPdQdRZ.e/ee	f ddSdTZ0e/ee	f ddUdVdWZ1d
de/ee	f ddXdYdZZ2d
de/ee	f ddXd[d\Z3d
de4dd]d^d_Z5d
dddd`daZ6d
de7eddbdcddZ8d
de7ddedfdgZ9dS )hCallbackzz
    Abstract base class used to build new callbacks.

    Subclass this class and override any of the relevant hooks
    )returnc                 C   s   | j jS )at  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   m/var/www/html/stable-diffusion-webui/venv/lib/python3.9/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)kwargsr   c                 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_key2   s    zCallback._generate_state_keyz
pl.Trainerzpl.LightningModuleN)trainer	pl_modulestager   c                 C   s   dS )z9Called when fit, validate, test, predict, or tune begins.Nr   r   r   r   r   r   r   r   setup;   s    zCallback.setupc                 C   s   dS )z7Called when fit, validate, test, predict, or tune ends.Nr   r   r   r   r   teardown>   s    zCallback.teardown)r   r   r   c                 C   s   dS )zCalled when fit begins.Nr   r   r   r   r   r   r   on_fit_startA   s    zCallback.on_fit_startc                 C   s   dS )zCalled when fit ends.Nr   r   r   r   r   
on_fit_endD   s    zCallback.on_fit_endc                 C   s   dS )z/Called when the validation sanity check starts.Nr   r   r   r   r   on_sanity_check_startG   s    zCallback.on_sanity_check_startc                 C   s   dS )z-Called when the validation sanity check ends.Nr   r   r   r   r   on_sanity_check_endJ   s    zCallback.on_sanity_check_end)r   r   batch	batch_idxr   c                 C   s   dS )z#Called when the train batch begins.Nr   )r   r   r   r#   r$   r   r   r   on_train_batch_startM   s    zCallback.on_train_batch_start)r   r   outputsr#   r$   r   c                 C   s   dS )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_endR   s    zCallback.on_train_batch_endc                 C   s   dS )z#Called when the train epoch begins.Nr   r   r   r   r   on_train_epoch_start\   s    zCallback.on_train_epoch_startc                 C   s   dS )aI  Called when the train epoch ends.

        To access all batch outputs at the end of the epoch, either:

        1. Implement `training_epoch_end` in the `LightningModule` and access outputs via the module OR
        2. Cache data across train batch hooks inside the callback implementation to post-process in this hook.
        Nr   r   r   r   r   on_train_epoch_end_   s    zCallback.on_train_epoch_endc                 C   s   dS )z!Called when the val epoch begins.Nr   r   r   r   r   on_validation_epoch_starth   s    z"Callback.on_validation_epoch_startc                 C   s   dS )zCalled when the val epoch ends.Nr   r   r   r   r   on_validation_epoch_endk   s    z Callback.on_validation_epoch_endc                 C   s   dS )z"Called when the test epoch begins.Nr   r   r   r   r   on_test_epoch_startn   s    zCallback.on_test_epoch_startc                 C   s   dS )z Called when the test epoch ends.Nr   r   r   r   r   on_test_epoch_endq   s    zCallback.on_test_epoch_endc                 C   s   dS )z%Called when the predict epoch begins.Nr   r   r   r   r   on_predict_epoch_startt   s    zCallback.on_predict_epoch_start)r   r   r&   r   c                 C   s   dS )z#Called when the predict epoch ends.Nr   )r   r   r   r&   r   r   r   on_predict_epoch_endw   s    zCallback.on_predict_epoch_end)r   r   r#   r$   dataloader_idxr   c                 C   s   dS )z(Called when the validation batch begins.Nr   r   r   r   r#   r$   r0   r   r   r   on_validation_batch_startz   s    z"Callback.on_validation_batch_start)r   r   r&   r#   r$   r0   r   c                 C   s   dS )z&Called when the validation batch ends.Nr   r   r   r   r&   r#   r$   r0   r   r   r   on_validation_batch_end   s    	z Callback.on_validation_batch_endc                 C   s   dS )z"Called when the test batch begins.Nr   r1   r   r   r   on_test_batch_start   s    zCallback.on_test_batch_startc                 C   s   dS )z Called when the test batch ends.Nr   r3   r   r   r   on_test_batch_end   s    	zCallback.on_test_batch_endc                 C   s   dS )z%Called when the predict batch begins.Nr   r1   r   r   r   on_predict_batch_start   s    zCallback.on_predict_batch_startc                 C   s   dS )z#Called when the predict batch ends.Nr   r3   r   r   r   on_predict_batch_end   s    	zCallback.on_predict_batch_endc                 C   s   dS )zCalled when the train begins.Nr   r   r   r   r   on_train_start   s    zCallback.on_train_startc                 C   s   dS )zCalled when the train ends.Nr   r   r   r   r   on_train_end   s    zCallback.on_train_endc                 C   s   dS )z'Called when the validation loop begins.Nr   r   r   r   r   on_validation_start   s    zCallback.on_validation_startc                 C   s   dS )z%Called when the validation loop ends.Nr   r   r   r   r   on_validation_end   s    zCallback.on_validation_endc                 C   s   dS )zCalled when the test begins.Nr   r   r   r   r   on_test_start   s    zCallback.on_test_startc                 C   s   dS )zCalled when the test ends.Nr   r   r   r   r   on_test_end   s    zCallback.on_test_endc                 C   s   dS )zCalled when the predict begins.Nr   r   r   r   r   on_predict_start   s    zCallback.on_predict_startc                 C   s   dS )zCalled when predict ends.Nr   r   r   r   r   on_predict_end   s    zCallback.on_predict_end)r   r   	exceptionr   c                 C   s   dS )zACalled when any trainer execution is interrupted by an exception.Nr   )r   r   r   rA   r   r   r   on_exception   s    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   s    zCallback.state_dict)rC   r   c                 C   s   dS )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   rC   r   r   r   load_state_dict   s    zCallback.load_state_dict)r   r   
checkpointr   c                 C   s   dS )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` instance.
            pl_module: the current :class:`~pytorch_lightning.core.module.LightningModule` instance.
            checkpoint: the checkpoint dictionary that will be saved.
        Nr   r   r   r   rE   r   r   r   on_save_checkpoint   s    zCallback.on_save_checkpointc                 C   s   dS )ap  
        Called when loading a model checkpoint, use to reload state.

        Args:
            trainer: the current :class:`~pytorch_lightning.trainer.Trainer` instance.
            pl_module: the current :class:`~pytorch_lightning.core.module.LightningModule` instance.
            checkpoint: the full checkpoint dictionary that got loaded by the Trainer.
        Nr   rF   r   r   r   on_load_checkpoint   s    zCallback.on_load_checkpoint)r   r   lossr   c                 C   s   dS )z"Called before ``loss.backward()``.Nr   )r   r   r   rI   r   r   r   on_before_backward   s    zCallback.on_before_backwardc                 C   s   dS )zCCalled after ``loss.backward()`` and before optimizers are stepped.Nr   r   r   r   r   on_after_backward   s    zCallback.on_after_backward)r   r   	optimizeropt_idxr   c                 C   s   dS )z#Called before ``optimizer.step()``.Nr   )r   r   r   rL   rM   r   r   r   on_before_optimizer_step   s    z!Callback.on_before_optimizer_step)r   r   rL   r   c                 C   s   dS )z(Called before ``optimizer.zero_grad()``.Nr   )r   r   r   rL   r   r   r   on_before_zero_grad   s    zCallback.on_before_zero_grad):__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-   r.   r   r/   r2   r   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   BaseExceptionrB   r   rC   rD   rG   rH   r   rJ   rK   r   rN   rO   r   r   r   r   r
      s   		

	


	r
   )rR   typingr   r   r   r   r   Ztorchr   Ztorch.optimr   Zpytorch_lightningplZ!pytorch_lightning.utilities.typesr	   r
   r   r   r   r   <module>   s   