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.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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.recompute()
  43. self.fc2.recompute()
  44. self.fc3.recompute()
  45. self.fc1.matmul.shard(((1, 1), (1, 8)))
  46. self.fc2.matmul.shard(((1, 1), (1, 8)))
  47. self.fc3.matmul.shard(((1, 1), (1, 8)))
  48. self.relu4 = nn.ReLU()
  49. self.relu5 = nn.ReLU()
  50. self.transpose = P.Transpose()
  51. self.matmul1 = P.MatMul()
  52. self.matmul2 = P.MatMul()
  53. self.matmul_cell = MatMulCell()
  54. self.matmul_cell.recompute()
  55. self.fc1.recompute(mp_comm_recompute=False)
  56. self.fc2.recompute(mp_comm_recompute=False)
  57. self.fc3.recompute(mp_comm_recompute=False)
  58. self.matmul_cell.recompute(mp_comm_recompute=False)
  59. def construct(self, x):
  60. x = self.matmul_cell(x)
  61. q = self.fc1(x)
  62. k = self.fc2(x)
  63. v = self.fc3(x)
  64. k = self.transpose(k, (1, 0))
  65. c = self.relu4(self.matmul1(q, k))
  66. s = self.relu5(self.matmul2(c, v))
  67. s = self.fc4(s)
  68. return s
  69. def test_dmnet_train_step():
  70. context.reset_auto_parallel_context()
  71. _Context().set_backend_policy("vm")
  72. context.set_context(mode=context.GRAPH_MODE)
  73. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8)
  74. input_ = Tensor(np.ones([64, 128]).astype(np.float32) * 0.01)
  75. label = Tensor(np.zeros([32, 768]).astype(np.float32))
  76. net = train_step_with_loss_warp(DenseMutMulNet())
  77. net.set_auto_parallel()
  78. net.set_train()
  79. _cell_graph_executor.compile(net, input_, label)
  80. _Context().set_backend_policy("ge")