This repository was archived by the owner on Apr 19, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathexecutable.cc
More file actions
254 lines (228 loc) · 9.76 KB
/
executable.cc
File metadata and controls
254 lines (228 loc) · 9.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
//*****************************************************************************
// Copyright 2017-2020 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//*****************************************************************************
#include "ngraph/ngraph.hpp"
#include "ngraph/opsets/opset.hpp"
#include "logging/ngraph_log.h"
#include "ngraph_bridge/default_opset.h"
#include "ngraph_bridge/executable.h"
#include "ngraph_bridge/ie_tensor.h"
using namespace std;
using namespace ngraph;
namespace tensorflow {
namespace ngraph_bridge {
Executable::Executable(shared_ptr<Function> func, string device)
: m_device{device}, m_trivial_fn{nullptr}, m_function(func) {
NGRAPH_VLOG(2) << "Checking for unsupported ops in IE backend";
const auto& opset = ngraph::get_opset3();
for (const auto& node : func->get_ops()) {
if (!opset.contains_op_type(node.get())) {
NGRAPH_VLOG(0) << "UNSUPPORTED OP DETECTED: "
<< node->get_type_info().name;
THROW_IE_EXCEPTION << "Detected op not belonging to opset3!";
}
}
// A trivial function is one of
// 1. constant function (Const -> Result)
// 2. identity function (Parameter -> Result)
// 3. zero function (* -> Zero)
NGRAPH_VLOG(2) << "Checking for trivial functions in IE backend";
bool trivial_fn = true;
for (auto result : func->get_results()) {
auto parent = result->input_value(0).get_node_shared_ptr();
ngraph::Shape shape = {1};
if (result->get_output_partial_shape(0).is_static()) {
shape = result->get_shape();
}
trivial_fn &= ngraph::is_type<opset::Parameter>(parent) ||
ngraph::is_type<opset::Constant>(parent) ||
count(shape.begin(), shape.end(), 0);
}
if (trivial_fn) {
NGRAPH_VLOG(2) << "Function is trivial and can be short-circuited";
m_trivial_fn = func;
return;
}
NGRAPH_VLOG(2) << "Checking for function parameters in IE backend";
if (func->get_parameters().size() == 0) {
NGRAPH_VLOG(1) << "No parameters found in nGraph function!";
// Try to find a node that can be converted into a "static input"
bool param_replaced = false;
for (const auto& node : func->get_ordered_ops()) {
// Only try to convert constant nodes at the edge to parameters
// FIXME: IE cannot handle input parameters with i64/u6 precision
// at the moment
if (node->get_input_size() == 0 && ngraph::op::is_constant(node) &&
!(node->get_element_type() == ngraph::element::i64 ||
node->get_element_type() == ngraph::element::u64)) {
auto constant = ngraph::as_type_ptr<opset::Constant>(node);
auto element_type = constant->get_element_type();
auto shape = constant->get_shape();
auto param = std::make_shared<opset::Parameter>(element_type, shape);
param->set_friendly_name(node->get_friendly_name());
ngraph::replace_node(node, param);
// nGraph doesn't provide a way to set a parameter to an existing
// function, so we clone the function here...
func = make_shared<Function>(func->get_results(),
ParameterVector{param}, func->get_name());
auto ie_tensor = make_shared<IETensor>(element_type, shape);
ie_tensor->write(constant->get_data_ptr(),
shape_size(shape) * element_type.size());
m_hoisted_params.push_back(
make_pair(param->get_friendly_name(), ie_tensor));
NGRAPH_VLOG(1) << "Converted node " << constant << " to a parameter "
<< param;
param_replaced = true;
break;
}
}
if (!param_replaced) {
THROW_IE_EXCEPTION
<< "Unable to add a parameter to a function with no parameters!";
}
}
m_function = func;
NGRAPH_VLOG(2) << "Creating IE CNN network using nGraph function";
m_network = InferenceEngine::CNNNetwork(func);
if (std::getenv("NGRAPH_TF_DUMP_GRAPHS")) {
auto& name = m_network.getName();
m_network.serialize(name + ".xml", name + ".bin");
ngraph::plot_graph(func, "tf_function_" + name + "_ie.dot");
}
NGRAPH_VLOG(2) << "Loading IE CNN network to device " << m_device;
InferenceEngine::Core ie;
// Load network to the plugin (m_device) and create an infer request
InferenceEngine::ExecutableNetwork exe_network =
ie.LoadNetwork(m_network, m_device);
m_infer_req = exe_network.CreateInferRequest();
}
bool Executable::call(const vector<shared_ptr<runtime::Tensor>>& inputs,
vector<shared_ptr<runtime::Tensor>>& outputs) {
if (m_trivial_fn) {
NGRAPH_VLOG(2) << "Calling trivial IE function with inputs="
<< inputs.size() << " outputs=" << outputs.size();
return call_trivial(inputs, outputs);
}
// Check if the number of inputs that the CNN network expects is equal to the
// sum of the
// inputs specified and the inputs we hoisted, if any.
InferenceEngine::InputsDataMap input_info = m_network.getInputsInfo();
if (input_info.size() != (inputs.size() + m_hoisted_params.size())) {
THROW_IE_EXCEPTION
<< "Function inputs number differ from number of given inputs";
}
// Prepare input blobs
auto func = m_network.getFunction();
auto parameters = func->get_parameters();
for (int i = 0; i < inputs.size(); i++) {
shared_ptr<IETensor> tv = static_pointer_cast<IETensor>(inputs[i]);
m_infer_req.SetBlob(parameters[i]->get_friendly_name(), tv->get_blob());
}
for (const auto& it : m_hoisted_params) {
shared_ptr<IETensor> tv = static_pointer_cast<IETensor>(it.second);
m_infer_req.SetBlob(it.first, tv->get_blob());
}
InferenceEngine::OutputsDataMap output_info = m_network.getOutputsInfo();
if (outputs.size() == 0 && output_info.size() > 0) {
outputs.resize(output_info.size(), nullptr);
}
auto get_output_name = [](std::shared_ptr<ngraph::Node> node) {
// Since IE has no "result" nodes, we set the blob corresponding to the
// parent of this result node
auto parent = node->input_value(0).get_node_shared_ptr();
auto name = parent->get_friendly_name();
// if parent has multiple outputs, correctly identify the output feeding
// into this result
if (parent->outputs().size() > 1) {
name += "." + to_string(node->input_value(0).get_index());
}
return name;
};
// Prepare output blobs
auto results = func->get_results();
for (int i = 0; i < results.size(); i++) {
if (outputs[i] != nullptr) {
NGRAPH_VLOG(4) << "Executable::call() SetBlob()";
shared_ptr<IETensor> tv = static_pointer_cast<IETensor>(outputs[i]);
m_infer_req.SetBlob(get_output_name(results[i]), tv->get_blob());
}
}
m_infer_req.Infer();
// Set dynamic output blobs
for (int i = 0; i < results.size(); i++) {
if (outputs[i] == nullptr) {
NGRAPH_VLOG(4) << "Executable::call() GetBlob()";
auto blob = m_infer_req.GetBlob(get_output_name(results[i]));
outputs[i] = make_shared<IETensor>(blob);
}
}
return true;
}
bool Executable::call_trivial(const vector<shared_ptr<runtime::Tensor>>& inputs,
vector<shared_ptr<runtime::Tensor>>& outputs) {
// outputs are in the same order as results
auto results = m_trivial_fn->get_results();
if (outputs.size() == 0 && results.size() > 0) {
outputs.resize(results.size(), nullptr);
}
for (int i = 0; i < results.size(); i++) {
auto& shape = results[i]->get_shape();
if (count(shape.begin(), shape.end(), 0)) {
if (outputs[i] == nullptr) {
outputs[i] =
make_shared<IETensor>(results[i]->get_element_type(), shape);
}
NGRAPH_VLOG(2) << "Skipping function with zero dim result...";
continue;
}
auto parent = results[i]->input_value(0).get_node_shared_ptr();
if (ngraph::is_type<opset::Parameter>(parent)) {
NGRAPH_VLOG(2) << "Calling parameter -> result function...";
auto param = ngraph::as_type_ptr<opset::Parameter>(parent);
auto index = m_trivial_fn->get_parameter_index(param);
if (index < 0) {
THROW_IE_EXCEPTION << "Input parameter " << param->get_friendly_name()
<< " not found in trivial function";
}
if (outputs[i] == nullptr) {
outputs[i] = make_shared<IETensor>(inputs[index]->get_element_type(),
inputs[index]->get_shape());
}
auto size = inputs[index]->get_size_in_bytes();
unsigned char* buf_ptr = new unsigned char[size];
inputs[index]->read(buf_ptr, size);
outputs[i]->write(buf_ptr, size);
delete buf_ptr;
} else if (ngraph::is_type<opset::Constant>(parent)) {
NGRAPH_VLOG(2) << "Calling constant -> result function...";
auto constant = ngraph::as_type_ptr<opset::Constant>(parent);
if (outputs[i] == nullptr) {
outputs[i] = make_shared<IETensor>(
constant->get_element_type(), constant->get_shape(),
const_cast<void*>(constant->get_data_ptr()));
} else {
outputs[i]->write(constant->get_data_ptr(),
shape_size(constant->get_shape()) *
constant->get_element_type().size());
}
} else {
THROW_IE_EXCEPTION << "Expected constant or parameter feeding to a "
"result in trivial function";
}
}
return true;
}
}
}