Deprecate nGraph v0 ops and builders (#1856)
[platform/upstream/dldt.git] / ngraph / test / backend / slice.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}, slice_scalar)
35 {
36     Shape shape_a{};
37     auto A = make_shared<op::Parameter>(element::f32, shape_a);
38     Shape shape_r{};
39     auto r = make_shared<op::Slice>(A, Coordinate{}, Coordinate{});
40     auto f = make_shared<Function>(make_shared<op::v0::Abs>(r), ParameterVector{A});
41
42     auto backend = runtime::Backend::create("${BACKEND_NAME}");
43
44     // Create some tensors for input/output
45     auto a = backend->create_tensor(element::f32, shape_a);
46     copy_data(a, vector<float>{312});
47     auto result = backend->create_tensor(element::f32, shape_r);
48
49     auto handle = backend->compile(f);
50     handle->call_with_validate({result}, {a});
51     EXPECT_TRUE(test::all_close_f(
52         (vector<float>{312}), read_vector<float>(result), MIN_FLOAT_TOLERANCE_BITS));
53 }
54
55 NGRAPH_TEST(${BACKEND_NAME}, slice_matrix)
56 {
57     Shape shape_a{4, 4};
58     auto A = make_shared<op::Parameter>(element::f32, shape_a);
59     Shape shape_r{3, 2};
60     auto r = make_shared<op::Slice>(A, Coordinate{0, 1}, Coordinate{3, 3});
61     auto f = make_shared<Function>(r, ParameterVector{A});
62
63     auto backend = runtime::Backend::create("${BACKEND_NAME}");
64
65     // Create some tensors for input/output
66     auto a = backend->create_tensor(element::f32, shape_a);
67     copy_data(a, vector<float>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16});
68     auto result = backend->create_tensor(element::f32, shape_r);
69
70     auto handle = backend->compile(f);
71     handle->call_with_validate({result}, {a});
72     EXPECT_TRUE(test::all_close_f(
73         (vector<float>{2, 3, 6, 7, 10, 11}), read_vector<float>(result), MIN_FLOAT_TOLERANCE_BITS));
74 }
75
76 NGRAPH_TEST(${BACKEND_NAME}, slice_vector)
77 {
78     Shape shape_a{16};
79     auto A = make_shared<op::Parameter>(element::f32, shape_a);
80     Shape shape_r{12};
81     auto r = make_shared<op::Slice>(A, Coordinate{2}, Coordinate{14});
82     auto f = make_shared<Function>(r, ParameterVector{A});
83
84     auto backend = runtime::Backend::create("${BACKEND_NAME}");
85
86     // Create some tensors for input/output
87     auto a = backend->create_tensor(element::f32, shape_a);
88     copy_data(a, vector<float>{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15});
89     auto result = backend->create_tensor(element::f32, shape_r);
90
91     auto handle = backend->compile(f);
92     handle->call_with_validate({result}, {a});
93     EXPECT_TRUE(test::all_close_f((vector<float>{2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}),
94                                   read_vector<float>(result),
95                                   MIN_FLOAT_TOLERANCE_BITS));
96 }
97
98 NGRAPH_TEST(${BACKEND_NAME}, slice_matrix_axis_0_overlap)
99 {
100     Shape shape_a{4, 4};
101     auto A = make_shared<op::Parameter>(element::f32, shape_a);
102     auto B = make_shared<op::Parameter>(element::f32, shape_a);
103     auto C = make_shared<op::Add>(A, B);
104     Shape shape_r{2, 4};
105     auto D = make_shared<op::Slice>(C, Coordinate{0, 0}, Coordinate{2, 4});
106     auto E = make_shared<op::Slice>(C, Coordinate{1, 0}, Coordinate{3, 4});
107     auto r = make_shared<op::Add>(D, E);
108     auto f = make_shared<Function>(r, ParameterVector{A, B});
109
110     auto backend = runtime::Backend::create("${BACKEND_NAME}");
111
112     // Create some tensors for input/output
113     auto a = backend->create_tensor(element::f32, shape_a);
114     copy_data(a, vector<float>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16});
115     auto b = backend->create_tensor(element::f32, shape_a);
116     copy_data(b, vector<float>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16});
117     auto result = backend->create_tensor(element::f32, shape_r);
118
119     auto handle = backend->compile(f);
120     handle->call_with_validate({result}, {a, b});
121     EXPECT_TRUE(test::all_close_f((vector<float>{12, 16, 20, 24, 28, 32, 36, 40}),
122                                   read_vector<float>(result),
123                                   MIN_FLOAT_TOLERANCE_BITS));
124 }
125
126 NGRAPH_TEST(${BACKEND_NAME}, slice_matrix_axis_0_in_place)
127 {
128     Shape shape_a{4, 4};
129     auto A = make_shared<op::Parameter>(element::f32, shape_a);
130     Shape shape_r{2, 4};
131     auto D = make_shared<op::Slice>(A, Coordinate{0, 0}, Coordinate{2, 4});
132     auto E = make_shared<op::Slice>(A, Coordinate{2, 0}, Coordinate{4, 4});
133     auto r = make_shared<op::Add>(D, E);
134     auto f = make_shared<Function>(r, ParameterVector{A});
135
136     auto backend = runtime::Backend::create("${BACKEND_NAME}");
137
138     // Create some tensors for input/output
139     auto a = backend->create_tensor(element::f32, shape_a);
140     copy_data(a, vector<float>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16});
141     auto result = backend->create_tensor(element::f32, shape_r);
142
143     auto handle = backend->compile(f);
144     handle->call_with_validate({result}, {a});
145     EXPECT_TRUE(test::all_close_f((vector<float>{10, 12, 14, 16, 18, 20, 22, 24}),
146                                   read_vector<float>(result),
147                                   MIN_FLOAT_TOLERANCE_BITS));
148 }
149
150 NGRAPH_TEST(${BACKEND_NAME}, slice_matrix_axis_0_in_place_twice)
151 {
152     Shape shape_a{4, 4};
153     auto A = make_shared<op::Parameter>(element::f32, shape_a);
154     Shape shape_r{1, 4};
155     auto B = make_shared<op::Slice>(A, Coordinate{0, 0}, Coordinate{2, 4});
156     auto D = make_shared<op::Slice>(B, Coordinate{1, 0}, Coordinate{2, 4});
157     auto E = make_shared<op::Slice>(A, Coordinate{2, 0}, Coordinate{3, 4});
158     auto r = make_shared<op::Add>(D, E);
159     auto f = make_shared<Function>(r, ParameterVector{A});
160
161     auto backend = runtime::Backend::create("${BACKEND_NAME}");
162
163     // Create some tensors for input/output
164     auto a = backend->create_tensor(element::f32, shape_a);
165     copy_data(a, vector<float>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16});
166     auto result = backend->create_tensor(element::f32, shape_r);
167
168     auto handle = backend->compile(f);
169     handle->call_with_validate({result}, {a});
170     EXPECT_TRUE(test::all_close_f(
171         (vector<float>{14, 16, 18, 20}), read_vector<float>(result), MIN_FLOAT_TOLERANCE_BITS));
172 }
173
174 NGRAPH_TEST(${BACKEND_NAME}, slice_matrix_axis_0_in_place_twice_overlap)
175 {
176     Shape shape_a{5, 4};
177     auto A = make_shared<op::Parameter>(element::f32, shape_a);
178     Shape shape_r{2, 4};
179     auto B = make_shared<op::Slice>(A, Coordinate{1, 0}, Coordinate{5, 4});
180     auto D = make_shared<op::Slice>(B, Coordinate{1, 0}, Coordinate{3, 4});
181     auto E = make_shared<op::Slice>(B, Coordinate{2, 0}, Coordinate{4, 4});
182     auto r = make_shared<op::Add>(D, E);
183     auto f = make_shared<Function>(r, ParameterVector{A});
184
185     auto backend = runtime::Backend::create("${BACKEND_NAME}");
186
187     // Create some tensors for input/output
188     auto a = backend->create_tensor(element::f32, shape_a);
189     copy_data(a,
190               vector<float>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20});
191     auto result = backend->create_tensor(element::f32, shape_r);
192
193     auto handle = backend->compile(f);
194     handle->call_with_validate({result}, {a});
195     EXPECT_TRUE(test::all_close_f((vector<float>{22, 24, 26, 28, 30, 32, 34, 36}),
196                                   read_vector<float>(result),
197                                   MIN_FLOAT_TOLERANCE_BITS));
198 }
199
200 NGRAPH_TEST(${BACKEND_NAME}, slice_matrix_axis_0_in_place_with_reshape)
201 {
202     Shape shape_a{4, 5};
203     auto A = make_shared<op::Parameter>(element::f32, shape_a);
204     Shape shape_r{2, 4};
205     auto B = make_shared<op::Slice>(A, Coordinate{1, 0}, Coordinate{4, 5});
206     auto C = make_shared<op::Reshape>(B, AxisVector{1, 0}, Shape{5, 3});
207     auto D = make_shared<op::Slice>(C, Coordinate{1, 0}, Coordinate{5, 3});
208     auto E = make_shared<op::Reshape>(D, AxisVector{1, 0}, Shape{3, 4});
209     auto r = make_shared<op::Slice>(E, Coordinate{1, 0}, Coordinate{3, 4});
210     auto f = make_shared<Function>(r, ParameterVector{A});
211
212     auto backend = runtime::Backend::create("${BACKEND_NAME}");
213
214     // Create some tensors for input/output
215     auto a = backend->create_tensor(element::f32, shape_a);
216     copy_data(a,
217               vector<float>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20});
218     auto result = backend->create_tensor(element::f32, shape_r);
219
220     auto handle = backend->compile(f);
221     handle->call_with_validate({result}, {a});
222     EXPECT_TRUE(test::all_close_f((vector<float>{12, 13, 14, 15, 17, 18, 19, 20}),
223                                   read_vector<float>(result),
224                                   MIN_FLOAT_TOLERANCE_BITS));
225 }
226
227 NGRAPH_TEST(${BACKEND_NAME}, slice_matrix_strided)
228 {
229     Shape shape_a{4, 4};
230     auto A = make_shared<op::Parameter>(element::f32, shape_a);
231     Shape shape_r{2, 2};
232     auto r = make_shared<op::Slice>(A, Coordinate{1, 0}, Coordinate{4, 4}, Strides{2, 3});
233     auto f = make_shared<Function>(r, ParameterVector{A});
234
235     auto backend = runtime::Backend::create("${BACKEND_NAME}");
236
237     // Create some tensors for input/output
238     auto a = backend->create_tensor(element::f32, shape_a);
239     copy_data(a, vector<float>{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15});
240     auto result = backend->create_tensor(element::f32, shape_r);
241
242     auto handle = backend->compile(f);
243     handle->call_with_validate({result}, {a});
244     EXPECT_TRUE(test::all_close_f(
245         (vector<float>{4, 7, 12, 15}), read_vector<float>(result), MIN_FLOAT_TOLERANCE_BITS));
246 }
247
248 NGRAPH_TEST(${BACKEND_NAME}, slice_3d)
249 {
250     Shape shape_a{4, 4, 4};
251     auto A = make_shared<op::Parameter>(element::f32, shape_a);
252     Shape shape_r{2, 2, 2};
253     auto r = make_shared<op::Slice>(A, Coordinate{1, 1, 1}, Coordinate{3, 3, 3});
254     auto f = make_shared<Function>(r, ParameterVector{A});
255
256     auto backend = runtime::Backend::create("${BACKEND_NAME}");
257
258     // Create some tensors for input/output
259     auto a = backend->create_tensor(element::f32, shape_a);
260     copy_data(a, vector<float>{0,  1,  2,  3,  4,  5,  6,  7,  8,  9,  10, 11, 12, 13, 14, 15,
261
262                                16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
263
264                                32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
265
266                                48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63});
267     auto result = backend->create_tensor(element::f32, shape_r);
268
269     auto handle = backend->compile(f);
270     handle->call_with_validate({result}, {a});
271     EXPECT_TRUE(test::all_close_f((vector<float>{21, 22, 25, 26, 37, 38, 41, 42}),
272                                   read_vector<float>(result),
273                                   MIN_FLOAT_TOLERANCE_BITS));
274 }
275
276 NGRAPH_TEST(${BACKEND_NAME}, slice_3d_strided)
277 {
278     Shape shape_a{4, 4, 4};
279     auto A = make_shared<op::Parameter>(element::f32, shape_a);
280     Shape shape_r{2, 2, 2};
281     auto r = make_shared<op::Slice>(A, Coordinate{0, 0, 0}, Coordinate{4, 4, 4}, Strides{2, 2, 2});
282     auto f = make_shared<Function>(r, ParameterVector{A});
283
284     auto backend = runtime::Backend::create("${BACKEND_NAME}");
285
286     // Create some tensors for input/output
287     auto a = backend->create_tensor(element::f32, shape_a);
288     copy_data(a, vector<float>{0,  1,  2,  3,  4,  5,  6,  7,  8,  9,  10, 11, 12, 13, 14, 15,
289
290                                16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
291
292                                32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
293
294                                48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63});
295     auto result = backend->create_tensor(element::f32, shape_r);
296
297     auto handle = backend->compile(f);
298     handle->call_with_validate({result}, {a});
299     EXPECT_TRUE(test::all_close_f((vector<float>{0, 2, 8, 10, 32, 34, 40, 42}),
300                                   read_vector<float>(result),
301                                   MIN_FLOAT_TOLERANCE_BITS));
302 }
303
304 NGRAPH_TEST(${BACKEND_NAME}, slice_3d_strided_different_strides)
305 {
306     Shape shape_a{4, 4, 4};
307     auto A = make_shared<op::Parameter>(element::f32, shape_a);
308     Shape shape_r{2, 2, 2};
309     auto r = make_shared<op::Slice>(A, Coordinate{0, 0, 0}, Coordinate{4, 4, 4}, Strides{2, 2, 3});
310     auto f = make_shared<Function>(r, ParameterVector{A});
311
312     auto backend = runtime::Backend::create("${BACKEND_NAME}");
313
314     // Create some tensors for input/output
315     auto a = backend->create_tensor(element::f32, shape_a);
316     copy_data(a, vector<float>{0,  1,  2,  3,  4,  5,  6,  7,  8,  9,  10, 11, 12, 13, 14, 15,
317
318                                16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
319
320                                32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
321
322                                48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63});
323     auto result = backend->create_tensor(element::f32, shape_r);
324
325     auto handle = backend->compile(f);
326     handle->call_with_validate({result}, {a});
327     EXPECT_TRUE(test::all_close_f((vector<float>{0, 3, 8, 11, 32, 35, 40, 43}),
328                                   read_vector<float>(result),
329                                   MIN_FLOAT_TOLERANCE_BITS));
330 }
331
332 NGRAPH_TEST(${BACKEND_NAME}, slice_3d_strided_different_strides_int64)
333 {
334     Shape shape_a{4, 4, 4};
335     auto A = make_shared<op::Parameter>(element::i64, shape_a);
336     Shape shape_r{2, 2, 2};
337     auto r = make_shared<op::Slice>(A, Coordinate{0, 0, 0}, Coordinate{4, 4, 4}, Strides{2, 2, 3});
338     auto f = make_shared<Function>(r, ParameterVector{A});
339
340     auto backend = runtime::Backend::create("${BACKEND_NAME}");
341
342     // Create some tensors for input/output
343     auto a = backend->create_tensor(element::i64, shape_a);
344     copy_data(a, vector<int64_t>{0,  1,  2,  3,  4,  5,  6,  7,  8,  9,  10, 11, 12, 13, 14, 15,
345
346                                  16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
347
348                                  32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
349
350                                  48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63});
351     auto result = backend->create_tensor(element::i64, shape_r);
352
353     auto handle = backend->compile(f);
354     handle->call_with_validate({result}, {a});
355     EXPECT_EQ((vector<int64_t>{0, 3, 8, 11, 32, 35, 40, 43}), read_vector<int64_t>(result));
356 }
357
358 NGRAPH_TEST(${BACKEND_NAME}, slice_3d_start_just_oob)
359 {
360     Shape shape_a{20, 10, 5};
361     auto A = make_shared<op::Parameter>(element::f32, shape_a);
362     Shape shape_r{20, 0, 5};
363     auto r =
364         make_shared<op::Slice>(A, Coordinate{0, 10, 0}, Coordinate{20, 10, 5}, Strides{1, 1, 1});
365     auto f = make_shared<Function>(r, ParameterVector{A});
366
367     auto backend = runtime::Backend::create("${BACKEND_NAME}");
368
369     // Create some tensors for input/output
370     auto a = backend->create_tensor(element::f32, shape_a);
371     vector<float> a_data(20 * 10 * 5, 222.0f);
372     copy_data(a, a_data);
373     auto result = backend->create_tensor(element::f32, shape_r);
374
375     auto handle = backend->compile(f);
376     handle->call_with_validate({result}, {a});
377     EXPECT_TRUE(test::all_close_f((vector<float>{}), read_vector<float>(result)));
378 }