code: def test(self):
      skip_on_arch(min_arch=7)
  
      config_dict = {
          "train_batch_size": 2,
          "steps_per_print": 1,
          "sparse_gradients": True,
      }
      model, optimizer = get_model_optimizer()
      loss = torch.nn.BCEWithLogitsLoss()
      engine, _, _, _ = deepspeed.initialize(
          model=model, optimizer=optimizer, config=config_dict
      )
  
      x, offsets, y = get_data(engine.device)
  
      engine.gradient_average = True
      res = engine(x, offsets)
      engine.backward(loss(res, y))
  
      averaged_grads = {}
      for k, v in engine.named_parameters():
          grad = v.grad.to_dense() if v.grad.is_sparse else v.grad
          averaged_grads[k] = grad
          v.grad = None
  
      engine.gradient_average = False
      res = engine(x, offsets)
      engine.backward(loss(res, y))
  
      for k, v in engine.named_parameters():
          grad = v.grad.to_dense() if v.grad.is_sparse else v.grad
          assert torch.allclose(
              grad, averaged_grads[k] * engine.world_size
          )
  
file_path: DeepSpeed/tests/unit/runtime/sparse_tensor/test_averaging_sparse_gradients.py
imports: import torch
  import deepspeed
  from unit.common import DistributedTest
  from unit.util import skip_on_arch
label_desc: Use image node_id 1 for calling the TestSparseAdam obj's underlying member method code with example usage: obj.test() without return types