code: def test_closure_with_no_grad_optimizer(tmpdir):
      """Test that the closure is guaranteed to run with grad enabled.
  
      There are certain third-party library optimizers
      (such as Hugging Face Transformers' AdamW) that set `no_grad` during the `step` operation.
  
      """
  
      class NoGradAdamW(torch.optim.AdamW):
          @torch.no_grad()
          def step(self, closure):
              if closure is not None:
                  closure()
              return super().step()
  
      class TestModel(BoringModel):
          def training_step(self, batch, batch_idx):
              assert torch.is_grad_enabled()
              return super().training_step(batch, batch_idx)
  
          def configure_optimizers(self):
              return NoGradAdamW(self.parameters(), lr=0.1)
  
      trainer = Trainer(default_root_dir=tmpdir, fast_dev_run=1)
      model = TestModel()
      trainer.fit(model)
  
imports: import pytest
  import torch
  from lightning.pytorch import Trainer
  from lightning.pytorch.demos.boring_classes import BoringModel
  from lightning.pytorch.utilities.exceptions import MisconfigurationException
label_desc: Use image node_id 2 for calling a global function with example usage: test_closure_with_no_grad_optimizer(tmpdir) and returns: super, super, NoGradAdamW