Deprecate nGraph v0 ops and builders (#1856)
[platform/upstream/dldt.git] / ngraph / test / backend / gather.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 <algorithm>
18 #include <cinttypes>
19 #include <cmath>
20 #include <cstdlib>
21 #include <random>
22 #include <string>
23
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"
35
36 NGRAPH_SUPPRESS_DEPRECATED_START
37
38 using namespace std;
39 using namespace ngraph;
40
41 static string s_manifest = "${MANIFEST}";
42
43 NGRAPH_TEST(${BACKEND_NAME}, gather_4d_indices_no_axis_uint8)
44 {
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});
52
53     auto backend = runtime::Backend::create("${BACKEND_NAME}");
54
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);
63
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)));
74 }
75
76 NGRAPH_TEST(${BACKEND_NAME}, gather_4d_indices_no_axis_2d_input)
77 {
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});
85
86     auto backend = runtime::Backend::create("${BACKEND_NAME}");
87
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);
96
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));
110 }
111
112 NGRAPH_TEST(${BACKEND_NAME}, gather_3d_indices_no_axis_2d_input)
113 {
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});
121
122     auto backend = runtime::Backend::create("${BACKEND_NAME}");
123
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);
128     copy_data(
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);
131
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));
141 }
142
143 NGRAPH_TEST(${BACKEND_NAME}, gather_2d_indices_no_axis_2d_input)
144 {
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});
152
153     auto backend = runtime::Backend::create("${BACKEND_NAME}");
154
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);
161
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));
167 }
168
169 NGRAPH_TEST(${BACKEND_NAME}, gather_2d_negative_and_positive_indices_no_axis_2d_input)
170 {
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});
178
179     auto backend = runtime::Backend::create("${BACKEND_NAME}");
180
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);
187
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));
193 }
194
195 NGRAPH_TEST(${BACKEND_NAME}, gather_1d_indices_no_axis_1d_input)
196 {
197     Shape params_shape{3};
198     Shape indices_shape{2};
199     Shape out_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});
204
205     auto backend = runtime::Backend::create("${BACKEND_NAME}");
206
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);
213
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));
218 }
219
220 NGRAPH_TEST(${BACKEND_NAME}, gather_scalar_indices_no_axis_2d_input)
221 {
222     Shape params_shape{3, 2};
223     Shape indices_shape{};
224     Shape out_shape{2};
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});
229
230     auto backend = runtime::Backend::create("${BACKEND_NAME}");
231
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);
238
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));
243 }
244
245 NGRAPH_TEST(${BACKEND_NAME}, gather_2d_indices_axis_1_2d_input)
246 {
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});
254
255     auto backend = runtime::Backend::create("${BACKEND_NAME}");
256
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);
263
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));
269 }
270
271 NGRAPH_TEST(${BACKEND_NAME}, gather_1d_indices_axis_2_4d_input)
272 {
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});
280
281     auto backend = runtime::Backend::create("${BACKEND_NAME}");
282
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);
292
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));
300 }
301
302 NGRAPH_TEST(${BACKEND_NAME}, gather_scalar_indices_axis_1_2d_input)
303 {
304     Shape params_shape{3, 3};
305     Shape indices_shape{};
306     Shape out_shape{3};
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});
311
312     auto backend = runtime::Backend::create("${BACKEND_NAME}");
313
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);
320
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));
325 }
326
327 NGRAPH_TEST(${BACKEND_NAME}, gather_nd_single_indices)
328 {
329     Shape params_shape{3, 3};
330     Shape indices_shape{2};
331     Shape out_shape{};
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});
336
337     auto backend = runtime::Backend::create("${BACKEND_NAME}");
338
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);
345
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));
350 }
351
352 NGRAPH_TEST(${BACKEND_NAME}, gather_nd_scalar_from_2d)
353 {
354     Shape params_shape{2, 2};
355     Shape indices_shape{2, 2};
356     Shape out_shape{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});
361
362     auto backend = runtime::Backend::create("${BACKEND_NAME}");
363
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);
370
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));
375 }
376
377 NGRAPH_TEST(${BACKEND_NAME}, gather_nd_1d_from_2d)
378 {
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});
386
387     auto backend = runtime::Backend::create("${BACKEND_NAME}");
388
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);
395
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));
401 }
402
403 NGRAPH_TEST(${BACKEND_NAME}, gather_nd_scalar_from_3d)
404 {
405     Shape params_shape{2, 2, 2};
406     Shape indices_shape{2, 3};
407     Shape out_shape{2};
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});
412
413     auto backend = runtime::Backend::create("${BACKEND_NAME}");
414
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);
421
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));
426 }
427
428 NGRAPH_TEST(${BACKEND_NAME}, gather_nd_1d_from_3d)
429 {
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});
437
438     auto backend = runtime::Backend::create("${BACKEND_NAME}");
439
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);
446
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));
452 }
453
454 NGRAPH_TEST(${BACKEND_NAME}, gather_nd_2d_from_3d)
455 {
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});
463
464     auto backend = runtime::Backend::create("${BACKEND_NAME}");
465
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);
472
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));
478 }
479
480 NGRAPH_TEST(${BACKEND_NAME}, gather_nd_batch_scalar_from_2d)
481 {
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});
489
490     auto backend = runtime::Backend::create("${BACKEND_NAME}");
491
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);
498
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));
503 }
504
505 NGRAPH_TEST(${BACKEND_NAME}, gather_nd_batch_1d_from_2d)
506 {
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});
514
515     auto backend = runtime::Backend::create("${BACKEND_NAME}");
516
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);
523
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));
529 }
530
531 NGRAPH_TEST(${BACKEND_NAME}, gather_nd_batch_scalar_from_3d)
532 {
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});
540
541     auto backend = runtime::Backend::create("${BACKEND_NAME}");
542
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);
549
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));
555 }
556
557 NGRAPH_TEST(${BACKEND_NAME}, gather_nd_batch_1d_from_3d)
558 {
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});
566
567     auto backend = runtime::Backend::create("${BACKEND_NAME}");
568
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);
575
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));
581 }
582
583 NGRAPH_TEST(${BACKEND_NAME}, gather_nd_batch_2d_from_3d)
584 {
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});
592
593     auto backend = runtime::Backend::create("${BACKEND_NAME}");
594
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);
601
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));
607 }
608
609 NGRAPH_TEST(${BACKEND_NAME}, gather_no_axis_int8)
610 {
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});
618
619     auto backend = runtime::Backend::create("${BACKEND_NAME}");
620
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);
627
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)));
632 }
633
634 NGRAPH_TEST(${BACKEND_NAME}, gather_no_axis_int16)
635 {
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});
643
644     auto backend = runtime::Backend::create("${BACKEND_NAME}");
645
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);
652
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)));
657 }
658
659 NGRAPH_TEST(${BACKEND_NAME}, gather_no_axis_int32)
660 {
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});
668
669     auto backend = runtime::Backend::create("${BACKEND_NAME}");
670
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);
677
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)));
682 }
683
684 NGRAPH_TEST(${BACKEND_NAME}, gather_no_axis_int64)
685 {
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});
693
694     auto backend = runtime::Backend::create("${BACKEND_NAME}");
695
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);
702
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)));
707 }
708
709 NGRAPH_TEST(${BACKEND_NAME}, gather_no_axis_uint8)
710 {
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});
718
719     auto backend = runtime::Backend::create("${BACKEND_NAME}");
720
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);
727
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)));
732 }
733
734 NGRAPH_TEST(${BACKEND_NAME}, gather_no_axis_uint16)
735 {
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});
743
744     auto backend = runtime::Backend::create("${BACKEND_NAME}");
745
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);
752
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)));
757 }
758
759 NGRAPH_TEST(${BACKEND_NAME}, gather_no_axis_uint32)
760 {
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});
768
769     auto backend = runtime::Backend::create("${BACKEND_NAME}");
770
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);
777
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)));
782 }
783
784 NGRAPH_TEST(${BACKEND_NAME}, gather_no_axis_uint64)
785 {
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});
793
794     auto backend = runtime::Backend::create("${BACKEND_NAME}");
795
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);
802
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)));
807 }
808
809 NGRAPH_TEST(${BACKEND_NAME}, gather_no_axis_bool)
810 {
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});
818
819     auto backend = runtime::Backend::create("${BACKEND_NAME}");
820
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);
827
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)));
831 }