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}, slice_scalar)
37 auto A = make_shared<op::Parameter>(element::f32, shape_a);
39 auto r = make_shared<op::Slice>(A, Coordinate{}, Coordinate{});
40 auto f = make_shared<Function>(make_shared<op::v0::Abs>(r), ParameterVector{A});
42 auto backend = runtime::Backend::create("${BACKEND_NAME}");
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);
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));
55 NGRAPH_TEST(${BACKEND_NAME}, slice_matrix)
58 auto A = make_shared<op::Parameter>(element::f32, shape_a);
60 auto r = make_shared<op::Slice>(A, Coordinate{0, 1}, Coordinate{3, 3});
61 auto f = make_shared<Function>(r, ParameterVector{A});
63 auto backend = runtime::Backend::create("${BACKEND_NAME}");
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);
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));
76 NGRAPH_TEST(${BACKEND_NAME}, slice_vector)
79 auto A = make_shared<op::Parameter>(element::f32, shape_a);
81 auto r = make_shared<op::Slice>(A, Coordinate{2}, Coordinate{14});
82 auto f = make_shared<Function>(r, ParameterVector{A});
84 auto backend = runtime::Backend::create("${BACKEND_NAME}");
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);
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));
98 NGRAPH_TEST(${BACKEND_NAME}, slice_matrix_axis_0_overlap)
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);
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});
110 auto backend = runtime::Backend::create("${BACKEND_NAME}");
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);
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));
126 NGRAPH_TEST(${BACKEND_NAME}, slice_matrix_axis_0_in_place)
129 auto A = make_shared<op::Parameter>(element::f32, shape_a);
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});
136 auto backend = runtime::Backend::create("${BACKEND_NAME}");
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);
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));
150 NGRAPH_TEST(${BACKEND_NAME}, slice_matrix_axis_0_in_place_twice)
153 auto A = make_shared<op::Parameter>(element::f32, shape_a);
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});
161 auto backend = runtime::Backend::create("${BACKEND_NAME}");
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);
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));
174 NGRAPH_TEST(${BACKEND_NAME}, slice_matrix_axis_0_in_place_twice_overlap)
177 auto A = make_shared<op::Parameter>(element::f32, shape_a);
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});
185 auto backend = runtime::Backend::create("${BACKEND_NAME}");
187 // Create some tensors for input/output
188 auto a = backend->create_tensor(element::f32, shape_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);
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));
200 NGRAPH_TEST(${BACKEND_NAME}, slice_matrix_axis_0_in_place_with_reshape)
203 auto A = make_shared<op::Parameter>(element::f32, shape_a);
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});
212 auto backend = runtime::Backend::create("${BACKEND_NAME}");
214 // Create some tensors for input/output
215 auto a = backend->create_tensor(element::f32, shape_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);
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));
227 NGRAPH_TEST(${BACKEND_NAME}, slice_matrix_strided)
230 auto A = make_shared<op::Parameter>(element::f32, shape_a);
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});
235 auto backend = runtime::Backend::create("${BACKEND_NAME}");
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);
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));
248 NGRAPH_TEST(${BACKEND_NAME}, slice_3d)
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});
256 auto backend = runtime::Backend::create("${BACKEND_NAME}");
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,
262 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
264 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
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);
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));
276 NGRAPH_TEST(${BACKEND_NAME}, slice_3d_strided)
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});
284 auto backend = runtime::Backend::create("${BACKEND_NAME}");
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,
290 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
292 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
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);
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));
304 NGRAPH_TEST(${BACKEND_NAME}, slice_3d_strided_different_strides)
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});
312 auto backend = runtime::Backend::create("${BACKEND_NAME}");
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,
318 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
320 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
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);
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));
332 NGRAPH_TEST(${BACKEND_NAME}, slice_3d_strided_different_strides_int64)
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});
340 auto backend = runtime::Backend::create("${BACKEND_NAME}");
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,
346 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
348 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
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);
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));
358 NGRAPH_TEST(${BACKEND_NAME}, slice_3d_start_just_oob)
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};
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});
367 auto backend = runtime::Backend::create("${BACKEND_NAME}");
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);
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)));