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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. train_net.set_train()
  61. _executor.compile(train_net, _x, _b, auto_parallel_mode=True)
  62. context.reset_auto_parallel_context()
  63. def test_normal_split():
  64. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=2, global_rank=0)
  65. strategy1 = ((2, 1), (1, 2))
  66. strategy2 = ((1, 2, 1), (1, 2, 1))
  67. strategy3 = ((1, 2), (2, 1))
  68. net = Net(strategy1, strategy2, strategy3)
  69. compile_net(net)
  70. def test_normal_split2():
  71. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=4, global_rank=0)
  72. strategy1 = ((4, 1), (1, 4))
  73. strategy2 = ((1, 4, 1), (1, 4, 1))
  74. strategy3 = ((1, 4), (4, 1))
  75. net = Net(strategy1, strategy2, strategy3, split_tuple=(10, 20, 30, 4), param_shape=(64, 8))
  76. compile_net(net)
  77. def test_normal_split3():
  78. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=32, global_rank=17)
  79. strategy1 = ((4, 8), (1, 4))
  80. strategy2 = ((1, 4, 8), (1, 4, 8))
  81. strategy3 = ((1, 32), (32, 1))
  82. net = Net(strategy1, strategy2, strategy3, split_tuple=(10, 20, 30, 4), param_shape=(64, 8))
  83. compile_net(net)
  84. def test_normal_split_with_offset():
  85. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=2, global_rank=0)
  86. strategy1 = ((2, 1), (1, 2))
  87. strategy2 = ((1, 2, 1), (1, 2, 1))
  88. strategy3 = ((1, 2), (2, 1))
  89. net = Net(strategy1, strategy2, strategy3, split_string="manual_split_with_offset", split_tuple=((4, 0), (4, 4)))
  90. compile_net(net)
  91. def test_auto_parallel_error():
  92. context.set_context(save_graphs=True)
  93. context.set_auto_parallel_context(parallel_mode="auto_parallel", device_num=2, global_rank=0)
  94. net = Net()
  95. with pytest.raises(RuntimeError):
  96. compile_net(net)
  97. def test_axis_error():
  98. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=2, global_rank=0)
  99. strategy1 = ((2, 1), (1, 2))
  100. strategy2 = ((1, 2, 1), (1, 2, 1))
  101. strategy3 = ((1, 2), (2, 1))
  102. net = Net(strategy1, strategy2, strategy3, axis=1)
  103. with pytest.raises(RuntimeError):
  104. compile_net(net)
  105. def test_strategy_error():
  106. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  107. strategy1 = ((4, 1), (8, 1))
  108. strategy2 = ((1, 2, 1), (1, 2, 1))
  109. strategy3 = ((1, 2), (2, 1))
  110. net = Net(strategy1, strategy2, strategy3)
  111. with pytest.raises(RuntimeError):
  112. compile_net(net)
  113. def test_strategy_error2():
  114. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  115. strategy1 = ((4, 1), (1, 8))
  116. strategy2 = ((1, 2, 1), (1, 2, 1))
  117. strategy3 = ((1, 2), (2, 1))
  118. net = Net(strategy1, strategy2, strategy3)
  119. with pytest.raises(RuntimeError):
  120. compile_net(net)
  121. def test_strategy_error3():
  122. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  123. strategy1 = ((2, 1), (1, 2))
  124. strategy2 = ((1, 2, 1), (1, 2, 1))
  125. strategy3 = ((1, 2), (2, 1))
  126. net = Net(strategy1, strategy2, strategy3)
  127. with pytest.raises(RuntimeError):
  128. compile_net(net)
  129. def test_strategy_error4():
  130. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=2, global_rank=0)
  131. strategy1 = ((2, 8), (1, 2))
  132. strategy2 = ((1, 2, 1), (1, 2, 1))
  133. strategy3 = ((1, 2), (2, 1))
  134. net = Net(strategy1, strategy2, strategy3)
  135. with pytest.raises(RuntimeError):
  136. compile_net(net)
  137. def test_strategy_error5():
  138. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=4, global_rank=0)
  139. strategy1 = ((4, 1), (1, 4))
  140. strategy2 = ((1, 2, 1), (1, 2, 1))
  141. strategy3 = ((1, 2), (2, 1))
  142. net = Net(strategy1, strategy2, strategy3)
  143. with pytest.raises(RuntimeError):
  144. compile_net(net)
  145. def test_split_tuple_error():
  146. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=2, global_rank=0)
  147. strategy1 = ((2, 1), (1, 2))
  148. strategy2 = ((1, 2, 1), (1, 2, 1))
  149. strategy3 = ((1, 2), (2, 1))
  150. net = Net(strategy1, strategy2, strategy3, split_tuple=((5, 0), (5, 5)))
  151. with pytest.raises(RuntimeError):
  152. compile_net(net)
  153. def test_parameter_use_tensor_error():
  154. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=2, global_rank=0)
  155. strategy1 = ((2, 1), (1, 2))
  156. strategy2 = ((1, 2, 1), (1, 2, 1))
  157. strategy3 = ((1, 2), (2, 1))
  158. net = Net(strategy1, strategy2, strategy3, init_flag=False)
  159. with pytest.raises(RuntimeError):
  160. compile_net(net)