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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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 numpy as np
  12. import pytest
  13. import megengine as mge
  14. import megengine.distributed as dist
  15. from megengine import Parameter, Tensor, tensor
  16. from megengine.functional.distributed import (
  17. all_gather,
  18. all_reduce_max,
  19. all_reduce_min,
  20. all_reduce_sum,
  21. all_to_all,
  22. broadcast,
  23. gather,
  24. reduce_scatter_sum,
  25. reduce_sum,
  26. remote_recv,
  27. remote_send,
  28. scatter,
  29. )
  30. @pytest.mark.skipif(
  31. platform.system() == "Darwin", reason="do not imp GPU mode at macos now"
  32. )
  33. @pytest.mark.skipif(
  34. platform.system() == "Windows", reason="windows disable MGB_ENABLE_OPR_MM"
  35. )
  36. @pytest.mark.isolated_distributed
  37. def test_reduce_sum():
  38. world_size = 2
  39. port = dist.get_free_ports(1)[0]
  40. server = dist.Server(port)
  41. def worker(rank, data, expect, port):
  42. if mge.get_device_count("gpu") < world_size:
  43. return
  44. dist.init_process_group("localhost", port, world_size, rank, rank)
  45. inp = tensor(data)
  46. output = reduce_sum(inp)
  47. if rank == 0:
  48. assert np.allclose(output.numpy(), expect)
  49. else:
  50. assert np.allclose(output.numpy(), 0)
  51. def check(shape):
  52. x = np.random.rand(*shape).astype("float32")
  53. y = np.random.rand(*shape).astype("float32")
  54. z = x + y
  55. p0 = mp.Process(target=worker, args=(0, x, z, port))
  56. p1 = mp.Process(target=worker, args=(1, y, None, port))
  57. p0.start()
  58. p1.start()
  59. p0.join(10)
  60. p1.join(10)
  61. assert p0.exitcode == 0 and p1.exitcode == 0
  62. for shape in [(2, 3), (8, 10), (99, 77)]:
  63. check(shape)
  64. @pytest.mark.skipif(
  65. platform.system() == "Darwin", reason="do not imp GPU mode at macos now"
  66. )
  67. @pytest.mark.skipif(
  68. platform.system() == "Windows", reason="windows disable MGB_ENABLE_OPR_MM"
  69. )
  70. @pytest.mark.isolated_distributed
  71. def test_broadcast():
  72. world_size = 2
  73. port = dist.get_free_ports(1)[0]
  74. server = dist.Server(port)
  75. def worker(rank, data, expect, port):
  76. if mge.get_device_count("gpu") < world_size:
  77. return
  78. dist.init_process_group("localhost", port, world_size, rank, rank)
  79. inp = tensor(data)
  80. output = broadcast(inp)
  81. assert np.allclose(output.numpy(), expect)
  82. def check(shape):
  83. x = np.random.rand(*shape).astype("float32")
  84. y = x + 1
  85. p0 = mp.Process(target=worker, args=(0, x, x, port))
  86. p1 = mp.Process(target=worker, args=(1, y, x, port))
  87. p0.start()
  88. p1.start()
  89. p0.join(10)
  90. p1.join(10)
  91. assert p0.exitcode == 0 and p1.exitcode == 0
  92. for shape in [(2, 3), (8, 10), (99, 77)]:
  93. check(shape)
  94. @pytest.mark.skipif(
  95. platform.system() == "Darwin", reason="do not imp GPU mode at macos now"
  96. )
  97. @pytest.mark.skipif(
  98. platform.system() == "Windows", reason="windows disable MGB_ENABLE_OPR_MM"
  99. )
  100. @pytest.mark.isolated_distributed
  101. def test_all_gather():
  102. world_size = 2
  103. port = dist.get_free_ports(1)[0]
  104. server = dist.Server(port)
  105. def worker(rank, data, expect, port):
  106. if mge.get_device_count("gpu") < world_size:
  107. return
  108. dist.init_process_group("localhost", port, world_size, rank, rank)
  109. inp = tensor(data)
  110. output = all_gather(inp)
  111. assert np.allclose(output.numpy(), expect)
  112. def check(shape):
  113. x = np.random.rand(*shape).astype("float32")
  114. y = np.random.rand(*shape).astype("float32")
  115. z = np.concatenate((x, y))
  116. p0 = mp.Process(target=worker, args=(0, x, z, port))
  117. p1 = mp.Process(target=worker, args=(1, y, z, port))
  118. p0.start()
  119. p1.start()
  120. p0.join(10)
  121. p1.join(10)
  122. assert p0.exitcode == 0 and p1.exitcode == 0
  123. for shape in [(2, 3), (8, 10), (99, 77)]:
  124. check(shape)
  125. @pytest.mark.skipif(
  126. platform.system() == "Darwin", reason="do not imp GPU mode at macos now"
  127. )
  128. @pytest.mark.skipif(
  129. platform.system() == "Windows", reason="windows disable MGB_ENABLE_OPR_MM"
  130. )
  131. @pytest.mark.isolated_distributed
  132. def test_reduce_scatter_sum():
  133. world_size = 2
  134. port = dist.get_free_ports(1)[0]
  135. server = dist.Server(port)
  136. def worker(rank, data, expect, port):
  137. if mge.get_device_count("gpu") < world_size:
  138. return
  139. dist.init_process_group("localhost", port, world_size, rank, rank)
  140. inp = tensor(data)
  141. output = reduce_scatter_sum(inp)
  142. assert np.allclose(output.numpy(), expect)
  143. def check(shape):
  144. x = np.random.rand(*shape).astype("float32")
  145. y = np.random.rand(*shape).astype("float32")
  146. z = x + y
  147. p0 = mp.Process(target=worker, args=(0, x, z[: shape[0] // 2], port))
  148. p1 = mp.Process(target=worker, args=(1, y, z[shape[0] // 2 :], port))
  149. p0.start()
  150. p1.start()
  151. p0.join(10)
  152. p1.join(10)
  153. assert p0.exitcode == 0 and p1.exitcode == 0
  154. for shape in [(2, 4), (8, 10), (88, 44)]:
  155. check(shape)
  156. @pytest.mark.skipif(
  157. platform.system() == "Darwin", reason="do not imp GPU mode at macos now"
  158. )
  159. @pytest.mark.skipif(
  160. platform.system() == "Windows", reason="windows disable MGB_ENABLE_OPR_MM"
  161. )
  162. @pytest.mark.isolated_distributed
  163. def test_all_reduce_sum():
  164. world_size = 2
  165. port = dist.get_free_ports(1)[0]
  166. server = dist.Server(port)
  167. def worker(rank, data, expect, port):
  168. if mge.get_device_count("gpu") < world_size:
  169. return
  170. dist.init_process_group("localhost", port, world_size, rank, rank)
  171. inp = tensor(data)
  172. output = all_reduce_sum(inp)
  173. assert np.allclose(output.numpy(), expect)
  174. def check(shape):
  175. x = np.random.rand(*shape).astype("float32")
  176. y = np.random.rand(*shape).astype("float32")
  177. z = x + y
  178. p0 = mp.Process(target=worker, args=(0, x, z, port))
  179. p1 = mp.Process(target=worker, args=(1, y, z, port))
  180. p0.start()
  181. p1.start()
  182. p0.join(10)
  183. p1.join(10)
  184. assert p0.exitcode == 0 and p1.exitcode == 0
  185. for shape in [(2, 3), (8, 10), (99, 77)]:
  186. check(shape)
  187. @pytest.mark.skipif(
  188. platform.system() == "Darwin", reason="do not imp GPU mode at macos now"
  189. )
  190. @pytest.mark.skipif(
  191. platform.system() == "Windows", reason="windows disable MGB_ENABLE_OPR_MM"
  192. )
  193. @pytest.mark.isolated_distributed
  194. def test_all_reduce_max():
  195. world_size = 2
  196. port = dist.get_free_ports(1)[0]
  197. server = dist.Server(port)
  198. def worker(rank, data, expect, port):
  199. if mge.get_device_count("gpu") < world_size:
  200. return
  201. dist.init_process_group("localhost", port, world_size, rank, rank)
  202. inp = tensor(data)
  203. output = all_reduce_max(inp)
  204. assert np.allclose(output.numpy(), expect)
  205. def check(shape):
  206. x = np.random.rand(*shape).astype("float32")
  207. y = np.random.rand(*shape).astype("float32")
  208. z = np.maximum(x, y)
  209. p0 = mp.Process(target=worker, args=(0, x, z, port))
  210. p1 = mp.Process(target=worker, args=(1, y, z, port))
  211. p0.start()
  212. p1.start()
  213. p0.join(10)
  214. p1.join(10)
  215. assert p0.exitcode == 0 and p1.exitcode == 0
  216. for shape in [(2, 3), (8, 10), (99, 77)]:
  217. check(shape)
  218. @pytest.mark.skipif(
  219. platform.system() == "Darwin", reason="do not imp GPU mode at macos now"
  220. )
  221. @pytest.mark.skipif(
  222. platform.system() == "Windows", reason="windows disable MGB_ENABLE_OPR_MM"
  223. )
  224. @pytest.mark.isolated_distributed
  225. def test_all_reduce_min():
  226. world_size = 2
  227. port = dist.get_free_ports(1)[0]
  228. server = dist.Server(port)
  229. def worker(rank, data, expect, port):
  230. if mge.get_device_count("gpu") < world_size:
  231. return
  232. dist.init_process_group("localhost", port, world_size, rank, rank)
  233. inp = tensor(data)
  234. output = all_reduce_min(inp)
  235. assert np.allclose(output.numpy(), expect)
  236. def check(shape):
  237. x = np.random.rand(*shape).astype("float32")
  238. y = np.random.rand(*shape).astype("float32")
  239. z = np.minimum(x, y)
  240. p0 = mp.Process(target=worker, args=(0, x, z, port))
  241. p1 = mp.Process(target=worker, args=(1, y, z, port))
  242. p0.start()
  243. p1.start()
  244. p0.join(10)
  245. p1.join(10)
  246. assert p0.exitcode == 0 and p1.exitcode == 0
  247. for shape in [(2, 3), (8, 10), (99, 77)]:
  248. check(shape)
  249. @pytest.mark.skipif(
  250. platform.system() == "Darwin", reason="do not imp GPU mode at macos now"
  251. )
  252. @pytest.mark.skipif(
  253. platform.system() == "Windows", reason="windows disable MGB_ENABLE_OPR_MM"
  254. )
  255. @pytest.mark.isolated_distributed
  256. def test_gather():
  257. world_size = 2
  258. port = dist.get_free_ports(1)[0]
  259. server = dist.Server(port)
  260. def worker(rank, data, expect, port):
  261. if mge.get_device_count("gpu") < world_size:
  262. return
  263. dist.init_process_group("localhost", port, world_size, rank, rank)
  264. inp = tensor(data)
  265. output = gather(inp)
  266. if rank == 0:
  267. assert np.allclose(output.numpy(), expect)
  268. else:
  269. assert np.allclose(output.numpy(), 0)
  270. def check(shape):
  271. x = np.random.rand(*shape).astype("float32")
  272. y = np.random.rand(*shape).astype("float32")
  273. z = np.concatenate((x, y))
  274. p0 = mp.Process(target=worker, args=(0, x, z, port))
  275. p1 = mp.Process(target=worker, args=(1, y, None, port))
  276. p0.start()
  277. p1.start()
  278. p0.join(10)
  279. p1.join(10)
  280. assert p0.exitcode == 0 and p1.exitcode == 0
  281. for shape in [(2, 3), (8, 10), (99, 77)]:
  282. check(shape)
  283. @pytest.mark.skipif(
  284. platform.system() == "Darwin", reason="do not imp GPU mode at macos now"
  285. )
  286. @pytest.mark.skipif(
  287. platform.system() == "Windows", reason="windows disable MGB_ENABLE_OPR_MM"
  288. )
  289. @pytest.mark.isolated_distributed
  290. def test_scatter():
  291. world_size = 2
  292. port = dist.get_free_ports(1)[0]
  293. server = dist.Server(port)
  294. def worker(rank, data, expect, port):
  295. if mge.get_device_count("gpu") < world_size:
  296. return
  297. dist.init_process_group("localhost", port, world_size, rank, rank)
  298. inp = tensor(data)
  299. output = scatter(inp)
  300. assert np.allclose(output.numpy(), expect)
  301. def check(shape):
  302. x = np.random.rand(*shape).astype("float32")
  303. y = x + 1
  304. p0 = mp.Process(target=worker, args=(0, x, x[: shape[0] // 2], port))
  305. p1 = mp.Process(target=worker, args=(1, y, x[shape[0] // 2 :], port))
  306. p0.start()
  307. p1.start()
  308. p0.join(10)
  309. p1.join(10)
  310. assert p0.exitcode == 0 and p1.exitcode == 0
  311. for shape in [(2, 3), (8, 10), (100, 77)]:
  312. check(shape)
  313. @pytest.mark.skipif(
  314. platform.system() == "Darwin", reason="do not imp GPU mode at macos now"
  315. )
  316. @pytest.mark.skipif(
  317. platform.system() == "Windows", reason="windows disable MGB_ENABLE_OPR_MM"
  318. )
  319. @pytest.mark.isolated_distributed
  320. def test_all_to_all():
  321. world_size = 2
  322. port = dist.get_free_ports(1)[0]
  323. server = dist.Server(port)
  324. def worker(rank, data, expect, port):
  325. if mge.get_device_count("gpu") < world_size:
  326. return
  327. dist.init_process_group("localhost", port, world_size, rank, rank)
  328. inp = tensor(data)
  329. output = all_to_all(inp)
  330. assert np.allclose(output.numpy(), expect)
  331. def check(shape):
  332. x = np.random.rand(*shape).astype("float32")
  333. y = np.random.rand(*shape).astype("float32")
  334. a = np.concatenate((x[: shape[0] // 2], y[: shape[0] // 2]))
  335. b = np.concatenate((x[shape[0] // 2 :], y[shape[0] // 2 :]))
  336. p0 = mp.Process(target=worker, args=(0, x, a, port))
  337. p1 = mp.Process(target=worker, args=(1, y, b, port))
  338. p0.start()
  339. p1.start()
  340. p0.join(10)
  341. p1.join(10)
  342. assert p0.exitcode == 0 and p1.exitcode == 0
  343. for shape in [(2, 3), (8, 10), (100, 77)]:
  344. check(shape)
  345. @pytest.mark.skipif(
  346. platform.system() == "Darwin", reason="do not imp GPU mode at macos now"
  347. )
  348. @pytest.mark.skipif(
  349. platform.system() == "Windows", reason="windows disable MGB_ENABLE_OPR_MM"
  350. )
  351. @pytest.mark.isolated_distributed
  352. def test_io_remote():
  353. world_size = 2
  354. port = dist.get_free_ports(1)[0]
  355. server = dist.Server(port)
  356. val = np.random.rand(4, 5).astype(np.float32)
  357. def worker(rank):
  358. if mge.get_device_count("gpu") < world_size:
  359. return
  360. if rank == 0: # remote send
  361. dist.init_process_group("localhost", port, world_size, rank, rank)
  362. x = Tensor(val, device="gpu0")
  363. y = remote_send(x, 1)
  364. assert y.numpy()[0] == 0
  365. else: # remote recv
  366. dist.init_process_group("localhost", port, world_size, rank, rank)
  367. y = remote_recv(0, val.shape, val.dtype, cn="gpu1")
  368. np.testing.assert_almost_equal(val, y.numpy())
  369. procs = []
  370. for rank in range(world_size):
  371. p = mp.Process(target=worker, args=(rank,))
  372. p.start()
  373. procs.append(p)
  374. for p in procs:
  375. p.join(10)
  376. assert p.exitcode == 0

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