• 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
16import numpy as np
17import pytest
18
19import mindspore.context as context
20import mindspore.nn as nn
21from mindspore import Tensor
22from mindspore.common.api import ms_function
23from mindspore.ops import operations as P
24from mindspore.ops.operations import _inner_ops as inner
25
26
27x0 = np.random.rand(2, 3, 4, 4).astype(np.float32)
28axis0 = 3
29keep_dims0 = True
30
31x1 = np.random.rand(2, 3, 4, 4).astype(np.float32)
32axis1 = 3
33keep_dims1 = False
34
35x2 = np.random.rand(2, 3, 1, 4).astype(np.float32)
36axis2 = 2
37keep_dims2 = True
38
39x3 = np.random.rand(2, 3, 1, 4).astype(np.float32)
40axis3 = 2
41keep_dims3 = False
42
43x4 = np.random.rand(2, 3, 4, 4).astype(np.float32)
44axis4 = ()
45np_axis4 = None
46keep_dims4 = True
47
48x5 = np.random.rand(2, 3, 4, 4).astype(np.float32)
49axis5 = ()
50np_axis5 = None
51keep_dims5 = False
52
53x6 = np.random.rand(2, 3, 4, 4).astype(np.float32)
54axis6 = -2
55keep_dims6 = False
56
57x7 = np.random.rand(2, 3, 4, 4).astype(np.float32)
58axis7 = (-2, -1)
59keep_dims7 = True
60
61x8 = np.random.rand(1, 1, 1, 1).astype(np.float32)
62axis8 = ()
63np_axis8 = None
64keep_dims8 = True
65
66
67class ReduceMin(nn.Cell):
68    def __init__(self):
69        super(ReduceMin, self).__init__()
70
71        self.x0 = Tensor(x0)
72        self.axis0 = axis0
73        self.keep_dims0 = keep_dims0
74
75        self.x1 = Tensor(x1)
76        self.axis1 = axis1
77        self.keep_dims1 = keep_dims1
78
79        self.x2 = Tensor(x2)
80        self.axis2 = axis2
81        self.keep_dims2 = keep_dims2
82
83        self.x3 = Tensor(x3)
84        self.axis3 = axis3
85        self.keep_dims3 = keep_dims3
86
87        self.x4 = Tensor(x4)
88        self.axis4 = axis4
89        self.keep_dims4 = keep_dims4
90
91        self.x5 = Tensor(x5)
92        self.axis5 = axis5
93        self.keep_dims5 = keep_dims5
94
95        self.x6 = Tensor(x6)
96        self.axis6 = axis6
97        self.keep_dims6 = keep_dims6
98
99        self.x7 = Tensor(x7)
100        self.axis7 = axis7
101        self.keep_dims7 = keep_dims7
102
103        self.x8 = Tensor(x8)
104        self.axis8 = axis8
105        self.keep_dims8 = keep_dims8
106
107    @ms_function
108    def construct(self):
109        return (P.ReduceMin(self.keep_dims0)(self.x0, self.axis0),
110                P.ReduceMin(self.keep_dims1)(self.x1, self.axis1),
111                P.ReduceMin(self.keep_dims2)(self.x2, self.axis2),
112                P.ReduceMin(self.keep_dims3)(self.x3, self.axis3),
113                P.ReduceMin(self.keep_dims4)(self.x4, self.axis4),
114                P.ReduceMin(self.keep_dims5)(self.x5, self.axis5),
115                P.ReduceMin(self.keep_dims6)(self.x6, self.axis6),
116                P.ReduceMin(self.keep_dims7)(self.x7, self.axis7),
117                P.ReduceMin(self.keep_dims8)(self.x8, self.axis8))
118
119
120@pytest.mark.level0
121@pytest.mark.platform_x86_gpu_training
122@pytest.mark.env_onecard
123def test_ReduceMin():
124    context.set_context(mode=context.PYNATIVE_MODE, device_target='GPU')
125    reduce_min = ReduceMin()
126    output = reduce_min()
127
128    expect0 = np.min(x0, axis=axis0, keepdims=keep_dims0)
129    diff0 = abs(output[0].asnumpy() - expect0)
130    error0 = np.ones(shape=expect0.shape) * 1.0e-5
131    assert np.all(diff0 < error0)
132    assert output[0].shape == expect0.shape
133
134    expect1 = np.min(x1, axis=axis1, keepdims=keep_dims1)
135    diff1 = abs(output[1].asnumpy() - expect1)
136    error1 = np.ones(shape=expect1.shape) * 1.0e-5
137    assert np.all(diff1 < error1)
138    assert output[1].shape == expect1.shape
139
140    expect2 = np.min(x2, axis=axis2, keepdims=keep_dims2)
141    diff2 = abs(output[2].asnumpy() - expect2)
142    error2 = np.ones(shape=expect2.shape) * 1.0e-5
143    assert np.all(diff2 < error2)
144    assert output[2].shape == expect2.shape
145
146    expect3 = np.min(x3, axis=axis3, keepdims=keep_dims3)
147    diff3 = abs(output[3].asnumpy() - expect3)
148    error3 = np.ones(shape=expect3.shape) * 1.0e-5
149    assert np.all(diff3 < error3)
150    assert output[3].shape == expect3.shape
151
152    expect4 = np.min(x4, axis=np_axis4, keepdims=keep_dims4)
153    diff4 = abs(output[4].asnumpy() - expect4)
154    error4 = np.ones(shape=expect4.shape) * 1.0e-5
155    assert np.all(diff4 < error4)
156    assert output[4].shape == expect4.shape
157
158    expect5 = np.min(x5, axis=np_axis5, keepdims=keep_dims5)
159    diff5 = abs(output[5].asnumpy() - expect5)
160    error5 = np.ones(shape=expect5.shape) * 1.0e-5
161    assert np.all(diff5 < error5)
162    assert output[5].shape == expect5.shape
163
164    expect6 = np.min(x6, axis=axis6, keepdims=keep_dims6)
165    diff6 = abs(output[6].asnumpy() - expect6)
166    error6 = np.ones(shape=expect6.shape) * 1.0e-5
167    assert np.all(diff6 < error6)
168    assert output[6].shape == expect6.shape
169
170    expect7 = np.min(x7, axis=axis7, keepdims=keep_dims7)
171    diff7 = abs(output[7].asnumpy() - expect7)
172    error7 = np.ones(shape=expect7.shape) * 1.0e-5
173    assert np.all(diff7 < error7)
174
175    expect8 = np.min(x8, axis=np_axis8, keepdims=keep_dims8)
176    diff8 = abs(output[8].asnumpy() - expect8)
177    error8 = np.ones(shape=expect8.shape) * 1.0e-5
178    assert np.all(diff8 < error8)
179
180
181x_1 = x8
182axis_1 = 0
183x_2 = x1
184axis_2 = 0
185
186
187class ReduceMinDynamic(nn.Cell):
188    def __init__(self, x, axis):
189        super(ReduceMinDynamic, self).__init__()
190        self.reducemin = P.ReduceMin(False)
191        self.test_dynamic = inner.GpuConvertToDynamicShape()
192        self.x = x
193        self.axis = axis
194
195    def construct(self):
196        dynamic_x = self.test_dynamic(self.x)
197        return self.reducemin(dynamic_x, self.axis)
198
199@pytest.mark.level0
200@pytest.mark.platform_x86_gpu_training
201@pytest.mark.env_onecard
202def test_reduce_min_dynamic():
203    context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
204    net1 = ReduceMinDynamic(Tensor(x_1), axis_1)
205    net2 = ReduceMinDynamic(Tensor(x_2), axis_2)
206
207    expect_1 = np.min(x_1, axis=0, keepdims=False)
208    expect_2 = np.min(x_2, axis=0, keepdims=False)
209
210    output1 = net1()
211    output2 = net2()
212
213    np.testing.assert_almost_equal(output1.asnumpy(), expect_1)
214    np.testing.assert_almost_equal(output2.asnumpy(), expect_2)
215