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_embedding_lookup.py 7.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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, LazyAdam
  21. from mindspore.ops import operations as P
  22. from mindspore.common.initializer import initializer
  23. context.set_context(enable_sparse=True)
  24. class Net(Cell):
  25. def __init__(self,
  26. strategy1=None,
  27. strategy2=None,
  28. strategy3=None,
  29. axis=0,
  30. init_flag=True,
  31. split_tuple=(4, 4),
  32. split_string="manual_split",
  33. param_shape=(8, 8)):
  34. super().__init__()
  35. self.gatherv2 = P.EmbeddingLookup().shard(strategy1)
  36. self.gatherv2.add_prim_attr(split_string, split_tuple)
  37. self.gatherv2.add_prim_attr("primitive_target", "CPU")
  38. self.mul = P.Mul().shard(strategy2)
  39. self.reshape = P.Reshape()
  40. self.matmul = P.MatMul().shard(strategy3)
  41. self.matmul.add_prim_attr("forward_reduce_scatter", True)
  42. if init_flag:
  43. self.param = Parameter(initializer("ones", param_shape, ms.float32), name="gatherv2_param")
  44. else:
  45. self.param = Parameter(Tensor(np.ones(param_shape), dtype=ms.float32), name="gatherv2_param")
  46. self.mul_weight = Parameter(initializer("ones", (8, 8, 8), ms.float32), name="mul_weight")
  47. self.matmul_weight = Parameter(initializer("ones", (64, 16), ms.float32), name="matmul_weight")
  48. self.axis = axis
  49. def construct(self, x, b):
  50. out = self.gatherv2(self.param, x, self.axis)
  51. out = self.mul(out, b)
  52. return out
  53. _x = Tensor(np.ones([8, 8]), dtype=ms.int32)
  54. _b = Tensor(np.ones([8, 8, 8]), dtype=ms.float32)
  55. def compile_net(net):
  56. context.set_context(save_graphs=True)
  57. optimizer = LazyAdam(net.trainable_params(), learning_rate=0.1)
  58. optimizer.sparse_opt.add_prim_attr("primitive_target", "CPU")
  59. train_net = TrainOneStepCell(net, optimizer)
  60. train_net.set_auto_parallel()
  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_auto_parallel():
  98. context.set_context(save_graphs=True)
  99. context.set_auto_parallel_context(parallel_mode="auto_parallel", device_num=2, global_rank=0)
  100. net = Net(split_string="fake")
  101. compile_net(net)
  102. def test_axis_error():
  103. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=2, global_rank=0)
  104. strategy1 = ((2, 1), (1, 2))
  105. strategy2 = ((1, 2, 1), (1, 2, 1))
  106. strategy3 = ((1, 2), (2, 1))
  107. net = Net(strategy1, strategy2, strategy3, axis=1)
  108. with pytest.raises(RuntimeError):
  109. compile_net(net)
  110. def test_strategy_error():
  111. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  112. strategy1 = ((4, 1), (8, 1))
  113. strategy2 = ((1, 2, 1), (1, 2, 1))
  114. strategy3 = ((1, 2), (2, 1))
  115. net = Net(strategy1, strategy2, strategy3)
  116. with pytest.raises(RuntimeError):
  117. compile_net(net)
  118. def test_strategy_error2():
  119. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  120. strategy1 = ((4, 1), (1, 8))
  121. strategy2 = ((1, 2, 1), (1, 2, 1))
  122. strategy3 = ((1, 2), (2, 1))
  123. net = Net(strategy1, strategy2, strategy3)
  124. with pytest.raises(RuntimeError):
  125. compile_net(net)
  126. def test_strategy_error3():
  127. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  128. strategy1 = ((2, 1), (1, 2))
  129. strategy2 = ((1, 2, 1), (1, 2, 1))
  130. strategy3 = ((1, 2), (2, 1))
  131. net = Net(strategy1, strategy2, strategy3)
  132. with pytest.raises(RuntimeError):
  133. compile_net(net)
  134. def test_strategy_error4():
  135. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=2, global_rank=0)
  136. strategy1 = ((2, 8), (1, 2))
  137. strategy2 = ((1, 2, 1), (1, 2, 1))
  138. strategy3 = ((1, 2), (2, 1))
  139. net = Net(strategy1, strategy2, strategy3)
  140. with pytest.raises(RuntimeError):
  141. compile_net(net)
  142. def test_strategy_error5():
  143. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=4, global_rank=0)
  144. strategy1 = ((4, 1), (1, 4))
  145. strategy2 = ((1, 2, 1), (1, 2, 1))
  146. strategy3 = ((1, 2), (2, 1))
  147. net = Net(strategy1, strategy2, strategy3)
  148. with pytest.raises(RuntimeError):
  149. compile_net(net)
  150. def test_split_tuple_error():
  151. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=2, global_rank=0)
  152. strategy1 = ((2, 1), (1, 2))
  153. strategy2 = ((1, 2, 1), (1, 2, 1))
  154. strategy3 = ((1, 2), (2, 1))
  155. net = Net(strategy1, strategy2, strategy3, split_tuple=((5, 0), (5, 5)))
  156. with pytest.raises(RuntimeError):
  157. compile_net(net)
  158. def test_parameter_use_tensor_error():
  159. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=2, global_rank=0)
  160. strategy1 = ((2, 1), (1, 2))
  161. strategy2 = ((1, 2, 1), (1, 2, 1))
  162. strategy3 = ((1, 2), (2, 1))
  163. net = Net(strategy1, strategy2, strategy3, init_flag=False)
  164. with pytest.raises(RuntimeError):
  165. compile_net(net)