Deprecate nGraph v0 ops and builders (#1856)
[platform/upstream/dldt.git] / ngraph / test / backend / zero_sized.in.cpp
1 //*****************************************************************************
2 // Copyright 2017-2020 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //*****************************************************************************
16
17 #include "gtest/gtest.h"
18 #include "ngraph/ngraph.hpp"
19 #include "ngraph/runtime/tensor.hpp"
20 #include "runtime/backend.hpp"
21 #include "util/all_close.hpp"
22 #include "util/all_close_f.hpp"
23 #include "util/known_element_types.hpp"
24 #include "util/ndarray.hpp"
25 #include "util/test_control.hpp"
26 #include "util/test_tools.hpp"
27
28 NGRAPH_SUPPRESS_DEPRECATED_START
29
30 using namespace std;
31 using namespace ngraph;
32
33 static string s_manifest = "${MANIFEST}";
34
35 template <typename OP>
36 void make_unary_empty_test(const string& backend_name)
37 {
38     Shape shape{0};
39
40     ParameterVector params;
41     NodeVector result_list;
42     for (size_t i = 0; i < s_known_element_types.size(); i++)
43     {
44         shared_ptr<op::Parameter> p = make_shared<op::Parameter>(s_known_element_types[i], shape);
45         params.push_back(p);
46         result_list.push_back(make_shared<OP>(p));
47     }
48
49     auto f = make_shared<Function>(result_list, params);
50     auto backend = runtime::Backend::create(backend_name);
51
52     vector<shared_ptr<runtime::Tensor>> inputs;
53     vector<shared_ptr<runtime::Tensor>> outputs;
54     for (size_t i = 0; i < s_known_element_types.size(); i++)
55     {
56         inputs.push_back(backend->create_tensor(s_known_element_types[i], shape));
57         outputs.push_back(backend->create_tensor(s_known_element_types[i], shape));
58     }
59
60     auto handle = backend->compile(f);
61     handle->call_with_validate(outputs, inputs);
62
63     EXPECT_EQ(read_vector<float>(inputs[0]).size(), 0);
64     EXPECT_EQ(read_vector<double>(inputs[1]).size(), 0);
65     EXPECT_EQ(read_vector<int8_t>(inputs[2]).size(), 0);
66     EXPECT_EQ(read_vector<int16_t>(inputs[3]).size(), 0);
67     EXPECT_EQ(read_vector<int32_t>(inputs[4]).size(), 0);
68     EXPECT_EQ(read_vector<int64_t>(inputs[5]).size(), 0);
69     EXPECT_EQ(read_vector<uint8_t>(inputs[6]).size(), 0);
70     EXPECT_EQ(read_vector<uint16_t>(inputs[7]).size(), 0);
71     EXPECT_EQ(read_vector<uint32_t>(inputs[8]).size(), 0);
72     EXPECT_EQ(read_vector<uint64_t>(inputs[9]).size(), 0);
73
74     EXPECT_EQ(read_vector<float>(outputs[0]).size(), 0);
75     EXPECT_EQ(read_vector<double>(outputs[1]).size(), 0);
76     EXPECT_EQ(read_vector<int8_t>(outputs[2]).size(), 0);
77     EXPECT_EQ(read_vector<int16_t>(outputs[3]).size(), 0);
78     EXPECT_EQ(read_vector<int32_t>(outputs[4]).size(), 0);
79     EXPECT_EQ(read_vector<int64_t>(outputs[5]).size(), 0);
80     EXPECT_EQ(read_vector<uint8_t>(outputs[6]).size(), 0);
81     EXPECT_EQ(read_vector<uint16_t>(outputs[7]).size(), 0);
82     EXPECT_EQ(read_vector<uint32_t>(outputs[8]).size(), 0);
83     EXPECT_EQ(read_vector<uint64_t>(outputs[9]).size(), 0);
84 }
85
86 template <typename OP>
87 void make_binary_empty_test(const string& backend_name, bool is_comparison = false)
88 {
89     Shape shape{0};
90     ParameterVector A;
91     for (size_t i = 0; i < s_known_element_types.size(); i++)
92     {
93         A.push_back(make_shared<op::Parameter>(s_known_element_types[i], shape));
94     }
95
96     NodeVector result_list;
97     for (shared_ptr<op::Parameter> p : A)
98     {
99         result_list.push_back(make_shared<OP>(p, p));
100     }
101
102     auto f = make_shared<Function>(result_list, A);
103     auto backend = runtime::Backend::create(backend_name);
104
105     vector<shared_ptr<runtime::Tensor>> inputs;
106     vector<shared_ptr<runtime::Tensor>> outputs;
107     for (size_t i = 0; i < s_known_element_types.size(); i++)
108     {
109         inputs.push_back(backend->create_tensor(s_known_element_types[i], shape));
110         if (is_comparison)
111         {
112             outputs.push_back(backend->create_tensor(element::from<char>(), shape));
113         }
114         else
115         {
116             outputs.push_back(backend->create_tensor(s_known_element_types[i], shape));
117         }
118     }
119
120     auto handle = backend->compile(f);
121     handle->call_with_validate(outputs, inputs);
122
123     EXPECT_EQ(read_vector<float>(inputs[0]).size(), 0);
124     EXPECT_EQ(read_vector<double>(inputs[1]).size(), 0);
125     EXPECT_EQ(read_vector<int8_t>(inputs[2]).size(), 0);
126     EXPECT_EQ(read_vector<int16_t>(inputs[3]).size(), 0);
127     EXPECT_EQ(read_vector<int32_t>(inputs[4]).size(), 0);
128     EXPECT_EQ(read_vector<int64_t>(inputs[5]).size(), 0);
129     EXPECT_EQ(read_vector<uint8_t>(inputs[6]).size(), 0);
130     EXPECT_EQ(read_vector<uint16_t>(inputs[7]).size(), 0);
131     EXPECT_EQ(read_vector<uint32_t>(inputs[8]).size(), 0);
132     EXPECT_EQ(read_vector<uint64_t>(inputs[9]).size(), 0);
133
134     if (is_comparison)
135     {
136         EXPECT_EQ(read_vector<char>(outputs[0]).size(), 0);
137         EXPECT_EQ(read_vector<char>(outputs[1]).size(), 0);
138         EXPECT_EQ(read_vector<char>(outputs[2]).size(), 0);
139         EXPECT_EQ(read_vector<char>(outputs[3]).size(), 0);
140         EXPECT_EQ(read_vector<char>(outputs[4]).size(), 0);
141         EXPECT_EQ(read_vector<char>(outputs[5]).size(), 0);
142         EXPECT_EQ(read_vector<char>(outputs[6]).size(), 0);
143         EXPECT_EQ(read_vector<char>(outputs[7]).size(), 0);
144         EXPECT_EQ(read_vector<char>(outputs[8]).size(), 0);
145         EXPECT_EQ(read_vector<char>(outputs[9]).size(), 0);
146     }
147     else
148     {
149         EXPECT_EQ(read_vector<float>(outputs[0]).size(), 0);
150         EXPECT_EQ(read_vector<double>(outputs[1]).size(), 0);
151         EXPECT_EQ(read_vector<int8_t>(outputs[2]).size(), 0);
152         EXPECT_EQ(read_vector<int16_t>(outputs[3]).size(), 0);
153         EXPECT_EQ(read_vector<int32_t>(outputs[4]).size(), 0);
154         EXPECT_EQ(read_vector<int64_t>(outputs[5]).size(), 0);
155         EXPECT_EQ(read_vector<uint8_t>(outputs[6]).size(), 0);
156         EXPECT_EQ(read_vector<uint16_t>(outputs[7]).size(), 0);
157         EXPECT_EQ(read_vector<uint32_t>(outputs[8]).size(), 0);
158         EXPECT_EQ(read_vector<uint64_t>(outputs[9]).size(), 0);
159     }
160 }
161
162 NGRAPH_TEST(${BACKEND_NAME}, zero_sized_abs)
163 {
164     make_unary_empty_test<op::Abs>("${BACKEND_NAME}");
165 }
166
167 NGRAPH_TEST(${BACKEND_NAME}, zero_sized_ceiling)
168 {
169     make_unary_empty_test<op::Ceiling>("${BACKEND_NAME}");
170 }
171
172 NGRAPH_TEST(${BACKEND_NAME}, zero_sized_erf)
173 {
174     make_unary_empty_test<op::Erf>("${BACKEND_NAME}");
175 }
176
177 NGRAPH_TEST(${BACKEND_NAME}, zero_sized_exp)
178 {
179     make_unary_empty_test<op::Exp>("${BACKEND_NAME}");
180 }
181
182 NGRAPH_TEST(${BACKEND_NAME}, zero_sized_floor)
183 {
184     make_unary_empty_test<op::Floor>("${BACKEND_NAME}");
185 }
186
187 NGRAPH_TEST(${BACKEND_NAME}, zero_sized_log)
188 {
189     make_unary_empty_test<op::Log>("${BACKEND_NAME}");
190 }
191
192 NGRAPH_TEST(${BACKEND_NAME}, zero_sized_negative)
193 {
194     make_unary_empty_test<op::Negative>("${BACKEND_NAME}");
195 }
196
197 NGRAPH_TEST(${BACKEND_NAME}, zero_sized_not)
198 {
199     Shape shape{0};
200     auto A = make_shared<op::Parameter>(element::from<char>(), shape);
201     auto f = make_shared<Function>(make_shared<op::Not>(A), ParameterVector{A});
202
203     auto backend = runtime::Backend::create("${BACKEND_NAME}");
204
205     auto a = backend->create_tensor(element::from<char>(), shape);
206     auto result = backend->create_tensor(element::from<char>(), shape);
207
208     auto handle = backend->compile(f);
209     handle->call_with_validate({result}, {a});
210
211     auto in_vec = read_vector<char>(a);
212     auto out_vec = read_vector<char>(result);
213
214     EXPECT_EQ(in_vec.size(), 0);
215     EXPECT_EQ(out_vec.size(), 0);
216 }
217
218 NGRAPH_TEST(${BACKEND_NAME}, zero_sized_sign)
219 {
220     make_unary_empty_test<op::Sign>("${BACKEND_NAME}");
221 }
222
223 NGRAPH_TEST(${BACKEND_NAME}, zero_sized_sqrt)
224 {
225     make_unary_empty_test<op::Sqrt>("${BACKEND_NAME}");
226 }
227
228 NGRAPH_TEST(${BACKEND_NAME}, zero_sized_sin)
229 {
230     make_unary_empty_test<op::Sin>("${BACKEND_NAME}");
231 }
232
233 NGRAPH_TEST(${BACKEND_NAME}, zero_sized_sinh)
234 {
235     make_unary_empty_test<op::Sinh>("${BACKEND_NAME}");
236 }
237
238 NGRAPH_TEST(${BACKEND_NAME}, zero_sized_cos)
239 {
240     make_unary_empty_test<op::Cos>("${BACKEND_NAME}");
241 }
242
243 NGRAPH_TEST(${BACKEND_NAME}, zero_sized_cosh)
244 {
245     make_unary_empty_test<op::Cosh>("${BACKEND_NAME}");
246 }
247
248 NGRAPH_TEST(${BACKEND_NAME}, zero_sized_tan)
249 {
250     make_unary_empty_test<op::Tan>("${BACKEND_NAME}");
251 }
252
253 NGRAPH_TEST(${BACKEND_NAME}, zero_sized_tanh)
254 {
255     make_unary_empty_test<op::Tanh>("${BACKEND_NAME}");
256 }
257
258 NGRAPH_TEST(${BACKEND_NAME}, zero_sized_asin)
259 {
260     make_unary_empty_test<op::Asin>("${BACKEND_NAME}");
261 }
262
263 NGRAPH_TEST(${BACKEND_NAME}, zero_sized_acos)
264 {
265     make_unary_empty_test<op::Acos>("${BACKEND_NAME}");
266 }
267
268 NGRAPH_TEST(${BACKEND_NAME}, zero_sized_atan)
269 {
270     make_unary_empty_test<op::Atan>("${BACKEND_NAME}");
271 }
272
273 NGRAPH_TEST(${BACKEND_NAME}, zero_sized_add)
274 {
275     make_binary_empty_test<op::Add>("${BACKEND_NAME}");
276 }
277
278 NGRAPH_TEST(${BACKEND_NAME}, zero_sized_divide)
279 {
280     make_binary_empty_test<op::Divide>("${BACKEND_NAME}");
281 }
282
283 NGRAPH_TEST(${BACKEND_NAME}, zero_sized_eq)
284 {
285     make_binary_empty_test<op::Equal>("${BACKEND_NAME}", true);
286 }
287
288 NGRAPH_TEST(${BACKEND_NAME}, zero_sized_greater)
289 {
290     make_binary_empty_test<op::Greater>("${BACKEND_NAME}", true);
291 }
292
293 NGRAPH_TEST(${BACKEND_NAME}, zero_sized_greatereq)
294 {
295     make_binary_empty_test<op::GreaterEq>("${BACKEND_NAME}", true);
296 }
297
298 NGRAPH_TEST(${BACKEND_NAME}, zero_sized_less)
299 {
300     make_binary_empty_test<op::Less>("${BACKEND_NAME}", true);
301 }
302
303 NGRAPH_TEST(${BACKEND_NAME}, zero_sized_lesseq)
304 {
305     make_binary_empty_test<op::LessEq>("${BACKEND_NAME}", true);
306 }
307
308 NGRAPH_TEST(${BACKEND_NAME}, zero_sized_maximum)
309 {
310     make_binary_empty_test<op::Maximum>("${BACKEND_NAME}");
311 }
312
313 NGRAPH_TEST(${BACKEND_NAME}, zero_sized_minimum)
314 {
315     make_binary_empty_test<op::Minimum>("${BACKEND_NAME}");
316 }
317
318 NGRAPH_TEST(${BACKEND_NAME}, zero_sized_multiply)
319 {
320     make_binary_empty_test<op::Multiply>("${BACKEND_NAME}");
321 }
322
323 NGRAPH_TEST(${BACKEND_NAME}, zero_sized_not_equal)
324 {
325     make_binary_empty_test<op::NotEqual>("${BACKEND_NAME}", true);
326 }
327
328 NGRAPH_TEST(${BACKEND_NAME}, zero_sized_power)
329 {
330     make_binary_empty_test<op::Power>("${BACKEND_NAME}");
331 }
332
333 NGRAPH_TEST(${BACKEND_NAME}, zero_sized_subtract)
334 {
335     make_binary_empty_test<op::Subtract>("${BACKEND_NAME}");
336 }