benchmark_averaging.py 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import time
  2. import threading
  3. import argparse
  4. import torch
  5. import hivemind
  6. from hivemind.utils import LOCALHOST, increase_file_limit
  7. from hivemind.proto import runtime_pb2
  8. def sample_tensors(hid_size, num_layers):
  9. tensors = []
  10. for i in range(num_layers):
  11. tensors.append(torch.randn(hid_size, 3 * hid_size))
  12. tensors.append(torch.randn(3 * hid_size))
  13. tensors.append(torch.randn(3 * hid_size))
  14. tensors.append(torch.randn(hid_size, hid_size))
  15. tensors.append(torch.ones(hid_size))
  16. tensors.append(torch.zeros(hid_size))
  17. tensors.append(torch.randn(hid_size, 4 * hid_size))
  18. tensors.append(torch.randn(4 * hid_size))
  19. tensors.append(torch.ones(4 * hid_size))
  20. tensors.append(torch.randn(2, hid_size, hid_size, 2))
  21. tensors.append(torch.randn(hid_size))
  22. tensors.append(torch.randn(hid_size))
  23. tensors.append(torch.randn(hid_size))
  24. return tuple(tensors)
  25. def benchmark_averaging(num_peers: int, target_group_size: int, num_rounds: int,
  26. averaging_expiration: float, request_timeout: float, round_timeout: float,
  27. hid_size: int, num_layers: int, spawn_dtime: float):
  28. dht_root = hivemind.DHT(listen_on=f'{LOCALHOST}:*', start=True)
  29. peer_tensors = [sample_tensors(hid_size, num_layers)
  30. for _ in range(num_peers)]
  31. processes = {dht_root}
  32. def run_averager(index):
  33. dht = hivemind.DHT(listen_on=f'{LOCALHOST}:*',
  34. initial_peers=[f"{LOCALHOST}:{dht_root.port}"],
  35. start=True)
  36. averager = hivemind.DecentralizedAverager(
  37. peer_tensors[i], dht, prefix='my_tensor', initial_group_bits='0110', listen_on=f"{LOCALHOST}:*",
  38. compression_type=runtime_pb2.CompressionType.FLOAT16, target_group_size=target_group_size,
  39. averaging_expiration=averaging_expiration, request_timeout=request_timeout, start=True)
  40. processes.update({dht, averager})
  41. print(end=f'<started {index}>\n', flush=True)
  42. for _ in range(num_rounds):
  43. success = averager.step(timeout=round_timeout)
  44. print(end=('+' if success else '-'), flush=True)
  45. print(end=f'<finished {index}>\n', flush=True)
  46. threads = []
  47. for i in range(num_peers):
  48. thread = threading.Thread(target=run_averager, args=[i])
  49. threads.append(thread)
  50. thread.start()
  51. time.sleep(spawn_dtime)
  52. t = time.time()
  53. for thread in threads:
  54. thread.join()
  55. print(f"\ntest run took {time.time() - t:.3f} seconds")
  56. for process in processes:
  57. process.terminate()
  58. if __name__ == "__main__":
  59. parser = argparse.ArgumentParser()
  60. parser.add_argument('--num_peers', type=int, default=16, required=False)
  61. parser.add_argument('--target_group_size', type=int, default=4, required=False)
  62. parser.add_argument('--num_rounds', type=int, default=5, required=False)
  63. parser.add_argument('--hid_size', type=int, default=256, required=False)
  64. parser.add_argument('--num_layers', type=int, default=3, required=False)
  65. parser.add_argument('--averaging_expiration', type=float, default=15, required=False)
  66. parser.add_argument('--round_timeout', type=float, default=30, required=False)
  67. parser.add_argument('--request_timeout', type=float, default=3, required=False)
  68. parser.add_argument('--spawn_dtime', type=float, default=0.1, required=False)
  69. parser.add_argument('--increase_file_limit', action="store_true")
  70. args = vars(parser.parse_args())
  71. if args.pop('increase_file_limit', False):
  72. increase_file_limit()
  73. benchmark_averaging(**args)