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_auto_parallel_onehot.py 4.2 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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.optim.momentum import Momentum
  22. from mindspore.ops import composite as C
  23. from mindspore.ops import operations as P
  24. from mindspore.train import Model, ParallelMode
  25. from tests.dataset_mock import MindData
  26. from tests.ut.python.ops.test_math_ops import VirtualLoss
  27. context.set_context(mode=context.GRAPH_MODE)
  28. class Dataset(MindData):
  29. def __init__(self, predict, label, length=3):
  30. super(Dataset, self).__init__(size=length)
  31. self.predict = predict
  32. self.label = label
  33. self.index = 0
  34. self.length = length
  35. def __iter__(self):
  36. return self
  37. def __next__(self):
  38. if self.index >= self.length:
  39. raise StopIteration
  40. self.index += 1
  41. return self.predict, self.label
  42. def reset(self):
  43. self.index = 0
  44. class NetWithLoss(nn.Cell):
  45. def __init__(self, network):
  46. super(NetWithLoss, self).__init__()
  47. self.loss = VirtualLoss()
  48. self.network = network
  49. def construct(self, x, y, b):
  50. predict = self.network(x, y, b)
  51. return self.loss(predict)
  52. class GradWrap(nn.Cell):
  53. def __init__(self, network):
  54. super(GradWrap, self).__init__()
  55. self.network = network
  56. def construct(self, x, y, b):
  57. return C.grad_all(self.network)(x, y, b)
  58. def test_auto_parallel_arithmetic():
  59. class Net(nn.Cell):
  60. def __init__(self):
  61. super().__init__()
  62. self.matmul = P.MatMul()
  63. self.one_hot = P.OneHot()
  64. self.on_value = Tensor(1.0, ms.float32)
  65. self.off_value = Tensor(0.0, ms.float32)
  66. self.matmul2 = P.MatMul()
  67. def construct(self, x, y, b):
  68. out = self.matmul(x, y)
  69. out1 = self.one_hot(b, 64, self.on_value, self.off_value)
  70. out2 = self.matmul2(out, out1)
  71. return out2
  72. context.set_auto_parallel_context(device_num=8, global_rank=0)
  73. net = GradWrap(NetWithLoss(Net()))
  74. context.set_auto_parallel_context(parallel_mode="auto_parallel")
  75. net.set_auto_parallel()
  76. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  77. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  78. b = Tensor(np.ones([64]), dtype=ms.int32)
  79. _executor.compile(net, x, y, b)
  80. def test_auto_parallel_arithmetic_model():
  81. class NetOneHot(nn.Cell):
  82. def __init__(self):
  83. super().__init__()
  84. self.matmul = P.MatMul()
  85. self.one_hot = P.OneHot().set_strategy(((1, 8), (), ()))
  86. self.on_value = Tensor(1.0, ms.float32)
  87. self.off_value = Tensor(0.0, ms.float32)
  88. self.matmul2 = P.MatMul()
  89. self.w = Parameter(Tensor(np.zeros([32, 64]).astype(np.float32)), "weight", requires_grad=True)
  90. def construct(self, x, b):
  91. out = self.matmul(x, self.w)
  92. out1 = self.one_hot(b, 64, self.on_value, self.off_value)
  93. out2 = self.matmul2(out, out1)
  94. return out2
  95. context.reset_auto_parallel_context()
  96. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode=ParallelMode.AUTO_PARALLEL)
  97. net = NetOneHot()
  98. x = Tensor(np.ones([8, 32]), dtype=ms.float32)
  99. b = Tensor(np.ones([8]), dtype=ms.int32)
  100. dataset = Dataset(x, b, 2)
  101. opt = Momentum(net.trainable_params(), 0.1, 0.9)
  102. model = Model(net, optimizer=opt)
  103. model.train(2, dataset, dataset_sink_mode=False)