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_axis.py 4.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 numpy as np
  10. import pytest
  11. import megengine as mge
  12. import megengine.distributed as dist
  13. from megengine import tensor
  14. from megengine.distributed.functional import (
  15. all_gather,
  16. all_to_all,
  17. gather,
  18. reduce_scatter_sum,
  19. scatter,
  20. )
  21. from megengine.jit import trace
  22. @pytest.mark.require_ngpu(2)
  23. @pytest.mark.parametrize("shape", [(2, 3), (8, 10), (99, 77), (2, 2, 2, 2)], ids=str)
  24. @pytest.mark.parametrize("symbolic", [False, True], ids=str)
  25. @pytest.mark.parametrize("axis", [0, 1], ids=str)
  26. @pytest.mark.isolated_distributed
  27. def test_all_gather(shape, symbolic, axis):
  28. @dist.launcher(n_gpus=2)
  29. def worker(data, expect):
  30. rank = dist.get_rank()
  31. inp = tensor(data[rank])
  32. def func():
  33. output = all_gather(inp, axis=axis)
  34. return output
  35. func = trace(symbolic=symbolic)(func)
  36. output = func()
  37. assert np.allclose(output.numpy(), expect[rank])
  38. x = np.random.random_sample(shape).astype("float32")
  39. y = np.random.random_sample(shape).astype("float32")
  40. z = np.concatenate((x, y), axis=axis)
  41. data = (x, y)
  42. expect = (z, z)
  43. worker(data, expect)
  44. @pytest.mark.require_ngpu(2)
  45. @pytest.mark.parametrize(
  46. "shape,symbolic", [((2, 4, 6, 8), False), ((2, 4, 6, 8), True)], ids=str
  47. )
  48. @pytest.mark.parametrize("axis", [1, 0, 2, 3], ids=str)
  49. @pytest.mark.isolated_distributed
  50. def test_reduce_scatter_sum(shape, symbolic, axis):
  51. @dist.launcher(n_gpus=2)
  52. def worker(data, expect):
  53. rank = dist.get_rank()
  54. inp = tensor(data[rank])
  55. def func():
  56. output = reduce_scatter_sum(inp, axis=axis)
  57. return output
  58. func = trace(symbolic=symbolic)(func)
  59. output = func()
  60. assert np.allclose(output.numpy(), expect[rank])
  61. x = np.random.random_sample(shape).astype("float32")
  62. y = np.random.random_sample(shape).astype("float32")
  63. z = x + y
  64. data = (x, y)
  65. z = np.split(z, 2, axis=axis)
  66. z = np.concatenate(z, axis=0)
  67. expect = (z[: z.shape[0] // 2], z[z.shape[0] // 2 :])
  68. worker(data, expect)
  69. @pytest.mark.require_ngpu(2)
  70. @pytest.mark.parametrize(
  71. "shape,symbolic", [((2, 4, 6, 8), True), ((2, 4, 6, 8), False)], ids=str
  72. )
  73. @pytest.mark.parametrize("axis", [1, 0, 2, 3], ids=str)
  74. @pytest.mark.isolated_distributed
  75. def test_scatter(shape, symbolic, axis):
  76. @dist.launcher(n_gpus=2)
  77. def worker(data, expect):
  78. rank = dist.get_rank()
  79. inp = tensor(data[rank])
  80. def func():
  81. output = scatter(inp, axis=axis)
  82. return output
  83. func = trace(symbolic=symbolic)(func)
  84. output = func()
  85. assert np.allclose(output.numpy(), expect[rank])
  86. x = np.random.random_sample(shape).astype("float32")
  87. y = x + 1
  88. data = (x, y)
  89. _x = np.split(x, 2, axis=axis)
  90. _x = np.concatenate(_x, axis=0)
  91. expect = (_x[: _x.shape[0] // 2], _x[_x.shape[0] // 2 :])
  92. worker(data, expect)
  93. @pytest.mark.require_ngpu(2)
  94. @pytest.mark.parametrize("shape", [(2, 4, 6, 8)], ids=str)
  95. @pytest.mark.parametrize("symbolic", [False, True], ids=str)
  96. @pytest.mark.parametrize(
  97. "split_axis,concat_axis", [(0, 1), (1, 0), (2, 0), (0, 2), (2, 3)], ids=str
  98. )
  99. @pytest.mark.isolated_distributed
  100. def test_all_to_all(shape, symbolic, split_axis, concat_axis):
  101. @dist.launcher(n_gpus=2)
  102. def worker(data):
  103. rank = dist.get_rank()
  104. inp = tensor(data[rank])
  105. def func():
  106. all_to_all_output = all_to_all(
  107. inp, split_axis=split_axis, concat_axis=concat_axis
  108. )
  109. gather_C = gather(inp, axis=concat_axis)
  110. gather_B = gather(all_to_all_output, axis=split_axis)
  111. if rank == 0:
  112. return gather_B, gather_C
  113. return all_to_all_output
  114. func = trace(symbolic=symbolic)(func)
  115. ret = func()
  116. if rank == 0:
  117. assert np.allclose(ret[0], ret[1])
  118. x = np.random.random_sample(shape).astype("float32")
  119. y = np.random.random_sample(shape).astype("float32")
  120. data = (x, y)
  121. worker(data)

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