2020-03-27 14:49:12 +08:00
|
|
|
# Copyright 2019 Huawei Technologies Co., Ltd
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
# ============================================================================
|
|
|
|
import numpy as np
|
2020-05-18 16:42:35 +08:00
|
|
|
|
2020-03-27 14:49:12 +08:00
|
|
|
import mindspore.context as context
|
2020-04-22 16:44:19 +08:00
|
|
|
import mindspore.nn as nn
|
|
|
|
from mindspore import Tensor
|
2020-03-27 14:49:12 +08:00
|
|
|
from mindspore.common.initializer import initializer
|
|
|
|
from mindspore.common.parameter import Parameter
|
|
|
|
from mindspore.communication.management import init, NCCL_WORLD_COMM_GROUP, get_rank, get_group_size
|
2020-05-18 16:42:35 +08:00
|
|
|
from mindspore.ops import operations as P
|
2020-04-22 16:44:19 +08:00
|
|
|
|
2020-03-27 14:49:12 +08:00
|
|
|
context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
|
|
|
|
|
2020-08-27 15:11:02 +08:00
|
|
|
init()
|
2020-03-27 14:49:12 +08:00
|
|
|
rank = get_rank()
|
|
|
|
size = get_group_size()
|
2020-04-22 16:44:19 +08:00
|
|
|
x = np.ones([1, 1, 3, 3]).astype(np.float32) * 0.01 * (rank + 1)
|
|
|
|
|
2020-03-27 14:49:12 +08:00
|
|
|
|
|
|
|
class Net(nn.Cell):
|
2020-04-22 16:44:19 +08:00
|
|
|
def __init__(self):
|
2020-03-27 14:49:12 +08:00
|
|
|
super(Net, self).__init__()
|
|
|
|
self.all_gather = P.AllGather(group=NCCL_WORLD_COMM_GROUP)
|
|
|
|
self.x = Parameter(initializer(Tensor(x), x.shape), name='x')
|
|
|
|
|
|
|
|
def construct(self):
|
|
|
|
return self.all_gather(self.x)
|
|
|
|
|
2020-04-22 16:44:19 +08:00
|
|
|
|
2020-03-27 14:49:12 +08:00
|
|
|
def test_AllGather():
|
|
|
|
all_gather = Net()
|
|
|
|
output = all_gather()
|
|
|
|
|
|
|
|
expect = np.ones([1, 1, 3, 3]).astype(np.float32) * 0.01 * (0 + 1)
|
|
|
|
for i in range(size - 1):
|
|
|
|
tmp = np.ones([1, 1, 3, 3]).astype(np.float32) * 0.01 * (i + 2)
|
|
|
|
expect = np.concatenate((expect, tmp))
|
2020-11-03 20:51:30 +08:00
|
|
|
diff = np.absolute(output.asnumpy() - expect)
|
2020-03-27 14:49:12 +08:00
|
|
|
error = np.ones(shape=expect.shape) * 1.0e-5
|
|
|
|
assert np.all(diff < error)
|
2020-06-09 12:18:51 +08:00
|
|
|
assert output.shape == expect.shape
|