class_docstr: Tests for bluefog/torch/mpi_ops.py
class_docstr_tok: ["Tests","for","bluefog\/torch\/mpi_ops.py"]
docstr: Test that the neighbor all reduce (avg) 1D, 2D, 3D tensors
  correctly in a 2D meshgrid topology.
code: def test_neighbor_allreduce_avg_meshgrid_topo(self):
      """
      Test that the neighbor all reduce (avg) 1D, 2D, 3D tensors
      correctly in a 2D meshgrid topology.
      """
      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.HalfTensor, torch.FloatTensor, torch.DoubleTensor]
      if TEST_ON_GPU:
          dtypes += [torch.cuda.FloatTensor, torch.cuda.DoubleTensor]
  
      is_set = bf.set_topology(topology_util.MeshGrid2DGraph(size))
      assert is_set, "Topology set failed."
  
      topology = bf.load_topology()
      neighbor_array_with_self = np.nonzero(
          nx.to_numpy_matrix(topology)[rank]
      )[1]
      num_indegree = len(neighbor_array_with_self) - 1
      sum_value = neighbor_array_with_self.sum()
  
      dims = [1, 2, 3]
      for dtype, dim in itertools.product(dtypes, dims):
          tensor = torch.FloatTensor(*([23] * dim)).fill_(1).mul_(rank)
          tensor = self.cast_and_place(tensor, dtype)
          reduced_tensor = bf.neighbor_allreduce(tensor)
          eps = (
              EPSILON
              if tensor.dtype != torch.float16
              else LOOSE_EPSILON
          )
          tensor, reduced_tensor = self.convert_cpu_fp16_to_fp32(
              tensor, reduced_tensor
          )
          assert (
              list(reduced_tensor.shape) == [23] * dim
          ), "bf.neighbor_allreduce (avg) produces incorrect reduced shape"
          assert (
              reduced_tensor.data.mul_(num_indegree + 1) - sum_value
          ).abs().max() < eps, "bf.neighbor_allreduce (avg) produces incorrect reduced 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 22 for calling the OpsTests obj's underlying member method code with example usage: obj.test_neighbor_allreduce_avg_meshgrid_topo() without return types