gplaidmlbackend.cpp
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
//
// Copyright (C) 2019-2020 Intel Corporation
#ifdef HAVE_PLAIDML
#include "precomp.hpp"
#include <ade/util/algorithm.hpp>
#include <ade/util/range.hpp>
#include <ade/util/zip_range.hpp>
#include <ade/typed_graph.hpp>
#include <opencv2/gapi/gcommon.hpp>
#include <opencv2/gapi/util/any.hpp>
#include <opencv2/gapi/gtype_traits.hpp>
#include <opencv2/gapi/plaidml/plaidml.hpp>
#include "compiler/gobjref.hpp"
#include "compiler/gmodel.hpp"
#include "backends/plaidml/gplaidmlbackend.hpp"
#include "backends/plaidml/plaidml_util.hpp"
#include "api/gbackend_priv.hpp" // FIXME: Make it part of Backend SDK!
using GPlaidMLModel = ade::TypedGraph
< cv::gimpl::PlaidMLUnit
, cv::gimpl::Protocol
>;
// FIXME: Same issue with Typed and ConstTyped
using GConstGPlaidMLModel = ade::ConstTypedGraph
< cv::gimpl::PlaidMLUnit
, cv::gimpl::Protocol
>;
namespace
{
class GPlaidMLBackendImpl final: public cv::gapi::GBackend::Priv
{
virtual void unpackKernel(ade::Graph &graph,
const ade::NodeHandle &op_node,
const cv::GKernelImpl &impl) override
{
GPlaidMLModel gm(graph);
auto plaidml_impl = cv::util::any_cast<cv::GPlaidMLKernel>(impl.opaque);
gm.metadata(op_node).set(cv::gimpl::PlaidMLUnit{plaidml_impl});
}
virtual EPtr compile(const ade::Graph& graph,
const cv::GCompileArgs& args,
const std::vector<ade::NodeHandle>& nodes,
const std::vector<cv::gimpl::Data>& ins_data,
const std::vector<cv::gimpl::Data>& outs_data) const override
{
auto has_config = cv::gapi::getCompileArg<cv::gapi::plaidml::config>(args);
if (!has_config)
{
cv::util::throw_error(std::runtime_error("Config not found!\n"
"You must pass cv::gapi::plaidml::config to the graph compile arguments"));
}
const auto& arg = has_config.value();
return EPtr{new cv::gimpl::GPlaidMLExecutable(cv::gimpl::GPlaidMLExecutable::Config{arg.dev_id, arg.trg_id},
graph, nodes, ins_data, outs_data)};
}
};
}
cv::gapi::GBackend cv::gapi::plaidml::backend()
{
static cv::gapi::GBackend this_backend(std::make_shared<GPlaidMLBackendImpl>());
return this_backend;
}
void cv::gimpl::GPlaidMLExecutable::initBuffers(const std::vector<cv::gimpl::Data>& data,
std::vector<plaidml::exec::Binding>& bindings)
{
// NB: This is necessary because we keep a pointer to bindings elements to buffer_map
// In order to them to remain valid it's required to prevant reallocation
bindings.reserve(data.size());
for (const auto& d : data)
{
GAPI_Assert(d.shape == GShape::GMAT &&
"Now PlaidML backend supports only cv::GMat's");
const auto& desc = cv::util::get<cv::GMatDesc>(d.meta);
auto placeholder = plaidml::edsl::Placeholder(
cv::util::plaidml::depth_from_ocv(desc.depth),
{desc.size.width, desc.size.height, desc.chan});
const auto& shape = placeholder.shape();
plaidml::TensorShape tshape(shape.dtype(), shape.int_dims());
plaidml::Buffer buffer(m_cfg.dev_id, tshape);
bindings.push_back(plaidml::exec::Binding{std::move(placeholder),
std::move(buffer)});
auto& tensor_map = m_res.slot<plaidml::edsl::Tensor>();
// FIXME Avoid Copy here !!!
tensor_map.emplace(d.rc, bindings.back().tensor);
auto& buffer_map = m_res.slot<plaidml::Buffer*>();
buffer_map.emplace(d.rc, &(bindings.back().buffer));
}
}
void cv::gimpl::GPlaidMLExecutable::compile(const std::vector<cv::gimpl::Data>& ins_data,
const std::vector<cv::gimpl::Data>& outs_data)
{
initBuffers(ins_data, input_bindings_);
initBuffers(outs_data, output_bindings_);
ade::util::transform(outs_data, std::back_inserter(output_ids_),
[](const cv::gimpl::Data& d) { return d.rc; });
GConstGPlaidMLModel gcm(m_g);
for (const auto& nh : m_all_ops)
{
const auto& k = gcm.metadata(nh).get<PlaidMLUnit>().k;
GPlaidMLContext ctx;
const auto &op = m_gm.metadata(nh).get<Op>();
ctx.m_args.reserve(op.args.size());
using namespace std::placeholders;
ade::util::transform(op.args,
std::back_inserter(ctx.m_args),
std::bind(&GPlaidMLExecutable::packArg, this, _1));
for (const auto &out_it : ade::util::indexed(op.outs))
{
const auto out_port = ade::util::index(out_it);
const auto out_desc = ade::util::value(out_it);
auto& tensor_map = m_res.slot<plaidml::edsl::Tensor>();
// NB: Create tensor if need
auto& tensor = tensor_map[out_desc.id];
ctx.m_results[out_port] = GArg(&(tensor));
}
k.apply(ctx);
}
std::vector<plaidml::edsl::Tensor> output_tensors;
for (const auto& out_id : output_ids_)
{
auto& tensor_map = m_res.slot<plaidml::edsl::Tensor>();
// FIXME Avoid copy here !!!
output_tensors.emplace_back(tensor_map[out_id]);
}
plaidml::edsl::Program program("Program", output_tensors);
binder_.reset(new plaidml::exec::Binder(program));
for (const auto& binding : input_bindings_)
{
binder_->set_input(binding.tensor, binding.buffer);
}
for (const auto& binding : output_bindings_)
{
binder_->set_output(binding.tensor, binding.buffer);
}
exec_ = binder_->compile();
}
cv::gimpl::GPlaidMLExecutable::GPlaidMLExecutable(cv::gimpl::GPlaidMLExecutable::Config cfg,
const ade::Graph& g,
const std::vector<ade::NodeHandle>& nodes,
const std::vector<cv::gimpl::Data>& ins_data,
const std::vector<cv::gimpl::Data>& outs_data)
: m_cfg(std::move(cfg)), m_g(g), m_gm(m_g)
{
auto is_op = [&](ade::NodeHandle nh) {
return m_gm.metadata(nh).get<NodeType>().t == NodeType::OP;
};
std::copy_if(nodes.begin(), nodes.end(), std::back_inserter(m_all_ops), is_op);
compile(ins_data, outs_data);
}
void cv::gimpl::GPlaidMLExecutable::run(std::vector<InObj> &&input_objs,
std::vector<OutObj> &&output_objs)
{
for (auto& it : input_objs) bindInArg (it.first, it.second);
exec_->run();
for (auto& it : output_objs) bindOutArg(it.first, it.second);
}
void cv::gimpl::GPlaidMLExecutable::bindInArg(const RcDesc &rc, const GRunArg &arg)
{
switch (rc.shape)
{
case GShape::GMAT:
{
auto& tensor_map = m_res.slot<plaidml::edsl::Tensor>();
auto it = tensor_map.find(rc.id);
GAPI_Assert(it != tensor_map.end());
switch (arg.index())
{
case GRunArg::index_of<cv::RMat>():
{
auto& rmat = cv::util::get<cv::RMat>(arg);
auto view = rmat.access(cv::RMat::Access::R);
auto mat = cv::gimpl::asMat(view);
binder_->input(it->second).copy_from(mat.data);
}
break;
default: util::throw_error(std::logic_error("content type of the runtime argument does not match to resource description ?"));
}
}
break;
default:
util::throw_error(std::logic_error("Unsupported GShape type"));
}
}
void cv::gimpl::GPlaidMLExecutable::bindOutArg(const RcDesc &rc, const GRunArgP &arg)
{
switch (rc.shape)
{
case GShape::GMAT:
{
auto& tensor_map = m_res.slot<plaidml::edsl::Tensor>();
auto it = tensor_map.find(rc.id);
GAPI_Assert(it != tensor_map.end());
switch (arg.index())
{
case GRunArgP::index_of<cv::RMat*>() :
{
auto& rmat = *cv::util::get<cv::RMat*>(arg);
auto view = rmat.access(cv::RMat::Access::W);
auto mat = cv::gimpl::asMat(view);
binder_->output(it->second).copy_into(mat.data);
}
break;
default: util::throw_error(std::logic_error("content type of the runtime argument does not match to resource description ?"));
}
}
break;
default:
util::throw_error(std::logic_error("Unsupported GShape type"));
}
}
cv::GArg cv::gimpl::GPlaidMLExecutable::packArg(const GArg &arg)
{
GAPI_Assert( arg.kind != cv::detail::ArgKind::GMAT
&& arg.kind != cv::detail::ArgKind::GSCALAR
&& arg.kind != cv::detail::ArgKind::GARRAY
&& arg.kind != cv::detail::ArgKind::GOPAQUE);
if (arg.kind != cv::detail::ArgKind::GOBJREF)
{
// All other cases - pass as-is, with no transformations to GArg contents.
return arg;
}
GAPI_Assert(arg.kind == cv::detail::ArgKind::GOBJREF);
const cv::gimpl::RcDesc &ref = arg.get<cv::gimpl::RcDesc>();
switch (ref.shape)
{
case GShape::GMAT:
{
auto& tensor_map = m_res.slot<plaidml::edsl::Tensor>();
auto it = tensor_map.find(ref.id);
GAPI_Assert(it != tensor_map.end());
return GArg(it->second);
}
break;
default:
util::throw_error(std::logic_error("Unsupported GShape type"));
break;
}
}
#endif // HAVE_PLAIDML