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_one_dev.py 3.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 re
  15. from mindspore.train import Model, ParallelMode
  16. from mindspore.nn.loss import SoftmaxCrossEntropyWithLogits
  17. from mindspore.nn.optim.momentum import Momentum
  18. from mindspore import Tensor
  19. import mindspore as ms
  20. import numpy as np
  21. from mindspore.ops import operations as P
  22. import mindspore.nn as nn
  23. from mindspore.common.parameter import Parameter
  24. from tests.dataset_mock import MindData
  25. from mindspore import context
  26. from mindspore.parallel._utils import _reset_op_id
  27. from mindspore.common.api import _executor
  28. context.set_context(mode=context.GRAPH_MODE)
  29. class Dataset(MindData):
  30. def __init__(self, predict, label, length=3):
  31. super(Dataset, self).__init__(size=length)
  32. self.predict = predict
  33. self.label = label
  34. self.index = 0
  35. self.length = length
  36. def __iter__(self):
  37. return self
  38. def __next__(self):
  39. if self.index >= self.length:
  40. raise StopIteration
  41. self.index += 1
  42. return self.predict, self.label
  43. def reset(self):
  44. self.index = 0
  45. class AllToAllNet(nn.Cell):
  46. def __init__(self):
  47. super(AllToAllNet, self).__init__()
  48. self.matmul = P.MatMul()
  49. self.matmul_weight = Parameter(Tensor(np.ones([128, 32]), dtype=ms.float32), name="weight")
  50. self.transpose1 = P.Transpose()
  51. def construct(self, x):
  52. x = self.matmul(x, self.matmul_weight)
  53. x = self.transpose1(x, (1, 0))
  54. return x
  55. def all_to_all_net():
  56. return AllToAllNet()
  57. def all_to_all_common():
  58. learning_rate = 0.1
  59. momentum = 0.9
  60. epoch_size = 2
  61. context.reset_auto_parallel_context()
  62. context.set_auto_parallel_context(parallel_mode=ParallelMode.AUTO_PARALLEL, device_num=1, global_rank=0)
  63. predict = Tensor(np.ones([32, 128]), dtype=ms.float32)
  64. label = Tensor(np.ones([32]), dtype=ms.int32)
  65. dataset = Dataset(predict, label, 2)
  66. net = all_to_all_net()
  67. loss = SoftmaxCrossEntropyWithLogits(is_grad=False, sparse=True)
  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_one_dev():
  74. _reset_op_id()
  75. strategies = all_to_all_common()
  76. for (k, v) in strategies.items():
  77. if re.search('SoftmaxCrossEntropyWithLogits-op', k) is not None:
  78. assert v == [[1, 1], [1, 1]]
  79. elif re.search('Transpose-op', k) is not None:
  80. assert v == [[1, 1]]
  81. elif re.search('MatMul-op', k) is not None:
  82. assert v == [[1, 1], [1, 1]]