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 6.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 numpy as np
  13. import pytest
  14. import megengine as mge
  15. import megengine.distributed as dist
  16. from megengine.core.ops.builtin import CollectiveComm, ParamPackConcat, ParamPackSplit
  17. from megengine.distributed.helper import (
  18. get_device_count_by_fork,
  19. param_pack_concat,
  20. param_pack_split,
  21. )
  22. def _assert_q_empty(q):
  23. try:
  24. res = q.get(timeout=1)
  25. except Exception as e:
  26. assert isinstance(e, queue.Empty)
  27. else:
  28. assert False, "queue is not empty"
  29. def _assert_q_val(q, val):
  30. ret = q.get()
  31. assert ret == val
  32. @pytest.mark.skipif(
  33. platform.system() == "Darwin", reason="do not imp GPU mode at macos now"
  34. )
  35. @pytest.mark.skipif(
  36. platform.system() == "Windows", reason="do not imp GPU mode at Windows now"
  37. )
  38. @pytest.mark.skipif(get_device_count_by_fork("gpu") < 2, reason="need more gpu device")
  39. @pytest.mark.isolated_distributed
  40. def test_init_process_group():
  41. world_size = 2
  42. port = dist.get_free_ports(1)[0]
  43. server = dist.Server(port)
  44. def worker(rank, backend):
  45. dist.init_process_group("localhost", port, world_size, rank, rank, backend)
  46. assert dist.is_distributed() == True
  47. assert dist.get_rank() == rank
  48. assert dist.get_world_size() == world_size
  49. assert dist.get_backend() == backend
  50. py_server_addr = dist.get_py_server_addr()
  51. assert py_server_addr[0] == "localhost"
  52. assert py_server_addr[1] == port
  53. mm_server_addr = dist.get_mm_server_addr()
  54. assert mm_server_addr[0] == "localhost"
  55. assert mm_server_addr[1] > 0
  56. assert isinstance(dist.get_client(), dist.Client)
  57. def check(backend):
  58. procs = []
  59. for rank in range(world_size):
  60. p = mp.Process(target=worker, args=(rank, backend))
  61. p.start()
  62. procs.append(p)
  63. for p in procs:
  64. p.join(20)
  65. assert p.exitcode == 0
  66. check("nccl")
  67. @pytest.mark.skipif(
  68. platform.system() == "Darwin", reason="do not imp GPU mode at macos now"
  69. )
  70. @pytest.mark.skipif(
  71. platform.system() == "Windows", reason="do not imp GPU mode at Windows now"
  72. )
  73. @pytest.mark.skipif(get_device_count_by_fork("gpu") < 2, reason="need more gpu device")
  74. @pytest.mark.isolated_distributed
  75. def test_new_group():
  76. world_size = 3
  77. ranks = [2, 0]
  78. port = dist.get_free_ports(1)[0]
  79. server = dist.Server(port)
  80. def worker(rank):
  81. dist.init_process_group("localhost", port, world_size, rank, rank)
  82. if rank in ranks:
  83. group = dist.new_group(ranks)
  84. assert group.size == 2
  85. assert group.key == "2,0"
  86. assert group.rank == ranks.index(rank)
  87. assert group.comp_node == "gpu{}:2".format(rank)
  88. procs = []
  89. for rank in range(world_size):
  90. p = mp.Process(target=worker, args=(rank,))
  91. p.start()
  92. procs.append(p)
  93. for p in procs:
  94. p.join(20)
  95. assert p.exitcode == 0
  96. @pytest.mark.skipif(
  97. platform.system() == "Darwin", reason="do not imp GPU mode at macos now"
  98. )
  99. @pytest.mark.skipif(
  100. platform.system() == "Windows", reason="do not imp GPU mode at Windows now"
  101. )
  102. @pytest.mark.skipif(get_device_count_by_fork("gpu") < 2, reason="need more gpu device")
  103. @pytest.mark.isolated_distributed
  104. def test_group_barrier():
  105. world_size = 2
  106. port = dist.get_free_ports(1)[0]
  107. server = dist.Server(port)
  108. def worker(rank, q):
  109. dist.init_process_group("localhost", port, world_size, rank, rank)
  110. dist.group_barrier()
  111. if rank == 0:
  112. dist.group_barrier()
  113. q.put(0) # to be observed in rank 1
  114. else:
  115. _assert_q_empty(q) # q.put(0) is not executed in rank 0
  116. dist.group_barrier()
  117. _assert_q_val(q, 0) # q.put(0) executed in rank 0
  118. Q = mp.Queue()
  119. procs = []
  120. for rank in range(world_size):
  121. p = mp.Process(target=worker, args=(rank, Q))
  122. p.start()
  123. procs.append(p)
  124. for p in procs:
  125. p.join(20)
  126. assert p.exitcode == 0
  127. @pytest.mark.skipif(
  128. platform.system() == "Darwin", reason="do not imp GPU mode at macos now"
  129. )
  130. @pytest.mark.skipif(
  131. platform.system() == "Windows", reason="do not imp GPU mode at Windows now"
  132. )
  133. @pytest.mark.skipif(get_device_count_by_fork("gpu") < 2, reason="need more gpu device")
  134. @pytest.mark.isolated_distributed
  135. def test_synchronized():
  136. world_size = 2
  137. port = dist.get_free_ports(1)[0]
  138. server = dist.Server(port)
  139. @dist.synchronized
  140. def func(rank, q):
  141. q.put(rank)
  142. def worker(rank, q):
  143. dist.init_process_group("localhost", port, world_size, rank, rank)
  144. dist.group_barrier()
  145. if rank == 0:
  146. func(0, q) # q.put(0)
  147. q.put(2)
  148. else:
  149. _assert_q_val(q, 0) # func executed in rank 0
  150. _assert_q_empty(q) # q.put(2) is not executed
  151. func(1, q)
  152. _assert_q_val(
  153. q, 1
  154. ) # func in rank 1 executed earlier than q.put(2) in rank 0
  155. _assert_q_val(q, 2) # q.put(2) executed in rank 0
  156. Q = mp.Queue()
  157. procs = []
  158. for rank in range(world_size):
  159. p = mp.Process(target=worker, args=(rank, Q))
  160. p.start()
  161. procs.append(p)
  162. for p in procs:
  163. p.join(20)
  164. assert p.exitcode == 0
  165. def test_oprmm_hashable():
  166. lhs = (CollectiveComm(), ParamPackConcat(), ParamPackSplit())
  167. rhs = (CollectiveComm(), ParamPackConcat(), ParamPackSplit())
  168. assert lhs == rhs
  169. assert hash(lhs) == hash(rhs)
  170. def test_param_pack_split():
  171. a = mge.Tensor(np.ones((10,), np.int32))
  172. b, c = param_pack_split(a, [0, 1, 1, 10], [(1,), (3, 3)])
  173. assert np.allclose(b.numpy(), a.numpy()[1])
  174. assert np.allclose(c.numpy(), a.numpy()[1:].reshape(3, 3))
  175. def test_param_pack_concat():
  176. a = mge.Tensor(np.ones((1,), np.int32))
  177. b = mge.Tensor(np.ones((3, 3), np.int32))
  178. offsets_val = [0, 1, 1, 10]
  179. offsets = mge.Tensor(offsets_val, np.int32)
  180. c = param_pack_concat([a, b], offsets, offsets_val)
  181. assert np.allclose(np.concatenate([a.numpy(), b.numpy().flatten()]), c.numpy())

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