Trainer(resume_from_checkpoint=...) does not load optimizer state
🐛 Bug
The optimizer state is not loaded from the checkpoint. This is important if you want to correctly continue training. In practice, I had serious convergence issues if the optimizer state wasn't loaded.
To Reproduce
See code sample
Code sample
import os

import torch
from torch.nn import functional as F
from torch.utils.data import DataLoader
from torchvision.datasets import MNIST
from torchvision import transforms

import pytorch_lightning as pl
from pytorch_lightning.core.lightning import LightningModule

class LitModel(LightningModule):

    def __init__(self):
        super().__init__()
        self.l1 = torch.nn.Linear(28 * 28, 10)

    def forward(self, x):
        return torch.relu(self.l1(x.view(x.size(0), -1)))

    def training_step(self, batch, batch_idx):
        x, y = batch
        y_hat = self(x)
        loss = F.cross_entropy(y_hat, y)
        tensorboard_logs = {'train_loss': loss}
        return {'loss': loss, 'log': tensorboard_logs}

    def configure_optimizers(self):
        return torch.optim.Adam(self.parameters(), lr=0.001)

    def train_dataloader(self):
        dataset = MNIST(os.getcwd(), train=True, download=True, transform=transforms.ToTensor())
        loader = DataLoader(dataset, batch_size=32, num_workers=4, shuffle=True)
        return loader


model = LitModel()
trainer = pl.Trainer(fast_dev_run=True)
trainer.fit(model)

old_state = trainer.optimizers[0].state

trainer.save_checkpoint('checkpoint.pl')
new_trainer = pl.Trainer(resume_from_checkpoint='checkpoint.pl')

# currently new_trainer does not have optimizers, but inside it uses .init_optimizers to load them

new_optimizers, _, _ = new_trainer.init_optimizers(model)
new_state = new_optimizers[0].state

# new state is empty
old_state == new_state

Expected behavior
Initializing Trainer from checkpoint loads optimizer state.
Environment

PyTorch Version (e.g., 1.0): 1.5.0
OS (e.g., Linux): Linux
How you installed PyTorch (conda, pip, source): pip
Build command you used (if compiling from source):
Python version: 3.7.6
CUDA/cuDNN version: N/A
GPU models and configuration: CPU
Any other relevant information:

Additional context
What workaround can you use to load the optimizer state before the fix? The issue is that .init_optimizers is called inside .fit and even if you manually set trainer.optimizers is will be overwritten.