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

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

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