def test_closure_with_no_grad_optimizer(tmp_path):
    """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=tmp_path, fast_dev_run=1)
    model = TestModel()
    trainer.fit(model)
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.

    
tests/tests_pytorch/loops/optimization/test_closure.py