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

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