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_data_parallel_dense.py 2.6 kB

5 years ago
5 years ago
5 years ago
5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. """
  15. @File : test_data_parallel_dense.py
  16. @Desc : test data parallel dense
  17. """
  18. import numpy as np
  19. import mindspore.context as context
  20. import mindspore.nn as nn
  21. from mindspore import Tensor
  22. from mindspore.common.api import _executor
  23. from mindspore.nn import Momentum
  24. from mindspore.nn import TrainOneStepCell, WithLossCell
  25. from mindspore.ops import operations as P
  26. from mindspore.context import ParallelMode
  27. class DenseMMNet(nn.Cell):
  28. """DenseMMNet definition"""
  29. def __init__(self):
  30. super(DenseMMNet, self).__init__()
  31. self.fc1 = nn.Dense(128, 768, activation='relu')
  32. self.fc2 = nn.Dense(128, 768, activation='relu')
  33. self.fc3 = nn.Dense(128, 768, activation='relu')
  34. self.fc4 = nn.Dense(768, 768, activation='relu')
  35. self.relu4 = nn.ReLU()
  36. self.relu5 = nn.ReLU()
  37. self.transpose = P.Transpose()
  38. self.matmul1 = P.MatMul()
  39. self.matmul2 = P.MatMul()
  40. def construct(self, x):
  41. q = self.fc1(x)
  42. k = self.fc2(x)
  43. v = self.fc3(x)
  44. k = self.transpose(k, (1, 0))
  45. c = self.relu4(self.matmul1(q, k))
  46. s = self.relu5(self.matmul2(c, v))
  47. s = self.fc4(s)
  48. return s
  49. def test_data_parallel_dense():
  50. """test_data_parallel_dense"""
  51. context.set_context(mode=context.GRAPH_MODE)
  52. context.reset_auto_parallel_context()
  53. context.set_auto_parallel_context(parallel_mode=ParallelMode.DATA_PARALLEL, gradients_mean=True, device_num=8)
  54. inp = Tensor(np.ones([32, 128]).astype(np.float32) * 0.01)
  55. label = Tensor(np.zeros([32, 768]).astype(np.float32))
  56. net = DenseMMNet()
  57. loss_fn = nn.SoftmaxCrossEntropyWithLogits()
  58. optimizer = Momentum(filter(lambda x: x.requires_grad, net.get_parameters()),
  59. learning_rate=0.1,
  60. momentum=0.9)
  61. net = WithLossCell(net, loss_fn)
  62. net = TrainOneStepCell(net, optimizer)
  63. _executor.compile(net, inp, label)
  64. context.reset_auto_parallel_context()