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 <string>
21
22#include "nnfw.h"
23#include "nnfw_experimental.h"
24#include "nnfw_internal.h"
25
26#include <pybind11/stl.h>
27#include <pybind11/numpy.h>
28
29namespace onert
30{
31namespace api
32{
33namespace python
34{
35
36namespace py = pybind11;
37
51{
53 std::string dtype;
55 int32_t rank;
61};
62
70void ensure_status(NNFW_STATUS status);
71
78NNFW_LAYOUT getLayout(const char *layout = "");
79
86NNFW_TYPE getType(const char *type = "");
87
94const char *getStringType(NNFW_TYPE type);
95
104uint64_t num_elems(const nnfw_tensorinfo *tensor_info);
105
114py::list get_dims(const tensorinfo &tensor_info);
115
124void set_dims(tensorinfo &tensor_info, const py::list &array);
125
127{
128private:
129 nnfw_session *session;
130
131public:
132 NNFW_SESSION(const char *package_file_path, const char *backends);
134
135 void close_session();
136 void set_input_tensorinfo(uint32_t index, const tensorinfo *tensor_info);
137 void prepare();
138 void run();
139 void run_async();
140 void wait();
145 template <typename T> void set_input(uint32_t index, py::array_t<T> &buffer)
146 {
147 nnfw_tensorinfo tensor_info;
148 nnfw_input_tensorinfo(this->session, index, &tensor_info);
149 NNFW_TYPE type = tensor_info.dtype;
150 uint32_t input_elements = num_elems(&tensor_info);
151 size_t length = sizeof(T) * input_elements;
152
153 ensure_status(nnfw_set_input(session, index, type, buffer.request().ptr, length));
154 }
159 template <typename T> void set_output(uint32_t index, py::array_t<T> &buffer)
160 {
161 nnfw_tensorinfo tensor_info;
162 nnfw_output_tensorinfo(this->session, index, &tensor_info);
163 NNFW_TYPE type = tensor_info.dtype;
164 uint32_t output_elements = num_elems(&tensor_info);
165 size_t length = sizeof(T) * output_elements;
166
167 ensure_status(nnfw_set_output(session, index, type, buffer.request().ptr, length));
168 }
169 uint32_t input_size();
170 uint32_t output_size();
171 // process the input layout by receiving a string from Python instead of NNFW_LAYOUT
172 void set_input_layout(uint32_t index, const char *layout);
173 // process the output layout by receiving a string from Python instead of NNFW_LAYOUT
174 void set_output_layout(uint32_t index, const char *layout);
175 tensorinfo input_tensorinfo(uint32_t index);
176 tensorinfo output_tensorinfo(uint32_t index);
177
179 // Internal APIs
181 py::array get_output(uint32_t index);
182
184 // Experimental APIs for inference
187
189 // Experimental APIs for training
193
194 template <typename T> void train_set_input(uint32_t index, py::array_t<T> &buffer)
195 {
196 nnfw_tensorinfo tensor_info;
197 nnfw_input_tensorinfo(this->session, index, &tensor_info);
198
199 py::buffer_info buf_info = buffer.request();
200 const auto buf_shape = buf_info.shape;
201 assert(tensor_info.rank == static_cast<int32_t>(buf_shape.size()) && buf_shape.size() > 0);
202 tensor_info.dims[0] = static_cast<int32_t>(buf_shape.at(0));
203
204 ensure_status(nnfw_train_set_input(this->session, index, buffer.request().ptr, &tensor_info));
205 }
206 template <typename T> void train_set_expected(uint32_t index, py::array_t<T> &buffer)
207 {
208 nnfw_tensorinfo tensor_info;
209 nnfw_output_tensorinfo(this->session, index, &tensor_info);
210
211 py::buffer_info buf_info = buffer.request();
212 const auto buf_shape = buf_info.shape;
213 assert(tensor_info.rank == static_cast<int32_t>(buf_shape.size()) && buf_shape.size() > 0);
214 tensor_info.dims[0] = static_cast<int32_t>(buf_shape.at(0));
215
217 nnfw_train_set_expected(this->session, index, buffer.request().ptr, &tensor_info));
218 }
219 template <typename T> void train_set_output(uint32_t index, py::array_t<T> &buffer)
220 {
221 nnfw_tensorinfo tensor_info;
222 nnfw_output_tensorinfo(this->session, index, &tensor_info);
223 NNFW_TYPE type = tensor_info.dtype;
224 uint32_t output_elements = num_elems(&tensor_info);
225 size_t length = sizeof(T) * output_elements;
226
227 ensure_status(nnfw_train_set_output(session, index, type, buffer.request().ptr, length));
228 }
229
230 void train_prepare();
231 void train(bool update_weights);
232 float train_get_loss(uint32_t index);
233
234 void train_export_circle(const py::str &path);
235 void train_import_checkpoint(const py::str &path);
236 void train_export_checkpoint(const py::str &path);
237
238 // TODO Add other apis
239};
240
241} // namespace python
242} // namespace api
243} // namespace onert
244
245#endif // __ONERT_API_PYTHON_NNFW_API_WRAPPER_H__
int32_t type
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 APIImpl.cc:102
NNFW_STATUS nnfw_output_tensorinfo(nnfw_session *session, uint32_t index, nnfw_tensorinfo *tensor_info)
Get i-th output tensor info.
Definition APIImpl.cc:159
NNFW_STATUS nnfw_input_tensorinfo(nnfw_session *session, uint32_t index, nnfw_tensorinfo *tensor_info)
Get i-th input tensor info.
Definition APIImpl.cc:152
NNFW_STATUS nnfw_set_output(nnfw_session *session, uint32_t index, NNFW_TYPE type, void *buffer, size_t length)
Set output buffer.
Definition APIImpl.cc:109
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]