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 //*****************************************************************************
24 #include "gtest/gtest.h"
25 #include "ngraph/ngraph.hpp"
26 #include "ngraph/runtime/tensor.hpp"
27 #include "runtime/backend.hpp"
28 #include "util/all_close.hpp"
29 #include "util/all_close_f.hpp"
30 #include "util/ndarray.hpp"
31 #include "util/random.hpp"
32 #include "util/test_case.hpp"
33 #include "util/test_control.hpp"
34 #include "util/test_tools.hpp"
36 NGRAPH_SUPPRESS_DEPRECATED_START
39 using namespace ngraph;
41 static string s_manifest = "${MANIFEST}";
43 NGRAPH_TEST(${BACKEND_NAME}, gather_4d_indices_no_axis_uint8)
45 Shape params_shape{3, 2};
46 Shape indices_shape{2, 2, 3, 4};
47 Shape out_shape{2, 2, 3, 4, 2};
48 auto P = make_shared<op::Parameter>(element::u8, params_shape);
49 auto I = make_shared<op::Parameter>(element::i32, indices_shape);
50 auto G = make_shared<op::Gather>(P, I);
51 auto f = make_shared<Function>(G, ParameterVector{P, I});
53 auto backend = runtime::Backend::create("${BACKEND_NAME}");
55 // Create some tensors for input/output
56 auto p = backend->create_tensor(element::u8, params_shape);
57 copy_data(p, vector<uint8_t>{10, 11, 20, 21, 30, 31});
58 auto i = backend->create_tensor(element::i32, indices_shape);
59 copy_data(i, vector<int32_t>{0, 1, 1, 2, 0, 1, 1, 2, 0, 1, 1, 2, 0, 1, 1, 2,
60 0, 1, 1, 2, 0, 1, 1, 2, 0, 1, 1, 2, 0, 1, 1, 2,
61 0, 1, 1, 2, 0, 1, 1, 2, 0, 1, 1, 2, 0, 1, 1, 2});
62 auto result = backend->create_tensor(element::u8, out_shape);
64 auto c = backend->compile(f);
65 c->call_with_validate({result}, {p, i});
66 EXPECT_TRUE(test::all_close(
67 (vector<uint8_t>{10, 11, 20, 21, 20, 21, 30, 31, 10, 11, 20, 21, 20, 21, 30, 31,
68 10, 11, 20, 21, 20, 21, 30, 31, 10, 11, 20, 21, 20, 21, 30, 31,
69 10, 11, 20, 21, 20, 21, 30, 31, 10, 11, 20, 21, 20, 21, 30, 31,
70 10, 11, 20, 21, 20, 21, 30, 31, 10, 11, 20, 21, 20, 21, 30, 31,
71 10, 11, 20, 21, 20, 21, 30, 31, 10, 11, 20, 21, 20, 21, 30, 31,
72 10, 11, 20, 21, 20, 21, 30, 31, 10, 11, 20, 21, 20, 21, 30, 31}),
73 read_vector<uint8_t>(result)));
76 NGRAPH_TEST(${BACKEND_NAME}, gather_4d_indices_no_axis_2d_input)
78 Shape params_shape{3, 2};
79 Shape indices_shape{2, 2, 3, 4};
80 Shape out_shape{2, 2, 3, 4, 2};
81 auto P = make_shared<op::Parameter>(element::f32, params_shape);
82 auto I = make_shared<op::Parameter>(element::i32, indices_shape);
83 auto G = make_shared<op::Gather>(P, I);
84 auto f = make_shared<Function>(G, ParameterVector{P, I});
86 auto backend = runtime::Backend::create("${BACKEND_NAME}");
88 // Create some tensors for input/output
89 auto p = backend->create_tensor(element::f32, params_shape);
90 copy_data(p, vector<float>{1.0f, 1.1f, 2.0f, 2.1f, 3.0f, 3.1f});
91 auto i = backend->create_tensor(element::i32, indices_shape);
92 copy_data(i, vector<int32_t>{0, 1, 1, 2, 0, 1, 1, 2, 0, 1, 1, 2, 0, 1, 1, 2,
93 0, 1, 1, 2, 0, 1, 1, 2, 0, 1, 1, 2, 0, 1, 1, 2,
94 0, 1, 1, 2, 0, 1, 1, 2, 0, 1, 1, 2, 0, 1, 1, 2});
95 auto result = backend->create_tensor(element::f32, out_shape);
97 auto c = backend->compile(f);
98 c->call_with_validate({result}, {p, i});
99 EXPECT_TRUE(test::all_close_f(
100 (vector<float>{1.0f, 1.1f, 2.0f, 2.1f, 2.0f, 2.1f, 3.0f, 3.1f, 1.0f, 1.1f, 2.0f, 2.1f,
101 2.0f, 2.1f, 3.0f, 3.1f, 1.0f, 1.1f, 2.0f, 2.1f, 2.0f, 2.1f, 3.0f, 3.1f,
102 1.0f, 1.1f, 2.0f, 2.1f, 2.0f, 2.1f, 3.0f, 3.1f, 1.0f, 1.1f, 2.0f, 2.1f,
103 2.0f, 2.1f, 3.0f, 3.1f, 1.0f, 1.1f, 2.0f, 2.1f, 2.0f, 2.1f, 3.0f, 3.1f,
104 1.0f, 1.1f, 2.0f, 2.1f, 2.0f, 2.1f, 3.0f, 3.1f, 1.0f, 1.1f, 2.0f, 2.1f,
105 2.0f, 2.1f, 3.0f, 3.1f, 1.0f, 1.1f, 2.0f, 2.1f, 2.0f, 2.1f, 3.0f, 3.1f,
106 1.0f, 1.1f, 2.0f, 2.1f, 2.0f, 2.1f, 3.0f, 3.1f, 1.0f, 1.1f, 2.0f, 2.1f,
107 2.0f, 2.1f, 3.0f, 3.1f, 1.0f, 1.1f, 2.0f, 2.1f, 2.0f, 2.1f, 3.0f, 3.1f}),
108 read_vector<float>(result),
109 MIN_FLOAT_TOLERANCE_BITS));
112 NGRAPH_TEST(${BACKEND_NAME}, gather_3d_indices_no_axis_2d_input)
114 Shape params_shape{3, 2};
115 Shape indices_shape{2, 3, 4};
116 Shape out_shape{2, 3, 4, 2};
117 auto P = make_shared<op::Parameter>(element::f32, params_shape);
118 auto I = make_shared<op::Parameter>(element::i32, indices_shape);
119 auto G = make_shared<op::Gather>(P, I);
120 auto f = make_shared<Function>(G, ParameterVector{P, I});
122 auto backend = runtime::Backend::create("${BACKEND_NAME}");
124 // Create some tensors for input/output
125 auto p = backend->create_tensor(element::f32, params_shape);
126 copy_data(p, vector<float>{1.0f, 1.1f, 2.0f, 2.1f, 3.0f, 3.1f});
127 auto i = backend->create_tensor(element::i32, indices_shape);
129 i, vector<int32_t>{0, 1, 1, 2, 0, 1, 1, 2, 0, 1, 1, 2, 0, 1, 1, 2, 0, 1, 1, 2, 0, 1, 1, 2});
130 auto result = backend->create_tensor(element::f32, out_shape);
132 auto c = backend->compile(f);
133 c->call_with_validate({result}, {p, i});
134 EXPECT_TRUE(test::all_close_f(
135 (vector<float>{1.0f, 1.1f, 2.0f, 2.1f, 2.0f, 2.1f, 3.0f, 3.1f, 1.0f, 1.1f, 2.0f, 2.1f,
136 2.0f, 2.1f, 3.0f, 3.1f, 1.0f, 1.1f, 2.0f, 2.1f, 2.0f, 2.1f, 3.0f, 3.1f,
137 1.0f, 1.1f, 2.0f, 2.1f, 2.0f, 2.1f, 3.0f, 3.1f, 1.0f, 1.1f, 2.0f, 2.1f,
138 2.0f, 2.1f, 3.0f, 3.1f, 1.0f, 1.1f, 2.0f, 2.1f, 2.0f, 2.1f, 3.0f, 3.1f}),
139 read_vector<float>(result),
140 MIN_FLOAT_TOLERANCE_BITS));
143 NGRAPH_TEST(${BACKEND_NAME}, gather_2d_indices_no_axis_2d_input)
145 Shape params_shape{3, 2};
146 Shape indices_shape{2, 2};
147 Shape out_shape{2, 2, 2};
148 auto P = make_shared<op::Parameter>(element::f32, params_shape);
149 auto I = make_shared<op::Parameter>(element::i32, indices_shape);
150 auto G = make_shared<op::Gather>(P, I);
151 auto f = make_shared<Function>(G, ParameterVector{P, I});
153 auto backend = runtime::Backend::create("${BACKEND_NAME}");
155 // Create some tensors for input/output
156 auto p = backend->create_tensor(element::f32, params_shape);
157 copy_data(p, vector<float>{1.0f, 1.1f, 2.0f, 2.1f, 3.0f, 3.1f});
158 auto i = backend->create_tensor(element::i32, indices_shape);
159 copy_data(i, vector<int32_t>{0, 1, 1, 2});
160 auto result = backend->create_tensor(element::f32, out_shape);
162 auto c = backend->compile(f);
163 c->call_with_validate({result}, {p, i});
164 EXPECT_TRUE(test::all_close_f((vector<float>{1.0f, 1.1f, 2.0f, 2.1f, 2.0f, 2.1f, 3.0f, 3.1f}),
165 read_vector<float>(result),
166 MIN_FLOAT_TOLERANCE_BITS));
169 NGRAPH_TEST(${BACKEND_NAME}, gather_2d_negative_and_positive_indices_no_axis_2d_input)
171 Shape params_shape{3, 2};
172 Shape indices_shape{2, 2};
173 Shape out_shape{2, 2, 2};
174 auto P = make_shared<op::Parameter>(element::f32, params_shape);
175 auto I = make_shared<op::Parameter>(element::i32, indices_shape);
176 auto G = make_shared<op::Gather>(P, I);
177 auto f = make_shared<Function>(G, ParameterVector{P, I});
179 auto backend = runtime::Backend::create("${BACKEND_NAME}");
181 // Create some tensors for input/output
182 auto p = backend->create_tensor(element::f32, params_shape);
183 copy_data(p, vector<float>{1.0f, 1.1f, 2.0f, 2.1f, 3.0f, 3.1f});
184 auto i = backend->create_tensor(element::i32, indices_shape);
185 copy_data(i, vector<int32_t>{0, -2, 1, 2});
186 auto result = backend->create_tensor(element::f32, out_shape);
188 auto c = backend->compile(f);
189 c->call_with_validate({result}, {p, i});
190 EXPECT_TRUE(test::all_close_f((vector<float>{1.0f, 1.1f, 2.0f, 2.1f, 2.0f, 2.1f, 3.0f, 3.1f}),
191 read_vector<float>(result),
192 MIN_FLOAT_TOLERANCE_BITS));
195 NGRAPH_TEST(${BACKEND_NAME}, gather_1d_indices_no_axis_1d_input)
197 Shape params_shape{3};
198 Shape indices_shape{2};
200 auto P = make_shared<op::Parameter>(element::f32, params_shape);
201 auto I = make_shared<op::Parameter>(element::i32, indices_shape);
202 auto G = make_shared<op::Gather>(P, I);
203 auto f = make_shared<Function>(G, ParameterVector{P, I});
205 auto backend = runtime::Backend::create("${BACKEND_NAME}");
207 // Create some tensors for input/output
208 auto p = backend->create_tensor(element::f32, params_shape);
209 copy_data(p, vector<float>{1.0f, 2.0f, 3.0f});
210 auto i = backend->create_tensor(element::i32, indices_shape);
211 copy_data(i, vector<int32_t>{1, 0});
212 auto result = backend->create_tensor(element::f32, out_shape);
214 auto c = backend->compile(f);
215 c->call_with_validate({result}, {p, i});
216 EXPECT_TRUE(test::all_close_f(
217 (vector<float>{2.0f, 1.0f}), read_vector<float>(result), MIN_FLOAT_TOLERANCE_BITS));
220 NGRAPH_TEST(${BACKEND_NAME}, gather_scalar_indices_no_axis_2d_input)
222 Shape params_shape{3, 2};
223 Shape indices_shape{};
225 auto P = make_shared<op::Parameter>(element::f32, params_shape);
226 auto I = make_shared<op::Parameter>(element::i32, indices_shape);
227 auto G = make_shared<op::Gather>(P, I);
228 auto f = make_shared<Function>(G, ParameterVector{P, I});
230 auto backend = runtime::Backend::create("${BACKEND_NAME}");
232 // Create some tensors for input/output
233 auto p = backend->create_tensor(element::f32, params_shape);
234 copy_data(p, vector<float>{1.0f, 1.1f, 2.0f, 2.1f, 3.0f, 3.1f});
235 auto i = backend->create_tensor(element::i32, indices_shape);
236 copy_data(i, vector<int32_t>{1});
237 auto result = backend->create_tensor(element::f32, out_shape);
239 auto c = backend->compile(f);
240 c->call_with_validate({result}, {p, i});
241 EXPECT_TRUE(test::all_close_f(
242 (vector<float>{2.0f, 2.1f}), read_vector<float>(result), MIN_FLOAT_TOLERANCE_BITS));
245 NGRAPH_TEST(${BACKEND_NAME}, gather_2d_indices_axis_1_2d_input)
247 Shape params_shape{3, 3};
248 Shape indices_shape{1, 2};
249 Shape out_shape{3, 1, 2};
250 auto P = make_shared<op::Parameter>(element::f32, params_shape);
251 auto I = make_shared<op::Parameter>(element::i32, indices_shape);
252 auto G = make_shared<op::Gather>(P, I, 1);
253 auto f = make_shared<Function>(G, ParameterVector{P, I});
255 auto backend = runtime::Backend::create("${BACKEND_NAME}");
257 // Create some tensors for input/output
258 auto p = backend->create_tensor(element::f32, params_shape);
259 copy_data(p, vector<float>{1.0f, 1.1f, 1.2f, 2.0f, 2.1f, 2.2f, 3.0f, 3.1f, 3.2f});
260 auto i = backend->create_tensor(element::i32, indices_shape);
261 copy_data(i, vector<int32_t>{0, 2});
262 auto result = backend->create_tensor(element::f32, out_shape);
264 auto c = backend->compile(f);
265 c->call_with_validate({result}, {p, i});
266 EXPECT_TRUE(test::all_close_f((vector<float>{1.0f, 1.2f, 2.0f, 2.2f, 3.0f, 3.2f}),
267 read_vector<float>(result),
268 MIN_FLOAT_TOLERANCE_BITS));
271 NGRAPH_TEST(${BACKEND_NAME}, gather_1d_indices_axis_2_4d_input)
273 Shape params_shape{2, 2, 3, 3};
274 Shape indices_shape{2};
275 Shape out_shape{2, 2, 2, 3};
276 auto P = make_shared<op::Parameter>(element::f32, params_shape);
277 auto I = make_shared<op::Parameter>(element::i32, indices_shape);
278 auto G = make_shared<op::Gather>(P, I, 2);
279 auto f = make_shared<Function>(G, ParameterVector{P, I});
281 auto backend = runtime::Backend::create("${BACKEND_NAME}");
283 // Create some tensors for input/output
284 auto p = backend->create_tensor(element::f32, params_shape);
285 copy_data(p, vector<float>{1.0f, 1.1f, 1.2f, 2.0f, 2.1f, 2.2f, 3.0f, 3.1f, 3.2f,
286 1.0f, 1.1f, 1.2f, 2.0f, 2.1f, 2.2f, 3.0f, 3.1f, 3.2f,
287 1.0f, 1.1f, 1.2f, 2.0f, 2.1f, 2.2f, 3.0f, 3.1f, 3.2f,
288 1.0f, 1.1f, 1.2f, 2.0f, 2.1f, 2.2f, 3.0f, 3.1f, 3.2f});
289 auto i = backend->create_tensor(element::i32, indices_shape);
290 copy_data(i, vector<int32_t>{0, 2});
291 auto result = backend->create_tensor(element::f32, out_shape);
293 auto c = backend->compile(f);
294 c->call_with_validate({result}, {p, i});
295 EXPECT_TRUE(test::all_close_f(
296 (vector<float>{1.0f, 1.1f, 1.2f, 3.0f, 3.1f, 3.2f, 1.0f, 1.1f, 1.2f, 3.0f, 3.1f, 3.2f,
297 1.0f, 1.1f, 1.2f, 3.0f, 3.1f, 3.2f, 1.0f, 1.1f, 1.2f, 3.0f, 3.1f, 3.2f}),
298 read_vector<float>(result),
299 MIN_FLOAT_TOLERANCE_BITS));
302 NGRAPH_TEST(${BACKEND_NAME}, gather_scalar_indices_axis_1_2d_input)
304 Shape params_shape{3, 3};
305 Shape indices_shape{};
307 auto P = make_shared<op::Parameter>(element::f32, params_shape);
308 auto I = make_shared<op::Parameter>(element::i32, indices_shape);
309 auto G = make_shared<op::Gather>(P, I, 1);
310 auto f = make_shared<Function>(G, ParameterVector{P, I});
312 auto backend = runtime::Backend::create("${BACKEND_NAME}");
314 // Create some tensors for input/output
315 auto p = backend->create_tensor(element::f32, params_shape);
316 copy_data(p, vector<float>{1.0f, 1.1f, 1.2f, 2.0f, 2.1f, 2.2f, 3.0f, 3.1f, 3.2f});
317 auto i = backend->create_tensor(element::i32, indices_shape);
318 copy_data(i, vector<int32_t>{0});
319 auto result = backend->create_tensor(element::f32, out_shape);
321 auto c = backend->compile(f);
322 c->call_with_validate({result}, {p, i});
323 EXPECT_TRUE(test::all_close_f(
324 (vector<float>{1.0f, 2.0f, 3.0f}), read_vector<float>(result), MIN_FLOAT_TOLERANCE_BITS));
327 NGRAPH_TEST(${BACKEND_NAME}, gather_nd_single_indices)
329 Shape params_shape{3, 3};
330 Shape indices_shape{2};
332 auto P = make_shared<op::Parameter>(element::f32, params_shape);
333 auto I = make_shared<op::Parameter>(element::i32, indices_shape);
334 auto G = make_shared<op::GatherND>(P, I);
335 auto f = make_shared<Function>(G, ParameterVector{P, I});
337 auto backend = runtime::Backend::create("${BACKEND_NAME}");
339 // Create some tensors for input/output
340 auto p = backend->create_tensor(element::f32, params_shape);
341 copy_data(p, vector<float>{1.0f, 1.1f, 1.2f, 1.3f, 1.4f, 1.5f, 1.6f, 1.7f, 1.8f});
342 auto i = backend->create_tensor(element::i32, indices_shape);
343 copy_data(i, vector<int32_t>{1, 2});
344 auto result = backend->create_tensor(element::f32, out_shape);
346 auto c = backend->compile(f);
347 c->call_with_validate({result}, {p, i});
348 EXPECT_TRUE(test::all_close_f(
349 (vector<float>{1.5f}), read_vector<float>(result), MIN_FLOAT_TOLERANCE_BITS));
352 NGRAPH_TEST(${BACKEND_NAME}, gather_nd_scalar_from_2d)
354 Shape params_shape{2, 2};
355 Shape indices_shape{2, 2};
357 auto P = make_shared<op::Parameter>(element::f32, params_shape);
358 auto I = make_shared<op::Parameter>(element::i32, indices_shape);
359 auto G = make_shared<op::GatherND>(P, I);
360 auto f = make_shared<Function>(G, ParameterVector{P, I});
362 auto backend = runtime::Backend::create("${BACKEND_NAME}");
364 // Create some tensors for input/output
365 auto p = backend->create_tensor(element::f32, params_shape);
366 copy_data(p, vector<float>{1.0f, 1.1f, 1.2f, 1.3f});
367 auto i = backend->create_tensor(element::i32, indices_shape);
368 copy_data(i, vector<int32_t>{0, 0, 1, 1});
369 auto result = backend->create_tensor(element::f32, out_shape);
371 auto c = backend->compile(f);
372 c->call_with_validate({result}, {p, i});
373 EXPECT_TRUE(test::all_close_f(
374 (vector<float>{1.0f, 1.3f}), read_vector<float>(result), MIN_FLOAT_TOLERANCE_BITS));
377 NGRAPH_TEST(${BACKEND_NAME}, gather_nd_1d_from_2d)
379 Shape params_shape{2, 2};
380 Shape indices_shape{2, 1};
381 Shape out_shape{2, 2};
382 auto P = make_shared<op::Parameter>(element::f32, params_shape);
383 auto I = make_shared<op::Parameter>(element::i32, indices_shape);
384 auto G = make_shared<op::GatherND>(P, I);
385 auto f = make_shared<Function>(G, ParameterVector{P, I});
387 auto backend = runtime::Backend::create("${BACKEND_NAME}");
389 // Create some tensors for input/output
390 auto p = backend->create_tensor(element::f32, params_shape);
391 copy_data(p, vector<float>{1.0f, 1.1f, 1.2f, 1.3f});
392 auto i = backend->create_tensor(element::i32, indices_shape);
393 copy_data(i, vector<int32_t>{1, 0});
394 auto result = backend->create_tensor(element::f32, out_shape);
396 auto c = backend->compile(f);
397 c->call_with_validate({result}, {p, i});
398 EXPECT_TRUE(test::all_close_f((vector<float>{1.2f, 1.3f, 1.0f, 1.1f}),
399 read_vector<float>(result),
400 MIN_FLOAT_TOLERANCE_BITS));
403 NGRAPH_TEST(${BACKEND_NAME}, gather_nd_scalar_from_3d)
405 Shape params_shape{2, 2, 2};
406 Shape indices_shape{2, 3};
408 auto P = make_shared<op::Parameter>(element::f32, params_shape);
409 auto I = make_shared<op::Parameter>(element::i32, indices_shape);
410 auto G = make_shared<op::GatherND>(P, I);
411 auto f = make_shared<Function>(G, ParameterVector{P, I});
413 auto backend = runtime::Backend::create("${BACKEND_NAME}");
415 // Create some tensors for input/output
416 auto p = backend->create_tensor(element::f32, params_shape);
417 copy_data(p, vector<float>{1.0f, 1.1f, 1.2f, 1.3f, 2.0f, 2.1f, 2.2f, 2.3f});
418 auto i = backend->create_tensor(element::i32, indices_shape);
419 copy_data(i, vector<int32_t>{0, 0, 1, 1, 0, 1});
420 auto result = backend->create_tensor(element::f32, out_shape);
422 auto c = backend->compile(f);
423 c->call_with_validate({result}, {p, i});
424 EXPECT_TRUE(test::all_close_f(
425 (vector<float>{1.1f, 2.1f}), read_vector<float>(result), MIN_FLOAT_TOLERANCE_BITS));
428 NGRAPH_TEST(${BACKEND_NAME}, gather_nd_1d_from_3d)
430 Shape params_shape{2, 2, 2};
431 Shape indices_shape{2, 2};
432 Shape out_shape{2, 2};
433 auto P = make_shared<op::Parameter>(element::f32, params_shape);
434 auto I = make_shared<op::Parameter>(element::i32, indices_shape);
435 auto G = make_shared<op::GatherND>(P, I);
436 auto f = make_shared<Function>(G, ParameterVector{P, I});
438 auto backend = runtime::Backend::create("${BACKEND_NAME}");
440 // Create some tensors for input/output
441 auto p = backend->create_tensor(element::f32, params_shape);
442 copy_data(p, vector<float>{1.0f, 1.1f, 1.2f, 1.3f, 2.0f, 2.1f, 2.2f, 2.3f});
443 auto i = backend->create_tensor(element::i32, indices_shape);
444 copy_data(i, vector<int32_t>{0, 1, 1, 0});
445 auto result = backend->create_tensor(element::f32, out_shape);
447 auto c = backend->compile(f);
448 c->call_with_validate({result}, {p, i});
449 EXPECT_TRUE(test::all_close_f((vector<float>{1.2f, 1.3f, 2.0f, 2.1f}),
450 read_vector<float>(result),
451 MIN_FLOAT_TOLERANCE_BITS));
454 NGRAPH_TEST(${BACKEND_NAME}, gather_nd_2d_from_3d)
456 Shape params_shape{2, 2, 2};
457 Shape indices_shape{1, 1};
458 Shape out_shape{1, 2, 2};
459 auto P = make_shared<op::Parameter>(element::f32, params_shape);
460 auto I = make_shared<op::Parameter>(element::i32, indices_shape);
461 auto G = make_shared<op::GatherND>(P, I);
462 auto f = make_shared<Function>(G, ParameterVector{P, I});
464 auto backend = runtime::Backend::create("${BACKEND_NAME}");
466 // Create some tensors for input/output
467 auto p = backend->create_tensor(element::f32, params_shape);
468 copy_data(p, vector<float>{1.0f, 1.1f, 1.2f, 1.3f, 2.0f, 2.1f, 2.2f, 2.3f});
469 auto i = backend->create_tensor(element::i32, indices_shape);
470 copy_data(i, vector<int32_t>{1});
471 auto result = backend->create_tensor(element::f32, out_shape);
473 auto c = backend->compile(f);
474 c->call_with_validate({result}, {p, i});
475 EXPECT_TRUE(test::all_close_f((vector<float>{2.0f, 2.1f, 2.2f, 2.3f}),
476 read_vector<float>(result),
477 MIN_FLOAT_TOLERANCE_BITS));
480 NGRAPH_TEST(${BACKEND_NAME}, gather_nd_batch_scalar_from_2d)
482 Shape params_shape{2, 2};
483 Shape indices_shape{2, 1, 2};
484 Shape out_shape{2, 1};
485 auto P = make_shared<op::Parameter>(element::f32, params_shape);
486 auto I = make_shared<op::Parameter>(element::i32, indices_shape);
487 auto G = make_shared<op::GatherND>(P, I);
488 auto f = make_shared<Function>(G, ParameterVector{P, I});
490 auto backend = runtime::Backend::create("${BACKEND_NAME}");
492 // Create some tensors for input/output
493 auto p = backend->create_tensor(element::f32, params_shape);
494 copy_data(p, vector<float>{1.0f, 1.1f, 1.2f, 1.3f});
495 auto i = backend->create_tensor(element::i32, indices_shape);
496 copy_data(i, vector<int32_t>{0, 0, 0, 1});
497 auto result = backend->create_tensor(element::f32, out_shape);
499 auto c = backend->compile(f);
500 c->call_with_validate({result}, {p, i});
501 EXPECT_TRUE(test::all_close_f(
502 (vector<float>{1.0f, 1.1f}), read_vector<float>(result), MIN_FLOAT_TOLERANCE_BITS));
505 NGRAPH_TEST(${BACKEND_NAME}, gather_nd_batch_1d_from_2d)
507 Shape params_shape{2, 2};
508 Shape indices_shape{2, 1, 1};
509 Shape out_shape{2, 1, 2};
510 auto P = make_shared<op::Parameter>(element::f32, params_shape);
511 auto I = make_shared<op::Parameter>(element::i32, indices_shape);
512 auto G = make_shared<op::GatherND>(P, I);
513 auto f = make_shared<Function>(G, ParameterVector{P, I});
515 auto backend = runtime::Backend::create("${BACKEND_NAME}");
517 // Create some tensors for input/output
518 auto p = backend->create_tensor(element::f32, params_shape);
519 copy_data(p, vector<float>{1.0f, 1.1f, 1.2f, 1.3f});
520 auto i = backend->create_tensor(element::i32, indices_shape);
521 copy_data(i, vector<int32_t>{1, 0});
522 auto result = backend->create_tensor(element::f32, out_shape);
524 auto c = backend->compile(f);
525 c->call_with_validate({result}, {p, i});
526 EXPECT_TRUE(test::all_close_f((vector<float>{1.2f, 1.3f, 1.0f, 1.1f}),
527 read_vector<float>(result),
528 MIN_FLOAT_TOLERANCE_BITS));
531 NGRAPH_TEST(${BACKEND_NAME}, gather_nd_batch_scalar_from_3d)
533 Shape params_shape{2, 2, 2};
534 Shape indices_shape{2, 2, 3};
535 Shape out_shape{2, 2};
536 auto P = make_shared<op::Parameter>(element::f32, params_shape);
537 auto I = make_shared<op::Parameter>(element::i32, indices_shape);
538 auto G = make_shared<op::GatherND>(P, I);
539 auto f = make_shared<Function>(G, ParameterVector{P, I});
541 auto backend = runtime::Backend::create("${BACKEND_NAME}");
543 // Create some tensors for input/output
544 auto p = backend->create_tensor(element::f32, params_shape);
545 copy_data(p, vector<float>{1.0f, 1.1f, 1.2f, 1.3f, 2.0f, 2.1f, 2.2f, 2.3f});
546 auto i = backend->create_tensor(element::i32, indices_shape);
547 copy_data(i, vector<int32_t>{0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0});
548 auto result = backend->create_tensor(element::f32, out_shape);
550 auto c = backend->compile(f);
551 c->call_with_validate({result}, {p, i});
552 EXPECT_TRUE(test::all_close_f((vector<float>{1.1f, 2.1f, 1.3f, 2.2f}),
553 read_vector<float>(result),
554 MIN_FLOAT_TOLERANCE_BITS));
557 NGRAPH_TEST(${BACKEND_NAME}, gather_nd_batch_1d_from_3d)
559 Shape params_shape{2, 2, 2};
560 Shape indices_shape{2, 2, 2};
561 Shape out_shape{2, 2, 2};
562 auto P = make_shared<op::Parameter>(element::f32, params_shape);
563 auto I = make_shared<op::Parameter>(element::i32, indices_shape);
564 auto G = make_shared<op::GatherND>(P, I);
565 auto f = make_shared<Function>(G, ParameterVector{P, I});
567 auto backend = runtime::Backend::create("${BACKEND_NAME}");
569 // Create some tensors for input/output
570 auto p = backend->create_tensor(element::f32, params_shape);
571 copy_data(p, vector<float>{1.0f, 1.1f, 1.2f, 1.3f, 2.0f, 2.1f, 2.2f, 2.3f});
572 auto i = backend->create_tensor(element::i32, indices_shape);
573 copy_data(i, vector<int32_t>{0, 1, 1, 0, 0, 0, 1, 1});
574 auto result = backend->create_tensor(element::f32, out_shape);
576 auto c = backend->compile(f);
577 c->call_with_validate({result}, {p, i});
578 EXPECT_TRUE(test::all_close_f((vector<float>{1.2f, 1.3f, 2.0f, 2.1f, 1.0f, 1.1f, 2.2f, 2.3f}),
579 read_vector<float>(result),
580 MIN_FLOAT_TOLERANCE_BITS));
583 NGRAPH_TEST(${BACKEND_NAME}, gather_nd_batch_2d_from_3d)
585 Shape params_shape{2, 2, 2};
586 Shape indices_shape{2, 1, 1};
587 Shape out_shape{2, 1, 2, 2};
588 auto P = make_shared<op::Parameter>(element::f32, params_shape);
589 auto I = make_shared<op::Parameter>(element::i32, indices_shape);
590 auto G = make_shared<op::GatherND>(P, I);
591 auto f = make_shared<Function>(G, ParameterVector{P, I});
593 auto backend = runtime::Backend::create("${BACKEND_NAME}");
595 // Create some tensors for input/output
596 auto p = backend->create_tensor(element::f32, params_shape);
597 copy_data(p, vector<float>{1.0f, 1.1f, 1.2f, 1.3f, 2.0f, 2.1f, 2.2f, 2.3f});
598 auto i = backend->create_tensor(element::i32, indices_shape);
599 copy_data(i, vector<int32_t>{1, 0});
600 auto result = backend->create_tensor(element::f32, out_shape);
602 auto c = backend->compile(f);
603 c->call_with_validate({result}, {p, i});
604 EXPECT_TRUE(test::all_close_f((vector<float>{2.0f, 2.1f, 2.2f, 2.3f, 1.0f, 1.1f, 1.2f, 1.3f}),
605 read_vector<float>(result),
606 MIN_FLOAT_TOLERANCE_BITS));
609 NGRAPH_TEST(${BACKEND_NAME}, gather_no_axis_int8)
611 Shape params_shape{3, 2};
612 Shape indices_shape{2, 2};
613 Shape out_shape{2, 2, 2};
614 auto P = make_shared<op::Parameter>(element::i8, params_shape);
615 auto I = make_shared<op::Parameter>(element::i32, indices_shape);
616 auto G = make_shared<op::Gather>(P, I);
617 auto f = make_shared<Function>(G, ParameterVector{P, I});
619 auto backend = runtime::Backend::create("${BACKEND_NAME}");
621 // Create some tensors for input/output
622 auto p = backend->create_tensor(element::i8, params_shape);
623 copy_data(p, vector<int8_t>{10, 11, 20, 21, 30, 31});
624 auto i = backend->create_tensor(element::i32, indices_shape);
625 copy_data(i, vector<int32_t>{0, 1, 1, 2});
626 auto result = backend->create_tensor(element::i8, out_shape);
628 auto c = backend->compile(f);
629 c->call_with_validate({result}, {p, i});
630 EXPECT_TRUE(test::all_close((vector<int8_t>{10, 11, 20, 21, 20, 21, 30, 31}),
631 read_vector<int8_t>(result)));
634 NGRAPH_TEST(${BACKEND_NAME}, gather_no_axis_int16)
636 Shape params_shape{3, 2};
637 Shape indices_shape{2, 2};
638 Shape out_shape{2, 2, 2};
639 auto P = make_shared<op::Parameter>(element::i16, params_shape);
640 auto I = make_shared<op::Parameter>(element::i64, indices_shape);
641 auto G = make_shared<op::Gather>(P, I);
642 auto f = make_shared<Function>(G, ParameterVector{P, I});
644 auto backend = runtime::Backend::create("${BACKEND_NAME}");
646 // Create some tensors for input/output
647 auto p = backend->create_tensor(element::i16, params_shape);
648 copy_data(p, vector<int16_t>{10, 11, 20, 21, 30, 31});
649 auto i = backend->create_tensor(element::i64, indices_shape);
650 copy_data(i, vector<int64_t>{0, 1, 1, 2});
651 auto result = backend->create_tensor(element::i16, out_shape);
653 auto c = backend->compile(f);
654 c->call_with_validate({result}, {p, i});
655 EXPECT_TRUE(test::all_close((vector<int16_t>{10, 11, 20, 21, 20, 21, 30, 31}),
656 read_vector<int16_t>(result)));
659 NGRAPH_TEST(${BACKEND_NAME}, gather_no_axis_int32)
661 Shape params_shape{3, 2};
662 Shape indices_shape{2, 2};
663 Shape out_shape{2, 2, 2};
664 auto P = make_shared<op::Parameter>(element::i32, params_shape);
665 auto I = make_shared<op::Parameter>(element::i32, indices_shape);
666 auto G = make_shared<op::Gather>(P, I);
667 auto f = make_shared<Function>(G, ParameterVector{P, I});
669 auto backend = runtime::Backend::create("${BACKEND_NAME}");
671 // Create some tensors for input/output
672 auto p = backend->create_tensor(element::i32, params_shape);
673 copy_data(p, vector<int32_t>{10, 11, 20, 21, 30, 31});
674 auto i = backend->create_tensor(element::i32, indices_shape);
675 copy_data(i, vector<int32_t>{0, 1, 1, 2});
676 auto result = backend->create_tensor(element::i32, out_shape);
678 auto c = backend->compile(f);
679 c->call_with_validate({result}, {p, i});
680 EXPECT_TRUE(test::all_close((vector<int32_t>{10, 11, 20, 21, 20, 21, 30, 31}),
681 read_vector<int32_t>(result)));
684 NGRAPH_TEST(${BACKEND_NAME}, gather_no_axis_int64)
686 Shape params_shape{3, 2};
687 Shape indices_shape{2, 2};
688 Shape out_shape{2, 2, 2};
689 auto P = make_shared<op::Parameter>(element::i64, params_shape);
690 auto I = make_shared<op::Parameter>(element::i64, indices_shape);
691 auto G = make_shared<op::Gather>(P, I);
692 auto f = make_shared<Function>(G, ParameterVector{P, I});
694 auto backend = runtime::Backend::create("${BACKEND_NAME}");
696 // Create some tensors for input/output
697 auto p = backend->create_tensor(element::i64, params_shape);
698 copy_data(p, vector<int64_t>{10, 11, 20, 21, 30, 31});
699 auto i = backend->create_tensor(element::i64, indices_shape);
700 copy_data(i, vector<int64_t>{0, 1, 1, 2});
701 auto result = backend->create_tensor(element::i64, out_shape);
703 auto c = backend->compile(f);
704 c->call_with_validate({result}, {p, i});
705 EXPECT_TRUE(test::all_close((vector<int64_t>{10, 11, 20, 21, 20, 21, 30, 31}),
706 read_vector<int64_t>(result)));
709 NGRAPH_TEST(${BACKEND_NAME}, gather_no_axis_uint8)
711 Shape params_shape{3, 2};
712 Shape indices_shape{2, 2};
713 Shape out_shape{2, 2, 2};
714 auto P = make_shared<op::Parameter>(element::u8, params_shape);
715 auto I = make_shared<op::Parameter>(element::i32, indices_shape);
716 auto G = make_shared<op::Gather>(P, I);
717 auto f = make_shared<Function>(G, ParameterVector{P, I});
719 auto backend = runtime::Backend::create("${BACKEND_NAME}");
721 // Create some tensors for input/output
722 auto p = backend->create_tensor(element::u8, params_shape);
723 copy_data(p, vector<uint8_t>{10, 11, 20, 21, 30, 31});
724 auto i = backend->create_tensor(element::i32, indices_shape);
725 copy_data(i, vector<int32_t>{0, 1, 1, 2});
726 auto result = backend->create_tensor(element::u8, out_shape);
728 auto c = backend->compile(f);
729 c->call_with_validate({result}, {p, i});
730 EXPECT_TRUE(test::all_close((vector<uint8_t>{10, 11, 20, 21, 20, 21, 30, 31}),
731 read_vector<uint8_t>(result)));
734 NGRAPH_TEST(${BACKEND_NAME}, gather_no_axis_uint16)
736 Shape params_shape{3, 2};
737 Shape indices_shape{2, 2};
738 Shape out_shape{2, 2, 2};
739 auto P = make_shared<op::Parameter>(element::u16, params_shape);
740 auto I = make_shared<op::Parameter>(element::i64, indices_shape);
741 auto G = make_shared<op::Gather>(P, I);
742 auto f = make_shared<Function>(G, ParameterVector{P, I});
744 auto backend = runtime::Backend::create("${BACKEND_NAME}");
746 // Create some tensors for input/output
747 auto p = backend->create_tensor(element::u16, params_shape);
748 copy_data(p, vector<uint16_t>{10, 11, 20, 21, 30, 31});
749 auto i = backend->create_tensor(element::i64, indices_shape);
750 copy_data(i, vector<int64_t>{0, 1, 1, 2});
751 auto result = backend->create_tensor(element::u16, out_shape);
753 auto c = backend->compile(f);
754 c->call_with_validate({result}, {p, i});
755 EXPECT_TRUE(test::all_close((vector<uint16_t>{10, 11, 20, 21, 20, 21, 30, 31}),
756 read_vector<uint16_t>(result)));
759 NGRAPH_TEST(${BACKEND_NAME}, gather_no_axis_uint32)
761 Shape params_shape{3, 2};
762 Shape indices_shape{2, 2};
763 Shape out_shape{2, 2, 2};
764 auto P = make_shared<op::Parameter>(element::u32, params_shape);
765 auto I = make_shared<op::Parameter>(element::i32, indices_shape);
766 auto G = make_shared<op::Gather>(P, I);
767 auto f = make_shared<Function>(G, ParameterVector{P, I});
769 auto backend = runtime::Backend::create("${BACKEND_NAME}");
771 // Create some tensors for input/output
772 auto p = backend->create_tensor(element::u32, params_shape);
773 copy_data(p, vector<uint32_t>{10, 11, 20, 21, 30, 31});
774 auto i = backend->create_tensor(element::i32, indices_shape);
775 copy_data(i, vector<int32_t>{0, 1, 1, 2});
776 auto result = backend->create_tensor(element::u32, out_shape);
778 auto c = backend->compile(f);
779 c->call_with_validate({result}, {p, i});
780 EXPECT_TRUE(test::all_close((vector<uint32_t>{10, 11, 20, 21, 20, 21, 30, 31}),
781 read_vector<uint32_t>(result)));
784 NGRAPH_TEST(${BACKEND_NAME}, gather_no_axis_uint64)
786 Shape params_shape{3, 2};
787 Shape indices_shape{2, 2};
788 Shape out_shape{2, 2, 2};
789 auto P = make_shared<op::Parameter>(element::u64, params_shape);
790 auto I = make_shared<op::Parameter>(element::i64, indices_shape);
791 auto G = make_shared<op::Gather>(P, I);
792 auto f = make_shared<Function>(G, ParameterVector{P, I});
794 auto backend = runtime::Backend::create("${BACKEND_NAME}");
796 // Create some tensors for input/output
797 auto p = backend->create_tensor(element::u64, params_shape);
798 copy_data(p, vector<uint64_t>{10, 11, 20, 21, 30, 31});
799 auto i = backend->create_tensor(element::i64, indices_shape);
800 copy_data(i, vector<int64_t>{0, 1, 1, 2});
801 auto result = backend->create_tensor(element::u64, out_shape);
803 auto c = backend->compile(f);
804 c->call_with_validate({result}, {p, i});
805 EXPECT_TRUE(test::all_close((vector<uint64_t>{10, 11, 20, 21, 20, 21, 30, 31}),
806 read_vector<uint64_t>(result)));
809 NGRAPH_TEST(${BACKEND_NAME}, gather_no_axis_bool)
811 Shape params_shape{3, 2};
812 Shape indices_shape{2, 2};
813 Shape out_shape{2, 2, 2};
814 auto P = make_shared<op::Parameter>(element::boolean, params_shape);
815 auto I = make_shared<op::Parameter>(element::i64, indices_shape);
816 auto G = make_shared<op::Gather>(P, I);
817 auto f = make_shared<Function>(G, ParameterVector{P, I});
819 auto backend = runtime::Backend::create("${BACKEND_NAME}");
821 // Create some tensors for input/output
822 auto p = backend->create_tensor(element::boolean, params_shape);
823 copy_data(p, vector<char>{1, 1, 1, 0, 0, 1});
824 auto i = backend->create_tensor(element::i64, indices_shape);
825 copy_data(i, vector<int64_t>{0, 1, 1, 2});
826 auto result = backend->create_tensor(element::boolean, out_shape);
828 auto c = backend->compile(f);
829 c->call_with_validate({result}, {p, i});
830 EXPECT_TRUE(test::all_close((vector<char>{1, 1, 1, 0, 1, 0, 0, 1}), read_vector<char>(result)));