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_manual_gatherv2.py 2.7 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. from mindspore.common.initializer import initializer
  22. class Net(Cell):
  23. def __init__(self, strategy1=None, strategy2=None, strategy3=None):
  24. super().__init__()
  25. self.gatherv2 = P.GatherV2().set_strategy(strategy1)
  26. self.gatherv2.add_prim_attr("manual_split", ((1, 0), (7, 1)))
  27. self.mul = P.Mul().set_strategy(strategy2)
  28. self.reshape = P.Reshape()
  29. self.matmul = P.MatMul().set_strategy(strategy3)
  30. self.matmul.add_prim_attr("forward_reduce_scatter", True)
  31. self.param = Parameter(initializer("ones", (8, 64), ms.float32), name="gatherv2_param")
  32. self.mul_weight = Parameter(initializer("ones", (2, 4, 64), ms.float32), name="mul_weight")
  33. self.matmul_weight = Parameter(initializer("ones", (256, 16), ms.float32), name="matmul_weight")
  34. def construct(self, x, b):
  35. out = self.gatherv2(self.param, x, 0)
  36. out = self.mul(out, self.mul_weight)
  37. out = self.reshape(out, (2, 256))
  38. out = self.matmul(out, self.matmul_weight)
  39. return out
  40. _x = Tensor(np.ones([2, 4]), dtype=ms.int32)
  41. _b = Tensor(np.ones([64, 8]), dtype=ms.float32)
  42. def compile_net(net):
  43. optimizer = Momentum(net.trainable_params(), learning_rate=0.1, momentum=0.9)
  44. train_net = TrainOneStepCell(net, optimizer)
  45. train_net.set_auto_parallel()
  46. _executor.compile(train_net, _x, _b)
  47. context.reset_auto_parallel_context()
  48. def test_neg_data_parallel():
  49. context.set_context(save_graphs=True)
  50. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=2, global_rank=0)
  51. strategy1 = ((2, 1), (1, 2))
  52. strategy2 = ((1, 2, 1), (1, 2, 1))
  53. strategy3 = ((1, 2), (2, 1))
  54. net = Net(strategy1, strategy2, strategy3)
  55. compile_net(net)