code: def test_allreduce_weighted(n_client_mode_peers: int = 2):
      n_peers = 4
      client_modes = [True] * n_client_mode_peers + [False] * (
          n_peers - n_client_mode_peers
      )
      random.shuffle(client_modes)
  
      tensors1 = [torch.randn(123), torch.zeros(3)]
      tensors2 = [torch.rand(123), torch.ones(3)]
      tensors3 = [-torch.rand(123), torch.arange(3).to(torch.float32)]
      tensors4 = [
          torch.randn(123) ** 3,
          torch.arange(3).to(torch.float32) / 2,
      ]
  
      dht_instances = launch_dht_instances(4)
      averagers = [
          DecentralizedAverager(
              tensors,
              dht=dht,
              target_group_size=4,
              min_matchmaking_time=15,
              prefix="mygroup",
              client_mode=client_mode,
              start=True,
          )
          for tensors, dht, client_mode in zip(
              [tensors1, tensors2, tensors3, tensors4],
              dht_instances,
              client_modes,
          )
      ]
  
      weights = list(
          map(float, np.random.rand(len(averagers)) * 10 + 0.01)
      )
      reference = [
          (
              tensors1[i] * weights[0]
              + tensors2[i] * weights[1]
              + tensors3[i] * weights[2]
              + tensors4[i] * weights[3]
          )
          / sum(weights)
          for i in range(len(tensors1))
      ]
  
      futures = []
      for averager, weight in zip(averagers, weights):
          futures.append(averager.step(weight=weight, wait=False))
      for future in futures:
          future.result()
  
      for future, averager in zip(futures, averagers):
          with averager.get_tensors() as averaged_tensors:
              for ref, our in zip(reference, averaged_tensors):
                  assert torch.allclose(ref, our, atol=1e-6)
  
      for process in averagers + dht_instances:
          process.shutdown()
  
imports: import random
  import time
  import numpy
  import pytest
  import torch
  import hivemind
  from hivemind.averaging import DecentralizedAverager
  from hivemind.averaging.allreduce import AveragingMode
  from hivemind.averaging.control import AveragingStage
  from hivemind.averaging.key_manager import GroupKeyManager
  from hivemind.averaging.load_balancing import load_balance_peers
  from hivemind.averaging.partition import AllreduceException
  from hivemind.p2p import PeerID
  from test_utils.dht_swarms import launch_dht_instances
label_desc: Use image node_id 4 for calling a global function with example usage: test_allreduce_weighted(n_client_mode_peers) without return types