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 "ngraph/runtime/tensor.hpp"
20 #include "runtime/backend.hpp"
21 #include "util/all_close.hpp"
22 #include "util/all_close_f.hpp"
23 #include "util/ndarray.hpp"
24 #include "util/test_control.hpp"
25 #include "util/test_tools.hpp"
27 NGRAPH_SUPPRESS_DEPRECATED_START
30 using namespace ngraph;
32 static string s_manifest = "${MANIFEST}";
34 NGRAPH_TEST(${BACKEND_NAME}, reverse_0d)
37 auto A = make_shared<op::Parameter>(element::f32, shape);
38 auto f = make_shared<Function>(make_shared<op::Reverse>(A, AxisSet{}), ParameterVector{A});
40 auto backend = runtime::Backend::create("${BACKEND_NAME}");
42 // Create some tensors for input/output
43 auto a = backend->create_tensor(element::f32, shape);
44 copy_data(a, vector<float>{6});
45 auto result = backend->create_tensor(element::f32, shape);
47 auto handle = backend->compile(f);
48 handle->call_with_validate({result}, {a});
49 EXPECT_TRUE(test::all_close_f(
50 (vector<float>{6}), read_vector<float>(result), MIN_FLOAT_TOLERANCE_BITS));
53 NGRAPH_TEST(${BACKEND_NAME}, reverse_1d_nochange)
56 auto A = make_shared<op::Parameter>(element::f32, shape);
57 auto f = make_shared<Function>(make_shared<op::Reverse>(A, AxisSet{}), ParameterVector{A});
59 auto backend = runtime::Backend::create("${BACKEND_NAME}");
61 // Create some tensors for input/output
62 auto a = backend->create_tensor(element::f32, shape);
63 copy_data(a, vector<float>{0, 1, 2, 3, 4, 5, 6, 7});
64 auto result = backend->create_tensor(element::f32, shape);
66 auto handle = backend->compile(f);
67 handle->call_with_validate({result}, {a});
68 EXPECT_TRUE(test::all_close_f((vector<float>{0, 1, 2, 3, 4, 5, 6, 7}),
69 read_vector<float>(result),
70 MIN_FLOAT_TOLERANCE_BITS));
73 NGRAPH_TEST(${BACKEND_NAME}, reverse_1d_0)
76 auto A = make_shared<op::Parameter>(element::f32, shape);
77 auto f = make_shared<Function>(make_shared<op::Reverse>(A, AxisSet{0}), ParameterVector{A});
79 auto backend = runtime::Backend::create("${BACKEND_NAME}");
81 // Create some tensors for input/output
82 auto a = backend->create_tensor(element::f32, shape);
83 copy_data(a, vector<float>{0, 1, 2, 3, 4, 5, 6, 7});
84 auto result = backend->create_tensor(element::f32, shape);
86 auto handle = backend->compile(f);
87 handle->call_with_validate({result}, {a});
88 EXPECT_TRUE(test::all_close_f((vector<float>{7, 6, 5, 4, 3, 2, 1, 0}),
89 read_vector<float>(result),
90 MIN_FLOAT_TOLERANCE_BITS));
93 NGRAPH_TEST(${BACKEND_NAME}, reverse_2d_nochange)
96 auto A = make_shared<op::Parameter>(element::f32, shape);
97 auto f = make_shared<Function>(make_shared<op::Reverse>(A, AxisSet{}), ParameterVector{A});
99 auto backend = runtime::Backend::create("${BACKEND_NAME}");
101 // Create some tensors for input/output
102 auto a = backend->create_tensor(element::f32, shape);
104 test::NDArray<float, 2>({{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, {9, 10, 11}}).get_vector());
105 auto result = backend->create_tensor(element::f32, shape);
107 auto handle = backend->compile(f);
108 handle->call_with_validate({result}, {a});
109 EXPECT_TRUE(test::all_close_f(
110 (test::NDArray<float, 2>({{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, {9, 10, 11}}).get_vector()),
111 read_vector<float>(result),
112 MIN_FLOAT_TOLERANCE_BITS));
115 NGRAPH_TEST(${BACKEND_NAME}, reverse_2d_0)
118 auto A = make_shared<op::Parameter>(element::f32, shape);
119 auto f = make_shared<Function>(make_shared<op::Reverse>(A, AxisSet{0}), ParameterVector{A});
121 auto backend = runtime::Backend::create("${BACKEND_NAME}");
123 // Create some tensors for input/output
124 auto a = backend->create_tensor(element::f32, shape);
126 test::NDArray<float, 2>({{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, {9, 10, 11}}).get_vector());
127 auto result = backend->create_tensor(element::f32, shape);
129 auto handle = backend->compile(f);
130 handle->call_with_validate({result}, {a});
131 EXPECT_TRUE(test::all_close_f(
132 (test::NDArray<float, 2>({{9, 10, 11}, {6, 7, 8}, {3, 4, 5}, {0, 1, 2}}).get_vector()),
133 read_vector<float>(result),
134 MIN_FLOAT_TOLERANCE_BITS));
137 NGRAPH_TEST(${BACKEND_NAME}, reverse_2d_1)
140 auto A = make_shared<op::Parameter>(element::f32, shape);
141 auto f = make_shared<Function>(make_shared<op::Reverse>(A, AxisSet{1}), ParameterVector{A});
143 auto backend = runtime::Backend::create("${BACKEND_NAME}");
145 // Create some tensors for input/output
146 auto a = backend->create_tensor(element::f32, shape);
148 test::NDArray<float, 2>({{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, {9, 10, 11}}).get_vector());
149 auto result = backend->create_tensor(element::f32, shape);
151 auto handle = backend->compile(f);
152 handle->call_with_validate({result}, {a});
153 EXPECT_TRUE(test::all_close_f(
154 (test::NDArray<float, 2>({{2, 1, 0}, {5, 4, 3}, {8, 7, 6}, {11, 10, 9}}).get_vector()),
155 read_vector<float>(result),
156 MIN_FLOAT_TOLERANCE_BITS));
159 NGRAPH_TEST(${BACKEND_NAME}, reverse_2d_01)
162 auto A = make_shared<op::Parameter>(element::f32, shape);
163 auto f = make_shared<Function>(make_shared<op::Reverse>(A, AxisSet{0, 1}), ParameterVector{A});
165 auto backend = runtime::Backend::create("${BACKEND_NAME}");
167 // Create some tensors for input/output
168 auto a = backend->create_tensor(element::f32, shape);
170 test::NDArray<float, 2>({{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, {9, 10, 11}}).get_vector());
171 auto result = backend->create_tensor(element::f32, shape);
173 auto handle = backend->compile(f);
174 handle->call_with_validate({result}, {a});
175 EXPECT_TRUE(test::all_close_f(
176 (test::NDArray<float, 2>({{11, 10, 9}, {8, 7, 6}, {5, 4, 3}, {2, 1, 0}}).get_vector()),
177 read_vector<float>(result),
178 MIN_FLOAT_TOLERANCE_BITS));
181 NGRAPH_TEST(${BACKEND_NAME}, reverse_3d_nochange)
183 Shape shape{2, 4, 3};
184 auto A = make_shared<op::Parameter>(element::f32, shape);
185 auto f = make_shared<Function>(make_shared<op::Reverse>(A, AxisSet{}), ParameterVector{A});
187 auto backend = runtime::Backend::create("${BACKEND_NAME}");
189 // Create some tensors for input/output
190 auto a = backend->create_tensor(element::f32, shape);
192 test::NDArray<float, 3>({{{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, {9, 10, 11}},
193 {{12, 13, 14}, {15, 16, 17}, {18, 19, 20}, {21, 22, 23}}})
195 auto result = backend->create_tensor(element::f32, shape);
197 auto handle = backend->compile(f);
198 handle->call_with_validate({result}, {a});
199 EXPECT_TRUE(test::all_close_f(
200 (test::NDArray<float, 3>({{{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, {9, 10, 11}},
201 {{12, 13, 14}, {15, 16, 17}, {18, 19, 20}, {21, 22, 23}}})
203 read_vector<float>(result),
204 MIN_FLOAT_TOLERANCE_BITS));
207 NGRAPH_TEST(${BACKEND_NAME}, reverse_3d_0)
209 Shape shape{2, 4, 3};
210 auto A = make_shared<op::Parameter>(element::f32, shape);
211 auto f = make_shared<Function>(make_shared<op::Reverse>(A, AxisSet{0}), ParameterVector{A});
213 auto backend = runtime::Backend::create("${BACKEND_NAME}");
215 // Create some tensors for input/output
216 auto a = backend->create_tensor(element::f32, shape);
218 test::NDArray<float, 3>({{{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, {9, 10, 11}},
219 {{12, 13, 14}, {15, 16, 17}, {18, 19, 20}, {21, 22, 23}}})
221 auto result = backend->create_tensor(element::f32, shape);
223 auto handle = backend->compile(f);
224 handle->call_with_validate({result}, {a});
225 EXPECT_TRUE(test::all_close_f(
226 (test::NDArray<float, 3>({{{12, 13, 14}, {15, 16, 17}, {18, 19, 20}, {21, 22, 23}},
227 {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, {9, 10, 11}}})
229 read_vector<float>(result),
230 MIN_FLOAT_TOLERANCE_BITS));
233 NGRAPH_TEST(${BACKEND_NAME}, reverse_3d_1)
235 Shape shape{2, 4, 3};
236 auto A = make_shared<op::Parameter>(element::f32, shape);
237 auto f = make_shared<Function>(make_shared<op::Reverse>(A, AxisSet{1}), ParameterVector{A});
239 auto backend = runtime::Backend::create("${BACKEND_NAME}");
241 // Create some tensors for input/output
242 auto a = backend->create_tensor(element::f32, shape);
244 test::NDArray<float, 3>({{{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, {9, 10, 11}},
245 {{12, 13, 14}, {15, 16, 17}, {18, 19, 20}, {21, 22, 23}}})
247 auto result = backend->create_tensor(element::f32, shape);
249 auto handle = backend->compile(f);
250 handle->call_with_validate({result}, {a});
251 EXPECT_TRUE(test::all_close_f(
252 (test::NDArray<float, 3>({{{9, 10, 11}, {6, 7, 8}, {3, 4, 5}, {0, 1, 2}},
253 {{21, 22, 23}, {18, 19, 20}, {15, 16, 17}, {12, 13, 14}}})
255 read_vector<float>(result),
256 MIN_FLOAT_TOLERANCE_BITS));
259 NGRAPH_TEST(${BACKEND_NAME}, reverse_3d_2)
261 Shape shape{2, 4, 3};
262 auto A = make_shared<op::Parameter>(element::f32, shape);
263 auto f = make_shared<Function>(make_shared<op::Reverse>(A, AxisSet{2}), ParameterVector{A});
265 auto backend = runtime::Backend::create("${BACKEND_NAME}");
267 // Create some tensors for input/output
268 auto a = backend->create_tensor(element::f32, shape);
270 test::NDArray<float, 3>({{{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, {9, 10, 11}},
271 {{12, 13, 14}, {15, 16, 17}, {18, 19, 20}, {21, 22, 23}}})
273 auto result = backend->create_tensor(element::f32, shape);
275 auto handle = backend->compile(f);
276 handle->call_with_validate({result}, {a});
277 EXPECT_TRUE(test::all_close_f(
278 (test::NDArray<float, 3>({{{2, 1, 0}, {5, 4, 3}, {8, 7, 6}, {11, 10, 9}},
279 {{14, 13, 12}, {17, 16, 15}, {20, 19, 18}, {23, 22, 21}}})
281 read_vector<float>(result),
282 MIN_FLOAT_TOLERANCE_BITS));
285 NGRAPH_TEST(${BACKEND_NAME}, reverse_3d_01)
287 Shape shape{2, 4, 3};
288 auto A = make_shared<op::Parameter>(element::f32, shape);
289 auto f = make_shared<Function>(make_shared<op::Reverse>(A, AxisSet{0, 1}), ParameterVector{A});
291 auto backend = runtime::Backend::create("${BACKEND_NAME}");
293 // Create some tensors for input/output
294 auto a = backend->create_tensor(element::f32, shape);
296 test::NDArray<float, 3>({{{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, {9, 10, 11}},
297 {{12, 13, 14}, {15, 16, 17}, {18, 19, 20}, {21, 22, 23}}})
299 auto result = backend->create_tensor(element::f32, shape);
301 auto handle = backend->compile(f);
302 handle->call_with_validate({result}, {a});
303 EXPECT_TRUE(test::all_close_f(
304 (test::NDArray<float, 3>({{{21, 22, 23}, {18, 19, 20}, {15, 16, 17}, {12, 13, 14}},
305 {{9, 10, 11}, {6, 7, 8}, {3, 4, 5}, {0, 1, 2}}})
307 read_vector<float>(result),
308 MIN_FLOAT_TOLERANCE_BITS));
311 NGRAPH_TEST(${BACKEND_NAME}, reverse_3d_02)
313 Shape shape{2, 4, 3};
314 auto A = make_shared<op::Parameter>(element::f32, shape);
315 auto f = make_shared<Function>(make_shared<op::Reverse>(A, AxisSet{0, 2}), ParameterVector{A});
317 auto backend = runtime::Backend::create("${BACKEND_NAME}");
319 // Create some tensors for input/output
320 auto a = backend->create_tensor(element::f32, shape);
322 test::NDArray<float, 3>({{{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, {9, 10, 11}},
323 {{12, 13, 14}, {15, 16, 17}, {18, 19, 20}, {21, 22, 23}}})
325 auto result = backend->create_tensor(element::f32, shape);
327 auto handle = backend->compile(f);
328 handle->call_with_validate({result}, {a});
329 EXPECT_TRUE(test::all_close_f(
330 (test::NDArray<float, 3>({{{14, 13, 12}, {17, 16, 15}, {20, 19, 18}, {23, 22, 21}},
331 {{2, 1, 0}, {5, 4, 3}, {8, 7, 6}, {11, 10, 9}}})
333 read_vector<float>(result),
334 MIN_FLOAT_TOLERANCE_BITS));
337 NGRAPH_TEST(${BACKEND_NAME}, reverse_3d_12)
339 Shape shape{2, 4, 3};
340 auto A = make_shared<op::Parameter>(element::f32, shape);
341 auto f = make_shared<Function>(make_shared<op::Reverse>(A, AxisSet{1, 2}), ParameterVector{A});
343 auto backend = runtime::Backend::create("${BACKEND_NAME}");
345 // Create some tensors for input/output
346 auto a = backend->create_tensor(element::f32, shape);
348 test::NDArray<float, 3>({{{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, {9, 10, 11}},
349 {{12, 13, 14}, {15, 16, 17}, {18, 19, 20}, {21, 22, 23}}})
351 auto result = backend->create_tensor(element::f32, shape);
353 auto handle = backend->compile(f);
354 handle->call_with_validate({result}, {a});
355 EXPECT_TRUE(test::all_close_f(
356 (test::NDArray<float, 3>({{{11, 10, 9}, {8, 7, 6}, {5, 4, 3}, {2, 1, 0}},
357 {{23, 22, 21}, {20, 19, 18}, {17, 16, 15}, {14, 13, 12}}})
359 read_vector<float>(result),
360 MIN_FLOAT_TOLERANCE_BITS));
363 NGRAPH_TEST(${BACKEND_NAME}, reverse_3d_012)
365 Shape shape{2, 4, 3};
366 auto A = make_shared<op::Parameter>(element::f32, shape);
368 make_shared<Function>(make_shared<op::Reverse>(A, AxisSet{0, 1, 2}), ParameterVector{A});
370 auto backend = runtime::Backend::create("${BACKEND_NAME}");
372 // Create some tensors for input/output
373 auto a = backend->create_tensor(element::f32, shape);
375 test::NDArray<float, 3>({{{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, {9, 10, 11}},
376 {{12, 13, 14}, {15, 16, 17}, {18, 19, 20}, {21, 22, 23}}})
378 auto result = backend->create_tensor(element::f32, shape);
380 auto handle = backend->compile(f);
381 handle->call_with_validate({result}, {a});
382 EXPECT_TRUE(test::all_close_f(
383 (test::NDArray<float, 3>({{{23, 22, 21}, {20, 19, 18}, {17, 16, 15}, {14, 13, 12}},
384 {{11, 10, 9}, {8, 7, 6}, {5, 4, 3}, {2, 1, 0}}})
386 read_vector<float>(result),
387 MIN_FLOAT_TOLERANCE_BITS));
390 NGRAPH_TEST(${BACKEND_NAME}, reverse_v1_incorrect_rev_axes_rank_index_mode)
392 const auto Data = make_shared<op::Parameter>(element::f32, Shape{2, 2, 2});
393 const auto Rev_Axes = make_shared<op::Parameter>(element::i64, Shape{1, 1}); // correct: 1D
395 EXPECT_THROW(make_shared<Function>(
396 make_shared<op::v1::Reverse>(Data, Rev_Axes, op::v1::Reverse::Mode::INDEX),
397 ParameterVector{Data, Rev_Axes}),
398 ngraph::NodeValidationFailure);
401 NGRAPH_TEST(${BACKEND_NAME}, reverse_v1_incorrect_rev_axes_elems_mask_mode)
403 const auto Data = make_shared<op::Parameter>(element::f32, Shape{2, 2, 2});
404 const auto Rev_Axes = make_shared<op::Parameter>(element::boolean, Shape{2}); // correct: 3
406 EXPECT_THROW(make_shared<op::v1::Reverse>(Data, Rev_Axes, op::v1::Reverse::Mode::MASK),
407 ngraph::NodeValidationFailure);
410 NGRAPH_TEST(${BACKEND_NAME}, reverse_v1_axes_out_of_bounds)
412 const auto Data = make_shared<op::Parameter>(element::f32, Shape{2, 2, 2});
413 const auto Rev_Axes = op::Constant::create(element::i64, Shape{2}, {1, 10});
415 EXPECT_THROW(make_shared<op::v1::Reverse>(Data, Rev_Axes, op::v1::Reverse::Mode::INDEX),
416 ngraph::NodeValidationFailure);
419 NGRAPH_TEST(${BACKEND_NAME}, reverse_v1_too_many_axes)
421 const auto Data = make_shared<op::Parameter>(element::f32, Shape{2, 2, 2});
422 const auto Rev_Axes = op::Constant::create(element::i64, Shape{4}, {0, 1, 2, 3});
424 EXPECT_THROW(make_shared<op::v1::Reverse>(Data, Rev_Axes, op::v1::Reverse::Mode::INDEX),
425 ngraph::NodeValidationFailure);