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_util.py 4.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. # MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  2. #
  3. # Copyright (c) 2014-2020 Megvii Inc. All rights reserved.
  4. #
  5. # Unless required by applicable law or agreed to in writing,
  6. # software distributed under the License is distributed on an
  7. # "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  8. import multiprocessing as mp
  9. import platform
  10. import queue
  11. from time import sleep
  12. import pytest
  13. import megengine as mge
  14. import megengine._internal as mgb
  15. import megengine.distributed as dist
  16. _LOCALHOST = "127.0.0.1"
  17. def _assert_q_empty(q):
  18. try:
  19. res = q.get(timeout=1)
  20. except Exception as e:
  21. assert isinstance(e, queue.Empty)
  22. else:
  23. assert False, "queue is not empty"
  24. def _assert_q_val(q, val):
  25. ret = q.get()
  26. assert ret == val
  27. def _init_process_group_wrapper(world_size, rank, dev, backend, q):
  28. if rank == 0:
  29. dist.init_process_group(_LOCALHOST, 0, world_size, rank, dev, backend)
  30. q.put(dist.get_master_port())
  31. else:
  32. port = q.get()
  33. dist.init_process_group(_LOCALHOST, port, world_size, rank, dev, backend)
  34. @pytest.mark.skipif(
  35. platform.system() == "Darwin", reason="do not imp GPU mode at macos now"
  36. )
  37. @pytest.mark.isolated_distributed
  38. def test_create_mm_server():
  39. def worker():
  40. if not mge.is_cuda_available():
  41. return
  42. port = mgb.config.create_mm_server("0.0.0.0", 0)
  43. assert port > 0
  44. res = mgb.config.create_mm_server("0.0.0.0", port)
  45. assert res == -1
  46. p = mp.Process(target=worker)
  47. p.start()
  48. p.join(10)
  49. assert p.exitcode == 0
  50. @pytest.mark.skipif(
  51. platform.system() == "Darwin", reason="do not imp GPU mode at macos now"
  52. )
  53. @pytest.mark.isolated_distributed
  54. def test_init_process_group():
  55. world_size = 2
  56. def worker(rank, backend, q):
  57. if not mge.is_cuda_available():
  58. return
  59. _init_process_group_wrapper(world_size, rank, rank, backend, q)
  60. assert dist.is_distributed() == True
  61. assert dist.get_master_ip() == _LOCALHOST
  62. assert dist.get_master_port() > 0
  63. assert dist.get_world_size() == world_size
  64. assert dist.get_rank() == rank
  65. assert dist.get_backend() == backend
  66. def check(backend):
  67. Q = mp.Queue()
  68. p0 = mp.Process(target=worker, args=(0, backend, Q))
  69. p1 = mp.Process(target=worker, args=(1, backend, Q))
  70. p0.start()
  71. p1.start()
  72. p0.join(10)
  73. p1.join(10)
  74. assert p0.exitcode == 0 and p1.exitcode == 0
  75. check("nccl")
  76. check("ucx")
  77. @pytest.mark.skipif(
  78. platform.system() == "Darwin", reason="do not imp GPU mode at macos now"
  79. )
  80. @pytest.mark.isolated_distributed
  81. def test_group_barrier():
  82. world_size = 2
  83. ip = "127.0.0.1"
  84. backend = "nccl"
  85. def worker(rank, q):
  86. if not mge.is_cuda_available():
  87. return
  88. _init_process_group_wrapper(world_size, rank, rank, backend, q)
  89. dist.group_barrier()
  90. if rank == 0:
  91. dist.group_barrier()
  92. q.put(0) # to be observed in rank 1
  93. else:
  94. _assert_q_empty(q) # q.put(0) is not executed in rank 0
  95. dist.group_barrier()
  96. _assert_q_val(q, 0) # q.put(0) executed in rank 0
  97. Q = mp.Queue()
  98. p0 = mp.Process(target=worker, args=(0, Q))
  99. p1 = mp.Process(target=worker, args=(1, Q))
  100. p0.start()
  101. p1.start()
  102. p0.join(10)
  103. p1.join(10)
  104. assert p0.exitcode == 0 and p1.exitcode == 0
  105. @pytest.mark.skipif(
  106. platform.system() == "Darwin", reason="do not imp GPU mode at macos now"
  107. )
  108. @pytest.mark.isolated_distributed
  109. def test_synchronized():
  110. world_size = 2
  111. backend = "nccl"
  112. @dist.synchronized
  113. def func(rank, q):
  114. q.put(rank)
  115. def worker(rank, q):
  116. if not mge.is_cuda_available():
  117. return
  118. _init_process_group_wrapper(world_size, rank, rank, backend, q)
  119. dist.group_barrier()
  120. if rank == 0:
  121. func(0, q) # q.put(0)
  122. q.put(2)
  123. else:
  124. _assert_q_val(q, 0) # func executed in rank 0
  125. _assert_q_empty(q) # q.put(2) is not executed
  126. func(1, q)
  127. _assert_q_val(
  128. q, 1
  129. ) # func in rank 1 executed earlier than q.put(2) in rank 0
  130. _assert_q_val(q, 2) # q.put(2) executed in rank 0
  131. Q = mp.Queue()
  132. p0 = mp.Process(target=worker, args=(0, Q))
  133. p1 = mp.Process(target=worker, args=(1, Q))
  134. p0.start()
  135. p1.start()
  136. p0.join(10)
  137. p1.join(10)
  138. assert p0.exitcode == 0 and p1.exitcode == 0

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