/
tensor_filter_trix_engine.cc
382 lines (329 loc) · 9.96 KB
/
tensor_filter_trix_engine.cc
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/* SPDX-License-Identifier: LGPL-2.1-only */
/**
* Copyright (C) 2020 Samsung Electronics
* Copyright (C) 2020 Dongju Chae <dongju.chae@samsung.com>
*/
/**
* @file tensor_filter_trix_engine.cc
* @date 20 Jan 2020
* @brief NNStreamer tensor-filter subplugin for TRIx devices
* @see http://github.com/nnstreamer/nnstreamer
* @author Dongju Chae <dongju.chae@samsung.com>
* @bug No known bugs except for NYI items
*/
#include <tensor_filter_trix_engine.hh>
using namespace std;
namespace nnstreamer
{
void init_filter_trix_engine (void) __attribute__ ((constructor));
void fini_filter_trix_engine (void) __attribute__ ((destructor));
TensorFilterTRIxEngine *TensorFilterTRIxEngine::registered = nullptr;
const char *TensorFilterTRIxEngine::name = "trix-engine";
const accl_hw TensorFilterTRIxEngine::hw_list[] = { ACCL_NPU_SR };
const int TensorFilterTRIxEngine::num_hw = 1;
/**
* @brief Construct a new TRIx-Engine subplugin instance
*/
TensorFilterTRIxEngine::TensorFilterTRIxEngine ()
: dev_type_ (NPUCOND_CONN_UNKNOWN), dev_ (nullptr), model_path_ (nullptr),
model_meta_ (nullptr), model_id_ (0), trix_in_info_ (), trix_out_info_ ()
{
gst_tensors_info_init (addressof (nns_in_info_));
gst_tensors_info_init (addressof (nns_out_info_));
}
/**
* @brief Destruct the TRIx-Engine subplugin instance
*/
TensorFilterTRIxEngine::~TensorFilterTRIxEngine ()
{
g_free (model_path_);
g_free (model_meta_);
gst_tensors_info_free (std::addressof (nns_in_info_));
gst_tensors_info_free (std::addressof (nns_out_info_));
if (dev_ != nullptr) {
unregisterNPUmodel_all (dev_);
putNPUdevice (dev_);
}
}
/**
* @brief Method to get an empty object
*/
tensor_filter_subplugin &
TensorFilterTRIxEngine::getEmptyInstance ()
{
return *(new TensorFilterTRIxEngine ());
}
/**
* @brief Configure TRIx-Engine instance
*/
void
TensorFilterTRIxEngine::configure_instance (const GstTensorFilterProperties *prop)
{
GstTensorInfo *_info;
uint32_t i, j, rank_limit;
if (!prop->model_files[0] || prop->model_files[0][0] == '\0') {
nns_loge ("Unable to find a model filepath given\n");
throw invalid_argument ("Unable to find a model filepath given");
}
model_path_ = g_strdup (prop->model_files[0]);
model_meta_ = getNPUmodel_metadata (model_path_, false);
if (model_meta_ == nullptr) {
nns_loge ("Unable to extract the model metadata\n");
throw runtime_error ("Unable to extract the model metadata");
}
int status = -ENOENT;
for (int h = 0; h < prop->num_hw; h++) {
/* TRIV2 alias for now */
if (prop->hw_list[h] == ACCL_NPU_SR) {
status = getNPUdeviceByTypeAny (&dev_, NPUCOND_TRIV2_CONN_SOCIP, 2);
if (status == 0)
break;
}
}
if (status != 0) {
nns_loge ("Unable to find a proper NPU device\n");
throw runtime_error ("Unable to find a proper NPU device");
}
generic_buffer model_file;
model_file.filepath = model_path_;
model_file.size = model_meta_->size;
model_file.type = BUFFER_FILE;
if (registerNPUmodel (dev_, &model_file, &model_id_) != 0) {
nns_loge ("Unable to register the model\n");
throw runtime_error ("Unable to register the model");
}
rank_limit = MIN (MAX_RANK, NNS_TENSOR_RANK_LIMIT);
/* check user-provided input tensor info */
if (prop->input_meta.num_tensors == 0) {
nns_in_info_.num_tensors = model_meta_->input_seg_num;
for (i = 0; i < nns_in_info_.num_tensors; i++) {
_info = gst_tensors_info_get_nth_info (&nns_in_info_, i);
_info->type = _NNS_UINT8;
for (j = 0; j < rank_limit; j++)
_info->dimension[j] = model_meta_->input_seg_dims[i][rank_limit - j - 1];
for (; j < NNS_TENSOR_RANK_LIMIT; j++)
_info->dimension[j] = 0;
}
} else {
gst_tensors_info_copy (&nns_in_info_, &prop->input_meta);
}
/* check user-provided output tensor info */
if (prop->output_meta.num_tensors == 0) {
nns_out_info_.num_tensors = model_meta_->output_seg_num;
for (i = 0; i < nns_out_info_.num_tensors; i++) {
_info = gst_tensors_info_get_nth_info (&nns_out_info_, i);
_info->type = _NNS_UINT8;
for (j = 0; j < rank_limit; j++)
_info->dimension[j] = model_meta_->output_seg_dims[i][rank_limit - j - 1];
for (; j < NNS_TENSOR_RANK_LIMIT; j++)
_info->dimension[j] = 0;
}
} else {
gst_tensors_info_copy (&nns_out_info_, &prop->output_meta);
}
set_data_info (prop);
}
/**
* @brief Convert data layout (from NNStreamer to TRIx-Engine)
*/
data_layout
TensorFilterTRIxEngine::convert_data_layout (const tensor_layout &layout)
{
switch (layout) {
case _NNS_LAYOUT_NHWC:
return DATA_LAYOUT_NHWC;
case _NNS_LAYOUT_NCHW:
return DATA_LAYOUT_NCHW;
default:
return DATA_LAYOUT_MODEL;
}
}
/**
* @brief Convert data type (from NNStreamer to TRIx-Engine)
*/
data_type
TensorFilterTRIxEngine::convert_data_type (const tensor_type &type)
{
switch (type) {
case _NNS_INT32:
return DATA_TYPE_INT32;
case _NNS_UINT32:
return DATA_TYPE_UINT32;
case _NNS_INT16:
return DATA_TYPE_INT16;
case _NNS_UINT16:
return DATA_TYPE_UINT16;
case _NNS_INT8:
return DATA_TYPE_INT8;
case _NNS_UINT8:
return DATA_TYPE_UINT8;
case _NNS_FLOAT64:
return DATA_TYPE_FLOAT64;
case _NNS_FLOAT32:
return DATA_TYPE_FLOAT32;
case _NNS_INT64:
return DATA_TYPE_INT64;
case _NNS_UINT64:
return DATA_TYPE_UINT64;
default:
return DATA_TYPE_MODEL;
}
}
/**
* @brief Set data info of input/output tensors using metadata
*/
void
TensorFilterTRIxEngine::set_data_info (const GstTensorFilterProperties *prop)
{
GstTensorInfo *_info;
const tensor_layout *input_layout = &(prop->input_layout[0]);
const tensor_layout *output_layout = &(prop->output_layout[0]);
trix_in_info_.num_info = model_meta_->input_seg_num;
for (uint32_t idx = 0; idx < trix_in_info_.num_info; ++idx) {
_info = gst_tensors_info_get_nth_info (&nns_in_info_, idx);
trix_in_info_.info[idx].layout = convert_data_layout (input_layout[idx]);
trix_in_info_.info[idx].type = convert_data_type (_info->type);
}
trix_out_info_.num_info = model_meta_->output_seg_num;
for (uint32_t idx = 0; idx < trix_out_info_.num_info; ++idx) {
_info = gst_tensors_info_get_nth_info (&nns_out_info_, idx);
trix_out_info_.info[idx].layout = convert_data_layout (output_layout[idx]);
trix_out_info_.info[idx].type = convert_data_type (_info->type);
}
}
/**
* @brief Feed the tensor data to input buffers before invoke()
*/
void
TensorFilterTRIxEngine::feed_input_data (const GstTensorMemory *input, input_buffers *input_buf)
{
input_buf->num_buffers = model_meta_->input_seg_num;
for (uint32_t idx = 0; idx < input_buf->num_buffers; ++idx) {
input_buf->bufs[idx].addr = input[idx].data;
input_buf->bufs[idx].size = input[idx].size;
input_buf->bufs[idx].type = BUFFER_MAPPED;
}
}
/**
* @brief Extract the tensor data from output buffers after invoke()
*/
void
TensorFilterTRIxEngine::extract_output_data (
const output_buffers *output_buf, GstTensorMemory *output)
{
/* internal logic error */
assert (output_buf->num_buffers == model_meta_->output_seg_num);
for (uint32_t idx = 0; idx < output_buf->num_buffers; ++idx) {
output[idx].data = output_buf->bufs[idx].addr;
output[idx].size = output_buf->bufs[idx].size;
}
}
/**
* @brief Invoke TRIxEngine using input tensors
*/
void
TensorFilterTRIxEngine::invoke (const GstTensorMemory *input, GstTensorMemory *output)
{
int req_id;
int status;
status = createNPU_request (dev_, model_id_, &req_id);
if (status != 0) {
nns_loge ("Unable to create NPU request with model id (%u): %d", model_id_, status);
return;
}
input_buffers input_buf;
output_buffers output_buf;
/* feed input data to npu-engine */
feed_input_data (input, &input_buf);
status = setNPU_requestData (
dev_, req_id, &input_buf, &trix_in_info_, &output_buf, &trix_out_info_);
if (status != 0) {
nns_loge ("Unable to create NPU request for model %u", model_id_);
return;
}
status = submitNPU_request (dev_, req_id);
if (status != 0) {
nns_loge ("Unable to submit NPU request with id (%u): %d", req_id, status);
return;
}
/* extract output data from npu-engine */
extract_output_data (&output_buf, output);
status = removeNPU_request (dev_, req_id);
if (status != 0) {
nns_loge ("Unable to remove NPU request with id (%u): %d", req_id, status);
return;
}
}
/**
* @brief Get TRIxEngine framework info.
*/
void
TensorFilterTRIxEngine::getFrameworkInfo (GstTensorFilterFrameworkInfo &info)
{
info.name = name;
info.allow_in_place = FALSE;
info.allocate_in_invoke = TRUE;
info.run_without_model = FALSE;
info.verify_model_path = TRUE;
info.hw_list = hw_list;
info.num_hw = num_hw;
}
/**
* @brief Get TRIxEngine model info.
*/
int
TensorFilterTRIxEngine::getModelInfo (
model_info_ops ops, GstTensorsInfo &in_info, GstTensorsInfo &out_info)
{
if (ops != GET_IN_OUT_INFO) {
return -ENOENT;
}
gst_tensors_info_copy (addressof (in_info), addressof (nns_in_info_));
gst_tensors_info_copy (addressof (out_info), addressof (nns_out_info_));
return 0;
}
/**
* @brief Method to handle the event
*/
int
TensorFilterTRIxEngine::eventHandler (event_ops ops, GstTensorFilterFrameworkEventData &data)
{
UNUSED (ops);
UNUSED (data);
return -ENOENT;
}
/**
* @brief Register the subpkugin
*/
void
TensorFilterTRIxEngine::init_filter_trix_engine ()
{
registered = tensor_filter_subplugin::register_subplugin<TensorFilterTRIxEngine> ();
}
/**
* @brief Destruct the subplugin
*/
void
TensorFilterTRIxEngine::fini_filter_trix_engine ()
{
/* internal logic error */
assert (registered != nullptr);
tensor_filter_subplugin::unregister_subplugin (registered);
}
/**
* @brief Subplugin initializer
*/
void
init_filter_trix_engine ()
{
TensorFilterTRIxEngine::init_filter_trix_engine ();
}
/**
* @brief Subplugin finalizer
*/
void
fini_filter_trix_engine ()
{
TensorFilterTRIxEngine::fini_filter_trix_engine ();
}
} /* namespace nnstreamer */