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_alltoall.py 4.0 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # Copyright 2019 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. import mindspore.nn as nn
  17. from mindspore import Tensor
  18. from mindspore import context
  19. from mindspore.common.api import _executor
  20. from mindspore.common.parameter import Parameter
  21. from mindspore.nn.loss import SoftmaxCrossEntropyWithLogits
  22. from mindspore.nn.optim.momentum import Momentum
  23. from mindspore.ops import operations as P
  24. from mindspore.parallel._utils import _reset_op_id
  25. from mindspore.train import Model, ParallelMode
  26. from tests.dataset_mock import MindData
  27. class Dataset(MindData):
  28. def __init__(self, predict, label, length=3):
  29. super(Dataset, self).__init__(size=length)
  30. self.predict = predict
  31. self.label = label
  32. self.index = 0
  33. self.length = length
  34. def __iter__(self):
  35. return self
  36. def __next__(self):
  37. if self.index >= self.length:
  38. raise StopIteration
  39. self.index += 1
  40. return self.predict, self.label
  41. def reset(self):
  42. self.index = 0
  43. class AllToAllNet(nn.Cell):
  44. def __init__(self, strategy1):
  45. super(AllToAllNet, self).__init__()
  46. self.matmul = P.MatMul().set_strategy(((1, 1), (1, 8)))
  47. self.matmul_weight = Parameter(Tensor(np.ones([128, 256]), dtype=ms.float32), name="weight")
  48. self.transpose1 = P.Transpose().set_strategy(strategy1)
  49. def construct(self, x):
  50. x = self.matmul(x, self.matmul_weight)
  51. x = self.transpose1(x, (1, 0))
  52. return x
  53. def all_to_all_net(strategy1):
  54. return AllToAllNet(strategy1=strategy1)
  55. def all_to_all_common(strategy1):
  56. learning_rate = 0.1
  57. momentum = 0.9
  58. epoch_size = 2
  59. context.reset_auto_parallel_context()
  60. context.set_auto_parallel_context(parallel_mode=ParallelMode.SEMI_AUTO_PARALLEL, device_num=8)
  61. predict = Tensor(np.ones([32, 128]), dtype=ms.float32)
  62. label = Tensor(np.ones([32]), dtype=ms.int32)
  63. dataset = Dataset(predict, label, 2)
  64. net = all_to_all_net(strategy1)
  65. loss = SoftmaxCrossEntropyWithLogits(is_grad=False, sparse=True)
  66. loss.softmax_cross_entropy.set_strategy(((8, 1), (8, 1)))
  67. loss.one_hot.set_strategy(((8, 1), (), ()))
  68. opt = Momentum(net.trainable_params(), learning_rate, momentum)
  69. model = Model(net, loss, opt)
  70. model.train(epoch_size, dataset, dataset_sink_mode=False)
  71. strategys = _executor._get_strategy(model._train_network)
  72. return strategys
  73. def test_all_to_all():
  74. strategy1 = ((8, 1),)
  75. context.set_context(mode=context.GRAPH_MODE, save_graphs=False)
  76. _reset_op_id()
  77. strategys = all_to_all_common(strategy1)
  78. print(strategys)
  79. expect_dict = {'Default/network-_VirtualDatasetCell/_backbone-WithLossCell/_loss_fn-SoftmaxCrossEntropyWithLogits'
  80. '/SoftmaxCrossEntropyWithLogits-op3': [[8, 1], [8, 1]],
  81. 'Default/network-_VirtualDatasetCell/_backbone-WithLossCell/_loss_fn-SoftmaxCrossEntropyWithLogits/'
  82. 'OneHot-op4': [[8, 1], [], []],
  83. 'Default/network-_VirtualDatasetCell/_backbone-WithLossCell/_backbone-AllToAllNet/Transpose-op1': [
  84. [8, 1]],
  85. 'Default/network-_VirtualDatasetCell/_backbone-WithLossCell/_backbone-AllToAllNet/MatMul-op0': [
  86. [1, 1], [1, 8]]}
  87. assert strategys == expect_dict
  88. context.set_context(save_graphs=False)
  89. if __name__ == '__main__':
  90. test_all_to_all()