• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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# ============================================================================
15import numpy as np
16import mindspore as ms
17import mindspore.context as context
18from mindspore import Tensor, Parameter
19import mindspore.nn as nn
20from mindspore.common.api import _cell_graph_executor
21from mindspore.nn import TrainOneStepCell, Momentum
22from mindspore.ops import operations as P
23
24
25class Net(nn.Cell):
26    def __init__(self, mul_weight, strategy=None):
27        super(Net, self).__init__()
28        self.reluv2 = P.ReLUV2().shard(strategy)
29        self.mul = P.Mul()
30        self.weight = Parameter(mul_weight, "w1")
31
32    def construct(self, x):
33        out = self.mul(x, self.weight)
34        output, _ = self.reluv2(out)
35        return output
36
37
38_w1 = Tensor(np.ones([32, 16, 48, 64]), dtype=ms.float32)
39_x = Tensor(np.ones([32, 16, 48, 64]), dtype=ms.float32)
40
41
42def compile_net(net):
43    context.set_context(mode=context.GRAPH_MODE)
44    optimizer = Momentum(net.trainable_params(), learning_rate=0.1, momentum=0.9)
45    train_net = TrainOneStepCell(net, optimizer)
46    train_net.set_auto_parallel()
47    train_net.set_train()
48    _cell_graph_executor.compile(train_net, _x)
49    context.reset_auto_parallel_context()
50
51
52def test_reluv2():
53    context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
54    strategy = ((2, 1, 2, 2),)
55    net = Net(_w1, strategy)
56    compile_net(net)
57
58
59def test_reluv2_no_full():
60    context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
61    strategy = ((2, 1, 2, 1),)
62    net = Net(_w1, strategy)
63    compile_net(net)
64
65
66def test_reluv2_no_strategy():
67    context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
68    strategy = None
69    net = Net(_w1, strategy)
70    compile_net(net)
71
72
73def test_reluv2_auto_parallel():
74    context.set_auto_parallel_context(parallel_mode="auto_parallel", device_num=8, global_rank=0)
75    net = Net(_w1)
76    compile_net(net)
77