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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 _cell_graph_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. optimizer = LazyAdam(net.trainable_params(), learning_rate=0.1)
  61. optimizer.sparse_opt.add_prim_attr("primitive_target", "CPU")
  62. train_net = TrainOneStepCell(net, optimizer)
  63. train_net.set_auto_parallel()
  64. train_net.set_train()
  65. _cell_graph_executor.compile(train_net, _x, _b, auto_parallel_mode=True)
  66. context.reset_auto_parallel_context()
  67. def test_normal_split():
  68. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=2, global_rank=0)
  69. strategy1 = ((2, 1), (1, 2))
  70. strategy2 = ((1, 2, 1), (1, 2, 1))
  71. strategy3 = ((1, 2), (2, 1))
  72. net = Net(strategy1, strategy2, strategy3)
  73. compile_net(net)
  74. def test_normal_split2():
  75. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=4, global_rank=0)
  76. strategy1 = ((4, 1), (1, 4))
  77. strategy2 = ((1, 4, 1), (1, 4, 1))
  78. strategy3 = ((1, 4), (4, 1))
  79. net = Net(strategy1, strategy2, strategy3, split_tuple=(10, 20, 30, 4), param_shape=(64, 8))
  80. compile_net(net)
  81. def test_normal_split3():
  82. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=32, global_rank=17)
  83. strategy1 = ((4, 8), (1, 4))
  84. strategy2 = ((1, 4, 8), (1, 4, 8))
  85. strategy3 = ((1, 32), (32, 1))
  86. net = Net(strategy1, strategy2, strategy3, split_tuple=(10, 20, 30, 4), param_shape=(64, 8))
  87. compile_net(net)
  88. def test_normal_split_with_offset():
  89. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=2, global_rank=0)
  90. strategy1 = ((2, 1), (1, 2))
  91. strategy2 = ((1, 2, 1), (1, 2, 1))
  92. strategy3 = ((1, 2), (2, 1))
  93. net = Net(strategy1, strategy2, strategy3, split_string="manual_split_with_offset", split_tuple=((4, 0), (4, 4)))
  94. compile_net(net)
  95. def test_auto_parallel_error():
  96. context.set_auto_parallel_context(parallel_mode="auto_parallel", device_num=2, global_rank=0)
  97. net = Net()
  98. with pytest.raises(RuntimeError):
  99. compile_net(net)
  100. def test_auto_parallel():
  101. context.set_auto_parallel_context(parallel_mode="auto_parallel", device_num=2, global_rank=0)
  102. net = Net(split_string="fake")
  103. compile_net(net)
  104. def test_axis_error():
  105. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=2, global_rank=0)
  106. strategy1 = ((2, 1), (1, 2))
  107. strategy2 = ((1, 2, 1), (1, 2, 1))
  108. strategy3 = ((1, 2), (2, 1))
  109. net = Net(strategy1, strategy2, strategy3, axis=1)
  110. with pytest.raises(RuntimeError):
  111. compile_net(net)
  112. def test_strategy_error():
  113. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  114. strategy1 = ((4, 1), (8, 1))
  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_error2():
  121. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  122. strategy1 = ((4, 1), (1, 8))
  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_error3():
  129. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  130. strategy1 = ((2, 1), (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_error4():
  137. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=2, global_rank=0)
  138. strategy1 = ((2, 8), (1, 2))
  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_strategy_error5():
  145. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=4, global_rank=0)
  146. strategy1 = ((4, 1), (1, 4))
  147. strategy2 = ((1, 2, 1), (1, 2, 1))
  148. strategy3 = ((1, 2), (2, 1))
  149. net = Net(strategy1, strategy2, strategy3)
  150. with pytest.raises(RuntimeError):
  151. compile_net(net)
  152. def test_split_tuple_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, split_tuple=((5, 0), (5, 5)))
  158. with pytest.raises(RuntimeError):
  159. compile_net(net)
  160. def test_parameter_use_tensor_error():
  161. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=2, global_rank=0)
  162. strategy1 = ((2, 1), (1, 2))
  163. strategy2 = ((1, 2, 1), (1, 2, 1))
  164. strategy3 = ((1, 2), (2, 1))
  165. net = Net(strategy1, strategy2, strategy3, init_flag=False)
  166. with pytest.raises(RuntimeError):
  167. compile_net(net)