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_f.hpp"
22 #include "util/test_control.hpp"
23 #include "util/test_tools.hpp"
25 NGRAPH_SUPPRESS_DEPRECATED_START
28 using namespace ngraph;
30 static string s_manifest = "${MANIFEST}";
32 NGRAPH_TEST(${BACKEND_NAME}, create_dynamic_backend)
34 auto backend = runtime::Backend::create("${BACKEND_NAME}", true);
35 ASSERT_NE(backend, nullptr);
36 ASSERT_TRUE(backend->supports_dynamic_tensors());
39 NGRAPH_TEST(${BACKEND_NAME}, create_dynamic_tensor)
41 auto backend = runtime::Backend::create("${BACKEND_NAME}", true);
42 auto t = backend->create_dynamic_tensor(element::f32, PartialShape{2, Dimension::dynamic(), 3});
43 ASSERT_TRUE(t->get_partial_shape().same_scheme(PartialShape{2, Dimension::dynamic(), 3}));
46 NGRAPH_TEST(${BACKEND_NAME}, dynamic_abc)
49 // Create a graph for f(a,b,c) = (a+b)*c, where a, b, c all have shape {2,?,3}.
51 auto a = make_shared<op::Parameter>(element::f32, PartialShape{2, Dimension::dynamic(), 3});
52 auto b = make_shared<op::Parameter>(element::f32, PartialShape{2, Dimension::dynamic(), 3});
53 auto c = make_shared<op::Parameter>(element::f32, PartialShape{2, Dimension::dynamic(), 3});
55 auto a_plus_b_times_c = (a + b) * c;
57 auto f = make_shared<Function>(NodeVector{a_plus_b_times_c}, ParameterVector{a, b, c});
60 // Get a backend with dynamic support, and compile f.
62 auto backend = runtime::Backend::create("${BACKEND_NAME}", true);
64 auto ex = backend->compile(f);
67 // Create a dynamic output tensor with shape {2,?,3}.
70 backend->create_dynamic_tensor(element::f32, PartialShape{2, Dimension::dynamic(), 3});
73 // For each of n=[0,...,5), run the compiled executable against a test vector of shape
74 // {2,n,3}, and check the results.
76 for (size_t middle_dim = 0; middle_dim < 5; middle_dim++)
78 // Fill in some test input values, which we'll use for a, b, and c.
79 vector<float> inputs(2 * middle_dim * 3);
80 for (size_t i = 0; i < 2 * middle_dim * 3; i++)
85 // Create static tensors for the inputs and copy data.
86 auto t_a = backend->create_tensor(element::f32, Shape{2, middle_dim, 3});
87 auto t_b = backend->create_tensor(element::f32, Shape{2, middle_dim, 3});
88 auto t_c = backend->create_tensor(element::f32, Shape{2, middle_dim, 3});
90 copy_data(t_a, inputs);
91 copy_data(t_b, inputs);
92 copy_data(t_c, inputs);
94 // Call ex, writing result into t_r (note we're using the same t_r from outside the loop.)
95 ex->call_with_validate({t_r}, {t_a, t_b, t_c});
97 // After call, t_r should have a shape of {2,n,3}.
98 ASSERT_EQ(t_r->get_shape(), (Shape{2, middle_dim, 3}));
100 // Read out the results, and compare them against expected values.
101 auto results = read_vector<float>(t_r);
103 vector<float> expected_values(2 * middle_dim * 3);
104 for (size_t i = 0; i < 2 * middle_dim * 3; i++)
106 expected_values[i] = (i + i) * i;
109 EXPECT_TRUE(test::all_close_f(results, expected_values));
113 static void axpy_test(const PartialShape& input_pshape, const std::vector<Shape>& input_shapes)
115 auto a = make_shared<op::Parameter>(element::f32, input_pshape);
116 auto x = make_shared<op::Parameter>(element::f32, input_pshape);
117 auto y = make_shared<op::Parameter>(element::f32, input_pshape);
119 auto axpy = a * x + y;
121 auto f = make_shared<Function>(NodeVector{axpy}, ParameterVector{a, x, y});
122 auto backend = runtime::Backend::create("${BACKEND_NAME}", true);
123 auto ex = backend->compile(f);
125 auto t_r = backend->create_dynamic_tensor(element::f32, input_pshape);
127 for (auto& shape : input_shapes)
129 vector<float> inputs(shape_size(shape));
130 for (size_t i = 0; i < shape_size(shape); i++)
135 auto t_a = backend->create_tensor(element::f32, shape);
136 auto t_x = backend->create_tensor(element::f32, shape);
137 auto t_y = backend->create_tensor(element::f32, shape);
139 copy_data(t_a, inputs);
140 copy_data(t_x, inputs);
141 copy_data(t_y, inputs);
143 ex->call_with_validate({t_r}, {t_a, t_x, t_y});
145 ASSERT_EQ(t_r->get_shape(), shape);
147 auto results = read_vector<float>(t_r);
149 vector<float> expected_values(shape_size(shape));
150 for (size_t i = 0; i < shape_size(shape); i++)
152 expected_values[i] = (i * i) + i;
155 EXPECT_TRUE(test::all_close_f(results, expected_values));
159 NGRAPH_TEST(${BACKEND_NAME}, dynamic_axpy)
161 // Test with shape {?, 3, 3}.
162 axpy_test(PartialShape{Dimension::dynamic(), 3, 3}, {Shape{2, 3, 3}, Shape{5, 3, 3}});
164 // Test with shape {?, ?, ?}.
165 axpy_test(PartialShape::dynamic(3),
166 {Shape{2, 3, 3}, Shape{5, 3, 3}, Shape{2, 5, 2}, Shape{8, 1, 8}});
168 // Test with shape ?. (Rank unknown.)
169 axpy_test(PartialShape::dynamic(),
177 Shape{2, 3, 4, 5, 2}});
180 static void to_vector_test(const PartialShape& input_pshape, const std::vector<Shape>& input_shapes)
182 auto x = make_shared<op::Parameter>(element::f32, input_pshape);
184 shared_ptr<Node> x_new_shape = make_shared<op::v0::ShapeOf>(x);
185 x_new_shape = make_shared<op::Product>(x_new_shape, AxisSet{0});
186 x_new_shape = make_shared<op::Reshape>(x_new_shape, AxisVector{}, Shape{1});
188 auto x_reshaped = make_shared<op::v1::Reshape>(x, x_new_shape, true);
190 auto f = make_shared<Function>(NodeVector{x_reshaped}, ParameterVector{x});
191 auto backend = runtime::Backend::create("${BACKEND_NAME}", true);
192 auto ex = backend->compile(f);
194 auto t_r = backend->create_dynamic_tensor(element::f32, PartialShape::dynamic(1));
196 for (auto& shape : input_shapes)
198 vector<float> inputs(shape_size(shape));
199 for (size_t i = 0; i < shape_size(shape); i++)
204 auto t_x = backend->create_tensor(element::f32, shape);
206 copy_data(t_x, inputs);
208 ex->call_with_validate({t_r}, {t_x});
210 ASSERT_EQ(t_r->get_shape(), (Shape{shape_size(shape)}));
212 auto results = read_vector<float>(t_r);
214 EXPECT_TRUE(test::all_close_f(results, inputs));
218 NGRAPH_TEST(${BACKEND_NAME}, dynamic_to_vector)
220 // Test with shape {?, 3, 3}.
221 to_vector_test(PartialShape{Dimension::dynamic(), 3, 3}, {Shape{2, 3, 3}, Shape{5, 3, 3}});
223 // Test with shape {?, ?, ?}.
224 to_vector_test(PartialShape::dynamic(3),
225 {Shape{2, 3, 3}, Shape{5, 3, 3}, Shape{2, 5, 2}, Shape{8, 1, 8}});
227 // Test with shape ?. (Rank unknown.)
228 to_vector_test(PartialShape::dynamic(),
236 Shape{2, 3, 4, 5, 2}});
239 static void reverse_shape_test(const PartialShape& input_pshape,
240 const std::vector<Shape>& input_shapes)
242 auto x = make_shared<op::Parameter>(element::f32, input_pshape);
244 shared_ptr<Node> x_new_shape = make_shared<op::v0::ShapeOf>(x);
245 x_new_shape = make_shared<op::Reverse>(x_new_shape, AxisSet{0});
247 auto x_reshaped = make_shared<op::v1::Reshape>(x, x_new_shape, true);
249 auto f = make_shared<Function>(NodeVector{x_reshaped}, ParameterVector{x});
250 auto backend = runtime::Backend::create("${BACKEND_NAME}", true);
251 auto ex = backend->compile(f);
253 auto t_r = backend->create_dynamic_tensor(element::f32, PartialShape::dynamic());
255 for (auto& shape : input_shapes)
257 vector<float> inputs(shape_size(shape));
258 for (size_t i = 0; i < shape_size(shape); i++)
263 auto t_x = backend->create_tensor(element::f32, shape);
265 copy_data(t_x, inputs);
267 ex->call_with_validate({t_r}, {t_x});
269 Shape expected_shape = shape;
270 std::reverse(expected_shape.begin(), expected_shape.end());
271 ASSERT_EQ(t_r->get_shape(), expected_shape);
273 auto results = read_vector<float>(t_r);
275 EXPECT_TRUE(test::all_close_f(results, inputs));
279 NGRAPH_TEST(${BACKEND_NAME}, dynamic_reverse_shape)
281 // Test with shape {?, 3, 3}.
282 reverse_shape_test(PartialShape{Dimension::dynamic(), 3, 3}, {Shape{2, 3, 3}, Shape{5, 3, 3}});
284 // Test with shape {?, ?, ?}.
285 reverse_shape_test(PartialShape::dynamic(3),
286 {Shape{2, 3, 3}, Shape{5, 3, 3}, Shape{2, 5, 2}, Shape{8, 1, 8}});
288 // Test with shape ?. (Rank unknown.)
289 reverse_shape_test(PartialShape::dynamic(),
297 Shape{2, 3, 4, 5, 2}});