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_functional_distributed.py 7.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 platform
  10. import numpy as np
  11. import pytest
  12. import megengine as mge
  13. import megengine.distributed as dist
  14. from megengine import Parameter, tensor
  15. from megengine.core._imperative_rt.core2 import sync
  16. from megengine.device import get_default_device, set_default_device
  17. from megengine.distributed.helper import get_device_count_by_fork
  18. from megengine.functional.distributed import (
  19. all_gather,
  20. all_reduce_max,
  21. all_reduce_min,
  22. all_reduce_sum,
  23. all_to_all,
  24. broadcast,
  25. gather,
  26. reduce_scatter_sum,
  27. reduce_sum,
  28. remote_recv,
  29. remote_send,
  30. scatter,
  31. )
  32. @pytest.mark.require_ngpu(2)
  33. @pytest.mark.parametrize("shape", [(), (1,), (2, 3), (8, 10), (99, 77)], ids=str)
  34. @pytest.mark.isolated_distributed
  35. def test_reduce_sum(shape):
  36. @dist.launcher(n_gpus=2)
  37. def worker(data, expect):
  38. rank = dist.get_rank()
  39. inp = tensor(data[rank])
  40. output = reduce_sum(inp)
  41. if rank == 0:
  42. assert np.allclose(output.numpy(), expect[rank])
  43. x = np.random.random_sample(shape).astype("float32")
  44. y = np.random.random_sample(shape).astype("float32")
  45. z = x + y
  46. data = (x, y)
  47. expect = (z, None)
  48. worker(data, expect)
  49. @pytest.mark.require_ngpu(2)
  50. @pytest.mark.parametrize("shape", [(), (1,), (2, 3), (8, 10), (99, 77)], ids=str)
  51. @pytest.mark.isolated_distributed
  52. def test_broadcast(shape):
  53. @dist.launcher(n_gpus=2)
  54. def worker(data, expect):
  55. rank = dist.get_rank()
  56. inp = tensor(data[rank])
  57. output = broadcast(inp)
  58. assert np.allclose(output.numpy(), expect[rank])
  59. x = np.random.random_sample(shape).astype("float32")
  60. y = x + 1
  61. data = (x, y)
  62. expect = (x, x)
  63. worker(data, expect)
  64. @pytest.mark.require_ngpu(2)
  65. @pytest.mark.parametrize("shape", [(1,), (2, 3), (8, 10), (99, 77)], ids=str)
  66. @pytest.mark.isolated_distributed
  67. def test_all_gather(shape):
  68. @dist.launcher(n_gpus=2)
  69. def worker(data, expect):
  70. rank = dist.get_rank()
  71. inp = tensor(data[rank])
  72. output = all_gather(inp)
  73. assert np.allclose(output.numpy(), expect[rank])
  74. x = np.random.random_sample(shape).astype("float32")
  75. y = np.random.random_sample(shape).astype("float32")
  76. z = np.concatenate((x, y))
  77. data = (x, y)
  78. expect = (z, z)
  79. worker(data, expect)
  80. @pytest.mark.require_ngpu(2)
  81. @pytest.mark.parametrize("shape", [(2, 3), (8, 10), (88, 44)], ids=str)
  82. @pytest.mark.isolated_distributed
  83. def test_reduce_scatter_sum(shape):
  84. @dist.launcher(n_gpus=2)
  85. def worker(data, expect):
  86. rank = dist.get_rank()
  87. inp = tensor(data[rank])
  88. output = reduce_scatter_sum(inp)
  89. assert np.allclose(output.numpy(), expect[rank])
  90. x = np.random.random_sample(shape).astype("float32")
  91. y = np.random.random_sample(shape).astype("float32")
  92. z = x + y
  93. data = (x, y)
  94. expect = (z[: shape[0] // 2], z[shape[0] // 2 :])
  95. worker(data, expect)
  96. @pytest.mark.require_ngpu(2)
  97. @pytest.mark.parametrize("shape", [(), (1,), (2, 3), (8, 10), (99, 77)], ids=str)
  98. @pytest.mark.isolated_distributed
  99. def test_all_reduce_sum(shape):
  100. @dist.launcher(n_gpus=2)
  101. def worker(data, expect):
  102. rank = dist.get_rank()
  103. inp = tensor(data[rank])
  104. output = all_reduce_sum(inp)
  105. assert np.allclose(output.numpy(), expect[rank])
  106. x = np.random.random_sample(shape).astype("float32")
  107. y = np.random.random_sample(shape).astype("float32")
  108. z = x + y
  109. data = (x, y)
  110. expect = (z, z)
  111. worker(data, expect)
  112. @pytest.mark.require_ngpu(2)
  113. @pytest.mark.parametrize("shape", [(), (1,), (2, 3), (8, 10), (99, 77)], ids=str)
  114. @pytest.mark.isolated_distributed
  115. def test_all_reduce_max(shape):
  116. @dist.launcher(n_gpus=2)
  117. def worker(data, expect):
  118. rank = dist.get_rank()
  119. inp = tensor(data[rank])
  120. output = all_reduce_max(inp)
  121. assert np.allclose(output.numpy(), expect[rank])
  122. x = np.random.random_sample(shape).astype("float32")
  123. y = np.random.random_sample(shape).astype("float32")
  124. z = np.maximum(x, y)
  125. data = (x, y)
  126. expect = (z, z)
  127. worker(data, expect)
  128. @pytest.mark.require_ngpu(2)
  129. @pytest.mark.parametrize("shape", [(), (1,), (2, 3), (8, 10), (99, 77)], ids=str)
  130. @pytest.mark.isolated_distributed
  131. def test_all_reduce_min(shape):
  132. @dist.launcher(n_gpus=2)
  133. def worker(data, expect):
  134. rank = dist.get_rank()
  135. inp = tensor(data[rank])
  136. output = all_reduce_min(inp)
  137. assert np.allclose(output.numpy(), expect[rank])
  138. x = np.random.random_sample(shape).astype("float32")
  139. y = np.random.random_sample(shape).astype("float32")
  140. z = np.minimum(x, y)
  141. data = (x, y)
  142. expect = (z, z)
  143. worker(data, expect)
  144. @pytest.mark.require_ngpu(2)
  145. @pytest.mark.parametrize("shape", [(2, 3), (8, 10), (99, 77)], ids=str)
  146. @pytest.mark.isolated_distributed
  147. def test_gather(shape):
  148. @dist.launcher(n_gpus=2)
  149. def worker(data, expect):
  150. rank = dist.get_rank()
  151. inp = tensor(data[rank])
  152. output = gather(inp)
  153. if rank == 0:
  154. assert np.allclose(output.numpy(), expect[rank])
  155. x = np.random.random_sample(shape).astype("float32")
  156. y = np.random.random_sample(shape).astype("float32")
  157. z = np.concatenate((x, y))
  158. data = (x, y)
  159. expect = (z, None)
  160. worker(data, expect)
  161. @pytest.mark.require_ngpu(2)
  162. @pytest.mark.parametrize("shape", [(2, 3), (8, 10), (100, 77)], ids=str)
  163. @pytest.mark.isolated_distributed
  164. def test_scatter(shape):
  165. @dist.launcher(n_gpus=2)
  166. def worker(data, expect):
  167. rank = dist.get_rank()
  168. inp = tensor(data[rank])
  169. output = scatter(inp)
  170. assert np.allclose(output.numpy(), expect[rank])
  171. x = np.random.random_sample(shape).astype("float32")
  172. y = x + 1
  173. data = (x, y)
  174. expect = (x[: shape[0] // 2], x[shape[0] // 2 :])
  175. worker(data, expect)
  176. @pytest.mark.require_ngpu(2)
  177. @pytest.mark.parametrize("shape", [(2, 3), (8, 10), (100, 77)], ids=str)
  178. @pytest.mark.isolated_distributed
  179. def test_all_to_all(shape):
  180. @dist.launcher(n_gpus=2)
  181. def worker(data, expect):
  182. rank = dist.get_rank()
  183. inp = tensor(data[rank])
  184. output = all_to_all(inp)
  185. assert np.allclose(output.numpy(), expect[rank])
  186. x = np.random.random_sample(shape).astype("float32")
  187. y = np.random.random_sample(shape).astype("float32")
  188. a = np.concatenate((x[: shape[0] // 2], y[: shape[0] // 2]))
  189. b = np.concatenate((x[shape[0] // 2 :], y[shape[0] // 2 :]))
  190. data = (x, y)
  191. expect = (a, b)
  192. worker(data, expect)
  193. @pytest.mark.require_ngpu(2)
  194. @pytest.mark.isolated_distributed
  195. @pytest.mark.parametrize("shape", [(), (1,), (4, 5)], ids=str)
  196. def test_io_remote(shape):
  197. @dist.launcher(n_gpus=2)
  198. def worker(val, shape):
  199. rank = dist.get_rank()
  200. if rank == 0: # remote send
  201. x = tensor(val, device="xpu0")
  202. remote_send(x, 1)
  203. sync()
  204. else: # remote recv
  205. y = remote_recv(0, shape, np.float32)
  206. assert y.device == get_default_device()
  207. np.testing.assert_almost_equal(val, y.numpy())
  208. val = np.random.random_sample(shape).astype("float32")
  209. worker(val, shape)

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