• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2019 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
24
25
26grad_all = C.GradOperation(get_all=True)
27
28
29class GradWrap(nn.Cell):
30    def __init__(self, network):
31        super(GradWrap, self).__init__()
32        self.network = network
33
34    def construct(self, x, y):
35        return grad_all(self.network)(x, y)
36
37
38def compile_net(net, x, y):
39    net.set_auto_parallel()
40    net.set_train()
41    _cell_graph_executor.compile(net, x, y)
42
43
44def test_sum_as_loss():
45    class Net(nn.Cell):
46        def __init__(self, strategy0, strategy1):
47            super().__init__()
48            self.fc_nobias = P.MatMul(transpose_b=True).shard(strategy0)
49            self.reduce_sum = P.ReduceSum(keep_dims=False).shard(strategy1)
50
51        def construct(self, x, y):
52            out = self.fc_nobias(x, y)
53            out = self.reduce_sum(out, (0, 1))
54            return out
55
56    context.set_auto_parallel_context(device_num=16, global_rank=0)
57    strategy0 = ((4, 1), (4, 1))
58    strategy1 = ((4, 1),)
59    net = GradWrap(Net(strategy0, strategy1))
60    context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
61
62    x = Tensor(np.ones([64, 32]), dtype=ms.float32)
63    y = Tensor(np.ones([64, 32]), dtype=ms.float32)
64    compile_net(net, x, y)
65
66
67def test_sum_as_loss2():
68    class Net(nn.Cell):
69        def __init__(self, strategy0, strategy1):
70            super().__init__()
71            self.fc_nobias = P.MatMul(transpose_b=True).shard(strategy0)
72            self.reduce_sum = P.ReduceSum(keep_dims=False).shard(strategy1)
73
74        def construct(self, x, y):
75            out = self.fc_nobias(x, y)
76            out = self.reduce_sum(out, (0, 1))
77            return out
78
79    context.set_auto_parallel_context(device_num=16, global_rank=0)
80    strategy0 = ((4, 1), (4, 1))
81    strategy1 = ((1, 1),)
82    net = GradWrap(Net(strategy0, strategy1))
83    context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
84
85    x = Tensor(np.ones([64, 32]), dtype=ms.float32)
86    y = Tensor(np.ones([64, 32]), dtype=ms.float32)
87    compile_net(net, x, y)
88