Deprecate nGraph v0 ops and builders (#1856)
[platform/upstream/dldt.git] / ngraph / test / backend / dynamic.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_f.hpp"
22 #include "util/test_control.hpp"
23 #include "util/test_tools.hpp"
24
25 NGRAPH_SUPPRESS_DEPRECATED_START
26
27 using namespace std;
28 using namespace ngraph;
29
30 static string s_manifest = "${MANIFEST}";
31
32 NGRAPH_TEST(${BACKEND_NAME}, create_dynamic_backend)
33 {
34     auto backend = runtime::Backend::create("${BACKEND_NAME}", true);
35     ASSERT_NE(backend, nullptr);
36     ASSERT_TRUE(backend->supports_dynamic_tensors());
37 }
38
39 NGRAPH_TEST(${BACKEND_NAME}, create_dynamic_tensor)
40 {
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}));
44 }
45
46 NGRAPH_TEST(${BACKEND_NAME}, dynamic_abc)
47 {
48     //
49     // Create a graph for f(a,b,c) = (a+b)*c, where a, b, c all have shape {2,?,3}.
50     //
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});
54
55     auto a_plus_b_times_c = (a + b) * c;
56
57     auto f = make_shared<Function>(NodeVector{a_plus_b_times_c}, ParameterVector{a, b, c});
58
59     //
60     // Get a backend with dynamic support, and compile f.
61     //
62     auto backend = runtime::Backend::create("${BACKEND_NAME}", true);
63
64     auto ex = backend->compile(f);
65
66     //
67     // Create a dynamic output tensor with shape {2,?,3}.
68     //
69     auto t_r =
70         backend->create_dynamic_tensor(element::f32, PartialShape{2, Dimension::dynamic(), 3});
71
72     //
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.
75     //
76     for (size_t middle_dim = 0; middle_dim < 5; middle_dim++)
77     {
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++)
81         {
82             inputs[i] = i;
83         }
84
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});
89
90         copy_data(t_a, inputs);
91         copy_data(t_b, inputs);
92         copy_data(t_c, inputs);
93
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});
96
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}));
99
100         // Read out the results, and compare them against expected values.
101         auto results = read_vector<float>(t_r);
102
103         vector<float> expected_values(2 * middle_dim * 3);
104         for (size_t i = 0; i < 2 * middle_dim * 3; i++)
105         {
106             expected_values[i] = (i + i) * i;
107         }
108
109         EXPECT_TRUE(test::all_close_f(results, expected_values));
110     }
111 }
112
113 static void axpy_test(const PartialShape& input_pshape, const std::vector<Shape>& input_shapes)
114 {
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);
118
119     auto axpy = a * x + y;
120
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);
124
125     auto t_r = backend->create_dynamic_tensor(element::f32, input_pshape);
126
127     for (auto& shape : input_shapes)
128     {
129         vector<float> inputs(shape_size(shape));
130         for (size_t i = 0; i < shape_size(shape); i++)
131         {
132             inputs[i] = i;
133         }
134
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);
138
139         copy_data(t_a, inputs);
140         copy_data(t_x, inputs);
141         copy_data(t_y, inputs);
142
143         ex->call_with_validate({t_r}, {t_a, t_x, t_y});
144
145         ASSERT_EQ(t_r->get_shape(), shape);
146
147         auto results = read_vector<float>(t_r);
148
149         vector<float> expected_values(shape_size(shape));
150         for (size_t i = 0; i < shape_size(shape); i++)
151         {
152             expected_values[i] = (i * i) + i;
153         }
154
155         EXPECT_TRUE(test::all_close_f(results, expected_values));
156     }
157 }
158
159 NGRAPH_TEST(${BACKEND_NAME}, dynamic_axpy)
160 {
161     // Test with shape {?, 3, 3}.
162     axpy_test(PartialShape{Dimension::dynamic(), 3, 3}, {Shape{2, 3, 3}, Shape{5, 3, 3}});
163
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}});
167
168     // Test with shape ?. (Rank unknown.)
169     axpy_test(PartialShape::dynamic(),
170               {Shape{2, 3, 3},
171                Shape{5, 3, 3},
172                Shape{2, 5, 2},
173                Shape{8, 1, 8},
174                Shape{5},
175                Shape{8, 2},
176                Shape{8, 2, 8, 2},
177                Shape{2, 3, 4, 5, 2}});
178 }
179
180 static void to_vector_test(const PartialShape& input_pshape, const std::vector<Shape>& input_shapes)
181 {
182     auto x = make_shared<op::Parameter>(element::f32, input_pshape);
183
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});
187
188     auto x_reshaped = make_shared<op::v1::Reshape>(x, x_new_shape, true);
189
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);
193
194     auto t_r = backend->create_dynamic_tensor(element::f32, PartialShape::dynamic(1));
195
196     for (auto& shape : input_shapes)
197     {
198         vector<float> inputs(shape_size(shape));
199         for (size_t i = 0; i < shape_size(shape); i++)
200         {
201             inputs[i] = i;
202         }
203
204         auto t_x = backend->create_tensor(element::f32, shape);
205
206         copy_data(t_x, inputs);
207
208         ex->call_with_validate({t_r}, {t_x});
209
210         ASSERT_EQ(t_r->get_shape(), (Shape{shape_size(shape)}));
211
212         auto results = read_vector<float>(t_r);
213
214         EXPECT_TRUE(test::all_close_f(results, inputs));
215     }
216 }
217
218 NGRAPH_TEST(${BACKEND_NAME}, dynamic_to_vector)
219 {
220     // Test with shape {?, 3, 3}.
221     to_vector_test(PartialShape{Dimension::dynamic(), 3, 3}, {Shape{2, 3, 3}, Shape{5, 3, 3}});
222
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}});
226
227     // Test with shape ?. (Rank unknown.)
228     to_vector_test(PartialShape::dynamic(),
229                    {Shape{2, 3, 3},
230                     Shape{5, 3, 3},
231                     Shape{2, 5, 2},
232                     Shape{8, 1, 8},
233                     Shape{5},
234                     Shape{8, 2},
235                     Shape{8, 2, 8, 2},
236                     Shape{2, 3, 4, 5, 2}});
237 }
238
239 static void reverse_shape_test(const PartialShape& input_pshape,
240                                const std::vector<Shape>& input_shapes)
241 {
242     auto x = make_shared<op::Parameter>(element::f32, input_pshape);
243
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});
246
247     auto x_reshaped = make_shared<op::v1::Reshape>(x, x_new_shape, true);
248
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);
252
253     auto t_r = backend->create_dynamic_tensor(element::f32, PartialShape::dynamic());
254
255     for (auto& shape : input_shapes)
256     {
257         vector<float> inputs(shape_size(shape));
258         for (size_t i = 0; i < shape_size(shape); i++)
259         {
260             inputs[i] = i;
261         }
262
263         auto t_x = backend->create_tensor(element::f32, shape);
264
265         copy_data(t_x, inputs);
266
267         ex->call_with_validate({t_r}, {t_x});
268
269         Shape expected_shape = shape;
270         std::reverse(expected_shape.begin(), expected_shape.end());
271         ASSERT_EQ(t_r->get_shape(), expected_shape);
272
273         auto results = read_vector<float>(t_r);
274
275         EXPECT_TRUE(test::all_close_f(results, inputs));
276     }
277 }
278
279 NGRAPH_TEST(${BACKEND_NAME}, dynamic_reverse_shape)
280 {
281     // Test with shape {?, 3, 3}.
282     reverse_shape_test(PartialShape{Dimension::dynamic(), 3, 3}, {Shape{2, 3, 3}, Shape{5, 3, 3}});
283
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}});
287
288     // Test with shape ?. (Rank unknown.)
289     reverse_shape_test(PartialShape::dynamic(),
290                        {Shape{2, 3, 3},
291                         Shape{5, 3, 3},
292                         Shape{2, 5, 2},
293                         Shape{8, 1, 8},
294                         Shape{5},
295                         Shape{8, 2},
296                         Shape{8, 2, 8, 2},
297                         Shape{2, 3, 4, 5, 2}});
298 }