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_manual_gatherv2.py 7.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. # Copyright 2020 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ============================================================================
  15. import numpy as np
  16. import pytest
  17. import mindspore as ms
  18. from mindspore import context, Tensor, Parameter
  19. from mindspore.common.api import _executor
  20. from mindspore.nn import Cell, TrainOneStepCell, Momentum
  21. from mindspore.ops import operations as P
  22. from mindspore.common.initializer import initializer
  23. class Net(Cell):
  24. def __init__(self,
  25. strategy1=None,
  26. strategy2=None,
  27. strategy3=None,
  28. axis=0,
  29. init_flag=True,
  30. split_tuple=(4, 4),
  31. split_string="manual_split",
  32. param_shape=(8, 8)):
  33. super().__init__()
  34. self.gatherv2 = P.GatherV2().shard(strategy1)
  35. self.gatherv2.add_prim_attr(split_string, split_tuple)
  36. self.mul = P.Mul().shard(strategy2)
  37. self.reshape = P.Reshape()
  38. self.matmul = P.MatMul().shard(strategy3)
  39. self.matmul.add_prim_attr("forward_reduce_scatter", True)
  40. if init_flag:
  41. self.param = Parameter(initializer("ones", param_shape, ms.float32), name="gatherv2_param")
  42. else:
  43. self.param = Parameter(Tensor(np.ones(param_shape), dtype=ms.float32), name="gatherv2_param")
  44. self.mul_weight = Parameter(initializer("ones", (8, 8, 8), ms.float32), name="mul_weight")
  45. self.matmul_weight = Parameter(initializer("ones", (64, 16), ms.float32), name="matmul_weight")
  46. self.axis = axis
  47. def construct(self, x, b):
  48. out = self.gatherv2(self.param, x, self.axis)
  49. out = self.mul(out, self.mul_weight)
  50. out = self.reshape(out, (8, 64))
  51. out = self.matmul(out, self.matmul_weight)
  52. return out
  53. _x = Tensor(np.ones([8, 8]), dtype=ms.int32)
  54. _b = Tensor(np.ones([64, 8]), dtype=ms.float32)
  55. def compile_net(net):
  56. context.set_context(save_graphs=True)
  57. optimizer = Momentum(net.trainable_params(), learning_rate=0.1, momentum=0.9)
  58. train_net = TrainOneStepCell(net, optimizer)
  59. train_net.set_auto_parallel()
  60. _executor.compile(train_net, _x, _b, auto_parallel_mode=True)
  61. context.reset_auto_parallel_context()
  62. def test_normal_split():
  63. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=2, global_rank=0)
  64. strategy1 = ((2, 1), (1, 2))
  65. strategy2 = ((1, 2, 1), (1, 2, 1))
  66. strategy3 = ((1, 2), (2, 1))
  67. net = Net(strategy1, strategy2, strategy3)
  68. compile_net(net)
  69. def test_normal_split2():
  70. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=4, global_rank=0)
  71. strategy1 = ((4, 1), (1, 4))
  72. strategy2 = ((1, 4, 1), (1, 4, 1))
  73. strategy3 = ((1, 4), (4, 1))
  74. net = Net(strategy1, strategy2, strategy3, split_tuple=(10, 20, 30, 4), param_shape=(64, 8))
  75. compile_net(net)
  76. def test_normal_split3():
  77. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=32, global_rank=17)
  78. strategy1 = ((4, 8), (1, 4))
  79. strategy2 = ((1, 4, 8), (1, 4, 8))
  80. strategy3 = ((1, 32), (32, 1))
  81. net = Net(strategy1, strategy2, strategy3, split_tuple=(10, 20, 30, 4), param_shape=(64, 8))
  82. compile_net(net)
  83. def test_normal_split_with_offset():
  84. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=2, global_rank=0)
  85. strategy1 = ((2, 1), (1, 2))
  86. strategy2 = ((1, 2, 1), (1, 2, 1))
  87. strategy3 = ((1, 2), (2, 1))
  88. net = Net(strategy1, strategy2, strategy3, split_string="manual_split_with_offset", split_tuple=((4, 0), (4, 4)))
  89. compile_net(net)
  90. def test_auto_parallel_error():
  91. context.set_context(save_graphs=True)
  92. context.set_auto_parallel_context(parallel_mode="auto_parallel", device_num=2, global_rank=0)
  93. net = Net()
  94. with pytest.raises(RuntimeError):
  95. compile_net(net)
  96. def test_axis_error():
  97. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=2, global_rank=0)
  98. strategy1 = ((2, 1), (1, 2))
  99. strategy2 = ((1, 2, 1), (1, 2, 1))
  100. strategy3 = ((1, 2), (2, 1))
  101. net = Net(strategy1, strategy2, strategy3, axis=1)
  102. with pytest.raises(RuntimeError):
  103. compile_net(net)
  104. def test_strategy_error():
  105. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  106. strategy1 = ((4, 1), (8, 1))
  107. strategy2 = ((1, 2, 1), (1, 2, 1))
  108. strategy3 = ((1, 2), (2, 1))
  109. net = Net(strategy1, strategy2, strategy3)
  110. with pytest.raises(RuntimeError):
  111. compile_net(net)
  112. def test_strategy_error2():
  113. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  114. strategy1 = ((4, 1), (1, 8))
  115. strategy2 = ((1, 2, 1), (1, 2, 1))
  116. strategy3 = ((1, 2), (2, 1))
  117. net = Net(strategy1, strategy2, strategy3)
  118. with pytest.raises(RuntimeError):
  119. compile_net(net)
  120. def test_strategy_error3():
  121. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  122. strategy1 = ((2, 1), (1, 2))
  123. strategy2 = ((1, 2, 1), (1, 2, 1))
  124. strategy3 = ((1, 2), (2, 1))
  125. net = Net(strategy1, strategy2, strategy3)
  126. with pytest.raises(RuntimeError):
  127. compile_net(net)
  128. def test_strategy_error4():
  129. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=2, global_rank=0)
  130. strategy1 = ((2, 8), (1, 2))
  131. strategy2 = ((1, 2, 1), (1, 2, 1))
  132. strategy3 = ((1, 2), (2, 1))
  133. net = Net(strategy1, strategy2, strategy3)
  134. with pytest.raises(RuntimeError):
  135. compile_net(net)
  136. def test_strategy_error5():
  137. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=4, global_rank=0)
  138. strategy1 = ((4, 1), (1, 4))
  139. strategy2 = ((1, 2, 1), (1, 2, 1))
  140. strategy3 = ((1, 2), (2, 1))
  141. net = Net(strategy1, strategy2, strategy3)
  142. with pytest.raises(RuntimeError):
  143. compile_net(net)
  144. def test_split_tuple_error():
  145. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=2, global_rank=0)
  146. strategy1 = ((2, 1), (1, 2))
  147. strategy2 = ((1, 2, 1), (1, 2, 1))
  148. strategy3 = ((1, 2), (2, 1))
  149. net = Net(strategy1, strategy2, strategy3, split_tuple=((5, 0), (5, 5)))
  150. with pytest.raises(RuntimeError):
  151. compile_net(net)
  152. def test_parameter_use_tensor_error():
  153. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=2, global_rank=0)
  154. strategy1 = ((2, 1), (1, 2))
  155. strategy2 = ((1, 2, 1), (1, 2, 1))
  156. strategy3 = ((1, 2), (2, 1))
  157. net = Net(strategy1, strategy2, strategy3, init_flag=False)
  158. with pytest.raises(RuntimeError):
  159. compile_net(net)