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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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=True)
  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. _executor.compile(train_net, _x, _b)
  64. context.reset_auto_parallel_context()
  65. def test_stridedslice_no_fully_fetch_split_error():
  66. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  67. strategy1 = ((2, 2, 2), (2, 2, 2))
  68. strategy2 = ((2, 2, 2),)
  69. net = Net(_w1, _w2, (0, 0, 0), (128, 64, 32), (1, 1, 1), strategy1, strategy2, is_parameter=True)
  70. with pytest.raises(RuntimeError):
  71. compile_net(net)
  72. def test_stridedslice_strides_no_1_split_error():
  73. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  74. strategy1 = ((2, 2, 2), (2, 2, 2))
  75. strategy2 = ((1, 2, 2),)
  76. net = Net(_w1, _w2, (0, 0, 0), (128, 64, 32), (1, 1, 2), strategy1, strategy2, is_parameter=True)
  77. with pytest.raises(RuntimeError):
  78. compile_net(net)
  79. def test_stridedslice_mask_no_0_split_error():
  80. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  81. strategy1 = ((2, 2, 2), (2, 2, 2))
  82. strategy2 = ((1, 2, 2),)
  83. net = Net(_w1, _w2, (0, 0, 0), (128, 64, 32), (1, 1, 1), strategy1, strategy2, is_parameter=True, mask=1)
  84. with pytest.raises(RuntimeError):
  85. compile_net(net)
  86. def test_stridedslice_begin_size_smaller():
  87. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  88. strategy1 = ((1, 4, 1), (1, 4, 2))
  89. strategy2 = ((1, 4, 2),)
  90. net = Net(_w1, _w2, (0, 0), (128, 64), (1, 1), strategy1, strategy2, is_parameter=True)
  91. compile_net(net)
  92. def test_stridedslice_parameter():
  93. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  94. strategy1 = ((1, 4, 1), (1, 4, 2))
  95. strategy2 = ((1, 4, 2),)
  96. net = Net(_w1, _w2, (0, 0, 0), (128, 64, 32), (1, 1, 1), strategy1, strategy2, is_parameter=True)
  97. compile_net(net)
  98. def test_stridedslice_tensor():
  99. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  100. strategy1 = ((1, 4, 1), (1, 4, 2))
  101. strategy2 = ((1, 4, 2),)
  102. net = Net(_w1, _w2, (0, 0, 0), (128, 64, 32), (1, 1, 1), strategy1, strategy2, is_parameter=False)
  103. compile_net(net)
  104. def test_stridedslice_parameter_no_full_split():
  105. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  106. strategy1 = ((1, 4, 1), (1, 4, 2))
  107. strategy2 = ((1, 2, 2),)
  108. net = Net(_w1, _w2, (0, 0, 0), (128, 64, 32), (1, 1, 1), strategy1, strategy2, is_parameter=True)
  109. compile_net(net)
  110. def test_stridedslice_output():
  111. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  112. strategy1 = ((1, 8, 1), (1, 8, 1))
  113. strategy2 = ((1, 8, 1),)
  114. net = Net2(_w2, (0, 0, 0), (64, 64, 1), (1, 1, 1), strategy1, strategy2)
  115. compile_net(net)
  116. def test_stridedslice_output_no_full_split():
  117. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  118. strategy1 = ((1, 8, 1), (1, 8, 1))
  119. strategy2 = ((1, 4, 1),)
  120. net = Net2(_w2, (0, 0, 0), (64, 64, 1), (1, 1, 1), strategy1, strategy2)
  121. compile_net(net)
  122. def test_stridedslice_no_strategy():
  123. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  124. strategy1 = ((1, 8, 1), (1, 8, 1))
  125. strategy2 = None
  126. net = Net2(_w2, (0, 0, 0), (128, 64, 1), (1, 1, 1), strategy1, strategy2)
  127. compile_net(net)
  128. def test_stridedslice_auto_parallel():
  129. context.set_auto_parallel_context(parallel_mode="auto_parallel", device_num=8, global_rank=0)
  130. net = Net2(_w2, (0, 0, 0), (32, 64, 1), (1, 1, 1))
  131. compile_net(net)