• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2021 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# ============================================================================
15""" test scatter update """
16import numpy as np
17import pytest
18import mindspore.nn as nn
19from mindspore import Tensor, Model, Parameter
20from mindspore.ops import operations as P
21from mindspore import context
22
23
24class Net(nn.Cell):
25    """Net definition"""
26    def __init__(self, strategy1=None, strategy2=None):
27        super(Net, self).__init__()
28        self.inputs = Parameter(Tensor(np.ones([32, 64, 128]).astype(np.float32)), "input")
29        self.indices = Tensor(np.ones([4, 8]).astype(np.int32))
30        self.updates = Tensor(np.ones([4, 8, 64, 128]).astype(np.float32))
31        self.scatter_update = P.ScatterUpdate().shard(strategy1)
32        self.add = P.TensorAdd().shard(strategy2)
33        self.relu = P.ReLU()
34
35    def construct(self, x):
36        out = self.scatter_update(self.inputs, self.indices, self.updates)
37        out = self.add(x, out)
38        out = self.relu(out)
39        return out
40
41
42def test_distribute_predict():
43    context.set_context(mode=context.GRAPH_MODE)
44    context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, full_batch=True)
45    inputs = Tensor(np.ones([32, 64, 128]).astype(np.float32))
46    strategy1 = ((1, 2, 4), (1, 1), (1, 1, 2, 4))
47    strategy2 = ((1, 2, 4), (1, 2, 4))
48    net = Net(strategy1, strategy2)
49    model = Model(net)
50    predict_map = model.infer_predict_layout(inputs)
51    output = model.predict(inputs)
52    context.reset_auto_parallel_context()
53    return predict_map, output
54
55
56def test_scatter_update_wrong_strategy():
57    context.set_context(mode=context.GRAPH_MODE)
58    context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, full_batch=True)
59    inputs = Tensor(np.ones([32, 64, 128]).astype(np.float32))
60    strategy1 = ((1, 2, 4), (1, 1), (1, 1, 4, 2))
61    strategy2 = ((1, 2, 4), (1, 2, 4))
62    net = Net(strategy1, strategy2)
63    model = Model(net)
64    with pytest.raises(RuntimeError):
65        model.predict(inputs)
66    context.reset_auto_parallel_context()
67
68
69def test_distribute_predict_auto_parallel():
70    context.set_context(mode=context.GRAPH_MODE)
71    context.set_auto_parallel_context(parallel_mode="auto_parallel", device_num=8, full_batch=True)
72    inputs = Tensor(np.ones([32, 64, 128]).astype(np.float32))
73    net = Net()
74    model = Model(net)
75    predict_map = model.infer_predict_layout(inputs)
76    output = model.predict(inputs)
77    context.reset_auto_parallel_context()
78    return predict_map, output
79