1 //*****************************************************************************
2 // Copyright 2017-2020 Intel Corporation
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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 //*****************************************************************************
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"
23 NGRAPH_SUPPRESS_DEPRECATED_START
26 using namespace ngraph;
28 static string s_manifest = "${MANIFEST}";
29 using TestEngine = test::ENGINE_CLASS_NAME(${BACKEND_NAME});
31 // Trivial case with no reduced axes.
32 NGRAPH_TEST(${BACKEND_NAME}, max_trivial)
35 auto A = make_shared<op::Parameter>(element::f32, shape);
36 auto f = make_shared<Function>(make_shared<op::Max>(A, AxisSet{}), ParameterVector{A});
38 std::vector<float> a{1, 2, 3, 4};
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);
46 NGRAPH_TEST(${BACKEND_NAME}, max_trivial_int8)
49 auto A = make_shared<op::Parameter>(element::i8, shape);
50 auto f = make_shared<Function>(make_shared<op::Max>(A, AxisSet{}), ParameterVector{A});
52 std::vector<int8_t> a{1, 2, 3, 4};
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});
60 // Failure has been reported at 5D for some reason
61 NGRAPH_TEST(${BACKEND_NAME}, max_trivial_5d)
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});
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};
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);
77 NGRAPH_TEST(${BACKEND_NAME}, max_trivial_5d_int32)
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});
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};
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});
93 NGRAPH_TEST(${BACKEND_NAME}, max_to_scalar)
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});
99 std::vector<float> a{1, 2, 3, 4};
101 auto test_case = test::TestCase<TestEngine>(f);
102 test_case.add_input<float>(a);
103 test_case.add_expected_output<float>(Shape{}, {4});
107 NGRAPH_TEST(${BACKEND_NAME}, max_to_scalar_int8)
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});
113 std::vector<int8_t> a{1, 2, 3, 4};
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});
121 NGRAPH_TEST(${BACKEND_NAME}, max_matrix_columns)
124 auto A = make_shared<op::Parameter>(element::f32, shape_a);
126 auto f = make_shared<Function>(make_shared<op::Max>(A, AxisSet{0}), ParameterVector{A});
128 std::vector<float> a{1, 2, 3, 4, 5, 6};
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});
136 NGRAPH_TEST(${BACKEND_NAME}, max_matrix_rows)
139 auto A = make_shared<op::Parameter>(element::f32, shape_a);
141 auto f = make_shared<Function>(make_shared<op::Max>(A, AxisSet{1}), ParameterVector{A});
143 std::vector<float> a{1, 2, 3, 4, 5, 6};
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});
151 NGRAPH_TEST(${BACKEND_NAME}, max_matrix_rows_int32)
154 auto A = make_shared<op::Parameter>(element::i32, shape_a);
156 auto f = make_shared<Function>(make_shared<op::Max>(A, AxisSet{1}), ParameterVector{A});
158 std::vector<int32_t> a{1, 2, 3, 4, 5, 6};
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});
166 NGRAPH_TEST(${BACKEND_NAME}, max_matrix_rows_zero)
169 auto A = make_shared<op::Parameter>(element::f32, shape_a);
171 auto f = make_shared<Function>(make_shared<op::Max>(A, AxisSet{1}), ParameterVector{A});
173 std::vector<float> a{};
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()});
184 NGRAPH_TEST(${BACKEND_NAME}, max_matrix_rows_zero_int32)
187 auto A = make_shared<op::Parameter>(element::i32, shape_a);
189 auto f = make_shared<Function>(make_shared<op::Max>(A, AxisSet{1}), ParameterVector{A});
191 std::vector<int32_t> a{};
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();
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});
203 NGRAPH_TEST(${BACKEND_NAME}, max_matrix_cols_zero)
205 // Now the reduction (g(x:float32[2,2],y:float32[]) = reduce(x,y,f,axes={})).
207 auto A = make_shared<op::Parameter>(element::f32, shape_a);
209 auto f = make_shared<Function>(make_shared<op::Max>(A, AxisSet{0}), ParameterVector{A});
211 std::vector<float> a{};
213 auto test_case = test::TestCase<TestEngine>(f);
214 test_case.add_input<float>({a});
215 test_case.add_expected_output<float>(
217 {-std::numeric_limits<float>::infinity(), -std::numeric_limits<float>::infinity()});
221 NGRAPH_TEST(${BACKEND_NAME}, max_vector_zero)
224 auto A = make_shared<op::Parameter>(element::f32, shape_a);
226 auto f = make_shared<Function>(make_shared<op::Max>(A, AxisSet{0}), ParameterVector{A});
228 std::vector<float> a{};
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()});
236 NGRAPH_TEST(${BACKEND_NAME}, max_matrix_to_scalar_zero_by_zero)
239 auto A = make_shared<op::Parameter>(element::f32, shape_a);
241 auto f = make_shared<Function>(make_shared<op::Max>(A, AxisSet{0, 1}), ParameterVector{A});
243 std::vector<float> a{};
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()});
251 NGRAPH_TEST(${BACKEND_NAME}, max_3d_to_matrix_most_sig)
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});
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};
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);
267 NGRAPH_TEST(${BACKEND_NAME}, max_3d_to_matrix_least_sig)
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});
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};
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);
283 NGRAPH_TEST(${BACKEND_NAME}, max_3d_to_vector)
285 Shape shape_a{3, 3, 3};
286 auto A = make_shared<op::Parameter>(element::f32, shape_a);
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};
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);
298 NGRAPH_TEST(${BACKEND_NAME}, max_3d_to_scalar)
300 Shape shape_a{3, 3, 3};
301 auto A = make_shared<op::Parameter>(element::f32, shape_a);
303 auto f = make_shared<Function>(make_shared<op::Max>(A, AxisSet{0, 1, 2}), ParameterVector{A});
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};
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);
314 NGRAPH_TEST(${BACKEND_NAME}, max_3d_to_scalar_int32)
316 Shape shape_a{3, 3, 3};
317 auto A = make_shared<op::Parameter>(element::i32, shape_a);
319 auto f = make_shared<Function>(make_shared<op::Max>(A, AxisSet{0, 1, 2}), ParameterVector{A});
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};
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});
330 NGRAPH_TEST(${BACKEND_NAME}, max_3d_to_scalar_double)
332 Shape shape_a{3, 3, 3};
333 auto A = make_shared<op::Parameter>(element::f64, shape_a);
335 auto f = make_shared<Function>(make_shared<op::Max>(A, AxisSet{0, 1, 2}), ParameterVector{A});
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};
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});
347 NGRAPH_TEST(${BACKEND_NAME}, max_3d_eliminate_zero_dim)
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});
354 std::vector<float> a{};
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()});