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_squeeze_info.py 2.6 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. import numpy as np
  15. import mindspore as ms
  16. from mindspore import context, Tensor
  17. from mindspore.common.api import _executor
  18. from mindspore.nn import Cell
  19. from mindspore.ops import operations as P
  20. class Net(Cell):
  21. def __init__(self, strategy1=None, strategy2=None, axis=()):
  22. super().__init__()
  23. self.squeeze = P.Squeeze(axis=axis).shard(strategy1)
  24. self.mul = P.Mul().shard(strategy2)
  25. def construct(self, x, b):
  26. out = self.squeeze(x)
  27. out = self.mul(out, b)
  28. return out
  29. _x = Tensor(np.ones([64, 1, 32, 1]), dtype=ms.float32)
  30. _b = Tensor(np.ones([64, 32]), dtype=ms.float32)
  31. def compile_net(net):
  32. net.set_auto_parallel()
  33. net.set_train()
  34. _executor.compile(net, _x, _b)
  35. context.reset_auto_parallel_context()
  36. def test_squeeze_data_parallel():
  37. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=16, global_rank=0)
  38. strategy1 = ((16, 1, 1, 1),)
  39. strategy2 = ((16, 1), (16, 1))
  40. net = Net(strategy1, strategy2)
  41. compile_net(net)
  42. def test_squeeze_model_parallel():
  43. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=16, global_rank=0)
  44. strategy1 = ((1, 1, 16, 1),)
  45. strategy2 = ((1, 16), (1, 16))
  46. net = Net(strategy1, strategy2)
  47. compile_net(net)
  48. def test_squeeze_specified_axis():
  49. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=16, global_rank=0)
  50. strategy1 = ((4, 1, 4, 1),)
  51. strategy2 = ((8, 2), (8, 2))
  52. net = Net(strategy1, strategy2, (1, 3))
  53. compile_net(net)
  54. def test_squeeze_auto_parallel():
  55. context.set_auto_parallel_context(parallel_mode="auto_parallel", device_num=16, global_rank=0)
  56. net = Net()
  57. compile_net(net)
  58. def test_squeeze_repeat_calc():
  59. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=16, global_rank=0)
  60. strategy1 = ((1, 1, 8, 1),)
  61. strategy2 = ((2, 8), (2, 8))
  62. net = Net(strategy1, strategy2)
  63. compile_net(net)