def test_lightning_optimizer_automatic_optimization_lbfgs_zero_grad(tmp_path):
    """Test zero_grad is called the same number of times as LBFGS requires for reevaluation of the loss in
    automatic_optimization."""

    seed_everything(0)

    class TestModel(BoringModel):
        def configure_optimizers(self):
            return torch.optim.LBFGS(self.parameters())

    model = TestModel()
    trainer = Trainer(
        default_root_dir=tmp_path, limit_train_batches=1, limit_val_batches=1, max_epochs=1, enable_model_summary=False
    )

    with patch("torch.optim.LBFGS.zero_grad") as zero_grad:
        trainer.fit(model)

    lbfgs = model.optimizers()
    max_iter = lbfgs.param_groups[0]["max_iter"]
    assert zero_grad.call_count == max_iter
Test zero_grad is called the same number of times as LBFGS requires for reevaluation of the loss in
    automatic_optimization.
tests/tests_pytorch/core/test_lightning_optimizer.py