ONE - On-device Neural Engine
Loading...
Searching...
No Matches
nnfw_api_wrapper.h
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#ifndef __ONERT_API_PYTHON_NNFW_API_WRAPPER_H__
18#define __ONERT_API_PYTHON_NNFW_API_WRAPPER_H__
19
20#include "nnfw.h"
21#include "nnfw_experimental.h"
22#include "nnfw_internal.h"
23
24#include <pybind11/stl.h>
25#include <pybind11/numpy.h>
26
27namespace onert
28{
29namespace api
30{
31namespace python
32{
33
34namespace py = pybind11;
35
49{
51 const char *dtype;
53 int32_t rank;
59};
60
68void ensure_status(NNFW_STATUS status);
69
76NNFW_LAYOUT getLayout(const char *layout = "");
77
84NNFW_TYPE getType(const char *type = "");
85
92const char *getStringType(NNFW_TYPE type);
93
102uint64_t num_elems(const nnfw_tensorinfo *tensor_info);
103
112py::list get_dims(const tensorinfo &tensor_info);
113
122void set_dims(tensorinfo &tensor_info, const py::list &array);
123
125{
126private:
127 nnfw_session *session;
128
129public:
130 NNFW_SESSION(const char *package_file_path, const char *backends);
132
133 void close_session();
134 void set_input_tensorinfo(uint32_t index, const tensorinfo *tensor_info);
135 void prepare();
136 void run();
137 void run_async();
138 void wait();
143 template <typename T> void set_input(uint32_t index, py::array_t<T> &buffer)
144 {
145 nnfw_tensorinfo tensor_info;
146 nnfw_input_tensorinfo(this->session, index, &tensor_info);
147 NNFW_TYPE type = tensor_info.dtype;
148 uint32_t input_elements = num_elems(&tensor_info);
149 size_t length = sizeof(T) * input_elements;
150
151 ensure_status(nnfw_set_input(session, index, type, buffer.request().ptr, length));
152 }
157 template <typename T> void set_output(uint32_t index, py::array_t<T> &buffer)
158 {
159 nnfw_tensorinfo tensor_info;
160 nnfw_output_tensorinfo(this->session, index, &tensor_info);
161 NNFW_TYPE type = tensor_info.dtype;
162 uint32_t output_elements = num_elems(&tensor_info);
163 size_t length = sizeof(T) * output_elements;
164
165 ensure_status(nnfw_set_output(session, index, type, buffer.request().ptr, length));
166 }
167 uint32_t input_size();
168 uint32_t output_size();
169 // process the input layout by receiving a string from Python instead of NNFW_LAYOUT
170 void set_input_layout(uint32_t index, const char *layout);
171 // process the output layout by receiving a string from Python instead of NNFW_LAYOUT
172 void set_output_layout(uint32_t index, const char *layout);
173 tensorinfo input_tensorinfo(uint32_t index);
174 tensorinfo output_tensorinfo(uint32_t index);
175
177 // Internal APIs
179 py::array get_output(uint32_t index);
180
182 // Experimental APIs for inference
185
187 // Experimental APIs for training
191
192 template <typename T> void train_set_input(uint32_t index, py::array_t<T> &buffer)
193 {
194 nnfw_tensorinfo tensor_info;
195 nnfw_input_tensorinfo(this->session, index, &tensor_info);
196
197 py::buffer_info buf_info = buffer.request();
198 const auto buf_shape = buf_info.shape;
199 assert(tensor_info.rank == static_cast<int32_t>(buf_shape.size()) && buf_shape.size() > 0);
200 tensor_info.dims[0] = static_cast<int32_t>(buf_shape.at(0));
201
202 ensure_status(nnfw_train_set_input(this->session, index, buffer.request().ptr, &tensor_info));
203 }
204 template <typename T> void train_set_expected(uint32_t index, py::array_t<T> &buffer)
205 {
206 nnfw_tensorinfo tensor_info;
207 nnfw_output_tensorinfo(this->session, index, &tensor_info);
208
209 py::buffer_info buf_info = buffer.request();
210 const auto buf_shape = buf_info.shape;
211 assert(tensor_info.rank == static_cast<int32_t>(buf_shape.size()) && buf_shape.size() > 0);
212 tensor_info.dims[0] = static_cast<int32_t>(buf_shape.at(0));
213
215 nnfw_train_set_expected(this->session, index, buffer.request().ptr, &tensor_info));
216 }
217 template <typename T> void train_set_output(uint32_t index, py::array_t<T> &buffer)
218 {
219 nnfw_tensorinfo tensor_info;
220 nnfw_output_tensorinfo(this->session, index, &tensor_info);
221 NNFW_TYPE type = tensor_info.dtype;
222 uint32_t output_elements = num_elems(&tensor_info);
223 size_t length = sizeof(T) * output_elements;
224
225 ensure_status(nnfw_train_set_output(session, index, type, buffer.request().ptr, length));
226 }
227
228 void train_prepare();
229 void train(bool update_weights);
230 float train_get_loss(uint32_t index);
231
232 void train_export_circle(const py::str &path);
233 void train_import_checkpoint(const py::str &path);
234 void train_export_checkpoint(const py::str &path);
235
236 // TODO Add other apis
237};
238
239} // namespace python
240} // namespace api
241} // namespace onert
242
243#endif // __ONERT_API_PYTHON_NNFW_API_WRAPPER_H__
void train_set_output(uint32_t index, py::array_t< T > &buffer)
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 set_input(uint32_t index, py::array_t< T > &buffer)
process input array according to data type of numpy array sent by Python (int, float,...
void set_input_layout(uint32_t index, const char *layout)
void train_set_input(uint32_t index, py::array_t< T > &buffer)
void train_set_traininfo(const nnfw_train_info *info)
void set_output_layout(uint32_t index, const char *layout)
void set_output(uint32_t index, py::array_t< T > &buffer)
process output array according to data type of numpy array sent by Python (int, float,...
void set_prepare_config(NNFW_PREPARE_CONFIG config)
tensorinfo output_tensorinfo(uint32_t index)
void train_set_expected(uint32_t index, py::array_t< T > &buffer)
void train_export_checkpoint(const py::str &path)
volatile const char info[]
void set_dims(tensorinfo &tensor_info, const py::list &array)
Set nnfw_tensorinfo->dims.
const char * getStringType(NNFW_TYPE type)
NNFW_TYPE getType(const char *type="")
void ensure_status(NNFW_STATUS status)
Handle errors with NNFW_STATUS in API functions.
NNFW_LAYOUT getLayout(const char *layout="")
uint64_t num_elems(const nnfw_tensorinfo *tensor_info)
Get the total number of elements in nnfw_tensorinfo->dims.
py::list get_dims(const tensorinfo &tensor_info)
Get nnfw_tensorinfo->dims.
This file describes runtime API.
NNFW_STATUS nnfw_set_input(nnfw_session *session, uint32_t index, NNFW_TYPE type, const void *buffer, size_t length)
Set input buffer.
Definition nnfw_api.cc:96
NNFW_STATUS nnfw_output_tensorinfo(nnfw_session *session, uint32_t index, nnfw_tensorinfo *tensor_info)
Get i-th output tensor info.
Definition nnfw_api.cc:153
NNFW_STATUS nnfw_input_tensorinfo(nnfw_session *session, uint32_t index, nnfw_tensorinfo *tensor_info)
Get i-th input tensor info.
Definition nnfw_api.cc:146
NNFW_STATUS nnfw_set_output(nnfw_session *session, uint32_t index, NNFW_TYPE type, void *buffer, size_t length)
Set output buffer.
Definition nnfw_api.cc:103
NNFW_LAYOUT
Data format of a tensor.
Definition nnfw.h:134
NNFW_PREPARE_CONFIG
Configuration key for prepare (compile and schedule)
NNFW_STATUS
Result values returned from a call to an API function.
Definition onert-micro.h:86
NNFW_STATUS nnfw_train_set_input(nnfw_session *session, uint32_t index, void *input, const nnfw_tensorinfo *input_info)
Set training input.
NNFW_STATUS nnfw_train_set_output(nnfw_session *session, uint32_t index, NNFW_TYPE type, void *buffer, size_t length)
Set training output buffer.
NNFW_STATUS nnfw_train_set_expected(nnfw_session *session, uint32_t index, void *expected, const nnfw_tensorinfo *expected_info)
Set training expected output.
NNFW_TYPE
Definition onert-micro.h:75
#define NNFW_MAX_RANK
Maximum rank expressible with nnfw.
tensor info describes the type and shape of tensors
NNFW_TYPE dtype
int32_t dims[NNFW_MAX_RANK]
Training information to prepare training.
tensor info describes the type and shape of tensors
int32_t dims[NNFW_MAX_RANK]