class_docstr: Tests for bluefog/torch/mpi_ops.py
class_docstr_tok: ["Tests","for","bluefog\/torch\/mpi_ops.py"]
code: def test_pair_gossip_weighted(self):
      size = bf.size()
      rank = bf.rank()
      target_rank = rank - 1 if rank % 2 else rank + 1
      if bf.size() % 2:
          warnings.warn(
              "Pair gossip(weighted) only run with even processes. Skipped."
          )
          return
      dtypes = [torch.HalfTensor, torch.FloatTensor, torch.DoubleTensor]
      if TEST_ON_GPU:
          dtypes += [torch.cuda.FloatTensor, torch.cuda.DoubleTensor]
  
      # Pair gossip cannot run with negotiation yet.
      bf.set_skip_negotiate_stage(True)
  
      expect_result = 0.25 * rank + 0.75 * target_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)
          # gossiped_tensor = bf.pair_gossip(
          #    tensor, target_rank, self_weight=0.25, pair_weight=0.75)
          tensor, gossiped_tensor = self.convert_cpu_fp16_to_fp32(
              tensor, gossiped_tensor
          )
          assert (
              list(gossiped_tensor.shape) == [23] * dim
          ), "bf.pair_gossip(weighted) produces incorrect reduced shape"
          assert (
              gossiped_tensor.data - expect_result
          ).abs().max() < EPSILON, "bf.pair_gossip(weighted) produces incorrect reduced tensor"
      bf.set_skip_negotiate_stage(False)
  
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 39 for calling the OpsTests obj's underlying member method code with example usage: obj.test_pair_gossip_weighted() without return types