Remove get_arguments (#1323)
[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, broadcast)
83 {
84     Shape shape1{1};
85     auto arg0 = make_shared<op::Parameter>(element::f32, shape1);
86     OutputVector new_args{make_shared<op::Parameter>(element::f32, shape1)};
87
88     Shape shape{4, 1, 3};
89     AxisSet axes{0, 2};
90
91     auto node = make_shared<op::Broadcast>(arg0, shape, axes);
92     auto new_node = node->copy_with_new_inputs(new_args);
93     auto node_cast = as_type_ptr<op::Broadcast>(new_node);
94     ASSERT_NE(node_cast, nullptr);
95
96     ASSERT_TRUE(nullptr != new_node);
97     ASSERT_TRUE(new_args == new_node->input_values());
98     ASSERT_TRUE(shape == node_cast->get_broadcast_shape());
99     ASSERT_TRUE(axes == node_cast->get_broadcast_axes());
100 }
101
102 TEST(copy, ceiling)
103 {
104     ASSERT_TRUE(check_unary<op::Ceiling>());
105 }
106
107 TEST(copy, concat)
108 {
109     Shape shape{1};
110     auto arg0 = make_shared<op::Parameter>(element::f32, shape);
111     auto arg1 = make_shared<op::Parameter>(element::f32, shape);
112     OutputVector new_args{make_shared<op::Parameter>(element::f32, shape),
113                           make_shared<op::Parameter>(element::f32, shape)};
114     size_t axis = 0;
115     auto node = make_shared<op::Concat>(NodeVector{arg0, arg1}, axis);
116     auto new_node = node->clone_with_new_inputs(new_args);
117     auto node_cast = as_type_ptr<op::Concat>(new_node);
118     ASSERT_NE(node_cast, nullptr);
119
120     ASSERT_TRUE(nullptr != new_node);
121     ASSERT_TRUE(new_args == new_node->input_values());
122     ASSERT_TRUE(node_cast->get_concatenation_axis() == axis);
123 }
124
125 TEST(copy, constant)
126 {
127     Shape shape{};
128     vector<float> c{2.4f};
129     auto& et = element::f32;
130     auto node = op::Constant::create(et, shape, c);
131     auto new_node = node->clone_with_new_inputs(OutputVector{});
132     auto node_cast = as_type_ptr<op::Constant>(new_node);
133     ASSERT_NE(node_cast, nullptr);
134     ASSERT_TRUE(nullptr != new_node);
135     ASSERT_TRUE(OutputVector{} == new_node->input_values());
136     ASSERT_TRUE(node_cast->get_vector<float>() == c);
137     ASSERT_TRUE(node_cast->get_shape() == shape);
138     ASSERT_TRUE(node_cast->get_element_type() == et);
139 }
140
141 TEST(copy, convert)
142 {
143     Shape shape;
144     auto& et = element::f64;
145     auto arg0 = make_shared<op::Parameter>(element::f32, shape);
146     OutputVector new_args{make_shared<op::Parameter>(element::f32, shape)};
147
148     auto node = make_shared<op::Convert>(arg0, et);
149     auto new_node = node->clone_with_new_inputs(new_args);
150     auto node_cast = as_type_ptr<op::Convert>(new_node);
151     ASSERT_NE(node_cast, nullptr);
152
153     ASSERT_TRUE(nullptr != new_node);
154     ASSERT_TRUE(new_args == new_node->input_values());
155     ASSERT_TRUE(et == node_cast->get_convert_element_type());
156 }
157
158 TEST(copy, cos)
159 {
160     ASSERT_TRUE(check_unary<op::Cos>());
161 }
162
163 TEST(copy, cosh)
164 {
165     ASSERT_TRUE(check_unary<op::Cosh>());
166 }
167
168 TEST(copy, divide)
169 {
170     ASSERT_TRUE(check_binary<op::Divide>());
171 }
172
173 TEST(copy, dot)
174 {
175     ASSERT_TRUE(check_binary<op::Dot>());
176 }
177
178 TEST(copy, equal)
179 {
180     ASSERT_TRUE(check_binary<op::Equal>());
181 }
182
183 TEST(copy, exp)
184 {
185     ASSERT_TRUE(check_unary<op::Exp>());
186 }
187
188 TEST(copy, floor)
189 {
190     ASSERT_TRUE(check_unary<op::Floor>());
191 }
192
193 TEST(copy, greater_eq)
194 {
195     ASSERT_TRUE(check_binary<op::GreaterEq>());
196 }
197
198 TEST(copy, greater)
199 {
200     ASSERT_TRUE(check_binary<op::Greater>());
201 }
202
203 TEST(copy, less_eq)
204 {
205     ASSERT_TRUE(check_binary<op::LessEq>());
206 }
207
208 TEST(copy, less)
209 {
210     ASSERT_TRUE(check_binary<op::Less>());
211 }
212
213 TEST(copy, log)
214 {
215     ASSERT_TRUE(check_unary<op::Log>());
216 }
217
218 TEST(copy, maximum)
219 {
220     ASSERT_TRUE(check_binary<op::Maximum>());
221 }
222
223 TEST(copy, minimum)
224 {
225     ASSERT_TRUE(check_binary<op::Minimum>());
226 }
227
228 TEST(copy, multiply)
229 {
230     ASSERT_TRUE(check_binary<op::Multiply>());
231 }
232
233 TEST(copy, negative)
234 {
235     ASSERT_TRUE(check_unary<op::Negative>());
236 }
237
238 TEST(copy, not_equal)
239 {
240     ASSERT_TRUE(check_binary<op::NotEqual>());
241 }
242
243 TEST(copy, parameter)
244 {
245     Shape shape{1};
246     auto node = make_shared<op::Parameter>(element::f32, shape);
247     auto new_node = node->clone_with_new_inputs({});
248     auto node_cast = as_type_ptr<op::Parameter>(new_node);
249     ASSERT_NE(node_cast, nullptr);
250
251     ASSERT_TRUE(nullptr != new_node);
252     ASSERT_TRUE(new_node->input_values().size() == 0);
253     ASSERT_TRUE(node->has_same_type(new_node));
254 }
255
256 TEST(copy, power)
257 {
258     ASSERT_TRUE(check_binary<op::Power>());
259 }
260
261 TEST(copy, reshape)
262 {
263     Shape shape_in{2, 3, 4};
264     AxisVector axes{0, 1, 2};
265     Shape shape_out{6, 4};
266
267     auto arg0 = make_shared<op::Parameter>(element::f32, shape_in);
268     OutputVector new_args{make_shared<op::Parameter>(element::f32, shape_in)};
269
270     auto node = make_shared<op::Reshape>(arg0, axes, shape_out);
271     auto new_node = node->clone_with_new_inputs(new_args);
272     auto node_cast = as_type_ptr<op::Reshape>(new_node);
273     ASSERT_NE(node_cast, nullptr);
274
275     ASSERT_TRUE(nullptr != new_node);
276     ASSERT_TRUE(new_args == new_node->input_values());
277     ASSERT_TRUE(axes == node_cast->get_input_order());
278     ASSERT_TRUE(shape_out == node_cast->get_output_shape(0));
279 }
280
281 TEST(copy, select)
282 {
283     Shape shape{1};
284     auto arg0 = make_shared<op::Parameter>(element::boolean, shape);
285     auto arg1 = make_shared<op::Parameter>(element::f32, shape);
286     auto arg2 = make_shared<op::Parameter>(element::f32, shape);
287     OutputVector new_args{make_shared<op::Parameter>(element::boolean, shape),
288                           make_shared<op::Parameter>(element::f32, shape),
289                           make_shared<op::Parameter>(element::f32, shape)};
290
291     auto node = make_shared<op::Select>(arg0, arg1, arg2);
292     auto new_node = node->clone_with_new_inputs(new_args);
293     auto node_cast = as_type_ptr<op::Select>(new_node);
294     ASSERT_NE(node_cast, nullptr);
295
296     ASSERT_TRUE(nullptr != new_node);
297     ASSERT_TRUE(new_args == new_node->input_values());
298 }
299
300 TEST(copy, sign)
301 {
302     ASSERT_TRUE(check_unary<op::Sign>());
303 }
304
305 TEST(copy, sin)
306 {
307     ASSERT_TRUE(check_unary<op::Sin>());
308 }
309
310 TEST(copy, sinh)
311 {
312     ASSERT_TRUE(check_unary<op::Sinh>());
313 }
314
315 TEST(copy, slice)
316 {
317     Shape shape_in{2, 3, 4};
318     Coordinate lower{0, 0, 0};
319     Coordinate upper{2, 3, 4};
320     Strides strides{1, 1, 1};
321
322     auto arg0 = make_shared<op::Parameter>(element::f32, shape_in);
323     OutputVector new_args{make_shared<op::Parameter>(element::f32, shape_in)};
324
325     auto node = make_shared<op::Slice>(arg0, lower, upper, strides);
326     auto new_node = node->clone_with_new_inputs(new_args);
327     auto node_cast = as_type_ptr<op::Slice>(new_node);
328     ASSERT_NE(node_cast, nullptr);
329
330     ASSERT_TRUE(nullptr != new_node);
331     ASSERT_TRUE(new_args == new_node->input_values());
332     ASSERT_TRUE(lower == node_cast->get_lower_bounds());
333     ASSERT_TRUE(upper == node_cast->get_upper_bounds());
334     ASSERT_TRUE(strides == node_cast->get_strides());
335 }
336
337 TEST(copy, subtract)
338 {
339     ASSERT_TRUE(check_binary<op::Subtract>());
340 }
341
342 TEST(copy, sum)
343 {
344     Shape shape{4, 3};
345     AxisSet axes{1};
346     auto arg0 = make_shared<op::Parameter>(element::f32, shape);
347
348     auto node = make_shared<op::Sum>(arg0, axes);
349     OutputVector new_args{make_shared<op::Parameter>(element::f32, shape),
350                           node->input_value(1).get_node_shared_ptr()};
351     auto new_node = node->clone_with_new_inputs(new_args);
352     auto node_cast = as_type_ptr<op::Sum>(new_node);
353     ASSERT_NE(node_cast, nullptr);
354
355     ASSERT_TRUE(nullptr != new_node);
356     ASSERT_TRUE(new_args == new_node->input_values());
357     ASSERT_TRUE(axes == node_cast->get_reduction_axes());
358 }
359
360 TEST(copy, tan)
361 {
362     ASSERT_TRUE(check_unary<op::Tan>());
363 }
364
365 TEST(copy, tanh)
366 {
367     ASSERT_TRUE(check_unary<op::Tanh>());
368 }