• 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
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.dropout1 = P.Dropout(keep_prob=0.5).shard(strategy2)
29        self.relu = P.ReLU().shard(strategy2)
30        self.dropout2 = P.Dropout(keep_prob=0.5).shard(strategy2)
31        self.relu2 = P.ReLU().shard(strategy2)
32        self.mul_weight = Parameter(mul_weight, "w1")
33
34    def construct(self, x, b):
35        out = self.mul(x, self.mul_weight)
36        out, _ = self.dropout1(out)
37        out = self.relu(out)
38        out, _ = self.dropout2(out)
39        out = self.relu2(out)
40        return out
41
42
43_x = Tensor(np.ones([128, 64]), dtype=ms.float32)
44_w1 = Tensor(np.ones([128, 64]), dtype=ms.float32)
45_b = Tensor(np.ones([128, 64]), dtype=ms.float32)
46
47
48def compile_net(net):
49    optimizer = Momentum(net.trainable_params(), learning_rate=0.1, momentum=0.9)
50    train_net = TrainOneStepCell(net, optimizer)
51    train_net.set_auto_parallel()
52    train_net.set_train()
53    _cell_graph_executor.compile(train_net, _x, _b)
54    context.reset_auto_parallel_context()
55
56
57def test_dropout_data_parallel():
58    context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=16, global_rank=0)
59    strategy1 = ((16, 1), (16, 1))
60    strategy2 = ((16, 1),)
61    net = Net(_w1, strategy1, strategy2)
62    compile_net(net)
63
64
65def test_dropout_model_parallel():
66    context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=16, global_rank=0)
67    strategy1 = ((1, 16), (1, 16))
68    strategy2 = ((1, 16),)
69    net = Net(_w1, strategy1, strategy2)
70    compile_net(net)
71
72
73def test_dropout_mixed_parallel():
74    context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=16, global_rank=0)
75    strategy1 = ((4, 4), (4, 4))
76    strategy2 = ((4, 4),)
77    net = Net(_w1, strategy1, strategy2)
78    compile_net(net)
79
80
81def test_dropout_auto_parallel():
82    context.set_auto_parallel_context(parallel_mode="auto_parallel", device_num=16, global_rank=0)
83    net = Net(_w1)
84    compile_net(net)
85
86
87def test_dropout_repeat_calc():
88    context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=16, global_rank=0)
89    strategy1 = ((4, 4), (4, 4))
90    strategy2 = ((2, 4),)
91    net = Net(_w1, strategy1, strategy2)
92    compile_net(net)
93