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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. @pytest.fixture(scope="module", autouse=True)
  24. def setup_teardown():
  25. context.set_context(enable_sparse=True)
  26. yield
  27. context.set_context(enable_sparse=False)
  28. class Net(Cell):
  29. def __init__(self,
  30. strategy1=None,
  31. strategy2=None,
  32. strategy3=None,
  33. axis=0,
  34. init_flag=True,
  35. split_tuple=(4, 4),
  36. split_string="manual_split",
  37. param_shape=(8, 8)):
  38. super().__init__()
  39. self.gatherv2 = P.EmbeddingLookup().shard(strategy1)
  40. self.gatherv2.add_prim_attr(split_string, split_tuple)
  41. self.gatherv2.add_prim_attr("primitive_target", "CPU")
  42. self.mul = P.Mul().shard(strategy2)
  43. self.reshape = P.Reshape()
  44. self.matmul = P.MatMul().shard(strategy3)
  45. self.matmul.add_prim_attr("forward_reduce_scatter", True)
  46. if init_flag:
  47. self.param = Parameter(initializer("ones", param_shape, ms.float32), name="gatherv2_param")
  48. else:
  49. self.param = Parameter(Tensor(np.ones(param_shape), dtype=ms.float32), name="gatherv2_param")
  50. self.mul_weight = Parameter(initializer("ones", (8, 8, 8), ms.float32), name="mul_weight")
  51. self.matmul_weight = Parameter(initializer("ones", (64, 16), ms.float32), name="matmul_weight")
  52. self.axis = axis
  53. def construct(self, x, b):
  54. out = self.gatherv2(self.param, x, self.axis)
  55. out = self.mul(out, b)
  56. return out
  57. _x = Tensor(np.ones([8, 8]), dtype=ms.int32)
  58. _b = Tensor(np.ones([8, 8, 8]), dtype=ms.float32)
  59. def compile_net(net):
  60. context.set_context(save_graphs=False)
  61. optimizer = LazyAdam(net.trainable_params(), learning_rate=0.1)
  62. optimizer.sparse_opt.add_prim_attr("primitive_target", "CPU")
  63. train_net = TrainOneStepCell(net, optimizer)
  64. train_net.set_auto_parallel()
  65. train_net.set_train()
  66. _executor.compile(train_net, _x, _b, auto_parallel_mode=True)
  67. context.reset_auto_parallel_context()
  68. def test_normal_split():
  69. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=2, global_rank=0)
  70. strategy1 = ((2, 1), (1, 2))
  71. strategy2 = ((1, 2, 1), (1, 2, 1))
  72. strategy3 = ((1, 2), (2, 1))
  73. net = Net(strategy1, strategy2, strategy3)
  74. compile_net(net)
  75. def test_normal_split2():
  76. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=4, global_rank=0)
  77. strategy1 = ((4, 1), (1, 4))
  78. strategy2 = ((1, 4, 1), (1, 4, 1))
  79. strategy3 = ((1, 4), (4, 1))
  80. net = Net(strategy1, strategy2, strategy3, split_tuple=(10, 20, 30, 4), param_shape=(64, 8))
  81. compile_net(net)
  82. def test_normal_split3():
  83. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=32, global_rank=17)
  84. strategy1 = ((4, 8), (1, 4))
  85. strategy2 = ((1, 4, 8), (1, 4, 8))
  86. strategy3 = ((1, 32), (32, 1))
  87. net = Net(strategy1, strategy2, strategy3, split_tuple=(10, 20, 30, 4), param_shape=(64, 8))
  88. compile_net(net)
  89. def test_normal_split_with_offset():
  90. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=2, global_rank=0)
  91. strategy1 = ((2, 1), (1, 2))
  92. strategy2 = ((1, 2, 1), (1, 2, 1))
  93. strategy3 = ((1, 2), (2, 1))
  94. net = Net(strategy1, strategy2, strategy3, split_string="manual_split_with_offset", split_tuple=((4, 0), (4, 4)))
  95. compile_net(net)
  96. def test_auto_parallel_error():
  97. context.set_context(save_graphs=False)
  98. context.set_auto_parallel_context(parallel_mode="auto_parallel", device_num=2, global_rank=0)
  99. net = Net()
  100. with pytest.raises(RuntimeError):
  101. compile_net(net)
  102. def test_auto_parallel():
  103. context.set_context(save_graphs=False)
  104. context.set_auto_parallel_context(parallel_mode="auto_parallel", device_num=2, global_rank=0)
  105. net = Net(split_string="fake")
  106. compile_net(net)
  107. def test_axis_error():
  108. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=2, global_rank=0)
  109. strategy1 = ((2, 1), (1, 2))
  110. strategy2 = ((1, 2, 1), (1, 2, 1))
  111. strategy3 = ((1, 2), (2, 1))
  112. net = Net(strategy1, strategy2, strategy3, axis=1)
  113. with pytest.raises(RuntimeError):
  114. compile_net(net)
  115. def test_strategy_error():
  116. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  117. strategy1 = ((4, 1), (8, 1))
  118. strategy2 = ((1, 2, 1), (1, 2, 1))
  119. strategy3 = ((1, 2), (2, 1))
  120. net = Net(strategy1, strategy2, strategy3)
  121. with pytest.raises(RuntimeError):
  122. compile_net(net)
  123. def test_strategy_error2():
  124. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  125. strategy1 = ((4, 1), (1, 8))
  126. strategy2 = ((1, 2, 1), (1, 2, 1))
  127. strategy3 = ((1, 2), (2, 1))
  128. net = Net(strategy1, strategy2, strategy3)
  129. with pytest.raises(RuntimeError):
  130. compile_net(net)
  131. def test_strategy_error3():
  132. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  133. strategy1 = ((2, 1), (1, 2))
  134. strategy2 = ((1, 2, 1), (1, 2, 1))
  135. strategy3 = ((1, 2), (2, 1))
  136. net = Net(strategy1, strategy2, strategy3)
  137. with pytest.raises(RuntimeError):
  138. compile_net(net)
  139. def test_strategy_error4():
  140. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=2, global_rank=0)
  141. strategy1 = ((2, 8), (1, 2))
  142. strategy2 = ((1, 2, 1), (1, 2, 1))
  143. strategy3 = ((1, 2), (2, 1))
  144. net = Net(strategy1, strategy2, strategy3)
  145. with pytest.raises(RuntimeError):
  146. compile_net(net)
  147. def test_strategy_error5():
  148. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=4, global_rank=0)
  149. strategy1 = ((4, 1), (1, 4))
  150. strategy2 = ((1, 2, 1), (1, 2, 1))
  151. strategy3 = ((1, 2), (2, 1))
  152. net = Net(strategy1, strategy2, strategy3)
  153. with pytest.raises(RuntimeError):
  154. compile_net(net)
  155. def test_split_tuple_error():
  156. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=2, global_rank=0)
  157. strategy1 = ((2, 1), (1, 2))
  158. strategy2 = ((1, 2, 1), (1, 2, 1))
  159. strategy3 = ((1, 2), (2, 1))
  160. net = Net(strategy1, strategy2, strategy3, split_tuple=((5, 0), (5, 5)))
  161. with pytest.raises(RuntimeError):
  162. compile_net(net)
  163. def test_parameter_use_tensor_error():
  164. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=2, global_rank=0)
  165. strategy1 = ((2, 1), (1, 2))
  166. strategy2 = ((1, 2, 1), (1, 2, 1))
  167. strategy3 = ((1, 2), (2, 1))
  168. net = Net(strategy1, strategy2, strategy3, init_flag=False)
  169. with pytest.raises(RuntimeError):
  170. compile_net(net)