• 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.nn as nn
18from mindspore import Tensor
19from mindspore import context
20from mindspore.common.api import _cell_graph_executor
21from mindspore.ops import composite as C
22from mindspore.ops import operations as P
23from tests.ut.python.ops.test_math_ops import VirtualLoss
24
25
26grad_all = C.GradOperation(get_all=True)
27
28
29class NetWithLoss(nn.Cell):
30    def __init__(self, network):
31        super(NetWithLoss, self).__init__()
32        self.loss = VirtualLoss()
33        self.network = network
34
35    def construct(self, x, y):
36        predict = self.network(x, y)
37        return self.loss(predict)
38
39
40class GradWrap(nn.Cell):
41    def __init__(self, network):
42        super(GradWrap, self).__init__()
43        self.network = network
44
45    def construct(self, x, y):
46        return grad_all(self.network)(x, y)
47
48
49class Net(nn.Cell):
50    def __init__(self, strategy1=None, strategy2=None):
51        super().__init__()
52        self.dropout = P.Dropout(keep_prob=0.6).shard(strategy1)
53        self.matmul = P.MatMul().shard(strategy2)
54
55    def construct(self, x, y):
56        out = self.matmul(x, y)
57        out, _ = self.dropout(out)
58        return out
59
60
61def test_dropout_semi_auto():
62    context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
63    net = GradWrap(NetWithLoss(Net()))
64    net.set_auto_parallel()
65
66    x = Tensor(np.ones([64, 32]), dtype=ms.float32)
67    y = Tensor(np.ones([32, 128]), dtype=ms.float32)
68    net.set_train()
69    _cell_graph_executor.compile(net, x, y)
70
71
72def test_dropout_semi_auto2():
73    context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
74    strategy1 = ((8, 1),)
75    strategy2 = ((4, 2), (2, 1))
76    net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
77    net.set_auto_parallel()
78
79    x = Tensor(np.ones([64, 32]), dtype=ms.float32)
80    y = Tensor(np.ones([32, 128]), dtype=ms.float32)
81    net.set_train()
82    _cell_graph_executor.compile(net, x, y)
83
84
85def test_dropout_semi_auto3():
86    context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
87    strategy1 = ((2, 4),)
88    strategy2 = ((4, 2), (2, 1))
89    net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
90    net.set_auto_parallel()
91
92    x = Tensor(np.ones([64, 32]), dtype=ms.float32)
93    y = Tensor(np.ones([32, 128]), dtype=ms.float32)
94    net.set_train()
95    _cell_graph_executor.compile(net, x, y)
96
97
98def test_dropout_auto():
99    context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="auto_parallel")
100    net = GradWrap(NetWithLoss(Net()))
101    net.set_auto_parallel()
102
103    x = Tensor(np.ones([64, 32]), dtype=ms.float32)
104    y = Tensor(np.ones([32, 128]), dtype=ms.float32)
105    net.set_train()
106    _cell_graph_executor.compile(net, x, y)
107