ce09f3c94b813780bd3eeade2ed3e93786fbc672
[platform/upstream/dldt.git] / ngraph / test / backend / max.in.cpp
1 //*****************************************************************************
2 // Copyright 2017-2020 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //*****************************************************************************
16
17 #include "gtest/gtest.h"
18 #include "ngraph/ngraph.hpp"
19 #include "util/engine/test_engines.hpp"
20 #include "util/test_case.hpp"
21 #include "util/test_control.hpp"
22
23 NGRAPH_SUPPRESS_DEPRECATED_START
24
25 using namespace std;
26 using namespace ngraph;
27
28 static string s_manifest = "${MANIFEST}";
29 using TestEngine = test::ENGINE_CLASS_NAME(${BACKEND_NAME});
30
31 // Trivial case with no reduced axes.
32 NGRAPH_TEST(${BACKEND_NAME}, max_trivial)
33 {
34     Shape shape{2, 2};
35     auto A = make_shared<op::Parameter>(element::f32, shape);
36     auto f = make_shared<Function>(make_shared<op::Max>(A, AxisSet{}), ParameterVector{A});
37
38     std::vector<float> a{1, 2, 3, 4};
39
40     auto test_case = test::TestCase<TestEngine>(f);
41     test_case.add_input<float>({a});
42     test_case.add_expected_output<float>(shape, {1, 2, 3, 4});
43     test_case.run(MIN_FLOAT_TOLERANCE_BITS);
44 }
45
46 NGRAPH_TEST(${BACKEND_NAME}, max_trivial_int8)
47 {
48     Shape shape{2, 2};
49     auto A = make_shared<op::Parameter>(element::i8, shape);
50     auto f = make_shared<Function>(make_shared<op::Max>(A, AxisSet{}), ParameterVector{A});
51
52     std::vector<int8_t> a{1, 2, 3, 4};
53
54     auto test_case = test::TestCase<TestEngine>(f);
55     test_case.add_input<int8_t>({a});
56     test_case.add_expected_output<int8_t>(shape, {1, 2, 3, 4});
57     test_case.run();
58 }
59
60 // Failure has been reported at 5D for some reason
61 NGRAPH_TEST(${BACKEND_NAME}, max_trivial_5d)
62 {
63     Shape shape{2, 2, 2, 2, 2};
64     auto A = make_shared<op::Parameter>(element::f32, shape);
65     auto f = make_shared<Function>(make_shared<op::Max>(A, AxisSet{}), ParameterVector{A});
66
67     std::vector<float> a{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
68                          1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
69
70     auto test_case = test::TestCase<TestEngine>(f);
71     test_case.add_input<float>({a});
72     test_case.add_expected_output<float>(shape, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
73                                                  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1});
74     test_case.run(MIN_FLOAT_TOLERANCE_BITS);
75 }
76
77 NGRAPH_TEST(${BACKEND_NAME}, max_trivial_5d_int32)
78 {
79     Shape shape{2, 2, 2, 2, 2};
80     auto A = make_shared<op::Parameter>(element::i32, shape);
81     auto f = make_shared<Function>(make_shared<op::Max>(A, AxisSet{}), ParameterVector{A});
82
83     std::vector<int32_t> a{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
84                            1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
85
86     auto test_case = test::TestCase<TestEngine>(f);
87     test_case.add_input<int32_t>({a});
88     test_case.add_expected_output<int32_t>(shape, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
89                                                    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1});
90     test_case.run();
91 }
92
93 NGRAPH_TEST(${BACKEND_NAME}, max_to_scalar)
94 {
95     Shape shape{2, 2};
96     auto A = make_shared<op::Parameter>(element::f32, shape);
97     auto f = make_shared<Function>(make_shared<op::Max>(A, AxisSet{0, 1}), ParameterVector{A});
98
99     std::vector<float> a{1, 2, 3, 4};
100
101     auto test_case = test::TestCase<TestEngine>(f);
102     test_case.add_input<float>(a);
103     test_case.add_expected_output<float>(Shape{}, {4});
104     test_case.run();
105 }
106
107 NGRAPH_TEST(${BACKEND_NAME}, max_to_scalar_int8)
108 {
109     Shape shape{2, 2};
110     auto A = make_shared<op::Parameter>(element::i8, shape);
111     auto f = make_shared<Function>(make_shared<op::Max>(A, AxisSet{0, 1}), ParameterVector{A});
112
113     std::vector<int8_t> a{1, 2, 3, 4};
114
115     auto test_case = test::TestCase<TestEngine>(f);
116     test_case.add_input<int8_t>({a});
117     test_case.add_expected_output<int8_t>(shape, {4});
118     test_case.run();
119 }
120
121 NGRAPH_TEST(${BACKEND_NAME}, max_matrix_columns)
122 {
123     Shape shape_a{3, 2};
124     auto A = make_shared<op::Parameter>(element::f32, shape_a);
125     Shape shape_rt{2};
126     auto f = make_shared<Function>(make_shared<op::Max>(A, AxisSet{0}), ParameterVector{A});
127
128     std::vector<float> a{1, 2, 3, 4, 5, 6};
129
130     auto test_case = test::TestCase<TestEngine>(f);
131     test_case.add_input<float>({a});
132     test_case.add_expected_output<float>(shape_rt, {5, 6});
133     test_case.run();
134 }
135
136 NGRAPH_TEST(${BACKEND_NAME}, max_matrix_rows)
137 {
138     Shape shape_a{3, 2};
139     auto A = make_shared<op::Parameter>(element::f32, shape_a);
140     Shape shape_rt{3};
141     auto f = make_shared<Function>(make_shared<op::Max>(A, AxisSet{1}), ParameterVector{A});
142
143     std::vector<float> a{1, 2, 3, 4, 5, 6};
144
145     auto test_case = test::TestCase<TestEngine>(f);
146     test_case.add_input<float>({a});
147     test_case.add_expected_output<float>(shape_rt, {2, 4, 6});
148     test_case.run();
149 }
150
151 NGRAPH_TEST(${BACKEND_NAME}, max_matrix_rows_int32)
152 {
153     Shape shape_a{3, 2};
154     auto A = make_shared<op::Parameter>(element::i32, shape_a);
155     Shape shape_rt{3};
156     auto f = make_shared<Function>(make_shared<op::Max>(A, AxisSet{1}), ParameterVector{A});
157
158     std::vector<int32_t> a{1, 2, 3, 4, 5, 6};
159
160     auto test_case = test::TestCase<TestEngine>(f);
161     test_case.add_input<int32_t>({a});
162     test_case.add_expected_output<int32_t>(shape_rt, {2, 4, 6});
163     test_case.run();
164 }
165
166 NGRAPH_TEST(${BACKEND_NAME}, max_matrix_rows_zero)
167 {
168     Shape shape_a{3, 0};
169     auto A = make_shared<op::Parameter>(element::f32, shape_a);
170     Shape shape_rt{3};
171     auto f = make_shared<Function>(make_shared<op::Max>(A, AxisSet{1}), ParameterVector{A});
172
173     std::vector<float> a{};
174
175     auto test_case = test::TestCase<TestEngine>(f);
176     test_case.add_input<float>({a});
177     test_case.add_expected_output<float>(shape_rt,
178                                          {-std::numeric_limits<float>::infinity(),
179                                           -std::numeric_limits<float>::infinity(),
180                                           -std::numeric_limits<float>::infinity()});
181     test_case.run();
182 }
183
184 NGRAPH_TEST(${BACKEND_NAME}, max_matrix_rows_zero_int32)
185 {
186     Shape shape_a{3, 0};
187     auto A = make_shared<op::Parameter>(element::i32, shape_a);
188     Shape shape_rt{3};
189     auto f = make_shared<Function>(make_shared<op::Max>(A, AxisSet{1}), ParameterVector{A});
190
191     std::vector<int32_t> a{};
192
193     int32_t minval = std::numeric_limits<int32_t>::has_infinity
194                          ? -std::numeric_limits<int32_t>::infinity()
195                          : std::numeric_limits<int32_t>::min();
196
197     auto test_case = test::TestCase<TestEngine>(f);
198     test_case.add_input<int32_t>({a});
199     test_case.add_expected_output<int32_t>(shape_rt, {minval, minval, minval});
200     test_case.run();
201 }
202
203 NGRAPH_TEST(${BACKEND_NAME}, max_matrix_cols_zero)
204 {
205     // Now the reduction (g(x:float32[2,2],y:float32[]) = reduce(x,y,f,axes={})).
206     Shape shape_a{0, 2};
207     auto A = make_shared<op::Parameter>(element::f32, shape_a);
208     Shape shape_rt{2};
209     auto f = make_shared<Function>(make_shared<op::Max>(A, AxisSet{0}), ParameterVector{A});
210
211     std::vector<float> a{};
212
213     auto test_case = test::TestCase<TestEngine>(f);
214     test_case.add_input<float>({a});
215     test_case.add_expected_output<float>(
216         shape_rt,
217         {-std::numeric_limits<float>::infinity(), -std::numeric_limits<float>::infinity()});
218     test_case.run();
219 }
220
221 NGRAPH_TEST(${BACKEND_NAME}, max_vector_zero)
222 {
223     Shape shape_a{0};
224     auto A = make_shared<op::Parameter>(element::f32, shape_a);
225     Shape shape_rt{};
226     auto f = make_shared<Function>(make_shared<op::Max>(A, AxisSet{0}), ParameterVector{A});
227
228     std::vector<float> a{};
229
230     auto test_case = test::TestCase<TestEngine>(f);
231     test_case.add_input<float>({a});
232     test_case.add_expected_output<float>(shape_rt, {-std::numeric_limits<float>::infinity()});
233     test_case.run();
234 }
235
236 NGRAPH_TEST(${BACKEND_NAME}, max_matrix_to_scalar_zero_by_zero)
237 {
238     Shape shape_a{0, 0};
239     auto A = make_shared<op::Parameter>(element::f32, shape_a);
240     Shape shape_rt{};
241     auto f = make_shared<Function>(make_shared<op::Max>(A, AxisSet{0, 1}), ParameterVector{A});
242
243     std::vector<float> a{};
244
245     auto test_case = test::TestCase<TestEngine>(f);
246     test_case.add_input<float>({a});
247     test_case.add_expected_output<float>(shape_rt, {-std::numeric_limits<float>::infinity()});
248     test_case.run();
249 }
250
251 NGRAPH_TEST(${BACKEND_NAME}, max_3d_to_matrix_most_sig)
252 {
253     Shape shape_a{3, 3, 3};
254     auto A = make_shared<op::Parameter>(element::f32, shape_a);
255     Shape shape_rt{3, 3};
256     auto f = make_shared<Function>(make_shared<op::Max>(A, AxisSet{0}), ParameterVector{A});
257
258     std::vector<float> a{1,  2,  3,  4,  5,  6,  7,  8,  9,  10, 11, 12, 13, 14,
259                          15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27};
260
261     auto test_case = test::TestCase<TestEngine>(f);
262     test_case.add_input<float>({a});
263     test_case.add_expected_output<float>(shape_rt, {19, 20, 21, 22, 23, 24, 25, 26, 27});
264     test_case.run(MIN_FLOAT_TOLERANCE_BITS);
265 }
266
267 NGRAPH_TEST(${BACKEND_NAME}, max_3d_to_matrix_least_sig)
268 {
269     Shape shape_a{3, 3, 3};
270     auto A = make_shared<op::Parameter>(element::f32, shape_a);
271     Shape shape_rt{3, 3};
272     auto f = make_shared<Function>(make_shared<op::Max>(A, AxisSet{2}), ParameterVector{A});
273
274     std::vector<float> a{1,  2,  3,  4,  5,  6,  7,  8,  9,  10, 11, 12, 13, 14,
275                          15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27};
276
277     auto test_case = test::TestCase<TestEngine>(f);
278     test_case.add_input<float>({a});
279     test_case.add_expected_output<float>(shape_rt, {3, 6, 9, 12, 15, 18, 21, 24, 27});
280     test_case.run(MIN_FLOAT_TOLERANCE_BITS);
281 }
282
283 NGRAPH_TEST(${BACKEND_NAME}, max_3d_to_vector)
284 {
285     Shape shape_a{3, 3, 3};
286     auto A = make_shared<op::Parameter>(element::f32, shape_a);
287     Shape shape_rt{3};
288     auto f = make_shared<Function>(make_shared<op::Max>(A, AxisSet{0, 1}), ParameterVector{A});
289     std::vector<float> a{1,  2,  3,  4,  5,  6,  7,  8,  9,  10, 11, 12, 13, 14,
290                          15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27};
291
292     auto test_case = test::TestCase<TestEngine>(f);
293     test_case.add_input<float>({a});
294     test_case.add_expected_output<float>(shape_rt, {25.0f, 26.0f, 27.0f});
295     test_case.run(MIN_FLOAT_TOLERANCE_BITS);
296 }
297
298 NGRAPH_TEST(${BACKEND_NAME}, max_3d_to_scalar)
299 {
300     Shape shape_a{3, 3, 3};
301     auto A = make_shared<op::Parameter>(element::f32, shape_a);
302     Shape shape_rt{};
303     auto f = make_shared<Function>(make_shared<op::Max>(A, AxisSet{0, 1, 2}), ParameterVector{A});
304
305     std::vector<float> a{1,  2,  3,  4,  5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
306                          13, 12, 11, 10, 9, 8, 7, 6, 5, 4,  3,  2,  1};
307
308     auto test_case = test::TestCase<TestEngine>(f);
309     test_case.add_input<float>({a});
310     test_case.add_expected_output<float>(shape_rt, {14.0f});
311     test_case.run(MIN_FLOAT_TOLERANCE_BITS);
312 }
313
314 NGRAPH_TEST(${BACKEND_NAME}, max_3d_to_scalar_int32)
315 {
316     Shape shape_a{3, 3, 3};
317     auto A = make_shared<op::Parameter>(element::i32, shape_a);
318     Shape shape_rt{};
319     auto f = make_shared<Function>(make_shared<op::Max>(A, AxisSet{0, 1, 2}), ParameterVector{A});
320
321     std::vector<int32_t> a{1,  2,  3,  4,  5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
322                            13, 12, 11, 10, 9, 8, 7, 6, 5, 4,  3,  2,  1};
323
324     auto test_case = test::TestCase<TestEngine>(f);
325     test_case.add_input<int32_t>({a});
326     test_case.add_expected_output<int32_t>(shape_rt, {14});
327     test_case.run();
328 }
329
330 NGRAPH_TEST(${BACKEND_NAME}, max_3d_to_scalar_double)
331 {
332     Shape shape_a{3, 3, 3};
333     auto A = make_shared<op::Parameter>(element::f64, shape_a);
334     Shape shape_rt{};
335     auto f = make_shared<Function>(make_shared<op::Max>(A, AxisSet{0, 1, 2}), ParameterVector{A});
336
337 std:
338     vector<double> a{1,  2,  3,  4,  5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
339                      13, 12, 11, 10, 9, 8, 7, 6, 5, 4,  3,  2,  1};
340
341     auto test_case = test::TestCase<TestEngine>(f);
342     test_case.add_input<double>({a});
343     test_case.add_expected_output<double>(shape_rt, {14});
344     test_case.run();
345 }
346
347 NGRAPH_TEST(${BACKEND_NAME}, max_3d_eliminate_zero_dim)
348 {
349     Shape shape_a{3, 0, 2};
350     auto A = make_shared<op::Parameter>(element::f32, shape_a);
351     Shape shape_rt{3, 2};
352     auto f = make_shared<Function>(make_shared<op::Max>(A, AxisSet{1}), ParameterVector{A});
353
354     std::vector<float> a{};
355
356     auto test_case = test::TestCase<TestEngine>(f);
357     test_case.add_input<float>({a});
358     test_case.add_expected_output<float>(shape_rt,
359                                          {-std::numeric_limits<float>::infinity(),
360                                           -std::numeric_limits<float>::infinity(),
361                                           -std::numeric_limits<float>::infinity(),
362                                           -std::numeric_limits<float>::infinity(),
363                                           -std::numeric_limits<float>::infinity(),
364                                           -std::numeric_limits<float>::infinity()});
365     test_case.run();
366 }