summaryrefslogtreecommitdiff
path: root/runtime/onert/api/include/nnfw.h
blob: 7d9fc048ff1d60a9bc65cd56a8b52b91cd2bcb62 (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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*
 * 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.
 */

/**
 * @file  nnfw.h
 * @brief This file describes runtime API
 */
#ifndef __NNFW_H__
#define __NNFW_H__

#include <stddef.h>
#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif

/**
 * @brief Session to query with runtime
 *
 * <p>nnfw_session is started and passed by calling {@link nnfw_create_session}.
 * Each session has its own inference environment, such as model to inference, backend usage, etc.
 *
 * <p>Load model by calling {@link nnfw_load_model_from_file}
 *
 * <p>After loading, prepare inference by calling {@link nnfw_prepare}.
 * Application can set runtime environment before prepare by calling
 * {@link nnfw_set_available_backends} and {@link nnfw_set_op_backend}, and it is optional.
 *
 * <p>Application can inference by calling {@link nnfw_run}.
 * Before inference, application has responsibility to set input tensor to set input data by calling
 * {@link nnfw_set_output}, and output tensor to get output by calling {@link nnfw_set_input}
 *
 * <p>To support input and output setting, application can get
 * input and output tensor information by calling<ul>
 * <li>{@link nnfw_input_size}</li>
 * <li>{@link nnfw_output_size}</li>
 * <li>{@link nnfw_input_tensorinfo}</li>
 * <li>{@link nnfw_output_tensorinfo}</li>
 * </ul>
 *
 * <p>Application can inference many times using one session,
 * but next inference can do after prior inference end
 *
 * <p>Application cannot use muitiple model using one session
 */
typedef struct nnfw_session nnfw_session;

/**
 * @brief Tensor types
 *
 * The type of tensor represented in {@link nnfw_tensorinfo}
 */
typedef enum {
  /** A tensor of 32 bit floating point */
  NNFW_TYPE_TENSOR_FLOAT32 = 0,
  /** A tensor of 32 bit signed integer */
  NNFW_TYPE_TENSOR_INT32 = 1,
  /**
   * A tensor of 8 bit integers that represent real numbers.
   *
   * real_value = (integer_value - zeroPoint) * scale.
   */
  NNFW_TYPE_TENSOR_QUANT8_ASYMM = 2,
  /** A tensor of boolean */
  NNFW_TYPE_TENSOR_BOOL = 3,
  /** A tensor of 8 bit unsigned integer */
  NNFW_TYPE_TENSOR_UINT8 = 4,
} NNFW_TYPE;

/**
 * @brief Result Values
 */
typedef enum {
  /** Successful */
  NNFW_STATUS_NO_ERROR = 0,
  /** Failed */
  NNFW_STATUS_ERROR = 1,
} NNFW_STATUS;

/**
 * @brief Data format of a tensor
 */
typedef enum {
  /** Don't care layout */
  NNFW_LAYOUT_NONE = 0,
  /**
   * Channel last layout
   * If rank is 4, layout is NHWC
   */
  NNFW_LAYOUT_CHANNELS_LAST = 1,
  /**
   * Channel first layout
   * If rank is 4, layout is NCHW
   */
  NNFW_LAYOUT_CHANNELS_FIRST = 2,
} NNFW_LAYOUT;

/**
 * @brief Information ID for retrieving information on nnfw (e.g. version)
 */
typedef enum {
  /** nnfw runtime version
   * Its value is uint32 in 0xMMmmmmPP, where MM = major, mmmm = minor, PP = patch.
   */
  NNFW_INFO_ID_VERSION = 0,
} NNFW_INFO_ID;

/**
 * @brief Maximum rank expressible with nnfw
 */
#define NNFW_MAX_RANK (6)

/**
 * @brief tensor info describes the type and shape of tensors
 *
 * <p>This structure is used to describe input and output tensors.
 * Application can get input and output tensor type and shape described in model by using
 * {@link nnfw_input_tensorinfo} and {@link nnfw_output_tensorinfo}
 *
 * <p>Maximum rank is 6 (NNFW_MAX_RANK). And tensor's dimension value is filled in 'dims' field from
 * index 0.
 * For example, if tensor's rank is 4,
 * application can get dimension value from dims[0], dims[1], dims[2], and dims[3]
 */
typedef struct nnfw_tensorinfo
{
  /** The data type */
  NNFW_TYPE dtype;
  /** The number of dimensions (rank) */
  int32_t rank;
  /**
   * The dimension of tensor.
   * Maximum rank is 6 (NNFW_MAX_RANK).
   */
  int32_t dims[NNFW_MAX_RANK];
} nnfw_tensorinfo;

/**
 * @brief Create a new session instance.
 *
 * <p>This only creates a session.
 * Model is loaded after {@link nnfw_load_model_from_file} is invoked.
 * And inference is performed after {@link nnfw_run} is invoked.
 *
 * <p>{@link nnfw_close_session} should be called once
 * if session is no longer need
 *
 * @param[out]  session The session to be created
 * @return      NNFW_STATUS_NO_ERROR if successful
 */
NNFW_STATUS nnfw_create_session(nnfw_session **session);

/**
 * @brief Close a session instance
 *
 * After called, access to closed session by application will be invalid
 *
 * @param[in] session The session to be closed
 * @return    @c NNFW_STATUS_NO_ERROR if successful
 */
NNFW_STATUS nnfw_close_session(nnfw_session *session);

/**
 * @brief     Load model from nnpackage file or directory
 *
 * @param[in] session           nnfw_session loading the given nnpackage file/dir
 * @param[in] package_file_path Path to the nnpackage file or unzipped directory to be loaded
 *
 * @return    @c NNFW_STATUS_NO_ERROR if successful
 */
NNFW_STATUS nnfw_load_model_from_file(nnfw_session *session, const char *package_file_path);

/**
 * @brief     Apply i-th input's tensor info to resize input tensor
 *
 * This function should be called before {@link nnfw_prepare} is invoked, and
 * should be called after {@link nnfw_load_model_from_file} is invoked
 * See {@link nnfw_prepare} for information applying updated tensor info
 * If this function is called many times for same index, tensor info is overwritten
 *
 * @param[in] session     Session to the input tensor info is to be set
 * @param[in] index       Index of input to be applied (0-indexed)
 * @param[in] tensor_info Tensor info to be applied
 * @return    @c NNFW_STATUS_NO_ERROR if successful, otherwise return @c NNFW_STATUS_ERROR
 */
NNFW_STATUS nnfw_apply_tensorinfo(nnfw_session *session, uint32_t index,
                                  nnfw_tensorinfo tensor_info);

/**
 * @brief     Prepare session to be ready for inference
 *
 * This phase may finalize model compilation, scheduling, and additional settings.
 * If {@link nnfw_apply_tensor} is called to apply input tensor info different with model
 * before this function, tries to resize all tensors.
 *
 * @param[in] session the session to be prepared
 * @return    @c NNFW_STATUS_NO_ERROR if successful, otherwise return @c NNFW_STATUS_ERROR
 */
NNFW_STATUS nnfw_prepare(nnfw_session *session);

/**
 * @brief     Run inference
 *
 * <p>This function should be called after model is loaded by {@link nnfw_load_model_from_file},
 * session is prepared for inference by {@link nnfw_prepare}, set input and output buffers
 * by {@link nnfw_set_input} and {@link nnfw_set_output}.</p>
 *
 * <p>This function return after inference is finished.</p>
 *
 * @param[in] session The session to run inference
 * @return    @c NNFW_STATUS_NO_ERROR if successful
 */
NNFW_STATUS nnfw_run(nnfw_session *session);

/**
 * @brief     Set input buffer
 *
 * This function should be called after {@link nnfw_prepare}, and before first inference
 * on session by {@link nnfw_run}. Application can reuse buffer for many inferences.
 *
 * @param[in] session Session to the input is to be set
 * @param[in] index   Index of input to be set (0-indexed)
 * @param[in] type    Type of the input
 * @param[in] buffer  Raw buffer for input
 * @param[in] length  Size of bytes of input buffer
 *
 * @return    @c 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);

/**
 * @brief       Set output buffer
 *
 * This function should be called after {@link nnfw_prepare}, and before first inference
 * on session by {@link nnfw_run}. Application can reuse buffer for many inferences.
 *
 * @param[in]   session Session from inference output is to be extracted
 * @param[in]   index   Index of output to be set (0-indexed)
 * @param[in]   type    Type of the output
 * @param[out]  buffer  Raw buffer for output
 * @param[in]   length  Size of bytes of output buffer
 *
 * @return      @c 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);

/**
 * @brief       Get the number of inputs
 *
 * Application can call this function to get number of inputs defined in loaded model.
 * This function should be called after {@link nnfw_load_model_from_file} is invoked to load model
 *
 * @param[in]   session Session from input information is to be extracted
 * @param[out]  number  Variable which the number of inputs is put into
 *
 * @return      @c NNFW_STATUS_NO_ERROR if successful
 */
NNFW_STATUS nnfw_input_size(nnfw_session *session, uint32_t *number);

/**
 * @brief       Get the number of outputs
 *
 * Application can call this function to get number of outputs defined in loaded model.
 * This function should be called after {@link nnfw_load_model_from_file} is invoked to load model
 *
 * @param[in]   session Session from output information is to be extracted
 * @param[out]  number  Variable which the number of outputs is put into
 *
 * @return      @c NNFW_STATUS_NO_ERROR if successful
 */
NNFW_STATUS nnfw_output_size(nnfw_session *session, uint32_t *number);

/**
 * @brief Set the layout of an input
 *
 * The input that does not call this has NNFW_LAYOUT_NHWC 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);

/**
 * @brief Set the layout of an output
 *
 * The output that does not call this has NNFW_LAYOUT_NHWC 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);

/**
 * @brief       Get i-th input tensor info
 *
 * <p>Before {@link nnfw_prepare} is invoked, this function return tensor info in model,
 * so updated tensor info by {@link nnfw_apply_tensorinfo} is not returned.</p>
 *
 * <p>After {@link nnfw_prepare} is invoked, this function return updated tensor info
 * if tensor info is updated by {@link nnfw_apply_tensorinfo}.</p>
 *
 * @param[in]   session     Session from input information is to be extracted
 * @param[in]   index       Index of input
 * @param[out]  tensor_info Tensor info (shape, type, etc)
 *
 * @return      @c NNFW_STATUS_NO_ERROR if successful
 */
NNFW_STATUS nnfw_input_tensorinfo(nnfw_session *session, uint32_t index,
                                  nnfw_tensorinfo *tensor_info);

/**
 * @brief     Get i-th output tensor info
 *
 * <p>Before {@link nnfw_prepare} is invoked, this function return tensor info in model,
 * so updated tensor info by {@link nnfw_apply_tensorinfo} is not returned.</p>
 *
 * <p>After {@link nnfw_prepare} is invoked, this function return updated tensor info
 * if tensor info is updated by {@link nnfw_apply_tensorinfo}.</p>
 *
 * @param[in]   session     Session from output information is to be extracted
 * @param[in]   index       Index of output
 * @param[out]  tensor_info Tensor info (shape, type, etc)
 *
 * @return      @c NNFW_STATUS_NO_ERROR if successful
 */
NNFW_STATUS nnfw_output_tensorinfo(nnfw_session *session, uint32_t index,
                                   nnfw_tensorinfo *tensor_info);

/**
 * @brief     Set available backends
 *
 * This function should be called before {@link nnfw_prepare} is invoked.
 *
 * <p>Supported backends differs on each platforms.
 * For example, `x86_64` supports "cpu" only.
 * Can set multiple backends by semicolon (ex: "acl_cl;cpu").
 * Among the multiple backends, the 1st element is used as default backend.</p>
 *
 * @note      Possible backend strings are: "cpu", "acl_cl", "acl_neon", "srcn"
 *
 * @param[in] session session to which avilable backends are set
 * @param[in] backends available backends on which nnfw uses
 *
 * @return @c NNFW_STATUS_NO_ERROR if successful
 */
NNFW_STATUS nnfw_set_available_backends(nnfw_session *session, const char *backends);

/**
 * @brief     Set the operation's backend
 *
 * This function should be called before {@link nnfw_prepare} is invoked.
 *
 * <p>Supported backends differs on each platforms.
 * For example, `x86_64` supports "cpu" only.
 * The backend for op has higher priority than available backends specified by
 * nnfw_set_available_backends.</p>
 *
 * @note      Possible backend strings are: "cpu", "acl_cl", "acl_neon"
 *
 * @param[in] session session to be modified
 * @param[in] op operation to be set
 * @param[in] backend bakcend on which operation run
 *
 * @return @c NNFW_STATUS_NO_ERROR if successful
 */
NNFW_STATUS nnfw_set_op_backend(nnfw_session *session, const char *op, const char *backend);

/**
 * @brief     Retrieve uint32 type of nnfw information for given information ID.
 *
 * <p>Retrieves the information of property given by information id </p>
 *
 * @note: The input session could be null for global information (e.g. runtime version).*
 *
 * @param[in] session session to be queried on.
 * @param[in] information ID to be queried
 * @param[out] val uint32 value to be returned.
 *
 * @return @c NNFW_STATUS_NO_ERROR if successful
 */
NNFW_STATUS nnfw_query_info_u32(nnfw_session *session, NNFW_INFO_ID id, uint32_t *val);

#ifdef __cplusplus
}
#endif

#endif