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.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. # -*- coding: utf-8 -*-
  2. # MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  3. #
  4. # Copyright (c) 2014-2021 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.device import get_default_device
  18. from megengine.distributed.helper import param_pack_concat, param_pack_split
  19. def _assert_q_empty(q):
  20. try:
  21. res = q.get(timeout=1)
  22. except Exception as e:
  23. assert isinstance(e, queue.Empty)
  24. else:
  25. assert False, "queue is not empty"
  26. def _assert_q_val(q, val):
  27. ret = q.get()
  28. assert ret == val
  29. @pytest.mark.require_ngpu(2)
  30. @pytest.mark.parametrize("backend", ["nccl"])
  31. @pytest.mark.isolated_distributed
  32. def test_init_process_group(backend):
  33. world_size = 2
  34. server = dist.Server()
  35. port = server.py_server_port
  36. def worker(rank):
  37. dist.init_process_group("localhost", port, world_size, rank, rank, backend)
  38. assert dist.is_distributed() == True
  39. assert dist.get_rank() == rank
  40. assert dist.get_world_size() == world_size
  41. assert dist.get_backend() == backend
  42. py_server_addr = dist.get_py_server_addr()
  43. assert py_server_addr[0] == "localhost"
  44. assert py_server_addr[1] == port
  45. mm_server_addr = dist.get_mm_server_addr()
  46. assert mm_server_addr[0] == "localhost"
  47. assert mm_server_addr[1] > 0
  48. assert isinstance(dist.get_client(), dist.Client)
  49. procs = []
  50. for rank in range(world_size):
  51. p = mp.Process(target=worker, args=(rank,))
  52. p.start()
  53. procs.append(p)
  54. for p in procs:
  55. p.join(20)
  56. assert p.exitcode == 0
  57. @pytest.mark.require_ngpu(3)
  58. @pytest.mark.isolated_distributed
  59. def test_new_group():
  60. world_size = 3
  61. ranks = [2, 0]
  62. @dist.launcher
  63. def worker():
  64. rank = dist.get_rank()
  65. if rank in ranks:
  66. group = dist.new_group(ranks)
  67. assert group.size == 2
  68. assert group.key == "2,0"
  69. assert group.rank == ranks.index(rank)
  70. dt = get_default_device()[:-1]
  71. assert group.comp_node == "{}{}:2".format(dt, rank)
  72. worker()
  73. @pytest.mark.require_ngpu(2)
  74. @pytest.mark.isolated_distributed
  75. def test_group_barrier():
  76. world_size = 2
  77. server = dist.Server()
  78. port = server.py_server_port
  79. def worker(rank, q):
  80. dist.init_process_group("localhost", port, world_size, rank, rank)
  81. dist.group_barrier()
  82. if rank == 0:
  83. dist.group_barrier()
  84. q.put(0) # to be observed in rank 1
  85. else:
  86. _assert_q_empty(q) # q.put(0) is not executed in rank 0
  87. dist.group_barrier()
  88. _assert_q_val(q, 0) # q.put(0) executed in rank 0
  89. Q = mp.Queue()
  90. procs = []
  91. for rank in range(world_size):
  92. p = mp.Process(target=worker, args=(rank, Q))
  93. p.start()
  94. procs.append(p)
  95. for p in procs:
  96. p.join(20)
  97. assert p.exitcode == 0
  98. @pytest.mark.require_ngpu(2)
  99. @pytest.mark.isolated_distributed
  100. def test_synchronized():
  101. world_size = 2
  102. server = dist.Server()
  103. port = server.py_server_port
  104. @dist.synchronized
  105. def func(rank, q):
  106. q.put(rank)
  107. def worker(rank, q):
  108. dist.init_process_group("localhost", port, world_size, rank, rank)
  109. dist.group_barrier()
  110. if rank == 0:
  111. func(0, q) # q.put(0)
  112. q.put(2)
  113. else:
  114. _assert_q_val(q, 0) # func executed in rank 0
  115. _assert_q_empty(q) # q.put(2) is not executed
  116. func(1, q)
  117. _assert_q_val(
  118. q, 1
  119. ) # func in rank 1 executed earlier than q.put(2) in rank 0
  120. _assert_q_val(q, 2) # q.put(2) executed in rank 0
  121. Q = mp.Queue()
  122. procs = []
  123. for rank in range(world_size):
  124. p = mp.Process(target=worker, args=(rank, Q))
  125. p.start()
  126. procs.append(p)
  127. for p in procs:
  128. p.join(20)
  129. assert p.exitcode == 0
  130. @pytest.mark.require_ngpu(2)
  131. @pytest.mark.isolated_distributed
  132. def test_user_set_get():
  133. @dist.launcher
  134. def worker():
  135. # set in race condition
  136. dist.get_client().user_set("foo", 1)
  137. # get in race condition
  138. ret = dist.get_client().user_get("foo")
  139. assert ret == 1
  140. worker()
  141. def test_oprmm_hashable():
  142. lhs = (CollectiveComm(), ParamPackConcat(), ParamPackSplit())
  143. rhs = (CollectiveComm(), ParamPackConcat(), ParamPackSplit())
  144. assert lhs == rhs
  145. assert hash(lhs) == hash(rhs)
  146. def test_param_pack_split():
  147. a = mge.Tensor(np.ones((10,), np.int32))
  148. b, c = param_pack_split(a, [0, 1, 1, 10], [(1,), (3, 3)])
  149. assert np.allclose(b.numpy(), a.numpy()[1])
  150. assert np.allclose(c.numpy(), a.numpy()[1:].reshape(3, 3))
  151. def test_param_pack_concat():
  152. a = mge.Tensor(np.ones((1,), np.int32))
  153. b = mge.Tensor(np.ones((3, 3), np.int32))
  154. offsets_val = [0, 1, 1, 10]
  155. offsets = mge.Tensor(offsets_val, np.int32)
  156. c = param_pack_concat([a, b], offsets, offsets_val)
  157. assert np.allclose(np.concatenate([a.numpy(), b.numpy().flatten()]), c.numpy())
  158. @pytest.mark.require_ngpu(2)
  159. @pytest.mark.parametrize("early_return", [False, True], ids=["common", "early_return"])
  160. @pytest.mark.parametrize("output_size", [10, 10000], ids=["small_size", "large_size"])
  161. @pytest.mark.isolated_distributed
  162. def test_collect_results(early_return, output_size):
  163. @dist.launcher
  164. def worker():
  165. if early_return:
  166. exit(0)
  167. return [dist.get_rank()] * output_size
  168. results = worker()
  169. world_size = len(results)
  170. assert world_size > 0
  171. expects = (
  172. [None] * world_size
  173. if early_return
  174. else [[dev] * output_size for dev in range(world_size)]
  175. )
  176. assert results == expects
  177. @pytest.mark.require_ngpu(2)
  178. @pytest.mark.isolated_distributed
  179. def test_user_set_pop():
  180. @dist.launcher
  181. def worker():
  182. # set in race condition
  183. dist.get_client().user_set("foo", 1)
  184. if dist.get_rank() == 1:
  185. ret = dist.get_client().user_pop("foo")
  186. assert ret == 1
  187. worker()
  188. @pytest.mark.require_ngpu(2)
  189. @pytest.mark.isolated_distributed
  190. def test_get_cuda_compute_capability():
  191. assert mge.device.get_cuda_compute_capability(0) > 0
  192. assert mge.device.get_cuda_compute_capability(1) > 0
  193. @dist.launcher
  194. def worker():
  195. x = mge.tensor([1.0])
  196. assert mge.device.get_cuda_compute_capability(dist.get_rank()) > 0
  197. worker()