You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_distributed.py 5.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. # -*- coding: utf-8 -*-
  2. # MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  3. #
  4. # Copyright (c) 2014-2020 Megvii Inc. All rights reserved.
  5. #
  6. # Unless required by applicable law or agreed to in writing,
  7. # software distributed under the License is distributed on an
  8. # "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. import multiprocessing as mp
  10. import platform
  11. import queue
  12. import pytest
  13. import megengine as mge
  14. import megengine.distributed as dist
  15. def _assert_q_empty(q):
  16. try:
  17. res = q.get(timeout=1)
  18. except Exception as e:
  19. assert isinstance(e, queue.Empty)
  20. else:
  21. assert False, "queue is not empty"
  22. def _assert_q_val(q, val):
  23. ret = q.get()
  24. assert ret == val
  25. @pytest.mark.skipif(
  26. platform.system() == "Darwin", reason="do not imp GPU mode at macos now"
  27. )
  28. @pytest.mark.skipif(
  29. platform.system() == "Windows", reason="do not imp GPU mode at Windows now"
  30. )
  31. @pytest.mark.isolated_distributed
  32. def test_init_process_group():
  33. world_size = 2
  34. port = dist.get_free_ports(1)[0]
  35. server = dist.Server(port)
  36. def worker(rank, backend):
  37. if mge.get_device_count("gpu") < world_size:
  38. return
  39. dist.init_process_group("localhost", port, world_size, rank, rank, backend)
  40. assert dist.is_distributed() == True
  41. assert dist.get_rank() == rank
  42. assert dist.get_world_size() == world_size
  43. assert dist.get_backend() == backend
  44. py_server_addr = dist.get_py_server_addr()
  45. assert py_server_addr[0] == "localhost"
  46. assert py_server_addr[1] == port
  47. mm_server_addr = dist.get_mm_server_addr()
  48. assert mm_server_addr[0] == "localhost"
  49. assert mm_server_addr[1] > 0
  50. assert isinstance(dist.get_client(), dist.Client)
  51. def check(backend):
  52. procs = []
  53. for rank in range(world_size):
  54. p = mp.Process(target=worker, args=(rank, backend))
  55. p.start()
  56. procs.append(p)
  57. for p in procs:
  58. p.join(20)
  59. assert p.exitcode == 0
  60. check("nccl")
  61. check("ucx")
  62. @pytest.mark.skipif(
  63. platform.system() == "Darwin", reason="do not imp GPU mode at macos now"
  64. )
  65. @pytest.mark.skipif(
  66. platform.system() == "Windows", reason="do not imp GPU mode at Windows now"
  67. )
  68. @pytest.mark.isolated_distributed
  69. def test_new_group():
  70. world_size = 3
  71. ranks = [2, 0]
  72. port = dist.get_free_ports(1)[0]
  73. server = dist.Server(port)
  74. def worker(rank):
  75. if mge.get_device_count("gpu") < world_size:
  76. return
  77. dist.init_process_group("localhost", port, world_size, rank, rank)
  78. if rank in ranks:
  79. group = dist.new_group(ranks)
  80. assert group.size == 2
  81. assert group.key == "2,0"
  82. assert group.rank == ranks.index(rank)
  83. assert group.comp_node == "gpu{}:2".format(rank)
  84. procs = []
  85. for rank in range(world_size):
  86. p = mp.Process(target=worker, args=(rank,))
  87. p.start()
  88. procs.append(p)
  89. for p in procs:
  90. p.join(20)
  91. assert p.exitcode == 0
  92. @pytest.mark.skipif(
  93. platform.system() == "Darwin", reason="do not imp GPU mode at macos now"
  94. )
  95. @pytest.mark.skipif(
  96. platform.system() == "Windows", reason="do not imp GPU mode at Windows now"
  97. )
  98. @pytest.mark.isolated_distributed
  99. def test_group_barrier():
  100. world_size = 2
  101. port = dist.get_free_ports(1)[0]
  102. server = dist.Server(port)
  103. def worker(rank, q):
  104. if mge.get_device_count("gpu") < world_size:
  105. return
  106. dist.init_process_group("localhost", port, world_size, rank, rank)
  107. dist.group_barrier()
  108. if rank == 0:
  109. dist.group_barrier()
  110. q.put(0) # to be observed in rank 1
  111. else:
  112. _assert_q_empty(q) # q.put(0) is not executed in rank 0
  113. dist.group_barrier()
  114. _assert_q_val(q, 0) # q.put(0) executed in rank 0
  115. Q = mp.Queue()
  116. procs = []
  117. for rank in range(world_size):
  118. p = mp.Process(target=worker, args=(rank, Q))
  119. p.start()
  120. procs.append(p)
  121. for p in procs:
  122. p.join(20)
  123. assert p.exitcode == 0
  124. @pytest.mark.skipif(
  125. platform.system() == "Darwin", reason="do not imp GPU mode at macos now"
  126. )
  127. @pytest.mark.skipif(
  128. platform.system() == "Windows", reason="do not imp GPU mode at Windows now"
  129. )
  130. @pytest.mark.isolated_distributed
  131. def test_synchronized():
  132. world_size = 2
  133. port = dist.get_free_ports(1)[0]
  134. server = dist.Server(port)
  135. @dist.synchronized
  136. def func(rank, q):
  137. q.put(rank)
  138. def worker(rank, q):
  139. if mge.get_device_count("gpu") < world_size:
  140. return
  141. dist.init_process_group("localhost", port, world_size, rank, rank)
  142. dist.group_barrier()
  143. if rank == 0:
  144. func(0, q) # q.put(0)
  145. q.put(2)
  146. else:
  147. _assert_q_val(q, 0) # func executed in rank 0
  148. _assert_q_empty(q) # q.put(2) is not executed
  149. func(1, q)
  150. _assert_q_val(
  151. q, 1
  152. ) # func in rank 1 executed earlier than q.put(2) in rank 0
  153. _assert_q_val(q, 2) # q.put(2) executed in rank 0
  154. Q = mp.Queue()
  155. procs = []
  156. for rank in range(world_size):
  157. p = mp.Process(target=worker, args=(rank, Q))
  158. p.start()
  159. procs.append(p)
  160. for p in procs:
  161. p.join(20)
  162. assert p.exitcode == 0

MegEngine 安装包中集成了使用 GPU 运行代码所需的 CUDA 环境,不用区分 CPU 和 GPU 版。 如果想要运行 GPU 程序,请确保机器本身配有 GPU 硬件设备并安装好驱动。 如果你想体验在云端 GPU 算力平台进行深度学习开发的感觉,欢迎访问 MegStudio 平台