• 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
16
17import mindspore as ms
18from mindspore import context, Tensor, Parameter
19from mindspore.common.api import _cell_graph_executor
20from mindspore.nn import Cell, TrainOneStepCell, Momentum
21from mindspore.ops import operations as P
22
23
24class Net(Cell):
25    def __init__(self, mul_weight, strategy1=None, strategy2=None):
26        super().__init__()
27        self.mul = P.Mul().shard(strategy1)
28        self.sigmoid = P.Sigmoid().shard(strategy2)
29        self.mul_weight = Parameter(mul_weight, "w1")
30
31    def construct(self, x, b):
32        out = self.mul(x, self.mul_weight)
33        out = self.sigmoid(out)
34        return out
35
36
37_x = Tensor(np.ones([64, 32]), dtype=ms.float32)
38_w1 = Tensor(np.ones([64, 32]), dtype=ms.float32)
39_b = Tensor(np.ones([64, 32]), dtype=ms.float32)
40
41
42def compile_net(net):
43    optimizer = Momentum(net.trainable_params(), learning_rate=0.1, momentum=0.9)
44    train_net = TrainOneStepCell(net, optimizer)
45    train_net.set_auto_parallel()
46    train_net.set_train()
47    _cell_graph_executor.compile(train_net, _x, _b)
48    context.reset_auto_parallel_context()
49
50
51def test_auto_parallel_activation():
52    context.set_auto_parallel_context(parallel_mode="auto_parallel", device_num=16, global_rank=0)
53    strategy1 = ((4, 4), (4, 4))
54    strategy2 = None
55    net = Net(_w1, strategy1, strategy2)
56    compile_net(net)
57