ONE - On-device Neural Engine
Loading...
Searching...
No Matches
nnfw_session_bindings.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2025 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
18
19#include "nnfw_api_wrapper.h"
20
21namespace onert::api::python
22{
23
24namespace py = pybind11;
25
26// Bind the `NNFW_SESSION` class with common inference APIs
27void bind_nnfw_session(py::module_ &m)
28{
29 py::class_<NNFW_SESSION>(m, "nnfw_session", py::module_local())
30 .def(
31 py::init<const char *, const char *>(), py::arg("package_file_path"), py::arg("backends"),
32 "Create a new session instance, load model from nnpackage file or directory, "
33 "set available backends and prepare session to be ready for inference\n"
34 "Parameters:\n"
35 "\tpackage_file_path (str): Path to the nnpackage file or unzipped directory to be loaded\n"
36 "\tbackends (str): Available backends on which nnfw uses\n"
37 "\t\tMultiple backends can be set and they must be separated by a semicolon "
38 "(ex: \"acl_cl;cpu\")\n"
39 "\t\tAmong the multiple backends, the 1st element is used as the default backend.")
40 .def("set_input_tensorinfo", &NNFW_SESSION::set_input_tensorinfo, py::arg("index"),
41 py::arg("tensor_info"),
42 "Set input model's tensor info for resizing.\n"
43 "Parameters:\n"
44 "\tindex (int): Index of input to be set (0-indexed)\n"
45 "\ttensor_info (tensorinfo): Tensor info to be set")
46 .def("prepare", &NNFW_SESSION::prepare, "Prepare for inference")
47 .def("run", &NNFW_SESSION::run, "Run inference")
48 .def("run_async", &NNFW_SESSION::run_async, "Run inference asynchronously")
49 .def("wait", &NNFW_SESSION::wait, "Wait for asynchronous run to finish")
50 .def(
51 "set_input",
52 [](NNFW_SESSION &session, uint32_t index, py::array_t<float> &buffer) {
53 session.set_input<float>(index, buffer);
54 },
55 py::arg("index"), py::arg("buffer"),
56 "Set input buffer\n"
57 "Parameters:\n"
58 "\tindex (int): Index of input to be set (0-indexed)\n"
59 "\tbuffer (numpy): Raw buffer for input")
60 .def(
61 "set_input",
62 [](NNFW_SESSION &session, uint32_t index, py::array_t<int> &buffer) {
63 session.set_input<int>(index, buffer);
64 },
65 py::arg("index"), py::arg("buffer"),
66 "Set input buffer\n"
67 "Parameters:\n"
68 "\tindex (int): Index of input to be set (0-indexed)\n"
69 "\tbuffer (numpy): Raw buffer for input")
70 .def(
71 "set_input",
72 [](NNFW_SESSION &session, uint32_t index, py::array_t<uint8_t> &buffer) {
73 session.set_input<uint8_t>(index, buffer);
74 },
75 py::arg("index"), py::arg("buffer"),
76 "Set input buffer\n"
77 "Parameters:\n"
78 "\tindex (int): Index of input to be set (0-indexed)\n"
79 "\tbuffer (numpy): Raw buffer for input")
80 .def(
81 "set_input",
82 [](NNFW_SESSION &session, uint32_t index, py::array_t<bool> &buffer) {
83 session.set_input<bool>(index, buffer);
84 },
85 py::arg("index"), py::arg("buffer"),
86 "Set input buffer\n"
87 "Parameters:\n"
88 "\tindex (int): Index of input to be set (0-indexed)\n"
89 "\tbuffer (numpy): Raw buffer for input")
90 .def(
91 "set_input",
92 [](NNFW_SESSION &session, uint32_t index, py::array_t<int64_t> &buffer) {
93 session.set_input<int64_t>(index, buffer);
94 },
95 py::arg("index"), py::arg("buffer"),
96 "Set input buffer\n"
97 "Parameters:\n"
98 "\tindex (int): Index of input to be set (0-indexed)\n"
99 "\tbuffer (numpy): Raw buffer for input")
100 .def(
101 "set_input",
102 [](NNFW_SESSION &session, uint32_t index, py::array_t<int8_t> &buffer) {
103 session.set_input<int8_t>(index, buffer);
104 },
105 py::arg("index"), py::arg("buffer"),
106 "Set input buffer\n"
107 "Parameters:\n"
108 "\tindex (int): Index of input to be set (0-indexed)\n"
109 "\tbuffer (numpy): Raw buffer for input")
110 .def(
111 "set_input",
112 [](NNFW_SESSION &session, uint32_t index, py::array_t<int16_t> &buffer) {
113 session.set_input<int16_t>(index, buffer);
114 },
115 py::arg("index"), py::arg("buffer"),
116 "Set input buffer\n"
117 "Parameters:\n"
118 "\tindex (int): Index of input to be set (0-indexed)\n"
119 "\tbuffer (numpy): Raw buffer for input")
120 .def("input_size", &NNFW_SESSION::input_size,
121 "Get the number of inputs defined in loaded model\n"
122 "Returns:\n"
123 "\tint: The number of inputs")
124 .def("output_size", &NNFW_SESSION::output_size,
125 "Get the number of outputs defined in loaded model\n"
126 "Returns:\n"
127 "\tint: The number of outputs")
128 .def("set_input_layout", &NNFW_SESSION::set_input_layout, py::arg("index"),
129 py::arg("layout") = "NONE",
130 "Set the layout of an input\n"
131 "Parameters:\n"
132 "\tindex (int): Index of input to be set (0-indexed)\n"
133 "\tlayout (str): Layout to set to target input")
134 .def("input_tensorinfo", &NNFW_SESSION::input_tensorinfo, py::arg("index"),
135 "Get i-th input tensor info\n"
136 "Parameters:\n"
137 "\tindex (int): Index of input\n"
138 "Returns:\n"
139 "\ttensorinfo: Tensor info (shape, type, etc)")
140 .def("output_tensorinfo", &NNFW_SESSION::output_tensorinfo, py::arg("index"),
141 "Get i-th output tensor info\n"
142 "Parameters:\n"
143 "\tindex (int): Index of output\n"
144 "Returns:\n"
145 "\ttensorinfo: Tensor info (shape, type, etc)")
146 .def("get_output", &NNFW_SESSION::get_output, py::arg("index"),
147 R"pbdoc(
148 Retrieve the internally-allocated dynamic output as a copy.
149 Parameters:
150 index (int): Index of the output tensor (0-indexed)
151 Returns:
152 numpy.ndarray: a copy of the internal buffer
153 )pbdoc")
154 .def("set_prepare_config", &NNFW_SESSION::set_prepare_config, py::arg("config"),
155 "Set configuration to prepare");
156}
157
158// Bind the `NNFW_SESSION` class with experimental APIs
159void bind_experimental_nnfw_session(py::module_ &m)
160{
161 // Add experimental APIs for the `NNFW_SESSION` class
162 m.attr("nnfw_session")
163 .cast<py::class_<NNFW_SESSION>>()
164 .def("train_get_traininfo", &NNFW_SESSION::train_get_traininfo,
165 "Retrieve training information for the model.")
166 .def("train_set_traininfo", &NNFW_SESSION::train_set_traininfo, py::arg("info"),
167 "Set training information for the model.")
168 .def("train_prepare", &NNFW_SESSION::train_prepare, "Prepare for training")
169 .def("train", &NNFW_SESSION::train, py::arg("update_weights") = true,
170 "Run a training step, optionally updating weights.")
171 .def("train_get_loss", &NNFW_SESSION::train_get_loss, py::arg("index"),
172 "Retrieve the training loss for a specific index.")
173 .def("train_set_input", &NNFW_SESSION::train_set_input<float>, py::arg("index"),
174 py::arg("buffer"), "Set training input tensor for the given index (float).")
175 .def("train_set_input", &NNFW_SESSION::train_set_input<int>, py::arg("index"),
176 py::arg("buffer"), "Set training input tensor for the given index (int).")
177 .def("train_set_input", &NNFW_SESSION::train_set_input<uint8_t>, py::arg("index"),
178 py::arg("buffer"), "Set training input tensor for the given index (uint8).")
179 .def("train_set_expected", &NNFW_SESSION::train_set_expected<float>, py::arg("index"),
180 py::arg("buffer"), "Set expected output tensor for the given index (float).")
181 .def("train_set_expected", &NNFW_SESSION::train_set_expected<int>, py::arg("index"),
182 py::arg("buffer"), "Set expected output tensor for the given index (int).")
183 .def("train_set_expected", &NNFW_SESSION::train_set_expected<uint8_t>, py::arg("index"),
184 py::arg("buffer"), "Set expected output tensor for the given index (uint8).")
185 .def("train_set_output", &NNFW_SESSION::train_set_output<float>, py::arg("index"),
186 py::arg("buffer"), "Set output tensor for the given index (float).")
187 .def("train_set_output", &NNFW_SESSION::train_set_output<int>, py::arg("index"),
188 py::arg("buffer"), "Set output tensor for the given index (int).")
189 .def("train_set_output", &NNFW_SESSION::train_set_output<uint8_t>, py::arg("index"),
190 py::arg("buffer"), "Set output tensor for the given index (uint8).")
191 .def("train_export_circle", &NNFW_SESSION::train_export_circle, py::arg("path"),
192 "Export the trained model to a circle file.")
193 .def("train_import_checkpoint", &NNFW_SESSION::train_import_checkpoint, py::arg("path"),
194 "Import a training checkpoint from a file.")
195 .def("train_export_checkpoint", &NNFW_SESSION::train_export_checkpoint, py::arg("path"),
196 "Export the training checkpoint to a file.");
197}
198
199} // namespace onert::api::python
void train_export_circle(const py::str &path)
py::array get_output(uint32_t index)
tensorinfo input_tensorinfo(uint32_t index)
void train_import_checkpoint(const py::str &path)
void set_input_tensorinfo(uint32_t index, const tensorinfo *tensor_info)
void train(bool update_weights)
void set_input_layout(uint32_t index, const char *layout)
void train_set_traininfo(const nnfw_train_info *info)
void set_prepare_config(NNFW_PREPARE_CONFIG config)
tensorinfo output_tensorinfo(uint32_t index)
void train_export_checkpoint(const py::str &path)
void bind_nnfw_session(pybind11::module_ &m)
void bind_experimental_nnfw_session(pybind11::module_ &m)