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 ring topology.
code: def test_neighbor_allreduce_avg_ring_topo(self):
      """
      Test that the neighbor all reduce (avg) 1D, 2D, 3D tensors correctly
      in a ring 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]
  
      for connect_direction in [1, 2]:
          is_set = bf.set_topology(
              topology_util.RingGraph(size, connect_direction)
          )
          assert is_set, "Topology set failed."
  
          num_indegree = 1
          sum_value = (
              rank
              + (rank + (1 if connect_direction == 1 else -1)) % size
          )
  
          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 24 for calling the OpsTests obj's underlying member method code with example usage: obj.test_neighbor_allreduce_avg_ring_topo() without return types