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_split.py 5.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. import mindspore.context as context
  18. from mindspore import Tensor, Parameter
  19. import mindspore.nn as nn
  20. from mindspore.common.api import _executor
  21. from mindspore.nn import TrainOneStepCell, Momentum
  22. from mindspore.ops import operations as P
  23. class Net(nn.Cell):
  24. def __init__(self, mul_weight, axis=0, out_nums=1, strategy1=None, strategy2=None, strategy3=None):
  25. super(Net, self).__init__()
  26. self.split = P.Split(axis, out_nums).shard(strategy1)
  27. self.mul = P.Mul().shard(strategy2)
  28. self.matmul = P.MatMul(transpose_b=True).shard(strategy2)
  29. self.matmul2 = P.MatMul().shard(strategy3)
  30. self.weight = Parameter(mul_weight, "w1")
  31. def construct(self, x):
  32. out = self.mul(x, self.weight)
  33. out1, out2, out3 = self.split(out)
  34. out = self.matmul(out1, out2)
  35. out = self.matmul2(out, out3)
  36. return out
  37. class Net1(nn.Cell):
  38. def __init__(self, mul_weight, axis=0, out_nums=1, strategy1=None, strategy2=None):
  39. super(Net1, self).__init__()
  40. self.split = P.Split(axis, out_nums).shard(strategy1)
  41. self.mul = P.Mul().shard(strategy2)
  42. self.weight = Parameter(mul_weight, "w1")
  43. def construct(self, x):
  44. out1, out2 = self.split(self.weight)
  45. out = self.mul(x, out1)
  46. out = self.mul(out, out2)
  47. return out
  48. class Net2(nn.Cell):
  49. def __init__(self, mul_weight, axis=0, out_nums=1, strategy1=None, strategy2=None):
  50. super(Net2, self).__init__()
  51. self.split = P.Split(axis, out_nums).shard(strategy1)
  52. self.mul = P.Mul().shard(strategy2)
  53. self.weight = Parameter(mul_weight, "w1")
  54. def construct(self, x):
  55. out = self.mul(x, self.weight)
  56. out1, _ = self.split(out)
  57. return out1
  58. _w = Tensor(np.ones([48, 64]), dtype=ms.float32)
  59. _x = Tensor(np.ones([48, 64]), dtype=ms.float32)
  60. _w1 = Tensor(np.ones([96, 64, 32]), dtype=ms.float32)
  61. _x1 = Tensor(np.ones([48, 64, 32]), dtype=ms.float32)
  62. _w2 = Tensor(np.ones([48, 64, 32]), dtype=ms.float32)
  63. def compile_net(net):
  64. context.set_context(mode=context.GRAPH_MODE, save_graphs=True)
  65. optimizer = Momentum(net.trainable_params(), learning_rate=0.1, momentum=0.9)
  66. train_net = TrainOneStepCell(net, optimizer)
  67. train_net.set_auto_parallel()
  68. train_net.set_train()
  69. _executor.compile(train_net, _x)
  70. context.reset_auto_parallel_context()
  71. def compile_net1(net):
  72. context.set_context(mode=context.GRAPH_MODE, save_graphs=True)
  73. optimizer = Momentum(net.trainable_params(), learning_rate=0.1, momentum=0.9)
  74. train_net = TrainOneStepCell(net, optimizer)
  75. train_net.set_auto_parallel()
  76. train_net.set_train()
  77. _executor.compile(train_net, _x1)
  78. context.reset_auto_parallel_context()
  79. def test_split_parameter():
  80. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  81. strategy1 = ((1, 4, 2),)
  82. strategy2 = ((1, 4, 2), (1, 4, 2))
  83. net = Net1(_w1, 0, 2, strategy1, strategy2)
  84. compile_net1(net)
  85. def test_split_parameter_no_full_split():
  86. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  87. strategy1 = ((1, 2, 2),)
  88. strategy2 = ((1, 4, 2), (1, 4, 2))
  89. net = Net1(_w1, 0, 2, strategy1, strategy2)
  90. compile_net1(net)
  91. def test_split_tensor():
  92. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  93. strategy1 = ((1, 8),)
  94. strategy2 = ((1, 8), (1, 8))
  95. strategy3 = ((1, 1), (1, 8))
  96. net = Net(_w, 0, 3, strategy1, strategy2, strategy3)
  97. compile_net(net)
  98. def test_split_output():
  99. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  100. strategy1 = ((1, 4, 2),)
  101. strategy2 = ((1, 4, 2), (1, 4, 2))
  102. net = Net2(_w2, 0, 2, strategy1, strategy2)
  103. compile_net1(net)
  104. def test_split_output_no_full_split():
  105. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  106. strategy1 = ((1, 2, 2),)
  107. strategy2 = ((1, 4, 2), (1, 4, 2))
  108. net = Net2(_w2, 0, 2, strategy1, strategy2)
  109. compile_net1(net)
  110. def test_split_no_strategy():
  111. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  112. strategy1 = None
  113. strategy2 = ((1, 4, 2), (1, 4, 2))
  114. net = Net2(_w2, 0, 2, strategy1, strategy2)
  115. compile_net1(net)
  116. def test_split_auto_parallel():
  117. context.set_auto_parallel_context(parallel_mode="auto_parallel", device_num=8, global_rank=0)
  118. net = Net2(_w2, 0, 2)
  119. compile_net1(net)