Rover written with RUST
Programming RK3588's NPU with RUST
2025.11.05 12:41
This is what ended up running on Robot with brain equiped with NPU.
My current board is O-Pi 5 with Rockchip 3588s (it has 6 TOPS NPU).
---------------------------------------------------------------------------------------------------
Programming the Rockchip RK3588's NPU with Rust is achievable by leveraging the existing C/C++ interfaces provided by Rockchip's RKNN Runtime and wrapping them in Rust.
Steps to Program RK3588 NPU with Rust:
-
Understand RKNN Runtime:Rockchip provides the RKNN Runtime, a C/C++ library (
librknnrt.so) for deploying and running RKNN models on their NPU platforms. This is the primary interface for interacting with the NPU. -
Rust Bindings (rknpu2-rs):A community-driven project called
rknpu2-rsoffers unofficial Rust bindings for the RKNN framework. This crate provides a safe and idiomatic Rust interface to the underlying C library functions. -
Setup and Installation:
- Deploy
librknnrt.so: Obtain thelibrknnrt.soshared library from the official RKNN SDK or therknpu2repository (e.g.,runtime/<CHIP>/Linux/librknn_api/<ARCH>/librknnrt.so) and place it in a system library folder like/lib. - Clone and Build
rknpu2-rs:
- Deploy
git clone https://github.com/RoggeOhta/rknpu2-rs.git
cd rknpu2-rs
cargo build
cargo test
-
Model Conversion:Before running models on the NPU, they must be converted to the RKNN format using the
RKNN-Toolkit2(Python-based SDK). This process involves taking models from frameworks like TensorFlow or PyTorch and optimizing them for the Rockchip NPU. -
Inference with Rust:Once you have the RKNN model and the
rknpu2-rsbindings, you can load the model and perform inference using the Rust API. Therknpu2-rscrate will handle the interaction with thelibrknnrt.solibrary, allowing you to feed input data to the NPU and retrieve the results.
Key Considerations:
-
RKNN-Toolkit2:While the NPU interaction is in Rust, the model conversion step still relies on the Python-based
RKNN-Toolkit2. -
Driver Compatibility:Ensure your Rockchip RK3588 device has the correct NPU drivers installed (e.g., NPU driver 0.9.6 or higher).
-
Rust Toolchain:Use a compatible Rust toolchain, potentially a specific nightly version if required by the
rknpu2-rscrate or other dependencies. -
Official Documentation:Refer to the official Rockchip RKNN documentation for detailed information on model conversion, RKNN Runtime usage, and NPU specifics.