# Buggy code
def dump_checkpoint(self, weights_only: bool = False) -> dict:
    """Creating model checkpoint.

    Args:
        weights_only: saving model weights only

    Return:
         structured dictionary
    """
    checkpoint = {
        "epoch": self.current_epoch + 1,
        "global_step": self.global_step + 1,
        "pytorch-lightning_version": pytorch_lightning.__version__,
    }

    if not weights_only:
        # TODO support more generic way for callbacks to persist a state_dict in a checkpoint
        checkpoint_callbacks = [
            c for c in self.callbacks if isinstance(c, ModelCheckpoint)
        ]
        early_stopping_callbacks = [
            c for c in self.callbacks if isinstance(c, EarlyStopping)
        ]

        if checkpoint_callbacks:
            # we add the official checkpoint callback to the end of the list
            # extra user provided callbacks will not be persisted yet
            checkpoint[ModelCheckpoint.CHECKPOINT_STATE_BEST_SCORE] = (
                self.checkpoint_callback.best_model_score
            )
            checkpoint[ModelCheckpoint.CHECKPOINT_STATE_BEST_PATH] = (
                self.checkpoint_callback.best_model_path
            )

        if early_stopping_callbacks and checkpoint_callbacks:
            # we add the official early stopping callback to the end of the list
            # extra user provided callbacks will not be persisted yet
            checkpoint["early_stop_callback_state_dict"] = early_stopping_callbacks[
                -1
            ].state_dict()

        # save optimizers
        optimizer_states = []
        for i, optimizer in enumerate(self.optimizers):
            optimizer_states.append(optimizer.state_dict())
        checkpoint["optimizer_states"] = optimizer_states

        # save lr schedulers
        lr_schedulers = []
        for scheduler in self.lr_schedulers:
            lr_schedulers.append(scheduler["scheduler"].state_dict())
        checkpoint["lr_schedulers"] = lr_schedulers

        # save native amp scaling
        if self.amp_backend == AMPType.NATIVE and not self.use_tpu:
            checkpoint["native_amp_scaling_state"] = self.scaler.state_dict()
        elif self.amp_backend == AMPType.APEX:
            checkpoint["amp_scaling_state"] = amp.state_dict()

    # add the module_arguments and state_dict from the model
    model = self.get_model()

    checkpoint["state_dict"] = model.state_dict()

    if model.hparams:
        if hasattr(model, "_hparams_name"):
            checkpoint[LightningModule.CHECKPOINT_HYPER_PARAMS_NAME] = (
                model._hparams_name
            )
        # add arguments to the checkpoint
        checkpoint[LightningModule.CHECKPOINT_HYPER_PARAMS_KEY] = model.hparams
        if OMEGACONF_AVAILABLE:
            if isinstance(model.hparams, Container):
                checkpoint[LightningModule.CHECKPOINT_HYPER_PARAMS_TYPE] = type(
                    model.hparams
                )

    # give the model a chance to add a few things
    model.on_save_checkpoint(checkpoint)

    return checkpoint


# Traceback (AttributeError)
Running command:
python pipe/train_cnn.py
/home/gianluca/git/kaggle/siim-isic-melanoma-classification/.venv/lib/python3.7/site-packages/pytorch_lightning/utilities/distributed.py:25: UserWarning: Checkpoint directory /home/gianluca/git/kaggle/siim-isic-melanoma-classification/models exists and is not empty with save_top_k != 0.All files in this directory will be deleted when
a checkpoint is saved!
warnings.warn(*args, **kwargs)
Using cache found in /home/gianluca/.cache/torch/hub/facebookresearch_WSL-Images_master
GPU available: True, used: True
TPU available: False, using: 0 TPU cores
CUDA_VISIBLE_DEVICES: [0]
Using native 16bit precision.
Traceback (most recent call last):
File "pipe/train_cnn.py", line 237, in <module>
main(create_submission=True)
File "pipe/train_cnn.py", line 48, in main
preds, weight_fpath = train(fold_number=fold_number, folds=folds)
File "pipe/train_cnn.py", line 120, in train
trainer.fit(model)
File "/home/gianluca/git/kaggle/siim-isic-melanoma-classification/.venv/lib/python3.7/site-packages/pytorch_lightning/trainer/trainer.py", line 956, in fit
self._run_lr_finder_internally(model)
File "/home/gianluca/git/kaggle/siim-isic-melanoma-classification/.venv/lib/python3.7/site-packages/pytorch_lightning/trainer/lr_finder.py", line 58, in _run_lr_finder_internally
lr_finder = self.lr_find(model)
File "/home/gianluca/git/kaggle/siim-isic-melanoma-classification/.venv/lib/python3.7/site-packages/pytorch_lightning/trainer/lr_finder.py", line 180, in lr_find
self.save_checkpoint(str(save_path))
File "/home/gianluca/git/kaggle/siim-isic-melanoma-classification/.venv/lib/python3.7/site-packages/pytorch_lightning/trainer/training_io.py", line 268, in save_checkpoint
checkpoint = self.dump_checkpoint(weights_only)
File "/home/gianluca/git/kaggle/siim-isic-melanoma-classification/.venv/lib/python3.7/site-packages/pytorch_lightning/trainer/training_io.py", line 362, in dump_checkpoint
checkpoint['native_amp_scaling_state'] = self.scaler.state_dict()
AttributeError: 'NoneType' object has no attribute 'state_dict'
ERROR: failed to reproduce 'train_cnn.dvc': stage: 'train_cnn.dvc' cmd 'python pipe/train_cnn.py' failed

# Fixed code
def dump_checkpoint(self, weights_only: bool = False) -> dict:
    """Creating model checkpoint.

    Args:
        weights_only: saving model weights only

    Return:
         structured dictionary
    """
    checkpoint = {
        "epoch": self.current_epoch + 1,
        "global_step": self.global_step + 1,
        "pytorch-lightning_version": pytorch_lightning.__version__,
    }

    if not weights_only:
        # TODO support more generic way for callbacks to persist a state_dict in a checkpoint
        checkpoint_callbacks = [
            c for c in self.callbacks if isinstance(c, ModelCheckpoint)
        ]
        early_stopping_callbacks = [
            c for c in self.callbacks if isinstance(c, EarlyStopping)
        ]

        if checkpoint_callbacks:
            # we add the official checkpoint callback to the end of the list
            # extra user provided callbacks will not be persisted yet
            checkpoint[ModelCheckpoint.CHECKPOINT_STATE_BEST_SCORE] = (
                self.checkpoint_callback.best_model_score
            )
            checkpoint[ModelCheckpoint.CHECKPOINT_STATE_BEST_PATH] = (
                self.checkpoint_callback.best_model_path
            )

        if early_stopping_callbacks and checkpoint_callbacks:
            # we add the official early stopping callback to the end of the list
            # extra user provided callbacks will not be persisted yet
            checkpoint["early_stop_callback_state_dict"] = early_stopping_callbacks[
                -1
            ].state_dict()

        # save optimizers
        optimizer_states = []
        for i, optimizer in enumerate(self.optimizers):
            optimizer_states.append(optimizer.state_dict())
        checkpoint["optimizer_states"] = optimizer_states

        # save lr schedulers
        lr_schedulers = []
        for scheduler in self.lr_schedulers:
            lr_schedulers.append(scheduler["scheduler"].state_dict())
        checkpoint["lr_schedulers"] = lr_schedulers

        # save native amp scaling
        if (
            self.amp_backend == AMPType.NATIVE
            and not self.use_tpu
            and self.scaler is not None
        ):
            checkpoint["native_amp_scaling_state"] = self.scaler.state_dict()
        elif self.amp_backend == AMPType.APEX:
            checkpoint["amp_scaling_state"] = amp.state_dict()

    # add the module_arguments and state_dict from the model
    model = self.get_model()

    checkpoint["state_dict"] = model.state_dict()

    if model.hparams:
        if hasattr(model, "_hparams_name"):
            checkpoint[LightningModule.CHECKPOINT_HYPER_PARAMS_NAME] = (
                model._hparams_name
            )
        # add arguments to the checkpoint
        checkpoint[LightningModule.CHECKPOINT_HYPER_PARAMS_KEY] = model.hparams
        if OMEGACONF_AVAILABLE:
            if isinstance(model.hparams, Container):
                checkpoint[LightningModule.CHECKPOINT_HYPER_PARAMS_TYPE] = type(
                    model.hparams
                )

    # give the model a chance to add a few things
    model.on_save_checkpoint(checkpoint)

    return checkpoint