Rockchip · AI & Linux Hub

AI inference

RKNN Toolkit Lite2 V2.5.0, part 3: model loading, encryption and Runtime initialization

RKNN Toolkit Lite2 V2.5.0, part 3: model loading, encryption and Runtime initialization · RKNN2 V2.5.0

· ArmSoM Editorial · Rockchip source documentation · 6 min read

Platform reference for RKNN Toolkit Lite2 V2.5.0, Debian 10/11/12 aarch64 and the SoCs listed in the source. For an ArmSoM board, verify SoC, architecture, Python wheel, BSP NPU driver and Runtime compatibility. No hardware tests were performed. The source encryption section lists more platforms than the Lite2 installation table; both are retained without extending Lite2 device support by inference.

Adapted from the local Chinese and English V2.5.0 sources, retaining section numbers. Chinese uses Chinese figures; English, Russian and German use English-source figures. PDF wraps, continued tables and code indentation are restored. Example outputs are not ArmSoM tests.

Rockchip source

RKNN Toolkit Lite2 V2.5.0, part 1: installation, ResNet18 and return conventions

RKNN Toolkit Lite2 V2.5.0, part 2: lifecycle, input layout and output reuse

RKNN Toolkit Lite2 V2.5.0, part 3: model loading, encryption and Runtime initialization

RKNN Toolkit Lite2 V2.5.0, part 4: inference, device and version query APIs

RKNN Toolkit Lite2 V2.5.0, part 5: error checks, Core Mask and logging

3. RKNNLite Class

RKNNLite is the main Python class in RKNN Toolkit Lite2. It provides object initialization, RKNN model loading, device enumeration, Runtime initialization, model inference, SDK version query, model supported-platform query, and resource release.

3.1 Object Initialization and Release

init

API RKNNLite(…)
Function Initialize an RKNNLite object. Detailed logging can be controlled through verbose and
verbose_file.
Parameters verbose: Specifies whether detailed log information is printed to the screen.
  verbose_file: Specifies the log file path. When this parameter is set and
verbose=True, logs are also written to the specified file.
Return
Value
RKNNLite object.

Example:

# Show detailed logs on screen and save them to inference.log.
rknn_lite = RKNNLite(
    verbose=True,
    verbose_file='./inference.log'
)

# Only show detailed logs on screen.
rknn_lite = RKNNLite(verbose=True)

release

API release
Function Release resources used by the RKNNLite object after use.
Parameters None
Return Value None.

Example:

rknn_lite.release()

Usage Notes:

Call release() after the RKNNLite object is no longer needed.

Finish model inference and related query operations before releasing the object.

3.2 Model Loading and Runtime Initialization

load_rknn

API load_rknn
Function Load an RKNN model.
Parameters path: Path of the RKNN model file.
  key_path: (Optional) Path of the key-envelope file for an encrypted model. This
parameter is required when loading an RKCE-encrypted model and can be omitted for a
plaintext model.
Return
Value
Success: 0; failure: -1.

Example (plaintext model):

# Load resnet_18.rknn from the current directory.
ret = rknn_lite.load_rknn('./resnet_18.rknn')

Example (encrypted model):

# Load an encrypted model together with its key envelope.
ret = rknn_lite.load_rknn(
    './encrypted_model.rknn',
    key_path='./encrypted_model_key.bin'
)

Usage Notes:

Call load_rknn() before init_runtime() .

The model file should be an RKNN model generated by RKNN Toolkit2.

Encrypted Model Description:

RKNN Toolkit Lite2 supports loading encrypted RKNN models. Encrypted models use a hybrid encryption scheme (AES-256-CTR + RSA-OAEP): an RKCE identifier is written into the model file header, the model body is encrypted with AES-CTR, and the AES key is wrapped by RSA-OAEP into a key envelope (RKKE format) that is deployed to the device together with the encrypted model.

When loading an encrypted model, pass the key-envelope file through the key_path parameter. At Runtime, rknn_init() automatically detects the RKCE identifier and transparently performs decryption. After decryption, the subsequent inference flow is the same as for a plaintext model.

Encrypted models are generated on a PC/server using the encryption tool provided by RKNN Toolkit2 (the RK public key rk_public_key.pem is required). Encryption produces two companion files:

File Description
xxx.rknn Encrypted RKNN model (RKCE identifier + AES-CTR-encrypted body)
xxx_key.bin RSA-OAEP-encrypted key envelope (deployed together with the encrypted model)

Encrypted models are supported on the RK3562, RK3566 series, RK3568 series, RK3576 series, RK3588 series, RV1126B, RV1103B series, and RV1106B series.

Notes:

The encrypted model and its key envelope must be used as a matched pair; neither can be omitted.

The key envelope contains key material required to decrypt the model and should be properly protected from disclosure.

For more details about the encryption scheme and encryption tool, see the “Model Encryption” section in RKNN2 SDK Development Guide.

init_runtime

API init_runtime
Function Initialize the Runtime environment. This API must be called before inference.
Parameters target: Target platform. Default: None. When using the local NPU on an aarch64 target
device, this is normally left as None.
  device_id: Device ID. Default: None. Use this only when the current runtime
environment explicitly supports device selection.
  async_mode: Whether to enable asynchronous mode. Default: False.
  core_mask: NPU Core working mode. Default: RKNNLite.NPU_CORE_AUTO.
Return
Value
Returns 0 on success; returns -1 if prerequisites are not met or Runtime initialization
fails.

core_mask can use the following constants defined by the RKNNLite class:

core_mask Value Description
RKNNLite.NPU_CORE_AUTO 0 Automatic mode. Default value.
RKNNLite.NPU_CORE_0 1 Core 0 mode.
RKNNLite.NPU_CORE_1 2 Core 1 mode.
RKNNLite.NPU_CORE_2 4 Core 2 mode.
RKNNLite.NPU_CORE_0_1 3 Core 0/1 combined mode.
RKNNLite.NPU_CORE_0_1_2 7 Core 0/1/2 combined mode. The current interface description marks
this as supported only on RK3588.
RKNNLite.NPU_CORE_ALL 0xffff All NPU Cores.

The source code notes that core_mask is valid only for RK3576 / RK3588.

Example:

ret = rknn_lite.init_runtime(
    core_mask=RKNNLite.NPU_CORE_AUTO
)
if ret != 0:
    print('Init runtime environment failed')
    exit(ret)

Usage Notes:

Call load_rknn() first. If no model has been loaded, the API returns -1 directly.

If a Runtime already exists on the object, the old Runtime is released before reinitialization.

The API returns -1 if an exception occurs during Runtime creation, model graph construction, Core Mask setup, or version checking.

inference() can only be called after init_runtime() succeeds. get_sdk_version() also requires the Runtime to be initialized.

Explore this topic → · Learning paths →