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_o2_level.py 7.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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.nn import Cell, Momentum
  20. from mindspore.ops import operations as P
  21. from mindspore.train import Model
  22. from tests.dataset_mock import MindData
  23. class Dataset(MindData):
  24. def __init__(self, predict, label, length=3):
  25. super(Dataset, self).__init__(size=length)
  26. self.predict = predict
  27. self.label = label
  28. self.index = 0
  29. self.length = length
  30. def __iter__(self):
  31. return self
  32. def __next__(self):
  33. if self.index >= self.length:
  34. raise StopIteration
  35. self.index += 1
  36. return self.predict, self.label
  37. def reset(self):
  38. self.index = 0
  39. class Net(Cell):
  40. def __init__(self, weight, w2, begin, end, strides, strategy1=None, strategy2=None, is_parameter=True, mask=0):
  41. super().__init__()
  42. self.mul = P.Mul().shard(strategy1)
  43. self.strided_slice = P.StridedSlice(begin_mask=mask).shard(strategy2)
  44. if is_parameter:
  45. self.weight = Parameter(weight, "w1")
  46. else:
  47. self.weight = weight
  48. self.mul2 = P.Mul()
  49. self.weight2 = Parameter(w2, "w2")
  50. self.begin = begin
  51. self.end = end
  52. self.strides = strides
  53. def construct(self, x, b):
  54. out = self.strided_slice(
  55. self.weight, self.begin, self.end, self.strides)
  56. out = self.mul(x, out)
  57. out = self.mul2(out, self.weight2)
  58. return out
  59. class Net2(Cell):
  60. def __init__(self, weight2, begin, end, strides, strategy1=None, strategy2=None):
  61. super().__init__()
  62. self.mul = P.Mul().shard(strategy1)
  63. self.strided_slice = P.StridedSlice().shard(strategy2)
  64. self.weight2 = Parameter(weight2, "w2")
  65. self.begin = begin
  66. self.end = end
  67. self.strides = strides
  68. def construct(self, x, b):
  69. out = self.mul(x, self.weight2)
  70. out = self.strided_slice(out, self.begin, self.end, self.strides)
  71. return out
  72. _x = Tensor(np.ones([16, 64, 1]), dtype=ms.float32)
  73. _b = Tensor(np.ones([16, 64, 32]), dtype=ms.float32)
  74. _w1 = Tensor(np.ones([256, 64, 32]), dtype=ms.float32)
  75. _w2 = Tensor(np.ones([128, 64, 1]), dtype=ms.float32)
  76. def compile_net(net):
  77. context.set_context(save_graphs=False)
  78. learning_rate = 0.1
  79. momentum = 0.9
  80. epoch_size = 2
  81. dataset = Dataset(_x, _b)
  82. opt = Momentum(net.trainable_params(), learning_rate, momentum)
  83. model = Model(net, optimizer=opt, amp_level="O2")
  84. model.train(epoch_size, dataset, dataset_sink_mode=False)
  85. context.reset_auto_parallel_context()
  86. def test_stridedslice_no_fully_fetch_split_error():
  87. context.set_auto_parallel_context(
  88. parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  89. strategy1 = ((2, 2, 2), (2, 2, 2))
  90. strategy2 = ((2, 2, 2),)
  91. net = Net(_w1, _w2, (0, 0, 0), (128, 64, 32), (1, 1, 1),
  92. strategy1, strategy2, is_parameter=True)
  93. with pytest.raises(RuntimeError):
  94. compile_net(net)
  95. def test_stridedslice_strides_no_1_split_error():
  96. context.set_auto_parallel_context(
  97. parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  98. strategy1 = ((2, 2, 2), (2, 2, 2))
  99. strategy2 = ((1, 2, 2),)
  100. net = Net(_w1, _w2, (0, 0, 0), (128, 64, 32), (1, 1, 2),
  101. strategy1, strategy2, is_parameter=True)
  102. with pytest.raises(RuntimeError):
  103. compile_net(net)
  104. def test_stridedslice_mask_no_0_split_error():
  105. context.set_auto_parallel_context(
  106. parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  107. strategy1 = ((2, 2, 2), (2, 2, 2))
  108. strategy2 = ((1, 2, 2),)
  109. net = Net(_w1, _w2, (0, 0, 0), (128, 64, 32), (1, 1, 1),
  110. strategy1, strategy2, is_parameter=True, mask=1)
  111. with pytest.raises(RuntimeError):
  112. compile_net(net)
  113. def test_stridedslice_begin_size_smaller():
  114. context.set_auto_parallel_context(
  115. parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  116. strategy1 = ((1, 4, 1), (1, 4, 2))
  117. strategy2 = ((1, 4, 2),)
  118. net = Net(_w1, _w2, (0, 0), (128, 64), (1, 1),
  119. strategy1, strategy2, is_parameter=True)
  120. compile_net(net)
  121. def test_stridedslice_parameter():
  122. context.set_auto_parallel_context(
  123. parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  124. strategy1 = ((1, 4, 1), (1, 4, 2))
  125. strategy2 = ((1, 4, 2),)
  126. net = Net(_w1, _w2, (0, 0, 0), (128, 64, 32), (1, 1, 1),
  127. strategy1, strategy2, is_parameter=True)
  128. compile_net(net)
  129. def test_stridedslice_tensor():
  130. context.set_auto_parallel_context(
  131. parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  132. strategy1 = ((1, 4, 1), (1, 4, 2))
  133. strategy2 = ((1, 4, 2),)
  134. net = Net(_w1, _w2, (0, 0, 0), (128, 64, 32), (1, 1, 1),
  135. strategy1, strategy2, is_parameter=False)
  136. compile_net(net)
  137. def test_stridedslice_parameter_no_full_split():
  138. context.set_auto_parallel_context(
  139. parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  140. strategy1 = ((1, 4, 1), (1, 4, 2))
  141. strategy2 = ((1, 2, 2),)
  142. net = Net(_w1, _w2, (0, 0, 0), (128, 64, 32), (1, 1, 1),
  143. strategy1, strategy2, is_parameter=True)
  144. compile_net(net)
  145. def test_stridedslice_output():
  146. context.set_auto_parallel_context(
  147. parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  148. strategy1 = ((1, 8, 1), (1, 8, 1))
  149. strategy2 = ((1, 8, 1),)
  150. net = Net2(_w2, (0, 0, 0), (64, 64, 1), (1, 1, 1), strategy1, strategy2)
  151. compile_net(net)
  152. def test_stridedslice_output_no_full_split():
  153. context.set_auto_parallel_context(
  154. parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  155. strategy1 = ((1, 8, 1), (1, 8, 1))
  156. strategy2 = ((1, 4, 1),)
  157. net = Net2(_w2, (0, 0, 0), (64, 64, 1), (1, 1, 1), strategy1, strategy2)
  158. compile_net(net)
  159. def test_stridedslice_no_strategy():
  160. context.set_auto_parallel_context(
  161. parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  162. strategy1 = ((1, 8, 1), (1, 8, 1))
  163. strategy2 = None
  164. net = Net2(_w2, (0, 0, 0), (128, 64, 1), (1, 1, 1), strategy1, strategy2)
  165. compile_net(net)
  166. def test_stridedslice_auto_parallel():
  167. context.set_auto_parallel_context(
  168. parallel_mode="auto_parallel", device_num=8, global_rank=0)
  169. net = Net2(_w2, (0, 0, 0), (32, 64, 1), (1, 1, 1))
  170. compile_net(net)