class_docstr: Tests for bluefog/torch/mpi_ops.py
class_docstr_tok: ["Tests","for","bluefog\/torch\/mpi_ops.py"]
docstr: Test that the allreduce correctly averages 1D, 2D, 3D tensors inplace.
code: def test_allreduce_avg_inplace(self):
      """Test that the allreduce correctly averages 1D, 2D, 3D tensors inplace."""
      size = bf.size()
      rank = bf.rank()
      if size <= 1:
          fname = inspect.currentframe().f_code.co_name
          warnings.warn("Skip {} due to size 1".format(fname))
          return
      dtypes = [torch.FloatTensor, torch.DoubleTensor, torch.HalfTensor]
      if TEST_ON_GPU:
          dtypes += [torch.cuda.FloatTensor, torch.cuda.DoubleTensor]
  
      dims = [1, 2, 3]
      for dtype, dim in itertools.product(dtypes, dims):
          torch.manual_seed(123456)
          tensor = torch.FloatTensor(*([23] * dim)).fill_(1).mul_(rank)
          name = "allreduce_tensor_{}_{}".format(dim, dtype)
          tensor = self.cast_and_place(tensor, dtype)
  
          bf.allreduce_(tensor, average=True, name=name)
          tensor = self.convert_cpu_fp16_to_fp32(tensor)[0]
          exp_tenosr = torch.ones_like(tensor).mul_((size - 1) / 2)
          assert torch.allclose(
              tensor, exp_tenosr
          ), "bf.allreduce_(avg) produces incorrect tensor"
  
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import inspect
  import itertools
  import unittest
  import warnings
  import numpy
  import pytest
  import torch
  import networkx
  import bluefog.torch
  from bluefog.common import topology_util
  from common import is_openmpi_built
label_desc: Use image node_id 9 for calling the OpsTests obj's underlying member method code with example usage: obj.test_allreduce_avg_inplace() without return types