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

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

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