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

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

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