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_slice.py 4.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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, strategy1=None, strategy2=None, is_parameter=True):
  24. super().__init__()
  25. self.mul = P.Mul().shard(strategy1)
  26. self.slice = P.Slice().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. def construct(self, x, b):
  36. out = self.slice(self.weight, self.begin, self.end)
  37. out = self.mul(x, out)
  38. out = self.mul2(out, self.weight2)
  39. return out
  40. class Net2(Cell):
  41. def __init__(self, weight2, begin, end, strategy1=None, strategy2=None):
  42. super().__init__()
  43. self.mul = P.Mul().shard(strategy1)
  44. self.slice = P.Slice().shard(strategy2)
  45. self.weight2 = Parameter(weight2, "w2")
  46. self.begin = begin
  47. self.end = end
  48. def construct(self, x, b):
  49. out = self.mul(x, self.weight2)
  50. out = self.slice(out, self.begin, self.end)
  51. return out
  52. _x = Tensor(np.ones([128, 64, 1]), dtype=ms.float32)
  53. _w1 = Tensor(np.ones([256, 64, 32]), dtype=ms.float32)
  54. _w2 = Tensor(np.ones([128, 64, 1]), dtype=ms.float32)
  55. _b = Tensor(np.ones([128, 64, 32]), dtype=ms.float32)
  56. def compile_net(net):
  57. optimizer = Momentum(net.trainable_params(), learning_rate=0.1, momentum=0.9)
  58. train_net = TrainOneStepCell(net, optimizer)
  59. train_net.set_auto_parallel()
  60. train_net.set_train()
  61. _executor.compile(train_net, _x, _b)
  62. context.reset_auto_parallel_context()
  63. def test_slice_no_fully_fetch_split_error():
  64. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  65. strategy1 = ((2, 2, 2), (2, 2, 2))
  66. strategy2 = ((2, 2, 2),)
  67. net = Net(_w1, _w2, (0, 0, 0), (128, 64, 32), strategy1, strategy2, is_parameter=True)
  68. with pytest.raises(RuntimeError):
  69. compile_net(net)
  70. def test_slice_parameter():
  71. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  72. strategy1 = ((1, 4, 1), (1, 4, 2))
  73. strategy2 = ((1, 4, 2),)
  74. net = Net(_w1, _w2, (0, 0, 0), (128, 64, 32), strategy1, strategy2)
  75. compile_net(net)
  76. def test_slice_tensor():
  77. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  78. strategy1 = ((1, 4, 1), (1, 4, 2))
  79. strategy2 = ((1, 4, 2),)
  80. net = Net(_w1, _w2, (0, 0, 0), (128, 64, 32), strategy1, strategy2, is_parameter=False)
  81. compile_net(net)
  82. def test_slice_parameter_no_full_split():
  83. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  84. strategy1 = ((1, 4, 1), (1, 4, 2))
  85. strategy2 = ((1, 2, 2),)
  86. net = Net(_w1, _w2, (0, 0, 0), (128, 64, 32), strategy1, strategy2, is_parameter=True)
  87. compile_net(net)
  88. def test_slice_output():
  89. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  90. strategy1 = ((1, 8, 1), (1, 8, 1))
  91. strategy2 = ((1, 8, 1),)
  92. net = Net2(_w2, (0, 0, 0), (64, 64, 1), strategy1, strategy2)
  93. compile_net(net)
  94. def test_stridedslice_output_no_full_split():
  95. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  96. strategy1 = ((1, 8, 1), (1, 8, 1))
  97. strategy2 = ((1, 4, 1),)
  98. net = Net2(_w2, (0, 0, 0), (64, 64, 1), strategy1, strategy2)
  99. compile_net(net)
  100. def test_stridedslice_no_strategy():
  101. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  102. strategy1 = ((1, 8, 1), (1, 8, 1))
  103. strategy2 = None
  104. net = Net2(_w2, (0, 0, 0), (128, 64, 1), strategy1, strategy2)
  105. compile_net(net)
  106. def test_slice_auto_parallel():
  107. context.set_auto_parallel_context(parallel_mode="auto_parallel", device_num=8, global_rank=0)
  108. net = Net2(_w2, (0, 0, 0), (32, 64, 1))
  109. compile_net(net)