Deprecate nGraph v0 ops and builders (#1856)
[platform/upstream/dldt.git] / ngraph / test / backend / constant.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/ndarray.hpp"
24 #include "util/test_control.hpp"
25 #include "util/test_tools.hpp"
26
27 NGRAPH_SUPPRESS_DEPRECATED_START
28
29 using namespace std;
30 using namespace ngraph;
31
32 static string s_manifest = "${MANIFEST}";
33
34 NGRAPH_TEST(${BACKEND_NAME}, tensor_constant)
35 {
36     Shape shape{2, 2, 2};
37     auto A = op::Constant::create(element::f32, shape, {1, 2, 3, 4, 5, 6, 7, 8});
38     auto f = make_shared<Function>(A, ParameterVector{});
39
40     auto backend = runtime::Backend::create("${BACKEND_NAME}");
41
42     // Create some tensors for input/output
43     auto result = backend->create_tensor(element::f32, shape);
44
45     auto handle = backend->compile(f);
46     handle->call_with_validate({result}, {});
47     EXPECT_TRUE(test::all_close_f((vector<float>{1, 2, 3, 4, 5, 6, 7, 8}),
48                                   read_vector<float>(result),
49                                   MIN_FLOAT_TOLERANCE_BITS));
50 }
51
52 NGRAPH_TEST(${BACKEND_NAME}, tensor_2constant)
53 {
54     Shape shape{2, 2, 2};
55     auto A = op::Constant::create(element::f32, shape, {1, 2, 3, 4, 5, 6, 7, 8});
56     auto f = make_shared<Function>(NodeVector{A, A}, ParameterVector{});
57
58     auto backend = runtime::Backend::create("${BACKEND_NAME}");
59
60     // Create some tensors for input/output
61     auto result0 = backend->create_tensor(element::f32, shape);
62     auto result1 = backend->create_tensor(element::f32, shape);
63
64     auto handle = backend->compile(f);
65     handle->call_with_validate({result0, result1}, {});
66     EXPECT_TRUE(test::all_close_f((vector<float>{1, 2, 3, 4, 5, 6, 7, 8}),
67                                   read_vector<float>(result0),
68                                   MIN_FLOAT_TOLERANCE_BITS));
69     EXPECT_TRUE(test::all_close_f((vector<float>{1, 2, 3, 4, 5, 6, 7, 8}),
70                                   read_vector<float>(result1),
71                                   MIN_FLOAT_TOLERANCE_BITS));
72 }
73
74 NGRAPH_TEST(${BACKEND_NAME}, tensor_constant_with_op)
75 {
76     Shape shape{2, 2, 2};
77     auto A = op::Constant::create(element::f32, shape, {-1, 2, 3, -4, 5, -6, -7, 8});
78     auto f = make_shared<Function>(make_shared<op::Abs>(A), ParameterVector{});
79
80     auto backend = runtime::Backend::create("${BACKEND_NAME}");
81
82     // Create some tensors for input/output
83     auto result = backend->create_tensor(element::f32, shape);
84
85     auto handle = backend->compile(f);
86     handle->call_with_validate({result}, {});
87     EXPECT_TRUE(test::all_close_f((vector<float>{1, 2, 3, 4, 5, 6, 7, 8}),
88                                   read_vector<float>(result),
89                                   MIN_FLOAT_TOLERANCE_BITS));
90 }
91
92 NGRAPH_TEST(${BACKEND_NAME}, constant_multi_use)
93 {
94     auto A = make_shared<op::Constant>(element::i32, Shape{}, std::vector<std::string>{"388"});
95     auto f = make_shared<Function>(A, ParameterVector{});
96     auto backend = runtime::Backend::create("${BACKEND_NAME}");
97
98     std::shared_ptr<runtime::Tensor> r1 = backend->create_tensor(element::i32, Shape{});
99     auto handle = backend->compile(f);
100     handle->call_with_validate({r1}, std::vector<std::shared_ptr<runtime::Tensor>>{});
101     EXPECT_EQ(read_vector<int>(r1), std::vector<int>{388});
102
103     std::shared_ptr<runtime::Tensor> r2 = backend->create_tensor(element::i32, Shape{});
104     handle->call_with_validate({r2}, std::vector<std::shared_ptr<runtime::Tensor>>{});
105     EXPECT_EQ(read_vector<int>(r2), std::vector<int>{388});
106 }
107
108 NGRAPH_TEST(${BACKEND_NAME}, scalar_constant_float32)
109 {
110     auto r = op::Constant::create(element::f32, Shape{}, {4.75});
111     auto f = make_shared<Function>(r, ParameterVector{});
112
113     auto backend = runtime::Backend::create("${BACKEND_NAME}");
114
115     // Create some tensors for input/output
116     auto result = backend->create_tensor(element::f32, Shape{});
117
118     auto handle = backend->compile(f);
119     handle->call_with_validate({result}, {});
120     EXPECT_TRUE(test::all_close_f(
121         vector<float>{4.75f}, read_vector<float>(result), MIN_FLOAT_TOLERANCE_BITS));
122 }
123
124 NGRAPH_TEST(${BACKEND_NAME}, scalar_constant_int64)
125 {
126     auto r = op::Constant::create(element::i64, Shape{}, {0x4000000000000001});
127     auto f = make_shared<Function>(r, ParameterVector{});
128
129     auto backend = runtime::Backend::create("${BACKEND_NAME}");
130
131     // Create some tensors for input/output
132     auto result = backend->create_tensor(element::i64, Shape{});
133
134     auto handle = backend->compile(f);
135     handle->call_with_validate({result}, {});
136     EXPECT_EQ(vector<int64_t>{0x4000000000000001}, read_vector<int64_t>(result));
137 }
138
139 NGRAPH_TEST(${BACKEND_NAME}, tensor_constant_float32)
140 {
141     Shape shape{2, 2};
142     auto r = op::Constant::create(element::f32, shape, {4.75, 4.5, -5.25, 0.0});
143     auto f = make_shared<Function>(r, ParameterVector{});
144
145     auto backend = runtime::Backend::create("${BACKEND_NAME}");
146
147     // Create some tensors for input/output
148     auto result = backend->create_tensor(element::f32, shape);
149
150     auto handle = backend->compile(f);
151     handle->call_with_validate({result}, {});
152     EXPECT_TRUE(test::all_close_f((vector<float>{4.75f, 4.5f, -5.25f, 0.0f}),
153                                   read_vector<float>(result),
154                                   MIN_FLOAT_TOLERANCE_BITS));
155 }
156
157 NGRAPH_TEST(${BACKEND_NAME}, tensor_constant_int64)
158 {
159     Shape shape{2};
160     auto r = op::Constant::create(element::i64, shape, {0x4000000000000001, 0x4000000000000002});
161     auto f = make_shared<Function>(r, ParameterVector{});
162     auto backend = runtime::Backend::create("${BACKEND_NAME}");
163     // Create some tensors for input/output
164     auto result = backend->create_tensor(element::i64, shape);
165     auto handle = backend->compile(f);
166     handle->call_with_validate({result}, {});
167     EXPECT_EQ((vector<int64_t>{0x4000000000000001, 0x4000000000000002}),
168               read_vector<int64_t>(result));
169 }
170
171 NGRAPH_TEST(${BACKEND_NAME}, constant_equality_bool)
172 {
173     Shape shape{4};
174     // auto A = make_shared<op::Parameter>(element::boolean, shape);
175     // auto B = make_shared<op::Parameter>(element::boolean, shape);
176     // auto f = make_shared<Function>(make_shared<op::Equal>(A, B), ParameterVector{A, B});
177
178     auto A = op::Constant::create(element::boolean, shape, {true, false, true, false});
179     auto B = op::Constant::create(element::boolean, shape, {true, true, true, true});
180     auto f = make_shared<Function>(make_shared<op::Equal>(A, B), ParameterVector{});
181
182     auto backend = runtime::Backend::create("${BACKEND_NAME}");
183
184     // Create some tensors for input/output
185     auto result = backend->create_tensor(element::boolean, shape);
186
187     auto handle = backend->compile(f);
188     handle->call_with_validate({result}, {});
189     EXPECT_EQ((vector<char>{true, false, true, false}), read_vector<char>(result));
190 }