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_gatherd.py 2.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # Copyright 2021 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 _cell_graph_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, dim, index, strategy1=None, strategy2=None):
  22. super().__init__()
  23. self.gatherd = P.GatherD().shard(strategy1)
  24. self.neg = P.Neg().shard(strategy2)
  25. self.input = Parameter(index, "w1")
  26. self.dim = dim
  27. def construct(self, x, b):
  28. out = self.gatherd(self.input, self.dim, x)
  29. out = self.neg(out)
  30. return out
  31. _x = Tensor(np.ones([16, 32, 64]), dtype=ms.int32)
  32. _w1 = Tensor(np.ones([16, 32, 64]), dtype=ms.float32)
  33. _b = Tensor(np.ones([16, 32, 64]), dtype=ms.float32)
  34. def compile_net(net):
  35. optimizer = Momentum(net.trainable_params(), learning_rate=0.1, momentum=0.9)
  36. train_net = TrainOneStepCell(net, optimizer)
  37. train_net.set_auto_parallel()
  38. train_net.set_train()
  39. _cell_graph_executor.compile(train_net, _x, _b)
  40. context.reset_auto_parallel_context()
  41. def test_gathernd_dim0():
  42. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=16, global_rank=0)
  43. strategy1 = ((1, 2, 8), (1, 2, 8))
  44. strategy2 = ((1, 2, 8),)
  45. net = Net(0, _w1, strategy1, strategy2)
  46. compile_net(net)
  47. def test_gathernd_dim2():
  48. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=16, global_rank=0)
  49. strategy1 = ((2, 8, 1), (2, 8, 1))
  50. strategy2 = ((2, 8, 1),)
  51. net = Net(2, _w1, strategy1, strategy2)
  52. compile_net(net)
  53. def test_gathernd_dim2_default_batch_parallel():
  54. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=16, global_rank=0)
  55. strategy1 = None
  56. strategy2 = ((2, 8, 1),)
  57. net = Net(2, _w1, strategy1, strategy2)
  58. compile_net(net)
  59. def test_gathernd_repeat_calc():
  60. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=16, global_rank=0)
  61. strategy1 = ((1, 2, 4), (1, 2, 4))
  62. strategy2 = ((1, 2, 4),)
  63. net = Net(0, _w1, strategy1, strategy2)
  64. compile_net(net)