• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2020-2021 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
16import pytest
17import numpy as np
18from mindspore import Tensor
19from mindspore.ops import operations as P
20import mindspore.nn as nn
21import mindspore.context as context
22from mindspore.common.api import ms_function
23
24context.set_context(mode=context.PYNATIVE_MODE, device_target="CPU")
25
26
27class NetReduce(nn.Cell):
28    def __init__(self):
29        super(NetReduce, self).__init__()
30        self.axis0 = 0
31        self.axis1 = 1
32        self.axis2 = -1
33        self.axis3 = (0, 1)
34        self.axis4 = (0, 1, 2)
35        self.axis5 = (-1,)
36        self.axis6 = ()
37        self.reduce_mean = P.ReduceMean(False)
38        self.reduce_sum = P.ReduceSum(False)
39        self.reduce_max = P.ReduceMax(False)
40        self.reduce_min = P.ReduceMin(False)
41
42    @ms_function
43    def construct(self, indice):
44        return (self.reduce_mean(indice, self.axis0),
45                self.reduce_mean(indice, self.axis1),
46                self.reduce_mean(indice, self.axis2),
47                self.reduce_mean(indice, self.axis3),
48                self.reduce_mean(indice, self.axis4),
49                self.reduce_sum(indice, self.axis0),
50                self.reduce_sum(indice, self.axis2),
51                self.reduce_max(indice, self.axis0),
52                self.reduce_max(indice, self.axis2),
53                self.reduce_max(indice, self.axis5),
54                self.reduce_max(indice, self.axis6),
55                self.reduce_min(indice, self.axis0),
56                self.reduce_min(indice, self.axis1),
57                self.reduce_min(indice, self.axis2),
58                self.reduce_min(indice, self.axis3),
59                self.reduce_min(indice, self.axis4),
60                self.reduce_min(indice, self.axis5),
61                self.reduce_min(indice, self.axis6))
62
63
64class NetReduceLogic(nn.Cell):
65    def __init__(self):
66        super(NetReduceLogic, self).__init__()
67        self.axis0 = 0
68        self.axis1 = -1
69        self.axis2 = (0, 1, 2)
70        self.axis3 = ()
71        self.reduce_all = P.ReduceAll(False)
72        self.reduce_any = P.ReduceAny(False)
73
74    @ms_function
75    def construct(self, indice):
76        return (self.reduce_all(indice, self.axis0),
77                self.reduce_all(indice, self.axis1),
78                self.reduce_all(indice, self.axis2),
79                self.reduce_all(indice, self.axis3),
80                self.reduce_any(indice, self.axis0),
81                self.reduce_any(indice, self.axis1),
82                self.reduce_any(indice, self.axis2),
83                self.reduce_any(indice, self.axis3),)
84
85
86@pytest.mark.level0
87@pytest.mark.platform_x86_cpu
88@pytest.mark.env_onecard
89def test_reduce():
90    reduce = NetReduce()
91    indice = Tensor(np.array([
92        [[0., 2., 1., 4., 0., 2.], [3., 1., 2., 2., 4., 0.]],
93        [[2., 0., 1., 5., 0., 1.], [1., 0., 0., 4., 4., 3.]],
94        [[4., 1., 4., 0., 0., 0.], [2., 5., 1., 0., 1., 3.]]
95    ]).astype(np.float32))
96    output = reduce(indice)
97    print(output[0])
98    print(output[1])
99    print(output[2])
100    print(output[3])
101    print(output[4])
102    print(output[5])
103    print(output[6])
104    print(output[7])
105    print(output[8])
106    print(output[9])
107    print(output[10])
108    print(output[11])
109    print(output[12])
110    print(output[13])
111    print(output[14])
112    print(output[15])
113    print(output[16])
114    print(output[17])
115    expect_0 = np.array([[2., 1., 2., 3., 0., 1], [2., 2., 1., 2., 3., 2.]]).astype(np.float32)
116    expect_1 = np.array([[1.5, 1.5, 1.5, 3., 2., 1.], [1.5, 0., 0.5, 4.5, 2., 2.], [3., 3., 2.5, 0., 0.5, 1.5]]).astype(
117        np.float32)
118    expect_2 = np.array([[1.5, 2.], [1.5, 2.], [1.5, 2.]]).astype(np.float32)
119    expect_3 = np.array([2, 1.5, 1.5, 2.5, 1.5, 1.5]).astype(np.float32)
120    expect_4 = np.array([1.75]).astype(np.float32)
121    expect_5 = np.array([[6., 3., 6., 9., 0., 3.], [6., 6., 3., 6., 9., 6.]]).astype(np.float32)
122    expect_6 = np.array([[9., 12.], [9., 12.], [9., 12.]]).astype(np.float32)
123    expect_7 = np.array([[4., 2., 4., 5., 0., 2.], [3., 5., 2., 4., 4., 3.]]).astype(np.float32)
124    expect_8 = np.array([[4., 4.], [5., 4.], [4., 5.]]).astype(np.float32)
125    expect_9 = np.array([[0., 0., 1., 0., 0., 0.], [1., 0., 0., 0., 1., 0.]]).astype(np.float32)
126    expect_10 = np.array([[0., 1., 1., 2., 0., 0.], [1., 0., 0., 4., 0., 1.], [2., 1., 1., 0., 0., 0.]]).astype(
127        np.float32)
128    expect_11 = np.array([[0., 0.], [0., 0.], [0., 0.]]).astype(np.float32)
129    expect_12 = np.array([0., 0., 0., 0., 0., 0.]).astype(np.float32)
130    assert (output[0].asnumpy() == expect_0).all()
131    assert (output[1].asnumpy() == expect_1).all()
132    assert (output[2].asnumpy() == expect_2).all()
133    assert (output[3].asnumpy() == expect_3).all()
134    assert (output[4].asnumpy() == expect_4).all()
135    assert (output[5].asnumpy() == expect_5).all()
136    assert (output[6].asnumpy() == expect_6).all()
137    assert (output[7].asnumpy() == expect_7).all()
138    assert (output[8].asnumpy() == expect_8).all()
139    assert (output[9].asnumpy() == expect_8).all()
140    assert (output[10].asnumpy() == 5.0).all()
141    assert (output[11].asnumpy() == expect_9).all()
142    assert (output[12].asnumpy() == expect_10).all()
143    assert (output[13].asnumpy() == expect_11).all()
144    assert (output[14].asnumpy() == expect_12).all()
145    assert (output[15].asnumpy() == 0.0).all()
146    assert (output[16].asnumpy() == expect_11).all()
147    assert (output[17].asnumpy() == 0.0).all()
148
149
150@pytest.mark.level0
151@pytest.mark.platform_x86_cpu
152@pytest.mark.env_onecard
153def test_reduce_logic():
154    reduce_logic = NetReduceLogic()
155    indice_bool = Tensor([[[False, True, True, True, False, True],
156                           [True, True, True, True, True, False]],
157                          [[True, False, True, True, False, True],
158                           [True, False, False, True, True, True]],
159                          [[True, True, True, False, False, False],
160                           [True, True, True, False, True, True]]])
161    output = reduce_logic(indice_bool)
162    expect_all_1 = np.array([[False, False, True, False, False, False],
163                             [True, False, False, False, True, False]])
164    expect_all_2 = np.array([[False, False], [False, False], [False, False]])
165    expect_all_3 = False
166    expect_all_4 = False
167    expect_any_1 = np.array([[True, True, True, True, False, True], [True, True, True, True, True, True]])
168    expect_any_2 = np.array([[True, True], [True, True], [True, True]])
169    expect_any_3 = True
170    expect_any_4 = True
171
172    assert (output[0].asnumpy() == expect_all_1).all()
173    assert (output[1].asnumpy() == expect_all_2).all()
174    assert (output[2].asnumpy() == expect_all_3).all()
175    assert (output[3].asnumpy() == expect_all_4).all()
176    assert (output[4].asnumpy() == expect_any_1).all()
177    assert (output[5].asnumpy() == expect_any_2).all()
178    assert (output[6].asnumpy() == expect_any_3).all()
179    assert (output[7].asnumpy() == expect_any_4).all()
180
181
182test_reduce()
183test_reduce_logic()
184