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 //*****************************************************************************
20 #include "gtest/gtest.h"
22 #include "ngraph/ngraph.hpp"
23 #include "util/ndarray.hpp"
24 #include "util/test_tools.hpp"
27 using namespace ngraph;
29 template <typename OP>
33 auto arg0 = make_shared<op::Parameter>(element::f32, shape);
34 OutputVector new_args{make_shared<op::Parameter>(element::f32, shape)};
36 auto node = make_shared<OP>(arg0);
37 auto new_node = node->copy_with_new_inputs(new_args);
39 return (nullptr != new_node) && (new_args == new_node->input_values());
42 template <typename OP>
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)};
51 auto node = make_shared<OP>(arg0, arg1);
52 auto new_node = node->copy_with_new_inputs(new_args);
54 return (nullptr != new_node) && (new_args == new_node->input_values());
59 ASSERT_TRUE(check_unary<op::Abs>());
64 ASSERT_TRUE(check_unary<op::Acos>());
69 ASSERT_TRUE(check_binary<op::Add>());
74 ASSERT_TRUE(check_unary<op::Asin>());
79 ASSERT_TRUE(check_unary<op::Atan>());
85 auto arg0 = make_shared<op::Parameter>(element::f32, shape1);
86 OutputVector new_args{make_shared<op::Parameter>(element::f32, shape1)};
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);
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());
104 ASSERT_TRUE(check_unary<op::Ceiling>());
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)};
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);
120 ASSERT_TRUE(nullptr != new_node);
121 ASSERT_TRUE(new_args == new_node->input_values());
122 ASSERT_TRUE(node_cast->get_concatenation_axis() == axis);
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);
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)};
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);
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());
160 ASSERT_TRUE(check_unary<op::Cos>());
165 ASSERT_TRUE(check_unary<op::Cosh>());
170 ASSERT_TRUE(check_binary<op::Divide>());
175 ASSERT_TRUE(check_binary<op::Dot>());
180 ASSERT_TRUE(check_binary<op::Equal>());
185 ASSERT_TRUE(check_unary<op::Exp>());
190 ASSERT_TRUE(check_unary<op::Floor>());
193 TEST(copy, greater_eq)
195 ASSERT_TRUE(check_binary<op::GreaterEq>());
200 ASSERT_TRUE(check_binary<op::Greater>());
205 ASSERT_TRUE(check_binary<op::LessEq>());
210 ASSERT_TRUE(check_binary<op::Less>());
215 ASSERT_TRUE(check_unary<op::Log>());
220 ASSERT_TRUE(check_binary<op::Maximum>());
225 ASSERT_TRUE(check_binary<op::Minimum>());
230 ASSERT_TRUE(check_binary<op::Multiply>());
235 ASSERT_TRUE(check_unary<op::Negative>());
238 TEST(copy, not_equal)
240 ASSERT_TRUE(check_binary<op::NotEqual>());
243 TEST(copy, parameter)
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);
251 ASSERT_TRUE(nullptr != new_node);
252 ASSERT_TRUE(new_node->input_values().size() == 0);
253 ASSERT_TRUE(node->has_same_type(new_node));
258 ASSERT_TRUE(check_binary<op::Power>());
263 Shape shape_in{2, 3, 4};
264 AxisVector axes{0, 1, 2};
265 Shape shape_out{6, 4};
267 auto arg0 = make_shared<op::Parameter>(element::f32, shape_in);
268 OutputVector new_args{make_shared<op::Parameter>(element::f32, shape_in)};
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);
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));
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)};
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);
296 ASSERT_TRUE(nullptr != new_node);
297 ASSERT_TRUE(new_args == new_node->input_values());
302 ASSERT_TRUE(check_unary<op::Sign>());
307 ASSERT_TRUE(check_unary<op::Sin>());
312 ASSERT_TRUE(check_unary<op::Sinh>());
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};
322 auto arg0 = make_shared<op::Parameter>(element::f32, shape_in);
323 OutputVector new_args{make_shared<op::Parameter>(element::f32, shape_in)};
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);
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());
339 ASSERT_TRUE(check_binary<op::Subtract>());
346 auto arg0 = make_shared<op::Parameter>(element::f32, shape);
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);
355 ASSERT_TRUE(nullptr != new_node);
356 ASSERT_TRUE(new_args == new_node->input_values());
357 ASSERT_TRUE(axes == node_cast->get_reduction_axes());
362 ASSERT_TRUE(check_unary<op::Tan>());
367 ASSERT_TRUE(check_unary<op::Tanh>());