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_square.py 3.0 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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):
  22. super(Net, self).__init__()
  23. self.mul = P.Mul().shard(strategy1)
  24. self.square = P.Square().shard(strategy2)
  25. self.mul2 = P.Mul().shard(strategy1)
  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.square(out)
  30. out = self.mul2(out, b)
  31. return out
  32. _x = Tensor(np.ones([128, 64, 32]), dtype=ms.float32)
  33. _w1 = Tensor(np.ones([128, 64, 32]), dtype=ms.float32)
  34. _b = Tensor(np.ones([128, 64, 32]), dtype=ms.float32)
  35. def compile_net(net):
  36. optimizer = Momentum(net.trainable_params(), learning_rate=0.1, momentum=0.9)
  37. train_net = TrainOneStepCell(net, optimizer)
  38. train_net.set_auto_parallel()
  39. train_net.set_train()
  40. _executor.compile(train_net, _x, _b)
  41. context.reset_auto_parallel_context()
  42. def test_square_data_parallel():
  43. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=16, global_rank=0)
  44. strategy1 = ((16, 1, 1), (16, 1, 1))
  45. strategy2 = ((16, 1, 1),)
  46. net = Net(_w1, strategy1, strategy2)
  47. compile_net(net)
  48. def test_square_model_parallel():
  49. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=16, global_rank=0)
  50. strategy1 = ((1, 1, 16), (1, 1, 16))
  51. strategy2 = ((1, 1, 16),)
  52. net = Net(_w1, strategy1, strategy2)
  53. compile_net(net)
  54. def test_square_hybrid_parallel():
  55. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=16, global_rank=0)
  56. strategy1 = ((2, 2, 4), (2, 2, 4))
  57. strategy2 = ((2, 2, 4),)
  58. net = Net(_w1, strategy1, strategy2)
  59. compile_net(net)
  60. def test_square_auto_parallel():
  61. context.set_auto_parallel_context(parallel_mode="auto_parallel", device_num=16, global_rank=0)
  62. net = Net(_w1)
  63. compile_net(net)
  64. def test_square_repeat_calc():
  65. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=16, global_rank=0)
  66. strategy1 = ((2, 2, 4), (2, 2, 4))
  67. strategy2 = ((1, 2, 2),)
  68. net = Net(_w1, strategy1, strategy2)
  69. compile_net(net)