code: def test_lightning_optimizer_automatic_optimization_lbfgs_zero_grad(
      tmpdir,
  ):
      """Test zero_grad is called the same number of times as LBFGS requires for reevaluation of the loss in
      automatic_optimization."""
  
      class TestModel(BoringModel):
          def configure_optimizers(self):
              return torch.optim.LBFGS(self.parameters())
  
      model = TestModel()
      trainer = Trainer(
          default_root_dir=tmpdir,
          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
  
imports: from copy import deepcopy
  from unittest.mock import DEFAULT, Mock, patch
  import pytest
  import torch
  from lightning.pytorch import Trainer
  from lightning.pytorch.core.optimizer import LightningOptimizer
  from lightning.pytorch.demos.boring_classes import BoringModel
  from lightning.pytorch.loops.optimization.automatic import Closure
  from lightning.pytorch.tuner.tuning import Tuner
  from torch.optim import SGD, Adam, Optimizer
label_desc: Use image node_id 8 for calling a global function with example usage: test_lightning_optimizer_automatic_optimization_lbfgs_zero_grad(tmpdir) and returns: torch