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_expand_dims.py 4.0 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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, Parameter
  17. from mindspore.common.api import _executor
  18. from mindspore.nn import Cell, TrainOneStepCell, Momentum
  19. from mindspore.ops import operations as P
  20. class Net(Cell):
  21. def __init__(self, mul_weight, strategy1=None, strategy2=None, strategy3=None):
  22. super().__init__()
  23. self.mul = P.Mul().set_strategy(strategy1)
  24. self.expand_dims = P.ExpandDims().set_strategy(strategy2)
  25. self.mul2 = P.Mul().set_strategy(strategy3)
  26. self.mul_weight = Parameter(mul_weight, "w1")
  27. def construct(self, x, b):
  28. out = self.mul(x, self.mul_weight)
  29. out = self.expand_dims(out, -1)
  30. out = self.mul2(out, b)
  31. return out
  32. class Net2(Cell):
  33. def __init__(self, mul_weight, strategy1=None, strategy2=None):
  34. super().__init__()
  35. self.expand_dims = P.ExpandDims().set_strategy(strategy1)
  36. self.mul = P.Mul().set_strategy(strategy2)
  37. self.mul_weight = Parameter(mul_weight, "w1")
  38. def construct(self, x, b):
  39. out = self.expand_dims(self.mul_weight, -1)
  40. out = self.mul(out, b)
  41. return out
  42. _x = Tensor(np.ones([128, 64, 32]), dtype=ms.float32)
  43. _w1 = Tensor(np.ones([128, 64, 32]), dtype=ms.float32)
  44. _b = Tensor(np.ones([128, 64, 32, 1]), dtype=ms.float32)
  45. def compile_net(net):
  46. optimizer = Momentum(net.trainable_params(), learning_rate=0.1, momentum=0.9)
  47. train_net = TrainOneStepCell(net, optimizer)
  48. train_net.set_auto_parallel()
  49. _executor.compile(train_net, _x, _b)
  50. context.reset_auto_parallel_context()
  51. def test_expand_dims_data_parallel():
  52. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=16, global_rank=0)
  53. strategy1 = ((16, 1, 1), (16, 1, 1))
  54. strategy2 = ((16, 1, 1),)
  55. strategy3 = ((16, 1, 1, 1), (16, 1, 1, 1))
  56. net = Net(_w1, strategy1, strategy2, strategy3)
  57. compile_net(net)
  58. def test_expand_dims_model_parallel():
  59. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=16, global_rank=0)
  60. strategy1 = ((1, 1, 16), (1, 1, 16))
  61. strategy2 = ((1, 1, 16),)
  62. strategy3 = ((1, 1, 16, 1), (1, 1, 16, 1))
  63. net = Net(_w1, strategy1, strategy2, strategy3)
  64. compile_net(net)
  65. def test_expand_dims_hybrid_parallel():
  66. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=16, global_rank=0)
  67. strategy1 = ((2, 2, 4), (2, 2, 4))
  68. strategy2 = ((2, 2, 4),)
  69. strategy3 = ((2, 2, 4, 1), (2, 2, 4, 1))
  70. net = Net(_w1, strategy1, strategy2, strategy3)
  71. compile_net(net)
  72. def test_expand_dims_auto_parallel():
  73. context.set_auto_parallel_context(parallel_mode="auto_parallel", device_num=16, global_rank=0)
  74. net = Net(_w1)
  75. compile_net(net)
  76. def test_expand_dims_repeat_calc():
  77. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=16, global_rank=0)
  78. strategy1 = ((2, 2, 4), (2, 2, 4))
  79. strategy2 = ((1, 2, 2),)
  80. strategy3 = ((2, 2, 4, 1), (2, 2, 4, 1))
  81. net = Net(_w1, strategy1, strategy2, strategy3)
  82. compile_net(net)
  83. def test_expand_dims_parameter():
  84. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=16, global_rank=0)
  85. strategy1 = ((1, 2, 2),)
  86. strategy2 = ((2, 2, 4, 1), (2, 2, 4, 1))
  87. net = Net2(_w1, strategy1, strategy2)
  88. compile_net(net)