Deprecate nGraph v0 ops and builders (#1856)
[platform/upstream/dldt.git] / ngraph / test / backend / any.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 <string>
22
23 #include "gtest/gtest.h"
24 #include "ngraph/ngraph.hpp"
25 #include "ngraph/runtime/tensor.hpp"
26 #include "runtime/backend.hpp"
27 #include "util/all_close.hpp"
28 #include "util/all_close_f.hpp"
29 #include "util/ndarray.hpp"
30 #include "util/random.hpp"
31 #include "util/test_control.hpp"
32 #include "util/test_tools.hpp"
33
34 NGRAPH_SUPPRESS_DEPRECATED_START
35
36 using namespace std;
37 using namespace ngraph;
38
39 static string s_manifest = "${MANIFEST}";
40
41 // Trivial case with no reduced axes.
42 NGRAPH_TEST(${BACKEND_NAME}, any_trivial)
43 {
44     Shape shape{2, 2};
45     auto A = make_shared<op::Parameter>(element::boolean, shape);
46     auto f = make_shared<Function>(make_shared<op::Any>(A, AxisSet{}), ParameterVector{A});
47
48     auto backend = runtime::Backend::create("${BACKEND_NAME}");
49
50     // Create some tensors for input/output
51     auto a = backend->create_tensor(element::boolean, shape);
52     copy_data(a, vector<char>{0, 1, 1, 0});
53     auto result = backend->create_tensor(element::boolean, shape);
54
55     auto handle = backend->compile(f);
56     handle->call_with_validate({result}, {a});
57     EXPECT_EQ((vector<char>{0, 1, 1, 0}), read_vector<char>(result));
58 }
59
60 NGRAPH_TEST(${BACKEND_NAME}, any_2x2_to_scalar_true)
61 {
62     Shape shape{2, 2};
63     auto A = make_shared<op::Parameter>(element::boolean, shape);
64     auto f = make_shared<Function>(make_shared<op::Any>(A, AxisSet{0, 1}), ParameterVector{A});
65
66     auto backend = runtime::Backend::create("${BACKEND_NAME}");
67
68     // Create some tensors for input/output
69     auto a = backend->create_tensor(element::boolean, shape);
70     copy_data(a, vector<char>{0, 1, 1, 0});
71     auto result = backend->create_tensor(element::boolean, Shape{});
72
73     auto handle = backend->compile(f);
74     handle->call_with_validate({result}, {a});
75     EXPECT_EQ((vector<char>{1}), read_vector<char>(result));
76 }
77
78 NGRAPH_TEST(${BACKEND_NAME}, any_2x2_to_scalar_false)
79 {
80     Shape shape{2, 2};
81     auto A = make_shared<op::Parameter>(element::boolean, shape);
82     auto f = make_shared<Function>(make_shared<op::Any>(A, AxisSet{0, 1}), 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::boolean, shape);
88     copy_data(a, vector<char>{0, 0, 0, 0});
89     auto result = backend->create_tensor(element::boolean, Shape{});
90
91     auto handle = backend->compile(f);
92     handle->call_with_validate({result}, {a});
93     EXPECT_EQ((vector<char>{0}), read_vector<char>(result));
94 }
95
96 NGRAPH_TEST(${BACKEND_NAME}, any_2x0_to_scalar)
97 {
98     Shape shape{2, 0};
99     auto A = make_shared<op::Parameter>(element::boolean, shape);
100     auto f = make_shared<Function>(make_shared<op::Any>(A, AxisSet{0, 1}), ParameterVector{A});
101
102     auto backend = runtime::Backend::create("${BACKEND_NAME}");
103
104     // Create some tensors for input/output
105     auto a = backend->create_tensor(element::boolean, shape);
106     auto result = backend->create_tensor(element::boolean, Shape{});
107
108     auto handle = backend->compile(f);
109     handle->call_with_validate({result}, {a});
110     EXPECT_EQ((vector<char>{0}), read_vector<char>(result));
111 }
112
113 NGRAPH_TEST(${BACKEND_NAME}, any_2x3_eliminate_col_dim)
114 {
115     Shape shape{2, 3};
116     auto A = make_shared<op::Parameter>(element::boolean, shape);
117     auto f = make_shared<Function>(make_shared<op::Any>(A, AxisSet{1}), ParameterVector{A});
118
119     auto backend = runtime::Backend::create("${BACKEND_NAME}");
120
121     // Create some tensors for input/output
122     auto a = backend->create_tensor(element::boolean, shape);
123     copy_data(a, test::NDArray<char, 2>({{0, 1, 0}, {0, 0, 0}}).get_vector());
124     auto result = backend->create_tensor(element::boolean, Shape{2});
125
126     auto handle = backend->compile(f);
127     handle->call_with_validate({result}, {a});
128     EXPECT_EQ((vector<char>{1, 0}), read_vector<char>(result));
129 }
130
131 NGRAPH_TEST(${BACKEND_NAME}, any_2x3_eliminate_row_dim)
132 {
133     Shape shape{2, 3};
134     auto A = make_shared<op::Parameter>(element::boolean, shape);
135     auto f = make_shared<Function>(make_shared<op::Any>(A, AxisSet{0}), ParameterVector{A});
136
137     auto backend = runtime::Backend::create("${BACKEND_NAME}");
138
139     // Create some tensors for input/output
140     auto a = backend->create_tensor(element::boolean, shape);
141     copy_data(a, test::NDArray<char, 2>({{0, 1, 0}, {0, 0, 1}}).get_vector());
142     auto result = backend->create_tensor(element::boolean, Shape{3});
143
144     auto handle = backend->compile(f);
145     handle->call_with_validate({result}, {a});
146     EXPECT_EQ((vector<char>{0, 1, 1}), read_vector<char>(result));
147 }
148
149 NGRAPH_TEST(${BACKEND_NAME}, any_2x2x3_eliminate_dim_0)
150 {
151     Shape shape{2, 2, 3};
152     auto A = make_shared<op::Parameter>(element::boolean, shape);
153     auto f = make_shared<Function>(make_shared<op::Any>(A, AxisSet{0}), ParameterVector{A});
154
155     auto backend = runtime::Backend::create("${BACKEND_NAME}");
156
157     // Create some tensors for input/output
158     auto a = backend->create_tensor(element::boolean, shape);
159     copy_data(
160         a, test::NDArray<char, 3>({{{0, 1, 0}, {0, 0, 1}}, {{1, 0, 1}, {0, 0, 0}}}).get_vector());
161     auto result = backend->create_tensor(element::boolean, Shape{2, 3});
162
163     auto handle = backend->compile(f);
164     handle->call_with_validate({result}, {a});
165     EXPECT_EQ((vector<char>{1, 1, 1, 0, 0, 1}), read_vector<char>(result));
166 }
167
168 NGRAPH_TEST(${BACKEND_NAME}, any_2x2x3_eliminate_dim_1)
169 {
170     Shape shape{2, 2, 3};
171     auto A = make_shared<op::Parameter>(element::boolean, shape);
172     auto f = make_shared<Function>(make_shared<op::Any>(A, AxisSet{1}), ParameterVector{A});
173
174     auto backend = runtime::Backend::create("${BACKEND_NAME}");
175
176     // Create some tensors for input/output
177     auto a = backend->create_tensor(element::boolean, shape);
178     copy_data(
179         a, test::NDArray<char, 3>({{{0, 1, 0}, {0, 0, 1}}, {{1, 0, 1}, {0, 0, 0}}}).get_vector());
180     auto result = backend->create_tensor(element::boolean, Shape{2, 3});
181
182     auto handle = backend->compile(f);
183     handle->call_with_validate({result}, {a});
184     EXPECT_EQ((vector<char>{0, 1, 1, 1, 0, 1}), read_vector<char>(result));
185 }
186
187 NGRAPH_TEST(${BACKEND_NAME}, any_2x2x3_eliminate_dim_2)
188 {
189     Shape shape{2, 2, 3};
190     auto A = make_shared<op::Parameter>(element::boolean, shape);
191     auto f = make_shared<Function>(make_shared<op::Any>(A, AxisSet{2}), ParameterVector{A});
192
193     auto backend = runtime::Backend::create("${BACKEND_NAME}");
194
195     // Create some tensors for input/output
196     auto a = backend->create_tensor(element::boolean, shape);
197     copy_data(
198         a, test::NDArray<char, 3>({{{0, 1, 0}, {0, 0, 1}}, {{1, 0, 1}, {0, 0, 0}}}).get_vector());
199     auto result = backend->create_tensor(element::boolean, Shape{2, 2});
200
201     auto handle = backend->compile(f);
202     handle->call_with_validate({result}, {a});
203     EXPECT_EQ((vector<char>{1, 1, 1, 0}), read_vector<char>(result));
204 }
205
206 NGRAPH_TEST(${BACKEND_NAME}, any_2x2x3_eliminate_dims_0_1)
207 {
208     Shape shape{2, 2, 3};
209     auto A = make_shared<op::Parameter>(element::boolean, shape);
210     auto f = make_shared<Function>(make_shared<op::Any>(A, AxisSet{0, 1}), 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::boolean, shape);
216     copy_data(
217         a, test::NDArray<char, 3>({{{0, 1, 0}, {0, 0, 1}}, {{1, 0, 1}, {0, 0, 0}}}).get_vector());
218     auto result = backend->create_tensor(element::boolean, Shape{3});
219
220     auto handle = backend->compile(f);
221     handle->call_with_validate({result}, {a});
222     EXPECT_EQ((vector<char>{1, 1, 1}), read_vector<char>(result));
223 }
224
225 NGRAPH_TEST(${BACKEND_NAME}, any_2x2x3_eliminate_dims_0_2)
226 {
227     Shape shape{2, 2, 3};
228     auto A = make_shared<op::Parameter>(element::boolean, shape);
229     auto f = make_shared<Function>(make_shared<op::Any>(A, AxisSet{0, 2}), ParameterVector{A});
230
231     auto backend = runtime::Backend::create("${BACKEND_NAME}");
232
233     // Create some tensors for input/output
234     auto a = backend->create_tensor(element::boolean, shape);
235     copy_data(
236         a, test::NDArray<char, 3>({{{0, 1, 0}, {0, 0, 1}}, {{1, 0, 1}, {0, 0, 0}}}).get_vector());
237     auto result = backend->create_tensor(element::boolean, Shape{2});
238
239     auto handle = backend->compile(f);
240     handle->call_with_validate({result}, {a});
241     EXPECT_EQ((vector<char>{1, 1}), read_vector<char>(result));
242 }
243
244 NGRAPH_TEST(${BACKEND_NAME}, any_2x2x3_eliminate_dims_1_2)
245 {
246     Shape shape{2, 2, 3};
247     auto A = make_shared<op::Parameter>(element::boolean, shape);
248     auto f = make_shared<Function>(make_shared<op::Any>(A, AxisSet{1, 2}), ParameterVector{A});
249
250     auto backend = runtime::Backend::create("${BACKEND_NAME}");
251
252     // Create some tensors for input/output
253     auto a = backend->create_tensor(element::boolean, shape);
254     copy_data(
255         a, test::NDArray<char, 3>({{{0, 1, 0}, {0, 0, 1}}, {{1, 0, 1}, {0, 0, 0}}}).get_vector());
256     auto result = backend->create_tensor(element::boolean, Shape{2});
257
258     auto handle = backend->compile(f);
259     handle->call_with_validate({result}, {a});
260     EXPECT_EQ((vector<char>{1, 1}), read_vector<char>(result));
261 }
262
263 NGRAPH_TEST(${BACKEND_NAME}, any_2x2x3_eliminate_dims_0_1_2)
264 {
265     Shape shape{2, 2, 3};
266     auto A = make_shared<op::Parameter>(element::boolean, shape);
267     auto f = make_shared<Function>(make_shared<op::Any>(A, AxisSet{0, 1, 2}), ParameterVector{A});
268
269     auto backend = runtime::Backend::create("${BACKEND_NAME}");
270
271     // Create some tensors for input/output
272     auto a = backend->create_tensor(element::boolean, shape);
273     copy_data(
274         a, test::NDArray<char, 3>({{{0, 1, 0}, {0, 0, 1}}, {{1, 0, 1}, {0, 0, 0}}}).get_vector());
275     auto result = backend->create_tensor(element::boolean, Shape{});
276
277     auto handle = backend->compile(f);
278     handle->call_with_validate({result}, {a});
279     EXPECT_EQ((vector<char>{1}), read_vector<char>(result));
280 }