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"
19 #include "ngraph/builder/autobroadcast.hpp"
20 #include "ngraph/file_util.hpp"
21 #include "ngraph/ngraph.hpp"
22 #include "util/test_tools.hpp"
26 NGRAPH_SUPPRESS_DEPRECATED_START
29 using namespace ngraph;
31 TEST(build_graph, build_simple)
33 // Function with 4 parameters
34 auto arg0 = make_shared<op::Parameter>(element::f32, Shape{7, 3});
35 auto arg1 = make_shared<op::Parameter>(element::f32, Shape{3});
36 auto arg2 = make_shared<op::Parameter>(element::f32, Shape{32, 7});
37 auto arg3 = make_shared<op::Parameter>(element::f32, Shape{32, 7});
38 auto broadcast_1 = builder::opset1::make_broadcast(arg3, Shape{10, 32, 7}, AxisSet{0});
39 auto b1 = builder::opset1::make_broadcast(arg3, Shape{10, 32, 7}, AxisSet{0});
40 auto dot = make_shared<op::Dot>(arg2, arg0);
41 ASSERT_EQ(dot->input_value(0).get_node_shared_ptr(), arg2);
42 ASSERT_EQ(dot->input_value(1).get_node_shared_ptr(), arg0);
44 auto cluster_0 = make_shared<Function>(dot, ParameterVector{arg0, arg1, arg2, arg3});
46 ASSERT_EQ(cluster_0->get_output_op(0)->input_value(0).get_node_shared_ptr(), dot);
49 // Check node comparisons
50 TEST(build_graph, node_comparison)
52 auto arg0 = make_shared<op::Parameter>(element::f32, Shape{32, 3});
53 auto arg1 = make_shared<op::Parameter>(element::f32, Shape{3});
54 auto arg2 = make_shared<op::Parameter>(element::f32, Shape{32});
56 auto dot = make_shared<op::Dot>(arg0, arg1);
57 auto add = make_shared<op::Add>(dot, arg2);
59 auto parg = make_shared<op::Parameter>(element::f32, Shape{});
60 auto pattern_dot = make_shared<op::Dot>(parg, parg);
63 TEST(build_graph, literal)
65 // float scalar from a float
66 // auto float0 = FloatConstant::make(3.0);
67 vector<float> float_t{3.0};
68 auto float0 = make_shared<op::Constant>(element::f32, Shape{}, float_t);
69 ASSERT_EQ(float0->get_vector<float>(), std::vector<float>{3.0});
70 ASSERT_EQ(float0->get_element_type(), element::f32);
71 ASSERT_EQ(float0->get_shape(), Shape{});
72 auto d = make_shared<op::Dot>(float0, float0);
73 ASSERT_EQ(d->input_values().at(0).get_node_shared_ptr(), float0);
74 ASSERT_EQ(d->input_values().at(1).get_node_shared_ptr(), float0);
76 vector<int32_t> int32{3};
77 auto int32_0 = make_shared<op::Constant>(element::i32, Shape{}, int32);
78 ASSERT_EQ(int32_0->get_vector<int32_t>(), std::vector<int>{3});
79 ASSERT_EQ(int32_0->get_element_type(), element::i32);
80 ASSERT_EQ(int32_0->get_shape(), Shape{});
83 TEST(build_graph, tensor)
85 // float scalar from a float
86 // auto float0 = FloatConstant::make(3.0);
88 vector<float> float_t(shape_size(shape), 0);
89 auto float0 = make_shared<op::Constant>(element::f32, shape, float_t);
90 ASSERT_EQ(float0->get_element_type(), element::f32);
91 ASSERT_EQ(float0->get_shape(), shape);
92 auto d = make_shared<op::Add>(float0, float0);
93 ASSERT_EQ(d->input_values().at(0).get_node_shared_ptr(), float0);
94 ASSERT_EQ(d->input_values().at(1).get_node_shared_ptr(), float0);
97 vector<int32_t> idata(shape_size(ishape), 0);
98 auto int32_0 = make_shared<op::Constant>(element::i32, ishape, idata);
99 ASSERT_EQ(int32_0->get_element_type(), element::i32);
100 ASSERT_EQ(int32_0->get_shape(), ishape);
103 // Check functions with undeclared parameters
104 TEST(build_graph, function_undeclared_parameters)
106 // Function with 4 parameters
107 auto arg0 = make_shared<op::Parameter>(element::f32, Shape{7, 3});
108 auto arg1 = make_shared<op::Parameter>(element::f32, Shape{3});
109 auto arg2 = make_shared<op::Parameter>(element::f32, Shape{32, 7});
110 auto arg3 = make_shared<op::Parameter>(element::f32, Shape{32, 7});
111 auto broadcast_1 = builder::opset1::make_broadcast(arg3, Shape{10, 32, 7}, AxisSet{0});
112 auto b1 = builder::opset1::make_broadcast(arg3, Shape{10, 32, 7}, AxisSet{0});
113 auto dot = make_shared<op::Dot>(arg2, arg0);
114 ASSERT_EQ(dot->input_values()[0].get_node_shared_ptr(), arg2);
115 ASSERT_EQ(dot->input_values()[1].get_node_shared_ptr(), arg0);
118 auto f = make_shared<Function>(dot, ParameterVector{arg0, arg1, arg3});
120 // Should have thrown, so fail if it didn't
121 FAIL() << "Undeclared parameter not detected.";
123 catch (const ngraph_error& error)
125 EXPECT_EQ(error.what(), std::string("Function references undeclared parameter"));
129 FAIL() << "Function construction failed for unexpected reason";
133 // Check no-arg construction
134 TEST(build_graph, no_arg_construction)
137 // Parameters aren't converted yet
138 auto arg0 = make_shared<op::Parameter>(element::f32, Shape{7});
139 auto arg1 = make_shared<op::Parameter>(element::f32, Shape{7});
140 auto arg2 = make_shared<op::Parameter>(element::f32, Shape{7});
141 auto arg3 = make_shared<op::Parameter>(element::f32, Shape{7});
142 auto add0 = make_shared<op::Add>();
143 auto abs0 = make_shared<op::Abs>();
144 auto acos0 = make_shared<op::Acos>();
145 auto add1 = make_shared<op::Add>();
146 add0->set_argument(1, arg0);
147 add0->set_argument(0, arg1);
148 abs0->set_argument(0, add0);
149 acos0->set_argument(0, add0);
150 add1->set_argument(0, acos0);
151 add1->set_argument(1, abs0);
152 NodeVector ops{arg0, arg1, add0, abs0, acos0, add1};
153 validate_nodes_and_infer_types(ops);
154 ASSERT_EQ(add1->get_output_shape(0), Shape{7});
157 TEST(build_graph, multi_output_split_dynamic)
159 const auto data = make_shared<op::Parameter>(element::f32, PartialShape::dynamic());
160 const auto axis = op::Constant::create(element::i64, Shape{}, {1});
161 const auto split = make_shared<op::Split>(data, axis, 2);
162 auto abs = make_shared<op::Abs>(split->output(1));
163 EXPECT_TRUE(abs->get_output_partial_shape(0).same_scheme(PartialShape::dynamic()));
165 auto new_parameter = make_shared<op::Parameter>(element::f32, Shape{2, 4});
166 split->input(0).replace_source_output(new_parameter->output(0));
168 auto f = make_shared<Function>(abs, ParameterVector{new_parameter});
170 f->validate_nodes_and_infer_types();
171 EXPECT_EQ(abs->get_shape(), (Shape{2, 2}));
174 TEST(build_graph, function_revalidate_and_infer)
176 auto arg = make_shared<op::Parameter>(element::f32, Shape{2, 4, 6, 8});
177 auto pattern = op::Constant::create(element::i64, Shape{6}, {1, 3, 16, 2, 2, 2});
179 auto r = make_shared<op::v1::Reshape>(arg, pattern, true);
180 auto relu = make_shared<op::Relu>(r);
181 auto f = make_shared<Function>(relu, ParameterVector{arg});
183 EXPECT_EQ(r->get_output_element_type(0), element::f32);
184 EXPECT_EQ(r->get_output_shape(0), (Shape{1, 3, 16, 2, 2, 2}));
185 EXPECT_EQ(f->get_output_shape(0), (Shape{1, 3, 16, 2, 2, 2}));
187 auto new_pattern = op::Constant::create(element::i64, Shape{2}, {32, 12});
188 r->input(1).replace_source_output(new_pattern->output(0));
190 f->validate_nodes_and_infer_types();
191 EXPECT_EQ(r->get_output_shape(0), (Shape{32, 12}));
192 EXPECT_EQ(f->get_output_shape(0), (Shape{32, 12}));
195 TEST(build_graph, default_output_checks)
199 std::shared_ptr<Node> empty;
200 auto nullout = Output<Node>(empty);
204 FAIL() << "nullptr initialization of Output failed";