Platform scope and source
This article follows the RKNN V2.5.0 vendor PDF. Only the chips and interfaces listed in the source are in scope. For an ArmSoM board, check the SoC, NPU driver, Toolkit/Runtime, SDK/BSP and kernel versions first. No ArmSoM hardware validation has been performed; source examples and measurements are not ArmSoM test results.
Series navigation
- Platforms, headers and linking
- Memory ownership and general/zero-copy flows
- Device, context and initialization flags
- Model, tensor and dynamic shape queries
- Input, execution, output and dynamic shapes
- Tensor memory allocation and synchronization
- Matrix multiplication APIs and dynamic shapes
- Custom operator registration and attributes
- Status codes, tensors, memory and extension types
- Matrix multiplication structures
- Custom operator types and deprecated API migration
5.3. Inference Execution
Set input, execute inference, get output and dynamic shape setting.
rknn_inputs_set
The input data of the model can be set by the rknn_inputs_set function. This function can support multiple inputs, each of which is a rknn_input structure object. The user needs to set these object field before passing in rknn_inputs_set function (Note: unavailable on RV1106/RV1103/RV1103B/RV1106B/RK2118).
| API | rknn_inputs_set |
|---|---|
| Description | Set the model input data. |
| Parameter | rknn_context context: The object of rknn_contex. |
| Parameter | uint32_t n_inputs: Number of inputs. |
| Parameter | rknn_input inputs[]: Array of rknn_input. |
| Return | int: Error code (See Status Codes). |
Sample Code:
rknn_input inputs[1];
memset(inputs, 0, sizeof(inputs));
inputs[0].index = 0;
inputs[0].type = RKNN_TENSOR_UINT8;
inputs[0].size = img_width*img_height*img_channels;
inputs[0].fmt = RKNN_TENSOR_NHWC;
inputs[0].buf = in_data;
inputs[0].pass_through = 0;
ret = rknn_inputs_set(ctx, 1, inputs);
rknn_run
The rknn_run function will perform a model inference. The input data need to be configured by the rknn_inputs_set function or zero-copy interface before rknn_run is called.
| API | rknn_run |
|---|---|
| Description | Perform a model inference. |
| Parameter | rknn_context context: The object of rknn_contex. |
| Parameter | rknn_run_extend* extend: Reserved for extension. It is not used currently and accepted NULL only. |
| Return | int: Error code (See Status Codes). |
Sample Code:
ret = rknn_run(ctx, NULL);
rknn_outputs_get
The rknn_outputs_get function can get the output data from the model. This function can get multiple output data, each of which is a rknn_output structure object that needs to be created and initialized in turn before the function is called (Note: RV1106/RV1103/RV1106B/RV1103B/RK2118 unsupported) .
There are two ways to store buffers for output data:
- The user allocate and release buffers themselves. In this case, the rknn_output.is_prealloc needs to be set to 1, and the rknn_output.buf points to users’ allocated buffer.
- The other is allocated by rknn. At this time, the rknn_output .is_prealloc needs to be set to 0. After the function is executed, rknn_output.buf will be created and store the output data.
| API | rknn_outputs_get |
|---|---|
| Description | Get model inference output data. |
| Parameter | rknn_context context: The object of rknn_context. |
| Parameter | uint32_t n_outputs: Number of output. |
| Parameter | rknn_output outputs[]: Array of rknn_output. |
| Parameter | rknn_run_extend* extend: Reserved for extension, currently not used yet. Accepting NULL only. |
| Return | int: Error code (See Status Codes). |
Sample Code:
rknn_output outputs[io_num.n_output];
memset(outputs, 0, sizeof(outputs));
for (int i = 0; i < io_num.n_output; i++) {
outputs[i].index = i;
outputs[i].is_prealloc = 0;
outputs[i].want_float = 1;
}
ret = rknn_outputs_get(ctx, io_num.n_output, outputs, NULL);
rknn_outputs_release
The rknn_outputs_release function will release the relevant resources of the rknn_output object.
| API | rknn_outputs_release |
|---|---|
| Description | Release the rknn_output object. |
| Parameter | rknn_context context: rknn_context object. |
| Parameter | uint32_t n_outputs: Number of output. |
| Parameter | rknn_output outputs[]: The array of rknn_output to be released. |
| Return | int: Error code (See Status Codes). |
Sample Code:
ret = rknn_outputs_release(ctx, io_num.n_output, outputs);
rknn_set_input_shape (deprecated)
This interface has been deprecated. Please use the rknn_set_input_shapes interface to bind input shapes. The current version is unavailable. If you want to continue using this interface, please use version 1.5.0 SDK and refer to the usage guide document of version 1.5.0.
rknn_set_input_shapes
For dynamic shape input RKNN models, the currently used input shape must be specified before inference. The interface passes in the number of inputs and the rknn_tensor_attr array, which contains each input shape and corresponding data layout information. The index, name, shape (dims) and memory layout information (fmt) of each rknn_tensor_attr structure object must be filled. Other members of the rknn_tensor_attr structure do not need to be set. Before using this interface, you can use the rknn_query function to query the number of input shapes and the list of dynamic shapes supported by the RKNN model, and the shape of the input data is required to be in the list of input shapes supported by the model. When running for the first time or switching a new input shape every time, you need to call this interface to set a new shape, otherwise, you don’t need to call this interface repeatedly.
| API | rknn_set_input_shapes |
|---|---|
| Description | Set the input shape currently used by the model. |
| Parameter | rknn_context context: rknn_context object. |
| Parameter | uint32_t n_inputs: The number of input tensors。 |
| Parameter | rknn_tensor_attr *: The attribute array pointer of tensor to pass all the input shape information. The user needs to set the index, name, dims, fmt, and n_dims members of each input attribute structure, and other members do not need to be set. |
| Return | int: Error code (See Status Codes). |
Sample Code:
for (int i = 0; i < io_num.n_input; i++) {
for (int j = 0; j < input_attrs[i].n_dims; ++j) {
input_attrs[i].dims[j] = dyn_range[i].dyn_range[0][j];
}
}
ret = rknn_set_input_shapes(ctx, io_num.n_input, input_attrs);
if (ret < 0) {
fprintf(stderr, "rknn_set_input_shapes error! ret=%d\n", ret);
return -1;
}
Source and editorial notes
Technical chapters are split by use. Code, identifiers and parameters are retained. Chinese follows the Chinese source; English, Russian and German follow the English source. No invented figures are added to chapters without figures.
Source caveats: Chapter numbers and example output are retained; old SDK versions in example logs are not the current document version. The Runtime source jumps from chapter 3 to 5 and repeats query subsection numbers. Its rknn_init parameter table calls extend disabled, while later types describe user_key and shared-weight fields. The native dynamic-output prose uses RKNN_QUERY_CURRENT_OUTPUT_ATTR, but its example uses RKNN_QUERY_CURRENT_NATIVE_OUTPUT_ATTR. Some native NHWC/NC1HWC2 descriptions also confuse input and output. Verify against actual V2.5.0 headers and the target SDK; these are not validated behaviors. The RK2118 cross-reference says RKNN3_RK2118_Quick_Start.pdf, while the current directory contains an RKNN2 quick-start guide.
