Deprecate nGraph v0 ops and builders (#1856)
[platform/upstream/dldt.git] / ngraph / test / backend / reverse.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 "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"
26
27 NGRAPH_SUPPRESS_DEPRECATED_START
28
29 using namespace std;
30 using namespace ngraph;
31
32 static string s_manifest = "${MANIFEST}";
33
34 NGRAPH_TEST(${BACKEND_NAME}, reverse_0d)
35 {
36     Shape shape{};
37     auto A = make_shared<op::Parameter>(element::f32, shape);
38     auto f = make_shared<Function>(make_shared<op::Reverse>(A, AxisSet{}), ParameterVector{A});
39
40     auto backend = runtime::Backend::create("${BACKEND_NAME}");
41
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);
46
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));
51 }
52
53 NGRAPH_TEST(${BACKEND_NAME}, reverse_1d_nochange)
54 {
55     Shape shape{8};
56     auto A = make_shared<op::Parameter>(element::f32, shape);
57     auto f = make_shared<Function>(make_shared<op::Reverse>(A, AxisSet{}), ParameterVector{A});
58
59     auto backend = runtime::Backend::create("${BACKEND_NAME}");
60
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);
65
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));
71 }
72
73 NGRAPH_TEST(${BACKEND_NAME}, reverse_1d_0)
74 {
75     Shape shape{8};
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});
78
79     auto backend = runtime::Backend::create("${BACKEND_NAME}");
80
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);
85
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));
91 }
92
93 NGRAPH_TEST(${BACKEND_NAME}, reverse_2d_nochange)
94 {
95     Shape shape{4, 3};
96     auto A = make_shared<op::Parameter>(element::f32, shape);
97     auto f = make_shared<Function>(make_shared<op::Reverse>(A, AxisSet{}), ParameterVector{A});
98
99     auto backend = runtime::Backend::create("${BACKEND_NAME}");
100
101     // Create some tensors for input/output
102     auto a = backend->create_tensor(element::f32, shape);
103     copy_data(a,
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);
106
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));
113 }
114
115 NGRAPH_TEST(${BACKEND_NAME}, reverse_2d_0)
116 {
117     Shape shape{4, 3};
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});
120
121     auto backend = runtime::Backend::create("${BACKEND_NAME}");
122
123     // Create some tensors for input/output
124     auto a = backend->create_tensor(element::f32, shape);
125     copy_data(a,
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);
128
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));
135 }
136
137 NGRAPH_TEST(${BACKEND_NAME}, reverse_2d_1)
138 {
139     Shape shape{4, 3};
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});
142
143     auto backend = runtime::Backend::create("${BACKEND_NAME}");
144
145     // Create some tensors for input/output
146     auto a = backend->create_tensor(element::f32, shape);
147     copy_data(a,
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);
150
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));
157 }
158
159 NGRAPH_TEST(${BACKEND_NAME}, reverse_2d_01)
160 {
161     Shape shape{4, 3};
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});
164
165     auto backend = runtime::Backend::create("${BACKEND_NAME}");
166
167     // Create some tensors for input/output
168     auto a = backend->create_tensor(element::f32, shape);
169     copy_data(a,
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);
172
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));
179 }
180
181 NGRAPH_TEST(${BACKEND_NAME}, reverse_3d_nochange)
182 {
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});
186
187     auto backend = runtime::Backend::create("${BACKEND_NAME}");
188
189     // Create some tensors for input/output
190     auto a = backend->create_tensor(element::f32, shape);
191     copy_data(a,
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}}})
194                   .get_vector());
195     auto result = backend->create_tensor(element::f32, shape);
196
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}}})
202              .get_vector()),
203         read_vector<float>(result),
204         MIN_FLOAT_TOLERANCE_BITS));
205 }
206
207 NGRAPH_TEST(${BACKEND_NAME}, reverse_3d_0)
208 {
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});
212
213     auto backend = runtime::Backend::create("${BACKEND_NAME}");
214
215     // Create some tensors for input/output
216     auto a = backend->create_tensor(element::f32, shape);
217     copy_data(a,
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}}})
220                   .get_vector());
221     auto result = backend->create_tensor(element::f32, shape);
222
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}}})
228              .get_vector()),
229         read_vector<float>(result),
230         MIN_FLOAT_TOLERANCE_BITS));
231 }
232
233 NGRAPH_TEST(${BACKEND_NAME}, reverse_3d_1)
234 {
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});
238
239     auto backend = runtime::Backend::create("${BACKEND_NAME}");
240
241     // Create some tensors for input/output
242     auto a = backend->create_tensor(element::f32, shape);
243     copy_data(a,
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}}})
246                   .get_vector());
247     auto result = backend->create_tensor(element::f32, shape);
248
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}}})
254              .get_vector()),
255         read_vector<float>(result),
256         MIN_FLOAT_TOLERANCE_BITS));
257 }
258
259 NGRAPH_TEST(${BACKEND_NAME}, reverse_3d_2)
260 {
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});
264
265     auto backend = runtime::Backend::create("${BACKEND_NAME}");
266
267     // Create some tensors for input/output
268     auto a = backend->create_tensor(element::f32, shape);
269     copy_data(a,
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}}})
272                   .get_vector());
273     auto result = backend->create_tensor(element::f32, shape);
274
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}}})
280              .get_vector()),
281         read_vector<float>(result),
282         MIN_FLOAT_TOLERANCE_BITS));
283 }
284
285 NGRAPH_TEST(${BACKEND_NAME}, reverse_3d_01)
286 {
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});
290
291     auto backend = runtime::Backend::create("${BACKEND_NAME}");
292
293     // Create some tensors for input/output
294     auto a = backend->create_tensor(element::f32, shape);
295     copy_data(a,
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}}})
298                   .get_vector());
299     auto result = backend->create_tensor(element::f32, shape);
300
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}}})
306              .get_vector()),
307         read_vector<float>(result),
308         MIN_FLOAT_TOLERANCE_BITS));
309 }
310
311 NGRAPH_TEST(${BACKEND_NAME}, reverse_3d_02)
312 {
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});
316
317     auto backend = runtime::Backend::create("${BACKEND_NAME}");
318
319     // Create some tensors for input/output
320     auto a = backend->create_tensor(element::f32, shape);
321     copy_data(a,
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}}})
324                   .get_vector());
325     auto result = backend->create_tensor(element::f32, shape);
326
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}}})
332              .get_vector()),
333         read_vector<float>(result),
334         MIN_FLOAT_TOLERANCE_BITS));
335 }
336
337 NGRAPH_TEST(${BACKEND_NAME}, reverse_3d_12)
338 {
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});
342
343     auto backend = runtime::Backend::create("${BACKEND_NAME}");
344
345     // Create some tensors for input/output
346     auto a = backend->create_tensor(element::f32, shape);
347     copy_data(a,
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}}})
350                   .get_vector());
351     auto result = backend->create_tensor(element::f32, shape);
352
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}}})
358              .get_vector()),
359         read_vector<float>(result),
360         MIN_FLOAT_TOLERANCE_BITS));
361 }
362
363 NGRAPH_TEST(${BACKEND_NAME}, reverse_3d_012)
364 {
365     Shape shape{2, 4, 3};
366     auto A = make_shared<op::Parameter>(element::f32, shape);
367     auto f =
368         make_shared<Function>(make_shared<op::Reverse>(A, AxisSet{0, 1, 2}), ParameterVector{A});
369
370     auto backend = runtime::Backend::create("${BACKEND_NAME}");
371
372     // Create some tensors for input/output
373     auto a = backend->create_tensor(element::f32, shape);
374     copy_data(a,
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}}})
377                   .get_vector());
378     auto result = backend->create_tensor(element::f32, shape);
379
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}}})
385              .get_vector()),
386         read_vector<float>(result),
387         MIN_FLOAT_TOLERANCE_BITS));
388 }
389
390 NGRAPH_TEST(${BACKEND_NAME}, reverse_v1_incorrect_rev_axes_rank_index_mode)
391 {
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
394
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);
399 }
400
401 NGRAPH_TEST(${BACKEND_NAME}, reverse_v1_incorrect_rev_axes_elems_mask_mode)
402 {
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
405
406     EXPECT_THROW(make_shared<op::v1::Reverse>(Data, Rev_Axes, op::v1::Reverse::Mode::MASK),
407                  ngraph::NodeValidationFailure);
408 }
409
410 NGRAPH_TEST(${BACKEND_NAME}, reverse_v1_axes_out_of_bounds)
411 {
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});
414
415     EXPECT_THROW(make_shared<op::v1::Reverse>(Data, Rev_Axes, op::v1::Reverse::Mode::INDEX),
416                  ngraph::NodeValidationFailure);
417 }
418
419 NGRAPH_TEST(${BACKEND_NAME}, reverse_v1_too_many_axes)
420 {
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});
423
424     EXPECT_THROW(make_shared<op::v1::Reverse>(Data, Rev_Axes, op::v1::Reverse::Mode::INDEX),
425                  ngraph::NodeValidationFailure);
426 }