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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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.nn as nn
  16. import mindspore as ms
  17. from mindspore import Tensor, context, Parameter
  18. from mindspore.common.api import _cell_graph_executor
  19. from mindspore.ops import operations as P
  20. from mindspore.common.initializer import initializer
  21. from mindspore.context import _Context
  22. from ....train_step_wrap import train_step_with_loss_warp
  23. class MatMulCell(nn.Cell):
  24. def __init__(self):
  25. super(MatMulCell, self).__init__()
  26. self.reshape = P.Reshape()
  27. self.matmul0 = P.MatMul()
  28. self.weight = Parameter(initializer("ones", [128, 64], ms.float32), name="weight")
  29. self.relu = P.ReLU().shard(((1, 8),))
  30. def construct(self, x):
  31. x = self.matmul0(x, self.weight)
  32. x = self.reshape(x, (32, 128))
  33. x = self.relu(x)
  34. return x
  35. class DenseMutMulNet(nn.Cell):
  36. def __init__(self):
  37. super(DenseMutMulNet, self).__init__()
  38. self.fc1 = nn.Dense(128, 768, activation='relu')
  39. self.fc2 = nn.Dense(128, 768, activation='relu')
  40. self.fc3 = nn.Dense(128, 768, activation='relu')
  41. self.fc4 = nn.Dense(768, 768, activation='relu')
  42. self.fc1.matmul.shard(((1, 1), (1, 8)))
  43. self.fc2.matmul.shard(((1, 1), (1, 8)))
  44. self.fc3.matmul.shard(((1, 1), (1, 8)))
  45. self.relu4 = nn.ReLU()
  46. self.relu5 = nn.ReLU()
  47. self.transpose = P.Transpose()
  48. self.matmul1 = P.MatMul()
  49. self.matmul2 = P.MatMul()
  50. self.matmul_cell = MatMulCell()
  51. self.fc1.recompute(mp_comm_recompute=False)
  52. self.fc2.recompute(mp_comm_recompute=False)
  53. self.fc3.recompute(mp_comm_recompute=False)
  54. self.matmul_cell.recompute(mp_comm_recompute=False)
  55. def construct(self, x):
  56. x = self.matmul_cell(x)
  57. q = self.fc1(x)
  58. k = self.fc2(x)
  59. v = self.fc3(x)
  60. k = self.transpose(k, (1, 0))
  61. c = self.relu4(self.matmul1(q, k))
  62. s = self.relu5(self.matmul2(c, v))
  63. s = self.fc4(s)
  64. return s
  65. def test_dmnet_train_step():
  66. context.reset_auto_parallel_context()
  67. _Context().set_backend_policy("vm")
  68. context.set_context(mode=context.GRAPH_MODE)
  69. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8)
  70. input_ = Tensor(np.ones([64, 128]).astype(np.float32) * 0.01)
  71. label = Tensor(np.zeros([32, 768]).astype(np.float32))
  72. net = train_step_with_loss_warp(DenseMutMulNet())
  73. net.set_auto_parallel()
  74. net.set_train()
  75. _cell_graph_executor.compile(net, input_, label)
  76. _Context().set_backend_policy("ge")