blob: 482932fd46175722bb497098c2b058525c01d27a (
plain)
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
|
/*
* Copyright (c) 2022 Samsung Electronics Co., Ltd. All Rights Reserved
*
* 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.
*/
#ifndef __ONERT_BACKEND_TRIX_DEV_CONTEXT_H__
#define __ONERT_BACKEND_TRIX_DEV_CONTEXT_H__
#include <libnpuhost.h>
namespace onert
{
namespace backend
{
namespace trix
{
class DevContext
{
public:
DevContext()
{
auto device_count = getnumNPUdeviceByType(NPUCOND_TRIV2_CONN_SOCIP);
if (device_count <= 0)
{
throw std::runtime_error("Unable to find TRIV2 NPU device");
}
// Use NPU 0 device
if (getNPUdeviceByType(&_dev_handle, NPUCOND_TRIV2_CONN_SOCIP, 0) < 0)
{
throw std::runtime_error("Failed to get TRIV2 NPU device handle");
}
}
~DevContext()
{
if (_dev_handle != nullptr)
{
unregisterNPUmodel_all(_dev_handle);
putNPUdevice(_dev_handle);
}
}
npudev_h getDev() { return _dev_handle; }
template <typename T> void setDataInfo(tensors_data_info *info, std::vector<T *> &tensors)
{
info->num_info = static_cast<uint32_t>(tensors.size());
for (uint32_t idx = 0; idx < info->num_info; ++idx)
{
info->info[idx].layout = convertDataLayout(tensors[idx]->layout());
info->info[idx].type = convertDataType(tensors[idx]->data_type());
}
}
template <typename T> void setBuffer(generic_buffers *buf, std::vector<T *> &tensors)
{
buf->num_buffers = static_cast<uint32_t>(tensors.size());
for (uint32_t idx = 0; idx < buf->num_buffers; ++idx)
{
buf->bufs[idx].addr = tensors[idx]->buffer();
buf->bufs[idx].size = static_cast<uint64_t>(tensors[idx]->total_size());
buf->bufs[idx].type = BUFFER_MAPPED;
}
}
private:
data_layout convertDataLayout(const ir::Layout layout)
{
switch (layout)
{
case ir::Layout::NCHW:
return DATA_LAYOUT_NCHW;
case ir::Layout::NHWC:
return DATA_LAYOUT_NHWC;
default:
throw std::runtime_error("Unknown Layout");
}
}
data_type convertDataType(const ir::DataType type)
{
switch (type)
{
case ir::DataType::QUANT_UINT8_ASYMM:
return DATA_TYPE_QASYMM8;
case ir::DataType::QUANT_INT16_SYMM:
return DATA_TYPE_QSYMM16;
default:
throw std::runtime_error("Unsupported data type");
}
}
private:
// NPU device handle
// TODO Support multicore npu device
npudev_h _dev_handle;
};
} // namespace trix
} // namespace backend
} // namespace onert
#endif // __ONERT_BACKEND_TRIX_DEV_CONTEXT_H__
|