Deprecate nGraph v0 ops and builders (#1856)
[platform/upstream/dldt.git] / ngraph / test / util / test_tools.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 <algorithm>
18
19 #include "ngraph/ngraph.hpp"
20 #include "ngraph/util.hpp"
21 #include "test_tools.hpp"
22
23 NGRAPH_SUPPRESS_DEPRECATED_START
24
25 using namespace std;
26 using namespace ngraph;
27
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)
31 {
32     bool rc = true;
33     for (auto it = nodes.rbegin(); it != nodes.rend(); it++)
34     {
35         auto node_tmp = *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;
40
41         for (shared_ptr<Node> n : dependencies_tmp)
42         {
43             dependencies.push_back(n.get());
44         }
45         auto tmp = it;
46         for (tmp++; tmp != nodes.rend(); tmp++)
47         {
48             auto dep_tmp = *tmp;
49             auto found = find(dependencies.begin(), dependencies.end(), dep_tmp.get());
50             if (found != dependencies.end())
51             {
52                 dependencies.erase(found);
53             }
54         }
55         if (dependencies.size() > 0)
56         {
57             rc = false;
58         }
59     }
60     return rc;
61 }
62
63 shared_ptr<Function> make_test_graph()
64 {
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{});
71
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);
75
76     auto t3 = make_shared<op::Add>(t1, arg_4);
77     auto t4 = make_shared<op::Add>(t2, arg_5);
78
79     auto r0 = make_shared<op::Add>(t3, t4);
80
81     auto f0 = make_shared<Function>(r0, ParameterVector{arg_0, arg_1, arg_2, arg_3, arg_4, arg_5});
82
83     return f0;
84 }
85
86 template <>
87 void copy_data<bool>(std::shared_ptr<ngraph::runtime::Tensor> tv, const std::vector<bool>& data)
88 {
89     std::vector<char> data_char(data.begin(), data.end());
90     copy_data(tv, data_char);
91 }
92
93 template <>
94 void init_int_tv<char>(ngraph::runtime::Tensor* tv,
95                        std::default_random_engine& engine,
96                        char min,
97                        char max)
98 {
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)
103     {
104         element = static_cast<char>(dist(engine));
105     }
106     tv->write(vec.data(), vec.size() * sizeof(char));
107 }
108
109 template <>
110 void init_int_tv<int8_t>(ngraph::runtime::Tensor* tv,
111                          std::default_random_engine& engine,
112                          int8_t min,
113                          int8_t max)
114 {
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)
119     {
120         element = static_cast<int8_t>(dist(engine));
121     }
122     tv->write(vec.data(), vec.size() * sizeof(int8_t));
123 }
124
125 template <>
126 void init_int_tv<uint8_t>(ngraph::runtime::Tensor* tv,
127                           std::default_random_engine& engine,
128                           uint8_t min,
129                           uint8_t max)
130 {
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)
135     {
136         element = static_cast<uint8_t>(dist(engine));
137     }
138     tv->write(vec.data(), vec.size() * sizeof(uint8_t));
139 }
140
141 void random_init(ngraph::runtime::Tensor* tv, std::default_random_engine& engine)
142 {
143     element::Type et = tv->get_element_type();
144     if (et == element::boolean)
145     {
146         init_int_tv<char>(tv, engine, 0, 1);
147     }
148     else if (et == element::f32)
149     {
150         init_real_tv<float>(tv, engine, numeric_limits<float>::min(), 1.0f);
151     }
152     else if (et == element::f64)
153     {
154         init_real_tv<double>(tv, engine, numeric_limits<double>::min(), 1.0);
155     }
156     else if (et == element::i8)
157     {
158         init_int_tv<int8_t>(tv, engine, -1, 1);
159     }
160     else if (et == element::i16)
161     {
162         init_int_tv<int16_t>(tv, engine, -1, 1);
163     }
164     else if (et == element::i32)
165     {
166         init_int_tv<int32_t>(tv, engine, 0, 1);
167     }
168     else if (et == element::i64)
169     {
170         init_int_tv<int64_t>(tv, engine, 0, 1);
171     }
172     else if (et == element::u8)
173     {
174         init_int_tv<uint8_t>(tv, engine, 0, 1);
175     }
176     else if (et == element::u16)
177     {
178         init_int_tv<uint16_t>(tv, engine, 0, 1);
179     }
180     else if (et == element::u32)
181     {
182         init_int_tv<uint32_t>(tv, engine, 0, 1);
183     }
184     else if (et == element::u64)
185     {
186         init_int_tv<uint64_t>(tv, engine, 0, 1);
187     }
188     else
189     {
190         throw runtime_error("unsupported type");
191     }
192 }
193
194 template <>
195 string get_results_str(const std::vector<char>& ref_data,
196                        const std::vector<char>& actual_data,
197                        size_t max_results)
198 {
199     stringstream ss;
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)
203     {
204         ss << std::endl
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]);
208     }
209     ss << std::endl;
210
211     return ss.str();
212 }
213
214 ::testing::AssertionResult test_ordered_ops(shared_ptr<Function> f, const NodeVector& required_ops)
215 {
216     unordered_set<Node*> seen;
217     for (auto& node_ptr : f->get_ordered_ops())
218     {
219         Node* node = node_ptr.get();
220         if (seen.count(node) > 0)
221         {
222             return ::testing::AssertionFailure() << "Duplication in ordered ops";
223         }
224         size_t arg_count = node->get_input_size();
225         for (size_t i = 0; i < arg_count; ++i)
226         {
227             Node* dep = node->get_input_node_ptr(i);
228             if (seen.count(dep) == 0)
229             {
230                 return ::testing::AssertionFailure() << "Argument " << *dep
231                                                      << " does not occur before op" << *node;
232             }
233         }
234         for (auto& dep_ptr : node->get_control_dependencies())
235         {
236             if (seen.count(dep_ptr.get()) == 0)
237             {
238                 return ::testing::AssertionFailure() << "Control dependency " << *dep_ptr
239                                                      << " does not occur before op" << *node;
240             }
241         }
242         seen.insert(node);
243     }
244     for (auto& node_ptr : required_ops)
245     {
246         if (seen.count(node_ptr.get()) == 0)
247         {
248             return ::testing::AssertionFailure() << "Required op " << *node_ptr
249                                                  << "does not occur in ordered ops";
250         }
251     }
252     return ::testing::AssertionSuccess();
253 }
254
255 constexpr NodeTypeInfo ngraph::TestOpMultiOut::type_info;
256
257 bool ngraph::TestOpMultiOut::evaluate(const HostTensorVector& outputs,
258                                       const HostTensorVector& inputs) const
259 {
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());
262     return true;
263 }