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 gather 1D, 2D, 3D tensors correctly in dynamic setting.
code: def test_neighbor_allgather_dynamic(self):
      """Test that the neighbor all gather 1D, 2D, 3D tensors correctly in dynamic setting."""
      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.IntTensor,
          torch.DoubleTensor,
          torch.LongTensor,
          torch.ByteTensor,
          torch.CharTensor,
          torch.ShortTensor,
          torch.HalfTensor,
      ]
      if TEST_ON_GPU:
          dtypes += [torch.cuda.FloatTensor, torch.cuda.DoubleTensor]
  
      # Connect to all other ranks
      neighbor_ranks = [i for i in range(size) if i != rank]
      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)
          gathered = bf.neighbor_allgather(
              tensor, dst_ranks=neighbor_ranks, src_ranks=neighbor_ranks
          )
          tensor, gathered = self.convert_cpu_fp16_to_fp32(
              tensor, gathered
          )
  
          assert list(gathered.shape) == [23 * (size - 1)] + [23] * (
              dim - 1
          )
  
          candidate_ranks = list(map(float, neighbor_ranks[:]))
          gathered_ranks = []
  
          for i, _ in enumerate(neighbor_ranks):
              rank_tensor = gathered[i * 23 : (i + 1) * 23]
              assert (
                  list(rank_tensor.shape) == [23] * dim
              ), "bf.neighbor_allgather(dynamic) produces incorrect gathered shape"
              assert (
                  rank_tensor.data.min() == rank_tensor.data.max()
              ), "bf.neighbor_allgather(dynamic) produces incorrect gathered tensor"
              gathered_ranks.append(rank_tensor.data.max().item())
  
          assert (
              candidate_ranks == gathered_ranks
          ), "bf.neighbor_allgather(dynamic) produces incorrect gathered 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 34 for calling the OpsTests obj's underlying member method code with example usage: obj.test_neighbor_allgather_dynamic() without return types