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_stridedslice.py 6.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. class Net(Cell):
  23. def __init__(self, weight, w2, begin, end, strides, strategy1=None, strategy2=None, is_parameter=True, mask=0):
  24. super().__init__()
  25. self.mul = P.Mul().shard(strategy1)
  26. self.strided_slice = P.StridedSlice(begin_mask=mask).shard(strategy2)
  27. if is_parameter:
  28. self.weight = Parameter(weight, "w1")
  29. else:
  30. self.weight = weight
  31. self.mul2 = P.Mul()
  32. self.weight2 = Parameter(w2, "w2")
  33. self.begin = begin
  34. self.end = end
  35. self.strides = strides
  36. def construct(self, x, b):
  37. out = self.strided_slice(self.weight, self.begin, self.end, self.strides)
  38. out = self.mul(x, out)
  39. out = self.mul2(out, self.weight2)
  40. return out
  41. class Net2(Cell):
  42. def __init__(self, weight2, begin, end, strides, strategy1=None, strategy2=None):
  43. super().__init__()
  44. self.mul = P.Mul().shard(strategy1)
  45. self.strided_slice = P.StridedSlice().shard(strategy2)
  46. self.weight2 = Parameter(weight2, "w2")
  47. self.begin = begin
  48. self.end = end
  49. self.strides = strides
  50. def construct(self, x, b):
  51. out = self.mul(x, self.weight2)
  52. out = self.strided_slice(out, self.begin, self.end, self.strides)
  53. return out
  54. _x = Tensor(np.ones([128, 64, 1]), dtype=ms.float32)
  55. _w1 = Tensor(np.ones([256, 64, 32]), dtype=ms.float32)
  56. _w2 = Tensor(np.ones([128, 64, 1]), dtype=ms.float32)
  57. _b = Tensor(np.ones([128, 64, 32]), dtype=ms.float32)
  58. def compile_net(net):
  59. context.set_context(save_graphs=False)
  60. optimizer = Momentum(net.trainable_params(), learning_rate=0.1, momentum=0.9)
  61. train_net = TrainOneStepCell(net, optimizer)
  62. train_net.set_auto_parallel()
  63. train_net.set_train()
  64. _executor.compile(train_net, _x, _b)
  65. context.reset_auto_parallel_context()
  66. def test_stridedslice_no_fully_fetch_split_error():
  67. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  68. strategy1 = ((2, 2, 2), (2, 2, 2))
  69. strategy2 = ((2, 2, 2),)
  70. net = Net(_w1, _w2, (0, 0, 0), (128, 64, 32), (1, 1, 1), strategy1, strategy2, is_parameter=True)
  71. with pytest.raises(RuntimeError):
  72. compile_net(net)
  73. def test_stridedslice_strides_no_1_split_error():
  74. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  75. strategy1 = ((2, 2, 2), (2, 2, 2))
  76. strategy2 = ((1, 2, 2),)
  77. net = Net(_w1, _w2, (0, 0, 0), (128, 64, 32), (1, 1, 2), strategy1, strategy2, is_parameter=True)
  78. with pytest.raises(RuntimeError):
  79. compile_net(net)
  80. def test_stridedslice_mask_no_0_split_error():
  81. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  82. strategy1 = ((2, 2, 2), (2, 2, 2))
  83. strategy2 = ((1, 2, 2),)
  84. net = Net(_w1, _w2, (0, 0, 0), (128, 64, 32), (1, 1, 1), strategy1, strategy2, is_parameter=True, mask=1)
  85. with pytest.raises(RuntimeError):
  86. compile_net(net)
  87. def test_stridedslice_begin_size_smaller():
  88. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  89. strategy1 = ((1, 4, 1), (1, 4, 2))
  90. strategy2 = ((1, 4, 2),)
  91. net = Net(_w1, _w2, (0, 0), (128, 64), (1, 1), strategy1, strategy2, is_parameter=True)
  92. compile_net(net)
  93. def test_stridedslice_parameter():
  94. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  95. strategy1 = ((1, 4, 1), (1, 4, 2))
  96. strategy2 = ((1, 4, 2),)
  97. net = Net(_w1, _w2, (0, 0, 0), (128, 64, 32), (1, 1, 1), strategy1, strategy2, is_parameter=True)
  98. compile_net(net)
  99. def test_stridedslice_tensor():
  100. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  101. strategy1 = ((1, 4, 1), (1, 4, 2))
  102. strategy2 = ((1, 4, 2),)
  103. net = Net(_w1, _w2, (0, 0, 0), (128, 64, 32), (1, 1, 1), strategy1, strategy2, is_parameter=False)
  104. compile_net(net)
  105. def test_stridedslice_parameter_no_full_split():
  106. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  107. strategy1 = ((1, 4, 1), (1, 4, 2))
  108. strategy2 = ((1, 2, 2),)
  109. net = Net(_w1, _w2, (0, 0, 0), (128, 64, 32), (1, 1, 1), strategy1, strategy2, is_parameter=True)
  110. compile_net(net)
  111. def test_stridedslice_output():
  112. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  113. strategy1 = ((1, 8, 1), (1, 8, 1))
  114. strategy2 = ((1, 8, 1),)
  115. net = Net2(_w2, (0, 0, 0), (64, 64, 1), (1, 1, 1), strategy1, strategy2)
  116. compile_net(net)
  117. def test_stridedslice_output_no_full_split():
  118. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  119. strategy1 = ((1, 8, 1), (1, 8, 1))
  120. strategy2 = ((1, 4, 1),)
  121. net = Net2(_w2, (0, 0, 0), (64, 64, 1), (1, 1, 1), strategy1, strategy2)
  122. compile_net(net)
  123. def test_stridedslice_no_strategy():
  124. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  125. strategy1 = ((1, 8, 1), (1, 8, 1))
  126. strategy2 = None
  127. net = Net2(_w2, (0, 0, 0), (128, 64, 1), (1, 1, 1), strategy1, strategy2)
  128. compile_net(net)
  129. def test_stridedslice_auto_parallel():
  130. context.set_auto_parallel_context(parallel_mode="auto_parallel", device_num=8, global_rank=0)
  131. net = Net2(_w2, (0, 0, 0), (32, 64, 1), (1, 1, 1))
  132. compile_net(net)