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 4.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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(transpose_b=True)
  28. self.weight = Parameter(initializer("ones", [64, 128], 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, mp_comm_recompute=True, recompute_slice_activation=False):
  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), (8, 1)))
  43. self.fc2.matmul.shard(((1, 1), (8, 1)))
  44. self.fc3.matmul.shard(((1, 1), (8, 1)))
  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=mp_comm_recompute, recompute_slice_activation=recompute_slice_activation)
  52. self.fc2.recompute(mp_comm_recompute=mp_comm_recompute, recompute_slice_activation=recompute_slice_activation)
  53. self.fc3.recompute(mp_comm_recompute=mp_comm_recompute, recompute_slice_activation=recompute_slice_activation)
  54. self.matmul_cell.recompute(mp_comm_recompute=mp_comm_recompute,
  55. recompute_slice_activation=recompute_slice_activation)
  56. def construct(self, x):
  57. x = self.matmul_cell(x)
  58. q = self.fc1(x)
  59. k = self.fc2(x)
  60. v = self.fc3(x)
  61. k = self.transpose(k, (1, 0))
  62. c = self.relu4(self.matmul1(q, k))
  63. s = self.relu5(self.matmul2(c, v))
  64. s = self.fc4(s)
  65. return s
  66. def compile_net(mp_comm_recompute, recompute_slice_activation):
  67. context.reset_auto_parallel_context()
  68. _Context().set_backend_policy("vm")
  69. context.set_context(mode=context.GRAPH_MODE)
  70. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8)
  71. input_ = Tensor(np.ones([64, 128]).astype(np.float32) * 0.01)
  72. label = Tensor(np.zeros([32, 768]).astype(np.float32))
  73. net = train_step_with_loss_warp(DenseMutMulNet(mp_comm_recompute=mp_comm_recompute,
  74. recompute_slice_activation=recompute_slice_activation))
  75. net.set_auto_parallel()
  76. net.set_train()
  77. _cell_graph_executor.compile(net, input_, label)
  78. _Context().set_backend_policy("ge")
  79. def test_dmnet_train_step_mp_recompute():
  80. """
  81. Feature: test recompute interface.
  82. Description: test model parallel communication not recompute.
  83. Expectation: compile without error.
  84. """
  85. compile_net(False, False)
  86. def test_dmnet_train_step_recompute_activation_slice():
  87. """
  88. Feature: test recompute interface.
  89. Description: test slicing recompute cell output.
  90. Expectation: compile without error.
  91. """
  92. compile_net(True, True)
  93. def test_dmnet_train_step_mp_recompute_recompute_activation_slice():
  94. """
  95. Feature: test recompute interface.
  96. Description: test model parallel communication not recompute and slicing recompute cell output.
  97. Expectation: compile without error.
  98. """
  99. compile_net(False, True)