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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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, ReduceScatter
  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 ReduceScatterNet(nn.Cell):
  71. """ReduceScatterNet definition"""
  72. def __init__(self, input_channel, out_channel, op):
  73. super(ReduceScatterNet, self).__init__()
  74. self.dense = Dense(input_channel, out_channel)
  75. self.reducescatter = ReduceScatter(op)
  76. self.relu = ReLU()
  77. def construct(self, x):
  78. x = self.dense(x)
  79. x = self.reducescatter(x)
  80. return self.relu(x)
  81. class AlltoAllNet(nn.Cell):
  82. """AlltoAllNet definition"""
  83. def __init__(self, input_channel, out_channel):
  84. super(AlltoAllNet, self).__init__()
  85. self.dense = Dense(input_channel, out_channel)
  86. self.alltoall = _AlltoAll(1, 0, 1)
  87. self.relu = ReLU()
  88. def construct(self, x):
  89. x = self.dense(x)
  90. x = self.alltoall(x)
  91. return self.relu(x)
  92. def run_allreduce(op):
  93. """run_allreduce"""
  94. context.set_context(mode=context.GRAPH_MODE)
  95. input_tensor = Tensor(np.array([[1.2, 2.1], [2.2, 3.2]], dtype=np.float32))
  96. label_tensor = Tensor(np.array([[1.2], [2.2]], dtype=np.float32))
  97. network = AllReduceNet(2, 1, op)
  98. loss_fn = nn.SoftmaxCrossEntropyWithLogits()
  99. optimizer = Momentum(filter(lambda x: x.requires_grad, network.get_parameters()),
  100. learning_rate=0.1,
  101. momentum=0.9)
  102. network = WithLossCell(network, loss_fn)
  103. network = TrainOneStepCell(network, optimizer)
  104. _executor.compile(network, input_tensor, label_tensor)
  105. def test_allreduce():
  106. """test_allreduce"""
  107. context.set_context(mode=context.GRAPH_MODE)
  108. run_allreduce(ReduceOp.SUM)
  109. run_allreduce(ReduceOp.MAX)
  110. run_allreduce(ReduceOp.MIN)
  111. def test_allgather():
  112. """test_allgather"""
  113. context.set_context(mode=context.GRAPH_MODE)
  114. input_tensor = Tensor(np.array([[1.2, 2.1], [2.2, 3.2]], dtype=np.float32))
  115. label_tensor = Tensor(np.array([[1.2], [2.2]], dtype=np.float32))
  116. network = AllGatherNet(2, 1)
  117. loss_fn = nn.SoftmaxCrossEntropyWithLogits()
  118. optimizer = Momentum(filter(lambda x: x.requires_grad, network.get_parameters()),
  119. learning_rate=0.1,
  120. momentum=0.9)
  121. network = WithLossCell(network, loss_fn)
  122. network = TrainOneStepCell(network, optimizer)
  123. _executor.compile(network, input_tensor, label_tensor)
  124. def run_reducescatter(op):
  125. """run_reducescatter"""
  126. context.set_context(mode=context.GRAPH_MODE)
  127. input_tensor = Tensor(np.array([[1.2, 2.1], [2.2, 3.2]], dtype=np.float32))
  128. label_tensor = Tensor(np.array([[1.2], [2.2]], dtype=np.float32))
  129. network = ReduceScatterNet(2, 1, op)
  130. loss_fn = nn.SoftmaxCrossEntropyWithLogits()
  131. optimizer = Momentum(filter(lambda x: x.requires_grad, network.get_parameters()),
  132. learning_rate=0.1,
  133. momentum=0.9)
  134. network = WithLossCell(network, loss_fn)
  135. network = TrainOneStepCell(network, optimizer)
  136. _executor.compile(network, input_tensor, label_tensor)
  137. def test_reducescatter():
  138. """test_reducescatter"""
  139. context.set_context(mode=context.GRAPH_MODE)
  140. run_reducescatter(ReduceOp.SUM)
  141. def test_broadcast():
  142. """test_broadcast"""
  143. context.set_context(mode=context.GRAPH_MODE)
  144. input_tensor_1 = Tensor(np.array([[1.2, 2.1], [2.2, 3.2]], dtype=np.float32))
  145. label_tensor = Tensor(np.array([[1.2], [2.2]], dtype=np.float32))
  146. network = BroadCastNet(2, 1)
  147. loss_fn = nn.SoftmaxCrossEntropyWithLogits()
  148. optimizer = Momentum(filter(lambda x: x.requires_grad, network.get_parameters()),
  149. learning_rate=0.1,
  150. momentum=0.9)
  151. network = WithLossCell(network, loss_fn)
  152. network = TrainOneStepCell(network, optimizer)
  153. _executor.compile(network, input_tensor_1, label_tensor)
  154. def test_alltoall():
  155. """test_alltoall"""
  156. context.set_context(mode=context.GRAPH_MODE)
  157. input_tensor = Tensor(np.array([[1.2, 2.1], [2.2, 3.2]], dtype=np.float32))
  158. label_tensor = Tensor(np.array([[1.2], [2.2]], dtype=np.float32))
  159. network = AlltoAllNet(2, 1)
  160. loss_fn = nn.SoftmaxCrossEntropyWithLogits()
  161. optimizer = Momentum(filter(lambda x: x.requires_grad, network.get_parameters()),
  162. learning_rate=0.1,
  163. momentum=0.9)
  164. network = WithLossCell(network, loss_fn)
  165. network = TrainOneStepCell(network, optimizer)
  166. _executor.compile(network, input_tensor, label_tensor)