code: def test_accelerated_optimizer_step_was_skipped(self):
      model = torch.nn.Linear(5, 5)
      optimizer = torch.optim.SGD(model.parameters(), 0.1)
      accelerator = Accelerator(mixed_precision="fp16")
      model, optimizer = accelerator.prepare(model, optimizer)
  
      loss = model(torch.randn(2, 5, device=accelerator.device)).sum()
      accelerator.backward(loss)
      for p in model.parameters():
          # Fake the gradients, as if there's no overflow
          p.grad.fill_(0.01)
  
      optimizer.step()
      self.assertTrue(optimizer.step_was_skipped is False)
  
      loss = model(torch.randn(2, 5, device=accelerator.device)).sum()
      accelerator.backward(loss)
      for p in model.parameters():
          p.grad.fill_(0.01)
      # Manually set the gradients to be NaN, as if there's an overflow
      p.grad[0] = torch.tensor(float("nan"))
  
      optimizer.step()
      self.assertTrue(optimizer.step_was_skipped is True)
  
      loss = model(torch.randn(2, 5, device=accelerator.device)).sum()
      accelerator.backward(loss)
      for p in model.parameters():
          p.grad.fill_(0.01)
      # Manually set the gradients to be NaN, as if there's an overflow
      p.grad[0] = torch.tensor(float("nan"))
  
      optimizer.step()
      self.assertTrue(optimizer.step_was_skipped is True)
  
      loss = model(torch.randn(2, 5, device=accelerator.device)).sum()
      accelerator.backward(loss)
      for p in model.parameters():
          # Fake the gradients, as if there's no overflow
          p.grad.fill_(0.01)
  
      optimizer.step()
      self.assertTrue(optimizer.step_was_skipped is False)
  
      AcceleratorState._reset_state()
  
file_path: accelerate/tests/test_optimizer.py
imports: import pickle
  import unittest
  import torch
  from accelerate import Accelerator
  from accelerate.state import AcceleratorState
  from accelerate.test_utils import require_cpu, require_cuda
label_desc: Use image node_id 1 for calling the CudaOptimizerTester obj's underlying member method code with example usage: obj.test_accelerated_optimizer_step_was_skipped() without return types