• 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
24from mindspore.parallel._utils import _reset_op_id as reset_op_id
25from tests.ut.python.ops.test_math_ops import VirtualLoss
26
27
28grad_all = C.GradOperation(get_all=True)
29
30
31class NetWithLoss(nn.Cell):
32    def __init__(self, network):
33        super(NetWithLoss, self).__init__()
34        self.loss = VirtualLoss()
35        self.network = network
36
37    def construct(self, x, y, b):
38        predict = self.network(x, y, b)
39        return self.loss(predict)
40
41
42class GradWrap(nn.Cell):
43    def __init__(self, network):
44        super(GradWrap, self).__init__()
45        self.network = network
46
47    def construct(self, x, y, b):
48        return grad_all(self.network)(x, y, b)
49
50
51# core dump, step_auto_parallel should SetInputs for transpose axis
52def test_two_matmul_transpose():
53    class Net(nn.Cell):
54        def __init__(self):
55            super().__init__()
56            self.matmul1 = P.MatMul()
57            self.matmul2 = P.MatMul()
58            self.transpose1 = P.Transpose()
59            self.transpose2 = P.Transpose()
60
61        def construct(self, x, y, b):
62            out = self.matmul1(x, y)
63            out = self.matmul2(out, b)
64            out = self.transpose1(out, (1, 0))
65            out = self.transpose2(out, (1, 0))
66            return out
67
68    size = 16
69    context.set_auto_parallel_context(device_num=size, global_rank=0)
70    x = Tensor(np.ones([128, 32]), dtype=ms.float32)
71    y = Tensor(np.ones([32, 64]), dtype=ms.float32)
72    b = Tensor(np.ones([64, 64]), dtype=ms.float32)
73
74    net = NetWithLoss(Net())
75    context.set_auto_parallel_context(parallel_mode="auto_parallel")
76    net.set_auto_parallel()
77    reset_op_id()
78
79    net.set_train()
80    _cell_graph_executor.compile(net, x, y, b, phase='train')
81    strategies = _cell_graph_executor._get_shard_strategy(net)
82    print(strategies)
83    expected_strategies = {'Default/network-Net/Transpose-op0': [[1, 16]],
84                           'Default/network-Net/Transpose-op1': [[16, 1]],
85                           'Default/network-Net/MatMul-op3': [[16, 1], [1, 1]],
86                           'Default/network-Net/MatMul-op2': [[16, 1], [1, 1]]}
87    assert strategies == expected_strategies
88