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 //*****************************************************************************
19 #include "ngraph/ngraph.hpp"
20 #include "ngraph/util.hpp"
21 #include "test_tools.hpp"
23 NGRAPH_SUPPRESS_DEPRECATED_START
26 using namespace ngraph;
28 // This function traverses the vector of ops and verifies that each op's dependencies (its inputs)
29 // is located earlier in the vector. That is enough to be valid
30 bool validate_list(const vector<shared_ptr<Node>>& nodes)
33 for (auto it = nodes.rbegin(); it != nodes.rend(); it++)
36 NodeVector dependencies_tmp;
37 for (auto& val : node_tmp->input_values())
38 dependencies_tmp.emplace_back(val.get_node_shared_ptr());
39 vector<Node*> dependencies;
41 for (shared_ptr<Node> n : dependencies_tmp)
43 dependencies.push_back(n.get());
46 for (tmp++; tmp != nodes.rend(); tmp++)
49 auto found = find(dependencies.begin(), dependencies.end(), dep_tmp.get());
50 if (found != dependencies.end())
52 dependencies.erase(found);
55 if (dependencies.size() > 0)
63 shared_ptr<Function> make_test_graph()
65 auto arg_0 = make_shared<op::Parameter>(element::f32, Shape{});
66 auto arg_1 = make_shared<op::Parameter>(element::f32, Shape{});
67 auto arg_2 = make_shared<op::Parameter>(element::f32, Shape{});
68 auto arg_3 = make_shared<op::Parameter>(element::f32, Shape{});
69 auto arg_4 = make_shared<op::Parameter>(element::f32, Shape{});
70 auto arg_5 = make_shared<op::Parameter>(element::f32, Shape{});
72 auto t0 = make_shared<op::Add>(arg_0, arg_1);
73 auto t1 = make_shared<op::Dot>(t0, arg_2);
74 auto t2 = make_shared<op::Multiply>(t0, arg_3);
76 auto t3 = make_shared<op::Add>(t1, arg_4);
77 auto t4 = make_shared<op::Add>(t2, arg_5);
79 auto r0 = make_shared<op::Add>(t3, t4);
81 auto f0 = make_shared<Function>(r0, ParameterVector{arg_0, arg_1, arg_2, arg_3, arg_4, arg_5});
87 void copy_data<bool>(std::shared_ptr<ngraph::runtime::Tensor> tv, const std::vector<bool>& data)
89 std::vector<char> data_char(data.begin(), data.end());
90 copy_data(tv, data_char);
94 void init_int_tv<char>(ngraph::runtime::Tensor* tv,
95 std::default_random_engine& engine,
99 size_t size = tv->get_element_count();
100 std::uniform_int_distribution<int16_t> dist(static_cast<short>(min), static_cast<short>(max));
101 std::vector<char> vec(size);
102 for (char& element : vec)
104 element = static_cast<char>(dist(engine));
106 tv->write(vec.data(), vec.size() * sizeof(char));
110 void init_int_tv<int8_t>(ngraph::runtime::Tensor* tv,
111 std::default_random_engine& engine,
115 size_t size = tv->get_element_count();
116 std::uniform_int_distribution<int16_t> dist(static_cast<short>(min), static_cast<short>(max));
117 std::vector<int8_t> vec(size);
118 for (int8_t& element : vec)
120 element = static_cast<int8_t>(dist(engine));
122 tv->write(vec.data(), vec.size() * sizeof(int8_t));
126 void init_int_tv<uint8_t>(ngraph::runtime::Tensor* tv,
127 std::default_random_engine& engine,
131 size_t size = tv->get_element_count();
132 std::uniform_int_distribution<int16_t> dist(static_cast<short>(min), static_cast<short>(max));
133 std::vector<uint8_t> vec(size);
134 for (uint8_t& element : vec)
136 element = static_cast<uint8_t>(dist(engine));
138 tv->write(vec.data(), vec.size() * sizeof(uint8_t));
141 void random_init(ngraph::runtime::Tensor* tv, std::default_random_engine& engine)
143 element::Type et = tv->get_element_type();
144 if (et == element::boolean)
146 init_int_tv<char>(tv, engine, 0, 1);
148 else if (et == element::f32)
150 init_real_tv<float>(tv, engine, numeric_limits<float>::min(), 1.0f);
152 else if (et == element::f64)
154 init_real_tv<double>(tv, engine, numeric_limits<double>::min(), 1.0);
156 else if (et == element::i8)
158 init_int_tv<int8_t>(tv, engine, -1, 1);
160 else if (et == element::i16)
162 init_int_tv<int16_t>(tv, engine, -1, 1);
164 else if (et == element::i32)
166 init_int_tv<int32_t>(tv, engine, 0, 1);
168 else if (et == element::i64)
170 init_int_tv<int64_t>(tv, engine, 0, 1);
172 else if (et == element::u8)
174 init_int_tv<uint8_t>(tv, engine, 0, 1);
176 else if (et == element::u16)
178 init_int_tv<uint16_t>(tv, engine, 0, 1);
180 else if (et == element::u32)
182 init_int_tv<uint32_t>(tv, engine, 0, 1);
184 else if (et == element::u64)
186 init_int_tv<uint64_t>(tv, engine, 0, 1);
190 throw runtime_error("unsupported type");
195 string get_results_str(const std::vector<char>& ref_data,
196 const std::vector<char>& actual_data,
200 size_t num_results = std::min(static_cast<size_t>(max_results), ref_data.size());
201 ss << "First " << num_results << " results";
202 for (size_t i = 0; i < num_results; ++i)
205 << std::setw(4) << i << " ref: " << std::setw(16) << std::left
206 << static_cast<int>(ref_data[i]) << " actual: " << std::setw(16) << std::left
207 << static_cast<int>(actual_data[i]);
214 ::testing::AssertionResult test_ordered_ops(shared_ptr<Function> f, const NodeVector& required_ops)
216 unordered_set<Node*> seen;
217 for (auto& node_ptr : f->get_ordered_ops())
219 Node* node = node_ptr.get();
220 if (seen.count(node) > 0)
222 return ::testing::AssertionFailure() << "Duplication in ordered ops";
224 size_t arg_count = node->get_input_size();
225 for (size_t i = 0; i < arg_count; ++i)
227 Node* dep = node->get_input_node_ptr(i);
228 if (seen.count(dep) == 0)
230 return ::testing::AssertionFailure() << "Argument " << *dep
231 << " does not occur before op" << *node;
234 for (auto& dep_ptr : node->get_control_dependencies())
236 if (seen.count(dep_ptr.get()) == 0)
238 return ::testing::AssertionFailure() << "Control dependency " << *dep_ptr
239 << " does not occur before op" << *node;
244 for (auto& node_ptr : required_ops)
246 if (seen.count(node_ptr.get()) == 0)
248 return ::testing::AssertionFailure() << "Required op " << *node_ptr
249 << "does not occur in ordered ops";
252 return ::testing::AssertionSuccess();
255 constexpr NodeTypeInfo ngraph::TestOpMultiOut::type_info;
257 bool ngraph::TestOpMultiOut::evaluate(const HostTensorVector& outputs,
258 const HostTensorVector& inputs) const
260 inputs[0]->read(outputs[0]->get_data_ptr(), inputs[0]->get_size_in_bytes());
261 inputs[1]->read(outputs[1]->get_data_ptr(), inputs[1]->get_size_in_bytes());