def test_backward_count_with_closure(torch_backward, tmp_path):
    """Using a closure (e.g. with LBFGS) should lead to no extra backward calls."""

    class TestModel(BoringModel):
        def configure_optimizers(self):
            return torch.optim.LBFGS(self.parameters(), lr=0.1)

    model = TestModel()
    trainer = Trainer(max_steps=5, logger=False, enable_checkpointing=False)
    trainer.fit(model)
    assert torch_backward.call_count == 5

    torch_backward.reset_mock()

    trainer = Trainer(max_steps=5, accumulate_grad_batches=2, logger=False, enable_checkpointing=False)
    trainer.fit(model)
    assert torch_backward.call_count == 10
Using a closure (e.g. with LBFGS) should lead to no extra backward calls.
tests/tests_pytorch/trainer/optimization/test_backward_calls.py