code: def test_post_local_sgd_model_averaging_raises(
      average_parameters_mock, tmpdir
  ):
      """Test that when using DDP with post-localSGD a ValueError is thrown when the optimizer is
      ZeroRedundancyOptimizer."""
      from torch.distributed.optim import ZeroRedundancyOptimizer
  
      class OptimizerModel(BoringModel):
          def configure_optimizers(self):
              return ZeroRedundancyOptimizer(
                  params=self.parameters(),
                  optimizer_class=torch.optim.Adam,
                  lr=0.01,
              )
  
      model = OptimizerModel()
      strategy = DDPStrategy(
          ddp_comm_state=post_localSGD.PostLocalSGDState(
              process_group=None,
              subgroup=None,
              start_localSGD_iter=8,
          ),
          ddp_comm_hook=post_localSGD.post_localSGD_hook,
          model_averaging_period=4,
      )
  
      trainer = Trainer(
          fast_dev_run=True,
          accelerator="gpu",
          devices=2,
          strategy=strategy,
          default_root_dir=tmpdir,
          sync_batchnorm=True,
          enable_progress_bar=False,
          enable_model_summary=False,
      )
  
      with pytest.raises(
          ValueError,
          match="Currently model averaging cannot work with a distributed optimizer",
      ):
          trainer.fit(model)
  
      average_parameters_mock.assert_not_called()
  
file_path: pytorch-lightning/tests/tests_pytorch/strategies/test_ddp_integration_comm_hook.py
imports: from unittest import mock
  import pytest
  import torch
  from lightning.pytorch import Trainer
  from lightning.pytorch.demos.boring_classes import BoringModel
  from lightning.pytorch.strategies import DDPStrategy
  from tests_pytorch.helpers.runif import RunIf
label_desc: Use image node_id 7 for calling a global function with example usage: test_post_local_sgd_model_averaging_raises(average_parameters_mock, tmpdir) and returns: ZeroRedundancyOptimizer