• 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
18import mindspore.nn as nn
19from mindspore import Tensor
20from mindspore import context
21from mindspore.common.api import _cell_graph_executor
22from mindspore.ops import composite as C
23from mindspore.ops import operations as P
24from tests.ut.python.ops.test_math_ops import VirtualLoss
25
26
27grad_all = C.GradOperation(get_all=True)
28
29
30class NetWithLoss(nn.Cell):
31    def __init__(self, network):
32        super(NetWithLoss, self).__init__()
33        self.loss = VirtualLoss()
34        self.network = network
35
36    def construct(self, x, y):
37        predict = self.network(x, y)
38        return self.loss(predict)
39
40
41class GradWrap(nn.Cell):
42    def __init__(self, network):
43        super(GradWrap, self).__init__()
44        self.network = network
45
46    def construct(self, x, y):
47        return grad_all(self.network)(x, y)
48
49
50class Net(nn.Cell):
51    def __init__(self, strategy):
52        super().__init__()
53        self.reshape = P.Reshape()
54        self.mul = P.Mul().shard(strategy)
55        self.relu = P.ReLU()
56
57    def construct(self, x, y):
58        out = self.reshape(x, (10000, 36, 1))
59        out = self.mul(out, y)
60        out = self.relu(out)
61        return out
62
63
64def compile_net(net, x, y):
65    net.set_auto_parallel()
66    net.set_train()
67    _cell_graph_executor.compile(net, x, y)
68
69
70def test_reshape_parameter_data_parallel():
71    context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
72    strategy = ((8, 1, 1), (8, 1, 1))
73    net = GradWrap(NetWithLoss(Net(strategy)))
74    x = Tensor(np.ones([10000, 36]), dtype=ms.float32)
75    y = Tensor(np.ones([10000, 36, 1]), dtype=ms.float32)
76    compile_net(net, x, y)
77
78
79def test_reshape_parameter_model_parallel():
80    context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
81    strategy = ((4, 2, 1), (4, 2, 1))
82    net = GradWrap(NetWithLoss(Net(strategy)))
83    x = Tensor(np.ones([10000, 36]), dtype=ms.float32)
84    y = Tensor(np.ones([10000, 36, 1]), dtype=ms.float32)
85    compile_net(net, x, y)
86