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_concat.py 4.6 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 mindspore as ms
  17. from mindspore import context, Tensor, Parameter
  18. from mindspore.common.api import _executor
  19. from mindspore.nn import Cell, TrainOneStepCell, Momentum
  20. from mindspore.ops import operations as P
  21. class Net(Cell):
  22. def __init__(self, weight, weight2, strategy1=None, strategy2=None, is_parameter=True):
  23. super().__init__()
  24. self.concat = P.Concat(axis=0).set_strategy(strategy1)
  25. if is_parameter:
  26. self.weight = Parameter(weight, "w1")
  27. else:
  28. self.weight = weight
  29. self.mul = P.Mul().set_strategy(strategy2)
  30. self.weight2 = Parameter(weight2, "w2")
  31. def construct(self, x, b):
  32. out = self.concat((self.weight, self.weight2))
  33. out = self.mul(x, out)
  34. return out
  35. class Net2(Cell):
  36. def __init__(self, weight, strategy1=None, strategy2=None, axis=0):
  37. super().__init__()
  38. self.mul = P.Mul().set_strategy(strategy1)
  39. self.concat = P.Concat(axis=axis).set_strategy(strategy2)
  40. self.weight = Parameter(weight, "w")
  41. def construct(self, x, b):
  42. out = self.mul(x, b)
  43. out = self.concat((out, self.weight))
  44. return out
  45. _x = Tensor(np.ones([128, 64, 32]), dtype=ms.float32)
  46. _w1 = Tensor(np.ones([96, 64, 32]), dtype=ms.float32)
  47. _w2 = Tensor(np.ones([32, 64, 32]), dtype=ms.float32)
  48. _w3 = Tensor(np.ones([128, 16, 32]), dtype=ms.float32)
  49. _b = Tensor(np.ones([128, 64, 32]), dtype=ms.float32)
  50. def compile_net(net):
  51. context.set_context(save_graphs=True)
  52. optimizer = Momentum(net.trainable_params(), learning_rate=0.1, momentum=0.9)
  53. train_net = TrainOneStepCell(net, optimizer)
  54. train_net.set_auto_parallel()
  55. _executor.compile(train_net, _x, _b)
  56. context.reset_auto_parallel_context()
  57. def test_concat_parameter():
  58. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  59. strategy1 = ((1, 4, 2), (1, 4, 2))
  60. strategy2 = ((1, 4, 2), (1, 4, 2))
  61. net = Net(_w1, _w2, strategy1, strategy2, is_parameter=True)
  62. compile_net(net)
  63. def test_concat_parameter_no_full_split():
  64. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  65. strategy1 = ((1, 2, 2), (1, 2, 2))
  66. strategy2 = ((1, 4, 2), (1, 4, 2))
  67. net = Net(_w1, _w2, strategy1, strategy2, is_parameter=True)
  68. compile_net(net)
  69. def test_concat_tensor_and_parameter():
  70. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  71. strategy1 = ((1, 2, 2), (1, 2, 2))
  72. strategy2 = ((1, 4, 2), (1, 4, 2))
  73. net = Net(_w1, _w2, strategy1, strategy2, is_parameter=False)
  74. compile_net(net)
  75. def test_concat_output():
  76. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  77. strategy1 = ((2, 2, 2), (2, 2, 2))
  78. strategy2 = ((1, 4, 2), (1, 4, 2))
  79. net = Net2(_w1, strategy1, strategy2)
  80. compile_net(net)
  81. def test_concat_output_no_full_split():
  82. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  83. strategy1 = ((2, 2, 2), (2, 2, 2))
  84. strategy2 = ((1, 2, 2), (1, 2, 2))
  85. net = Net2(_w1, strategy1, strategy2)
  86. compile_net(net)
  87. def test_concat_no_strategy():
  88. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  89. strategy1 = ((2, 2, 2), (2, 2, 2))
  90. strategy2 = None
  91. net = Net2(_w3, strategy1, strategy2, axis=1)
  92. compile_net(net)
  93. def test_concat_auto_parallel():
  94. context.set_auto_parallel_context(parallel_mode="auto_parallel", device_num=8, global_rank=0)
  95. net = Net2(_w2)
  96. compile_net(net)
  97. def test_concat_auto_parallel2():
  98. context.set_auto_parallel_context(parallel_mode="auto_parallel", device_num=8, global_rank=0)
  99. strategy1 = None
  100. strategy2 = None
  101. net = Net2(_w3, strategy1, strategy2, axis=1)
  102. compile_net(net)