publish master branch snapshot, revision 9df5eb1f84e13a35720a918f88324561222ab114
[platform/upstream/dldt.git] / ngraph / test / copy.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 <memory>
18 #include <string>
19
20 #include "gtest/gtest.h"
21
22 #include "ngraph/ngraph.hpp"
23 #include "util/ndarray.hpp"
24 #include "util/test_tools.hpp"
25
26 using namespace std;
27 using namespace ngraph;
28
29 template <typename OP>
30 bool check_unary()
31 {
32     Shape shape{1};
33     auto arg0 = make_shared<op::Parameter>(element::f32, shape);
34     OutputVector new_args{make_shared<op::Parameter>(element::f32, shape)};
35
36     auto node = make_shared<OP>(arg0);
37     auto new_node = node->copy_with_new_inputs(new_args);
38
39     return (nullptr != new_node) && (new_args == new_node->input_values());
40 }
41
42 template <typename OP>
43 bool check_binary()
44 {
45     Shape shape{1};
46     auto arg0 = make_shared<op::Parameter>(element::f32, shape);
47     auto arg1 = make_shared<op::Parameter>(element::f32, shape);
48     OutputVector new_args{make_shared<op::Parameter>(element::f32, shape),
49                           make_shared<op::Parameter>(element::f32, shape)};
50
51     auto node = make_shared<OP>(arg0, arg1);
52     auto new_node = node->copy_with_new_inputs(new_args);
53
54     return (nullptr != new_node) && (new_args == new_node->input_values());
55 }
56
57 TEST(copy, abs)
58 {
59     ASSERT_TRUE(check_unary<op::Abs>());
60 }
61
62 TEST(copy, acos)
63 {
64     ASSERT_TRUE(check_unary<op::Acos>());
65 }
66
67 TEST(copy, add)
68 {
69     ASSERT_TRUE(check_binary<op::Add>());
70 }
71
72 TEST(copy, asin)
73 {
74     ASSERT_TRUE(check_unary<op::Asin>());
75 }
76
77 TEST(copy, atan)
78 {
79     ASSERT_TRUE(check_unary<op::Atan>());
80 }
81
82 TEST(copy, atan2)
83 {
84     ASSERT_TRUE(check_binary<op::Atan2>());
85 }
86
87 TEST(copy, broadcast)
88 {
89     Shape shape1{1};
90     auto arg0 = make_shared<op::Parameter>(element::f32, shape1);
91     OutputVector new_args{make_shared<op::Parameter>(element::f32, shape1)};
92
93     Shape shape{4, 1, 3};
94     AxisSet axes{0, 2};
95
96     auto node = make_shared<op::Broadcast>(arg0, shape, axes);
97     auto new_node = node->copy_with_new_inputs(new_args);
98     auto node_cast = as_type_ptr<op::Broadcast>(new_node);
99     ASSERT_NE(node_cast, nullptr);
100
101     ASSERT_TRUE(nullptr != new_node);
102     ASSERT_TRUE(new_args == new_node->input_values());
103     ASSERT_TRUE(shape == node_cast->get_broadcast_shape());
104     ASSERT_TRUE(axes == node_cast->get_broadcast_axes());
105 }
106
107 TEST(copy, ceiling)
108 {
109     ASSERT_TRUE(check_unary<op::Ceiling>());
110 }
111
112 TEST(copy, concat)
113 {
114     Shape shape{1};
115     auto arg0 = make_shared<op::Parameter>(element::f32, shape);
116     auto arg1 = make_shared<op::Parameter>(element::f32, shape);
117     OutputVector new_args{make_shared<op::Parameter>(element::f32, shape),
118                           make_shared<op::Parameter>(element::f32, shape)};
119     size_t axis = 0;
120     auto node = make_shared<op::Concat>(NodeVector{arg0, arg1}, axis);
121     auto new_node = node->clone_with_new_inputs(new_args);
122     auto node_cast = as_type_ptr<op::Concat>(new_node);
123     ASSERT_NE(node_cast, nullptr);
124
125     ASSERT_TRUE(nullptr != new_node);
126     ASSERT_TRUE(new_args == new_node->input_values());
127     ASSERT_TRUE(node_cast->get_concatenation_axis() == axis);
128 }
129
130 TEST(copy, constant)
131 {
132     Shape shape{};
133     vector<float> c{2.4f};
134     auto& et = element::f32;
135     auto node = op::Constant::create(et, shape, c);
136     auto new_node = node->clone_with_new_inputs(OutputVector{});
137     auto node_cast = as_type_ptr<op::Constant>(new_node);
138     ASSERT_NE(node_cast, nullptr);
139     ASSERT_TRUE(nullptr != new_node);
140     ASSERT_TRUE(NodeVector{} == new_node->get_arguments());
141     ASSERT_TRUE(node_cast->get_vector<float>() == c);
142     ASSERT_TRUE(node_cast->get_shape() == shape);
143     ASSERT_TRUE(node_cast->get_element_type() == et);
144 }
145
146 TEST(copy, convert)
147 {
148     Shape shape;
149     auto& et = element::f64;
150     auto arg0 = make_shared<op::Parameter>(element::f32, shape);
151     OutputVector new_args{make_shared<op::Parameter>(element::f32, shape)};
152
153     auto node = make_shared<op::Convert>(arg0, et);
154     auto new_node = node->clone_with_new_inputs(new_args);
155     auto node_cast = as_type_ptr<op::Convert>(new_node);
156     ASSERT_NE(node_cast, nullptr);
157
158     ASSERT_TRUE(nullptr != new_node);
159     ASSERT_TRUE(new_args == as_output_vector(new_node->get_arguments()));
160     ASSERT_TRUE(et == node_cast->get_convert_element_type());
161 }
162
163 TEST(copy, cos)
164 {
165     ASSERT_TRUE(check_unary<op::Cos>());
166 }
167
168 TEST(copy, cosh)
169 {
170     ASSERT_TRUE(check_unary<op::Cosh>());
171 }
172
173 TEST(copy, divide)
174 {
175     ASSERT_TRUE(check_binary<op::Divide>());
176 }
177
178 TEST(copy, dot)
179 {
180     ASSERT_TRUE(check_binary<op::Dot>());
181 }
182
183 TEST(copy, equal)
184 {
185     ASSERT_TRUE(check_binary<op::Equal>());
186 }
187
188 TEST(copy, exp)
189 {
190     ASSERT_TRUE(check_unary<op::Exp>());
191 }
192
193 TEST(copy, floor)
194 {
195     ASSERT_TRUE(check_unary<op::Floor>());
196 }
197
198 TEST(copy, greater_eq)
199 {
200     ASSERT_TRUE(check_binary<op::GreaterEq>());
201 }
202
203 TEST(copy, greater)
204 {
205     ASSERT_TRUE(check_binary<op::Greater>());
206 }
207
208 TEST(copy, less_eq)
209 {
210     ASSERT_TRUE(check_binary<op::LessEq>());
211 }
212
213 TEST(copy, less)
214 {
215     ASSERT_TRUE(check_binary<op::Less>());
216 }
217
218 TEST(copy, log)
219 {
220     ASSERT_TRUE(check_unary<op::Log>());
221 }
222
223 TEST(copy, maximum)
224 {
225     ASSERT_TRUE(check_binary<op::Maximum>());
226 }
227
228 TEST(copy, minimum)
229 {
230     ASSERT_TRUE(check_binary<op::Minimum>());
231 }
232
233 TEST(copy, multiply)
234 {
235     ASSERT_TRUE(check_binary<op::Multiply>());
236 }
237
238 TEST(copy, negative)
239 {
240     ASSERT_TRUE(check_unary<op::Negative>());
241 }
242
243 TEST(copy, not_equal)
244 {
245     ASSERT_TRUE(check_binary<op::NotEqual>());
246 }
247
248 TEST(copy, parameter)
249 {
250     Shape shape{1};
251     auto node = make_shared<op::Parameter>(element::f32, shape);
252     auto new_node = node->clone_with_new_inputs({});
253     auto node_cast = as_type_ptr<op::Parameter>(new_node);
254     ASSERT_NE(node_cast, nullptr);
255
256     ASSERT_TRUE(nullptr != new_node);
257     ASSERT_TRUE(new_node->get_arguments().size() == 0);
258     ASSERT_TRUE(node->has_same_type(new_node));
259 }
260
261 TEST(copy, power)
262 {
263     ASSERT_TRUE(check_binary<op::Power>());
264 }
265
266 TEST(copy, reshape)
267 {
268     Shape shape_in{2, 3, 4};
269     AxisVector axes{0, 1, 2};
270     Shape shape_out{6, 4};
271
272     auto arg0 = make_shared<op::Parameter>(element::f32, shape_in);
273     OutputVector new_args{make_shared<op::Parameter>(element::f32, shape_in)};
274
275     auto node = make_shared<op::Reshape>(arg0, axes, shape_out);
276     auto new_node = node->clone_with_new_inputs(new_args);
277     auto node_cast = as_type_ptr<op::Reshape>(new_node);
278     ASSERT_NE(node_cast, nullptr);
279
280     ASSERT_TRUE(nullptr != new_node);
281     ASSERT_TRUE(new_args == as_output_vector(new_node->get_arguments()));
282     ASSERT_TRUE(axes == node_cast->get_input_order());
283     ASSERT_TRUE(shape_out == node_cast->get_output_shape(0));
284 }
285
286 TEST(copy, select)
287 {
288     Shape shape{1};
289     auto arg0 = make_shared<op::Parameter>(element::boolean, shape);
290     auto arg1 = make_shared<op::Parameter>(element::f32, shape);
291     auto arg2 = make_shared<op::Parameter>(element::f32, shape);
292     OutputVector new_args{make_shared<op::Parameter>(element::boolean, shape),
293                           make_shared<op::Parameter>(element::f32, shape),
294                           make_shared<op::Parameter>(element::f32, shape)};
295
296     auto node = make_shared<op::Select>(arg0, arg1, arg2);
297     auto new_node = node->clone_with_new_inputs(new_args);
298     auto node_cast = as_type_ptr<op::Select>(new_node);
299     ASSERT_NE(node_cast, nullptr);
300
301     ASSERT_TRUE(nullptr != new_node);
302     ASSERT_TRUE(new_args == new_node->input_values());
303 }
304
305 TEST(copy, sign)
306 {
307     ASSERT_TRUE(check_unary<op::Sign>());
308 }
309
310 TEST(copy, sin)
311 {
312     ASSERT_TRUE(check_unary<op::Sin>());
313 }
314
315 TEST(copy, sinh)
316 {
317     ASSERT_TRUE(check_unary<op::Sinh>());
318 }
319
320 TEST(copy, slice)
321 {
322     Shape shape_in{2, 3, 4};
323     Coordinate lower{0, 0, 0};
324     Coordinate upper{2, 3, 4};
325     Strides strides{1, 1, 1};
326
327     auto arg0 = make_shared<op::Parameter>(element::f32, shape_in);
328     OutputVector new_args{make_shared<op::Parameter>(element::f32, shape_in)};
329
330     auto node = make_shared<op::Slice>(arg0, lower, upper, strides);
331     auto new_node = node->clone_with_new_inputs(new_args);
332     auto node_cast = as_type_ptr<op::Slice>(new_node);
333     ASSERT_NE(node_cast, nullptr);
334
335     ASSERT_TRUE(nullptr != new_node);
336     ASSERT_TRUE(new_args == as_output_vector(new_node->get_arguments()));
337     ASSERT_TRUE(lower == node_cast->get_lower_bounds());
338     ASSERT_TRUE(upper == node_cast->get_upper_bounds());
339     ASSERT_TRUE(strides == node_cast->get_strides());
340 }
341
342 TEST(copy, subtract)
343 {
344     ASSERT_TRUE(check_binary<op::Subtract>());
345 }
346
347 TEST(copy, sum)
348 {
349     Shape shape{4, 3};
350     AxisSet axes{1};
351     auto arg0 = make_shared<op::Parameter>(element::f32, shape);
352
353     auto node = make_shared<op::Sum>(arg0, axes);
354     OutputVector new_args{make_shared<op::Parameter>(element::f32, shape), node->get_argument(1)};
355     auto new_node = node->clone_with_new_inputs(new_args);
356     auto node_cast = as_type_ptr<op::Sum>(new_node);
357     ASSERT_NE(node_cast, nullptr);
358
359     ASSERT_TRUE(nullptr != new_node);
360     ASSERT_TRUE(new_args == as_output_vector(new_node->get_arguments()));
361     ASSERT_TRUE(axes == node_cast->get_reduction_axes());
362 }
363
364 TEST(copy, tan)
365 {
366     ASSERT_TRUE(check_unary<op::Tan>());
367 }
368
369 TEST(copy, tanh)
370 {
371     ASSERT_TRUE(check_unary<op::Tanh>());
372 }