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