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.py 5.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. """ test Communicate """
  15. import numpy as np
  16. from mindspore.ops.operations.comm_ops import AllReduce, AllGather, _AlltoAll, ReduceOp
  17. from mindspore.ops.operations.comm_ops import Broadcast
  18. from mindspore.communication.management import HCCL_WORLD_COMM_GROUP, NCCL_WORLD_COMM_GROUP, GlobalComm, init
  19. from mindspore.communication._comm_helper import Backend
  20. from mindspore import Tensor
  21. import mindspore.nn as nn
  22. from mindspore.ops.operations import Split
  23. from mindspore.common.api import _executor
  24. from mindspore.nn import Dense
  25. from mindspore.nn import ReLU
  26. from mindspore.nn import TrainOneStepCell, WithLossCell
  27. from mindspore.nn import Momentum
  28. import mindspore.context as context
  29. # pylint: disable=W0212
  30. # W0212: protected-access
  31. tag = 0
  32. init("hccl")
  33. class AllReduceNet(nn.Cell):
  34. """AllReduceNet definition"""
  35. def __init__(self, input_channel, out_channel, op):
  36. super(AllReduceNet, self).__init__()
  37. self.dense = Dense(input_channel, out_channel)
  38. self.reduce = AllReduce(op)
  39. self.relu = ReLU()
  40. def construct(self, x):
  41. x = self.dense(x)
  42. x = self.reduce(x)
  43. return self.relu(x)
  44. class BroadCastNet(nn.Cell):
  45. """BroadCastNet definition"""
  46. def __init__(self, input_channel, out_channel):
  47. super(BroadCastNet, self).__init__()
  48. self.dense = Dense(input_channel, out_channel)
  49. self.broadcast = Broadcast(0)
  50. def construct(self, x):
  51. x, = self.broadcast((x,))
  52. x = self.dense(x)
  53. return x
  54. class AllGatherNet(nn.Cell):
  55. """AllGatherNet definition"""
  56. def __init__(self, input_channel, out_channel):
  57. super(AllGatherNet, self).__init__()
  58. self.dense = Dense(input_channel, out_channel)
  59. if GlobalComm.BACKEND is Backend.HCCL:
  60. self.allgather = AllGather(group=HCCL_WORLD_COMM_GROUP)
  61. elif GlobalComm.BACKEND is Backend.NCCL:
  62. self.allgather = AllGather(group=NCCL_WORLD_COMM_GROUP)
  63. else:
  64. self.allgather = AllGather()
  65. self.relu = ReLU()
  66. def construct(self, x):
  67. x = self.dense(x)
  68. x = self.allgather(x)
  69. return self.relu(x)
  70. class AlltoAllNet(nn.Cell):
  71. """AlltoAllNet definition"""
  72. def __init__(self, input_channel, out_channel):
  73. super(AlltoAllNet, self).__init__()
  74. self.dense = Dense(input_channel, out_channel)
  75. self.alltoall = _AlltoAll(1, 0, 1)
  76. self.relu = ReLU()
  77. def construct(self, x):
  78. x = self.dense(x)
  79. x = self.alltoall(x)
  80. return self.relu(x)
  81. def run_allreduce(op):
  82. """run_allreduce"""
  83. context.set_context(mode=context.GRAPH_MODE)
  84. input_tensor = Tensor(np.array([[1.2, 2.1], [2.2, 3.2]], dtype=np.float32))
  85. label_tensor = Tensor(np.array([[1.2], [2.2]], dtype=np.float32))
  86. network = AllReduceNet(2, 1, op)
  87. loss_fn = nn.SoftmaxCrossEntropyWithLogits()
  88. optimizer = Momentum(filter(lambda x: x.requires_grad, network.get_parameters()),
  89. learning_rate=0.1,
  90. momentum=0.9)
  91. network = WithLossCell(network, loss_fn)
  92. network = TrainOneStepCell(network, optimizer)
  93. _executor.compile(network, input_tensor, label_tensor)
  94. def test_allreduce():
  95. """test_allreduce"""
  96. context.set_context(mode=context.GRAPH_MODE)
  97. run_allreduce(ReduceOp.SUM)
  98. run_allreduce(ReduceOp.MAX)
  99. run_allreduce(ReduceOp.MIN)
  100. def test_allgather():
  101. """test_allgather"""
  102. context.set_context(mode=context.GRAPH_MODE)
  103. input_tensor = Tensor(np.array([[1.2, 2.1], [2.2, 3.2]], dtype=np.float32))
  104. label_tensor = Tensor(np.array([[1.2], [2.2]], dtype=np.float32))
  105. network = AllGatherNet(2, 1)
  106. loss_fn = nn.SoftmaxCrossEntropyWithLogits()
  107. optimizer = Momentum(filter(lambda x: x.requires_grad, network.get_parameters()),
  108. learning_rate=0.1,
  109. momentum=0.9)
  110. network = WithLossCell(network, loss_fn)
  111. network = TrainOneStepCell(network, optimizer)
  112. _executor.compile(network, input_tensor, label_tensor)
  113. def test_broadcast():
  114. """test_broadcast"""
  115. context.set_context(mode=context.GRAPH_MODE)
  116. input_tensor_1 = Tensor(np.array([[1.2, 2.1], [2.2, 3.2]], dtype=np.float32))
  117. label_tensor = Tensor(np.array([[1.2], [2.2]], dtype=np.float32))
  118. network = BroadCastNet(2, 1)
  119. loss_fn = nn.SoftmaxCrossEntropyWithLogits()
  120. optimizer = Momentum(filter(lambda x: x.requires_grad, network.get_parameters()),
  121. learning_rate=0.1,
  122. momentum=0.9)
  123. network = WithLossCell(network, loss_fn)
  124. network = TrainOneStepCell(network, optimizer)
  125. _executor.compile(network, input_tensor_1, label_tensor)
  126. def test_alltoall():
  127. """test_alltoall"""
  128. context.set_context(mode=context.GRAPH_MODE)
  129. input_tensor = Tensor(np.array([[1.2, 2.1], [2.2, 3.2]], dtype=np.float32))
  130. label_tensor = Tensor(np.array([[1.2], [2.2]], dtype=np.float32))
  131. network = AlltoAllNet(2, 1)
  132. loss_fn = nn.SoftmaxCrossEntropyWithLogits()
  133. optimizer = Momentum(filter(lambda x: x.requires_grad, network.get_parameters()),
  134. learning_rate=0.1,
  135. momentum=0.9)
  136. network = WithLossCell(network, loss_fn)
  137. network = TrainOneStepCell(network, optimizer)
  138. _executor.compile(network, input_tensor, label_tensor)