• 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# ============================================================================
15""" test_fix_bug """
16import numpy as np
17import pytest
18
19import mindspore.nn as nn
20from mindspore import Tensor
21from mindspore.ops import composite as C
22from mindspore.ops import operations as P
23from mindspore.common import dtype as ms
24from mindspore.common.api import _cell_graph_executor
25
26
27class assignment1_Net(nn.Cell):
28    """ assignment1_Net definition """
29
30    def __init__(self, number):
31        super().__init__()
32        self.number = number
33        self.relu = nn.ReLU()
34
35    def construct(self, x):
36        y = self.number
37        for _ in [1, y]:
38            x = self.relu(x)
39        return x
40
41
42class assignment2_Net(nn.Cell):
43    """ assignment2_Net definition """
44
45    def __init__(self, number):
46        super().__init__()
47        self.number = number
48        self.relu = nn.ReLU()
49
50    def construct(self, x):
51        a, b = self.number
52        for _ in [a, b]:
53            x = self.relu(x)
54        return x
55
56
57def assignment_operator_base(number):
58    """ assignment_operator_base """
59    input_np = np.random.randn(2, 3, 4, 5).astype(np.float32)
60    input_me = Tensor(input_np)
61    x = number
62    if isinstance(x, int):
63        net = assignment1_Net(x)
64    else:
65        net = assignment2_Net(x)
66    _cell_graph_executor.compile(net, input_me)
67
68
69def test_ME_assignment_operator_0010():
70    """ test_ME_assignment_operator_0010 """
71    assignment_operator_base(3)
72
73
74def test_ME_assignment_operator_0020():
75    """ test_ME_assignment_operator_0020 """
76    assignment_operator_base((1, 3))
77
78
79class unsupported_method_net(nn.Cell):
80    """ unsupported_method_net definition """
81
82    def __init__(self):
83        super().__init__()
84        self.relu = nn.ReLU()
85
86    def construct(self, x):
87        with open("a.txt") as f:
88            f.read()
89        return x
90
91
92def test_compile_unspported():
93    """ test_compile_unspported """
94    input_np = np.random.randn(2, 3, 4, 5).astype(np.float32)
95    input_me = Tensor(input_np)
96    net = unsupported_method_net()
97    with pytest.raises(RuntimeError):
98        _cell_graph_executor.compile(net, input_me)
99
100
101def test_parser_map_0002():
102    class NetMap0002(nn.Cell):
103        def __init__(self):
104            super().__init__()
105            self.relu = nn.ReLU()
106            self.hypermap = C.Map()
107
108        def mul(self, x=2, y=4):
109            return x * y
110
111        def construct(self, x):
112            if map(self.mul) == 8:
113                x = self.relu(x)
114            return x
115    input_np_x = np.random.randn(2, 3, 4, 5).astype(np.float32)
116    input_me_x = Tensor(input_np_x)
117
118    net = NetMap0002()
119    with pytest.raises(TypeError):
120        net(input_me_x)
121
122
123def test_fix_expanddims_loss_scale():
124    class ControlOneIfOneScaleOneScale(nn.Cell):
125        def __init__(self):
126            super().__init__()
127            self.op = P.ExpandDims()
128
129        def construct(self, x, y, data):
130            if x > y:
131                out = 1
132            else:
133                out = 2
134            if x > y:
135                out = self.op(data, out)
136            else:
137                out = self.op(data, out)
138            return out
139    net = ControlOneIfOneScaleOneScale()
140    x = Tensor(1, ms.float32)
141    y = Tensor(0, ms.float32)
142    input_shape = (1024, 512, 7, 7)
143    input_data = np.random.randn(*input_shape).astype(np.float32)
144    net = ControlOneIfOneScaleOneScale()
145    net(x, y, Tensor(input_data))
146