ONE - On-device Neural Engine
Loading...
Searching...
No Matches
nnfw_api_wrapper_pybind.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2023 Samsung Electronics Co., Ltd. All Rights Reserved
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 "nnfw_api_wrapper.h"
18
19namespace py = pybind11;
20
21PYBIND11_MODULE(libnnfw_api_pybind, m)
22{
23 m.doc() = "nnfw python plugin";
24
25 py::class_<tensorinfo>(m, "tensorinfo", "tensorinfo describes the type and shape of tensors")
26 .def(py::init<>(), "The constructor of tensorinfo")
27 .def_readwrite("dtype", &tensorinfo::dtype, "The data type")
28 .def_readwrite("rank", &tensorinfo::rank, "The number of dimensions (rank)")
29 .def_property(
30 "dims", [](const tensorinfo &ti) { return get_dims(ti); },
31 [](tensorinfo &ti, const py::list &dims_list) { set_dims(ti, dims_list); },
32 "The dimension of tensor. Maximum rank is 6 (NNFW_MAX_RANK).");
33
34 py::class_<NNFW_SESSION>(m, "nnfw_session")
35 .def(
36 py::init<const char *, const char *>(), py::arg("package_file_path"), py::arg("backends"),
37 "Create a new session instance, load model from nnpackage file or directory, "
38 "set available backends and prepare session to be ready for inference\n"
39 "Parameters:\n"
40 "\tpackage_file_path (str): Path to the nnpackage file or unzipped directory to be loaded\n"
41 "\tbackends (str): Available backends on which nnfw uses\n"
42 "\t\tMultiple backends can be set and they must be separated by a semicolon "
43 "(ex: \"acl_cl;cpu\")\n"
44 "\t\tAmong the multiple backends, the 1st element is used as the default backend.")
45 .def("set_input_tensorinfo", &NNFW_SESSION::set_input_tensorinfo, py::arg("index"),
46 py::arg("tensor_info"),
47 "Set input model's tensor info for resizing.\n"
48 "Parameters:\n"
49 "\tindex (int): Index of input to be set (0-indexed)\n"
50 "\ttensor_info (tensorinfo): Tensor info to be set")
51 .def("run", &NNFW_SESSION::run, "Run inference")
52 .def("run_async", &NNFW_SESSION::run_async, "Run inference asynchronously")
53 .def("wait", &NNFW_SESSION::wait, "Wait for asynchronous run to finish")
54 .def(
55 "set_input",
56 [](NNFW_SESSION &session, uint32_t index, py::array_t<float> &buffer) {
57 session.set_input<float>(index, buffer);
58 },
59 py::arg("index"), py::arg("buffer"),
60 "Set input buffer\n"
61 "Parameters:\n"
62 "\tindex (int): Index of input to be set (0-indexed)\n"
63 "\tbuffer (numpy): Raw buffer for input")
64 .def(
65 "set_input",
66 [](NNFW_SESSION &session, uint32_t index, py::array_t<int> &buffer) {
67 session.set_input<int>(index, buffer);
68 },
69 py::arg("index"), py::arg("buffer"),
70 "Set input buffer\n"
71 "Parameters:\n"
72 "\tindex (int): Index of input to be set (0-indexed)\n"
73 "\tbuffer (numpy): Raw buffer for input")
74 .def(
75 "set_input",
76 [](NNFW_SESSION &session, uint32_t index, py::array_t<uint8_t> &buffer) {
77 session.set_input<uint8_t>(index, buffer);
78 },
79 py::arg("index"), py::arg("buffer"),
80 "Set input buffer\n"
81 "Parameters:\n"
82 "\tindex (int): Index of input to be set (0-indexed)\n"
83 "\tbuffer (numpy): Raw buffer for input")
84 .def(
85 "set_input",
86 [](NNFW_SESSION &session, uint32_t index, py::array_t<bool> &buffer) {
87 session.set_input<bool>(index, buffer);
88 },
89 py::arg("index"), py::arg("buffer"),
90 "Set input buffer\n"
91 "Parameters:\n"
92 "\tindex (int): Index of input to be set (0-indexed)\n"
93 "\tbuffer (numpy): Raw buffer for input")
94 .def(
95 "set_input",
96 [](NNFW_SESSION &session, uint32_t index, py::array_t<int64_t> &buffer) {
97 session.set_input<int64_t>(index, buffer);
98 },
99 py::arg("index"), py::arg("buffer"),
100 "Set input buffer\n"
101 "Parameters:\n"
102 "\tindex (int): Index of input to be set (0-indexed)\n"
103 "\tbuffer (numpy): Raw buffer for input")
104 .def(
105 "set_input",
106 [](NNFW_SESSION &session, uint32_t index, py::array_t<int8_t> &buffer) {
107 session.set_input<int8_t>(index, buffer);
108 },
109 py::arg("index"), py::arg("buffer"),
110 "Set input buffer\n"
111 "Parameters:\n"
112 "\tindex (int): Index of input to be set (0-indexed)\n"
113 "\tbuffer (numpy): Raw buffer for input")
114 .def(
115 "set_input",
116 [](NNFW_SESSION &session, uint32_t index, py::array_t<int16_t> &buffer) {
117 session.set_input<int16_t>(index, buffer);
118 },
119 py::arg("index"), py::arg("buffer"),
120 "Set input buffer\n"
121 "Parameters:\n"
122 "\tindex (int): Index of input to be set (0-indexed)\n"
123 "\tbuffer (numpy): Raw buffer for input")
124 .def(
125 "set_output",
126 [](NNFW_SESSION &session, uint32_t index, py::array_t<float> &buffer) {
127 session.set_output<float>(index, buffer);
128 },
129 py::arg("index"), py::arg("buffer"),
130 "Set output buffer\n"
131 "Parameters:\n"
132 "\tindex (int): Index of output to be set (0-indexed)\n"
133 "\tbuffer (numpy): Raw buffer for output")
134 .def(
135 "set_output",
136 [](NNFW_SESSION &session, uint32_t index, py::array_t<int> &buffer) {
137 session.set_output<int>(index, buffer);
138 },
139 py::arg("index"), py::arg("buffer"),
140 "Set output buffer\n"
141 "Parameters:\n"
142 "\tindex (int): Index of output to be set (0-indexed)\n"
143 "\tbuffer (numpy): Raw buffer for output")
144 .def(
145 "set_output",
146 [](NNFW_SESSION &session, uint32_t index, py::array_t<uint8_t> &buffer) {
147 session.set_output<uint8_t>(index, buffer);
148 },
149 py::arg("index"), py::arg("buffer"),
150 "Set output buffer\n"
151 "Parameters:\n"
152 "\tindex (int): Index of output to be set (0-indexed)\n"
153 "\tbuffer (numpy): Raw buffer for output")
154 .def(
155 "set_output",
156 [](NNFW_SESSION &session, uint32_t index, py::array_t<bool> &buffer) {
157 session.set_output<bool>(index, buffer);
158 },
159 py::arg("index"), py::arg("buffer"),
160 "Set output buffer\n"
161 "Parameters:\n"
162 "\tindex (int): Index of output to be set (0-indexed)\n"
163 "\tbuffer (numpy): Raw buffer for output")
164 .def(
165 "set_output",
166 [](NNFW_SESSION &session, uint32_t index, py::array_t<int64_t> &buffer) {
167 session.set_output<int64_t>(index, buffer);
168 },
169 py::arg("index"), py::arg("buffer"),
170 "Set output buffer\n"
171 "Parameters:\n"
172 "\tindex (int): Index of output to be set (0-indexed)\n"
173 "\tbuffer (numpy): Raw buffer for output")
174 .def(
175 "set_output",
176 [](NNFW_SESSION &session, uint32_t index, py::array_t<int8_t> &buffer) {
177 session.set_output<int8_t>(index, buffer);
178 },
179 py::arg("index"), py::arg("buffer"),
180 "Set output buffer\n"
181 "Parameters:\n"
182 "\tindex (int): Index of output to be set (0-indexed)\n"
183 "\tbuffer (numpy): Raw buffer for output")
184 .def(
185 "set_output",
186 [](NNFW_SESSION &session, uint32_t index, py::array_t<int16_t> &buffer) {
187 session.set_output<int16_t>(index, buffer);
188 },
189 py::arg("index"), py::arg("buffer"),
190 "Set output buffer\n"
191 "Parameters:\n"
192 "\tindex (int): Index of output to be set (0-indexed)\n"
193 "\tbuffer (numpy): Raw buffer for output")
194 .def("input_size", &NNFW_SESSION::input_size,
195 "Get the number of inputs defined in loaded model\n"
196 "Returns:\n"
197 "\tint: The number of inputs")
198 .def("output_size", &NNFW_SESSION::output_size,
199 "Get the number of outputs defined in loaded model\n"
200 "Returns:\n"
201 "\tint: The number of outputs")
202 .def("set_input_layout", &NNFW_SESSION::set_input_layout, py::arg("index"),
203 py::arg("layout") = "NONE",
204 "Set the layout of an input\n"
205 "Parameters:\n"
206 "\tindex (int): Index of input to be set (0-indexed)\n"
207 "\tlayout (str): Layout to set to target input")
208 .def("set_output_layout", &NNFW_SESSION::set_output_layout, py::arg("index"),
209 py::arg("layout") = "NONE",
210 "Set the layout of an output\n"
211 "Parameters:\n"
212 "\tindex (int): Index of output to be set (0-indexed)\n"
213 "\tlayout (str): Layout to set to target output")
214 .def("input_tensorinfo", &NNFW_SESSION::input_tensorinfo, py::arg("index"),
215 "Get i-th input tensor info\n"
216 "Parameters:\n"
217 "\tindex (int): Index of input\n"
218 "Returns:\n"
219 "\ttensorinfo: Tensor info (shape, type, etc)")
220 .def("output_tensorinfo", &NNFW_SESSION::output_tensorinfo, py::arg("index"),
221 "Get i-th output tensor info\n"
222 "Parameters:\n"
223 "\tindex (int): Index of output\n"
224 "Returns:\n"
225 "\ttensorinfo: Tensor info (shape, type, etc)");
226}
tensorinfo output_tensorinfo(uint32_t index)
uint32_t output_size()
void set_input_layout(uint32_t index, const char *layout)
void set_output_layout(uint32_t index, const char *layout)
tensorinfo input_tensorinfo(uint32_t index)
uint32_t input_size()
void set_input_tensorinfo(uint32_t index, const tensorinfo *tensor_info)
py::list get_dims(const tensorinfo &tensor_info)
Get nnfw_tensorinfo->dims.
void set_dims(tensorinfo &tensor_info, const py::list &array)
Set nnfw_tensorinfo->dims.
PYBIND11_MODULE(libnnfw_api_pybind, m)
tensor info describes the type and shape of tensors
const char * dtype