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#include "nnfw.h"
18
19#include <pybind11/stl.h>
20#include <pybind11/numpy.h>
21
22namespace py = pybind11;
23
37{
39 const char *dtype;
41 int32_t rank;
47};
48
56void ensure_status(NNFW_STATUS status);
57
64NNFW_LAYOUT getLayout(const char *layout = "");
65
72NNFW_TYPE getType(const char *type = "");
73
80const char *getStringType(NNFW_TYPE type);
81
90uint64_t num_elems(const nnfw_tensorinfo *tensor_info);
91
100py::list get_dims(const tensorinfo &tensor_info);
101
110void set_dims(tensorinfo &tensor_info, const py::list &array);
111
113{
114private:
115 nnfw_session *session;
116
117public:
118 NNFW_SESSION(const char *package_file_path, const char *backends);
120
121 void close_session();
122 void set_input_tensorinfo(uint32_t index, const tensorinfo *tensor_info);
123 void run();
124 void run_async();
125 void wait();
130 template <typename T> void set_input(uint32_t index, py::array_t<T> &buffer)
131 {
132 nnfw_tensorinfo tensor_info;
133 nnfw_input_tensorinfo(this->session, index, &tensor_info);
134 NNFW_TYPE type = tensor_info.dtype;
135 uint32_t input_elements = num_elems(&tensor_info);
136 size_t length = sizeof(T) * input_elements;
137
138 ensure_status(nnfw_set_input(session, index, type, buffer.request().ptr, length));
139 }
144 template <typename T> void set_output(uint32_t index, py::array_t<T> &buffer)
145 {
146 nnfw_tensorinfo tensor_info;
147 nnfw_output_tensorinfo(this->session, index, &tensor_info);
148 NNFW_TYPE type = tensor_info.dtype;
149 uint32_t output_elements = num_elems(&tensor_info);
150 size_t length = sizeof(T) * output_elements;
151
152 ensure_status(nnfw_set_output(session, index, type, buffer.request().ptr, length));
153 }
154 uint32_t input_size();
155 uint32_t output_size();
156 // process the input layout by receiving a string from Python instead of NNFW_LAYOUT
157 void set_input_layout(uint32_t index, const char *layout);
158 // process the output layout by receiving a string from Python instead of NNFW_LAYOUT
159 void set_output_layout(uint32_t index, const char *layout);
160 tensorinfo input_tensorinfo(uint32_t index);
161 tensorinfo output_tensorinfo(uint32_t index);
162};
tensorinfo output_tensorinfo(uint32_t index)
uint32_t output_size()
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 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)
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,...
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:141
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:134
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_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="")
py::list get_dims(const tensorinfo &tensor_info)
Get nnfw_tensorinfo->dims.
uint64_t num_elems(const nnfw_tensorinfo *tensor_info)
Get the total number of elements in nnfw_tensorinfo->dims.
void set_dims(tensorinfo &tensor_info, const py::list &array)
Set nnfw_tensorinfo->dims.
const char * getStringType(NNFW_TYPE type)
NNFW_STATUS
Result values returned from a call to an API function.
Definition onert-micro.h:86
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
tensor info describes the type and shape of tensors
int32_t dims[NNFW_MAX_RANK]
const char * dtype