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 //*****************************************************************************
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"
27 NGRAPH_SUPPRESS_DEPRECATED_START
30 using namespace ngraph;
32 static string s_manifest = "${MANIFEST}";
34 NGRAPH_TEST(${BACKEND_NAME}, tensor_constant)
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{});
40 auto backend = runtime::Backend::create("${BACKEND_NAME}");
42 // Create some tensors for input/output
43 auto result = backend->create_tensor(element::f32, shape);
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));
52 NGRAPH_TEST(${BACKEND_NAME}, tensor_2constant)
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{});
58 auto backend = runtime::Backend::create("${BACKEND_NAME}");
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);
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));
74 NGRAPH_TEST(${BACKEND_NAME}, tensor_constant_with_op)
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{});
80 auto backend = runtime::Backend::create("${BACKEND_NAME}");
82 // Create some tensors for input/output
83 auto result = backend->create_tensor(element::f32, shape);
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));
92 NGRAPH_TEST(${BACKEND_NAME}, constant_multi_use)
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}");
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});
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});
108 NGRAPH_TEST(${BACKEND_NAME}, scalar_constant_float32)
110 auto r = op::Constant::create(element::f32, Shape{}, {4.75});
111 auto f = make_shared<Function>(r, ParameterVector{});
113 auto backend = runtime::Backend::create("${BACKEND_NAME}");
115 // Create some tensors for input/output
116 auto result = backend->create_tensor(element::f32, Shape{});
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));
124 NGRAPH_TEST(${BACKEND_NAME}, scalar_constant_int64)
126 auto r = op::Constant::create(element::i64, Shape{}, {0x4000000000000001});
127 auto f = make_shared<Function>(r, ParameterVector{});
129 auto backend = runtime::Backend::create("${BACKEND_NAME}");
131 // Create some tensors for input/output
132 auto result = backend->create_tensor(element::i64, Shape{});
134 auto handle = backend->compile(f);
135 handle->call_with_validate({result}, {});
136 EXPECT_EQ(vector<int64_t>{0x4000000000000001}, read_vector<int64_t>(result));
139 NGRAPH_TEST(${BACKEND_NAME}, tensor_constant_float32)
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{});
145 auto backend = runtime::Backend::create("${BACKEND_NAME}");
147 // Create some tensors for input/output
148 auto result = backend->create_tensor(element::f32, shape);
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));
157 NGRAPH_TEST(${BACKEND_NAME}, tensor_constant_int64)
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));
171 NGRAPH_TEST(${BACKEND_NAME}, constant_equality_bool)
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});
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{});
182 auto backend = runtime::Backend::create("${BACKEND_NAME}");
184 // Create some tensors for input/output
185 auto result = backend->create_tensor(element::boolean, shape);
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));