summaryrefslogtreecommitdiff
path: root/runtime/neurun/api/src/nnfw_api.cc
blob: bdac4c89b8154cd997e6bd27672192e3e800e539 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "nnfw_api_internal.h"

/*
 * Create a new session instance
 *
 * @param session the session to be created
 * @return NNFW_STATUS_NO_ERROR if successful
 */
NNFW_STATUS nnfw_create_session(nnfw_session **session)
{
  *session = new nnfw_session();

  return NNFW_STATUS_NO_ERROR;
}

/*
 * Close a session instance
 *
 * @param session the session to be closed
 * @return NNFW_STATUS_NO_ERROR if successful
 */
NNFW_STATUS nnfw_close_session(nnfw_session *session)
{
  delete session;
  return NNFW_STATUS_NO_ERROR;
}

#define NNFW_RETURN_ERROR_IF_NULL(p) \
  do                                 \
  {                                  \
    if ((p) == NULL)                 \
      return NNFW_STATUS_ERROR;      \
  } while (0)

/*
 * Load model from nnpackage file or directory
 *
 * @param session nnfw_session loading the given nnpackage file/dir
 * @param package_file_path path to the nnpackage file or unzipped directory to be loaded
 *
 * @return NNFW_STATUS_NO_ERROR if successful
 */
NNFW_STATUS nnfw_load_model_from_file(nnfw_session *session, const char *pacakge_file_path)
{
  NNFW_RETURN_ERROR_IF_NULL(session);
  return session->load_model_from_file(pacakge_file_path);
}

/*
 * Prepare session to be ready for inference
 * This phase may finalize model compilation, scheduling, and additional settings.
 *
 * @param session the session to be prepared
 * @return NNFW_STATUS_NO_ERROR if successful
 */
NNFW_STATUS nnfw_prepare(nnfw_session *session)
{
  NNFW_RETURN_ERROR_IF_NULL(session);
  return session->prepare();
}

/*
 * Run inference
 *
 * @param session the session to run inference
 * @return NNFW_STATUS_NO_ERROR if successful
 */
NNFW_STATUS nnfw_run(nnfw_session *session)
{
  NNFW_RETURN_ERROR_IF_NULL(session);
  return session->run();
}

/*
 * Set input
 *
 * @param session session to the input is to be set
 * @param index index of input to be set (0-indexed)
 * @param type type of the input
 * @param buffer raw buffer for input
 * @param length size of bytes of output
 *
 * @return NNFW_STATUS_NO_ERROR if successful
 */

NNFW_STATUS nnfw_set_input(nnfw_session *session, uint32_t index, NNFW_TYPE type,
                           const void *buffer, size_t length)
{
  NNFW_RETURN_ERROR_IF_NULL(session);
  return session->set_input(index, type, buffer, length);
}

/*
 * Set output
 *
 * @param session session from inference output is to be extracted
 * @param index index of output to be set (0-indexed)
 * @param type type of the output
 * @param buffer raw buffer for output
 * @param length size of bytes of output
 *
 * @return NNFW_STATUS_NO_ERROR if successful
 */

NNFW_STATUS nnfw_set_output(nnfw_session *session, uint32_t index, NNFW_TYPE type, void *buffer,
                            size_t length)
{
  NNFW_RETURN_ERROR_IF_NULL(session);
  return session->set_output(index, type, buffer, length);
}

/*
 * Get the number of inputs
 *
 * @param[in] session session from input information is to be extracted
 * @param[out] number variable which the number of inputs is put into
 *
 * @return NNFW_STATUS_NO_ERROR if successful
 */

NNFW_STATUS nnfw_input_size(nnfw_session *session, uint32_t *number)
{
  NNFW_RETURN_ERROR_IF_NULL(session);
  return session->input_size(number);
}

/*
 * Get the number of outputs
 *
 * @param[in] session session from output information is to be extracted
 * @param[out] number variable which the number of outputs is put into
 *
 * @return NNFW_STATUS_NO_ERROR if successful
 */
NNFW_STATUS nnfw_output_size(nnfw_session *session, uint32_t *number)
{
  NNFW_RETURN_ERROR_IF_NULL(session);
  return session->output_size(number);
}

/*
 * Set the layout of an input
 * @note The input that does not call this has NNFW_LAYOUT_CHANNELS_LAST layout
 *
 * @param[in] session session from inference input is to be extracted
 * @param[in] index   index of input to be set (0-indexed)
 * @param[in] layout  layout to set to target input
 *
 * @return NNFW_STATUS_NO_ERROR if successful
 */
NNFW_STATUS nnfw_set_input_layout(nnfw_session *session, uint32_t index, NNFW_LAYOUT layout)
{
  NNFW_RETURN_ERROR_IF_NULL(session);
  return session->set_input_layout(index, layout);
}

/*
 * Set the layout of an output
 * @note The output that does not call this has NNFW_LAYOUT_CHANNELS_LAST layout
 *
 * @param[in] session session from inference output is to be extracted
 * @param[in] index   index of output to be set (0-indexed)
 * @param[in] layout  layout to set to target output
 *
 * @return NNFW_STATUS_NO_ERROR if successful
 */
NNFW_STATUS nnfw_set_output_layout(nnfw_session *session, uint32_t index, NNFW_LAYOUT layout)
{
  NNFW_RETURN_ERROR_IF_NULL(session);
  return session->set_output_layout(index, layout);
}

/*
 * Get i-th input tensor info
 *
 * @param[in] session session from input information is to be extracted
 * @param[in] index index of input
 * @param[out] tensor_info nnfw_tensor_info
 *
 * @return NNFW_STATUS_NO_ERROR if successful
 */
NNFW_STATUS nnfw_input_tensorinfo(nnfw_session *session, uint32_t index,
                                  nnfw_tensorinfo *tensor_info)
{
  NNFW_RETURN_ERROR_IF_NULL(session);
  return session->input_tensorinfo(index, tensor_info);
}

/*
 * Get i-th output tensor info
 *
 * @param[in] session session from output information is to be extracted
 * @param[in] index index of output
 * @param[out] tensor_info nnfw_tensor_info
 *
 * @return NNFW_STATUS_NO_ERROR if successful
 */
NNFW_STATUS nnfw_output_tensorinfo(nnfw_session *session, uint32_t index,
                                   nnfw_tensorinfo *tensor_info)
{
  NNFW_RETURN_ERROR_IF_NULL(session);
  return session->output_tensorinfo(index, tensor_info);
}

/*
 * Register custom operation
 * @param session session to register this operation
 * @param id operation id
 * @param info registration info ( eval function, etc. )
 * @return NNFW_STATUS_NO_ERROR if successful
 */
NNFW_STATUS nnfw_register_custom_op_info(nnfw_session *session, const char *id,
                                         custom_kernel_registration_info *info)
{
  NNFW_RETURN_ERROR_IF_NULL(session);
  return session->register_custom_operation(id, info->eval_function);
}

NNFW_STATUS nnfw_apply_tensorinfo(nnfw_session *session, uint32_t index,
                                  nnfw_tensorinfo tensor_info)
{
  NNFW_RETURN_ERROR_IF_NULL(session);
  return session->apply_tensorinfo(index, tensor_info);
}

/*
 * Set available backends
 *
 * @param[in] session session to which a avilable backends are set
 * @param[in] backends available backends on which nnfw uses
 */
NNFW_STATUS nnfw_set_available_backends(nnfw_session *session, const char *backends)
{
  NNFW_RETURN_ERROR_IF_NULL(session);
  return session->set_available_backends(backends);
}

/*
 * Set the operation's backend
 *
 * @param[in] session session to be modified
 * @param[in] op operation to be set
 * @param[in] backend bakcend on which operation run
 *
 * @return NNFW_STATUS_NO_ERROR if successful
 */
NNFW_STATUS nnfw_set_op_backend(nnfw_session *session, const char *op, const char *backend)
{
  NNFW_RETURN_ERROR_IF_NULL(session);
  return session->set_op_backend(op, backend);
}