code: def test_allgather_torch_cupy(ray_start_distributed_2_nodes_4_gpus):
      world_size = 4
      shape = [10, 10]
      actors, _ = create_collective_workers(world_size)
  
      # tensor is pytorch, list is cupy
      for i, a in enumerate(actors):
          t = torch.ones(shape, dtype=torch.float32).cuda() * (i + 1)
          ray.wait([a.set_buffer.remote(t)])
          list_buffer = [
              cp.ones(shape, dtype=cp.float32)
              for _ in range(world_size)
          ]
          ray.wait([a.set_list_buffer.remote(list_buffer)])
      results = ray.get([a.do_allgather.remote() for a in actors])
      for i in range(world_size):
          for j in range(world_size):
              assert (
                  results[i][j]
                  == cp.ones(shape, dtype=cp.float32) * (j + 1)
              ).all()
  
      # tensor is cupy, list is pytorch
      for i, a in enumerate(actors):
          t = cp.ones(shape, dtype=cp.float32) * (i + 1)
          ray.wait([a.set_buffer.remote(t)])
          list_buffer = [
              torch.ones(shape, dtype=torch.float32).cuda()
              for _ in range(world_size)
          ]
          ray.wait([a.set_list_buffer.remote(list_buffer)])
      results = ray.get([a.do_allgather.remote() for a in actors])
      for i in range(world_size):
          for j in range(world_size):
              assert (
                  results[i][j]
                  == torch.ones(shape, dtype=torch.float32).cuda()
                  * (j + 1)
              ).all()
  
      # some tensors in the list are pytorch, some are cupy
      for i, a in enumerate(actors):
          t = cp.ones(shape, dtype=cp.float32) * (i + 1)
          ray.wait([a.set_buffer.remote(t)])
          list_buffer = []
          for j in range(world_size):
              if j % 2 == 0:
                  list_buffer.append(
                      torch.ones(shape, dtype=torch.float32).cuda()
                  )
              else:
                  list_buffer.append(cp.ones(shape, dtype=cp.float32))
          ray.wait([a.set_list_buffer.remote(list_buffer)])
      results = ray.get([a.do_allgather.remote() for a in actors])
      for i in range(world_size):
          for j in range(world_size):
              if j % 2 == 0:
                  assert (
                      results[i][j]
                      == torch.ones(shape, dtype=torch.float32).cuda()
                      * (j + 1)
                  ).all()
              else:
                  assert (
                      results[i][j]
                      == cp.ones(shape, dtype=cp.float32) * (j + 1)
                  ).all()
  
file_path: ray/python/ray/util/collective/tests/distributed_gpu_tests/test_distributed_allgather.py
imports: import pytest
  import ray
  import cupy
  import torch
  from ray.util.collective.tests.util import create_collective_workers, init_tensors_for_gather_scatter
label_desc: Use image node_id 5 for calling a global function with example usage: test_allgather_torch_cupy(ray_start_distributed_2_nodes_4_gpus) without return types