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_full_batch.py 3.0 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. 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.parameter import Parameter
  20. from mindspore.nn.loss import SoftmaxCrossEntropyWithLogits
  21. from mindspore.nn.optim.momentum import Momentum
  22. from mindspore.ops import operations as P
  23. from mindspore.parallel._utils import _reset_op_id
  24. from mindspore.train import Model, ParallelMode
  25. from tests.dataset_mock import MindData
  26. class Dataset(MindData):
  27. def __init__(self, predict, label, length=3):
  28. super(Dataset, self).__init__(size=length)
  29. self.predict = predict
  30. self.label = label
  31. self.index = 0
  32. self.length = length
  33. def __iter__(self):
  34. return self
  35. def __next__(self):
  36. if self.index >= self.length:
  37. raise StopIteration
  38. self.index += 1
  39. return self.predict, self.label
  40. def reset(self):
  41. self.index = 0
  42. class AllToAllNet(nn.Cell):
  43. def __init__(self, strategy1):
  44. super(AllToAllNet, self).__init__()
  45. self.matmul = P.MatMul().set_strategy(((1, 1), (1, 8)))
  46. self.matmul_weight = Parameter(Tensor(np.ones([128, 256]), dtype=ms.float32), name="weight")
  47. self.transpose1 = P.Transpose().set_strategy(strategy1)
  48. def construct(self, x):
  49. x = self.matmul(x, self.matmul_weight)
  50. x = self.transpose1(x, (1, 0))
  51. return x
  52. def all_to_all_net(strategy1):
  53. return AllToAllNet(strategy1=strategy1)
  54. def all_to_all_common(strategy1):
  55. learning_rate = 0.1
  56. momentum = 0.9
  57. epoch_size = 2
  58. context.set_context(mode=context.GRAPH_MODE, save_graphs=False)
  59. context.reset_auto_parallel_context()
  60. context.set_auto_parallel_context(parallel_mode=ParallelMode.SEMI_AUTO_PARALLEL, device_num=8, full_batch=True)
  61. predict = Tensor(np.ones([256, 128]), dtype=ms.float32)
  62. label = Tensor(np.ones([256]), 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. def test_all_to_all():
  72. strategy1 = ((8, 1),)
  73. _reset_op_id()
  74. all_to_all_common(strategy1)